make allocation universal, given a network

This commit is contained in:
Callan Bryant 2020-10-25 08:23:43 +00:00
parent 3e20f93bd7
commit af3e1b2ea9
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
3 changed files with 13 additions and 7 deletions

4
add.go
View File

@ -62,7 +62,8 @@ func Add() {
privateKey := GenerateJSONPrivateKey() privateKey := GenerateJSONPrivateKey()
publicKey := privateKey.PublicKey() publicKey := privateKey.PublicKey()
IP := conf.MustAllocateIP() IP := conf.MustAllocateIP(conf.Network.IPNet)
//IP6 := conf.MustAllocateIP(conf.Network6.IPNet)
peer := PeerConfig{ peer := PeerConfig{
Owner: owner, Owner: owner,
@ -73,6 +74,7 @@ func Add() {
PrivateKey: privateKey, // omitted from server config JSON! PrivateKey: privateKey, // omitted from server config JSON!
PresharedKey: GenerateJSONKey(), PresharedKey: GenerateJSONKey(),
IP: IP, IP: IP,
//IP6: IP6,
Networks: []JSONIPNet{}, Networks: []JSONIPNet{},
} }

View File

@ -154,12 +154,15 @@ func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
} }
// choose a free IP for a new Peer // choose a free IP for a new Peer
func (conf DsnetConfig) MustAllocateIP() net.IP { func (conf DsnetConfig) MustAllocateIP(network net.IPNet) net.IP {
network := conf.Network.IPNet
ones, bits := network.Mask.Size() ones, bits := network.Mask.Size()
zeros := bits - ones 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++ { for i := min; i <= max; i++ {
IP := make(net.IP, len(network.IP)) IP := make(net.IP, len(network.IP))

View File

@ -31,8 +31,9 @@ func Init() {
Networks: []JSONIPNet{}, Networks: []JSONIPNet{},
} }
IP := conf.MustAllocateIP() conf.IP = conf.MustAllocateIP(conf.Network.IPNet)
conf.IP = IP //conf.IP6 = conf.MustAllocateIP(conf.Network6.IPNet)
// DNS not set by default // DNS not set by default
//conf.DNS = IP //conf.DNS = IP