Sub topic 1
What the transport layer adds to IP
Prerequisites
This chapter assumes you know the following:
- The layered network model: application, transport, network and link layers, and that each layer wraps the data from the layer above.
- What an IP address is and what a packet is.
- Basic programming. The short examples use Python, but you only need to read them.
You do not need to know anything about TCP or UDP headers yet. Later chapters cover them.
What IP gives you, and what it does not
The network layer, which is IP, has one job: move a packet from one host to another host, identified by IP addresses. It does this on a best-effort basis. Best-effort means IP tries to deliver each packet but promises nothing about the result.
Specifically, IP does not guarantee any of these:
- Delivery. A router with a full queue drops packets. IP does not tell the sender.
- Order. Two packets sent one after the other can take different routes and arrive in reverse order.
- Single delivery. A packet can be duplicated on the way, for example by a link-layer retransmission.
- Integrity of the payload. The IPv4 header checksum covers only the header, not the data. IPv6 has no header checksum at all.
- Timing. Delay varies from packet to packet. This variation is called jitter.
- Which program receives the data. An IP address names a host (more precisely, an interface), not a program running on it.
This design is deliberate. Routers stay simple and fast because they keep no per-conversation state. Any extra guarantee is built at the edges, in the hosts, by the transport layer.
The two things the transport layer always adds
Every transport protocol adds at least two things to IP. Everything else is optional and differs between protocols.
- Process-to-process addressing. The transport layer uses port numbers to direct data to a particular program on the host. IP delivers to a machine, and the transport layer delivers to an application on that machine.
- Error detection on the data. Both TCP and UDP carry a checksum that covers the payload, so corrupted data can be noticed. (UDP over IPv4 allows the checksum to be switched off, but you should not do that.)
On top of that, a transport protocol may add some of these:
- Reliable delivery, using acknowledgments and retransmission.
- In-order delivery, using sequence numbers.
- Flow control, so a fast sender does not overwhelm a slow receiver.
- Congestion control, so senders do not overwhelm the network.
- Connection state, so both ends agree that a conversation exists.
TCP provides all of these. UDP provides only the two required ones. The rest of this tutorial explains how each is done and what it costs.
| Property | IP | UDP | TCP |
|---|---|---|---|
| Addresses | Host | Host and port | Host and port |
| Payload checksum | No | Yes | Yes |
| Reliable delivery | No | No | Yes |
| In-order delivery | No | No | Yes |
| Flow control | No | No | Yes |
| Congestion control | No | No | Yes |
| Connection state | No | No | Yes |
| Message boundaries kept | Per packet | Yes | No (byte stream) |
Where the transport layer runs
The transport layer is implemented in the operating system kernel of the two end hosts. Routers in the middle normally look only at the IP header and forward the packet. They do not read or change TCP or UDP headers.
This is called the end-to-end principle. Reliability is built where the application actually lives, because only the endpoints know whether data really arrived intact and complete. Intermediate devices that do look at transport headers exist, such as firewalls, load balancers and NAT devices, but they are additions to the basic model, and they are a common source of surprising behavior.
The diagram shows how a message passes down through the sending host, crosses routers that look only at IP, and is handed up to the transport layer on the receiving host.
Multiplexing and demultiplexing
A single host runs many networked programs at once: a browser with several tabs, a mail client, a chat program, a DNS resolver. They all share one IP address and one network interface. The transport layer sorts this out.
- Multiplexing happens on the sending side. Data from many applications is gathered, each piece is labeled with a source port and a destination port, and the pieces are handed to IP.
- Demultiplexing happens on the receiving side. The transport layer reads the port numbers in each arriving segment or datagram and hands the data to the right socket.
A port is a 16-bit number, so it ranges from 0 to 65535. By convention:
| Range | Name | Typical use |
|---|---|---|
| 0 to 1023 | Well-known ports | Standard services such as 80 (HTTP), 443 (HTTPS), 53 (DNS), 22 (SSH). Binding usually needs elevated privileges on Unix-like systems. |
| 1024 to 49151 | Registered ports | Assigned to specific applications, but usable by anyone. |
| 49152 to 65535 | Dynamic or ephemeral ports | Picked automatically by the OS for the client side of a connection. Many systems use a different default range. Linux, for example, uses 32768 to 60999. |
A server picks a fixed, well-known port so that clients can find it. A client usually does not care about its own port, so the operating system assigns an unused ephemeral port when the client first sends data or connects.
The five-tuple
A transport conversation is identified by a combination of values:
- Protocol (TCP or UDP)
- Source IP address
- Source port
- Destination IP address
- Destination port
This is the five-tuple. It explains a fact that surprises many beginners: a web server on port 443 can serve thousands of clients at once on that single port, because each connection has a different source address or source port, so each five-tuple is different.
The way the receiver uses the tuple differs by protocol, and this is the first visible difference between them:
- A UDP socket is identified only by its destination address and port. All datagrams sent to that port arrive at the same socket, no matter who sent them.
- A TCP connection is identified by the full four values (addresses and ports) plus the protocol. A listening socket on port 443 spawns a separate socket for each client that connects, and the kernel directs each arriving segment to the right one by matching the full tuple.
TCP and UDP port numbers are separate spaces. A program can use TCP port 9000 while another uses UDP port 9000 on the same host, because the protocol is part of the identity.
Seeing ports in practice
You can see the effect of a kernel-assigned ephemeral port with a few lines of Python. This program connects to a server and prints both ends of the connection. It needs a TCP server listening on port 9000, such as the echo server from the socket chapter. If nothing is listening, create_connection raises ConnectionRefusedError.
import socket
with socket.create_connection(("127.0.0.1", 9000)) as s:
print("my end: ", s.getsockname())
print("server end:", s.getpeername())A typical output looks like this:
my end: ('127.0.0.1', 51734)
server end: ('127.0.0.1', 9000)The server's port, 9000, was chosen by the programmer. The client's port, 51734, was chosen by the operating system. Run the program twice and you will see a different client port each time.
On Linux you can list the same information for all connections with ss -tn for TCP or ss -un for UDP.
Common errors and misconceptions
- "IP delivers data to a program." It does not. IP delivers to a host. Ports belong to the transport layer.
- "UDP is unreliable, so it loses a lot of data." UDP adds no loss. It simply adds no recovery. On a healthy network, most datagrams arrive. The application must still be written to cope with the ones that do not.
- "TCP guarantees my data is processed." TCP guarantees that bytes reach the receiving operating system's buffer, in order. If the receiving program crashes before it reads them, the data is lost, and the sender may never know.
- "A port identifies a connection." A port identifies an endpoint. A connection is identified by the whole tuple.
- "Port numbers are unique across the host." They are unique per protocol and local address, with exceptions such as options that let several sockets share a port. A port in use by one program usually causes
Address already in usewhen a second program tries to bind to it. - "The checksum makes data safe." The checksum catches accidental corruption. It does not stop an attacker, who can recompute it. Security needs encryption and authentication, such as TLS, which runs above the transport layer.
Summary of the chapter
IP moves packets between hosts with no promises. The transport layer runs on the end hosts and adds ports, so data reaches the right program, and a payload checksum, so corruption can be detected. TCP adds reliability, ordering, flow control, congestion control and connection state. UDP adds almost nothing beyond ports and the checksum. The next chapters take these one by one, beginning with how sockets and ports work.
Exercise
A web server listens on TCP port 443. Two browser tabs on the same laptop (IP 192.0.2.10) both connect to it at the same time. The server's IP is 198.51.100.5. How does the server tell the two connections apart, and what would the two five-tuples look like?
Show answer
The two connections differ in the source port, which the laptop's operating system assigns separately to each connection. For example, the tuples are (TCP, 192.0.2.10, 50001, 198.51.100.5, 443) and (TCP, 192.0.2.10, 50002, 198.51.100.5, 443). The protocol, both addresses and the destination port are the same, but the five-tuples are different, so the server's kernel delivers each arriving segment to the correct connection socket.
Exercise
Name two guarantees that UDP does not provide, and two things it does provide beyond what IP gives.
Show answer
UDP does not provide reliable delivery (no retransmission of lost datagrams) and does not provide ordering (datagrams can arrive in any order). Beyond IP, UDP provides port numbers, which allow delivery to a specific program, and a checksum that covers the payload, which allows corrupted datagrams to be detected and discarded.
Exercise
A colleague says, "My data was sent over TCP, so the other program definitely processed it." Explain why this claim is not guaranteed.
Show answer
TCP guarantees only that the bytes arrive, in order and without detected corruption, in the receiving operating system's buffer, and the sender's TCP is told they were acknowledged. If the receiving program crashes, closes the socket, or never calls recv before failing, it never processes the data. Confirming processing requires an acknowledgment at the application level.