mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-08 15:09:02 +01:00
Handle Network out of range (#347)
This commit is contained in:
parent
43e472c958
commit
02a6ac44be
@ -203,8 +203,11 @@ func (s *Server) registerPeer(peerKey wgtypes.Key, req *proto.LoginRequest) (*Pe
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if s, ok := status.FromError(err); ok && s.Code() == codes.FailedPrecondition {
|
s, ok := status.FromError(err)
|
||||||
return nil, err
|
if ok {
|
||||||
|
if s.Code() == codes.FailedPrecondition || s.Code() == codes.OutOfRange {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil, status.Errorf(codes.NotFound, "provided setup key doesn't exists")
|
return nil, status.Errorf(codes.NotFound, "provided setup key doesn't exists")
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/c-robinson/iplib"
|
"github.com/c-robinson/iplib"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
@ -77,13 +78,10 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) {
|
|||||||
takenIPMap[ip.String()] = struct{}{}
|
takenIPMap[ip.String()] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
ips, _, err := generateIPs(&ipNet, takenIPMap)
|
ips, _ := generateIPs(&ipNet, takenIPMap)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed allocating new IP for the ipNet %s and takenIps %s", ipNet.String(), takenIps)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ips) == 0 {
|
if len(ips) == 0 {
|
||||||
return nil, fmt.Errorf("failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
|
return nil, status.Errorf(codes.OutOfRange, "failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// pick a random IP
|
// pick a random IP
|
||||||
@ -95,7 +93,7 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generateIPs generates a list of all possible IPs of the given network excluding IPs specified in the exclusion list
|
// generateIPs generates a list of all possible IPs of the given network excluding IPs specified in the exclusion list
|
||||||
func generateIPs(ipNet *net.IPNet, exclusions map[string]struct{}) ([]net.IP, int, error) {
|
func generateIPs(ipNet *net.IPNet, exclusions map[string]struct{}) ([]net.IP, int) {
|
||||||
|
|
||||||
var ips []net.IP
|
var ips []net.IP
|
||||||
for ip := ipNet.IP.Mask(ipNet.Mask); ipNet.Contains(ip); incIP(ip) {
|
for ip := ipNet.IP.Mask(ipNet.Mask); ipNet.Contains(ip); incIP(ip) {
|
||||||
@ -108,10 +106,10 @@ func generateIPs(ipNet *net.IPNet, exclusions map[string]struct{}) ([]net.IP, in
|
|||||||
lenIPs := len(ips)
|
lenIPs := len(ips)
|
||||||
switch {
|
switch {
|
||||||
case lenIPs < 2:
|
case lenIPs < 2:
|
||||||
return ips, lenIPs, nil
|
return ips, lenIPs
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return ips[1 : len(ips)-1], lenIPs - 2, nil
|
return ips[1 : len(ips)-1], lenIPs - 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,7 +346,10 @@ func (am *DefaultAccountManager) AddPeer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
network := account.Network
|
network := account.Network
|
||||||
nextIp, _ := AllocatePeerIP(network.Net, takenIps)
|
nextIp, err := AllocatePeerIP(network.Net, takenIps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
newPeer := &Peer{
|
newPeer := &Peer{
|
||||||
Key: peer.Key,
|
Key: peer.Key,
|
||||||
|
Loading…
Reference in New Issue
Block a user