diff --git a/lang/core/net/cidr_to_ip.go b/lang/core/net/cidr_to_ip.go index e0ec3086..264071f1 100644 --- a/lang/core/net/cidr_to_ip.go +++ b/lang/core/net/cidr_to_ip.go @@ -73,6 +73,16 @@ func init() { T: types.NewType("func(a str) str"), F: CidrToMask, }) + simple.ModuleRegister(ModuleName, "cidr_to_network", &simple.Scaffold{ + I: &simple.Info{ + Pure: true, + Memo: true, + Fast: true, + Spec: true, + }, + T: types.NewType("func(a str) str"), + F: CidrToNetwork, + }) simple.ModuleRegister(ModuleName, "cidr_to_first", &simple.Scaffold{ I: &simple.Info{ Pure: true, @@ -135,6 +145,22 @@ func CidrToMask(ctx context.Context, input []types.Value) (types.Value, error) { }, nil } +// CidrToNetwork returns the network CIDR from a CIDR address. +func CidrToNetwork(ctx context.Context, input []types.Value) (types.Value, error) { + cidr := input[0].Str() + ip, ipnet, err := net.ParseCIDR(strings.TrimSpace(cidr)) + if err != nil { + return nil, err + } + + networkAddr := ip.Mask(ipnet.Mask) + ones, _ := ipnet.Mask.Size() + + return &types.StrValue{ + V: networkAddr.String() + "/" + strconv.Itoa(ones), + }, nil +} + // CidrToFirst returns the first usable IP from a CIDR address. func CidrToFirst(ctx context.Context, input []types.Value) (types.Value, error) { cidr := input[0].Str()