Loading
0x50Lesson 6 of 14

Join a network and share an address

See how devices get addresses and how NAT and IPv6 handle the shortage.

25 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • Describe how DHCP configures a device when it joins a network.
  • Explain how NAT lets many devices share one public address.
  • Read an IPv6 address and explain why it removes the need for NAT.

Connect to a café's Wi-Fi and you're online in seconds, without typing an address, a mask or a gateway. And even though IPv4 only has about 4.3 billion addresses for many more devices, your home has a dozen gadgets using just one. This lesson explains both tricks.

Private addressesLaptop192.168.1.10Phone192.168.1.11TV192.168.1.12Home routerDHCP: hands out addressesDefault gatewayNAT: shares one addressPublic address198.51.100.7InternetAll devices appear as one
A typical home: DHCP hands out private addresses, and NAT lets them all share one public address.

DHCP: getting configured automatically

A new device has no address, doesn't know who to ask, and can't yet use normal IP. DHCP solves this in four messages, often remembered as DORA: Discover, Offer, Request, Acknowledge.

Try it

Join a network

Step through a phone joining a network. Pay attention to the second prediction: a DHCP answer gives far more than an IP address.

Message 1 of 4Predicted 0/0
Phone
DHCP server

Predict: The phone has no IP address yet. How can it possibly send anything?

Private addresses and NAT

Three ranges are reserved for private use and never appear on the public Internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Millions of homes use the same addresses, and that's fine because they can't reach each other directly.

To get online, a home router performs Network Address Translation (NAT). It rewrites the source of outgoing packets to its one public address, and keeps a table so it knows where replies belong.

Try it

Share one public address

Complete the three goals. Both devices deliberately use source port 50000. What must the router do so replies don't get mixed up? Watch the public port column.

  • Connect from both devices at once
  • Deliver a reply to a device
  • Send a packet nobody asked for
Laptop 192.168.1.10:50000
Phone 192.168.1.11:50000
NAT table on the router (public address 198.51.100.7)

Empty. Connect from a device to create an entry.

IPv6: enough addresses for everything

NAT stretched IPv4, but the real fix is IPv6, whose addresses are 128 bits long. That's about 3.4 × 10³⁸ addresses, enough for every device to have a public one, so NAT isn't needed.

Addresses are written as eight groups of hexadecimal digits, with two shortcuts: leading zeros can be dropped from a group, and one run of all-zero groups can become ::.

A typical network is a /64, and devices can configure their own address from the router's announcements (SLAAC), often without DHCP. The rest works much as you've learned: prefixes, routes and longest-prefix match all carry over.

addresses.py
1import ipaddress
2
3for text in ["192.168.1.10", "8.8.8.8", "10.20.30.40"]:
4    address = ipaddress.ip_address(text)
5    print(text, "private" if address.is_private else "public")
6
7address = ipaddress.ip_address("2001:0db8:0000:0000:0000:0000:0000:0001")
8print(address.compressed)
9print(address.exploded)
Output
192.168.1.10 private
8.8.8.8 public
10.20.30.40 private
2001:db8::1
2001:0db8:0000:0000:0000:0000:0000:0001

Key takeaways

  • DHCP hands out an address, mask, gateway and DNS server on a lease.

  • NAT shares one public address by rewriting addresses and ports and remembering the mapping.

  • IPv6 provides enough addresses that devices no longer need to share.

Lesson quiz

7 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: model networking concepts

Use small Python programs to model packet, address, and protocol behavior. These exercises run locally in your browser and do not send network traffic.

Exercise 1

Classify an address

+25 XP

Read an IPv4 address and print private, self-assigned or public.

Addresses in 169.254.0.0/16 are self-assigned. Python also reports those as private, so check for them first.

  • A home address
  • After a DHCP failure
  • A public address
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Build a NAT table

+25 XP

Read a number of connections, then one address:port per line. Give each new connection the next public port, starting at 40000. A repeated connection reuses its port. Print the public port for each line.

This is the heart of what your router does for every connection.

  • Two devices on the same port
  • A repeated connection
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…