aboutsummaryrefslogtreecommitdiff
path: root/device/uapi.go
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josh@tailscale.com>2020-12-22 11:12:54 -0800
committerJosh Bleecher Snyder <josh@tailscale.com>2020-12-22 11:29:28 -0800
commit4794021bb8c697f107e6a2689bb9096eabb17888 (patch)
tree398f09211d65d0bd85ca6ece81072abd20868ebb /device/uapi.go
parent60b271ff95405de2b36ef99b64bd42516218e2e8 (diff)
downloadwireguard-go-js/sample-api.tar.gz
wireguard-go-js/sample-api.zip
device: add Device.ListenPort and Device.SetListenPortjs/sample-api
This is a sample commit for a possible way to make a Go API that lives alongside UAPI. The general idea is to add Device and Peer methods corresponding to UAPI directives, including a way to look up a peer from a device based on a public key, as in UAPI. The UAPI code then deals with parsing and generating textual input/output, and calls the Go methods to do the work. This commit also contains a bug fix for a racy access of device.net.port I will send an independently commit that fixes those directly in UAPI. This commit is NOT meant to be merged as-is. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Diffstat (limited to 'device/uapi.go')
-rw-r--r--device/uapi.go11
1 files changed, 3 insertions, 8 deletions
diff --git a/device/uapi.go b/device/uapi.go
index 2843a4c..3575c11 100644
--- a/device/uapi.go
+++ b/device/uapi.go
@@ -58,8 +58,8 @@ func (device *Device) IpcGetOperation(w io.StringWriter) error {
send("private_key=" + device.staticIdentity.privateKey.ToHex())
}
- if device.net.port != 0 {
- send(fmt.Sprintf("listen_port=%d", device.net.port))
+ if port := device.ListenPort(); port != 0 {
+ send(fmt.Sprintf("listen_port=%d", port))
}
if device.net.fwmark != 0 {
@@ -162,12 +162,7 @@ func (device *Device) IpcSetOperation(r io.Reader) error {
// update port and rebind
logDebug.Println("UAPI: Updating listen port")
-
- device.net.Lock()
- device.net.port = uint16(port)
- device.net.Unlock()
-
- if err := device.BindUpdate(); err != nil {
+ if err := device.SetListenPort(uint16(port)); err != nil {
logError.Println("Failed to set listen_port:", err)
return &IPCError{ipc.IpcErrorPortInUse}
}