From 131f70b2a42f8683259637f00a894544733f8c68 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Wed, 25 Jan 2023 16:01:46 +0530 Subject: [PATCH] fix address range check --- logic/nodes.go | 2 +- logic/util.go | 19 ++----------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/logic/nodes.go b/logic/nodes.go index c30aeb2a..9d0a886a 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -50,7 +50,7 @@ func GetNetworkNodes(network string) ([]models.Node, error) { func UpdateNode(currentNode *models.Node, newNode *models.Node) error { if newNode.Address.IP.String() != currentNode.Address.IP.String() { if network, err := GetParentNetwork(newNode.Network); err == nil { - if !IsAddressInCIDR(newNode.Address.IP.String(), network.AddressRange) { + if !IsAddressInCIDR(newNode.Address.IP, network.AddressRange) { return fmt.Errorf("invalid address provided; out of network range for node %s", newNode.ID) } } diff --git a/logic/util.go b/logic/util.go index 1d8d68eb..d2d74b25 100644 --- a/logic/util.go +++ b/logic/util.go @@ -5,7 +5,6 @@ import ( crand "crypto/rand" "encoding/base64" "encoding/json" - "fmt" "math/big" "math/rand" "net" @@ -40,26 +39,12 @@ func FileExists(f string) bool { } // IsAddressInCIDR - util to see if an address is in a cidr or not -func IsAddressInCIDR(address, cidr string) bool { +func IsAddressInCIDR(address net.IP, cidr string) bool { var _, currentCIDR, cidrErr = net.ParseCIDR(cidr) if cidrErr != nil { return false } - var addrParts = strings.Split(address, ".") - var addrPartLength = len(addrParts) - if addrPartLength != 4 { - return false - } else { - if addrParts[addrPartLength-1] == "0" || - addrParts[addrPartLength-1] == "255" { - return false - } - } - ip, _, err := net.ParseCIDR(fmt.Sprintf("%s/32", address)) - if err != nil { - return false - } - return currentCIDR.Contains(ip) + return currentCIDR.Contains(address) } // SetNetworkNodesLastModified - sets the network nodes last modified