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 2 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
12 changes: 11 additions & 1 deletion lib/18/socket.rb
Expand Up @@ -610,7 +610,17 @@ 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 sockaddr.length == 16
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't just check the length and thus assume a certain layout. It's better to switch on the family, whether it's AF_INET or AF_INET6

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point.

# IPv4 address
addresses << sockaddr[4, 4]
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also find another way of know which bytes are what. We can't be sure about the exact byte layouts of these so we shouldn't hard code these offsets here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But these offsets are not magic : they come from C struct definitions:

/* man 7 ip */
           struct sockaddr_in {
               sa_family_t    sin_family;   /* AF_INET */
               in_port_t      sin_port;      /* port number */
               struct in_addr sin_addr;   /* internet address */
           };

           struct in_addr {
               uint32_t       s_addr;
           };

and

/* man 7 ipv6 */
           struct sockaddr_in6 {
               sa_family_t     sin6_family;   /* AF_INET6 */
               in_port_t       sin6_port      /* port number */;
               uint32_t        sin6_flowinfo; /* flow information */
               struct in6_addr sin6_addr;     /* IPv6 adress */
               uint32_t        sin6_scope_id; /* Scope ID */
           };

           struct in6_addr {
               unsigned char   s6_addr[16];
           };

Copy link
Contributor

Choose a reason for hiding this comment

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

That's exactly my point :). We should get these offsets from struct definitions for these types and not hard code these offsets. They might be different due to platform alignment, type sizes etc. that's why we should depend on what information the platform supplies for these types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. So we can use Socket::SockAddr_In. SockAddr_In#offset_of gives offset of sockaddr_in* fields. But i don't known how to get their size.

else
# Ipv6 address
addresses << sockaddr[8, 16]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
Expand Down
12 changes: 11 additions & 1 deletion lib/19/socket.rb
Expand Up @@ -687,7 +687,17 @@ 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 sockaddr.length == 16
# IPv4 address
addresses << sockaddr[4, 4]
else
# Ipv6 address
addresses << sockaddr[8, 16]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
Expand Down
12 changes: 11 additions & 1 deletion lib/20/socket.rb
Expand Up @@ -687,7 +687,17 @@ 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 sockaddr.length == 16
# IPv4 address
addresses << sockaddr[4, 4]
else
# Ipv6 address
addresses << sockaddr[8, 16]
end
end
end

[hostname, alternatives.uniq, family] + addresses.uniq
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