}
}
+// 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.
}
func (device *Device) BindUpdate() error {
-
device.net.Lock()
defer device.net.Unlock()
+ return device.bindUpdateLocked()
+}
+
+func (device *Device) bindUpdateLocked() error {
// close existing sockets
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 {
// 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}
}