getaddrinfo() spec doesn't match behaviour
Heiko Wundram (Beenic)
wundram at beenic.net
Sun Feb 3 06:12:47 PST 2008
Hey all!
Before I go post this as a PR (or go about fixing the libc code), I just
wanted to ask whether this is a known issue (and I simply haven't been able
to find it), or if it's simply my stupidity that makes this fail.
Basically, I have the following code:
addrinfo hints;
addrinfo* res;
// Fill in hints structure.
memset(&hints,0,sizeof(hints));
hints.ai_flags = AI_PASSIVE | AI_V4MAPPED;
hints.ai_family = SERV.m_ipv6 ? AF_INET6 : AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Query address info.
if( ( rv = getaddrinfo(bindaddr,port,&hints,&res) ) ) {
if( rv == EAI_SYSTEM )
throw OSError("Failed resolving service and/or bind address");
throw ValueError(gai_strerror(rv));
}
Now, according to the man-page of getaddrinfo() and my previous knowledge,
this should work fine (for some bindaddr and port coming into this function
as const char*), but, alas, it doesn't, and fails with EAI_BADFLAGS.
Tracing through the libc code for getaddrinfo(), the cause of this failing is
pretty obvious:
hints.ai_flags is logically anded with AI_MASK at the beginning of the
function, and AI_MASK (at least in my local netdb.h header) does not contain
the flag AI_V4MAPPED. In case the result of that is non-zero (which it is due
to me specifying AI_V4MAPPED), the function returns EAI_BADFLAGS.
After that, getaddrinfo() does some checks on AI_V4MAPPED and AI_ALL (masking
the respective flags in case the ai_family isn't AF_INET6), which are
basically superfluous (as they can never be set anyway due to AI_MASK), but I
didn't find any other reference to the flag in the whole of
lib/libc/net/getaddrinfo.c, so I actually don't know whether the cause of
this failing is that it simply isn't implemented (completely), or there's any
other reason for AI_MASK not to contain these flags that I haven't grasped so
far.
If anyone out there can shed a hint on this, I'd be grateful, even if it's
just the fact that my netdb.h installation is broken.
Thanks!
--
Heiko Wundram
Product & Application Development
More information about the freebsd-hackers
mailing list