Skip to content

Commit 83d6787

Browse files
committed
added cidr check function
1 parent d7027e5 commit 83d6787

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cns/restserver/internalapi.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net"
1212
"net/http"
1313
"net/http/httptest"
14+
"net/netip"
1415
"reflect"
1516
"strconv"
1617
"strings"
@@ -584,3 +585,29 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
584585

585586
return returnCode
586587
}
588+
589+
// IsCIDRSuperset returns true if newCIDR is a superset of oldCIDR (i.e., all IPs in oldCIDR are contained in newCIDR).
590+
func validateCIDRSuperset(newCIDR, oldCIDR string) bool {
591+
// Parse newCIDR and oldCIDR into netip.Prefix
592+
newPrefix, err := netip.ParsePrefix(newCIDR)
593+
if err != nil {
594+
return false
595+
}
596+
597+
oldPrefix, err := netip.ParsePrefix(oldCIDR)
598+
if err != nil {
599+
return false
600+
}
601+
602+
// Condition 1: Check if the new prefix length is smaller (larger range) than the old prefix length
603+
if newPrefix.Bits() >= oldPrefix.Bits() {
604+
return false
605+
}
606+
607+
// Condition 2: Check for Overlap - this will also ensure containment
608+
if !newPrefix.Overlaps(oldPrefix) {
609+
return false
610+
}
611+
612+
return true
613+
}

0 commit comments

Comments
 (0)