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
-
#bottom ⇒ Object
get the lowest address in the range defined by the netmask.
-
#choice ⇒ Object
get a random address from within the range defined by the netmask.
-
#contains(i) ⇒ Object
(also: #contains?)
test if the address provided is in the range defined by the netmask.
-
#inet_addr(str) ⇒ Object
convert a string to IP.
-
#random(mask = 0xFFFFFFFF) ⇒ Object
(also: #random!)
randomize the “host” part of the IP address (destructive).
-
#set_int(ip) ⇒ Object
make an IP address take the int32 value provided.
-
#to_cidr_s ⇒ Object
to_s with a cidr prefix at the end.
-
#to_mlen ⇒ Object
get the mask length.
-
#top ⇒ Object
get the highest address in the range defined by the netmask.
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
#bottom ⇒ Object
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 |
#choice ⇒ Object
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_s ⇒ Object
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_mlen ⇒ Object
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 |