Operating System - Tru64 Unix
1827889 Members
1832 Online
109969 Solutions
New Discussion

Choosing a port number for my server.

 
SOLVED
Go to solution
Senthil_Kumar
Occasional Contributor

Choosing a port number for my server.

I am writing an application that will be run
by root user. This will be used by many
customers. I need to define a port number for
this server. I understand that a good way of
doing this is to hardcode a service name
against a 'Registered' port # (in the range
1024-49151), in the /etc/services file and
then make the client use getservbyname() to
lookup the actual port number. Is this the
correct/best way to do this ?
Are there any other issues to be considered
while selecting a port # ?

- Senthil.
5 REPLIES 5
Al Licause
Trusted Contributor

Re: Choosing a port number for my server.

Make a very thorough study of all of the available on line documentation, in particular the network programming guides for Tru64unix.

Then make your program as flexible as possible. Hardcoding something like a port number is risky. You'd be better off having a configuration file and/or a switch that would allow a change to the port number after the program has been compiled.

Can't tell you how many customers we have talked with having to deal with an old program that is inflexible and is now in conflict with another program.
Ivan Ferreira
Honored Contributor

Re: Choosing a port number for my server.

You can also improve security by not running the program as root. If you will open an unprivileged port, the program does not needs to run as root. And just ensure that port # is not a whell known port for a service that you may want to use in the future.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Steven Schweda
Honored Contributor

Re: Choosing a port number for my server.

> Hardcoding something like a port number is
> risky [...]

Perhaps that's why the OP said "hardcode a
service name", rather than "hardcode a port
number".

And, yes, that's the usual way to do this
sort of thing.

The primary consideration when selecting a
port number is avoiding a collision with
other services which use user-selected port
numbers. Having a service name in
"/etc/services" (or the corresponding NIS
data base) is a good way to do this.

Some effort to coordinate port assignments
may still be needed if you need to deal with
external systems whose "services" file (or
NIS data base) may already include locally
defined services.
Senthil_Kumar
Occasional Contributor

Re: Choosing a port number for my server.

Thanks to everyone for the quick
replies !!

>> You can also improve security by not
>> running the program as root.
>> If you will open an unprivileged port,
>> the program does not needs to run as
>> root.
True. But I need to run as root for
some other reasons.

>> Some effort to coordinate port
>> assignments may still be needed if you
>> need to deal with external systems
>> whose "services" file (or
>> NIS data base) may already include
>> locally defined services.

So, What is this NIS data base ? Is it
the same /etc/services file on the NIS
server or is it some other file ? (In other
words, does getservbyname() on a NIS
client, look for /etc/services file on the
NIS server too ?)
Steven Schweda
Honored Contributor
Solution

Re: Choosing a port number for my server.

> So, What is this NIS data base ?

It could be anything, depending on how NIS
is configured. The default is probably
"/etc/services" on the NIS master, however.
I'm not up-to-date on NIS, but these data
were once kept in "/var/yp/Makefile" on the
NIS master server.

What getservbyname() does is governed by the
system configuration. On Solaris,
"/etc/nsswitch.conf" tells it where to get
its information. On Tru64, it's
"/etc/nsswitch.conf" or "/etc/svc.conf".
(When it's using NIS, getservbyname() calls
NIS functions which communicate with an NIS
server. It will not access the NIS data base
file directly.)

In general, you just use getservbyname(), and
let the system manager worry about where the
actual data are kept.

See "man getservbyname" for the local fine
print.