in reply to gethostbyname("1.1.1") returns 1.1.0.1 ????

What makes you think that 1.1.1 or 9.9.9 are invalid representations of IP addresses? They *are* valid. If you're using the dotted number notation, there are at most 4 numbers. But if there are 3 or 2, the one or two numbers before the last are assumed to be 0. Hence, gethostbyname is quite correct, and you are incorrect by assuming to have it fed illegal input.

Abigail

Replies are listed 'Best First'.
Re: Re: gethostbyname("1.1.1") returns 1.1.0.1 ????
by pg (Canon) on Mar 06, 2003 at 03:25 UTC
    To prove Abigail-II is right, just do a
    ping 127.1
    
    and it would ping 127.0.0.1 for you.
Re: Re: gethostbyname("1.1.1") returns 1.1.0.1 ????
by jdporter (Paladin) on Mar 06, 2003 at 06:04 UTC
    if there are 3 or 2, the one or two numbers before the last are assumed to be 0.
    While this is "true", it obscures the fact that the range of the last part depends on how many parts there are.
    Parts:Range of last part:
    428-1 = 255
    3216-1 = 65535
    2224-1 = 16777215
    1232-1 = 4294967295
    Although, of course, the usual restrictions on legal addresses apply.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      Although it is possible that I am not understanding your point, I believe your correction is incorrect.

      127.1 is 127.0.0.1. There is no range. 127.1.1 is 127.1.0.1. Again, there is no range. (Well, to be exact, there is a range of 1) The spec allows zero's before the last to be left out as a convenient expression form, not as a method of defining subnet's or 'range'. IPv6 has a similar form that allows a single '::' to be specified within the address that indicates that all missing numbers at this location are 0. Again, it is a convenient expression form, and nothing more.

        I suppose you probably are misunderstanding me.

        By "range", I mean the range of possible numeric values in each part. For example, in the typical "dotted quad" notation, the range of legal values in each part is 0 to 255, the possible values of an unsigned 8-bit number.

        When there are only three parts, the first two are 8 bits in size (thus have the usual range 0-255), but the last part represents a 16-bit number, and thus can legally have any value in the range 0 - 65535. 127.1.54321 is a legal, valid IP address.

        If you disagree with that, then you are simply mistaken. Try it:  inet_aton("127.1.54321");

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Re: gethostbyname("1.1.1") returns 1.1.0.1 ????
by Mr_Person (Hermit) on Mar 06, 2003 at 16:19 UTC
    That's very interesting, I hadn't heard about that before. Just out of curiosity, do you happen to know the RFC (or whatever kind of document it is) that says all the different ways you can represent an IP address?

      I don't think there is an RFC that explicitly explains this, the reason it works is that if you take the address 127.1/24, the netmask specifies that the first 24 bits are the network address and the rest are the host address, the 127 falls within the network address part, setting the appropriate bits to one, and leaves all the other bits in the network part (even the unspecified ones) as zeros, since there is no third state that can represent the undefined value.

      So, the way I understand it, it works something like this:

      • 127.1 gets translated to binary and becomes 0111111100000001
      • The 24 bit netmask tells us that the host part is the last 8 bits, or 00000001
      • That leaves the other 24 bits for the network part, padded with zeros we get 011111110000000000000000
      • Stick these back together and you get 01111111000000000000000000000001
      • Split this long binary number into 4 bytes, and convert them back to decimal, and it leaves you with 127.0.0.1
        I'm pretty sure that's not the reason. First, addresses like 127.1 work even if you do not give a a netmask. Secondly, the notation was used even before we had classless networks, and we were still dealing with A, B, C and D class networks.

        Note that many utilities also accept addresses in hex, octal, or even a combination of hex, octal and decimal:

        $ ping -c 1 0x42.047.54.0x1b # www.perlmonks.org PING 0x42.047.54.0x1b (66.39.54.27): 56 data bytes 64 bytes from 66.39.54.27: icmp_seq=0 ttl=244 time=114.3 ms --- 0x42.047.54.0x1b ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max = 114.3/114.3/114.3 ms $

        Abigail