I just noticed that if I f开发者_Python百科eed the string "10 . 10 . 10 . 10" to getaddrinfo i get back the address 0.0.0.10. The result is the same on both OS X (Lion) and Linux (CentOS 5.6). You can easily verify it yourself with:
$ curl 'http://10 . 10 . 10 . 10/'
curl: (7) Failed to connect to 0.0.0.10: No route to host
Is there a specific reason for this behavior or should I consider it a bug? I would've expected it to fail to return any address.
Edit: Same result for "10 asdf", guess it's the space that makes it do that.
POSIX allows a string consisting of a single number (in either decimal, hex with a leading 0x
or 0X
, or octal with a leading 0
) to specify an IPv4 address: see the definition of inet_addr()
(referenced by the definition of getaddrinfo()
for the AF_INET
address family).
As far as I can see, it doesn't say anything about how the input string is terminated, so the fact that it stops at a space is probably just an implementation detail; but "10" is certainly an allowable representation of an IPv4 address, if it's parsed by getaddrinfo()
or inet_addr()
. (Interestingly, this form is not allowed by inet_pton()
with AF_INET
- that only accepts the standard four-component form.)
精彩评论