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

Socket.gethostbyname #2125

wants to merge 6 commits into from

Conversation

sdaubert
Copy link
Contributor

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.

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
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.

@sdaubert
Copy link
Contributor Author

I have studied Rubinius::FFI, and I think I understand the problem :

  • struct sockaddr_in6 must be defined in rakelib/platform.rake.
  • FFI.config must be used to obtain offsets and sizes for sockaddr_in* fields.

I will try to do that this week.

@dbussink
Copy link
Contributor

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.
@sdaubert
Copy link
Contributor Author

Yeah, it works !
Last commit uses platform layout to get packed address from sockaddr string.

# 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.

* Remove hard-coded offset and size for IPv6.
* Replace String#[] by String#byteslice to get address
  from sockaddr string.
@dbussink
Copy link
Contributor

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?

@sdaubert
Copy link
Contributor Author

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.
@sdaubert
Copy link
Contributor Author

I tried something with git rebase, but i'm not sure that's good.
May i do that in another branch ?

@dbussink
Copy link
Contributor

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.

@sdaubert
Copy link
Contributor Author

It seems I made a mess here. Please merge it locally on your machine.

@dbussink
Copy link
Contributor

Merged this manually

@dbussink dbussink closed this Jan 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants