summaryrefslogtreecommitdiff
path: root/client/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/client.go')
-rw-r--r--client/client.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/client/client.go b/client/client.go
index d668eb0..59b425d 100644
--- a/client/client.go
+++ b/client/client.go
@@ -7,11 +7,12 @@ import (
"context"
"errors"
"log/slog"
+ "net"
"os"
"sync"
"time"
- "codeberg.org/eduVPN/eduvpn-common/i18nerr"
+ "codeberg.org/eduVPN/eduvpn-common/i18n/err"
"codeberg.org/eduVPN/eduvpn-common/internal/api"
"codeberg.org/eduVPN/eduvpn-common/internal/config"
"codeberg.org/eduVPN/eduvpn-common/internal/discovery"
@@ -25,6 +26,31 @@ import (
"github.com/jwijenbergh/eduoauth-go"
)
+// CalculateGateway takes a CIDR encoded subnet `cidr` and returns the gateway and an error
+// TODO: move this somewhere else?
+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
+}
+
// Client is the main struct for the VPN client.
type Client struct {
// The name of the client