Close UDP connection when listen port changes
This commit is contained in:
parent
2ed9dac3a5
commit
c3d9ae402d
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
@ -105,8 +104,6 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
||||||
key := parts[0]
|
key := parts[0]
|
||||||
value := parts[1]
|
value := parts[1]
|
||||||
|
|
||||||
fmt.Println(key, value)
|
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
|
|
||||||
/* interface configuration */
|
/* interface configuration */
|
||||||
|
@ -125,16 +122,21 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "listen_port":
|
case "listen_port":
|
||||||
var port int
|
port, err := strconv.ParseUint(value, 10, 16)
|
||||||
_, err := fmt.Sscanf(value, "%d", &port)
|
if err != nil {
|
||||||
if err != nil || port > (1<<16) || port < 0 {
|
|
||||||
logError.Println("Failed to set listen_port:", err)
|
logError.Println("Failed to set listen_port:", err)
|
||||||
return &IPCError{Code: ipcErrorInvalidValue}
|
return &IPCError{Code: ipcErrorInvalidValue}
|
||||||
}
|
}
|
||||||
device.net.mutex.Lock()
|
netc := &device.net
|
||||||
device.net.addr.Port = port
|
netc.mutex.Lock()
|
||||||
device.net.conn, err = net.ListenUDP("udp", device.net.addr)
|
if netc.addr.Port != int(port) {
|
||||||
device.net.mutex.Unlock()
|
if netc.conn != nil {
|
||||||
|
netc.conn.Close()
|
||||||
|
}
|
||||||
|
netc.addr.Port = int(port)
|
||||||
|
netc.conn, err = net.ListenUDP("udp", netc.addr)
|
||||||
|
}
|
||||||
|
netc.mutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logError.Println("Failed to create UDP listener:", err)
|
logError.Println("Failed to create UDP listener:", err)
|
||||||
return &IPCError{Code: ipcErrorInvalidValue}
|
return &IPCError{Code: ipcErrorInvalidValue}
|
||||||
|
@ -151,15 +153,10 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
|
||||||
return &IPCError{Code: ipcErrorInvalidValue}
|
return &IPCError{Code: ipcErrorInvalidValue}
|
||||||
}
|
}
|
||||||
device.mutex.RLock()
|
device.mutex.RLock()
|
||||||
found, ok := device.peers[pubKey]
|
peer, _ := device.peers[pubKey]
|
||||||
device.mutex.RUnlock()
|
device.mutex.RUnlock()
|
||||||
if ok {
|
|
||||||
peer = found
|
|
||||||
} else {
|
|
||||||
peer = device.NewPeer(pubKey)
|
|
||||||
}
|
|
||||||
if peer == nil {
|
if peer == nil {
|
||||||
panic(errors.New("bug: failed to find / create peer"))
|
peer = device.NewPeer(pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "replace_peers":
|
case "replace_peers":
|
||||||
|
|
|
@ -7,12 +7,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* TODO:
|
|
||||||
* This code can be improved by using fsnotify once:
|
|
||||||
* https://github.com/fsnotify/fsnotify/pull/205
|
|
||||||
* Is merged
|
|
||||||
*/
|
|
||||||
|
|
||||||
type UAPIListener struct {
|
type UAPIListener struct {
|
||||||
listener net.Listener // unix socket listener
|
listener net.Listener // unix socket listener
|
||||||
connNew chan net.Conn
|
connNew chan net.Conn
|
||||||
|
|
Loading…
Reference in a new issue