Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.gethostbyname #2125

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/18/socket.rb
Expand Up @@ -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[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[8,16]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're not using the offsets here, but have the hardcoded values left over :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, since these should be interpreter as binary strings, it's probably better to use byteslice instead of String#[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops ! I missed one.
And i'll add #byteslice.

else
addresses << a[3]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
Expand Down
18 changes: 17 additions & 1 deletion lib/19/socket.rb
Expand Up @@ -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[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[8,16]
else
addresses << a[3]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
Expand Down
18 changes: 17 additions & 1 deletion lib/20/socket.rb
Expand Up @@ -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[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[8,16]
else
addresses << a[3]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
Expand Down
17 changes: 17 additions & 0 deletions rakelib/platform.rake
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/library/socket/socket/gethostbyname_spec.rb
Expand Up @@ -13,4 +13,14 @@
addr = Socket.gethostbyname('<any>').first;
addr.should == "0.0.0.0"
end

it "returns address list in pack format (IPv4)" do
laddr = Socket.gethostbyname('127.0.0.1')[3..-1];
laddr.should == ["\x7f\x00\x00\x01"]
end

it "returns address list in pack format (IPv6)" do
laddr = Socket.gethostbyname('::1')[3..-1]
laddr.should == ["\x00" * 15 + "\x01"]
end
end