From: Alan Johnson Date: 2008-11-19T18:58:25+09:00 Subject: [ruby-core:19989] [Bug #743] Socket.gethostbyname returns odd values Issue #743 has been updated by Alan Johnson. File bug743.patch added gethostbyname/gethostbyaddr seem to work oddly in many respects. 1) Socket.gethostbyname works differently than TCPSocket.gethostbyname. The latter doesn't seem to have any real purpose for existing. 2) The alias array is always empty. 3) When a host has multiple IP addresses, they are tacked onto the end of the array. To more faithfully represent a struct hostent, the fourth array member should itself be an array of addresses (matching h_addr_list). These problems mostly seem to stem from the fact that instead of faithfully calling the underlying library implementation, they ALSO call getaddrinfo. The least surprising effect of calling gethostbyname or gethostbyaddr would be for it to just call the underlying library implementation. That means that gethostbyname should not do a reverse lookup, and if you want a reverse lookup you should call gethostbyaddr or one of the other functions that does such a lookup. This is partially impeded by the fact that gethostbyaddr requires an struct in_addr parameter packed into a ruby string. This suggests that Socket also needs to implement inet_aton (and inet_ntoa just for symmetry). I'm attaching a patch that does the following: * Remove TCPSocket.gethostbyname * Implement Socket.gethostbyname and Socket.gethostbyaddr as calls to the underlying library implementations. * Make the fourth member of the result array of the above functions an array matching struct hostent's h_addr_list member. * Implement Socket.inet_aton and Socket.inet_ntoa. Subsequently code similar to the following will be possible: require 'socket' def print_hostent(hostent) hostname, aliases, family, addresses = hostent print "Hostname: #{hostname}\n" print "Aliases:\n" aliases.each { |a| print " #{a}\n" } print "Addresses:\n" addresses.each { |addr| print " #{Socket.inet_ntoa(addr)}\n" } end hostent = Socket.gethostbyname('www.ruby-lang.org') print_hostent hostent print "\n" hostent = Socket.gethostbyaddr(Socket.inet_aton("221.186.184.68")) print_hostent hostent $ ruby19 example.rb Hostname: carbon.ruby-lang.org Aliases: www.ruby-lang.org Addresses: 221.186.184.68 Hostname: carbon.ruby-lang.org Aliases: Addresses: 221.186.184.68 ---------------------------------------- http://redmine.ruby-lang.org/issues/show/743 ---------------------------------------- http://redmine.ruby-lang.org