Computer Networks

Learn how computers talk to each other!

OSI Model

The OSI (Open Systems Interconnection) model is a conceptual framework that describes how data moves through a network, divided into 7 layers:

Pasted image 20251218110251.png

Layer 1 - Physical Layer

Layer 3 - Network Layer

Layer 4 - Transport Layer

Layer 5 - Session Layer

Layer 6 - Presentation Layer

Layer 7 - Application Layer

What Happens When You Enter a URL in Your Browser?

Pasted image 20251218105334.png

  1. URL -> Universal Resource Locator (scheme://domain:port/path/resource)
  2. DNS Lookup (translate domain name to IP addresses)
    1. Browser looks up IP in DNS Cache (Browser -> OS -> Router -> ISP)
    2. Make request to DNS resolver which resolves via recursive queries to the DNS server (Root -> TLD -> Authoritative Name Server)
  3. Establish TCP Connection
    1. May be served via CDN (Content Delivery Network) -> Cache static and dynamic materials closer to the browser
    2. Use keep-alive connections to reuse an established TCP connection
    3. If HTTPS -> SSL/TLS handshake -> Use SSL session resumption to lower cost of handshake establishment
  4. Sends HTTP Request: Over connection to server and receives response to render the webpage

TCP vs UDP

Aspect TCP UDP
Connection Type Connection-oriented (requires a handshake to establish a connection) Connectionless (no handshake, data sent without establishing a connection)
Reliability Reliable (ensures data is delivered in order, without errors, and retransmits lost packets) Unreliable (no guarantee of delivery, order, or error-checking)
Data Transmission Data is sent as a stream (broken into segments) Data is sent in individual packets (datagrams)
Speed Slower due to overhead from error-checking, ordering, and retransmission Faster due to minimal overhead and no need for error-checking
Flow Control Supports flow control to prevent congestion No flow control, data is sent as fast as the application can send
Error Handling Performs error detection and recovery (retransmits lost or corrupted packets) No error recovery, though it can detect errors (but doesn’t correct them)
Packet Ordering Ensures packets arrive in order (sequencing) No guarantee of packet order, which may arrive out of sequence
Header Size Larger header (20 bytes minimum) Smaller header (8 bytes)
Overhead Higher overhead due to headers for connection management, error-checking, and acknowledgments Lower overhead, with a simpler header format and no connection management
Use Case Used for applications where reliability and ordered delivery are important (e.g., file transfer, web browsing, email) Used for applications where speed is more important than reliability (e.g., video streaming, online gaming, VoIP)
Applications Web browsing (HTTP/HTTPS), file transfer (FTP), email (SMTP) Streaming (video, audio), DNS, gaming, real-time communication

TCP 3-Way Handshake

0_O1Czi6A1i9EmRRdP.webp

  1. SYN (Synchronize):
    • The client initiates the connection by sending a TCP segment with the SYN (synchronize) flag set.
    • This segment also includes a randomly generated sequence number (say, x) that will be used to keep track of the bytes in the data stream.
    • This step signals the server that the client wants to establish a connection.
  2. SYN-ACK (Synchronize-Acknowledge):
    • Upon receiving the SYN segment, the server responds with a segment that has both the SYN and ACK (acknowledge) flags set.
    • The ACK acknowledges the client's SYN by setting the acknowledgment number to x+1, indicating that the server received the client's SYN request.
    • The server also generates its own sequence number (say, y) and sends it in the SYN-ACK segment.
  3. ACK (Acknowledge):
    • Finally, the client responds with an ACK segment, which acknowledges the server's SYN-ACK by setting the acknowledgment number to y+1.
    • At this point, both the client and the server have synchronized their sequence numbers, and the connection is established.

TCP 4-Way Termination

Pasted image 20251218102102.png

  1. FIN (Finish):
    • The side that wants to terminate the connection (usually the client) sends a FIN segment, indicating that it has finished sending data.
    • The FIN flag informs the other side that it should no longer expect data from the sender.
  2. ACK (Acknowledge):
    • The recipient (usually the server) responds with an ACK segment to acknowledge the receipt of the FIN segment. This indicates that it is aware that the connection is closing from the other side.
    • At this point, the connection is considered half-closed: the side that sent the FIN can no longer send data, but it can still receive data until the other side closes.
  3. FIN (Finish):
    • If the other side (the server, in this case) has finished its data transmission, it will also send a FIN segment to indicate it is done sending data as well.
  4. ACK (Acknowledge):
    • Finally, the first side (the client) responds with an ACK segment, acknowledging the server's FIN segment. At this point, the connection is fully closed.

Why TCP Termination Requires a 4-Way Handshake?

TCP Congestion Control

TLS/SSL

0e18db0d-f511-4f85-bb58-388fce70d42e_2631x2103.webp

  1. Client Hello:
    • The client (e.g., a browser) initiates the handshake by sending a Client Hello message to the server. This message includes:
      • The TLS version the client supports (e.g., TLS 1.2, TLS 1.3).
      • A randomly generated client random number, used later for key generation.
      • A list of cipher suites (encryption algorithms) the client supports.
      • A list of compression methods the client supports.
      • Optionally, a list of extensions, such as the Server Name Indication (SNI), which tells the server the domain name the client wants to connect to.
  2. Server Hello:
    • The server responds with a Server Hello message that includes:
      • The chosen TLS version (if multiple versions are supported).
      • A randomly generated server random number.
      • The chosen cipher suite from the list provided by the client.
      • The server may also send extensions (e.g., ALPN for protocol negotiation).
    • In the next steps, the server's identity is verified to establish trust.
  3. Server Certificate:
    • The server sends its digital certificate to the client. This certificate contains:
      • The server’s public key.
      • The identity of the server (domain name).
      • A digital signature from a trusted Certificate Authority (CA), which the client can verify.
    • The client uses the server’s public key to later establish a shared secret and verify the server's identity.
    • If client authentication is required, the server will request the client’s certificate (not common in typical web traffic).
  4. Key Exchange (TLS 1.2) or Key Share (TLS 1.3):
    • TLS 1.2: The client generates a pre-master secret, encrypts it with the server’s public key, and sends it to the server. Both parties use the pre-master secret, the client random, and the server random to generate the session keys (symmetric keys) that will be used to encrypt the communication.
    • Pasted image 20240922225520.png
    • TLS 1.3: The client and server use Elliptic-Curve Diffie-Hellman (ECDHE) or similar algorithms to securely share a key over the open connection. No pre-master secret is sent; instead, both parties generate shared keys using their private keys and the exchanged public parameters.
  5. Server Finished:
    • The server sends a Finished message, which is encrypted with the session key. This message contains a cryptographic hash of the entire handshake up to this point, ensuring that the handshake hasn't been tampered with.
  6. Client Finished:
    • The client responds with its own Finished message, similarly encrypted with the session key. It also includes a hash of the handshake. Once this is sent, the secure communication is established.

HTTP

0.9

1.0

1.1

2

3

WebSockets

Protocol designed to provide full-duplex (two-way) communication channels over a single, long-lived TCP connection

IPv4

IPv6

DNS

NAT

UDP Hole Punching

BGP

OSPF

Anycast / Multicast

ARP

VRRP

Wireguard

  1. IPv6
    1. GUA -> Global Unicast Address (2000/3)
    2. ULA -> Unicast Local Address (fc00/7)
  2. How does Virtual Private Network (VPN) do IP switching?
    • Allows users to change or "switch" their IP addresses by creating a secure, encrypted tunnel between the user’s device and a remote VPN server.
    • Process of VPN IP Switching:

      1. User Connects to a VPN Server:
        • When a user initiates a connection to a VPN, the VPN software on the user’s device (VPN client) establishes a secure connection to one of the VPN provider’s remote servers located in a different geographical region. The VPN client encrypts all traffic sent from the user’s device.
      2. VPN Server Assigns a New IP Address:
        • The VPN server assigns the user a new public IP address that corresponds to the server’s location. For instance, if the user connects to a VPN server in France, they will be assigned a French IP address. This is the IP address that will be visible to websites and services the user interacts with.
      3. IP Masking:
        • The VPN server hides the user's original IP address by acting as an intermediary. All internet traffic is routed through the VPN server, making it appear as though the requests are coming from the VPN server’s IP address instead of the user’s original IP.
      4. Routing of Internet Traffic:
        • When the user sends a request (such as visiting a website), it first goes to the VPN server. The VPN server forwards the request to the destination (the website or service), using the VPN-assigned IP address.
        • The response from the website is then sent back to the VPN server, which decrypts it (if necessary) and forwards it to the user. Throughout this process, the user's real IP address remains hidden, and only the VPN server's IP is exposed.
      5. Dynamic IP Switching:
        • Some VPN services provide the ability to switch between different VPN servers manually, which effectively changes the user's public IP address to match the location of the new server. For example, switching from a server in the US to one in the UK will change the user’s IP address from a US-based IP to a UK-based one.
      6. IP Rotation:
        • Certain VPN providers may offer dynamic IP rotation, where the VPN server periodically assigns a new IP address to the user during the same session. This adds an extra layer of anonymity, as the user’s apparent IP address keeps changing.
    • How VPNs Achieve IP Switching:

      1. Network Address Translation (NAT):
        • VPN servers use NAT to translate the user’s private IP address (assigned by their local network, e.g., 192.168.x.x) into the VPN server’s public IP address. All the traffic appears to originate from the VPN server’s IP, effectively masking the user’s true IP.
      2. Encryption:
        • VPNs secure all communication between the user’s device and the VPN server using encryption (such as AES-256). This ensures that even if someone intercepts the traffic, they won’t be able to see the user’s original IP address or the content of their data.
      3. Geolocation:
        • By choosing VPN servers in different countries, users can effectively change their virtual location. Websites and services will think the user is located wherever the VPN server is based, allowing the user to access region-restricted content or appear to be in a different country.