import (
"bufio"
- "errors"
"fmt"
"io"
"net"
key := parts[0]
value := parts[1]
- fmt.Println(key, value)
-
switch key {
/* interface configuration */
}
case "listen_port":
- var port int
- _, err := fmt.Sscanf(value, "%d", &port)
- if err != nil || port > (1<<16) || port < 0 {
+ port, err := strconv.ParseUint(value, 10, 16)
+ if err != nil {
logError.Println("Failed to set listen_port:", err)
return &IPCError{Code: ipcErrorInvalidValue}
}
- device.net.mutex.Lock()
- device.net.addr.Port = port
- device.net.conn, err = net.ListenUDP("udp", device.net.addr)
- device.net.mutex.Unlock()
+ netc := &device.net
+ netc.mutex.Lock()
+ if netc.addr.Port != int(port) {
+ 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 {
logError.Println("Failed to create UDP listener:", err)
return &IPCError{Code: ipcErrorInvalidValue}
return &IPCError{Code: ipcErrorInvalidValue}
}
device.mutex.RLock()
- found, ok := device.peers[pubKey]
+ peer, _ := device.peers[pubKey]
device.mutex.RUnlock()
- if ok {
- peer = found
- } else {
- peer = device.NewPeer(pubKey)
- }
if peer == nil {
- panic(errors.New("bug: failed to find / create peer"))
+ peer = device.NewPeer(pubKey)
}
case "replace_peers":