From af3e1b2ea9b0dd6b873f2d32f91b07eeab3b4610 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Sun, 25 Oct 2020 08:23:43 +0000 Subject: [PATCH] make allocation universal, given a network --- add.go | 4 +++- configtypes.go | 11 +++++++---- init.go | 5 +++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/add.go b/add.go index af5c75b..d1ed195 100644 --- a/add.go +++ b/add.go @@ -62,7 +62,8 @@ func Add() { privateKey := GenerateJSONPrivateKey() publicKey := privateKey.PublicKey() - IP := conf.MustAllocateIP() + IP := conf.MustAllocateIP(conf.Network.IPNet) + //IP6 := conf.MustAllocateIP(conf.Network6.IPNet) peer := PeerConfig{ Owner: owner, @@ -73,6 +74,7 @@ func Add() { PrivateKey: privateKey, // omitted from server config JSON! PresharedKey: GenerateJSONKey(), IP: IP, + //IP6: IP6, Networks: []JSONIPNet{}, } diff --git a/configtypes.go b/configtypes.go index 449c264..5f5ed3f 100644 --- a/configtypes.go +++ b/configtypes.go @@ -154,12 +154,15 @@ func (conf DsnetConfig) IPAllocated(IP net.IP) bool { } // choose a free IP for a new Peer -func (conf DsnetConfig) MustAllocateIP() net.IP { - network := conf.Network.IPNet +func (conf DsnetConfig) MustAllocateIP(network net.IPNet) net.IP { ones, bits := network.Mask.Size() zeros := bits - ones - min := 1 // avoids network addr - max := (1 << zeros) - 2 // avoids broadcast addr + overflow + + // avoids network addr + min := 1 + // avoids broadcast addr + overflow. Note there is no broadcast addr with + // IPv6, but I don't care about losing one when there are so many! + max := (1 << zeros) - 2 for i := min; i <= max; i++ { IP := make(net.IP, len(network.IP)) diff --git a/init.go b/init.go index 27bc830..5321b4f 100644 --- a/init.go +++ b/init.go @@ -31,8 +31,9 @@ func Init() { Networks: []JSONIPNet{}, } - IP := conf.MustAllocateIP() - conf.IP = IP + conf.IP = conf.MustAllocateIP(conf.Network.IPNet) + //conf.IP6 = conf.MustAllocateIP(conf.Network6.IPNet) + // DNS not set by default //conf.DNS = IP