lang: core: net: Add a helper to return the network ip
This commit is contained in:
@@ -73,6 +73,16 @@ func init() {
|
|||||||
T: types.NewType("func(a str) str"),
|
T: types.NewType("func(a str) str"),
|
||||||
F: CidrToMask,
|
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{
|
simple.ModuleRegister(ModuleName, "cidr_to_first", &simple.Scaffold{
|
||||||
I: &simple.Info{
|
I: &simple.Info{
|
||||||
Pure: true,
|
Pure: true,
|
||||||
@@ -135,6 +145,22 @@ func CidrToMask(ctx context.Context, input []types.Value) (types.Value, error) {
|
|||||||
}, nil
|
}, 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.
|
// CidrToFirst returns the first usable IP from a CIDR address.
|
||||||
func CidrToFirst(ctx context.Context, input []types.Value) (types.Value, error) {
|
func CidrToFirst(ctx context.Context, input []types.Value) (types.Value, error) {
|
||||||
cidr := input[0].Str()
|
cidr := input[0].Str()
|
||||||
|
|||||||
Reference in New Issue
Block a user