Why does Go net.IPv4 create a 16 byte address internally

Issue

I noticed Go creates a 16 byte internal representation of a 4 byte IPv4 Address when using:

// IPv4 returns the IP address (in 16-byte form) of the
// IPv4 address a.b.c.d.
func IPv4(a, b, c, d byte) IP {
    p := make(IP, IPv6len)
    copy(p, v4InV6Prefix)
    p[12] = a
    p[13] = b
    p[14] = c
    p[15] = d
    return p
}

https://golang.org/src/net/ip.go

Is there a reason that IPv4 is initially created with 16 bytes? I was doing some calculations for Broadcast and Networkaddress where I accessed the internal byte[] directly and got confused that I had to call To4() to do something like

start := binary.BigEndian.Uint32([]byte(ip))

and actually get the IPv4 Address as uint32.

Solution

It looks like they’re using a representation that allows them to easily handle both IPv4 and IPv6 addresses with the same code. They’re using a single 16-byte representation for either type of IP.

The comments at the top of the code (that you linked to) explain in pretty clearly:

// IPv4 addresses are 4 bytes; IPv6 addresses are 16 bytes.
// An IPv4 address can be converted to an IPv6 address by
// adding a canonical prefix (10 zeros, 2 0xFFs).
// This library accepts either size of byte slice but always
// returns 16-byte addresses.

You can pass in either 4-byte or 16-byte representations of IPv4 addresses, but the library will internally use 16-byte ones and always return those.

Answered By – Darshan Rivka Whittle

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.