Octet

What are the rules for shortening an IPv6 address?

Show the answer

Say this first

Two rules. First, drop leading zeros inside each hextet (0db8 becomes db8, 0000 becomes 0), never trailing zeros. Second, replace one run of consecutive all-zero hextets with ::, and only once per address. The canonical form (RFC 5952) uses :: on the longest run, the leftmost run if two are equal, never on a single zero hextet, and writes hex in lowercase.

Go deeper

Worked example

Output
2001:0db8:0000:0000:0000:ff00:0042:8329
2001:db8:0:0:0:ff00:42:8329      leading zeros removed
2001:db8::ff00:42:8329           longest zero run replaced with ::

Why :: only once

The reader rebuilds the address by counting the hextets that are written and filling the gap with zeros until there are eight. With two :: there is no way to know how many zeros go in each gap.

Expanding an address

Count the written hextets, then insert zeros for the rest.

Output
2001:db8:0:a::b:0      6 hextets written, so :: stands for 2
2001:0db8:0000:000a:0000:0000:000b:0000

RFC 5952 canonical form

Any valid form is accepted as input. RFC 5952 defines the one form tools should print, so logs, configs and search results match:

  • lowercase hex
  • leading zeros removed
  • :: on the longest run of two or more zero hextets, the first one on a tie
  • no :: for a single 0 hextet: 2001:db8:0:1:1:1:1:1 stays as is

Traps

  • Removing trailing zeros. 0100 is 100, not 1.
  • Using :: twice in one address.
  • Using :: to replace a single zero hextet. It is valid input but not canonical.
How did you do?Book 4.2 IP Version 6