Anyone know what function or file in linux holds the algorithm that finds a random port to use for the bind() system call? I'm hunting all over and cannot find the method that contains this algorithm in the Linux source.
Thanks!
The source port is most often randomly picked by the computer. The source port can be used by routers and firewalls in the network to distinguish between different communication flows or sessions since each session will have a different random source port associated with it.
The port number is chosen by the TCP implementation software from a range of port numbers called Ephemeral Ports. The exact mechanism for choosing the port number and the range to be used is Operating System dependent.
Source ports are randomly generated from the unregistered port range. The source/destination port works similar to your IP. The port you send from, is the port the service will reply too. For instance; a website is simply a server listening for connections on port 80 (or 443).
Registered port numbers are currently assigned by the Internet Assigned Numbers Authority (IANA) and were assigned by Internet Corporation for Assigned Names and Numbers (ICANN) before March 21, 2001, and were assigned by the Information Sciences Institute (USC/ISI) before 1998.
It's a long and complicated piece of code, which I'm not keen to try to grok. :-)
Have a look at the inet_csk_get_port
function (in net/ipv4/inet_connection_sock.c
) for TCP, and udp_lib_get_port
(in net/ipv4/udp.c
) for UDP. It's valid, at least, for 2.6.31, though it may vary for different versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With