0

I m developing a server in C which bind on a random port

I m afraid that the choosen random port could conflict with iptables rules. I mean if the choosen port could be already exist in iptables as a forwording port.

How I can check if the choosen port does not exist in the iptables rules with C?

MOHAMED
  • 151
  • 7

2 Answers2

1

If your server is using a standard protocol which has already been allocated an official port number, then you should use the official port number.

If the server is using a non-standard protocol without an official port number, you should be using a port number from the range 49152-65535 which is designated: Dynamic and/or Private Ports.

IANA haven't specified which of the numbers to use for dynamic assignments and which to use for locally defined services. However your OS might. In case of Linux any port number dynamically assigned to clients connecting outwards will be taken from the range specified in /proc/sys/net/ipv4/ip_local_port_range

By default this range is 32768 61000, so you need to avoid anything in that range or below. Port numbers above 61000 should be safe to use.

Usually you want a static port number in your configuration such that anybody who need to contact it from outside know which port number to contact. But you can choose a random number from the range and put that in your configuration file to make it a little less likely to be found by port scans or result in a conflict when you need to work with another service which had chosen a port number from the same range.

I usually use this command to pick a port number for any locally defined service:

echo $[61002+RANDOM%4532]
kasperd
  • 30,455
  • 17
  • 76
  • 124
0

Check http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml and choose a port that is not used much. (Preferably over 1024)

Simply make like any other linux deamon do, create and read a .conf to configure the port in case a user want to change it.

yagmoth555
  • 16,758
  • 4
  • 29
  • 50