Skip to content

Networking

1. Network Models

1.1 OSI Model (7 Layers)

LayerNameFunctionExamples
7ApplicationUser interface, application logicHTTP, DNS, SMTP, FTP
6PresentationData formatting, encryptionTLS/SSL, JPEG, ASCII
5SessionDialog control, synchronizationNetBIOS, RPC
4TransportEnd-to-end reliabilityTCP, UDP
3NetworkRouting, logical addressingIP, ICMP, OSPF, BGP
2Data LinkFraming, error detectionEthernet, Wi-Fi, PPP
1PhysicalBit transmission over mediumCables, fiber, radio waves

1.2 TCP/IP Model (4 Layers)

TCP/IP LayerOSI LayersProtocols
Application5, 6, 7HTTP, DNS, SMTP, FTP, TLS
Transport4TCP, UDP
Internet3IP, ICMP, ARP
Network Access1, 2Ethernet, Wi-Fi, MAC

1.3 Encapsulation

Each layer adds a header (and sometimes trailer) to the data from the layer above:

Application data
→ [TCP header | Application data]
→ [IP header | TCP header | Application data]
→ [Ethernet header | IP header | TCP header | Application data | FCS]

2. Physical Layer

2.1 Signals and Encoding

Analog signals: Continuous, represented by sine waves. Parameters: amplitude, frequency, phase.

Digital signals: Discrete (0s and 1s). Encoded as voltage levels.

Encoding schemes:

SchemeDescription
NRZ-LHigh = 0, Low = 1 (or vice versa)
NRZ-ITransition at 1, no transition at 0
ManchesterTransition at midpoint: low→high = 1, high→low = 0
Differential ManchesterTransition at start of each bit; no mid-bit transition = 1
4B/5B4 data bits encoded as 5 bits (80% efficiency)

2.2 Bandwidth and Data Rate

Nyquist theorem (noiseless channel):

C=2Blog2VC = 2B \log_2 V

where BB = bandwidth (Hz), VV = signal levels.

Shannon”s theorem (noisy channel):

C=Blog2(1+SNR)C = B \log_2(1 + \text{SNR})

where SNR = signal-to-noise ratio (power ratio). If SNRdB=30\text{SNR}_{\text{dB}} = 30, then SNR=1030/10=1000\text{SNR} = 10^{30/10} = 1000.

2.3 Multiplexing

FDM (Frequency-Division Multiplexing): Divide bandwidth into frequency bands, each carrying a separate signal.

TDM (Time-Division Multiplexing): Divide time into slots, each carrying data from a different source.

WDM (Wavelength-Division Multiplexing): FDM over fiber optics using different wavelengths (colors) of light.

Statistical TDM: Dynamically allocate time slots based on demand (more efficient than synchronous TDM).

2.4 Transmission Media

MediumBandwidthDistanceUse
Twisted pairUp to 1 GbpsUp to 100mLAN (Cat 5e/6/7)
Coaxial cableUp to 10 GbpsUp to 500mCable TV
Fiber opticUp to 100 TbpsUp to 100kmBackbone, WAN
Radio (Wi-Fi)Up to 9.6 GbpsUp to 100mWLAN

3.1 Framing

Divide the bit stream into frames for transmission.

Framing methods:

  • Byte count: First byte specifies frame length (vulnerable to count corruption).
  • Byte stuffing (flag bytes): Use special delimiter DLE STX/DLE ETX; escape DLE within data.
  • Bit stuffing: Flag pattern 01111110; after five consecutive 1s, insert a 0.
BIT_STUFF(data):
result = ""
count = 0
for each bit in data:
if bit == 1:
count += 1
if count == 5:
result += "10" // stuff a 0
count = 0
continue
else:
count = 0
result += bit
return result

3.2 Error Detection

Parity check: Add 1 bit so total 1s is even (even parity) or odd (odd parity). Detects single-bit errors.

Checksum: Sum all 16-bit words, take 1’s complement. Used in TCP, UDP, IP.

CRC (Cyclic Redundancy Check):

Given message M(x)M(x) and generator polynomial G(x)G(x):

  1. Append rr zero bits to M(x)M(x) where r=deg(G(x))r = \deg(G(x)).
  2. Divide by G(x)G(x) using polynomial (mod 2) division.
  3. Remainder R(x)R(x) is the CRC.
  4. Transmit M(x)xr+R(x)M(x) \cdot x^r + R(x).
  5. Receiver divides by G(x)G(x); zero remainder = no error.

Detection capability: Detects all single-bit errors, all double-bit errors, all odd-number errors, and any burst of length r\leq r.

3.3 Error Correction

Hamming code: Adds parity bits at positions 2i2^i to correct single-bit errors.

For mm data bits, use rr parity bits where 2rm+r+12^r \geq m + r + 1.

Data bitsParity bitsTotal
437
8412
16521

3.4 MAC Protocols

ALOHA: Transmit anytime. If collision, wait random time and retransmit. Throughput: 18.4%\sim 18.4\%.

Slotted ALOHA: Time divided into slots. Transmit only at slot boundaries. Throughput: 36.8%\sim 36.8\%.

CSMA/CD (Carrier Sense Multiple Access with Collision Detection):

  1. Sense channel: idle → transmit; busy → wait.
  2. While transmitting, detect collisions.
  3. On collision: send jam signal, wait (binary exponential backoff), retry.

Backoff time=rand(0,2k1)×slot time\text{Backoff time} = \text{rand}(0, 2^k - 1) \times \text{slot time}

where k=min(retries,10)k = \min(\text{retries}, 10). Used in Ethernet (IEEE 802.3).

CSMA/CA (Collision Avoidance): Used in Wi-Fi (802.11). Uses RTS/CTS handshake to reserve the channel before transmitting.

CSMA_CA(sender, receiver):
// Wait for channel idle for DIFS
while channel busy: wait
wait DIFS
send RTS
wait CTS
wait SIFS
send data
wait ACK after SIFS

4. Network Layer

4.1 IP Addressing

IPv4 address: 32 bits, written as 4 octets (e.g., 192.168.1.1).

IPv6 address: 128 bits, written as 8 groups of 4 hex digits (e.g., 2001:0db8:85a3::8a2e:0370:7334).

IPv4 address classes:

ClassRangeDefault SubnetMax Hosts
A0.0.0.0 – 127.255.255.255/822422^{24} - 2
B128.0.0.0 – 191.255.255.255/1621622^{16} - 2
C192.0.0.0 – 223.255.255.255/242822^8 - 2
D224.0.0.0 – 239.255.255.255Multicast
E240.0.0.0 – 255.255.255.255Reserved

4.2 Subnetting and CIDR

CIDR (Classless Inter-Domain Routing): Notation a.b.c.d/na.b.c.d/n where nn = number of network bits.

Subnet mask: nn 1-bits followed by 32n32-n 0-bits.

Number of subnets: 2subnet bits2^{\text{subnet bits}}

Hosts per subnet: 232n22^{32-n} - 2 (subtract network address and broadcast address).

Example: 192.168.1.0/26 → 64 addresses, 62 usable hosts, 4 subnets.

Subnetting algorithm:

SUBNET(network, prefix_length, num_subnets):
subnet_bits = ceil(log2(num_subnets))
new_prefix = prefix_length + subnet_bits
subnet_size = 2^(32 - new_prefix)
for i = 0 to num_subnets - 1:
start = network + i * subnet_size
print f"{start}/{new_prefix}"

4.3 NAT (Network Address Translation)

Translates private IP addresses to a public IP address for Internet access:

Private: (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)\text{Private: } (10.0.0.0/8, \ 172.16.0.0/12, \ 192.168.0.0/16)

NAT types:

  • Static NAT: One-to-one mapping.
  • Dynamic NAT: Pool of public IPs, assigned on demand.
  • PAT (NAT overload): Maps multiple private IPs to one public IP using port numbers.

4.4 Routing Protocols

RIP (Routing Information Protocol):

  • Distance-vector protocol. Uses Bellman-Ford algorithm.
  • Metric: hop count (max 15 hops).
  • Sends entire routing table every 30 seconds.
  • Convergence: Slow. Count-to-infinity problem.
RIP_UPDATE(router):
every 30 seconds:
for each destination d:
min_cost = INF
next_hop = NIL
for each neighbor n:
if cost(d, n) + 1 < min_cost:
min_cost = cost(d, n) + 1
next_hop = n
update routing table for d

OSPF (Open Shortest Path First):

  • Link-state protocol. Uses Dijkstra’s algorithm.
  • Each router has a complete map of the network topology.
  • Floods LSA (Link State Advertisements) on topology changes.
  • Fast convergence. Supports areas for scalability.

BGP (Border Gateway Protocol):

  • Path-vector protocol. Used for inter-AS (autonomous system) routing.
  • Exchanges reachability information between ASes.
  • Policy-based routing (economic, political considerations).
  • eBGP (between ASes) and iBGP (within an AS).
ProtocolTypeAlgorithmMetricScopeConvergence
RIPDistance-vectorBellman-FordHop countASSlow
OSPFLink-stateDijkstraCost (custom)Area/ASFast
BGPPath-vectorPolicy-basedAS pathInternetVaries

5. Transport Layer

5.1 UDP (User Datagram Protocol)

  • Connectionless: No handshake, no connection state.
  • Unreliable: No acknowledgment, no retransmission.
  • No ordering: Packets may arrive out of order.
  • No congestion control.
  • Header: 8 bytes (src port, dst port, length, checksum).
  • Use cases: DNS, DHCP, real-time streaming, gaming.

5.2 TCP (Transmission Control Protocol)

  • Connection-oriented: Three-way handshake.
  • Reliable: Acknowledgments, retransmissions, sequence numbers.
  • Ordered: Sequences guarantee in-order delivery.
  • Byte-stream: No message boundaries.
  • Flow controlled: Receiver controls sender’s rate.
  • Congestion controlled: Sender adjusts to network conditions.

TCP header: 20 bytes minimum (src port, dst port, seq number, ack number, flags, window, checksum, urgent pointer, options).

5.3 TCP Three-Way Handshake

Client Server
|--- SYN (seq=x) ------------------>|
|<-- SYN+ACK (seq=y, ack=x+1) -----|
|--- ACK (seq=x+1, ack=y+1) ------>|
| |
| CONNECTION ESTABLISHED |

5.4 TCP Reliable Delivery

Sequence numbers: Each byte is numbered. SYN and FIN consume one sequence number.

Acknowledgments: Cumulative; ACK nn means all bytes up to n1n-1 received.

Retransmission: On timeout (RTO = estimated RTT + 4 * RTT deviation):

EstimatedRTT=(1α)EstimatedRTT+αSampleRTT\text{EstimatedRTT} = (1-\alpha) \cdot \text{EstimatedRTT} + \alpha \cdot \text{SampleRTT}

DevRTT=(1β)DevRTT+βSampleRTTEstimatedRTT\text{DevRTT} = (1-\beta) \cdot \text{DevRTT} + \beta \cdot |\text{SampleRTT} - \text{EstimatedRTT}|

In most cases α=0.125\alpha = 0.125, β=0.25\beta = 0.25.

Fast retransmit: 3 duplicate ACKs trigger immediate retransmission without waiting for timeout.

5.5 TCP Flow Control

Sliding window: Receiver advertises available buffer space via the window field.

Sender maintains:
send_base: oldest un-ACKed byte
next_seq_num: next byte to send
Receiver maintains:
recv_base: next expected byte
window: advertised window size
  • Zero window probe: When window is 0, sender sends probes to detect when window opens.
  • Silly Window Syndrome: Avoided by Nagle’s algorithm (sender buffers small segments) and Clark’s solution (receiver doesn’t advertise small windows).

5.6 TCP Congestion Control

Slow start: Congestion window (cwnd) starts at 1 MSS. Doubles each RTT (exponential growth).

TCP_SLOW_START():
cwnd = 1 * MSS
ssthresh = Infinity
while true:
send min(cwnd, receiver_window) bytes
if timeout:
ssthresh = cwnd / 2
cwnd = 1 * MSS // restart slow start
else if 3 duplicate ACKs:
ssthresh = cwnd / 2
cwnd = ssthresh // fast recovery
if cwnd >= ssthresh:
enter congestion avoidance

Congestion avoidance: Linear growth (increase cwnd by 1 MSS per RTT).

Fast recovery: On 3 duplicate ACKs, halve cwnd (set to ssthresh) instead of resetting to 1.

Phases:

Phasecwnd GrowthTrigger
Slow startExponentialConnection start, timeout
Congestion avoidanceLinearcwnd ≥ ssthresh
Fast recovery3 duplicate ACKs
Timeout (RTO)Reset to 1 MSSTimeout

6. Application Layer

6.1 HTTP (HyperText Transfer Protocol)

Request methods:

MethodPurposeIdempotentHas body
GETRetrieve resourceYesNo
POSTCreate/submit dataNoYes
PUTReplace resourceYesYes
DELETERemove resourceYesNo
PATCHPartial updateNoYes
HEADGet headers onlyYesNo

Status codes:

  • 2xx: Success (200 OK, 201 Created)
  • 3xx: Redirection (301 Moved Permanently, 304 Not Modified)
  • 4xx: Client error (400 Bad Request, 401 Unauthorized, 404 Not Found)
  • 5xx: Server error (500 Internal Server Error, 503 Service Unavailable)

HTTP/1.1 vs HTTP/2 vs HTTP/3:

FeatureHTTP/1.1HTTP/2HTTP/3
TransportTCPTCPQUIC (UDP)
Connections1 per domainMultiplexedMultiplexed
Header formatTextHPACK compressedQPACK
OrderingHOL blockingHOL on TCPNo HOL

6.2 DNS (Domain Name System)

Hierarchical, distributed database mapping domain names to IP addresses.

Record types:

TypePurpose
AIPv4 address
AAAAIPv6 address
CNAMECanonical name (alias)
MXMail exchange server
NSAuthoritative name server
TXTText data

Resolution process:

DNS_RESOLVE(domain):
check local cache
query recursive resolver
if not cached:
query root server → TLD server → authoritative server
cache result
return IP address

6.3 SMTP (Simple Mail Transfer Protocol)

Email flow:

Sender MUA → Sender MTA (SMTP) → Intermediate MTA(s) → Receiver MTA (SMTP) → Receiver MUA (IMAP/POP3)

SMTP commands:

HELO hostname // Greet
MAIL FROM: <addr> // Sender
RCPT TO: <addr> // Recipient
DATA // Start message body
Subject: ...
<Message body>
. // End message
QUIT // Close

6.4 Other Application Protocols

ProtocolPortPurpose
FTP20/21File transfer (control/data)
SSH22Secure shell
DHCP67/68Auto IP configuration
IMAP143Email access (server-side)
POP3110Email download
HTTPS443HTTP over TLS
DNS53Name resolution

7. Common Pitfalls

  1. Confusing TCP and UDP use cases. TCP adds overhead for reliability, flow control, and congestion control. Use UDP for low-latency applications where occasional packet loss is acceptable (gaming, streaming, DNS).

  2. Forgetting the TCP three-way handshake cost. Every TCP connection requires a full round trip before data transfer begins. For short-lived connections, this overhead is significant. Use connection pooling.

  3. Underestimating the importance of MTU. If a packet exceeds the Maximum Transmission Unit, it gets fragmented. Fragmentation increases overhead and reassembly complexity. Path MTU discovery helps avoid this.

  4. Ignoring subnet mask errors. An incorrect subnet mask causes packets to be sent to the wrong gateway or subnet. Always double-check CIDR notation and subnet masks.

  5. Misunderstanding ARP (Address Resolution Protocol). ARP maps IP to MAC addresses and is broadcast-based. It works only within a broadcast domain. Routers use their own MAC addresses, not the destination’s.

  6. HTTP/1.1 head-of-line blocking. In HTTP/1.1, one slow response blocks subsequent responses on the same TCP connection. This is solved by HTTP/2 multiplexing or opening multiple connections.

  7. DNS caching issues. Long TTL values improve performance but delay propagation of changes. Short TTL values increase DNS query volume. Choose based on how often records change.

Worked Examples

Example 1: Subnet Calculation

Problem: An organisation has IP address 192.168.1.0/24 and needs to create 6 subnets of roughly equal size. Determine the subnet mask and the address range for each subnet. Solution: 6 subnets requires 3 bits (2^3 = 8 subnets). New mask: /24 + 3 = /27 (255.255.255.224). Block size: 256 - 224 = 32 addresses per subnet (30 usable). Subnets: 192.168.1.0/27 (hosts 1-30), 192.168.1.32/27 (hosts 33-62), 192.168.1.64/27 (hosts 65-94), 192.168.1.96/27 (hosts 97-126), 192.168.1.128/27 (hosts 129-158), 192.168.1.160/27 (hosts 161-190), 192.168.1.192/27, 192.168.1.224/27.

Example 2: TCP Three-Way Handshake

Problem: Describe the TCP three-way handshake and explain the purpose of each segment’s flags. Solution: Client sends SYN (seq=x). Server responds with SYN-ACK (seq=y, ack=x+1). Client sends ACK (ack=y+1). The SYN flag requests connection establishment and carries the initial sequence number. The SYN-ACK acknowledges the client’s SYN and provides the server’s initial sequence number. The final ACK acknowledges the server’s SYN. After this exchange, both sides have established their sequence numbers and the connection is established for full-duplex data transfer.

Summary

  • OSI model (7 layers) and TCP/IP model (4 layers) organize network functionality into abstraction layers with encapsulation.
  • Physical layer handles signal encoding, bandwidth (Nyquist/Shannon), and multiplexing (FDM, TDM, WDM).
  • Data link layer manages framing, error detection (CRC), error correction (Hamming codes), and medium access (CSMA/CD, CSMA/CA).
  • Network layer handles IP addressing, subnetting (CIDR), NAT, and routing (RIP, OSPF, BGP).
  • Transport layer provides UDP (fast, unreliable) and TCP (reliable, ordered, flow-controlled, congestion-controlled).
  • Application layer includes HTTP, DNS, SMTP, and other end-user protocols.

Cross-References

TopicLink
DatabasesView
Distributed SystemsView
Operating SystemsView