Permalink
Browse files

Modify end of Socket.gethostbyname to also convert IPv6 addresses into

packed strings. This code is taken from IPAddr#hton.
  • Loading branch information...
1 parent 20540b1 commit 90534f281e74d8f1421d2c82daae6dc8b95616e6 @sdaubert sdaubert committed Jan 3, 2013
Showing with 21 additions and 5 deletions.
  1. +21 −5 lib/19/socket.rb
View
@@ -691,12 +691,28 @@ def self.gethostbyname(hostname)
end
addresses.map! do |addr|
- if addr =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/
- s = ''
- $~[1..4].each { |v| s << v.to_i.chr }
- s
+ case addr
+ when /^\d+\.\d+\.\d+\.\d+$/
+ # IPv4 address
+ addr.split('.').map(&:to_i).pack('C*')
+ when /^::(ffff:)?(\d+\.\d+\.\d+\.\d+)$/i
+ # IPv4-mapped and IPv4-compatible IPv6 address
+ constant_bytes = "\x00" * 10
+ if $1
+ constant_bytes += "\xff" * 2
+ else
+ constant_bytes += "\x00" * 2
+ end
+ constant_bytes + $2.split('.').map(&:to_i).pack('C*')
else
- addr
+ # IPv6 address
+ left, right = addr.split('::')
+ right ||= ''
+ l = left.split(':')
+ r = right.split(':')
+ rest = 8 - l.size - r.size
+ ary = l + ['0'] * rest + r
+ ary.map { |v| v.to_i(16) }.pack('n8')
end
end

0 comments on commit 90534f2

Please sign in to comment.