summaryrefslogtreecommitdiffhomepage
path: root/tunnel/tools/libwg-go/api-android.go
diff options
context:
space:
mode:
Diffstat (limited to 'tunnel/tools/libwg-go/api-android.go')
-rw-r--r--tunnel/tools/libwg-go/api-android.go60
1 files changed, 51 insertions, 9 deletions
diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go
index cadbab12..23276c28 100644
--- a/tunnel/tools/libwg-go/api-android.go
+++ b/tunnel/tools/libwg-go/api-android.go
@@ -64,7 +64,7 @@ type TunnelHandle struct {
eventHandler unsafe.Pointer
cancel context.CancelFunc
master *ipvlan.IPVLANMaster
- // tunEP *stack.LinkEndpoint
+ tunEP stack.LinkEndpoint
tunFd int
}
@@ -203,7 +203,7 @@ func createNetTUNWithStack(stack *stack.Stack, nicID tcpip.NICID, localAddresses
}
//export wgTurnOnDhcp
-func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Pointer) int32 {
+func wgTurnOnDhcp(interfaceName string, lladdr string, settings string, eventHandler unsafe.Pointer) int32 {
tag := cstring("WireGuard/GoBackend/" + interfaceName)
logger := &device.Logger{
Verbosef: AndroidLogger{level: C.ANDROID_LOG_DEBUG, tag: tag}.Printf,
@@ -212,18 +212,16 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
stack := globalStack
- // FIXME IP address
- ipStr := "fe80::101"
// FIXME name
name := "wg0"
var tundev tun.Device
var tnet *netstack.Net
+ llipaddr := net.ParseIP(lladdr)
tundev, tnet, master, err := createNetTUNWithStack(
stack,
1,
- []net.IP{net.ParseIP(ipStr)},
- // []net.IP{net.ParseIP("fe80::1")},
- []net.IP{net.ParseIP("fe80::1")},
+ []net.IP{llipaddr},
+ []net.IP{net.ParseIP("ff02::1:2")},
1420)
if err != nil {
log.Panic(err)
@@ -289,7 +287,7 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
go func(ctx context.Context) {
logger.Verbosef("Start dhcp client")
- src, err := net.ResolveUDPAddr("udp6", fmt.Sprintf("[%s%%1]:546", ipStr))
+ src, err := net.ResolveUDPAddr("udp6", fmt.Sprintf("[%s%%1]:546", lladdr))
if err != nil {
logger.Errorf("ResolveUDPAddr: %v", err)
return
@@ -305,10 +303,18 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
}
logger.Verbosef("Conn: %v %v %v", src, dst, conn)
- hwAddr := []byte(" 101")
+ // TODO check byte 11, 12 of lladdr is ff fe
+ hwAddr := []byte{0, 0, 0, 0, 0, 0}
+ hwAddr[0] = llipaddr[8] ^ 0x02
+ hwAddr[1] = llipaddr[9]
+ hwAddr[2] = llipaddr[10]
+ hwAddr[3] = llipaddr[13]
+ hwAddr[4] = llipaddr[14]
+ hwAddr[5] = llipaddr[15]
addrs, err := netstack.RunDhcp(ctx, conn, hwAddr)
if err != nil {
+ conn.Close()
logger.Errorf("DHCP: %v", err)
return
}
@@ -316,6 +322,9 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
msg := ""
logger.Verbosef("DHCP finished")
+
+ conn.Close()
+
for _, addr := range addrs {
logger.Verbosef("Address: %v", addr)
if msg == "" {
@@ -337,6 +346,12 @@ func wgTurnOnDhcp(interfaceName string, settings string, eventHandler unsafe.Poi
//export wgSetFd
func wgSetFd(tunnelHandle int32, tunFd int32) {
+ tag := cstring(fmt.Sprintf("WireGuard/GoBackend/%x", tunnelHandle))
+ logger := &device.Logger{
+ Verbosef: AndroidLogger{level: C.ANDROID_LOG_DEBUG, tag: tag}.Printf,
+ Errorf: AndroidLogger{level: C.ANDROID_LOG_ERROR, tag: tag}.Printf,
+ }
+
handle, ok := tunnelHandles[tunnelHandle]
if !ok {
return
@@ -349,19 +364,46 @@ func wgSetFd(tunnelHandle int32, tunFd int32) {
log.Fatal(err)
}
handle.tunFd = int(tunFd)
+ handle.tunEP = tunEP
+ // FIXME panic
+// 03-23 22:19:25.182 9818 0 E Go : panic: runtime error: invalid memory address or nil pointer dereference
+// 03-23 22:19:25.182 9818 0 E Go : [signal SIGSEGV: segmentation violation code=0x1 addr=0x34 pc=0x9f0f5d58]
+// 03-23 22:19:25.182 9818 0 E Go : goroutine 59 [running]:
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard/tun/netstack/ipvlan.(*dispatcher).DeliverNetworkPacket(0x4f1a2008, 0x0, 0x0, 0x0, 0x0, 0x86dd, 0x4f1fe000)
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard/tun/netstack@v0.0.0-00010101000000-000000000000/ipvlan/endpoint.go:104 +0x48
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard/tun/netstack.(*netTun).Write(0x4f1ac000, 0x4f1ec900, 0x77, 0x898, 0x10, 0x1, 0x0, 0x0)
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard/tun/netstack@v0.0.0-00010101000000-000000000000/tun.go:187 +0x1d4
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard/device.(*Peer).RoutineSequentialReceiver(0x4f1f2000)
+// 03-23 22:19:25.182 9818 0 E Go : golang.zx2c4.com/wireguard@v0.0.0-20210306154438-593658d9755b/device/receive.go:479 +0x3d8
+// 03-23 22:19:25.183 9818 0 E Go : created by golang.zx2c4.com/wireguard/device.(*Peer).Start
+// 03-23 22:19:25.183 9818 0 E Go : golang.zx2c4.com/wireguard@v0.0.0-20210306154438-593658d9755b/device/peer.go:187 +0x254
+// 03-23 22:19:25.183 9818 9907 F libc : Fatal signal 6 (SIGABRT), code -6 in tid 9907 (DefaultDispatch)
+
handle.master.SetIntEP(tunEP)
+ logger.Verbosef("wgSetFd: %v %v", tunFd, tunEP)
+ tunnelHandles[tunnelHandle] = handle
}
//export wgTurnOff
func wgTurnOff(tunnelHandle int32) unsafe.Pointer {
+ tag := cstring(fmt.Sprintf("WireGuard/GoBackend/%x", tunnelHandle))
+ logger := &device.Logger{
+ Verbosef: AndroidLogger{level: C.ANDROID_LOG_DEBUG, tag: tag}.Printf,
+ Errorf: AndroidLogger{level: C.ANDROID_LOG_ERROR, tag: tag}.Printf,
+ }
+
handle, ok := tunnelHandles[tunnelHandle]
if !ok {
return nil
}
if handle.master != nil {
+ logger.Verbosef("IPLAN master: %v %v", handle.tunFd, handle.tunEP)
globalStack.RemoveNIC(1)
handle.master.SetIntEP(nil)
+// handle.tunEP.Wait()
unix.Close(handle.tunFd)
+ } else {
+ logger.Verbosef("No IPLAN master")
}
eventHandler := handle.eventHandler