-
Notifications
You must be signed in to change notification settings - Fork 588
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
Socket.gethostbyname #2125
Changes from 2 commits
45a1371
9bda567
77a888c
e10cc24
e956c23
f5e460f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
# IPv4 address | ||
addresses << sockaddr[4, 4] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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];
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.