Project

General

Profile

Bug #11616

getaddrinfo AI_NUMERICSERV doesn't work

Added by Rich Lowe 4 months ago. Updated 4 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
lib - userland libraries
Start date:
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:

Description

#include <sys/socket.h>

#include <err.h>
#include <netdb.h>
#include <strings.h>

int
main(int argc, char **argv)
{
    struct addrinfo hints, *ai;
    int ret;

    bzero(&hints, sizeof (hints));

    hints.ai_flags = AI_NUMERICHOST;

    if ((ret = getaddrinfo("127.0.0.1", "80",  &hints, &ai)) != 0)
        errx(1, "couldn't lookup localhost/80: %d=%s", ret,
            gai_strerror(ret));

    hints.ai_flags = (AI_NUMERICHOST | AI_NUMERICSERV);
    if ((ret = getaddrinfo("127.0.0.1", "80",  &hints, &ai)) != 0)
        errx(1, "couldn't lookup with numericserv localhost/80: %d=%s", ret,
            gai_strerror(ret));
}
; cc -Wall -m64 gai.c -o gai -lsocket -lnsl
; ./gai
gai: couldn't lookup with numericserv localhost/80: 8=node name or service name not known

"80" seems sufficiently numeric to me?
Looking at the code, there's a whole bunch of stuff we don't do in the NUMERICSERV case, but do do when otherwise given a numeric argument, though I'm not certain if it's relevant?

History

#1

Updated by Rich Lowe 4 months ago

It seems this is because the RFC says that in this case we cannot use the name service, and without a socket type, we can't tell what the options are so we... fail.

The code has comments suggesting that we could walk the services file for a local-only lookup.
It seems like we could also return every protocol as a possibility?

#2

Updated by Rich Lowe 4 months ago

Linux gives answers with STREAM/TCP, DGRAM/UDP, and RAW/0, which seems to match my "one of each" suggestion.

Also available in: Atom PDF