IPv6 Address
Introduction
We do a few exercises regarding IPv6 addresses.
Prefix Notation
We use write IPv6 addresses and networks in the prefix notation. Note that IPv6 does not have the broadcast address type. Instead, it has unicast, multicast, and anycast addresses. Below is a program that obtains the network number from a unicast address.
import socket
import sys
#
# python 3 only
#
def get_netnum(addr, plen):
netnumi = int.from_bytes(addr, byteorder='big') >> (128 - plen) << (128 - plen)
nmaski = 0xffffffffffffffffffffffffffffffff >> plen
netnum = netnumi.to_bytes(16, byteorder='big')
return netnum
def usage(prog):
print('Usage: ' + prog + ' ipv6address/prefix', file=sys.stderr)
def main(argv):
if len(argv) < 2:
usage(argv[0])
sys.exit(1)
addrp,plenp = sys.argv[1].split('/')
addr = socket.inet_pton(socket.AF_INET6, addrp)
plen = int(plenp)
netnum = get_netnum(addr, plen)
print(netnum)
print(socket.inet_ntop(socket.AF_INET6, netnum))
# to run as a script
if __name__ == '__main__':
main(sys.argv)
Exercises and Exploration
Let’s consider the following questions.
- Experiment with the above program.
- Write a program (or a few programs) to report the type of an IPv6 address, i.e., whether the address is a unicast address, a multicast address, a Document address if it is a unicast address, whether it is a private network address, a link-local address, a loop back address, or an embedded IPv4 address, or a global unicast address?