We all know where the 127.0.0.1 is, and most of us probably remember some well-known public DNS servers like 1.1.1.1 or 8.8.8.8.
Most of us prefer to write IPv4 addresses in a dot-decimal notation, consisting of four octets (four decimal numbers) each ranging from 0 to 255.
“In some cases of technical writing, IPv4 addresses may be presented in various hexadecimal, octal, or binary representations.” – Wikipedia
Four octets means 32 bits, and 32 bits can be represented as a single decimal number. For example:
127 * 256³ + 0 * 256² + 0 * 256 + 1
Which means your beloved localhost can also be written as:
2130706433
And yes – it works!
❯ ping 2130706433
PING 2130706433 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.073 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.118 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.083 ms
You can also omit zeroed octets 127.1 or write it in hex 0x7F000001 and it still feels like home!
So if you want to show off a bit of questionable knowledge in front of your colleagues, next time you check connectivity with ping 1.1.
But did you know that it also works for most of the browsers? Just try it yourself http://16843009.
If you’re still here and wondering why this works, the answer is buried in legacy parsing behavior of inet_aton and here is a simple C program that can be used as a playground:
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>
int main(void) {
char input[256];
struct in_addr addr;
printf("IPv4 address: ");
if (scanf("%255s", input) != 1) {
return 1;
}
if (inet_aton(input, &addr) == 0) {
fprintf(stderr, "Invalid address\n");
return 1;
}
uint32_t ip = ntohl(addr.s_addr);
printf("%u.%u.%u.%u\n",
(ip >> 24) & 0xff,
(ip >> 16) & 0xff,
(ip >> 8) & 0xff,
ip & 0xff);
return 0;
}
The next time someone tells you that IPv4 address must contain four numbers separated by dots, you can tell them that they are wrong by opening your browser and navigating to your local dev environment at http://2130706433:8080 (or whatever the port is).
I hope you didn’t implement security filters that only recognize dotted IPv4 notation ;-)