Module: BetterIpaddr::InstanceMethods
Constant Summary
Constants included from Constants
Constants::FAMILY_TO_BIT_LENGTH, Constants::NETMASK_TO_PREFIX_LENGTH, Constants::PREFIX_LENGTH_TO_NETMASK, Constants::SYMBOL_TO_FAMILY
Instance Attribute Summary collapse
-
#family ⇒ Integer
readonly
Return the magic number representing the address family.
-
#mask_addr ⇒ Integer
readonly
Return the integer representation of the netmask.
Class Method Summary collapse
Instance Method Summary collapse
-
#+(offset) ⇒ IPAddr
Return the address greater than the original address by the given offset.
-
#-(offset) ⇒ IPAddr
Return the address less than the original address by the given offset.
-
#<=>(other) ⇒ Integer
Compare this address with the integer representation of another address of the same address family.
-
#==(other) ⇒ Boolean
Test the equality of two IP addresses, or an IP address an integer representing an address in the same address family.
-
#[](offset) ⇒ IPAddr
The address at the given offset relative to the network address of the network.
-
#address_family_bit_length ⇒ Integer
Returns the number of bits allowed by the address family.
-
#base(full: false) ⇒ String
Returns a string representation of the address without a prefix length.
- #better_to_s(cidr: false, full: false) ⇒ Object
-
#cidr(cidr: nil, full: false) ⇒ String
Return a string containing the CIDR representation of the address.
-
#cover?(other) ⇒ Boolean
Test whether or not this address completely encloses the other address.
-
#each ⇒ Enumerator
Return an enumerator with the behavior described above.
-
#first ⇒ IPAddr
The first host address in the network.
-
#grow(shift) ⇒ IPAddr
Return a new address with the prefix length reduced by the given amount.
-
#host? ⇒ Boolean
Return true if the address represents a host (i.e., only one address).
-
#last ⇒ IPAddr
(also: #broadcast)
Return the last address in the network, which by convention is the broadcast address in IP networks.
-
#netmask ⇒ String
Return the string representation of the netmask.
-
#overlap?(other) ⇒ Boolean
Test whether or not two networks have any addresses in common (i.e., if either entirely encloses the other).
-
#prefix_length ⇒ Integer
(also: #prefixlen)
Return the prefix length.
-
#shrink(shift) ⇒ Boolean
Return a new address with the prefix length increased by the given amount.
-
#size ⇒ Integer
Return the number of host addresses representable by the network given its size.
-
#summarize_with(other) ⇒ IPAddr?
Returns a summary address if the two networks can be combined into a single network without covering any other networks.
-
#to_range ⇒ Range(IPAddr)
Return a range representing the network.
-
#wildcard ⇒ IPAddr
Return a wildcard mask representing the network.
Instance Attribute Details
#family ⇒ Integer (readonly)
Return the magic number representing the address family.
57 58 59 |
# File 'lib/better_ipaddr/methods.rb', line 57 def family @family end |
#mask_addr ⇒ Integer (readonly)
Return the integer representation of the netmask.
61 62 63 |
# File 'lib/better_ipaddr/methods.rb', line 61 def mask_addr @mask_addr end |
Class Method Details
Instance Method Details
#+(offset) ⇒ IPAddr
Return the address greater than the original address by the given offset. Note that the result will always be a host address.
76 77 78 |
# File 'lib/better_ipaddr/methods.rb', line 76 def +(offset) self.class.new(@addr + offset, family) end |
#-(offset) ⇒ IPAddr
Return the address less than the original address by the given offset. Note that the result will always be a host address.
87 88 89 |
# File 'lib/better_ipaddr/methods.rb', line 87 def -(offset) self + (-offset) end |
#<=>(other) ⇒ Integer
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/better_ipaddr/methods.rb', line 103 def <=>(other) if other.is_a?(IPAddr) family_difference = family <=> other.family return family_difference unless family_difference == 0 elsif !other.is_a?(Integer) fail ArgumentError, "Can't compare #{self.class} with #{other.class}" end address_difference = to_i <=> other.to_i if address_difference != 0 || !other.is_a?(IPAddr) return address_difference end other.instance_variable_get(:@mask_addr) <=> @mask_addr end |
#==(other) ⇒ Boolean
Test the equality of two IP addresses, or an IP address an integer representing an address in the same address family.
Returns false for any other type.
128 129 130 |
# File 'lib/better_ipaddr/methods.rb', line 128 def ==(other) (other.is_a?(IPAddr) || other.is_a?(Integer)) && (self <=> other) == 0 end |
#[](offset) ⇒ IPAddr
The address at the given offset relative to the network address of the network. A negative offset will be used to count backwards from the highest addresses within the network. desired address
139 140 141 142 143 |
# File 'lib/better_ipaddr/methods.rb', line 139 def [](offset) return self if offset.zero? && host? offset2 = offset >= 0 ? offset : size + offset self.class[to_i + offset2, family: family] end |
#address_family_bit_length ⇒ Integer
Returns the number of bits allowed by the address family. A more efficient form of this method is available to the specialized IPAddr child classes.
151 152 153 |
# File 'lib/better_ipaddr/methods.rb', line 151 def address_family_bit_length FAMILY_TO_BIT_LENGTH.fetch(family) end |
#base(full: false) ⇒ String
Returns a string representation of the address without a prefix length.
159 160 161 162 163 164 165 |
# File 'lib/better_ipaddr/methods.rb', line 159 def base(full: false) if full to_string else stdlib_to_s end end |
#better_to_s(cidr: false, full: false) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/better_ipaddr/methods.rb', line 167 def better_to_s(cidr: false, full: false) if cidr "#{base(full: full)}/#{prefix_length}" else base(full: full) end end |
#cidr(cidr: nil, full: false) ⇒ String
Return a string containing the CIDR representation of the address.
179 180 181 |
# File 'lib/better_ipaddr/methods.rb', line 179 def cidr(cidr: nil, full: false) better_to_s(cidr: true, full: full) end |
#cover?(other) ⇒ Boolean
Test whether or not this address completely encloses the other address.
185 186 187 |
# File 'lib/better_ipaddr/methods.rb', line 185 def cover?(other) first <= other.first && other.last <= last end |
#each ⇒ Enumerator
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/better_ipaddr/methods.rb', line 200 def each if block_given? (0...size).each do |offset| yield self[offset] end self else enum_for(:each) end end |
#first ⇒ IPAddr
The first host address in the network.
214 215 216 |
# File 'lib/better_ipaddr/methods.rb', line 214 def first self[0] end |
#grow(shift) ⇒ IPAddr
Return a new address with the prefix length reduced by the given amount. The new address will cover the original address.
223 224 225 |
# File 'lib/better_ipaddr/methods.rb', line 223 def grow(shift) mask(prefix_length - shift) end |
#host? ⇒ Boolean
Return true if the address represents a host (i.e., only one address).
229 230 231 |
# File 'lib/better_ipaddr/methods.rb', line 229 def host? prefix_length >= address_family_bit_length end |
#last ⇒ IPAddr Also known as: broadcast
Return the last address in the network, which by convention is the broadcast address in IP networks.
237 238 239 |
# File 'lib/better_ipaddr/methods.rb', line 237 def last self[-1] end |
#netmask ⇒ String
Return the string representation of the netmask.
65 66 67 |
# File 'lib/better_ipaddr/methods.rb', line 65 def netmask self.class[mask_addr].to_s end |
#overlap?(other) ⇒ Boolean
Test whether or not two networks have any addresses in common (i.e., if either entirely encloses the other).
246 247 248 |
# File 'lib/better_ipaddr/methods.rb', line 246 def overlap?(other) cover?(other) || other.cover?(self) end |
#prefix_length ⇒ Integer Also known as: prefixlen
Return the prefix length. A more efficient form of this method is available to the specialized IPAddr child classes.
255 256 257 |
# File 'lib/better_ipaddr/methods.rb', line 255 def prefix_length NETMASK_TO_PREFIX_LENGTH[family][mask_addr] end |
#shrink(shift) ⇒ Boolean
Return a new address with the prefix length increased by the given amount. The old address will cover the new address.
266 267 268 |
# File 'lib/better_ipaddr/methods.rb', line 266 def shrink(shift) mask(prefix_length + shift) end |
#size ⇒ Integer
Return the number of host addresses representable by the network given its size.
274 275 276 |
# File 'lib/better_ipaddr/methods.rb', line 274 def size 2**(address_family_bit_length - prefix_length) end |
#summarize_with(other) ⇒ IPAddr?
Returns a summary address if the two networks can be combined into a single network without covering any other networks. Returns nil if the two networks can’t be combined this way.
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/better_ipaddr/methods.rb', line 283 def summarize_with(other) if other.nil? nil elsif cover?(other) self elsif other.cover?(self) other elsif other.grow(1) == grow(1) grow(1) end end |
#to_range ⇒ Range(IPAddr)
Return a range representing the network. A block can be given to specify a conversion procedure, for example to convert the first and last addresses to integers before building the range.
300 301 302 303 304 305 306 |
# File 'lib/better_ipaddr/methods.rb', line 300 def to_range if block_given? (yield first)..(yield last) else first..last end end |
#wildcard ⇒ IPAddr
Return a wildcard mask representing the network.
311 312 313 |
# File 'lib/better_ipaddr/methods.rb', line 311 def wildcard _to_string(@mask_addr.to_i ^ (2**address_family_bit_length - 1)) end |