From 9acc3e0e73693ced0ce526b37b3366571d7135fe Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 4 Nov 2021 22:17:44 +0000 Subject: [PATCH] Add a set of ip prefix convert helpers --- utils.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/utils.go b/utils.go index 555227ec..ad0a72d7 100644 --- a/utils.go +++ b/utils.go @@ -163,3 +163,38 @@ func GrpcSocketDialer(ctx context.Context, addr string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "unix", addr) } + +func ipPrefixToString(prefixes []netaddr.IPPrefix) []string { + result := make([]string, len(prefixes)) + + for index, prefix := range prefixes { + result[index] = prefix.String() + } + + return result +} + +func stringToIpPrefix(prefixes []string) ([]netaddr.IPPrefix, error) { + result := make([]netaddr.IPPrefix, len(prefixes)) + + for index, prefixStr := range prefixes { + prefix, err := netaddr.ParseIPPrefix(prefixStr) + if err != nil { + return []netaddr.IPPrefix{}, err + } + + result[index] = prefix + } + + return result, nil +} + +func containsIpPrefix(prefixes []netaddr.IPPrefix, prefix netaddr.IPPrefix) bool { + for _, p := range prefixes { + if prefix == p { + return true + } + } + + return false +}