Computer Networks

OSI model, TCP/IP, protocols

Practice →

Computer Networks form the backbone of modern communication systems. This topic covers the fundamental concepts of how computers connect and communicate, including network models (OSI and TCP/IP), addressing schemes, protocols, and routing mechanisms. Understanding these concepts is essential for technical interviews in software engineering, system administration, and network engineering roles.

Complexity / Key Facts

Number of hosts in a subnet = 2^(host bits) - 2 (subtract 2 for network and broadcast addresses)\text{Number of hosts in a subnet = 2\textasciicircum{}(host bits) - 2 (subtract 2 for network and broadcast addresses)}
Subnet mask bits = 32 - host bits (for IPv4)\text{Subnet mask bits = 32 - host bits (for IPv4)}
Number of subnets = 2^(borrowed bits) when subnetting\text{Number of subnets = 2\textasciicircum{}(borrowed bits) when subnetting}
Bandwidth-Delay Product = Bandwidth x Round Trip Time (RTT)\text{Bandwidth-Delay Product = Bandwidth x Round Trip Time (RTT)}
Throughput = (Window Size) / (RTT)\text{Throughput = (Window Size) / (RTT)}
TCP Sequence Number wraparound time = 2^32 / (data rate in bytes per second)\text{TCP Sequence Number wraparound time = 2\textasciicircum{}32 / (data rate in bytes per second)}
CSMA/CD: Minimum frame size = 2 x Propagation Delay x Bandwidth\text{CSMA/CD: Minimum frame size = 2 x Propagation Delay x Bandwidth}
Efficiency of pure Aloha = 18.4% (1/2e), Slotted Aloha = 36.8% (1/e)\text{Efficiency of pure Aloha = 18.4\% (1/2e), Slotted Aloha = 36.8\% (1/e)}
Hamming distance for error detection: To detect d errors, need distance d+1\text{Hamming distance for error detection: To detect d errors, need distance d+1}
Hamming distance for error correction: To correct d errors, need distance 2d+1\text{Hamming distance for error correction: To correct d errors, need distance 2d+1}

Key Concepts

OSI Model (7 Layers)

The Open Systems Interconnection model standardizes network communication:

1. Physical Layer: Transmits raw bit stream over physical medium (cables, hubs, repeaters). Deals with voltage levels, pin layouts, timing.

2. Data Link Layer: Provides node-to-node delivery with error detection (MAC addressing, switches, bridges). Divided into LLC and MAC sublayers.

3. Network Layer: Handles logical addressing and routing (IP, routers). Determines best path from source to destination.

4. Transport Layer: End-to-end communication, reliability, flow control (TCP, UDP). TCP provides reliability; UDP provides speed.

5. Session Layer: Establishes, manages, and terminates sessions between applications. Handles dialogue control and synchronization.

6. Presentation Layer: Data translation, encryption, compression. Converts data formats between application and network.

7. Application Layer: Interface for applications to access network services (HTTP, FTP, SMTP, DNS).

Mnemonic: 'Please Do Not Throw Sausage Pizza Away' (bottom-up) or 'All People Seem To Need Data Processing' (top-down).

TCP/IP Model (4 Layers)

The practical implementation model used in the Internet:

1. Network Interface (Link) Layer: Combines OSI Physical and Data Link. Handles physical transmission and MAC addressing (Ethernet, Wi-Fi, ARP).

2. Internet Layer: Corresponds to OSI Network Layer. Responsible for logical addressing, routing, and fragmentation (IP, ICMP, IGMP).

3. Transport Layer: Same as OSI Transport. Provides process-to-process communication (TCP for reliable, UDP for fast).

4. Application Layer: Combines OSI Session, Presentation, and Application. Supports user applications (HTTP, HTTPS, FTP, SSH, DNS, SMTP).

Key Protocols by Layer:
Link: Ethernet, Wi-Fi (802.11), PPP, ARP
Internet: IPv4, IPv6, ICMP (ping), IGMP (multicast)
Transport: TCP, UDP, SCTP
Application: HTTP/80, HTTPS/443, FTP/20,21, SSH/22, DNS/53, SMTP/25, POP3/110, IMAP/143

IP Addressing and Subnetting

IPv4 addresses are 32-bit numbers written in dotted decimal notation (e.g., 192.168.1.1).

Address Classes (legacy, now mostly obsolete due to CIDR):
Class A: 1.0.0.0 to 126.255.255.255 (default mask /8, 16M hosts)
Class B: 128.0.0.0 to 191.255.255.255 (default mask /16, 65K hosts)
Class C: 192.0.0.0 to 223.255.255.255 (default mask /24, 254 hosts)
Class D: 224.0.0.0 to 239.255.255.255 (multicast)
Class E: 240.0.0.0 to 255.255.255.255 (reserved)

Private IP Ranges (RFC 1918):
10.0.0.0/8 (10.0.0.0 - 10.255.255.255)
172.16.0.0/12 (172.16.0.0 - 172.31.255.255)
192.168.0.0/16 (192.168.0.0 - 192.168.255.255)

Subnetting: Borrowing bits from host portion to create subnets.
CIDR notation: IP/prefix length (e.g., 192.168.1.0/24)
/24 = 255.255.255.0 = 256 addresses, 254 usable hosts
/16 = 255.255.0.0 = 65,536 addresses

IPv6: 128-bit addresses in hexadecimal (e.g., 2001:0db8:85a3::8a2e:0370:7334), eliminating NAT issues and providing vast address space.

TCP vs UDP

Transport Layer Protocols - choose based on application needs:

TCP (Transmission Control Protocol):
Connection-oriented (3-way handshake: SYN, SYN-ACK, ACK)
Reliable delivery with acknowledgments and retransmissions
Ordered delivery (sequence numbers, reordering at receiver)
Flow control (sliding window algorithm)
Congestion control (slow start, congestion avoidance, fast retransmit)
Header size: 20-60 bytes
Use cases: Web browsing (HTTP), email (SMTP), file transfer (FTP)

UDP (User Datagram Protocol):
Connectionless (no handshake, immediate transmission)
Unreliable (no acknowledgments, no retransmissions)
No ordering guarantees
No flow or congestion control
Header size: 8 bytes (much smaller than TCP)
Use cases: Video streaming, online gaming, VoIP, DNS queries

TCP Header Fields: Source/Dest Port, Sequence Number, Ack Number, Data Offset, Flags (URG, ACK, PSH, RST, SYN, FIN), Window Size, Checksum, Urgent Pointer, Options

UDP Header Fields: Source Port, Destination Port, Length, Checksum

Well-known Ports: 0-1023 (reserved)
20/21: FTP
22: SSH
25: SMTP
53: DNS
80: HTTP
110: POP3
143: IMAP
443: HTTPS
3306: MySQL
5432: PostgreSQL

Network Devices

Devices operate at different OSI layers with distinct functions:

Repeater (Layer 1): Amplifies and regenerates signals to extend cable length. No intelligence, just signal boosting.

Hub (Layer 1): Multi-port repeater. Broadcasts data to all connected devices. Creates one collision domain and one broadcast domain. Inefficient, rarely used today.

Bridge (Layer 2): Connects two network segments. Uses MAC addresses to filter traffic. Reduces collision domains.

Switch (Layer 2): Intelligent multi-port bridge. Uses MAC address table to forward frames only to intended recipient. Each port is a separate collision domain. Types: unmanaged, managed, PoE.

Router (Layer 3): Connects different networks (LANs, WANs). Uses IP addresses and routing tables to forward packets. Separates broadcast domains. Implements routing protocols.

Gateway (Layer 4-7): Protocol converter between different network architectures. Can translate between different protocols at multiple layers.

Access Point (Layer 2): Wireless connectivity device. Bridges wireless clients to wired networks.

Firewall (Layer 3-7): Security device filtering traffic based on rules. Types: packet filter, stateful inspection, application layer (proxy).

Load Balancer (Layer 4-7): Distributes traffic across multiple servers. Algorithms: round-robin, least connections, IP hash.

Routing Algorithms and Protocols

Routing determines the best path for data packets to travel from source to destination.

Types of Routing:
Static Routing: Manually configured routes. Simple, secure, but not scalable for large networks.
Dynamic Routing: Automatically adapts to network changes using routing protocols.

Routing Protocol Categories:

1. Distance Vector Protocols (Bellman-Ford algorithm):
RIP (Routing Information Protocol): Uses hop count (max 15), broadcasts every 30s
IGRP (Cisco proprietary): Uses bandwidth, delay, load, reliability
Characteristics: Simple, slower convergence, count-to-infinity problem

2. Link State Protocols (Dijkstra's SPF algorithm):
OSPF (Open Shortest Path First): Fast convergence, hierarchical with areas, uses cost metric
IS-IS: Similar to OSPF, used in large ISP networks
Characteristics: Complex, faster convergence, maintains complete topology map

3. Path Vector Protocols:
BGP (Border Gateway Protocol): Inter-domain routing between autonomous systems (AS). Uses policy-based routing with attributes.
eBGP: Between different AS
iBGP: Within same AS

4. Hybrid Protocols:
EIGRP (Enhanced IGRP): Cisco proprietary, combines distance vector and link state features

Routing Metrics: Hop count, Bandwidth, Delay, Load, Reliability, Cost

Autonomous System (AS): Collection of IP networks under single administrative control. Identified by AS Number (ASN).

Network Topologies

Physical and logical arrangements of network devices:

Bus Topology: All devices connected to single cable (backbone). Simple, cheap, but single point of failure and collision issues.

Star Topology: All devices connected to central hub/switch. Easy to manage, single device failure doesn't affect others, but central device failure is critical. Most common in LANs.

Ring Topology: Devices connected in circular fashion. Token passing controls access (Token Ring). No collisions, but single break disrupts entire network.

Mesh Topology: Every device connected to every other device. Highly redundant and reliable, but expensive and complex. Full mesh: n(n-1)/2 connections for n devices. Partial mesh: selected connections.

Tree Topology: Hierarchical star structure. Combines multiple star networks. Good for large networks, but root failure affects large portion.

Hybrid Topology: Combination of two or more topologies. Common in enterprise networks.

Ethernet (IEEE 802.3): Dominant LAN technology
CSMA/CD (Carrier Sense Multiple Access with Collision Detection): Listen before transmit, detect collisions, backoff and retry
Ethernet types: 10Base-T (10 Mbps), 100Base-TX (Fast Ethernet), 1000Base-T (Gigabit), 10GBase-T
Full-duplex (simultaneous send/receive, no collisions) vs Half-duplex

Network Security Concepts

Securing network communications and infrastructure:

Firewalls:
Packet Filtering: Examines source/dest IP, port, protocol. Fast but limited.
Stateful Inspection: Tracks connection state. More secure than simple filtering.
Application Layer (Proxy): Intercepts and inspects application data. Most secure, slower.
Next-Generation (NGFW): Deep packet inspection, intrusion prevention, application awareness.

VPN (Virtual Private Network):
Creates encrypted tunnel over public Internet
Types: Site-to-Site (connecting networks), Remote Access (individual users)
Protocols: IPsec (L3 encryption), SSL/TLS VPN (L4-7, via browser), PPTP, L2TP

NAT (Network Address Translation):
Translates private IPs to public IP(s)
Types: Static NAT (1:1), Dynamic NAT (pool of public IPs), PAT/Port NAT (many:1, port multiplexing)
Hides internal network structure, conserves IPv4 addresses

Encryption Protocols:
TLS/SSL: Secures web traffic (HTTPS)
IPsec: Secures IP communications (VPNs)
SSH: Secure remote login (replaces Telnet)
WPA2/WPA3: Wi-Fi security protocols

Threats: DDoS attacks, Man-in-the-Middle, Packet Sniffing, IP Spoofing, DNS Poisoning, ARP Spoofing

Tips

  • Memorize OSI layer order and functions using mnemonics. Common interview question: 'What layer does a router/switch operate at?' Router = Layer 3 (Network), Switch = Layer 2 (Data Link).
  • For subnetting problems, always draw out the binary representation. Practice converting between dotted decimal and CIDR notation quickly. Remember: number of hosts = 2^(host bits) - 2.
  • TCP 3-way handshake sequence is always SYN -> SYN-ACK -> ACK. Remember that final ACK may contain data. Connection termination uses 4-way handshake (FIN-ACK in each direction).
  • For routing protocol questions: Distance Vector = RIP (hop count, slow convergence); Link State = OSPF (cost-based, fast convergence); Path Vector = BGP (policy-based, inter-domain).
  • HTTP default port is 80, HTTPS is 443, SSH is 22. These come up frequently in interview questions about troubleshooting connectivity or firewall rules.
  • Understand NAT types: Static NAT (1-to-1 mapping), Dynamic NAT (pool-based), PAT (Port Address Translation - many-to-one using different ports). PAT is most common in home routers.

Practice with questions

Real placement-style technical questions.

Start Exercise →