Search code examples
network-programminglocalhostloopback-address

Why does the ip address 127.001 route to 127.0.0.1 and 127.011 route to 127.0.0.9?


Through an accidental typo I've realized that when you try to reach the ip address 127.0.01 it will successfully route to 127.0.0.1.

Playing around with this, I've noticed that there seems to be some kind of translation happening. Some examples:

  • 127.001 -> 127.0.0.1
  • 127.002 -> 127.0.0.2
  • 127.011 -> 127.0.0.9
  • 127.111 -> 127.0.0.111
  • 127.1111 -> 127.0.4.87
  • 127.9999 -> 127.0.39.15

What's the logic behind these translations? I've tried and replicated this on MacOS, Linux and Windows.


Solution

  • From wikipedia:

    When fewer than four numbers are specified in the address in dotted notation, the last value is treated as an integer of as many bytes as are required to fill out the address to four octets. Thus, the address 127.65530 is equivalent to 127.0.255.250.

    You can do the calculation yourself to check.

    Example:

    127.9999 = 01111111. 00000000 00100111 00001111

    = 127.0. 39. 15