class NetAddr::IPv4
IPv4 represents a single IPv4 address.
def cmp(other)
* 0 if the two are equal
* 1 if this IPv4 is numerically greater
cmp compares equality with another IPv4. Return:
def cmp(other) if (!other.kind_of?(IPv4)) raise ArgumentError, "Expected an IPv4 object for 'other' but got a #{other.class}." end if (self.addr > other.addr) return 1 elsif (self.addr < other.addr) return -1 end return 0 end
def initialize(i)
Create an IPv4 from an Integer. Must be between 0 and 2**32-1.
def initialize(i) if (!i.kind_of?(Integer)) raise ValidationError, "Expected an Integer for 'i' but got a #{i.class}." elsif ( (i < 0) || (i > 2**32-1) ) raise ValidationError, "#{i} is out of bounds for IPv4." end @addr = i end
def multicast_mac
multicast_mac returns the EUI48 multicast mac-address for this IP.
def multicast_mac mac = 0 if (@addr&0xf0000000 == 0xe0000000) # within 224.0.0.0/4 ? # map lower 23-bits of ip to 01:00:5e:00:00:00 mac = (@addr&0x007fffff) | 0x01005e000000 end return EUI48.new(mac) end
def next()
def next() if (self.addr == NetAddr::F32) return nil end return IPv4.new(self.addr + 1) end
def prev()
def prev() if (self.addr == 0) return nil end return IPv4.new(self.addr - 1) end
def to_net()
def to_net() NetAddr::IPv4Net.new(self,nil) end
def to_s()
def to_s() Util.int_to_IPv4(@addr) end
def version()
def version() return 4 end