Skip to content

Commit

Permalink
* lib/ipaddr.rb (IPSocket::getaddress): merge usa's patch.
Browse files Browse the repository at this point in the history
  [ruby-dev:21678]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5208 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
eban committed Dec 18, 2003
1 parent 3270fda commit 60f87fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Thu Dec 18 15:27:59 2003 WATANABE Hirofumi <[email protected]>

* lib/ipaddr.rb (IPSocket::getaddress): merge usa's patch.
[ruby-dev:21678]

Wed Dec 17 15:15:30 2003 Yukihiro Matsumoto <[email protected]>

* lib/cgi.rb (CGI::QueryExtension::Value::[]): should work like
Expand Down
56 changes: 53 additions & 3 deletions lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,41 @@
class Socket
AF_INET6 = Object.new
end

class << IPSocket
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

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

def valid?(addr)
valid_v4?(addr) || valid_v6?(addr)
end

alias getaddress_orig getaddress
def getaddress(s)
/^::/ =~ s ? s : getaddress_orig(s)
if valid?(s)
s
elsif /\A[-A-Za-z\d.]+\Z/ =~ s
getaddress_orig(s)
else
raise ArgumentError, "invalid address"
end
end
end
end
Expand Down Expand Up @@ -162,7 +193,26 @@ def to_i

# Returns a string containing the IP address representation.
def to_s
return IPSocket.getaddress(to_string)
str = to_string
return str if ipv4?

str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
loop do
break if str.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
break if str.sub!(/\b0:0:0:0:0\b/, ':')
break if str.sub!(/\b0:0:0:0\b/, ':')
break if str.sub!(/\b0:0:0\b/, ':')
break if str.sub!(/\b0:0\b/, ':')
break
end

if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\Z/i =~ str
str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
end

str
end

# Returns a string containing the IP address representation in
Expand Down Expand Up @@ -368,7 +418,7 @@ def initialize(addr = '::', family = Socket::AF_UNSPEC)
# Socket::AI_NUMERICHOST)
begin
IPSocket.getaddress(prefix) # test if address is vaild
rescue
rescue ArgumentError
raise ArgumentError, "invalid address"
end
@addr = @family = nil
Expand Down

0 comments on commit 60f87fe

Please sign in to comment.