Computer Networking › Section C · The transport layer › Topic C.2

Multiplexing and demultiplexing

A laptop may be streaming music, syncing files and loading three web pages at once, all through one network connection. Port numbers in every segment let the transport layer sort the incoming data to the right program and merge the outgoing data onto the network.

One host, many conversations

Data arriving at a host from the network layer could belong to any of its running network applications. The transport layer does not hand it straight to a process. It delivers it to a socket, the endpoint through which a process sends and receives network data, and a process may hold several sockets. Each socket therefore needs an identifier, and each segment needs header fields saying which socket it is for. Delivering the data in each arriving segment to the correct socket is demultiplexing. Gathering data from many sockets at the sending host, adding headers to make segments and passing them to the network layer is multiplexing. The same idea applies at any layer where one protocol serves several protocols above it.

Port numbers

The fields that make this work are the source port number and the destination port number, the first two fields of both UDP and TCP headers. Each is 16 bits, so ports run from 0 to 65,535. IANA, the body that maintains the official registry, divides them into three ranges. System or well-known ports, 0 to 1023, belong to standard services such as HTTP on 80, HTTPS on 443 and DNS on 53. Registered ports run from 1024 to 49,151. Dynamic or private ports, 49,152 to 65,535, are never assigned to services. A server binds to a fixed, published port so clients can find it; a client usually lets its operating system pick a temporary, or ephemeral, port, and the range used varies by system.

Connectionless demultiplexing: UDP

An unconnected UDP socket is identified by just two values: the destination IP address and the destination port number. When a UDP segment arrives, the host reads its destination port and delivers the data to the socket bound to that port. A consequence follows directly. Two segments from different hosts, or from different source ports on the same host, land in the same socket if their destination address and port match. What, then, is the source port for? It is half of a return address. A server that wants to reply copies the arriving segment's source IP address and source port into the destination fields of its answer.

Worked example: two clients, one UDP socket

This Python 3 sketch runs on one machine. 1. srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) and srv.bind(("127.0.0.1", 9999)) create a server socket on port 9999. 2. Two client sockets, a and b, each call sendto with a short message to ("127.0.0.1", 9999); they never call bind, so the operating system gives each its own ephemeral port. 3. The server calls data, addr = srv.recvfrom(1024) twice. Both messages arrive at the same socket, and addr differs each time, for example ("127.0.0.1", 54765) and then ("127.0.0.1", 54766). 4. The server replies with srv.sendto(data.upper(), addr), so each answer goes back to the right client. Check: one server socket served two clients, told apart only by the source port in each segment.

Connection-oriented demultiplexing: TCP

A TCP connection socket is identified by four values: source IP address, source port, destination IP address and destination port. A TCP server first creates a listening, or welcoming, socket on its published port. A connection request is a TCP segment with that destination port and a special setup flag set. When one arrives, the server's accept call creates a new connection socket, labelled with the request's four values. From then on, every segment matching all four values is delivered to that socket. So two segments with the same destination port but different source addresses or source ports go to different sockets, which is the opposite of what UDP does.

Worked example: three connections to port 443

A web server at 203.0.113.5 listens on port 443. Host P at 198.51.100.7 opens two connections, from source ports 50,001 and 50,002. Host Q at 192.0.2.40 opens one, and happens to choose source port 50,001 too. How many connection sockets does the server need, and is there a clash? 1. Write each four-tuple as source IP, source port, destination IP, destination port. 2. Connection 1 is (198.51.100.7, 50001, 203.0.113.5, 443). 3. Connection 2 is (198.51.100.7, 50002, 203.0.113.5, 443). 4. Connection 3 is (192.0.2.40, 50001, 203.0.113.5, 443). 5. All three tuples differ, so the server keeps three connection sockets plus its listening socket. Check: connections 1 and 3 share a source port but not a source IP, so there is no clash.

Seeing the four-tuple in Python

You can watch TCP demultiplexing on one machine. A server creates a SOCK_STREAM socket, binds it to ("127.0.0.1", 9998) and calls listen. Two clients each call socket.create_connection(("127.0.0.1", 9998)). The server then calls conn, addr = srv.accept() twice and prints conn.getsockname() and conn.getpeername() for each new socket. Both connection sockets report the same local end, port 9998, but different remote ports chosen by the clients' operating system, such as 62578 and 62579. The topic Socket programming explains these calls in more detail.

Servers, processes and threads

Every segment for a web server carries destination port 443 (or 80), including both the setup segments and the requests themselves; the server tells clients apart by source address and port. Some servers start a new process for each connection, but most busy servers run one process and give each connection socket its own thread, or handle many sockets in one event loop, so many sockets can belong to one process. Opening and closing a connection for every request, as non-persistent HTTP does, creates and destroys a socket each time, which adds load on a busy server; persistent connections reuse one socket for many requests.

Open ports and port scanning

A server waiting on an open port reveals that a service is running, and standard port numbers hint at which one. Port scanners such as the free tool nmap probe a host's ports and report which are open, closed or filtered. Administrators use them to audit their own networks for services that should not be exposed. Attackers use the same information to find software with known weaknesses, which is why scanning someone else's systems without permission is often prohibited. The defence is to run only the services you need, keep them patched and block other ports with a firewall, as the topic Operational security: firewalls and intrusion detection describes.

Common mistakes to avoid

First, thinking a port identifies a host; the IP address picks the host, and the port picks the socket on it. Second, assuming UDP keeps senders apart: segments from different sources to the same destination port share one socket. Third, assuming a TCP server needs a new port for each client; all connections share the server port and differ in the client's address or port. Fourth, forgetting that two clients may pick the same source port; only the full four-tuple must be unique. Fifth, equating sockets with processes; one process can hold many sockets.

Key takeaways

Demultiplexing delivers each arriving segment's data to the correct socket; multiplexing gathers data from sockets and adds headers to form segments. Source and destination ports, 16 bits each, make this possible: 0 to 1023 are well-known, 1024 to 49,151 registered and 49,152 to 65,535 dynamic. A UDP socket is identified by destination IP and port, so different senders share it, and the source port is the return address. A TCP connection socket is identified by the four-tuple, so each connection gets its own socket. Open ports reveal running services, which scanners detect.

Flashcards

Flashcard

Socket

The endpoint through which a process sends and receives network data

Transport delivers data to sockets, not directly to processes.

Test your understanding

Question 1 of 5

Two hosts each send a UDP segment to 10.0.0.5, port 5000, from different source ports. Where do the segments go?

Question 2 of 5

What identifies a TCP connection socket on a server?

Question 3 of 5

Host A (source port 40000) and host B (source port 40000) both connect to a server's port 443. What happens?

Question 4 of 5

Why does a UDP segment carry a source port number at all?

Question 5 of 5

Which port range does IANA never assign to a service, leaving it for dynamic use?

← Previous topicNext topic →