TCPSocket.gethostbyname difficulties

Because my customers want to access broken sites, and they don’t care that
they’re broken. They’ll tell me, “It loads in my browser… why can’t your
system find it?” If I can’t get this to work, I’ll just have to stop
checking DNS, which would be unfortunate.

Thanks, Guy.

Nathaniel

<:((><

···

ts [mailto:decoux@moulon.inra.fr] wrote:

ntalbott@proxytest:~$ host cancer.org
cancer.org has address 209.135.47.118
cancer.org has address 0.0.0.0

Why do you want to work with something which is broken ?

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

Since when did the lack of reverse mean that forward lookups were broken?

This is something that’s broken with Ruby (like I pointed out nearly two
years ago) and should have been fixed by now.

···

On Fri, 26 Sep 2003 22:53:24 +0900, ts decoux@moulon.inra.fr wrote:

Why do you want to work with something which is broken ?

I think your DNS server manages to do the reverse lookup while ours can’t.
Is that possible? I don’t know anything about how reverse lookups work…

I’ve just taken a look at the rfc1034. The section on inverse queries goes
like this:

You seem to have missed a critical section of the RFC:

  1. INTRODUCTION

This RFC introduces domain style names, their use for Internet mail
and host address support, and the protocols and servers used to
implement domain name facilities.

This RFC tells you how to implement a name server, not how to write
a zone file. Your quote is out of context.

3.7.2. Inverse queries (Optional)

Name servers may also support inverse queries that map a particular
resource to a domain name or domain names that have that resource.
For example, while a standard query might map a domain name to
a SOA RR, the corresponding inverse query might map the SOA RR
back to the domain name.

This paragraph refers to any RR, not just to A RRs, for example,
if I have seveal TXT RRs that are “foo”, I may be able to do an
inverse query on “foo” and get back all the records that have “foo”
as the TXT RR.

When I call TCPSocket.gethostbyname(“cancer.org”), tcpdump gives this:

14:42:34.763284 localhost.4023 > localhost.domain: 40415+ AAAA? cancer.org. (28)
14:42:34.764176 localhost.domain > localhost.4023: 40415 0/1/0 (77)
14:42:34.765011 localhost.4024 > localhost.domain: 40416+ A? cancer.org. (28)
14:42:34.766352 localhost.domain > localhost.4024: 40416 2/4/4 A 0.0.0.0, A 209.135.47.118 (211)
14:42:34.768112 localhost.4025 > localhost.domain: 40417+ PTR? 0.0.0.0.in-addr.arpa. (38)
14:42:34.826762 localhost.domain > localhost.4025: 40417 NXDomain 0/1/0 (102)

Ruby is asking for the in-addr.arpa domain, which is covered in various
RFCs, see RFC1101 in particular. It is not performing an inverse query.

···

Peter (Peter.Vanbroekhoven@cs.kuleuven.ac.be) wrote:


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Well, this domains just dont have a reverse DNS entry. Thats not a ruby
problem, but a DNS problem.

and this is not really broken. it is not mandatory for an IP address to have a
mapping to a domain naime IIRC.

and even if it exists it could be totally different from the original domain,
as you can map lots of domains to an IP address.

···

On Friday 26 September 2003 16:02, Nathaniel Talbott wrote:

ts [mailto:decoux@moulon.inra.fr] wrote:

ntalbott@proxytest:~$ host cancer.org
cancer.org has address 209.135.47.118
cancer.org has address 0.0.0.0

Why do you want to work with something which is broken ?

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

Because my customers want to access broken sites, and they don’t care that
they’re broken. They’ll tell me, “It loads in my browser… why can’t your
system find it?” If I can’t get this to work, I’ll just have to stop
checking DNS, which would be unfortunate.

Thanks, Guy.

Nathaniel

Because my customers want to access broken sites, and they don't care that
they're broken. They'll tell me, "It loads in my browser... why can't your
system find it?" If I can't get this to work, I'll just have to stop
checking DNS, which would be unfortunate.

You are right, you can't use DNS. You can try

svg% ruby -rsocket -e 'p Socket::getnameinfo(["AF_INET",80,"noblepack.com"])'
["205.178.141.161", "http"]
svg%

Guy Decoux

Why you don't quote completely the message. You find this normal ?

svg% host cancer.org
cancer.org has address 0.0.0.0
cancer.org has address 209.135.47.118
svg%

Guy Decoux

···

On Fri, 26 Sep 2003 22:53:24 +0900, > ts <decoux@moulon.inra.fr> wrote:

Why do you want to work with something which is broken ?

Since when did the lack of reverse mean that forward lookups were broken?

Well, this domains just dont have a reverse DNS entry. Thats
not a ruby problem, but a DNS problem.

TCPSocket#gethostbyname is somehow dependent on there being a reverse DNS
entry? That seems strange.

and this is not really broken. it is not mandatory for an IP
address to have a mapping to a domain naime IIRC.

and even if it exists it could be totally different from the
original domain, as you can map lots of domains to an IP address.

Right. So I still don’t understand why lookup is failing in Ruby for some
hosts that other programs can lookup OK. Apparently it’s related to the
reverse DNS entry (or lack thereof), but why?

Thanks,

Nathaniel

<:((><

···

Peter Triller [mailto:sentinel27@gmx.de] wrote:

Excellent! That’s what I was looking for. Since I don’t want to worry about
the port at this point, I’ve just used 0:

ntalbott@proxytest:~$ ruby -rsocket -e ‘p
Socket::getnameinfo([“AF_INET”, 0, “www.noblepack.com”])’
[“205.178.141.161”, “0”]

Works like a charm. Thanks Guy (and everyone else that contributed)!

Nathaniel

<:((><

···

ts [mailto:decoux@moulon.inra.fr] wrote:

You are right, you can’t use DNS. You can try

svg% ruby -rsocket -e ‘p
Socket::getnameinfo([“AF_INET”,80,“noblepack.com”])’
[“205.178.141.161”, “http”]
svg%

Since when did the lack of reverse mean that forward lookups were broken?
Why you don’t quote completely the message. You find this normal ?

svg% host cancer.org
cancer.org has address 0.0.0.0
cancer.org has address 209.135.47.118
svg%

sigh No, that’s not normal, but you then demonstrated that it had no
reverse – seemingly to make your point. My apologies if I misunderstood,
but the fact remains that Ruby’s gethostbyname is seriously broken and
won’t look up hostnames that have no reverse, “broken” or not.

For example, rotten.com can’t be looked up using Ruby and isn’t “broken”.

host rotten.com
rotten.com has address 216.218.248.174
host 216.218.248.174
Host not found.
irb
irb(main):001:0> require ‘socket’
=> true
irb(main):002:0> Socket::gethostbyname(‘rotten.com’)
SocketError: host not found
from (irb):2:in `gethostbyname’
from (irb):2

We already know that the “broken” cancer.org doesn’t work.

irb(main):003:0> Socket::gethostbyname(‘cancer.org’)
SocketError: host not found
from (irb):3:in `gethostbyname’
from (irb):3

But getaddrinfo does work, though requires you to specify a service name.

irb(main):007:0> Socket::getaddrinfo(‘rotten.com’, ‘http’)
=> [[“AF_INET”, 80, “216.218.248.174”, “216.218.248.174”, 2, 2, 17],
[“AF_INET”, 80, “216.218.248.174”, “216.218.248.174”, 2, 1, 6]]

Broken it might be, but Ruby is still able to look up cancer.org’s A records
with no trouble if you use getaddrinfo.

irb(main):009:0> Socket::getaddrinfo(‘cancer.org’, ‘http’)
=> [[“AF_INET”, 80, “0.0.0.0”, “0.0.0.0”, 2, 2, 17], [“AF_INET”, 80,
“209.135.47.118”, “209.135.47.118”, 2, 2, 17], [“AF_INET”, 80,
“209.135.47.118”, “209.135.47.118”, 2, 1, 6], [“AF_INET”, 80, “0.0.0.0”,
“0.0.0.0”, 2, 1, 6]]

Lest you think it’s not Ruby’s fault, both work fine with Perl.

perl -e ‘print join(“.”, unpack(“C4”, (gethostbyname(“rotten.com”))[4]))’
216.218.248.174
perl -e ‘print join(“.”, unpack(“C4”, (gethostbyname(“cancer.org”))[4])),“\n”’
209.135.47.118

They also work fine in Python.

import socket
socket.gethostbyname(“rotten.com”)
‘216.218.248.174’
socket.gethostbyname(“cancer.org”)
‘209.135.47.118’

Why does Ruby persist in this nonsense? People will expect gethostbyname to
Just Work since it does everywhere else. I posted RCR#46 about this bug
almost two years ago now and, it seems, people are still having problems
with it. Maybe it’s time to fix it finally?

RCR#46 lives at http://www.rubygarden.org/article.php?sid=118

···

On Sat, 27 Sep 2003 23:34:54 +0900, ts decoux@moulon.inra.fr wrote:

Sorry to barge in on your thread, but the topic reminded of
a question that you guys probably know the answer to:

How do I get the full name of the machine that I am on?
My current method involves ifconfig -a to get the IP and
nslookup to get the name.

Is there a ruby -e one liner to do this?

(Please forgive me if this is a stupid question.)

···


Jim Freeze

“The Computer made me do it.”

For example, rotten.com can't be looked up using Ruby and isn't "broken".

svg% ruby -rsocket -e 'p IPSocket.getaddress("rotten.com")'
"216.218.248.174"
svg%

svg% ruby -rsocket -e '3.times { p IPSocket.getaddress("cancer.org") }'
"0.0.0.0"
"209.135.47.118"
"0.0.0.0"
svg%

Guy Decoux

How do I get the full name of the machine that I am on?
My current method involves ifconfig -a to get the IP and
nslookup to get the name.

Is there a ruby -e one liner to do this?

(Please forgive me if this is a stupid question.)

ruby -e “require ‘socket’; puts Socket.gethostbyname(Socket.gethostname)[0]”

The extra Socket.gethostbyname(…)[0] is necessary because
Socket.gethostname does not (necessarily) give the full hostname. Also if
your hostname is listed in your /etc/hosts (or the equivalent under
windows) as localhost, Socket#gethostbyname still returns the simple
hostname and localhost as alias (Socket.gethostbyname(…)[1] gives the
list of aliases). This happened on one machine I tested it on…

Peter

Yes, I already showed that getaddrinfo worked fine. gethostbyname doesn’t.

http://www.c2.com/cgi/wiki?FixBrokenWindows sums it up quite well. That this
is still confusing people two years later is insane.

···

On Sun, 28 Sep 2003 21:10:03 +0900, ts decoux@moulon.inra.fr wrote:

For example, rotten.com can’t be looked up using Ruby and isn’t “broken”.
svg% ruby -rsocket -e ‘p IPSocket.getaddress(“rotten.com”)’
“216.218.248.174”

When I run this, I only get the machine name, not the FQDN of my
computer:

ruby -e “require ‘socket’; puts Socket.gethostbyname(Socket.gethostname)[0]”
juno

ruby -e “require ‘socket’; p Socket.gethostbyname(Socket.gethostname)”
[“juno”, , 2, “\235_/A”]

Is there a way to get the full name?

···

On Saturday, 27 September 2003 at 1:47:56 +0900, Peter wrote:

How do I get the full name of the machine that I am on?
My current method involves ifconfig -a to get the IP and
nslookup to get the name.

Is there a ruby -e one liner to do this?

(Please forgive me if this is a stupid question.)

ruby -e “require ‘socket’; puts Socket.gethostbyname(Socket.gethostname)[0]”

The extra Socket.gethostbyname(…)[0] is necessary because
Socket.gethostname does not (necessarily) give the full hostname. Also if
your hostname is listed in your /etc/hosts (or the equivalent under
windows) as localhost, Socket#gethostbyname still returns the simple
hostname and localhost as alias (Socket.gethostbyname(…)[1] gives the
list of aliases). This happened on one machine I tested it on…

Peter


Jim Freeze

Your lucky number has been disconnected.

When I run this, I only get the machine name, not the FQDN of my
computer:

ruby -e “require ‘socket’; puts Socket.gethostbyname(Socket.gethostname)[0]”
juno

ruby -e “require ‘socket’; p Socket.gethostbyname(Socket.gethostname)”
[“juno”, , 2, “\235_/A”]

You’re right. I tested it on 3 machines, and on 2 it gave the FQDN, on 1
it returned localhost. I had the hostname on that last machine point to
the external IP-address rather than localhost and then I had the same
problem. About the 2 machines it did work on: one is a WinXP machine, the
other is a linux machine at work. Apparently both have something my own
linux machine has not…

Is there a way to get the full name?

I’m out of ideas. None of the other calls I tested could get past the
hostname for some weird reason.

Peter