TCPSocket.gethostbyname difficulties

Discussion in 'Ruby' started by Nathaniel Talbott, Sep 25, 2003.

  1. I'm trying to use TCPSocket.gethostbyname to verify that a given domain
    actually exists in DNS. For some reason, though, some domains fail to
    resolve that resolve fine using other applications. For instance:

    irb(main):001:0> require 'socket'
    => true
    irb(main):002:0> TCPSocket.gethostbyname('noblepack.com')
    SocketError: host not found
    from (irb):2:in `gethostbyname'
    from (irb):2
    irb(main):003:0> TCPSocket.gethostbyname('google.com')
    => ["www.google.com", [], 2, "216.239.37.100"]

    I can browse to either of those hosts, so what's different about them? Any
    help would be greatly appreciated; my guess is that I just fail to
    understand something critical about DNS and/or gethostbyname.

    Thanks,


    Nathaniel

    <:((><
    Nathaniel Talbott, Sep 25, 2003
    #1
    1. Advertising

  2. Hi,

    In message "TCPSocket.gethostbyname difficulties"
    on 03/09/26, "Nathaniel Talbott" <> writes:

    |I'm trying to use TCPSocket.gethostbyname to verify that a given domain
    |actually exists in DNS. For some reason, though, some domains fail to
    |resolve that resolve fine using other applications. For instance:
    |
    | irb(main):001:0> require 'socket'
    | => true
    | irb(main):002:0> TCPSocket.gethostbyname('noblepack.com')
    | SocketError: host not found
    | from (irb):2:in `gethostbyname'
    | from (irb):2

    This works for me:

    ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    ruby 1.8.0 (2003-09-27) [i686-linux]
    ["noblepack.com", [], 2, "205.178.141.161"]

    Could you tell us more info?

    matz.
    Yukihiro Matsumoto, Sep 28, 2003
    #2
    1. Advertising

  3. Nathaniel Talbott

    James Britt Guest

    Yukihiro Matsumoto wrote:

    > This works for me:
    >
    > ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    > ruby 1.8.0 (2003-09-27) [i686-linux]
    > ["noblepack.com", [], 2, "205.178.141.161"]
    >
    > Could you tell us more info?


    Out of curiosity, I just tried the above, on my Win2k laptop, running
    /\ndy's 1.8 build:

    C:\>ruby -v
    ruby 1.8.0 (2003-08-04) [i386-mswin32]

    C:\> ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    -e:1:in `gethostbyname': host not found (SocketError)
    from -e:1

    However, I can ping that location just fine.

    I then tried a different address:

    C:\> ruby -r socket -e 'p TCPSocket.gethostbyname("jamesbritt.com")'
    ["amsnac3.com", [], 2, "66.246.52.127"]



    James Britt
    James Britt, Sep 28, 2003
    #3
  4. Nathaniel Talbott

    Mike Stok Guest

    In article <>,
    Yukihiro Matsumoto <> wrote:
    >Hi,
    >
    >In message "TCPSocket.gethostbyname difficulties"
    > on 03/09/26, "Nathaniel Talbott" <> writes:
    >
    >|I'm trying to use TCPSocket.gethostbyname to verify that a given domain
    >|actually exists in DNS. For some reason, though, some domains fail to
    >|resolve that resolve fine using other applications. For instance:
    >|
    >| irb(main):001:0> require 'socket'
    >| => true
    >| irb(main):002:0> TCPSocket.gethostbyname('noblepack.com')
    >| SocketError: host not found
    >| from (irb):2:in `gethostbyname'
    >| from (irb):2
    >
    >This works for me:
    >
    > ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    > ruby 1.8.0 (2003-09-27) [i686-linux]
    > ["noblepack.com", [], 2, "205.178.141.161"]
    >
    >Could you tell us more info?


    Although not addressed to me, on Mandrake Linux 9.1 I see

    [mike@ratdog mike]$ ruby -v -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    ruby 1.8.0 (2003-09-27) [i686-linux]
    -e:1:in `gethostbyname': host not found (SocketError)
    from -e:1
    [mike@ratdog mike]$ ping -c 1 noblepack.com
    PING noblepack.com (205.178.141.161) 56(84) bytes of data.
    64 bytes from 205.178.141.161: icmp_seq=1 ttl=119 time=52.9 ms

    --- noblepack.com ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 52.996/52.996/52.996/0.000 ms
    [mike@ratdog mike]$ ruby -v -r socket -e 'p TCPSocket.gethostbyname("stok.co.uk")'
    ruby 1.8.0 (2003-09-27) [i686-linux]
    ["berke-breathed.deathtongue.org", [], 2, "209.251.75.53"]

    Hope this helps,

    Mike
    --
    | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Stok, Sep 28, 2003
    #4
  5. Hi,

    In message "Re: TCPSocket.gethostbyname difficulties"
    on 03/09/29, James Britt <> writes:

    |Out of curiosity, I just tried the above, on my Win2k laptop, running
    |/\ndy's 1.8 build:
    |
    |C:\>ruby -v
    |ruby 1.8.0 (2003-08-04) [i386-mswin32]
    |
    |C:\> ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    |-e:1:in `gethostbyname': host not found (SocketError)
    | from -e:1
    |
    |However, I can ping that location just fine.

    Hmm, might be Win32 specific problem. Does stopping reverse lookup
    make any difference?

    Socket.do_not_reverse_lookup = false

    matz.
    Yukihiro Matsumoto, Sep 29, 2003
    #5
  6. Nathaniel Talbott

    James Britt Guest

    Yukihiro Matsumoto wrote:

    > Hmm, might be Win32 specific problem. Does stopping reverse lookup
    > make any difference?
    > Socket.do_not_reverse_lookup = false


    No. Same results.

    James
    James Britt, Sep 29, 2003
    #6
  7. Nathaniel Talbott

    ts Guest

    >>>>> "Y" == Yukihiro Matsumoto <> writes:

    Y> Socket.do_not_reverse_lookup = false

    If I'm right ruby don't use it in this case, perhaps a good idea to add
    this test.

    svg% host noblepack.com
    noblepack.com has address 205.178.141.161
    svg%

    svg% host 205.178.141.161
    Host 161.141.178.205.in-addr.arpa not found: 3(NXDOMAIN)
    svg%



    Guy Decoux
    ts, Sep 29, 2003
    #7
  8. Yukihiro Matsumoto () wrote:
    [...]
    > This works for me:
    >
    > ruby -r socket -e 'p TCPSocket.gethostbyname("noblepack.com")'
    > ruby 1.8.0 (2003-09-27) [i686-linux]
    > ["noblepack.com", [], 2, "205.178.141.161"]
    >
    > Could you tell us more info?


    This surprises me, since there is no PTR record for 205.178.141.161,
    and ruby (1.8.0 in my case) insists on doing a gethostbyaddr() on
    that address (in sock_hostbyname()) and will raise a "host not
    found" error if gethostbyaddr() fails (usually if there is no PTR
    record). That lookup is performed regardless of the value of
    Socket.do_not_reverse_lookup.

    Incidentally, what is the rationale behind requiring the presence of
    a PTR record? There are many hosts that do not have one, especially
    virtual web hosts, and I have found Socket/TCPSocket.gethostbyname to be
    fairly useless as a result (and instead resorted to using
    Resolv.getaddress[es]).

    Reimer Behrends
    Reimer Behrends, Sep 29, 2003
    #8
  9. Hi,

    In message "Re: TCPSocket.gethostbyname difficulties"
    on 03/09/30, Reimer Behrends <> writes:

    |This surprises me, since there is no PTR record for 205.178.141.161,
    |and ruby (1.8.0 in my case) insists on doing a gethostbyaddr() on
    |that address (in sock_hostbyname()) and will raise a "host not
    |found" error if gethostbyaddr() fails (usually if there is no PTR
    |record). That lookup is performed regardless of the value of
    |Socket.do_not_reverse_lookup.
    |
    |Incidentally, what is the rationale behind requiring the presence of
    |a PTR record? There are many hosts that do not have one, especially
    |virtual web hosts, and I have found Socket/TCPSocket.gethostbyname to be
    |fairly useless as a result (and instead resorted to using
    |Resolv.getaddress[es]).

    Ah, thanks. Your message reminds me Ruby's gethostbyname method is
    implemented by the combination of getaddrinfo() and gethostbyaddr().
    The reason behind this is plain gethostbyname() does not work well
    with IPv6 addresses (on some platforms). I want to fix this, but I
    don't know how (yet).

    matz.
    Yukihiro Matsumoto, Sep 30, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill

    GetHostByName in IIS

    Bill, Aug 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    447
  2. Eric Brunel
    Replies:
    0
    Views:
    1,444
    Eric Brunel
    Oct 27, 2003
  3. Fortepianissimo

    timeout in socket.gethostbyname()?

    Fortepianissimo, Dec 7, 2003, in forum: Python
    Replies:
    1
    Views:
    1,355
    Fortepianissimo
    Dec 8, 2003
  4. Frank Miles
    Replies:
    2
    Views:
    818
    George Kinney
    Jul 27, 2004
  5. msoulier
    Replies:
    2
    Views:
    108
    Eric Hodel
    Dec 21, 2006
Loading...

Share This Page