Reserving TCP/IP Port for Service

I've heard of requests from some development people asking to pre-reserve a particular TCP/IP port number. I often have to explain to them that there really isn't a way to "reserve" a port address. Sure, you can list the port number with the desired name of the service in the /etc/services file, given that it isn't allready allocated to another service. But this doesn't even guarentee that the port will be "reserved".

The only real way to ensure that a specific port number is available for a service or application is to be the first process to open that port. But suppose that you don't want the service to run all the time, just when necessary?

The solution is to configure the service as an Inetd service. The Inetd superserver starts early enough during the system boot sequence that you're fairly assured your service will get the desired port. The Inetd daemon doesn't actually start the service until a request, a connection request, occurs to the port number the service is configured to use. The Inetd then passes the socket connection as a file descriptor to the service, therefore the service just needs to use the standard files, STDIN and STDOUT to communicate. In fact you don't even need any socket calls in your service. Once the service is completed with the request, the Inetd goes back to listening for more requests.

In order to put your application under Inetd control, you need the following:

    • Read and write on STDIN and STDOUT.

    • Document the ports in /etc/services

    • Create an entry in the inetd.conf or xinetd.d directory that describes your service.

See also: http://sites.google.com/site/tfsidc/Reserving-TCPIP-Port-for-Service