Socket::gethostbyname()

I have done the following:

irb(main):103:0> response = Socket::gethostbyname('www.ebay.com')
=> ["pages.ebay.com", [], 2, "\020\002\000\000B\207\300X\000\000\000\000\000\000\000\000", "\020\002\000\000B\207\320X\000\000\000\000\000\000\000\000", "\020\002\000\000B\207\320e\000\000\000\000\000\000\000\000", "\020\002\000\000B\207\300W\000\000\000\000\000\000\000\000"]
irb(main):104:0>

What exactly are the 4 entries that are after the 2 in the array? From looking at the man page for gethostbyname(), it should return something resembling:

    struct hostent {
         char *h_name; /* official name of host */
         char **h_aliases; /* alias list */
         int h_addrtype; /* host address type */
         int h_length; /* length of address */
         char **h_addr_list; /* list of addresses from name server */
    };

I am thinking that the 4 entries after the '2' is the addr_list. I would kind of expect them to be in a sub array, but back to the question as to the format. From the command prompt:

[~]host www.ebay.com
www.ebay.com is an alias for pages.ebay.com.
pages.ebay.com has address 66.135.208.101
pages.ebay.com has address 66.135.192.87
pages.ebay.com has address 66.135.192.88
pages.ebay.com has address 66.135.208.88
[~]

I don't see how any of the above strings corresponds to the 4 ebay addresses. I can see how parts of the string correspond to the IP address.

irb(main):162:0> a1 = response[3]
=> "\020\002\000\000B\207\320X\000\000\000\000\000\000\000\000"
irb(main):163:0> a1.each_byte {|x| puts x}
16
2
0
66
135
208
88
0
....
0
=> "\020\002\000\000B\207\320X\000\000\000\000\000\000\000\000"
irb(main):164:0>

I can see that there is a slightly different call TCPSocket::gethostbyname that returns what I am looking for:

irb(main):165:0> response = TCPSocket::gethostbyname('www.ebay.com')
=> ["pages.ebay.com", [], 2, "66.135.208.88", "66.135.208.101", "66.135.192.87", "66.135.192.88"]
irb(main):166:0>

I am new and I am wondering where I would look into the documentation to get the specifics on this. I tried using http://www.ruby-doc.org/stdlib/, but I don't see enough details. What exactly is the relationship between Socket and TCPSocket?

[~]host www.ebay.com
www.ebay.com is an alias for pages.ebay.com.
pages.ebay.com has address 66.135.208.101
pages.ebay.com has address 66.135.192.87
pages.ebay.com has address 66.135.192.88
pages.ebay.com has address 66.135.208.88
[~]

svg% ruby -rsocket
Socket::gethostbyname("www.ebay.com")[3..-1].each do |ali|
   p Socket.unpack_sockaddr_in(ali)
end
^D
[0, "66.135.192.87"]
[0, "66.135.192.88"]
[0, "66.135.208.88"]
[0, "66.135.208.101"]
svg%

Guy Decoux

Hi,

···

In message "Re: Socket::gethostbyname()" on Sun, 3 Oct 2004 15:04:56 +0900, CarlosRivera <CarlosRivera@badnamefornospam.to> writes:

I have done the following:

irb(main):103:0> response = Socket::gethostbyname('www.ebay.com')
=> ["pages.ebay.com", , 2,
"\020\002\000\000B\207\300X\000\000\000\000\000\000\000\000",
"\020\002\000\000B\207\320X\000\000\000\000\000\000\000\000",
"\020\002\000\000B\207\320e\000\000\000\000\000\000\000\000",
"\020\002\000\000B\207\300W\000\000\000\000\000\000\000\000"]
irb(main):104:0>

What exactly are the 4 entries that are after the 2 in the array? From
looking at the man page for gethostbyname(), it should return something
resembling:

How about using TCPSocket::gethostbyname instead?

              matz.

I never would have guessed to have unpacked. Is there a good explanation of this somewhere? Also, during the unpack, what is the first element supposed to represent?

For a reference, I am reading the Socket class at:

http://www.ruby-doc.org/stdlib/

However, there seems to be no documentation on the method return types. Is something wrong with my browser? I can see detailed information about Net::HTTP.get().

ts wrote:

···

"C" == CarlosRivera <CarlosRivera@badnamefornospam.to> writes:

> [~]host www.ebay.com
> www.ebay.com is an alias for pages.ebay.com.
> pages.ebay.com has address 66.135.208.101
> pages.ebay.com has address 66.135.192.87
> pages.ebay.com has address 66.135.192.88
> pages.ebay.com has address 66.135.208.88
> [~]

svg% ruby -rsocket
Socket::gethostbyname("www.ebay.com")[3..-1].each do |ali|
   p Socket.unpack_sockaddr_in(ali)
end
^D
[0, "66.135.192.87"]
[0, "66.135.192.88"]
[0, "66.135.208.88"]
[0, "66.135.208.101"]
svg%

Guy Decoux

I would guess that it is a wrapper around the system's
gethostbyname (especially since you have to unpack it) so "man
gethostbyname" might give you some hints.

Note: this is only a guess. If you want an authoritative answer (and no
one else here provides it) I'd suggest checking the source code. It
seldom lies, though it is at times coy about divulging its secrets. (To
its credit though, I have found it to be as patient with me as I am with
it. Oh my stars! It's the weekend and I'm channeling Why!)

-- MarkusQ

···

On Sun, 2004-10-03 at 13:19, CarlosRivera wrote:

I never would have guessed to have unpacked. Is there a good
explanation of this somewhere? Also, during the unpack, what is the
first element supposed to represent?

For a reference, I am reading the Socket class at:

RDoc Documentation

However, there seems to be no documentation on the method return types.
  Is something wrong with my browser? I can see detailed information
about Net::HTTP.get().

ts wrote:
>>>>>>"C" == CarlosRivera <CarlosRivera@badnamefornospam.to> writes:
>
>
> > [~]host www.ebay.com
> > www.ebay.com is an alias for pages.ebay.com.
> > pages.ebay.com has address 66.135.208.101
> > pages.ebay.com has address 66.135.192.87
> > pages.ebay.com has address 66.135.192.88
> > pages.ebay.com has address 66.135.208.88
> > [~]
>
> svg% ruby -rsocket
> Socket::gethostbyname("www.ebay.com")[3..-1].each do |ali|
> p Socket.unpack_sockaddr_in(ali)
> end
> ^D
> [0, "66.135.192.87"]
> [0, "66.135.192.88"]
> [0, "66.135.208.88"]
> [0, "66.135.208.101"]
> svg%
>
>
> Guy Decoux
>
>

Nothing wrong with your browser. The standard of docs varies between
packages.

Gavin

···

On Monday, October 4, 2004, 6:19:55 AM, CarlosRivera wrote:

I never would have guessed to have unpacked. Is there a good
explanation of this somewhere? Also, during the unpack, what is the
first element supposed to represent?

For a reference, I am reading the Socket class at:

RDoc Documentation

However, there seems to be no documentation on the method return types.
  Is something wrong with my browser? I can see detailed information
about Net::HTTP.get().