Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • ipaddr.rb

Class/Module Index [+]

Quicksearch

IPSocket

Public Class Methods

getaddress(s) click to toggle source
 
               # File ipaddr.rb, line 51
def getaddress(s)
  if valid?(s)
    s
  elsif /\A[-A-Za-z\d.]+\Z/ =~ s
    getaddress_orig(s)
  else
    raise ArgumentError, "invalid address"
  end
end
            
Also aliased as: getaddress_orig
getaddress_orig(s) click to toggle source
Alias for: getaddress
valid?(addr) click to toggle source
 
               # File ipaddr.rb, line 46
def valid?(addr)
  valid_v4?(addr) || valid_v6?(addr)
end
            
valid_v4?(addr) click to toggle source
 
               # File ipaddr.rb, line 26
def valid_v4?(addr)
  if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
    return $~.captures.all? {|i| i.to_i < 256}
  end
  return false
end
            
valid_v6?(addr) click to toggle source
 
               # File ipaddr.rb, line 33
def valid_v6?(addr)
  # IPv6 (normal)
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
  return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
  # IPv6 (IPv4 compat)
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_v4?($')
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
  return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')

  false
end