]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Moved remaining platform dependent UAPI code
authorMathias Hall-Andersen <mathias@hall-andersen.dk>
Thu, 20 Jul 2017 13:06:24 +0000 (15:06 +0200)
committerMathias Hall-Andersen <mathias@hall-andersen.dk>
Thu, 20 Jul 2017 13:06:24 +0000 (15:06 +0200)
src/config.go
src/main.go
src/tun_linux.go
src/uapi_linux.go

index ad4ac97489cd470af7dc2101810e83fcd4747633..509da580fd92f9f7673f4e96f078e34b792fa359 100644 (file)
@@ -9,28 +9,19 @@ import (
        "strconv"
        "strings"
        "sync/atomic"
-       "syscall"
        "time"
 )
 
-const (
-       ipcErrorIO           = syscall.EIO
-       ipcErrorNoPeer       = syscall.EPROTO
-       ipcErrorNoKeyValue   = syscall.EPROTO
-       ipcErrorInvalidKey   = syscall.EPROTO
-       ipcErrorInvalidValue = syscall.EPROTO
-)
-
 type IPCError struct {
-       Code syscall.Errno
+       Code int64
 }
 
 func (s *IPCError) Error() string {
        return fmt.Sprintf("IPC error: %d", s.Code)
 }
 
-func (s *IPCError) ErrorCode() uintptr {
-       return uintptr(s.Code)
+func (s *IPCError) ErrorCode() int64 {
+       return s.Code
 }
 
 func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
index 4bece166618144ab7bd1579cbb59ab110f8be2fe..0857999c098c8189230c9f5820f66182a18f71e5 100644 (file)
@@ -1,11 +1,17 @@
 package main
 
 import (
+       "fmt"
        "log"
        "os"
        "runtime"
 )
 
+func printUsage() {
+       fmt.Printf("usage:\n")
+       fmt.Printf("%s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
+}
+
 func main() {
 
        // parse arguments
@@ -13,6 +19,7 @@ func main() {
        var foreground bool
        var interfaceName string
        if len(os.Args) < 2 || len(os.Args) > 3 {
+               printUsage()
                return
        }
 
@@ -21,6 +28,7 @@ func main() {
        case "-f", "--foreground":
                foreground = true
                if len(os.Args) != 3 {
+                       printUsage()
                        return
                }
                interfaceName = os.Args[2]
@@ -28,6 +36,7 @@ func main() {
        default:
                foreground = false
                if len(os.Args) != 2 {
+                       printUsage()
                        return
                }
                interfaceName = os.Args[1]
index a200bd8ca296f34c7392423471296eb85aa50c80..261d14254d0cb8ec7f2f11d7a1a4780dbc53a379 100644 (file)
@@ -1,17 +1,17 @@
 package main
 
+/* Implementation of the TUN device interface for linux
+ */
+
 import (
        "encoding/binary"
        "errors"
+       "golang.org/x/sys/unix"
        "os"
        "strings"
-       "syscall"
        "unsafe"
 )
 
-/* Implementation of the TUN device interface for linux
- */
-
 const CloneDevicePath = "/dev/net/tun"
 
 type NativeTun struct {
@@ -27,9 +27,9 @@ func (tun *NativeTun) setMTU(n int) error {
 
        // open datagram socket
 
-       fd, err := syscall.Socket(
-               syscall.AF_INET,
-               syscall.SOCK_DGRAM,
+       fd, err := unix.Socket(
+               unix.AF_INET,
+               unix.SOCK_DGRAM,
                0,
        )
 
@@ -37,17 +37,17 @@ func (tun *NativeTun) setMTU(n int) error {
                return err
        }
 
-       defer syscall.Close(fd)
+       defer unix.Close(fd)
 
        // do ioctl call
 
        var ifr [64]byte
        copy(ifr[:], tun.name)
        binary.LittleEndian.PutUint32(ifr[16:20], uint32(n))
-       _, _, errno := syscall.Syscall(
-               syscall.SYS_IOCTL,
+       _, _, errno := unix.Syscall(
+               unix.SYS_IOCTL,
                uintptr(fd),
-               uintptr(syscall.SIOCSIFMTU),
+               uintptr(unix.SIOCSIFMTU),
                uintptr(unsafe.Pointer(&ifr[0])),
        )
 
@@ -62,9 +62,9 @@ func (tun *NativeTun) MTU() (int, error) {
 
        // open datagram socket
 
-       fd, err := syscall.Socket(
-               syscall.AF_INET,
-               syscall.SOCK_DGRAM,
+       fd, err := unix.Socket(
+               unix.AF_INET,
+               unix.SOCK_DGRAM,
                0,
        )
 
@@ -72,16 +72,16 @@ func (tun *NativeTun) MTU() (int, error) {
                return 0, err
        }
 
-       defer syscall.Close(fd)
+       defer unix.Close(fd)
 
        // do ioctl call
 
        var ifr [64]byte
        copy(ifr[:], tun.name)
-       _, _, errno := syscall.Syscall(
-               syscall.SYS_IOCTL,
+       _, _, errno := unix.Syscall(
+               unix.SYS_IOCTL,
                uintptr(fd),
-               uintptr(syscall.SIOCGIFMTU),
+               uintptr(unix.SIOCGIFMTU),
                uintptr(unsafe.Pointer(&ifr[0])),
        )
        if errno != 0 {
@@ -117,18 +117,18 @@ func CreateTUN(name string) (TUNDevice, error) {
        // create new device
 
        var ifr [64]byte
-       var flags uint16 = syscall.IFF_TUN | syscall.IFF_NO_PI
+       var flags uint16 = unix.IFF_TUN | unix.IFF_NO_PI
        nameBytes := []byte(name)
-       if len(nameBytes) >= syscall.IFNAMSIZ {
+       if len(nameBytes) >= unix.IFNAMSIZ {
                return nil, errors.New("Name size too long")
        }
        copy(ifr[:], nameBytes)
        binary.LittleEndian.PutUint16(ifr[16:], flags)
 
-       _, _, errno := syscall.Syscall(
-               syscall.SYS_IOCTL,
+       _, _, errno := unix.Syscall(
+               unix.SYS_IOCTL,
                uintptr(fd.Fd()),
-               uintptr(syscall.TUNSETIFF),
+               uintptr(unix.TUNSETIFF),
                uintptr(unsafe.Pointer(&ifr[0])),
        )
        if errno != 0 {
index ee6ee0be89a448a369474095128df1a09757d482..1055dca581f8ee8e121d4da38f8af5509029ce0e 100644 (file)
@@ -2,11 +2,20 @@ package main
 
 import (
        "fmt"
+       "golang.org/x/sys/unix"
        "net"
        "os"
        "time"
 )
 
+const (
+       ipcErrorIO           = int64(unix.EIO)
+       ipcErrorNoPeer       = int64(unix.EPROTO)
+       ipcErrorNoKeyValue   = int64(unix.EPROTO)
+       ipcErrorInvalidKey   = int64(unix.EPROTO)
+       ipcErrorInvalidValue = int64(unix.EPROTO)
+)
+
 /* TODO:
  * This code can be improved by using fsnotify once:
  * https://github.com/fsnotify/fsnotify/pull/205