mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-09 23:20:26 +03:00
Move to unix.Syscall etc and tidy up
This commit is contained in:
parent
5d2dd061f0
commit
d7be706413
@ -2,12 +2,11 @@ package yggdrasil
|
|||||||
|
|
||||||
// The darwin platform specific tun parts
|
// The darwin platform specific tun parts
|
||||||
|
|
||||||
import "syscall"
|
|
||||||
import "unsafe"
|
import "unsafe"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "encoding/binary"
|
import "encoding/binary"
|
||||||
// import "fmt"
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
import water "github.com/songgao/water"
|
import water "github.com/songgao/water"
|
||||||
|
|
||||||
@ -22,8 +21,6 @@ func (tun *tunDevice) setup(ifname string, addr string, mtu int) error {
|
|||||||
return tun.setupAddress(addr)
|
return tun.setupAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
const AF_INET6 = 30
|
|
||||||
const IN6_IFF_NODAD = 0x0020
|
|
||||||
const SIOCAIFADDR_IN6 = 2155899162
|
const SIOCAIFADDR_IN6 = 2155899162
|
||||||
|
|
||||||
type in6_addrlifetime struct {
|
type in6_addrlifetime struct {
|
||||||
@ -51,11 +48,16 @@ type in6_aliasreq struct {
|
|||||||
ifra_lifetime in6_addrlifetime
|
ifra_lifetime in6_addrlifetime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ifreq struct {
|
||||||
|
ifr_name [16]byte
|
||||||
|
ifru_mtu uint32
|
||||||
|
}
|
||||||
|
|
||||||
func (tun *tunDevice) setupAddress(addr string) error {
|
func (tun *tunDevice) setupAddress(addr string) error {
|
||||||
var fd int
|
var fd int
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if fd, err = syscall.Socket(syscall.AF_INET6, syscall.SOCK_DGRAM, 0); err != nil {
|
if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil {
|
||||||
tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
tun.core.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -68,7 +70,7 @@ func (tun *tunDevice) setupAddress(addr string) error {
|
|||||||
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
|
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
|
||||||
|
|
||||||
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
|
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
|
||||||
ar.ifra_addr.sin6_family = AF_INET6
|
ar.ifra_addr.sin6_family = unix.AF_INET6
|
||||||
parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
|
parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
addr, _ := strconv.ParseUint(parts[i], 16, 16)
|
addr, _ := strconv.ParseUint(parts[i], 16, 16)
|
||||||
@ -79,28 +81,25 @@ func (tun *tunDevice) setupAddress(addr string) error {
|
|||||||
ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF
|
ar.ifra_lifetime.ia6t_vltime = 0xFFFFFFFF
|
||||||
ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF
|
ar.ifra_lifetime.ia6t_pltime = 0xFFFFFFFF
|
||||||
|
|
||||||
|
var ir ifreq
|
||||||
|
copy(ir.ifr_name[:], tun.iface.Name())
|
||||||
|
ir.ifru_mtu = 1280
|
||||||
|
|
||||||
tun.core.log.Printf("Interface name: %s", ar.ifra_name)
|
tun.core.log.Printf("Interface name: %s", ar.ifra_name)
|
||||||
tun.core.log.Printf("Interface IPv6: %s", addr)
|
tun.core.log.Printf("Interface IPv6: %s", addr)
|
||||||
|
tun.core.log.Printf("Interface MTU: %d", ir.ifru_mtu)
|
||||||
|
|
||||||
/*
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
||||||
var bytes *[unsafe.Sizeof(ar)]byte = (*[unsafe.Sizeof(ar)]byte)(unsafe.Pointer(&ar));
|
|
||||||
var out string;
|
|
||||||
for i := 0; i < int(unsafe.Sizeof(ar)); i ++ {
|
|
||||||
out += fmt.Sprintf("0x%02x", (*bytes)[i]);
|
|
||||||
if i % 8 == 7 {
|
|
||||||
out += "\n"
|
|
||||||
} else {
|
|
||||||
out += " "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tun.core.log.Printf("in6_aliasreq:\n%s", out);
|
|
||||||
*/
|
|
||||||
|
|
||||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
|
||||||
err = errno
|
err = errno
|
||||||
tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno)
|
tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
|
||||||
|
err = errno
|
||||||
|
tun.core.log.Printf("Error in SIOCSIFMTU: %v", errno)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user