13

I know IPv6 allows consecutive zeros to be omitted. But how about IPv4? I haven't found any reference to this on the Internet, including Wikipedia and RFC 791 – Internet Protocol. This document suggests that "Leading zeros can be omitted" in an IPv4 address (search for the term 'omitted'). Not specific enough.

Check out this shell session:

[~]$ ping -c 1 127.1
PING 127.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.040 ms

--- 127.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.040/0.040/0.040/0.000 ms
[~]$ ping -c 1 127.0.1
PING 127.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.044 ms

--- 127.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms
[~]$ ssh 127.1 :
The authenticity of host '127.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is 04:48:fa:f2:ef:95:7c:35:46:39:2e:d3:89:dd:cd:87.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.1' (ECDSA) to the list of known hosts.
alex@127.1's password: 

Clearly, both ping and ssh understand 127.1 and 127.0.1 to be the same as 127.0.0.1. Where is this specified?

  • 7
    [This man page](http://linux.die.net/man/3/inet_aton) linked in [this Stack Overflow post](http://stackoverflow.com/questions/10133820/is-there-any-documentation-for-ommiting-zeroes-in-dot-decimal-notation-of-ipv4-a) may be right up your alley. – nerdwaller Jul 01 '13 at 04:31
  • 1
    That's an ancient notation style, but yes: it does work :-) – Sander Steffann Jul 01 '13 at 07:41
  • 1
    @nerdwaller: Please post that as an answer. (Bonus points if you figure out why `ping 0.0.0.0` or `ping 0` works the same way...) – u1686_grawity Jul 01 '13 at 09:07
  • 2
    http://superuser.com/questions/486788/why-does-pinging-192-168-072-only-2-dots-return-a-response-from-192-168-0-58 – Oliver Salzburg Jul 01 '13 at 13:10

2 Answers2

16

There's a Stack Overflow post from about a year ago asking something similar (this post).

The main reason is how inet_aton() (man page) converts the octets into the binary address.

a.b.c.d

Each of the four numeric parts specifies a byte of the address; the bytes are assigned in left-to-right order to produce the binary address.

a.b.c

Parts a and b specify the first two bytes of the binary address. Part c is interpreted as a 16-bit value that defines the rightmost two bytes of the binary address. This notation is suitable for specifying (outmoded) Class B network addresses.

a.b

Part a specifies the first byte of the binary address. Part b is interpreted as a 24-bit value that defines the rightmost three bytes of the binary address. This notation is suitable for specifying (outmoded) Class C network addresses.

a

The value a is interpreted as a 32-bit value that is stored directly into the binary address without any byte rearrangement.

This isn't defined by POSIX.anything - but it is available pretty widely.

nerdwaller
  • 17,054
  • 2
  • 44
  • 44
  • 1
    Awesome, and I also checked the post you linked to on Stack Overflow. It had great insights on how different number systems can be used. @grawity [Wikipedia](https://en.wikipedia.org/wiki/Internet_Protocol_version_4#Special-use_addresses) says 0.0.0.0/8 is the current network. I assume 0.0.0.0/32 must be the current host and that ping only expects /32 "networks". I have not found an explicit reference though. – Alexandre de Verteuil Jul 01 '13 at 17:05
  • 2
    @AlexandredeVerteuil: ping expects *hosts*, not networks... so yes, /32 for IPv4. – u1686_grawity Jul 11 '13 at 07:50
4

It's a relic from the old days of classful addressing. 127.1 means network 127, host 1. (And, yes, 127.257 is legal because network 127 can have more than 256 hosts.

David Schwartz
  • 61,528
  • 7
  • 100
  • 149
  • 1
    127.257 meaning in particular 127.0.1.1, an address I actually see on old KMS server activator that worked on Windows 8.0 – Paul Stelian Apr 17 '17 at 18:26