|Lesson 4 || Port numbers |
|Objective ||Describe how a client process contacts its corresponding server process using port numbers. |
Linux Port Numbers
For client/server transactions to take place, a client process on machine A needs a way to contact its corresponding server process on machine B.
The machine's IP address
is insufficient, because many server processes may be running on machine B.
Thus, the transport layer
(TCP and UDP) provides an additional level of addressing, called port numbers
TCP and UDP both have a 16-bit range of port numbers (so that, for example, TCP port 50 and UDP port 50 are different).
- IP address: Server or Host address. Usually represented in dotted decimal notation, eg., 255.255.0.0.
- Transport layer: Provides communication sessions between computers.
Question: Do TCP and UDP both have a 16-bit range of port numbers?
Yes, both TCP and UDP have a 16-bit range of port numbers. This means that each protocol has 65,536 (2^16) available ports, ranging from 0 to 65,535. Port numbers are used to identify specific processes or services running on a device and to multiplex multiple connections over a single IP address.
Port numbers can be categorized into three ranges:
- Well-known ports (0-1023): These ports are assigned by the Internet Assigned Numbers Authority (IANA) for widely used services and protocols, such as HTTP (port 80), HTTPS (port 443), FTP (port 21), and DNS (port 53). Both TCP and UDP have their own set of well-known ports, and some services use the same port number for both protocols.
- Registered ports (1024-49151): These ports are registered with IANA by software applications that require specific port numbers but are not as widely used as well-known ports. Registered ports are used by many applications and services, and a specific port number may be associated with different applications depending on the protocol (TCP or UDP) being used.
- Dynamic or private ports (49152-65535): These ports are not controlled by IANA and can be used by applications without registration. They are often used for ephemeral (short-lived) connections, such as when a client connects to a server using a well-known or registered port, and the server assigns a dynamic port for the duration of the communication.
Both TCP and UDP use this 16-bit range of port numbers to distinguish between different services and connections on a device. However, it's important to note that TCP and UDP ports are distinct and independent, meaning that the same port number can be used simultaneously by different processes or services using TCP and UDP without conflict.
Associated Port Number
Each end of a network connection using TCP or UDP has an associated port number. TCP/IP rules say that a connection between machine A and machine B is uniquely identified by four numbers together:
- Machine A's IP address
- The port number used on machine A
- Machine B's IP address
- The port number used on machine B
Two connections are considered different if any ONE of these four numbers differ. For example, many connections may use port 23 on machine B
and port 23 on other machines, because the IP addresses of those other machines will be different from that of machine B.
A server program has to provide its identity to the client programs by way of listening on a specific port. Port is a unique number that identifies a connection or specific services on a given host. When we say identifying specific connection on specific port it means that the server application needs to register its service with the kernel by way of port number. When we request a kernel to register our service, a unique port number is provided by server application to the kernel to associate its services with this number. This port number should be known to the client application so that it can send its request to the host machine running this service. Let us see what all interfaces are providing to hook its services with specific port number and register its service with the kernel.
We want to start service using TCP transport protocol. The first step is to make a socket() system call .
The socket is a framework to communicate with the network protocol within the kernel.
This call opens a socket in the kernel. The arguments to the socket call are AF_INET and SOCK_STREAM. This means that we want to open an internet family socket of type STREAM referring to TCP. The socket initializes INET socket and TCP protocol specific data structures and a set of operations. It links the socket with the VFS, which is then associated with the file descriptor and returned to the application.
Now using this file descriptor, the server can request to kernel any operation on the socket.
Bind Socket with Specific port number
The next step is to bind the socket with a specific port number by making the bind() system call .
This is the way we are requesting a kernel to allocate a specific port number to its service. Here comes the concept of socket address whose C equivalent is sockaddr_in . This has two fields:
- port number and
- IP address.
If the host machine has more than one interface, an application can request a kernel to bind the socket with a given interface or with all the available interfaces. This means that application may want to accept connection requests from only one interface or from all the available interfaces. In the former case, the sin_addr field of the socket address is initialized to the specific IP address and the same field needs to be initialized to INADDR_ANY in the latter case.
Since this is INET address family, the sin_family field of the socket address is initialized to AF_INET. The port number to which we want to glue the services is initialized.
The socket address is now ready for registration as object sockaddr_in.
The socket address is passed to bind() call. If the return value is less than zero, the socket could not be bound to the given port number because there may be any reason, including the fact that a port number may already be allocated to some other services. Otherwise, we got the port number that was requested.