4 ms·
The most useful case for this shortcut is 127.1 -> 127.0.0.1
by dieulot 9y ago
The most useful case for this shortcut is 127.1 -> 127.0.0.1
- avip 9y agoDon't try that in the wild, most sw out there would ignore spec and use some arbitrary regex to validate IP format. i.e python: octets = ip_str.split('.') if len(octets) != 4: raise AddressValueError("Expected 4 octets in %r" % ip_str)
- sigjuice 9y agoWhat spec says that 127.1 and 127.0.0.1 are equivalent?
- nerdwaller 9y agoI don’t actually think it’s in a spec formally but is in a common c lib[0]. > 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. [0]: https://linux.die.net/man/3/inet_aton https://linux.die.net/man/3/inet_aton
- sigjuice 9y agoThanks. I just came across the man page myself while I was writing this tiny program. $ cat 127.1.c #include <stdio.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { struct in_addr addr; if (inet_aton(argv[1], &addr)) printf("%08x\n", addr.s_addr); return 0; } $ make 127.1 CFLAGS=-Wall cc -Wall 127.1.c -o 127.1 $ ./127.1 1.1 01000001 $ ./127.1 127.1 0100007f
- tzs 9y agoThe POSIX spec (IEEE 1003.1) says the same thing for inet_addr(), so it does occur in an actual spec.
- avip 9y agoYou are right, I faithfully assumed it's a spec without checking. Thanks.
- fwgwgwgch 9y agoWhere have you been all my life?
- mathgeek 9y agoSitting at 127.1, apparently.
- aexaey 9y ago0, which is a shorthand for 0.0.0.0 is likely the most code-golf-y way to write localhost, as many [EDIT: Linux] systems alias 0.0.0.0 to 127.0.0.1: $ ping 0 PING 0 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.032 ms Of course, don't expect this to work universally. A lot of software will try to be clever with input validation, and fail. Tangentially related: https://fosdem.org/2018/schedule/event/email_address_quiz/ https://fosdem.org/2018/schedule/event/email_address_quiz/
- mrkstu 9y agoStays unaliased on macOS: My-MacBook-Pro:bottle mrkstu$ ping 0 PING 0 (0.0.0.0): 56 data bytes ping: sendto: No route to host
- jamiek88 9y agoHowever ping to 127.1 works the same as localhost.
- hartator 9y agoIt's not fully true that 127.0.0.1 is the same as 0.0.0.0. For example, binding a webserver to 0.0.0.0 make it on the public network while 127.0.0.1 is strictly localhost.
- pishpash 9y ago0.0.0.0 is not localhost. It's "any address".
- aexaey 9y agoYes, you're right. What I was trying to say is - On Linux, INADDR_ANY (0.0.0.0) supplied to connect() or sendto() calls is treated as a synonym for INADDR_LOOPBACK (127.0.0.1) address. Not so for bind() or course.