[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
getaddrinfo: how to wildcard everything?
I'm finding myself with a small problem.
I have a program that can be told to listen on a particular (TCP) port.
It can also have a host address specified, but, if not, it uses
getaddrinfo() with a nil host string pointer.
This works fine.
Now, I want to add to this program the ability to let the kernel select
a port, that is, to bind to port 0. It is to then use getsockname and
report the port number. I implemented this by passing nil as the
service string to getaddrinfo().
When a host address is specified, this works fine. But, when no host
address is specified, getaddrinfo() fails, instead of returning port-0
structs for each supported address family.
What is the correct way to loop over all supported address families,
letting the kernel select a suitable port number for each one?
getaddrinfo(0,0,...) is the logically obvious way to do this, but that
doesn't work (to be fair, getaddrinfo(3) specifically says it won't
work - "[o]ne or both of [the first two] arguments must be a non-NULL
pointer" - but that's not much help here.)
Of course, I could just fix getaddrinfo() on my machines, and I may do
that. But that wouldn't help elsewhere, and this is one of the few
programs I actually want to use on other, stock, machines, some of
which are not even running any version of NetBSD. At least one such
other OS has a getaddrinfo(3) that (with different wording) makes the
same statement about non-nil arguments.
So I'm wondering if anyone here has enough insight into the design
decisions behind getaddrinfo() to know what the designer(s) intended
people use to do what I'm trying to do here: bind a port-0 (or moral
equivalent) socket in each supported address family. So far I'm not
seeing any way to do this that doesn't involve writing per-AF code in
the application, which of course is what getaddrinfo() is supposed to
free applications from.
/~\ The ASCII Mouse
\ / Ribbon Campaign
X Against HTML mouse%rodents-montreal.org@localhost
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
Main Index |
Thread Index |