pg has asked for the wisdom of the Perl Monks concerning the following question:

Update:

My appreciation goes to idsfa and zengargoyle. I appreciate their research and their scientific and serious way of thinking.

It turned out that Java and Perl does the same, see idsfa's post for detail. Also see posts from both of them for some useful RFC's.

Original:

I have used IO::Socket::INET for a long time, but last week I suddenly noticed something: Perl always resolves localhost to loopback 127.0.0.1, which is the least expected to me.

When my computer is connected to a network, I expect localhost being resolved to my IP address assigned (in the simplest case, let's don't talk about what if my computer has multiple IP assigned), not the loopback. That is true to quite a few of other languages.

Java does it in a very interesting way, it resolves localhost to your IP address, but when you run from applet, it resolves to loopback for security reason (which is quite stupid, as I have a way to break this anyway.)

Any way, what is the reasoning behind Perl? or just a bug?

  • Comment on Does Perl always resolve localhost to loopback?

Replies are listed 'Best First'.
Re: Does Perl always resolve localhost to loopback?
by idsfa (Vicar) on Oct 18, 2003 at 02:03 UTC

    Any deviation from this behaviour is BROKEN.

    See Section 4.1 of RFC1912, where localhost is defined as 127.0.0.1

    Thanks for finding this Java bug ... which JDK (so I can remember not to use it)?

    Updated:
    I just tested this on my 1.3.1 JDK, and it did not act as you describe. Perhaps you are confusing InetAddress.getByName("localhost") and InetAddress.getLocalHost()?


    Remember, when you stare long into the abyss, you could have been home eating ice cream.

      If I can ++ you twice, I will do it.

      • First ++ you, because your update did get me. Thanks. This is a very happy ending for me. I got something straightened, and can have a good night now. (I was frustrated, and almost thought that I was caught in another meaningless Java vs. Perl fighting, when I even didn't have the slightest intention of that.)
      • Secondly ++ you, because you have found that RFC. I appreciate your seriousness.

      Thank you again!

Re: Does Perl always resolve localhost to loopback?
by mpeppler (Vicar) on Oct 18, 2003 at 00:02 UTC
    I don't think that's perl - it's how gethostbyname() works. "localhost" should always resolve to 127.0.0.1. To get your actual IP address you'd have to use gethostbyname() with the name that maps to your current IP address, which will normally be set through dhcp if it is dynamic.

    Michael

      I agree with each word you said. Yes, this is the behavior of c's gethostbyname, and Perl have chosen not to put "decoration" on top of it.

      Java did interpreted localhost in a way that is not quite the same.

      Java resolve localhost to the DHCP assigned address, but for applet, it resolves to loopback, by doing this, they try to make your applet not able to listen on TCP port on the browser's PC. In older versions of Java, localhost always resolve to DHCP assigned address, even if it run as applet. They modified this behavior to increase security.

      (My purpose is not about what is right, what is wrong, but about what actually happens in different languages, and whether there is a particular reason behind them. I go with your answer: it is becasue of c, simple ;-)

Re: Does Perl always resolve localhost to loopback?
by sauoq (Abbot) on Oct 18, 2003 at 00:22 UTC

    As others have said, localhost should resolve to 127.0.0.1. If it ever resolves as anything else, your configuration is probably broken. Also, the recommended configuration for DNS servers is to keep a PTR record for 1.0.0.127.in-addr.arpa which resolves to 'localhost'.

    -sauoq
    "My two cents aren't worth a dime.";
    
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Does Perl always resolve localhost to loopback?
by zengargoyle (Deacon) on Oct 18, 2003 at 00:09 UTC

    it's exactly what i expect.

    $ ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.061 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.041 ms --- localhost ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1007ms rtt min/avg/max/mdev = 0.041/0.051/0.061/0.010 ms $ ping lapcat PING lapcat (192.168.254.74) 56(84) bytes of data. 64 bytes from lapcat (192.168.254.74): icmp_seq=1 ttl=64 time=0.053 ms 64 bytes from lapcat (192.168.254.74): icmp_seq=2 ttl=64 time=0.038 ms --- lapcat ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.038/0.045/0.053/0.010 ms

    i would be really annoyed if something turned my localhost into 192.168.254.74. that's just wrong.

    localhost should resolve to a 127. address. but if you want to monkey around check your /etc/hosts file and give the desired IP to localhost.

    # /etc/hosts 127.0.0.1 localhost 192.168.254.74 lapcat
Re: Does Perl always resolve localhost to loopback?
by Anonymous Monk on Oct 18, 2003 at 02:01 UTC
    Perl is not doing anything special, it is just letting the system libraries resolve localhost. This is not a bug.