Method: IPAddr.ntop

Defined in:
lib/ipaddr.rb

.ntop(addr) ⇒ Object

Convert a network byte ordered string form of an IP address into human readable form. It expects the string to be encoded in Encoding::ASCII_8BIT (BINARY).



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ipaddr.rb', line 114

def self.ntop(addr)
  if addr.is_a?(String) && addr.encoding != Encoding::BINARY
    raise InvalidAddressError, "invalid encoding (given #{addr.encoding}, expected BINARY)"
  end

  case addr.bytesize
  when 4
    addr.unpack('C4').join('.')
  when 16
    IN6FORMAT % addr.unpack('n8')
  else
    raise AddressFamilyError, "unsupported address family"
  end
end