correct IP enumeration
This commit is contained in:
parent
2fcda8b90a
commit
faf0e59887
11
types.go
11
types.go
@ -3,7 +3,6 @@ package dsnet
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -24,7 +23,7 @@ type PeerConfig struct {
|
|||||||
PublicKey JSONKey `validate:"required,len=44"`
|
PublicKey JSONKey `validate:"required,len=44"`
|
||||||
PresharedKey JSONKey `validate:"required,len=44"`
|
PresharedKey JSONKey `validate:"required,len=44"`
|
||||||
Endpoint net.UDPAddr `validate:"required,udp4_addr"`
|
Endpoint net.UDPAddr `validate:"required,udp4_addr"`
|
||||||
AllowedIPs []net.IPNet `validate:"dive,required,cidr"`
|
AllowedIPs []JSONIPNet `validate:"dive,required,cidr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
@ -86,7 +85,7 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
|
|||||||
func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
|
func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
|
||||||
for _, peer := range conf.Peers {
|
for _, peer := range conf.Peers {
|
||||||
for _, peerIPNet := range peer.AllowedIPs {
|
for _, peerIPNet := range peer.AllowedIPs {
|
||||||
if IP.Equal(peerIPNet.IP) {
|
if IP.Equal(peerIPNet.IPNet.IP) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,10 +110,10 @@ func (conf DsnetConfig) ChooseIP() (net.IP, error) {
|
|||||||
for j := 0; j < len(IP); j++ {
|
for j := 0; j < len(IP); j++ {
|
||||||
shift := (len(IP) - j - 1) * 8
|
shift := (len(IP) - j - 1) * 8
|
||||||
IP[j] = IP[j] | byte(i>>shift)
|
IP[j] = IP[j] | byte(i>>shift)
|
||||||
|
}
|
||||||
|
|
||||||
if conf.IPAllocated(IP) {
|
if conf.IPAllocated(IP) {
|
||||||
return IP, nil
|
return IP, nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user