Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who is wrong about the http://0:port?

Tags:

perl

lwp

psgi

The Plack suite commonly uses the http://0:port. E.g. the following

plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");'

prints

HTTP::Server::PSGI: Accepting connections at http://0:5000/

However, the LWP::UserAgent (or some deeper called modules) didn't accepts it, e.g. the:

perl -MLWP::UserAgent -E '$u=LWP::UserAgent->new;$res=$u->get("http://0:5000/valid/path");print $res->status_line'

prints:

500 No Host option provided

but the

perl -MLWP::UserAgent -E '$u=LWP::UserAgent->new;$res=$u->get("http://localhost:5000/valid/path");print $res->status_line'

prints

200 OK

The question is: who is wrong?

  • Is the http://0:port valid, e.g. the LWP is "wrong"
  • or it isn't valid and the PSGI uses it as only "randomly valid" shortcut?
like image 685
kobame Avatar asked Jul 06 '15 09:07

kobame


People also ask

Is port 0 a valid port?

A port number is a 16-bit unsigned integer, thus ranging from 0 to 65535. For TCP, port number 0 is reserved and cannot be used, while for UDP, the source port is optional and a value of zero means no port.

Why is port 0 used?

Port 0 is a wildcard port that tells the system to find a suitable port number. Unlike most port numbers, port 0 is a reserved port in TCP/IP networking, meaning that it should not be used in TCP or UDP messages. Network ports in TCP and UDP range from number zero up to 65535.

Should I block port 0?

As best practice, Port 0 should not be seen or used on your network, although this port is a valid TCP/UDP port, it is highly recommend that one should block any traffic using this port at your firewall.

What will happen when you bind port 0?

When trying to bind on port 0, actually a random port is selected.


1 Answers

The output of the Plack suite is the output of a server. A server typically binds a socket to a certain port and address in order to serve content there. The notation http://0:port means in this case: listen on port port on all addresses of this machine. This is handy if you don't know or don't want to specify all addresses of the machine where the server should be reachable.

The output of the LWP::UserAgent ist the output of a client. In order to open a connection to a server, you must explicitly specify the address and the port to connect to. 0 is no valid IP address, therefore the connection fails when you connect to http://0:port.

like image 50
eckes Avatar answered Oct 22 '22 01:10

eckes