mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-12 17:41:08 +01:00
02dfd73e08
- Separated synchronisation calls into independent processes - Commented code for submission
53 lines
918 B
Go
53 lines
918 B
Go
package ip
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"net"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
|
|
// ULABuilder: Create a new ULA in WireGuard
|
|
type ULABuilder struct{}
|
|
|
|
func getMeshPrefix(meshId string) [16]byte {
|
|
var ulaPrefix [16]byte
|
|
|
|
ulaPrefix[0] = 0xfd
|
|
|
|
s := sha1.Sum([]byte(meshId))
|
|
|
|
for i := 1; i < 7; i++ {
|
|
ulaPrefix[i] = s[i-1]
|
|
}
|
|
|
|
ulaPrefix[7] = 1
|
|
return ulaPrefix
|
|
}
|
|
|
|
func (u *ULABuilder) GetIPNet(meshId string) (*net.IPNet, error) {
|
|
meshBytes := getMeshPrefix(meshId)
|
|
var meshIP net.IP = meshBytes[:]
|
|
|
|
ip := fmt.Sprintf("%s/%d", meshIP.String(), 64)
|
|
_, net, err := net.ParseCIDR(ip)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return net, nil
|
|
}
|
|
|
|
func (u *ULABuilder) GetIP(key wgtypes.Key, meshId string, collisionCount uint8) (net.IP, error) {
|
|
ulaPrefix := getMeshPrefix(meshId)
|
|
|
|
c, err := NewCga(key, collisionCount, ulaPrefix)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.GetIP(), nil
|
|
}
|