generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ndp.go
44 lines (38 loc) · 1.29 KB
/
ndp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package zerotier
import (
"net"
"strconv"
"strings"
"github.com/pkg/errors"
)
func Get6Plane(networkID, address string) (string, error) {
// Taken from BSD-3-Clause-licensed project
// https://github.com/zerotier/terraform-provider-zerotier/blob/main/pkg/zerotier/member.go
networkIDInt, err := strconv.ParseUint(networkID, 16, 64)
if err != nil {
return "", errors.Wrap(err, "couldn't parse zerotier network id")
}
const maskOffset = 32
networkMask := uint32((networkIDInt >> maskOffset) ^ networkIDInt)
networkPrefix := strconv.FormatUint(uint64(networkMask), 16)
return net.ParseIP(buildIPv6("fc" + networkPrefix + address + "000000000001")).String(), nil
}
func GetRFC4193(networkID, address string) (string, error) {
// Taken from BSD-3-Clause-licensed project
// https://github.com/zerotier/terraform-provider-zerotier/blob/main/pkg/zerotier/member.go
return net.ParseIP(buildIPv6("fd" + networkID + "9993" + address)).String(), nil
}
func buildIPv6(data string) string {
// Taken from BSD-3-Clause-licensed project
// https://github.com/zerotier/terraform-provider-zerotier/blob/main/pkg/zerotier/member.go
s := strings.SplitAfter(data, "")
end := len(s) - 1
result := ""
for i, s := range s {
result += s
if (i+1)%4 == 0 && i != end {
result += ":"
}
}
return result
}