github.com/yggdrasil-network/yggdrasil-go@v0.5.6/contrib/mobile/mobile_ios.go (about)

     1  //go:build ios
     2  // +build ios
     3  
     4  package mobile
     5  
     6  /*
     7  #cgo CFLAGS: -x objective-c
     8  #cgo LDFLAGS: -framework Foundation
     9  #import <Foundation/Foundation.h>
    10  void Log(const char *text) {
    11    NSString *nss = [NSString stringWithUTF8String:text];
    12    NSLog(@"%@", nss);
    13  }
    14  */
    15  import "C"
    16  import (
    17  	"unsafe"
    18  
    19  	"github.com/yggdrasil-network/yggdrasil-go/src/tun"
    20  )
    21  
    22  type MobileLogger struct {
    23  }
    24  
    25  func (nsl MobileLogger) Write(p []byte) (n int, err error) {
    26  	p = append(p, 0)
    27  	cstr := (*C.char)(unsafe.Pointer(&p[0]))
    28  	C.Log(cstr)
    29  	return len(p), nil
    30  }
    31  
    32  func (m *Yggdrasil) TakeOverTUN(fd int32) error {
    33  	options := []tun.SetupOption{
    34  		tun.FileDescriptor(fd),
    35  		tun.InterfaceMTU(m.iprwc.MTU()),
    36  	}
    37  	var err error
    38  	m.tun, err = tun.New(m.iprwc, m.logger, options...)
    39  	return err
    40  }