diff options
Diffstat (limited to '')
-rw-r--r-- | device/uapi.go (renamed from uapi.go) | 53 |
1 files changed, 27 insertions, 26 deletions
@@ -3,11 +3,12 @@ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. */ -package main +package device import ( "bufio" "fmt" + "golang.zx2c4.com/wireguard/ipc" "io" "net" "strconv" @@ -28,7 +29,7 @@ func (s *IPCError) ErrorCode() int64 { return s.int64 } -func ipcGetOperation(device *Device, socket *bufio.Writer) *IPCError { +func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError { device.log.Debug.Println("UAPI: Processing get operation") @@ -101,14 +102,14 @@ func ipcGetOperation(device *Device, socket *bufio.Writer) *IPCError { for _, line := range lines { _, err := socket.WriteString(line + "\n") if err != nil { - return &IPCError{ipcErrorIO} + return &IPCError{ipc.IpcErrorIO} } } return nil } -func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { +func (device *Device) IpcSetOperation(socket *bufio.Reader) *IPCError { scanner := bufio.NewScanner(socket) logError := device.log.Error logDebug := device.log.Debug @@ -128,7 +129,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { } parts := strings.Split(line, "=") if len(parts) != 2 { - return &IPCError{ipcErrorProtocol} + return &IPCError{ipc.IpcErrorProtocol} } key := parts[0] value := parts[1] @@ -143,7 +144,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { err := sk.FromHex(value) if err != nil { logError.Println("Failed to set private_key:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } logDebug.Println("UAPI: Updating private key") device.SetPrivateKey(sk) @@ -155,7 +156,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { port, err := strconv.ParseUint(value, 10, 16) if err != nil { logError.Println("Failed to parse listen_port:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } // update port and rebind @@ -168,7 +169,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err := device.BindUpdate(); err != nil { logError.Println("Failed to set listen_port:", err) - return &IPCError{ipcErrorPortInUse} + return &IPCError{ipc.IpcErrorPortInUse} } case "fwmark": @@ -185,14 +186,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err != nil { logError.Println("Invalid fwmark", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } logDebug.Println("UAPI: Updating fwmark") if err := device.BindSetMark(uint32(fwmark)); err != nil { logError.Println("Failed to update fwmark:", err) - return &IPCError{ipcErrorPortInUse} + return &IPCError{ipc.IpcErrorPortInUse} } case "public_key": @@ -203,14 +204,14 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { case "replace_peers": if value != "true" { logError.Println("Failed to set replace_peers, invalid value:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } logDebug.Println("UAPI: Removing all peers") device.RemoveAllPeers() default: logError.Println("Invalid UAPI device key:", key) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } } @@ -225,7 +226,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { err := publicKey.FromHex(value) if err != nil { logError.Println("Failed to get peer by public key:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } // ignore peer with public key of device @@ -244,7 +245,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { peer, err = device.NewPeer(publicKey) if err != nil { logError.Println("Failed to create new peer:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } logDebug.Println(peer, "- UAPI: Created") } @@ -255,7 +256,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if value != "true" { logError.Println("Failed to set remove, invalid value:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } if !dummy { logDebug.Println(peer, "- UAPI: Removing") @@ -276,7 +277,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err != nil { logError.Println("Failed to set preshared key:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } case "endpoint": @@ -298,7 +299,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if err != nil { logError.Println("Failed to set endpoint:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } case "persistent_keepalive_interval": @@ -310,7 +311,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { secs, err := strconv.ParseUint(value, 10, 16) if err != nil { logError.Println("Failed to set persistent keepalive interval:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } old := peer.persistentKeepaliveInterval @@ -321,7 +322,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if old == 0 && secs != 0 { if err != nil { logError.Println("Failed to get tun device status:", err) - return &IPCError{ipcErrorIO} + return &IPCError{ipc.IpcErrorIO} } if device.isUp.Get() && !dummy { peer.SendKeepalive() @@ -334,7 +335,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if value != "true" { logError.Println("Failed to replace allowedips, invalid value:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } if dummy { @@ -350,7 +351,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { _, network, err := net.ParseCIDR(value) if err != nil { logError.Println("Failed to set allowed ip:", err) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } if dummy { @@ -364,12 +365,12 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { if value != "1" { logError.Println("Invalid protocol version:", value) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } default: logError.Println("Invalid UAPI peer key:", key) - return &IPCError{ipcErrorInvalid} + return &IPCError{ipc.IpcErrorInvalid} } } } @@ -377,7 +378,7 @@ func ipcSetOperation(device *Device, socket *bufio.Reader) *IPCError { return nil } -func ipcHandle(device *Device, socket net.Conn) { +func (device *Device) IpcHandle(socket net.Conn) { // create buffered read/writer @@ -403,11 +404,11 @@ func ipcHandle(device *Device, socket net.Conn) { switch op { case "set=1\n": device.log.Debug.Println("UAPI: Set operation") - status = ipcSetOperation(device, buffered.Reader) + status = device.IpcSetOperation(buffered.Reader) case "get=1\n": device.log.Debug.Println("UAPI: Get operation") - status = ipcGetOperation(device, buffered.Writer) + status = device.IpcGetOperation(buffered.Writer) default: device.log.Error.Println("Invalid UAPI operation:", op) |