Today, let's take a look at the ports of TCP and UDP packets. It has always been known that the port is used to distinguish different services of servers with the same IP address, and the port size is already known. When searching for traceroute information, I learned something that I didn't notice before.
(1) Preparations: Port Introduction
This part should be about the role of the port and other knowledge. However, there are too many blog posts on the Internet. So I can borrow a blog post for your reference-Understanding IP addresses and ports
(2) port range
Both the source port and destination port are used in the TCP and UDP (for example) packet structures.
For example, the length is 16 bits, so the port number range is 0 to (2 ^ 16-1), that is, 0 to 65535. 0 to 1023 are the system ports specified by IANA, that is, the system retention window.
More advanced,
According to the RFC 6335 documentation. The port number is actually divided into three ranges:
O system port, which is a recognized port, ranging from 0 to 1023.
O User port, also known as the registered port, from 1024 to 49151.
O temporary port, also known as the dynamic port (ephemeral port), from 49152 to 65535.
(3) Use of Ports
① System port, which is the default reserved port for the system to provide existing services. For example, HTTP is port 80 and DNS is port 53.
② User port, that is, the port bound by the user program. Generally, when a developer develops a new service, the port in this range is bound.
Note: The service bound to the preceding port has the corresponding entry list of TCP and UDP port numbers on Wikipedia.
③ Temporary ports, which are generally not accepted as bound to those services and can be used for any purpose.
(4) The temporary Port
Generally, when the client and the server are connected, services are differentiated by a single triplet (Server IP, server port, Client IP, and client port. Except the client port, the other three are basically definite. For the client port, unless the client program shows that a port number is specified, the IP stack selects a port number from the temporary port number by default to bind to the local port for service differentiation.
When the connection is disconnected, this port can be reused. Note that most IP stacks will not immediately reuse the port after it is released. Instead, they will re-use the port until the temporary port is used. This explains why every time a client program is run, it does not obtain the same port.
In addition, different operating systems have different limits on the temporary port range. The number of temporary port numbers is limited by the number of independent connections to the same host within the same time. In particular, when FTP is used, if a large number of files need to be transferred at the same time, it is easy to see that the temporary port number is insufficient, then, the IP stack will throw the error "the address has been used.
Appendix: On the ncftp official website, we provide a document describing the temporary port in detail-the ephemeral port range, which mentions the specific number of temporary ports in the operating system. If you are interested, take a look.
Analysis of TCP and UDP port numbers for computer networks