make allocation universal, given a network
This commit is contained in:
parent
3e20f93bd7
commit
af3e1b2ea9
4
add.go
4
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{},
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user