normalize cidr on network/egress gateway creation

This commit is contained in:
Matthew R. Kasun
2022-09-03 08:55:49 -04:00
parent edce72cf1e
commit 6b42cbe120
3 changed files with 39 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/c-robinson/iplib"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
@@ -169,3 +170,19 @@ func ShouldPublishPeerPorts(serverNode *models.Node) bool {
}
return false
}
// NormalCidr - returns the first address of CIDR
func NormalizeCIDR(address string) (string, error) {
ip, IPNet, err := net.ParseCIDR(address)
if err != nil {
return "", err
}
if ip.To4() == nil {
net6 := iplib.Net6FromStr(IPNet.String())
IPNet.IP = net6.FirstAddress()
} else {
net4 := iplib.Net4FromStr(IPNet.String())
IPNet.IP = net4.FirstAddress()
}
return IPNet.String(), nil
}