Sub topic 10
Public and private addresses
This chapter assumes you can read a dotted-decimal address, know that a prefix such as /24 splits an address into network and host parts, and recognize the special-purpose ranges from the previous chapter. It explains how addresses are split into public and private, and how to test which one you have.
Two kinds of address
A public address is globally unique and routable on the internet. Registries hand out blocks of public space to providers, and providers lease it to customers. A private address is one that anyone may use inside their own network. Routers on the internet do not forward traffic for private addresses, so two organizations can use the same private address without conflict.
Private addressing exists because IPv4 has only about 4.3 billion addresses, which is too few to give every device a public one. Reusing private space inside each network, and sharing a few public addresses through NAT, stretched the supply.
The three RFC 1918 ranges
RFC 1918 reserves three blocks for private use.
| Block | First address | Last address | Addresses |
|---|---|---|---|
| 10.0.0.0/8 | 10.0.0.0 | 10.255.255.255 | 16,777,216 |
| 172.16.0.0/12 | 172.16.0.0 | 172.31.255.255 | 1,048,576 |
| 192.168.0.0/16 | 192.168.0.0 | 192.168.255.255 | 65,536 |
The middle block is the one people get wrong. A /12 fixes the first 12 bits. That covers all of the first octet (172) and the first four bits of the second octet (0001). The remaining four bits of the second octet are free, so the second octet runs from 16 to 31. An address such as 172.32.0.1 is therefore public.
import ipaddress
RFC1918 = [
ipaddress.ip_network("10.0.0.0/8"),
ipaddress.ip_network("172.16.0.0/12"),
ipaddress.ip_network("192.168.0.0/16"),
]
def is_rfc1918(text):
address = ipaddress.ip_address(text)
return any(address in network for network in RFC1918)
print(is_rfc1918("10.20.30.40")) # True
print(is_rfc1918("172.31.255.254")) # True
print(is_rfc1918("172.32.0.1")) # False
print(is_rfc1918("8.8.8.8")) # FalseThe function checks membership in an explicit list of the three networks, so it answers exactly the question "is this an RFC 1918 address?"
Why is_private is not the same test
Python's is_private property is broader than RFC 1918. It also returns true for loopback, link-local, and other special-purpose ranges.
import ipaddress
print(ipaddress.ip_address("192.168.1.10").is_private) # True
print(ipaddress.ip_address("127.0.0.1").is_private) # True
print(ipaddress.ip_address("8.8.8.8").is_private) # FalseLoopback (127.0.0.0/8) is not an RFC 1918 range. A host cannot use it to talk to other machines. Use is_private to ask "is this address unlikely to be a normal internet host?" and use an explicit list when you need RFC 1918 specifically.
What happens at the border
A private address can be used inside a network, but a packet sent to the internet needs a public source address. Otherwise the reply would have nowhere to go. A router at the network edge performs network address translation (NAT) and rewrites the private source address to a public one. The next chapter covers this mechanism in detail.
Common mistakes
- Treating 172.32.0.1 and above as private, as described above.
- Choosing the same private range for two networks that later need to connect. Routing between them then becomes ambiguous.
- Assuming a private address is secure. It is only unreachable by normal routing from the internet, and it does not replace a firewall.
Exercise
Is 172.15.4.4 private under RFC 1918? Is 172.20.4.4? Explain using the second octet.
Show answer
172.15.4.4 is not private, because the second octet 15 is below 16, so it lies before 172.16.0.0/12. 172.20.4.4 is private, because a /12 covers second octets 16 through 31 and 20 falls in that range.
Exercise
What does ipaddress.ip_address("127.0.0.1").is_private return, and why would is_rfc1918("127.0.0.1") return something different?
Show answer
is_private returns True because it includes loopback and other special-purpose ranges. is_rfc1918("127.0.0.1") returns False because 127.0.0.0/8 is not one of the three RFC 1918 networks in the list.