Browse Source

device: return generic error from Ipc{Get,Set}Operation.

This makes uapi.go's public API conform to Go style in terms
of error types.

Signed-off-by: David Anderson <danderson@tailscale.com>
David Anderson 1 month ago
parent
commit
825dfdbe24
2 changed files with 16 additions and 5 deletions
  1. 15
    4
      device/uapi.go
  2. 1
    1
      go.mod

+ 15
- 4
device/uapi.go View File

@@ -7,6 +7,7 @@ package device
7 7
 
8 8
 import (
9 9
 	"bufio"
10
+	"errors"
10 11
 	"fmt"
11 12
 	"io"
12 13
 	"net"
@@ -31,7 +32,7 @@ func (s IPCError) ErrorCode() int64 {
31 32
 	return s.int64
32 33
 }
33 34
 
34
-func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError {
35
+func (device *Device) IpcGetOperation(socket *bufio.Writer) error {
35 36
 	lines := make([]string, 0, 100)
36 37
 	send := func(line string) {
37 38
 		lines = append(lines, line)
@@ -106,7 +107,7 @@ func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError {
106 107
 	return nil
107 108
 }
108 109
 
109
-func (device *Device) IpcSetOperation(socket *bufio.Reader) *IPCError {
110
+func (device *Device) IpcSetOperation(socket *bufio.Reader) error {
110 111
 	scanner := bufio.NewScanner(socket)
111 112
 	logError := device.log.Error
112 113
 	logDebug := device.log.Debug
@@ -421,10 +422,20 @@ func (device *Device) IpcHandle(socket net.Conn) {
421 422
 
422 423
 	switch op {
423 424
 	case "set=1\n":
424
-		status = device.IpcSetOperation(buffered.Reader)
425
+		err = device.IpcSetOperation(buffered.Reader)
426
+		if !errors.As(err, &status) {
427
+			// should never happen
428
+			device.log.Error.Println("Invalid UAPI error:", err)
429
+			status = &IPCError{1}
430
+		}
425 431
 
426 432
 	case "get=1\n":
427
-		status = device.IpcGetOperation(buffered.Writer)
433
+		err = device.IpcGetOperation(buffered.Writer)
434
+		if !errors.As(err, &status) {
435
+			// should never happen
436
+			device.log.Error.Println("Invalid UAPI error:", err)
437
+			status = &IPCError{1}
438
+		}
428 439
 
429 440
 	default:
430 441
 		device.log.Error.Println("Invalid UAPI operation:", op)

+ 1
- 1
go.mod View File

@@ -1,6 +1,6 @@
1 1
 module golang.zx2c4.com/wireguard
2 2
 
3
-go 1.12
3
+go 1.13
4 4
 
5 5
 require (
6 6
 	golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc

Loading…
Cancel
Save