summaryrefslogtreecommitdiffhomepage
path: root/tunnel/tools/libwg-go
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2021-03-15 20:22:58 +0000
committerMikael Magnusson <mikma@users.sourceforge.net>2023-02-07 22:43:22 +0100
commit421bae85375dbef7fb64759eae22134d3c42a1fe (patch)
treeeb94e3fd7d448a1f11fba03fa39f7f555cabd615 /tunnel/tools/libwg-go
parent3058775eb43b3c69a42939f52e493cca804f2108 (diff)
WIP: implement dhcp in tunnel
Diffstat (limited to 'tunnel/tools/libwg-go')
-rw-r--r--tunnel/tools/libwg-go/api-android.go51
-rw-r--r--tunnel/tools/libwg-go/go.mod2
-rw-r--r--tunnel/tools/libwg-go/jni.c27
3 files changed, 69 insertions, 11 deletions
diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go
index 2fb6e40f..cadbab12 100644
--- a/tunnel/tools/libwg-go/api-android.go
+++ b/tunnel/tools/libwg-go/api-android.go
@@ -32,6 +32,7 @@ import (
"golang.zx2c4.com/wireguard/tun/netstack/ipvlan"
"gvisor.dev/gvisor/pkg/tcpip"
+ "gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
@@ -63,12 +64,21 @@ type TunnelHandle struct {
eventHandler unsafe.Pointer
cancel context.CancelFunc
master *ipvlan.IPVLANMaster
+ // tunEP *stack.LinkEndpoint
+ tunFd int
}
+var globalStack *stack.Stack
var tunnelHandles map[int32]TunnelHandle
func init() {
tunnelHandles = make(map[int32]TunnelHandle)
+ opts := stack.Options{
+ NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
+ TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol},
+ HandleLocal: true,
+ }
+ globalStack = stack.New(opts)
signals := make(chan os.Signal)
signal.Notify(signals, unix.SIGUSR2)
go func() {
@@ -200,12 +210,7 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
Errorf: AndroidLogger{level: C.ANDROID_LOG_ERROR, tag: tag}.Printf,
}
- opts := stack.Options{
- NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
- TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol},
- HandleLocal: true,
- }
- stack := stack.New(opts)
+ stack := globalStack
// FIXME IP address
ipStr := "fe80::101"
@@ -308,14 +313,19 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
return
}
+ msg := ""
+
logger.Verbosef("DHCP finished")
for _, addr := range addrs {
logger.Verbosef("Address: %v", addr)
+ if msg == "" {
+ msg = "event=1\naddress=" + addr.String()
+ } else {
+ msg += "," + addr.String()
+ }
}
- // TODO move to wgSetTunFd
- // tunEP := newTun(stack, 2, "tun0")
- // master.SetIntEP(tunEP)
+ C.wgOnEvent(tunnelHandles[i].eventHandler, C.CString(msg))
}(ctx)
logger.Verbosef("Before wgOnEvent %v", eventHandler)
@@ -325,12 +335,35 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
return i
}
+//export wgSetFd
+func wgSetFd(tunnelHandle int32, tunFd int32) {
+ handle, ok := tunnelHandles[tunnelHandle]
+ if !ok {
+ return
+ }
+
+ // FIXME MTU
+ var mtu uint32 = 1280
+ tunEP, err := fdbased.New(&fdbased.Options{FDs: []int{int(tunFd)}, MTU: mtu})
+ if err != nil {
+ log.Fatal(err)
+ }
+ handle.tunFd = int(tunFd)
+ handle.master.SetIntEP(tunEP)
+}
+
//export wgTurnOff
func wgTurnOff(tunnelHandle int32) unsafe.Pointer {
handle, ok := tunnelHandles[tunnelHandle]
if !ok {
return nil
}
+ if handle.master != nil {
+ globalStack.RemoveNIC(1)
+ handle.master.SetIntEP(nil)
+ unix.Close(handle.tunFd)
+ }
+
eventHandler := handle.eventHandler
C.wgOnEvent(handle.eventHandler, C.CString("Called wgTurnOff"))
delete(tunnelHandles, tunnelHandle)
diff --git a/tunnel/tools/libwg-go/go.mod b/tunnel/tools/libwg-go/go.mod
index 6fdb520c..98bbf52a 100644
--- a/tunnel/tools/libwg-go/go.mod
+++ b/tunnel/tools/libwg-go/go.mod
@@ -13,4 +13,4 @@ require (
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
)
-replace golang.zx2c4.com/wireguard/tun/netstack => golang.m7n.se/wireguard/tun/netstack v0.0.0-20210315004706-824f10afd504
+replace golang.zx2c4.com/wireguard/tun/netstack => golang.m7n.se/wireguard/tun/netstack v0.0.0-20210315192121-e7c9dcf39d3f
diff --git a/tunnel/tools/libwg-go/jni.c b/tunnel/tools/libwg-go/jni.c
index de6a1e85..3897b2b7 100644
--- a/tunnel/tools/libwg-go/jni.c
+++ b/tunnel/tools/libwg-go/jni.c
@@ -14,6 +14,7 @@
struct go_string { const char *str; long n; };
extern int wgTurnOn(struct go_string ifname, int tun_fd, struct go_string settings, void *eventHandler);
extern int wgTurnOnDhcp(struct go_string ifname, struct go_string settings, void *eventHandler);
+extern void wgSetFd(int handle, int tun_fd);
extern void *wgTurnOff(int handle);
extern int wgGetSocketV4(int handle);
extern int wgGetSocketV6(int handle);
@@ -59,10 +60,29 @@ JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOn(JNI
const char *settings_str = (*env)->GetStringUTFChars(env, settings, 0);
size_t settings_len = (*env)->GetStringUTFLength(env, settings);
jobject event_handler = (*env)->NewGlobalRef(env, eventHandler);
+ int ret = wgTurnOn((struct go_string){
+ .str = ifname_str,
+ .n = ifname_len
+ }, tun_fd, (struct go_string){
+ .str = settings_str,
+ .n = settings_len
+ }, event_handler);
+ (*env)->ReleaseStringUTFChars(env, ifname, ifname_str);
+ (*env)->ReleaseStringUTFChars(env, settings, settings_str);
+ return ret;
+}
+
+JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOnDhcp(JNIEnv *env, jclass c, jstring ifname, jstring settings, jobject eventHandler)
+{
+ const char *ifname_str = (*env)->GetStringUTFChars(env, ifname, 0);
+ size_t ifname_len = (*env)->GetStringUTFLength(env, ifname);
+ const char *settings_str = (*env)->GetStringUTFChars(env, settings, 0);
+ size_t settings_len = (*env)->GetStringUTFLength(env, settings);
+ jobject event_handler = (*env)->NewGlobalRef(env, eventHandler);
int ret = wgTurnOnDhcp((struct go_string){
.str = ifname_str,
.n = ifname_len
- }, /* tun_fd,*/ (struct go_string){
+ }, (struct go_string){
.str = settings_str,
.n = settings_len
}, event_handler);
@@ -71,6 +91,11 @@ JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOn(JNI
return ret;
}
+JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgSetFd(JNIEnv *env, jclass c, jint handle, jint tun_fd)
+{
+ wgSetFd(handle, tun_fd);
+}
+
JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOff(JNIEnv *env, jclass c, jint handle)
{
jobject event_handler = wgTurnOff(handle);