online posts 15
networking 101: how computers talk

lesson 5.1 · chapter 5 of 5: ports · 35 min

Ports: TCP, UDP and the common numbers

objective

Say what a port is and why a message needs one, tell TCP from UDP, read a socket (address and port), watch a three-way handshake, and know the common port numbers, and why firewalls care about them.

1 explain

Which program is this for?

One address, lots of programs

A block of flats has one street address, but dozens of people live there. A parcel needs the street address and a flat number, or it sits in the hall.

A computer is the same. Its IP address (lesson 3.1) gets a message to the right computer. But on that computer a web browser, an email app and a video call are all running at once, and the message is for one of them. The flat number is the port number: a number from 0 to 65535 that says which program.

You met ports in lesson 1.2: they’re layer 4, transport. And in lesson 4.1, DHCP’s messages went to port 67 on the server and back to port 68 on the laptop.

in one sentence The IP address finds the computer; the port number finds the program on it.

Listening, and source and destination ports

A program that offers a service, like a web server, listens on a port: it tells the computer anything that arrives for port 443 is mine. Everyone knows the number in advance, so nobody has to ask.

Every message carries two ports, like it carries two IP addresses:

Who picks itExample, laptop to web server
destination portthe service’s well-known number443: the secure web server
source portthe computer that starts the conversation picks a spare one, for this conversation only51000

That temporary source port is called an ephemeral port (ephemeral means short-lived). When the server answers, it swaps them round: the answer comes from 443 and goes to 51000, so the laptop knows which program, and which conversation, it’s for.

Which numbers count as spare depends on the system. Windows and macOS pick from 49152 to 65535; Linux, by default, from 32768 to 60999.

in one sentence The destination port is the service’s well-known number; the source port is a temporary one the asking computer picks.

Sockets

An IP address and a port together are called a socket, written with a colon: 192.168.1.10:51000. A conversation between two programs is two sockets, one at each end:

192.168.1.10:51000  ⇄  192.168.2.10:443
   the laptop            the web server

Open a second tab to the same site and you get a second conversation, 192.168.1.10:51001 ⇄ 192.168.2.10:443. The server’s side is the same; the laptop’s port is different. That’s how one computer keeps dozens of conversations apart.

in one sentence A socket is an IP address plus a port; a conversation is a pair of sockets.

Two ways to send: TCP and UDP

Layer 4 has two main protocols, and every port number belongs to one of them (or to both, separately: TCP port 53 and UDP port 53 are two different doors).

TCPUDP
stands forTransmission Control ProtocolUser Datagram Protocol
likea phone call: you say hello first, and you know if the other side hung upa postcard: you write it and post it
before sendinga three-way handshake sets up a connectionnothing: it just sends
lost piecesnoticed and sent again; pieces arrive in orderlost is lost; the program has to ask again if it cares
costslower to start, more to keep track ofquick and light
used forweb pages, email, file copies, logging in to a router: anything that must arrive wholeDNS questions, DHCP, the time (NTP), monitoring (SNMP), voice and video calls

Voice and video use UDP for a reason: a bit of sound that arrives late is no use, so there’s no point sending it again. Better to skip it and carry on.

in one sentence TCP sets up a connection and makes sure everything arrives; UDP just sends, and is quicker for it.

The three-way handshake

Before TCP sends any data, the two sides agree to talk. Three messages, named after the flags they carry (a flag is a switch in the TCP header, on or off):

  1. SYN (synchronise): the laptop says I’d like to talk, and I’ll number my pieces starting here.
  2. SYN-ACK: the server says OK (ACK, acknowledge) and here’s where my numbering starts (SYN).
  3. ACK: the laptop says got it.

Now the connection is established, and data flows both ways. Every piece is numbered, and the other side acknowledges what it got, so anything missing is noticed and sent again. When they’re done, each side sends a FIN (finish) to close it.

in one sentence SYN, SYN-ACK, ACK: TCP’s hello, before any data.

Three ranges

The numbers 0 to 65535 are split into three ranges. IANA, the Internet Assigned Numbers Authority, the organisation that keeps the official list, writes down who each number is for.

RangeNameWhat’s in it
0–1023well-known (system) portsthe core services: web, email, DNS, SSH. On Linux, only an administrator’s program may listen on these
1024–49151registered (user) portsservices a company or project asked IANA for, like RDP on 3389
49152–65535dynamic (private) portsnever given to anyone: free for ephemeral ports
2 show

The common ports

You don’t need all 65,536. These are the ones network engineers see every day, from IANA’s list and the RFC (the standard) for each protocol:

PortProtocolNameWhat it’s for
20, 21TCPFTPFile Transfer Protocol: copying files. 21 carries the commands; 20 carries the files in FTP’s older active mode. Sends passwords as plain text
22TCPSSHSecure Shell: logging in to a switch, router or server and typing commands, encrypted. Also carries file copies (SCP, SFTP)
23TCPTelnetthe same job as SSH, but not encrypted: anyone who can see the traffic can read the password. Turned off on modern kit
25TCPSMTPSimple Mail Transfer Protocol: email servers passing mail to each other. Your mail app usually sends on 587 instead (submission)
53UDP and TCPDNSDomain Name System: names to addresses. Questions go over UDP; big answers and copies of whole zones go over TCP
67, 68UDPDHCP67 is the server, 68 the client: lesson 4.1
69UDPTFTPTrivial File Transfer Protocol: a very simple file copy, no password at all. Often used to load software onto network kit
80TCPHTTPweb pages, not encrypted
110, 143TCPPOP3, IMAPcollecting email from a mail server (encrypted versions: 995, 993)
123UDPNTPNetwork Time Protocol: keeps every device’s clock right, which matters when you compare their logs
161, 162UDPSNMPSimple Network Management Protocol: monitoring. The monitoring server asks devices on 161; devices send alerts (traps) to it on 162
179TCPBGPBorder Gateway Protocol: routers telling each other which networks they can reach, across the internet
389, 636TCPLDAP, LDAPSlooking up users and passwords in a directory. 636 is the encrypted one
443TCP (and UDP)HTTPSweb pages, encrypted with TLS. The newest version, HTTP/3, usually runs on UDP 443
514UDPsyslogdevices sending their log messages to a log server (over TLS, it’s TCP 6514)
3389TCPRDPRemote Desktop Protocol: using a Windows computer’s screen from somewhere else

Notice the pairs: plain and encrypted versions of the same service usually have different ports (80 and 443, 143 and 993, 389 and 636). The port tells you which one you’re talking to.

A port number is an agreement, not a law. You can run a web server on port 8080, and people often do. The well-known numbers are just where everyone looks first.

in one sentence A handful of numbers cover most of what you’ll see: 22, 53, 80, 443, and the rest of this table.

3 follow along

Watch the ports

A laptop at 192.168.1.10 looks up a name, then opens a secure web page, twice. Press Play, or step with →. In each step, look at layer 4 in the box under the diagram: the protocol, and the two ports.

What to notice:

  1. The destination port on the way out is always the service’s well-known number: 53, 443.
  2. The source port is one the laptop picks, a new one for each conversation.
  3. On the way back, the two ports swap places.
  4. UDP: one message each way. TCP: three messages of handshake before any data.

in one sentence Out to the service’s port from a temporary one; back with the two swapped.

Why firewalls care about ports

A firewall sits between networks and decides which messages may pass. A router can do a simpler version of the same job with an ACL (access control list): a list of permit and deny lines it checks, top to bottom. Both decide mostly on four things in each message: the protocol (TCP or UDP), the source and destination IP address, and the destination port.

The destination port is what says which service. So let people reach the web server, and nothing else on it becomes allow TCP to 192.168.2.10 port 443. Here it is as a router’s ACL line:

permit tcp any host 192.168.2.10 eq 443

Permit TCP from any address to the one host 192.168.2.10, when the destination port equals 443. At the end of every list is an unwritten deny everything else, so SSH, Telnet and the rest are blocked without anyone writing a line for them.

What about the answers coming back? They’re from port 443 to some ephemeral port, which nobody can know in advance. A stateful firewall solves that: it remembers each conversation it let out, and lets that conversation’s answers back in. That’s what the established in the table above is for.

in one sentence Firewalls and ACLs pick which services to allow by protocol and destination port; a stateful firewall lets the answers back in.

Try it on your own computer (optional)

You can list your own computer’s conversations, with both sockets for each.

On Windows, open Command Prompt and type:

netstat -an

On a Mac, open Terminal and type:

netstat -an -p tcp

Each line is one conversation: your socket on the left (Local Address), the other end’s on the right (Foreign Address), and its state. ESTABLISHED is a connection that finished its handshake; LISTEN (Windows says LISTENING) is a program waiting for one. Look for a :443 on the right: that’s a secure website, and your side of it has a high, temporary port. (On a Mac, the port comes after a dot instead of a colon: 192.168.1.10.51000.)

4 challenge

Get past the firewall

A small office has this firewall in front of its servers:

#protocoltoportaction
1TCP10.1.1.2022allow
2TCP10.1.1.30443allow
3UDP10.1.1.5353allow
4anyanyanydeny

For each attempt below, say which rule it matches and whether it gets through:

  1. an engineer logs in to 10.1.1.20 with SSH;
  2. the same engineer tries Telnet to 10.1.1.20;
  3. a browser opens https://10.1.1.30;
  4. a browser opens http://10.1.1.30 (no s);
  5. a laptop asks 10.1.1.53 for a name, in a normal small DNS question;
  6. the same laptop asks 10.1.1.53 for a name whose answer is too big for one UDP message.
hint

Turn each attempt into protocol and destination port first, from the common-ports table. Then go down the rules from the top: the first one that matches wins.

hint

http:// and https:// are different ports. And when a DNS answer doesn’t fit, the computer asks again over the other protocol, on the same port number.

show the solution
AttemptProtocol and portRuleResult
1SSH to 10.1.1.20TCP 221allowed
2Telnet to 10.1.1.20TCP 234blocked: the right server, but the wrong port
3HTTPS to 10.1.1.30TCP 4432allowed
4HTTP to 10.1.1.30TCP 804blocked: plain HTTP is a different port
5DNS question to 10.1.1.53UDP 533allowed
6big DNS answer from 10.1.1.53TCP 534blocked: rule 3 only allows UDP

Number 6 is a real fault: everyday names work, and the odd one with a big answer fails, which is confusing until you remember DNS uses both UDP and TCP on port 53. The fix is a fifth rule, above the deny: allow TCP to 10.1.1.53 port 53.

5 check: test yourself

Pick an answer for each question. You'll see straight away whether it's right, and why. Questions marked what happens next? are about the animations.

  1. A laptop opens a secure web page. What’s the destination port of its first message?

  2. Which one sends SYN, SYN-ACK, ACK before any data?

  3. Why does DNS usually use UDP?

  4. Two browser tabs on one laptop talk to the same web server, both to port 443. How does the laptop tell the answers apart?

  5. A firewall rule says allow TCP to 192.168.2.10 port 443. Someone tries Telnet to 192.168.2.10. What happens?