Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update Socket.gethostbyname to respect two new specs.

Socket.gethostbyname is updated for all ruby versions.

Socket.sockaddr_in is used to transform addresses got from
Socket.getaddrinfo to packed strings. A new structure is added for IPv6
addresses so they can be transformed properly.
  • Loading branch information...
commit 385f88a78ebf6cf7d2a5a2393f4359a945b102aa 1 parent de72b4a
@sdaubert sdaubert authored dbussink committed
View
18 lib/18/socket.rb
@@ -610,7 +610,23 @@ def self.gethostbyname(hostname)
alternatives = []
addrinfos.each do |a|
alternatives << a[2] unless a[2] == hostname
- addresses << a[3] if a[4] == family
+ # transform addresses to packed strings
+ if a[4] == family
+ sockaddr = Socket.sockaddr_in(1, a[3])
+ if family == AF_INET
+ # IPv4 address
+ offset = FFI.config("sockaddr_in.sin_addr.offset")
+ size = FFI.config("sockaddr_in.sin_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ elsif family == AF_INET6
+ # Ipv6 address
+ offset = FFI.config("sockaddr_in6.sin6_addr.offset")
+ size = FFI.config("sockaddr_in6.sin6_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ else
+ addresses << a[3]
+ end
+ end
end
[hostname, alternatives.uniq, family] + addresses.uniq
View
18 lib/19/socket.rb
@@ -687,7 +687,23 @@ def self.gethostbyname(hostname)
alternatives = []
addrinfos.each do |a|
alternatives << a[2] unless a[2] == hostname
- addresses << a[3] if a[4] == family
+ # transform addresses to packed strings
+ if a[4] == family
+ sockaddr = Socket.sockaddr_in(1, a[3])
+ if family == AF_INET
+ # IPv4 address
+ offset = FFI.config("sockaddr_in.sin_addr.offset")
+ size = FFI.config("sockaddr_in.sin_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ elsif family == AF_INET6
+ # Ipv6 address
+ offset = FFI.config("sockaddr_in6.sin6_addr.offset")
+ size = FFI.config("sockaddr_in6.sin6_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ else
+ addresses << a[3]
+ end
+ end
end
[hostname, alternatives.uniq, family] + addresses.uniq
View
18 lib/20/socket.rb
@@ -687,7 +687,23 @@ def self.gethostbyname(hostname)
alternatives = []
addrinfos.each do |a|
alternatives << a[2] unless a[2] == hostname
- addresses << a[3] if a[4] == family
+ # transform addresses to packed strings
+ if a[4] == family
+ sockaddr = Socket.sockaddr_in(1, a[3])
+ if family == AF_INET
+ # IPv4 address
+ offset = FFI.config("sockaddr_in.sin_addr.offset")
+ size = FFI.config("sockaddr_in.sin_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ elsif family == AF_INET6
+ # Ipv6 address
+ offset = FFI.config("sockaddr_in6.sin6_addr.offset")
+ size = FFI.config("sockaddr_in6.sin6_addr.size")
+ addresses << sockaddr.byteslice(offset, size)
+ else
+ addresses << a[3]
+ end
+ end
end
[hostname, alternatives.uniq, family] + addresses.uniq
View
17 rakelib/platform.rake
@@ -58,6 +58,23 @@ file 'runtime/platform.conf' => deps do |task|
s.field :sin_zero, :char_array
end.write_config(f)
+ Rubinius::FFI::Generators::Structures.new 'sockaddr_in6' do |s|
+ if BUILD_CONFIG[:windows]
+ s.include "ws2tcpip.h"
+ else
+ s.include "netinet/in.h"
+ s.include "sys/socket.h"
+ end
+ s.include "fcntl.h"
+ s.include "sys/stat.h"
+ s.name 'struct sockaddr_in6'
+ s.field :sin6_family, :sa_family_t
+ s.field :sin6_port, :ushort
+ s.field :sin6_flowinfo
+ s.field :sin6_addr, :char_array
+ s.field :sin6_scope_id
+ end.write_config(f)
+
unless BUILD_CONFIG[:windows]
sockaddr_un = Rubinius::FFI::Generators::Structures.new 'sockaddr_un' do |s|
s.include "sys/un.h"
Please sign in to comment.
Something went wrong with that request. Please try again.