123456789_123456789_123456789_123456789_123456789_

Class: IPSocket

Relationships & Source Files
Inherits: Object
Defined in: lib/ipaddr.rb

Class Method Summary

Class Method Details

.getaddress(s)

Returns a String based representation of a valid DNS hostname, IPv4 or IPv6 address.

IPSocket.getaddress 'localhost'         #=> "::1"
IPSocket.getaddress 'broadcasthost'     #=> "255.255.255.255"
IPSocket.getaddress 'www.ruby-lang.org' #=> "221.186.184.68"
IPSocket.getaddress 'www.ccc.de'        #=> "2a00:1328:e102:ccc0::122"
[ GitHub ]

  
# File 'lib/ipaddr.rb', line 775

def getaddress(s)
  if valid_v6?(s)
    s
  else
    getaddress_orig(s)
  end
end

.getaddress_orig (private)

[ GitHub ]

  
# File 'lib/ipaddr.rb', line 764

alias getaddress_orig getaddress

.valid_v6?(addr) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/ipaddr.rb', line 745

def valid_v6?(addr)
  case addr
  when IPAddr::RE_IPV6ADDRLIKE_FULL
    if $2
      $~[2,4].all? {|i| i.to_i < 256 }
    else
      true
    end
  when IPAddr::RE_IPV6ADDRLIKE_COMPRESSED
    if $4
      addr.count(':') <= 6 && $~[4,4].all? {|i| i.to_i < 256}
    else
      addr.count(':') <= 7
    end
  else
    false
  end
end