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

gethostbyname seems to return ipaddresses in reverse order for local host name on a multihomed machine. I have the metric set such that I don't see a private IP that should have a poorer metric (win32 gethostbyname is honoring this). I have perl v5.10.0 on Windows. (Getaddrinfo seems to be not defined in this) Binary build 1002 283697 provided by ActiveState
  • Comment on gethostbyname returns reverse order of ipaddress expected

Replies are listed 'Best First'.
Re: gethostbyname returns reverse order of ipaddress expected
by Anonymous Monk on Jun 17, 2013 at 03:07 UTC

    gethostbyname seems to return ipaddresses in reverse order for local host name on a multihomed machine.

    Can you show that?

    use Data::Dump; dd [ gethostbyname 'localhost' ]; __END__ ["VOL11", "", 2, 4, "\x7F\0\0\1"]

    I don't expect that gethostbyname is returning anything other than what gethostbyname is documented to return

Re: gethostbyname returns reverse order of ipaddress expected
by Khen1950fx (Canon) on Jun 17, 2013 at 13:47 UTC
    gethostbyname works for me. You didn't provide your code, so I can't tell if you are using it in the right way. I would do something like this:
    #!/usr/bin/perl use strict; use warnings; use Socket; my $packed_ip = gethostbyname('localhost.localdomain'); if (defined $packed_ip) { my $ip_address = inet_ntoa($packed_ip); print $ip_address, "\n"; }
    Make sure that you check for definedness.

    Using Sys::Hostname::FQDN, you can do an ascii-version of Anonymous Monk's post like this:

    #!/usr/bin/perl BEGIN { $| = 1; $^W = 1; } use strict; use warnings; use Socket; use Sys::Hostname::FQDN qw( asciihostinfo gethostinfo fqdn short ); my($name,$aliases,$addrtype,$length,@addrs) = asciihostinfo(); print qq|host info short name : |, short(), qq| long name ; |, fqdn(), qq| aliases : |, $aliases, qq| address type : |, $addrtype, qq| address length: |, $length, qq| IP address(es): |; foreach(@addrs) { print "\t$_\n"; }

      I should have been clearer its the choice of ipaddress to return that's the issue. If I return all addresses it get it in a reverse order than a C# program (both are copied below). if I use Khen1950fx's first example I get the answer as 192.168.0.1 which is the wrong address to use. Code that picks the first address to use now breaks down on a multiadapter machines.

      Outputs via C# and perl

      >listaddrcsharp.exe 10.123.67.139 192.168.0.1

      >perl d:\temp\in.pl 192.168.0.1 10.123.67.139

      Perl code
      use Socket; use IO::Socket; my $machineName = lc($ENV{COMPUTERNAME}); my ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($mach +ineName); foreach(@addrs) { ($a,$b,$c,$d)=unpack ('C4', $_); print "$a.$b.$c.$d\n"; }
      C# Code
      public static void Main(string[] args) { IPAddress[] ips; ips = Dns.GetHostAddresses(Environment.MachineName); foreach (IPAddress ip in ips) { if (ip.AddressFamily == AddressFamily.InterNetwork) Console.WriteLine(" {0}", ip); } }