This is another entry in a series of articles in which we talk about the fundamentals of how the internet works. We started with switches and routers and moved onto a bird’s eye view of the entire network stack.
In this article we will take a closer look at what is known as the Transport Layer (Layer 4) in the OSI model. This is the layer that segments, verifies and controls the flow of information between computers, and assigns various connections to ports. While Layer 4 is commonly associated with TCP (Transmission Control Protocol), there are other protocols that can occupy this spot. The second most common layer-4 protocol is UDP (User Datagram Protocol). In this article we will go over the similarities, differences and uses of these protocols.
What is TCP?
TCP is a protocol built atop IP that handles the segmentation, reassembly and verification of pieces of data sent between computers, known as packets. It is a connection-oriented protocol in that computers must acknowledge each other to be able to communicate.
TCP’s strength is correctness; it extensively error-checks and verifies messages such that no info is lost. Furthermore, TCP sends data in a particular sequence. The result is that any data sent via TCP will be delivered in its intended format.
Before sending and receiving data, TCP requires authentication for both computers in the form of a three-way handshake. The host establishing the connection sends a ‘SYN’ packet to the other host. The second host sends a ‘SYN-ACK’ packet to the first, and the first sends an ‘ACK’ to the second.
TCP is the most common protocol on the internet, and for good reason. TCP is the correct choice whenever a high level of data precision is required. Some services that rely on TCP are:
- HTTP/HTTPS (the protocol that delivers web pages)
- FTP (File Transfer Protocol)
- Email protocols (POP, IMAP, SMTP)
- Remote terminals (SSH, Telnet)
What is UDP?
UDP also sits atop IP and handles segmentation and reassembly of data sent over the internet, but in all other respects it is different.
UDP is connectionless; computers do not have to agree to exchange data before any transfer occurs. Packets sent via UDP are not ordered; they are send in a continuous stream which may affect how the data appears once delivered. UDP does not have the extensive error checking of TCP. UDP does not guarantee the delivery of data. Finally, UDP does not offer flow control like TCP does.
The major upside of UDP is its speed. UDP is a light-weight and fast protocol because it does not feature the safety checks of TCP, nor does it need to establish connections. UDP is preferable to TCP when time is more of a concern than accuracy.
UDP is most commonly used for voice and video communication. These mediums are well suited for UDP because corrupting a millisecond of audio or a single frame of video will hardly ruin the entire communication.
Another interesting use case for UDP is online video games. Communicating the states of hundreds of game objects in real time with 100% accuracy may be infeasible, so game developers sacrifice this accuracy to deliver this data on time as you play.
Other services that use UDP:
- DNS (Domain Name Service): the protocol that allows domain names (www.example.com) to map to IP addresses
- NTP (Network Time Protocol): a protocol for clock synchronization
- VPN tunneling (encrypted communications between the VPN and the public internet)
A Bit More About Layer 4
It’s important to remember that TCP and UDP occupy the same “spot” in the OSI model. As such they have access to the same set of ports. If one port is occupied by a TCP service, it cannot be used by a UDP service.
Transport Layer Security (TLS) is often said to inhabit Layer 4 as it too handles the authentication of connection endpoints and verification of data, but would likely fit into the Presentation Layer (Layer 6) as it also handles the encryption of data. Furthermore, it is built on top of TCP and applications must provide their own implementation of TLS. For example, HTTPS is the same as HTTP but with TLS included. This is a topic we will cover in a future article.
At SafeHouse, we make heavy use of TCP/TLS in our software to secure your private network. If you found what you read informative, consider checking out our site at www.safehouse.dev.