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
Conversation
Add two new specs to verify that addresses in address list returned by Socket.gethosbyname are in 'pack' format (one spec for IPv4 addresses an the other for IPv6 ones).
Socket.gethostbyname is updated for all ruby versions. Socket.sockaddr_in is used to transform addresses got from Socket.getaddrinfo to packed strings. For an IPv4 address, address is from 5th byte and is 4 bytes long. For an IPv6 address, address is from 8th byte and is 16 bytes long.
# transform addresses to packed strings | ||
if a[4] == family | ||
sockaddr = Socket.sockaddr_in(1, a[3]) | ||
if sockaddr.length == 16 |
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.
I have studied Rubinius::FFI, and I think I understand the problem :
I will try to do that this week. |
Yeah, that sounds like the direction I was also thinking about :). If you have any questions, don't hesitate to ask :). You can also try hopping into #rubinius on IRC to see if people are around there. |
* rakelib/platform.rake: Add new structure `sockaddr_in6` * lib/**/socket.rb : * Modify test to determine IP version. Use family instead of sockaddr string size. * Use platform configuration to determine offset and size of IP address in sockaddr string. `sockaddr_in` and `sockaddr_in6` platform structures are used for this purpose.
Yeah, it works ! |
# Ipv6 address | ||
offset = FFI.config("sockaddr_in6.sin6_addr.offset") | ||
size = FFI.config("sockaddr_in6.sin6_addr.size") | ||
addresses << sockaddr[8,16] |
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.
Looks like you're not using the offsets here, but have the hardcoded values left over :)
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.
BTW, since these should be interpreter as binary strings, it's probably better to use byteslice instead of String#[]
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.
Oops ! I missed one.
And i'll add #byteslice.
* Remove hard-coded offset and size for IPv6. * Replace String#[] by String#byteslice to get address from sockaddr string.
So, one final question, could you perhaps squash the implementation commits into a single commit? So the pull request is 2 commits, one for the specs, one for the implementation? |
I don't know how doing that... I am a git newbie. |
Socket.gethostbyname is updated for all ruby versions. * rakelib/platform.rake: Add new structure `sockaddr_in6` * lib/**/socket.rb : Socket.sockaddr_in is used to transform addresses got from Socket.getaddrinfo to packed strings. Platform configuration is then used to determine offset and size of IP address in sockaddr string. `sockaddr_in` and `sockaddr_in6` platform structures are used for this purpose.
…nto socket.gethostbyname
I tried something with git rebase, but i'm not sure that's good. |
git rebase should work yeah, if you do an interactive rebase you can squash commits together. If you have it right locally, you can force push it to overwrite the changes in the pull request. Otherwise I can merge it locally on my machine and squash it there. |
It seems I made a mess here. Please merge it locally on your machine. |
Merged this manually |
Update Socket.gethostbyname.
First, spec is updated to add tests on address list to verify that these addresses are in packed format.
In a second commit Socket.gethostbyname is updated for all ruby versions to pass spec.