diff --git a/management/server/grpcserver.go b/management/server/grpcserver.go index d35d0adb4c7..81d0e140375 100644 --- a/management/server/grpcserver.go +++ b/management/server/grpcserver.go @@ -203,8 +203,11 @@ func (s *Server) registerPeer(peerKey wgtypes.Key, req *proto.LoginRequest) (*Pe }, }) if err != nil { - if s, ok := status.FromError(err); ok && s.Code() == codes.FailedPrecondition { - return nil, err + s, ok := status.FromError(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") } diff --git a/management/server/network.go b/management/server/network.go index 8b9cd1d9b5f..c094c58aa0b 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -1,9 +1,10 @@ package server import ( - "fmt" "github.com/c-robinson/iplib" "github.com/rs/xid" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "math/rand" "net" "sync" @@ -77,13 +78,10 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) { takenIPMap[ip.String()] = struct{}{} } - ips, _, err := generateIPs(&ipNet, takenIPMap) - if err != nil { - return nil, fmt.Errorf("failed allocating new IP for the ipNet %s and takenIps %s", ipNet.String(), takenIps) - } + ips, _ := generateIPs(&ipNet, takenIPMap) 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 @@ -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 -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 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) switch { case lenIPs < 2: - return ips, lenIPs, nil + return ips, lenIPs default: - return ips[1 : len(ips)-1], lenIPs - 2, nil + return ips[1 : len(ips)-1], lenIPs - 2 } } diff --git a/management/server/peer.go b/management/server/peer.go index c4c04bb9959..a9b33f33465 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -346,7 +346,10 @@ func (am *DefaultAccountManager) AddPeer( } network := account.Network - nextIp, _ := AllocatePeerIP(network.Net, takenIps) + nextIp, err := AllocatePeerIP(network.Net, takenIps) + if err != nil { + return nil, err + } newPeer := &Peer{ Key: peer.Key,