Networking

What Is a Network?

IP Addresses

$ hostname -I           # Linux
192.168.1.42 fd00::1

$ ipconfig getifaddr en0  # macOS
192.168.1.42

$ ipconfig              # Windows
   IPv4 Address . . . . . : 192.168.1.42

Ports

Port Service
22 SSH
25 SMTP
53 DNS
80 HTTP
443 HTTPS
5432 PostgreSQL
3306 MySQL
$ netstat -an | grep LISTEN | head -n 5
tcp4   0   0   127.0.0.1.5432    *.*     LISTEN
tcp4   0   0   *.8000            *.*     LISTEN
tcp6   0   0   *.22              *.*     LISTEN

TCP vs. UDP

Think of TCP as a phone call:
  - You dial, the other end picks up (handshake)
  - Both sides take turns talking (ordered, reliable)
  - Either side can hang up gracefully (teardown)

Think of UDP as sending a postcard:
  - You write an address and drop it in a box
  - No confirmation it arrived
  - Very fast and simple

Sockets

import socket

HOST = "127.0.0.1"
PORT = 9000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((HOST, PORT))
    server.listen(1)
    print(f"listening on {HOST}:{PORT}")
    conn, addr = server.accept()
    with conn:
        print(f"connected from {addr}")
        data = conn.recv(1024)
        conn.sendall(data)   # echo it back
import socket

HOST = "127.0.0.1"
PORT = 9000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    client.sendall(b"hello from client")
    data = client.recv(1024)
    print(f"received: {data!r}")

Exercise: Echo Server

  1. Run the TCP server in one terminal and the client in another. What do you see on each side?

  2. Modify the server to handle multiple clients in sequence (add a while True loop around server.accept()). What happens if two clients connect at the same time?

DNS: Domain Name System

DNS Record Types

Type Purpose Example
A hostname → IPv4 address example.com → 93.184.216.34
AAAA hostname → IPv6 address example.com → 2606:2800:220:1::68
CNAME alias → canonical hostname www.example.com → example.com
MX mail exchange for a domain example.com → mail.example.com
TXT arbitrary text (verification, SPF records) "v=spf1 include:..."
NS authoritative name servers for a domain example.com → ns1.example.com

How DNS Resolution Works

# Add to /etc/hosts for local development:
127.0.0.1   myapp.local
127.0.0.1   api.myapp.local

Querying DNS from the Command Line

$ dig example.com A

;; ANSWER SECTION:
example.com.    3600  IN  A  93.184.216.34

$ dig example.com MX

;; ANSWER SECTION:
example.com.  3600  IN  MX  0 .

$ dig +short github.com
140.82.114.4
$ dig -x 93.184.216.34 +short
93.184.216.34.in-addr.arpa domain name pointer example.com.

DNS in Python

import socket

# Forward lookup: hostname → IP address
ip = socket.gethostbyname("example.com")
print(f"example.com → {ip}")
# example.com → 93.184.216.34

# Returns all addresses and aliases (useful for hosts with multiple IPs)
hostname, aliases, addresses = socket.gethostbyname_ex("github.com")
print(f"hostname: {hostname}")
print(f"addresses: {addresses}")

# Reverse lookup: IP address → hostname
name, _, _ = socket.gethostbyaddr("93.184.216.34")
print(f"93.184.216.34 → {name}")
import dns.resolver

answers = dns.resolver.resolve("example.com", "MX")
for rdata in answers:
    print(f"priority {rdata.preference}: {rdata.exchange}")

Exercise: DNS Exploration

  1. Use dig to find all the MX records for a domain you use regularly. What do the priority numbers mean?

  2. Add an entry to /etc/hosts that maps test.local to 127.0.0.1. Confirm you can ping test.local. What does this tell you about the order in which lookups are tried?

  3. What is the TTL on the A record for github.com? How does TTL affect how quickly DNS changes propagate?

Network Troubleshooting

$ traceroute github.com
 1  router.local (192.168.1.1)         1.2 ms
 2  10.0.0.1 (10.0.0.1)               8.4 ms
 3  ae-3.r01.tor01.net (206.x.x.x)   15.1 ms
…
12  lb-140-82-114-4-iad.github.com    22.8 ms

Exercise: Following a Request

Use the tools above to trace what happens when you access a familiar website:

  1. Use dig to find its IP address.
  2. Use ping to check latency to that IP.
  3. Use traceroute to see the path.
  4. Use curl -I to inspect the HTTP headers.

Do any of the hops in traceroute show * * *? What does that mean?