Module: IPAddr::IPAddrExtensions

Included in:
IPAddr
Defined in:
lib/ruckus/extensions/ipaddr.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



80
81
82
# File 'lib/ruckus/extensions/ipaddr.rb', line 80

def self.included(klass)
    klass.extend(ClassMethods)
end

Instance Method Details

#bottomObject

get the lowest address in the range defined by the netmask



116
117
118
119
120
# File 'lib/ruckus/extensions/ipaddr.rb', line 116

def bottom
    # XXX not used
    # XXX should use self.class instead of IPAddr
    IPAddr.inet_addr(self.to_i & self.mask_addr)
end

#choiceObject

get a random address from within the range defined by the netmask.



127
128
129
130
131
132
133
134
135
# File 'lib/ruckus/extensions/ipaddr.rb', line 127

def choice
    # XXX not used
    return self.clone if self.mask_addr == 0xFFFFFFFF

    span = self.top.to_i - self.bottom.to_i
    x = self.clone
    x.set_int(self.bottom.to_i + rand(span))
    return x
end

#contains(i) ⇒ Object Also known as: contains?

test if the address provided is in the range defined by the netmask



142
143
144
145
146
147
148
149
150
# File 'lib/ruckus/extensions/ipaddr.rb', line 142

def contains(i)
    # XXX not used
    # XXX should use self.class instead of IPAddr
    if not i.kind_of? IPAddr
        i = IPAddr.inet_addr i
    end

    i.to_i >= self.bottom.to_i and i.to_i <= self.top.to_i
end

#inet_addr(str) ⇒ Object

convert a string to IP



32
33
34
# File 'lib/ruckus/extensions/ipaddr.rb', line 32

def inet_addr(str)
    in_addr(str)
end

#random(mask = 0xFFFFFFFF) ⇒ Object Also known as: random!

randomize the “host” part of the IP address (destructive)



20
21
22
23
24
# File 'lib/ruckus/extensions/ipaddr.rb', line 20

def random(mask=0xFFFFFFFF)
    r = rand(0xFFFFFFFF) & mask
    i = self.to_i & (~mask)
    self.set_int(i | r)
end

#set_int(ip) ⇒ Object

make an IP address take the int32 value provided



12
13
14
# File 'lib/ruckus/extensions/ipaddr.rb', line 12

def set_int(ip)
    set(ip, Socket::AF_INET)
end

#to_cidr_sObject

to_s with a cidr prefix at the end



87
88
89
90
# File 'lib/ruckus/extensions/ipaddr.rb', line 87

def to_cidr_s
    # XXX not used
    "#{ to_s }/#{ self.class.mask2mlen(@mask_addr) }"
end

#to_mlenObject

get the mask length



96
97
98
99
100
# File 'lib/ruckus/extensions/ipaddr.rb', line 96

def to_mlen
    # XXX not used
    mask = self.to_i
    self.class.mask2mlen(mask)
end

#topObject

get the highest address in the range defined by the netmask



106
107
108
109
110
# File 'lib/ruckus/extensions/ipaddr.rb', line 106

def top
    # XXX not used
    # XXX should use self.class.inet_addr instead?
    IPAddr.inet_addr(self.to_i | (0xFFFFFFFF & (~self.mask_addr)))
end