Sub topic 3
Writing and compressing addresses
This chapter assumes you can read hexadecimal and convert between hex and binary. It also assumes you know that an IPv6 address has 128 bits, written as eight 16-bit groups. Here you will learn the rules for shortening an address, the rules for expanding it again, and the mistakes that make an address invalid or ambiguous.
The full form
An IPv6 address is eight groups of four hexadecimal digits, separated by colons. Each group is 16 bits, so the eight groups add up to 128 bits.
2001:0db8:0000:0000:0000:ff00:0042:8329This is the exploded form. It is exact but hard to read, and many real addresses contain long runs of zeros. Two rules shorten it. Both preserve the value of the address.
Rule 1: drop leading zeros in each group
You can remove leading zeros inside any group. You must keep at least one digit, so a group of all zeros becomes 0.
0db8becomesdb80042becomes420000becomes0ff00staysff00, because the zeros are trailing, not leading
Never remove trailing zeros. ff00 and ff are different groups, and swapping one for the other changes the address.
Applying Rule 1 to the example gives 2001:db8:0:0:0:ff00:42:8329.
Rule 2: replace one run of zero groups with `::`
A double colon stands for one or more consecutive groups that are all zero. The example has three zero groups in a row, so they collapse:
2001:db8:0:0:0:ff00:42:8329 -> 2001:db8::ff00:42:8329The double colon may appear only once in an address. The reason is that a reader recovers the hidden groups by arithmetic: they count the groups that are written, and the missing ones must make the total eight. With two :: markers, the reader could not tell how many zero groups each one hides.
2001:db8::1::1 invalid: two "::" markers, so the split is ambiguousWhich run to compress
An address can contain several zero runs. The recommended text form in RFC 5952 settles the choice with three rules:
- Compress the longest run of zero groups.
- If two runs are equally long, compress the first one.
- Never use
::for a single zero group. Write0instead.
Rule 3 is the one people miss. A single group saves no space, and a lone :: is easy to misread.
| Full address | Recommended form | Why |
|---|---|---|
| `2001:0db8:0000:0000:0001:0000:0000:0001` | `2001:db8::1:0:0:1` | Two runs of length 2 tie, so the first is compressed |
| `2001:0db8:0000:0001:0000:0000:0000:0001` | `2001:db8:0:1::1` | The longer run (3 groups) wins |
| `2001:0db8:0000:0001:0001:0001:0001:0001` | `2001:db8:0:1:1:1:1:1` | A single zero group is not compressed |
| `0000:0000:0000:0000:0000:0000:0000:0001` | `::1` | The whole address except the last group is zero |
| `0000:0000:0000:0000:0000:0000:0000:0000` | `::` | The entire address is zero |
The same rules are also summarized in this flow.
Valid IPv6 text is case-insensitive, so 2001:DB8::1 and 2001:db8::1 are the same address. RFC 5952 recommends lowercase for output so that logs, configuration files, and text searches match.
Expanding a compressed address
Expansion reverses the process. Take 2001:db8::ff00:42:8329:
- Split the text at
::. The left part has 2 groups (2001,db8). The right part has 3 groups (ff00,42,8329). - Subtract from eight: 8 − 2 − 3 = 3 groups are missing.
- Insert three
0groups where the::was. - Pad each group with leading zeros to four digits.
The result is 2001:0db8:0000:0000:0000:ff00:0042:8329. This function does the same thing:
def expand(text):
if text.count("::") > 1:
raise ValueError("more than one '::'")
if "::" in text:
head, tail = text.split("::")
left = head.split(":") if head else []
right = tail.split(":") if tail else []
missing = 8 - len(left) - len(right)
if missing < 1:
raise ValueError("'::' must replace at least one group")
groups = left + ["0"] * missing + right
else:
groups = text.split(":")
if len(groups) != 8:
raise ValueError("an address needs exactly 8 groups")
return ":".join(g.zfill(4) for g in groups)
print(expand("2001:db8::ff00:42:8329")) # 2001:0db8:0000:0000:0000:ff00:0042:8329
print(expand("::1")) # 0000:0000:0000:0000:0000:0000:0000:0001The function does not check that each group is valid hex. For real work, use the standard library, which does all the checks and implements the recommended output form:
import ipaddress
addr = ipaddress.ip_address("2001:0db8:0000:0000:0000:ff00:0042:8329")
print(addr.compressed) # 2001:db8::ff00:42:8329
print(addr.exploded) # 2001:0db8:0000:0000:0000:ff00:0042:8329
try:
ipaddress.ip_address("2001:db8::1::1")
except ValueError as err:
print("rejected:", err)Prefixes and compression
Prefixes use the same notation as addresses, followed by a slash and the prefix length. Compression applies to the address part:
2001:db8:abcd::/48is the prefix2001:0db8:abcd:0000:0000:0000:0000:0000/48.::/0is the default route, which matches every address.fe80::/10andff00::/8are the link-local and multicast prefixes.
The length is always written in decimal. It is a count of bits, not a hex value.
Writing addresses in other contexts
Colons already have meaning in URLs, socket addresses, and zone names, so IPv6 text needs some extra notation.
- URLs and ports. Put the address in square brackets:
http://[2001:db8::1]:8080/. Without the brackets, the port colon cannot be told apart from the address colons. - Zone identifiers. A link-local address is only meaningful on one link, so tools append
%and an interface name:fe80::1%eth0. On Windows the suffix is a number, such asfe80::1%12. The zone is not part of the 128-bit address. - Embedded IPv4. The last 32 bits can be written in dotted decimal, as in
::ffff:192.0.2.1. This appears in transition mechanisms, which a later chapter covers. - DNS reverse names. These use one hex digit per label, in reverse order, under
ip6.arpa. You will rarely type them by hand.
Common errors
| Mistake | Example | Problem |
|---|---|---|
| Two `::` markers | `2001::db8::1` | Ambiguous, so parsers reject it |
| Group longer than 4 digits | `2001:db80a::1` | A group holds 16 bits, at most four hex digits |
| Too many groups | `1:2:3:4:5:6:7:8:9` | More than 128 bits |
| Too few groups without `::` | `2001:db8:1:1` | Fewer than eight groups and nothing to fill the gap |
| Non-hex character | `2001:db8::g1` | Only `0-9` and `a-f` are valid |
| Removing trailing zeros | `ff00` written as `ff` | Changes the value |
| Using `::` for one zero group | `2001:db8::1:1:1:1:1` | Parsers accept this, but it is not the recommended form |
The last row is a style issue, not an error. Many parsers accept 2001:db8::1:1:1:1:1, but a tool that follows RFC 5952 would print 2001:db8:0:1:1:1:1:1.
Exercise
Write the recommended compressed form of 2001:0db8:0000:0000:0042:0000:0000:0001, and explain which zero run you compressed.
Show answer
2001:db8::42:0:0:1. After stripping leading zeros the groups are 2001, db8, 0, 0, 42, 0, 0, 1. There are two zero runs of equal length (2 groups each). RFC 5952 says to compress the first of equal-length runs, so only the first pair becomes ::, and the second pair stays as 0:0.
Exercise
How many groups does ::ffff:1 hide behind the ::, and what is the exploded form?
Show answer
The written groups are ffff and 1, which is 2 groups, so :: hides 8 − 2 = 6 zero groups. The exploded form is 0000:0000:0000:0000:0000:0000:ffff:0001.
Exercise
Why is 2001:db8::5::9 invalid, and why is [2001:db8::9]:443 the correct way to write that address with a port?
Show answer
It has two :: markers, so a parser cannot tell how many zero groups each one stands for. The brackets separate the address from the port, because the port's colon would otherwise look like one more address group separator.