Subject: | binding to port 0 to multi-address hostnames results in different ports on each address |
Since Net::Server 2.00, the package has done the right thing when
binding is requested on a named host that resolves to multiple IP
addresses: it binds to each associated IP address.
Another nice, relatively recent change is support of passing port 0, to
bind to an arbitrary port.
Unfortunately, these two nice features don't play well together.
On an IPv4/IPv6 machine, binding to "localhost" with port 0 makes it so
the port bound on 127.0.0.1 is different from the port bound on ::1.
For example:
0 dkg@pip:~$ perl -e 'use base qw(Net::Server); main->run(host =>
"localhost", port => 0)'
2012/06/25-02:32:01 main (type Net::Server) starting! pid(7338)
Resolved [localhost]:0 to [::1]:0, IPv6
Resolved [localhost]:0 to [127.0.0.1]:0, IPv4
Binding to TCP port 0 on host ::1 with IPv6
Bound to auto-assigned port 49245
Binding to TCP port 0 on host 127.0.0.1 with IPv4
Bound to auto-assigned port 45191
Group Not Defined. Defaulting to EGID '1000 4 20 24 25 27 29 44 46 107
109 110 115 126 1000'
User Not Defined. Defaulting to EUID '1000'
This is a bit surprising, since "localhost:45191" works to reach the
server via IPv4 but not IPv6, and "localhost:49245" works to reach the
server via IPv6 but not IPv4.
Perhaps once an auto-assigned port is successfully chosen, it could be
used in place of port 0 on all subsequently-bound addresses?
This runs the risk that some of the addresses are already in use, of
course, but the same risk exists when using explicit (non-0) port
requests on multi-address hostnames.
Maybe there are other possible solutions?