water/syscalls_linux.go
yinheli deffbe3fa6 fix for mips compile
issue detail : https://github.com/golang/go/issues/21596
const is untyped int
2017-08-28 16:29:47 +08:00

118 lines
2.4 KiB
Go

// +build linux
package water
import (
"os"
"strings"
"syscall"
"unsafe"
)
const (
cIFF_TUN = 0x0001
cIFF_TAP = 0x0002
cIFF_NO_PI = 0x1000
cIFF_MULTI_QUEUE = 0x0100
)
type ifReq struct {
Name [0x10]byte
Flags uint16
pad [0x28 - 0x10 - 2]byte
}
func ioctl(fd uintptr, request uint, argp uintptr) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(request), argp)
if errno != 0 {
return os.NewSyscallError("ioctl", errno)
}
return nil
}
func newTAP(config Config) (ifce *Interface, err error) {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
var flags uint16
flags = cIFF_TUN | cIFF_NO_PI
if config.PlatformSpecificParams.MultiQueue {
flags |= cIFF_MULTI_QUEUE
}
name, err := createInterface(file.Fd(), config.Name, flags)
if err != nil {
return nil, err
}
if err = setDeviceOptions(file.Fd(), config); err != nil {
return nil, err
}
ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: name}
return
}
func newTUN(config Config) (ifce *Interface, err error) {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
var flags uint16
flags = cIFF_TUN | cIFF_NO_PI
if config.PlatformSpecificParams.MultiQueue {
flags |= cIFF_MULTI_QUEUE
}
name, err := createInterface(file.Fd(), config.Name, flags)
if err != nil {
return nil, err
}
if err = setDeviceOptions(file.Fd(), config); err != nil {
return nil, err
}
ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: name}
return
}
func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName string, err error) {
var req ifReq
req.Flags = flags
copy(req.Name[:], ifName)
err = ioctl(fd, syscall.TUNSETIFF, uintptr(unsafe.Pointer(&req)))
if err != nil {
return
}
createdIFName = strings.Trim(string(req.Name[:]), "\x00")
return
}
func setDeviceOptions(fd uintptr, config Config) (err error) {
// Device Permissions
if config.Permissions != nil {
// Set Owner
if err = ioctl(fd, syscall.TUNSETOWNER, uintptr(config.Permissions.Owner)); err != nil {
return
}
// Set Group
if err = ioctl(fd, syscall.TUNSETGROUP, uintptr(config.Permissions.Group)); err != nil {
return
}
}
// Set/Clear Persist Device Flag
value := 0
if config.Persist {
value = 1
}
return ioctl(fd, syscall.TUNSETPERSIST, uintptr(value))
}