Sub topic 4
IP addresses and ports: how machines are addressed
This chapter assumes you can read a URL (scheme, host, port) and know roughly what an IP address is. The next two chapters, DNS and TCP, depend on the details here.
Why a host name is not enough
A host name such as example.com is for people. Routers move packets using numeric IP addresses. DNS turns the name into an address, which the next chapter covers. An address only identifies a machine, though, and one machine can run many network programs. A port number tells the machine which program should receive the traffic.
IP addresses
An IPv4 address is 32 bits, written as four decimal numbers from 0 to 255, such as 93.184.216.34. That allows about 4.3 billion addresses, which is too few for every device. An IPv6 address is 128 bits, written as eight groups of hexadecimal digits, such as 2606:2800:220:1:248:1893:25c8:1946. A run of zero groups can be shortened to ::.
DNS stores them in different record types: A for IPv4 and AAAA for IPv6. You can query both:
dig +short example.com A
dig +short example.com AAAASome addresses are private and are not routed on the public internet:
| Range | Use |
|---|---|
| `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` | Home and office networks |
| `127.0.0.1` (IPv6: `::1`) | Loopback, meaning your own machine |
Your laptop usually has a private address like 192.168.1.20. Your router uses network address translation (NAT) to share one public address among all devices on the network.
Ports
A port is a 16-bit number from 0 to 65535. The pair of IP address and port identifies one service on one machine. Some well-known ports:
| Port | Service |
|---|---|
| 53 | DNS |
| 80 | HTTP |
| 443 | HTTPS |
| 22 | SSH |
When a URL has no port, the browser uses the default for the scheme. The URL class reflects this by returning an empty string when the port is the default:
console.log(new URL('https://example.com/').port); // ''
console.log(new URL('https://example.com:443/').port); // '' (443 is the default)
console.log(new URL('https://example.com:8443/').port); // '8443'One connection, four values
The browser also needs a port for its own end. The operating system picks an unused ephemeral port, usually a high number such as 52814. A TCP connection is identified by four values: source IP, source port, destination IP and destination port. This is why one browser can hold many connections to the same server. Each connection gets a different source port.
Common errors
- IPv6 in a URL needs brackets. Write
http://[::1]:8080/, because the colons in the address would otherwise be confused with the port separator. - Using an IP address with HTTPS usually fails. Certificates are normally issued for names, so the browser reports a mismatch. Many servers also host several sites on one IP and choose the site from the
Hostheader. - A server bound to
127.0.0.1is reachable only from the same machine. To accept outside connections, a server must listen on0.0.0.0(all IPv4 interfaces) or a specific external address. - Timeout and "connection refused" mean different things. A refusal means the machine answered and nothing listens on that port. A timeout often means a firewall silently dropped the packets.
Exercise
A page at https://example.com loads fine, but the same server's IP address typed as https://93.184.216.34/ shows a certificate warning. Also, why does new URL('https://example.com:443/').port return an empty string?
Show answer
The certificate was issued for the name example.com, not for the IP address, so the browser sees a mismatch. The server may also pick the site from the Host header, which the IP-only URL does not supply. The port is empty because 443 is the default for https, so the URL parser treats it as implied and omits it.