From 05c180e7b28c4659b55e5bf1ebc4847e5b62230e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 12 Aug 2024 11:33:52 +0200 Subject: Util: Add a function to calculate the gateway --- util/util.go | 33 ++++++++++++++++++++++ util/util_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 util/util.go create mode 100644 util/util_test.go (limited to 'util') diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..c7816df --- /dev/null +++ b/util/util.go @@ -0,0 +1,33 @@ +// package util defines public utility functions to be used by applications +// these are outside of the client package as they can be used even if a client hasn't been created yet +package util + +import ( + "net" + + "github.com/eduvpn/eduvpn-common/i18nerr" +) + +// CalculateGateway takes a CIDR encoded subnet `cidr` and returns the gateway and an error +func CalculateGateway(cidr string) (string, error) { + _, ipn, err := net.ParseCIDR(cidr) + if err != nil { + return "", i18nerr.WrapInternalf(err, "failed to parse CIDR for calculating gateway: %v", cidr) + } + + ret := make(net.IP, len(ipn.IP)) + copy(ret, ipn.IP) + + for i := len(ret) - 1; i >= 0; i-- { + ret[i]++ + if ret[i] > 0 { + break + } + } + + if !ipn.Contains(ret) { + return "", i18nerr.Newf("IP network does not contain incremented IP: %v", ret) + } + + return ret.String(), nil +} diff --git a/util/util_test.go b/util/util_test.go new file mode 100644 index 0000000..0f4888d --- /dev/null +++ b/util/util_test.go @@ -0,0 +1,82 @@ +package util + +import ( + "testing" + + "github.com/eduvpn/eduvpn-common/internal/test" +) + +func TestCalculateGateway(t *testing.T) { + cases := []struct { + in string + want string + err string + }{ + // normal cases + { + in: "10.10.10.5/24", + want: "10.10.10.1", + err: "", + }, + { + in: "10.10.10.130/25", + want: "10.10.10.129", + err: "", + }, + { + in: "fd42::5/112", + want: "fd42::1", + err: "", + }, + { + in: "5502:df9::/64", + want: "5502:df9::1", + err: "", + }, + // unrealistic scenario but we have to handle these! + { + in: "5502:df9::0/128", + want: "", + err: "IP network does not contain incremented IP: 5502:df9::1", + }, + { + in: "5502:df9::ffff/128", + want: "", + err: "IP network does not contain incremented IP: 5502:df9::1:0", + }, + { + in: "10.0.0.0/32", + want: "", + err: "IP network does not contain incremented IP: 10.0.0.1", + }, + { + in: "10.0.0.255/32", + want: "", + err: "IP network does not contain incremented IP: 10.0.1.0", + }, + // parsing errors + { + in: "10.0.0.1", + want: "", + err: "An internal error occurred. The cause of the error is: invalid CIDR address: 10.0.0.1.", + }, + { + in: "bla", + want: "", + err: "An internal error occurred. The cause of the error is: invalid CIDR address: bla.", + }, + { + in: "5502:df9::ffff", + want: "", + err: "An internal error occurred. The cause of the error is: invalid CIDR address: 5502:df9::ffff.", + }, + } + + for _, c := range cases { + got, err := CalculateGateway(c.in) + test.AssertError(t, err, c.err) + if got != c.want { + t.Fatalf("got: %v not equal to want: %v", got, c.want) + } + } +} -- cgit v1.2.3