Skip to content

Commit 164effa

Browse files
committed
added cidr check function
1 parent 1d28dda commit 164effa

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"
@@ -640,3 +641,29 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
640641
func (service *HTTPRestService) SetVFForAccelnetNICs() error {
641642
return service.setVFForAccelnetNICs()
642643
}
644+
645+
// IsCIDRSuperset returns true if newCIDR is a superset of oldCIDR (i.e., all IPs in oldCIDR are contained in newCIDR).
646+
func validateCIDRSuperset(newCIDR, oldCIDR string) bool {
647+
// Parse newCIDR and oldCIDR into netip.Prefix
648+
newPrefix, err := netip.ParsePrefix(newCIDR)
649+
if err != nil {
650+
return false
651+
}
652+
653+
oldPrefix, err := netip.ParsePrefix(oldCIDR)
654+
if err != nil {
655+
return false
656+
}
657+
658+
// Condition 1: Check if the new prefix length is smaller (larger range) than the old prefix length
659+
if newPrefix.Bits() >= oldPrefix.Bits() {
660+
return false
661+
}
662+
663+
// Condition 2: Check for Overlap - this will also ensure containment
664+
if !newPrefix.Overlaps(oldPrefix) {
665+
return false
666+
}
667+
668+
return true
669+
}

0 commit comments

Comments
 (0)