device.isClosed.Set(false)
device.log = logger
+
device.tun.device = tun
+ mtu, err := device.tun.device.MTU()
+ if err != nil {
+ logger.Error.Println("Trouble determining MTU, assuming 1420:", err)
+ mtu = 1420
+ }
+ device.tun.mtu = int32(mtu)
+
device.peers.keyMap = make(map[NoisePublicKey]*Peer)
// initialize anti-DoS / anti-scanning features
Read([]byte, int) (int, error) // read a packet from the device (without any additional headers)
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
MTU() (int, error) // returns the MTU of the device
- Name() string // returns the current name
+ Name() (string, error) // fetches and returns the current name
Events() chan TUNEvent // returns a constant channel of events related to the device
Close() error // stops the device and closes the event channel
}
"golang.org/x/sys/unix"
"net"
"os"
+ "strconv"
"strings"
"time"
"unsafe"
return inter.Flags&net.FlagUp != 0, err
}
-func (tun *NativeTun) Name() string {
- return tun.name
-}
-
func getDummySock() (int, error) {
return unix.Socket(
unix.AF_INET,
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
- return 0, errors.New("Failed to get MTU of TUN device")
+ return 0, errors.New("Failed to get MTU of TUN device: " + strconv.FormatInt(int64(errno), 10))
}
// convert result to signed 32-bit int
return int(val), nil
}
+func (tun *NativeTun) Name() (string, error) {
+
+ var ifr [ifReqSize]byte
+ _, _, errno := unix.Syscall(
+ unix.SYS_IOCTL,
+ tun.fd.Fd(),
+ uintptr(unix.TUNGETIFF),
+ uintptr(unsafe.Pointer(&ifr[0])),
+ )
+ if errno != 0 {
+ return "", errors.New("Failed to get name of TUN device: " + strconv.FormatInt(int64(errno), 10))
+ }
+ tun.name = string(ifr[:])
+ return tun.name, nil
+}
+
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
if tun.nopi {
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
- uintptr(fd.Fd()),
+ fd.Fd(),
uintptr(unix.TUNSETIFF),
uintptr(unsafe.Pointer(&ifr[0])),
)