Last active
April 13, 2021 04:42
-
-
Save fakuivan/0bc12bf92439c82ca1a57a4d34b11822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.8 | |
from ipaddress import IPv6Network, IPv6Address | |
from typing import Optional, Tuple | |
from base64 import b32encode | |
# from https://github.com/zerotier/ZeroTierOne/blob/91b16310ea47a6de96edb488a61494f8ed8c139c/node/InetAddress.cpp#L427 | |
def mk6plane(nwid: int, nodeid: int | |
) -> Tuple[IPv6Network, IPv6Network, IPv6Address]: | |
""" | |
Given a ZeroTier node and network ID, return | |
a tuple where the first element is the subnet for the | |
whole 6plane network, the second is the subnet | |
assigned to the given node id and the 6plane address | |
for that node | |
""" | |
prefix = (nwid ^ (nwid >> 8*4)) & ((1 << 8*4) - 1) | |
net = IPv6Network(((0xfc << 8*15) + | |
(prefix << 8*11) + | |
(nodeid << 8*6), 80)) | |
return net.supernet(new_prefix=40), net, net[1] | |
# from https://github.com/zerotier/ZeroTierOne/blob/b6b11dbf8242ff17c58f10f817d754da3f8c00eb/osdep/LinuxEthernetTap.cpp#L143-L159 | |
def ifname(nwid: int, trial: int = 0) -> str: | |
""" | |
Given a ZeroTier network ID and a trial number, compute | |
the linux interface name for the network adapter | |
""" | |
nwid40 = (nwid ^ (nwid >> 8*3) + trial) & ((1 << 8*5) - 1) | |
return "zt" + b32encode(nwid40.to_bytes(5, "big") | |
).decode().lower() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment