Skip to content

Instantly share code, notes, and snippets.

@laduke
Last active September 23, 2023 15:53
Show Gist options
  • Save laduke/fa1e9a68a79d9038ab117ad0ab69927a to your computer and use it in GitHub Desktop.
Save laduke/fa1e9a68a79d9038ab117ad0ab69927a to your computer and use it in GitHub Desktop.
ZeroTier 6PLANE and RFC4193 address calculation

ZeroTier IPv6 Auto-Assign Addresses

https://www.zerotier.com/manual.shtml#2_2_3

  • A network ID is 16 hex digits (9bee8941b5de0691)
  • A node ID is 10 hex digits (1234512345)

ZeroTier RFC4193 (/128 for each device)

  • fd9b:ee89:41b5:de06:9199:9312:3451:2345

ZeroTier 6PLANE (/80 routable for each device)

  • fc2e:308f:d012:3451:2345:0000:0000:0001
@laduke
Copy link
Author

laduke commented Dec 4, 2018

js 6plane

var assert = require('assert')

var networkId = '1d7193940403e728'
var nodeId = '12345fedcb'

var splitBy2Re = /.{1,2}/g
var splitBy4Re = /.{1,4}/g
function sixPlane (networkId, nodeId) {
  var bytes = networkId.match(splitBy2Re)

  var networkPart = bytes
    .map(function xor (substr, idx, arr) {
      return parseInt(substr, 16) ^ parseInt(arr[idx + 4], 16)
    })
    .map(function toString (byte) {
      return byte.toString(16).toLowerCase()
    })
    .map(function pad (byte) {
      return (byte.length === 2) ? byte : '0' + byte
    })
    .slice(0, 4)

  var nodePart = nodeId.match(splitBy2Re)
    .slice(0, 5)

  var result = ['fc']
    .concat(networkPart)
    .concat(nodePart)
    .concat(['00', '00', '00', '00', '00', '01'])
    .join('')
    .match(splitBy4Re)
    .join(':')

  return result
}

assert(sixPlane(networkId, nodeId) === 'fc19:7274:bc12:345f:edcb:0000:0000:0001')

@laduke
Copy link
Author

laduke commented Jan 21, 2019

The RFC4193 is simpler

Something like:

function toRfc4193Ip (networkId, memberId) {
  return `fd${networkId}9993${memberId}`.match(/.{1,4}/g).join(':')
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment