Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv6 Link/Local and SLAAC address calculations based on specified MAC address. #57

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
44 changes: 44 additions & 0 deletions ipcalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,50 @@ def guess_network(self):
netmask = 0x100000000 - 2**(32-self.mask)
return Network(netmask & self.ip, mask=self.mask)

@staticmethod
def mac_to_slaac_suffix_int(mac):
""" converts a mac address in the form 'XX:XX:XX:XX:XX:XX' into the
an address with zeroes for the first 64bits and the SLAAC or Link/Local
bits for the last 64 bits and returned in integer form.
"""
mac = mac.strip()
invalidMacMsg = "mac address of '{}' is not valid".format(mac)
macparts = mac.upper().split(":")
if not len(macparts) == 6:
raise ValueError(invalidMacMsg)
addr = ((int(macparts[0], 16) ^ 0x02) << (8*7)) + \
(int(macparts[1], 16) << (8*6)) + \
(int(macparts[2], 16) << (8*5)) + \
(0xfffe << (8*3)) + \
(int(macparts[3], 16) << (8*2)) + \
(int(macparts[4], 16) << 8) + \
(int(macparts[5], 16))
return addr

@staticmethod
def mac_to_slaac_suffix(mac):
""" converts a mac address in the form XX:XX:XX:XX:XX:XX into the
an address with zeroes for the first 64bits and the SLAAC or Link/Local
bits for the last 64 bits
"""
addr = IP.mac_to_slaac_suffix_int(mac)
return IP(addr, mask=64)

@staticmethod
def mac_to_linklocal(mac):
"""Convert a MAC address in the form 'XX:XX:XX:XX:XX:XX' to an IPv6
Link/Local address.
"""
return IP((0xFE80 << 112) + IP.mac_to_slaac_suffix_int(mac), mask=64)

def toSlaac(self, mac):
""" For a given mac address in the form 'XX:XX:XX:XX:XX:XX', return the
SLAAC address with the same for this instance's address (assuming a
64 bit network mask, which *should* be the case).
"""
addr = self.mac_to_slaac_suffix_int(mac)
return IP((self.ip & (0xFFFFFFFFFFFFFFFF0000000000000000) | addr),
mask=64)

class Network(IP):

Expand Down
9 changes: 9 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ def test_ipv6_5(self):
ip = IP('8000:8000::0.0.0.1')
self.assertTrue(ip.ip == ((2**127) + (2**111) + 1))

def test_IP6_toSlaacc(self):
ip1 = IP("fd07:2218:1350:fa::a/64")
self.assertTrue(ip1.toSlaac("52:54:00:31:2a:c5").to_compressed() == "fd07:2218:1350:fa:5054:ff:fe31:2ac5")
ip2 = IP("fdcd:41a4:5559:fa02::12/64")
self.assertTrue(ip2.toSlaac("00:13:C6:01:51:5D").to_compressed() == "fdcd:41a4:5559:fa02:213:c6ff:fe01:515d")

def test_IP6_mac_to_linklocal(self):
self.assertTrue(IP.mac_to_linklocal("52:54:00:31:2a:c5").to_compressed() == "fe80::5054:ff:fe31:2ac5")
self.assertTrue(IP.mac_to_linklocal("00:13:C6:01:51:5D").to_compressed() == "fe80::213:c6ff:fe01:515d")

class TestIP(unittest.TestCase):

Expand Down