Stupid pet tricks with TCP, ping, and IP addresses

reddit

IPv4 addresses are, as we all know, 32-bit numbers, done in four byte-long chunks, e.g., 127.0.0.1. But there are some silly things you can do with that — the TCP/IP stack is, apparently, kinda savvy that way. So, for example:

ping -c 127.0.0.1 PING 127.0.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.038 ms 

Well, okay, nothing special there. But let's think about what "127.0.0.1" means to the computer — that's a "1" in the ones column, a zero in the 256's column, a zero in the 65,536's column, and a 127 in the 16,777,216's column, or… 2130706433, in decimal. Hmmm, I wonder…

ping -c1 2130706433 PING 2130706433 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.037 ms 

Well, lookee there! That's one pretty smart stack! What about hex?

ping -c1 0x7F000001 PING 0x7F000001 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.038 ms 

Dotted-quad hex?

ping -c1 0x7f.0.0.1 PING 0x7f.0.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.055 ms 

Octal?

ping -c1 017700000001 PING 017700000001 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.052 ms 

Binary?! Well, no — apparently, it does fall down on binary. But still, it's a fun glimpse into how Linux, and the stack, think about IP addresses.

EDIT: because of this post, I did some digging, and found out that this behavior is apparently part of a 2001 POSIX spec, and is nicely documented here: https://linux.die.net/man/3/inet_aton

submitted by /u/ravenpi
[link] [comments]