Method: NetAddr::IPv4Net#rel

Defined in:
lib/ipv4net.rb

#rel(other) ⇒ Object

rel determines the relationship to another IPv4Net. Returns:

  • 1 if this IPv4Net is the supernet of other

  • 0 if the two are equal

  • -1 if this IPv4Net is a subnet of other

  • nil if the networks are unrelated



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ipv4net.rb', line 157

def rel(other)
  if (!other.kind_of?(IPv4Net))
    raise ArgumentError, "Expected an IPv4Net object for 'other' but got a #{other.class}."
  end
  
  # when networks are equal then we can look exlusively at the netmask
  if (self.network.addr == other.network.addr)
    return self.netmask.cmp(other.netmask)
  end
  
  # when networks are not equal we can use hostmask to test if they are
  # related and which is the supernet vs the subnet
  hostmask = self.netmask.mask ^ NetAddr::F32
  otherHostmask = other.netmask.mask ^ NetAddr::F32
  if (self.network.addr|hostmask == other.network.addr|hostmask)
    return 1
  elsif (self.network.addr|otherHostmask == other.network.addr|otherHostmask)
    return -1
  end
  return nil
end