mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
Moved remaining platform dependent UAPI code
This commit is contained in:
parent
18714fc4a4
commit
47f8a3d89a
@ -9,28 +9,19 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ipcErrorIO = syscall.EIO
|
|
||||||
ipcErrorNoPeer = syscall.EPROTO
|
|
||||||
ipcErrorNoKeyValue = syscall.EPROTO
|
|
||||||
ipcErrorInvalidKey = syscall.EPROTO
|
|
||||||
ipcErrorInvalidValue = syscall.EPROTO
|
|
||||||
)
|
|
||||||
|
|
||||||
type IPCError struct {
|
type IPCError struct {
|
||||||
Code syscall.Errno
|
Code int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IPCError) Error() string {
|
func (s *IPCError) Error() string {
|
||||||
return fmt.Sprintf("IPC error: %d", s.Code)
|
return fmt.Sprintf("IPC error: %d", s.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IPCError) ErrorCode() uintptr {
|
func (s *IPCError) ErrorCode() int64 {
|
||||||
return uintptr(s.Code)
|
return s.Code
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func printUsage() {
|
||||||
|
fmt.Printf("usage:\n")
|
||||||
|
fmt.Printf("%s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// parse arguments
|
// parse arguments
|
||||||
@ -13,6 +19,7 @@ func main() {
|
|||||||
var foreground bool
|
var foreground bool
|
||||||
var interfaceName string
|
var interfaceName string
|
||||||
if len(os.Args) < 2 || len(os.Args) > 3 {
|
if len(os.Args) < 2 || len(os.Args) > 3 {
|
||||||
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,6 +28,7 @@ func main() {
|
|||||||
case "-f", "--foreground":
|
case "-f", "--foreground":
|
||||||
foreground = true
|
foreground = true
|
||||||
if len(os.Args) != 3 {
|
if len(os.Args) != 3 {
|
||||||
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
interfaceName = os.Args[2]
|
interfaceName = os.Args[2]
|
||||||
@ -28,6 +36,7 @@ func main() {
|
|||||||
default:
|
default:
|
||||||
foreground = false
|
foreground = false
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) != 2 {
|
||||||
|
printUsage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
interfaceName = os.Args[1]
|
interfaceName = os.Args[1]
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
/* Implementation of the TUN device interface for linux
|
||||||
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Implementation of the TUN device interface for linux
|
|
||||||
*/
|
|
||||||
|
|
||||||
const CloneDevicePath = "/dev/net/tun"
|
const CloneDevicePath = "/dev/net/tun"
|
||||||
|
|
||||||
type NativeTun struct {
|
type NativeTun struct {
|
||||||
@ -27,9 +27,9 @@ func (tun *NativeTun) setMTU(n int) error {
|
|||||||
|
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
fd, err := syscall.Socket(
|
fd, err := unix.Socket(
|
||||||
syscall.AF_INET,
|
unix.AF_INET,
|
||||||
syscall.SOCK_DGRAM,
|
unix.SOCK_DGRAM,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,17 +37,17 @@ func (tun *NativeTun) setMTU(n int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer syscall.Close(fd)
|
defer unix.Close(fd)
|
||||||
|
|
||||||
// do ioctl call
|
// do ioctl call
|
||||||
|
|
||||||
var ifr [64]byte
|
var ifr [64]byte
|
||||||
copy(ifr[:], tun.name)
|
copy(ifr[:], tun.name)
|
||||||
binary.LittleEndian.PutUint32(ifr[16:20], uint32(n))
|
binary.LittleEndian.PutUint32(ifr[16:20], uint32(n))
|
||||||
_, _, errno := syscall.Syscall(
|
_, _, errno := unix.Syscall(
|
||||||
syscall.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
uintptr(fd),
|
uintptr(fd),
|
||||||
uintptr(syscall.SIOCSIFMTU),
|
uintptr(unix.SIOCSIFMTU),
|
||||||
uintptr(unsafe.Pointer(&ifr[0])),
|
uintptr(unsafe.Pointer(&ifr[0])),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -62,9 +62,9 @@ func (tun *NativeTun) MTU() (int, error) {
|
|||||||
|
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
fd, err := syscall.Socket(
|
fd, err := unix.Socket(
|
||||||
syscall.AF_INET,
|
unix.AF_INET,
|
||||||
syscall.SOCK_DGRAM,
|
unix.SOCK_DGRAM,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -72,16 +72,16 @@ func (tun *NativeTun) MTU() (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer syscall.Close(fd)
|
defer unix.Close(fd)
|
||||||
|
|
||||||
// do ioctl call
|
// do ioctl call
|
||||||
|
|
||||||
var ifr [64]byte
|
var ifr [64]byte
|
||||||
copy(ifr[:], tun.name)
|
copy(ifr[:], tun.name)
|
||||||
_, _, errno := syscall.Syscall(
|
_, _, errno := unix.Syscall(
|
||||||
syscall.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
uintptr(fd),
|
uintptr(fd),
|
||||||
uintptr(syscall.SIOCGIFMTU),
|
uintptr(unix.SIOCGIFMTU),
|
||||||
uintptr(unsafe.Pointer(&ifr[0])),
|
uintptr(unsafe.Pointer(&ifr[0])),
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
@ -117,18 +117,18 @@ func CreateTUN(name string) (TUNDevice, error) {
|
|||||||
// create new device
|
// create new device
|
||||||
|
|
||||||
var ifr [64]byte
|
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)
|
nameBytes := []byte(name)
|
||||||
if len(nameBytes) >= syscall.IFNAMSIZ {
|
if len(nameBytes) >= unix.IFNAMSIZ {
|
||||||
return nil, errors.New("Name size too long")
|
return nil, errors.New("Name size too long")
|
||||||
}
|
}
|
||||||
copy(ifr[:], nameBytes)
|
copy(ifr[:], nameBytes)
|
||||||
binary.LittleEndian.PutUint16(ifr[16:], flags)
|
binary.LittleEndian.PutUint16(ifr[16:], flags)
|
||||||
|
|
||||||
_, _, errno := syscall.Syscall(
|
_, _, errno := unix.Syscall(
|
||||||
syscall.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
uintptr(fd.Fd()),
|
uintptr(fd.Fd()),
|
||||||
uintptr(syscall.TUNSETIFF),
|
uintptr(unix.TUNSETIFF),
|
||||||
uintptr(unsafe.Pointer(&ifr[0])),
|
uintptr(unsafe.Pointer(&ifr[0])),
|
||||||
)
|
)
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
|
@ -2,11 +2,20 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ipcErrorIO = int64(unix.EIO)
|
||||||
|
ipcErrorNoPeer = int64(unix.EPROTO)
|
||||||
|
ipcErrorNoKeyValue = int64(unix.EPROTO)
|
||||||
|
ipcErrorInvalidKey = int64(unix.EPROTO)
|
||||||
|
ipcErrorInvalidValue = int64(unix.EPROTO)
|
||||||
|
)
|
||||||
|
|
||||||
/* TODO:
|
/* TODO:
|
||||||
* This code can be improved by using fsnotify once:
|
* This code can be improved by using fsnotify once:
|
||||||
* https://github.com/fsnotify/fsnotify/pull/205
|
* https://github.com/fsnotify/fsnotify/pull/205
|
||||||
|
Loading…
Reference in New Issue
Block a user