What this course is, and how to use it
This is the ninth course in University Computer Science: computer networks 计算机网络. It explains how a message crosses the world by jumping between machines that owe each other nothing, and what each layer promises to the layer above it.
A limit, stated once: this is a study course, not a degree. Every header, checksum and sequence number in this handout is decoded byte by byte, on the exact values printed here, so you can check each one.
- Units 9.1–9.3 move a frame: layers, the link layer and IP, and routing.
- Units 9.4–9.5 make it reliable and useful: transport and application protocols.
- Units 9.6–9.7 secure it and grow it: TLS and the basics of distributed systems.
The habit to build: a protocol is a promise about bytes. Each unit decodes one promise at a time.
Layering and architecture
Why it matters. No single machine can do the whole journey. Layering 分层 lets each part be built, tested and replaced on its own. This is why Wi-Fi could change without any change to your browser.


The idea. Each layer offers a service 服务 to the layer above and uses the service of the layer below. The five-layer stack of this course is:
- physical 物理: bits as signals;
- link 链路: frames between neighbours;
- network 网络: packets between any two machines;
- transport 传输: data between programs;
- application 应用: the meaning of the data.
Going down, the data is encapsulated 封装: each layer adds its own header. Going up, each layer removes its header again.

Why layers are better than one big system. A change inside one layer needs no change anywhere else, as long as the service interface stays the same. Ethernet went from 10 Mb/s to 100 Gb/s with the same IP on top, because the link service (frames delivered on a best-effort basis) never changed. A "leaky" interface, where an upper layer reads a lower layer's header, destroys this independence: a program that depends on another layer's details breaks when that layer changes.
Worked example 1: one message, wrapped twice. A TCP segment with a 20-byte header travels inside a 20-byte IPv4 header, inside a 14-byte Ethernet header. On the wire the frame carries $14 + 20 + 20 = 54$ bytes of headers in front of the data. At the receiver, the network card removes 14 bytes, the IP layer removes 20, and TCP removes its 20. Each layer sees only its own part.
Worked example 2: the reference models. Compare the 5-layer course stack with the 7-layer OSI model. OSI has the same physical and link layers, and adds presentation and session layers below the application. The internet uses an "hourglass" shape: a narrow waist (IP), a wide top (many applications) and a wide bottom (many kinds of link). One protocol at the waist lets everything above work with everything below.
Common mistakes.
- Getting the direction wrong: services point up the stack.
- Mixing up the service (the promise) and the protocol (the bytes that keep the promise).
- Leaving out the physical layer when calculating delay. Signal travel time is physics, not software.
Summary. Services go up, protocols run between equal layers, and encapsulation joins them. The interface gives the independence. IP is the waist of the hourglass.
Link layer and IP
Why it matters. The link layer moves a frame between neighbours; IP moves a packet between machines that may be far apart. Mixing up the two kinds of address is the classic beginner's mistake.


The idea.
- An Ethernet frame has a 6-byte destination MAC address MAC 地址, a 6-byte source address, a 2-byte type, the payload, and a 4-byte CRC 循环冗余校验 check value.
- An IPv4 header has version and IHL (header length), total length, TTL 生存时间 (time to live), protocol, checksum, and a 4-byte source and destination IP address IP 地址.

Why the IP checksum covers only the header. The CRC protects the whole frame, on each link. The IPv4 checksum protects only the header and is checked and updated at each router. The reason: routers change the header (they lower the TTL by 1), but they should not take responsibility for the data. Each check covers exactly the part that the device changing it can touch.
Worked example 1: decoding a header. The course bytes are
45 00 00 3C ... 40 06 ...:
| bytes | field | value |
|---|---|---|
4 |
version | 4 |
5 |
IHL | 5 words, so $5 \times 4 = 20$ bytes |
00 3C |
total length | $0x003C = 60$ bytes (20 header + 40 payload) |
40 |
TTL | $0x40 = 64$ |
06 |
protocol | 6 = TCP |
Worked example 2: addresses at two scales. The MAC addresses change at every hop, from frame to frame, because they name neighbours. IP addresses normally identify the two ends, although network address translation can change them in transit. A packet that crosses 3 routers travels in 4 frames with 4 different destination MAC addresses, but with one destination IP address the whole way.
Common mistakes.
- Reading IHL as bytes. It counts 32-bit words.
- Expecting the IP checksum to cover the payload.
- Thinking MAC addresses do the routing. They find the next hop, which IP chooses.
Summary. MAC = neighbour; IP = the far end. IHL counts words; total length counts bytes. Each checksum covers exactly what can change it.
Routing
Why it matters. IP names the destination, and routing decides the next hop. Millions of machines agree on routes with no one in charge, using algorithms you can run on paper.
The idea. A router's routing table 路由表 maps destination prefixes to next hops. The longest prefix match 最长前缀匹配 rule chooses the most specific route. Our course network has routers A–F joined by links with weights, and Dijkstra's algorithm finds the shortest-path tree from A.

Dijkstra's algorithm, in numbers. The course weights are A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10, D–E 2, E–F 3 and D–F 6. At each step, settle the nearest unsettled router, then update its neighbours:
| step | settle | distance | via | why |
|---|---|---|---|---|
| 1 | C | 2 | A | direct link |
| 2 | B | 3 | C | $2 + 1 = 3$ beats the direct 4 |
| 3 | D | 8 | B | $3 + 5 = 8$ beats $2 + 8 = 10$ through C |
| 4 | E | 10 | D | $8 + 2 = 10$ beats $2 + 10 = 12$ through C |
| 5 | F | 13 | E | $10 + 3 = 13$ beats $8 + 6 = 14$ through D |
So the best path to F is A, C, B, D, E, F, with length $2 + 1 + 5 + 2 + 3 = 13$.
Worked example 1: longest prefix match. The table:
| prefix | next hop |
|---|---|
10.0.0.0/8 |
X |
10.1.0.0/16 |
Y |
10.1.2.0/24 |
Z |
| default | W |
For destination 10.1.2.200, three prefixes match, and the /24 is the
longest, so the next hop is Z. For 10.9.9.9, only the /8 matches, so X.
The default route catches everything else.
Worked example 2: why addresses are hierarchical. A flat table would need one row per host: about $4 \times 10^9$ rows for IPv4. Prefixes group addresses together: an ISP announces one /12 prefix that covers $2^{32-12} = 2^{20}$ addresses. Because this grouping hides the host bits, the global table stays at around $10^5$ entries.
Common mistakes.
- Forgetting the settled set, and updating a router whose distance is already final.
- Using the first matching row instead of the longest prefix.
- Counting hops when the measure is delay.
Summary. Tables map prefixes to next hops, and the longest prefix wins. Dijkstra builds the shortest-path tree by settling the nearest unsettled node. Grouping addresses keeps the tables a manageable size.
Transport (TCP/UDP)
Why it matters. IP only promises "best effort": packets may be lost, arrive in the wrong order, or arrive twice. The transport layer connects programs. TCP gives an ordered stream of bytes; UDP sends separate messages (datagrams) with no promise of delivery or order.
The idea.
- UDP 用户数据报协议: port numbers, a length and a checksum. Like a postcard.
- TCP 传输控制协议: sequence numbers, acknowledgements (ACKs), resending, and flow and congestion control. Like a courier who keeps a record of everything.
The three-way handshake 三次握手 (SYN, SYN-ACK, ACK) opens a TCP connection, and FIN closes it.

Why ACKs are cumulative. Each ACK gives the number of the next byte expected. ACK 200 means that bytes 0–199 have all arrived, whatever order they came in. If there is a gap, the ACK number stops growing, and after a timeout the sender resends from the gap. One number sums up everything the receiver has: simple to check and impossible to misunderstand.
Worked example 1: the record. The SYN uses sequence number 0, and the server's SYN-ACK uses its own 0.
| client sends | seq | server ACKs |
|---|---|---|
| 100 bytes | 1 | 101 |
| 50 bytes | 101 | 151 |
| 30 bytes (lost) | 151 | still 151 |
| the same 30 bytes again, after the timeout | 151 | 181 |
Worked example 2: what UDP is good for. A UDP header is 8 bytes (ports, length, checksum), against at least 20 for TCP. A DNS request fits in one UDP datagram: the request is one packet, and if it is lost, the client simply asks again. Video calls can live with some loss but not with delay: a frame that is due in 30 ms is useless if it is resent and arrives late. Here, UDP plus some logic in the application is the right choice.
Common mistakes.
- Counting the SYN as data. It uses up one sequence number but carries no data.
- Mixing up flow control (protects the receiver, using its window) and congestion control (protects the network).
- Expecting TCP to guarantee timing. It guarantees order and delivery, never speed.
Summary. UDP = ports and a checksum, for small request-sized jobs. TCP = a sequence record, cumulative ACKs and resending. The handshake opens a connection, FIN closes it, and windows protect both ends.
Application protocols
Why it matters. The transport layer moves bytes; the application layer agrees on what they mean. Reading a protocol is like reading a conversation that follows a grammar.
The idea.
- DNS 域名系统 turns a name into an address, by a chain of referrals: root → top-level domain (TLD) → the domain's own (authoritative) server.
- HTTP 超文本传输协议 messages have a request line, headers and a body. Status codes 状态码: $2xx$ success, $3xx$ redirect, $4xx$ client error, $5xx$ server error.

Why DNS is hierarchical. One global table of names would mean one global point of failure, and one global argument about who controls it. Delegation splits the work:
- the root (13 server addresses) knows the TLD servers;
- the
.comservers know each domain's authoritative servers; - the authoritative server knows the host.
Each answer refers the question one level down. Caching makes this fast: a resolver that has asked once does not need the full walk again until the answer's TTL runs out.
Worked example 1: one page, byte by byte. The browser sends
GET /index.html HTTP/1.1 with a Host: header. The server answers
200 OK, then headers (Content-Type: text/html,
Content-Length: 128), then a blank line, then 128 bytes of body. A
response with Content-Length 128 carries exactly 128 body bytes. A 404
carries headers and a short body; a 301 carries a Location: header
and little or no body.
Worked example 2: the codes as promises.
| code | meaning | what a web crawler does |
|---|---|---|
| 200 | here is the body | reads it |
| 301 | the page moved; Location says where |
follows the new address |
| 404 | nothing here | treats it as a dead end |
| 500 | the server failed | tries again later |
The status code is the contract that client programs are written against.
Common mistakes.
- Forgetting the blank line that ends the headers. The parser then waits for ever.
- Reading Content-Length as the size of the headers. It is the size of the body.
- Thinking DNS is one server. It is a tree of delegated servers, with caches.
Summary. DNS finds names by delegation and caching. HTTP is a request-and-response grammar: the status code is the promise, and the headers carry the extra information.
Network security
Why it matters. The network is like a shared phone line: anyone on the path can read, drop or fake messages. Security is the set of promises that make an open channel safe to use.


The idea.
- Symmetric 对称 encryption: one shared key; fast.
- Asymmetric 非对称 encryption: a key pair; the public key encrypts and the private key decrypts.
- TLS 传输层安全 uses both: asymmetric encryption to agree on a key, symmetric encryption for the data, and certificates to prove identity.

Three properties, and what gives each one.
| property | needs | without it |
|---|---|---|
| confidentiality 机密性 | encryption | anyone can listen in |
| integrity | a MAC 报文认证码 (message authentication code), checked in both directions; each Finished message proves the handshake was not changed | messages can be altered |
| authentication | a certificate chain to a root the client already trusts | an impostor can pose as the server |
Worked example 1: the handshake, step by step.
- ClientHello: the cipher suites the client offers, and a random value.
- ServerHello: the chosen suite and a random value, plus the certificate.
- Key exchange: a secret value (the premaster secret) encrypted with the server's public key.
- Both sides calculate the session keys from the three values.
- Both Finished messages are checked, and then data flows with symmetric encryption.
There is one slow asymmetric operation, then fast symmetric encryption for everything else. The asymmetric step is used only to agree on the symmetric key.
Worked example 2: the list of attacks.
| attack | stopped by |
|---|---|
| listening in (passive sniffing) | encryption |
| changing messages | the MAC (the Finished messages catch tampering before data flows) |
| pretending to be the server | certificates (the server proves it has the private key that matches the certified public key) |
| replaying an old handshake | the fresh random values in both Hellos |
Each attack has its own defence.
Common mistakes.
- Encrypting without authenticating: you then have a secure channel to the wrong machine.
- Trusting self-signed certificates in production.
- Thinking HTTPS makes the server honest. It makes the channel private and identifies the server; the content can still be false.
Summary. Symmetric encryption for speed, asymmetric for agreeing on keys, certificates for identity, and MACs for integrity. Every defence answers a named attack.
Distributed systems fundamentals
Why it matters. As soon as two machines work together over a network, parts of the system can fail on their own and messages can arrive out of order. Distributed systems is the study of keeping promises anyway.

The idea.
- A distributed system 分布式系统 shares state across machines, with partial failure 部分故障: some machines or components can fail while others continue operating.
- Messages can arrive out of order, or twice.
- Idempotence 幂等 means that running an operation again has no extra effect, so repeats are safe.
- The happens-before 先于关系 relation orders events, even without clocks.

Why clocks cannot order events. The clocks of two machines drift apart by amounts you cannot bound, so timestamps cannot safely order events on different machines. A Lamport clock Lamport 时钟 is a counter that respects cause and effect:
- before sending, add 1 and attach the counter: $C = C + 1$;
- on receiving, set $C = \max(C, \text{received}) + 1$.
If $a$ happens before $b$, then $C(a) < C(b)$. The reverse is not true: a smaller counter alone does not prove that one event caused the other.
Worked example 1: a Lamport trace.
- P0 sends $m_1$ with $C = 1$.
- P1 receives it: $C = \max(0, 1) + 1 = 2$.
- P1 sends $m_2$ with $C = 3$.
- P0 receives it: $C = \max(1, 3) + 1 = 4$.
$C(m_1) = 1 < C(m_2) = 3$: the order of cause and effect is kept, without any clock.
Worked example 2: idempotence by design. A payment service receives
deduct(user, 25, id=xyz). If the request arrives twice, the money must
be taken only once. Make the operation idempotent by its key: the ledger
records xyz as done, and the repeat does nothing. Systems that can
safely retry are built from idempotent operations plus a record of what
was done, not from hoping that the network stops sending duplicates.
Common mistakes.
- Using timestamps to order events on different machines. Clock drift makes them wrong.
- Retrying operations that are not idempotent (customers are charged twice).
- Thinking a call that returned means the work happened, or that a timeout means it did not. A timeout means you do not know: it is the third possible outcome.
Summary. Failures may be partial, temporary or permanent. Messages arrive out of order and twice. Lamport counters respect happens-before. Idempotence plus a record of completed work makes retries safe. A timeout means "unknown", not "failed".