aboutsummaryrefslogtreecommitdiff
path: root/device/device.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/device.go
parent60b271ff95405de2b36ef99b64bd42516218e2e8 (diff)
downloadwireguard-go-4794021bb8c697f107e6a2689bb9096eabb17888.tar.gz
wireguard-go-4794021bb8c697f107e6a2689bb9096eabb17888.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/device.go')
-rw-r--r--device/device.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/device/device.go b/device/device.go
index 5f128fc..f7885ce 100644
--- a/device/device.go
+++ b/device/device.go
@@ -90,6 +90,23 @@ type Device struct {
}
}
+// ListenPort returns the port that device is listening on.
+// It returns 0 if the device is not listening on any ports.
+func (device *Device) ListenPort() uint16 {
+ device.net.Lock()
+ defer device.net.Unlock()
+ return device.net.port
+}
+
+// SetListenPort sets the port that the device is listening on.
+// The new port is bound immediately.
+func (device *Device) SetListenPort(port uint16) error {
+ device.net.Lock()
+ defer device.net.Unlock()
+ device.net.port = port
+ return device.bindUpdateLocked()
+}
+
// An encryptionQueue is a channel of QueueOutboundElements awaiting encryption.
// An encryptionQueue is ref-counted using its wg field.
// An encryptionQueue created with newEncryptionQueue has one reference.
@@ -490,9 +507,12 @@ func (device *Device) BindSetMark(mark uint32) error {
}
func (device *Device) BindUpdate() error {
-
device.net.Lock()
defer device.net.Unlock()
+ return device.bindUpdateLocked()
+}
+
+func (device *Device) bindUpdateLocked() error {
// close existing sockets