From 2a6dd2ed926ddc44cd98d8c940aa01f830213ea0 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Fri, 21 Jul 2017 15:17:43 +0200 Subject: Fixed UAPI deadlock --- src/config.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/config.go') diff --git a/src/config.go b/src/config.go index d92e8d7..2d9ac50 100644 --- a/src/config.go +++ b/src/config.go @@ -106,17 +106,17 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { key := parts[0] value := parts[1] + fmt.Println(key, value) + switch key { /* interface configuration */ case "private_key": + var sk NoisePrivateKey if value == "" { - device.mutex.Lock() - device.privateKey = NoisePrivateKey{} - device.mutex.Unlock() + device.SetPrivateKey(sk) } else { - var sk NoisePrivateKey err := sk.FromHex(value) if err != nil { logError.Println("Failed to set private_key:", err) @@ -183,9 +183,7 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { switch key { case "remove": - peer.mutex.Lock() device.RemovePeer(peer.handshake.remoteStatic) - peer.mutex.Unlock() logDebug.Println("Removing", peer.String()) peer = nil @@ -236,7 +234,6 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { return &IPCError{Code: ipcErrorInvalidValue} } ones, _ := network.Mask.Size() - logError.Println(network, ones, network.IP) device.routingTable.Insert(network.IP, uint(ones), peer) default: -- cgit v1.2.3 From c3d9ae402d431b7697686dbaf021f879c8ccab36 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Sun, 23 Jul 2017 16:21:08 +0200 Subject: Close UDP connection when listen port changes --- src/config.go | 31 ++++++++++++++----------------- src/uapi_darwin.go | 6 ------ 2 files changed, 14 insertions(+), 23 deletions(-) (limited to 'src/config.go') diff --git a/src/config.go b/src/config.go index c889de0..9751a18 100644 --- a/src/config.go +++ b/src/config.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "errors" "fmt" "io" "net" @@ -105,8 +104,6 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { key := parts[0] value := parts[1] - fmt.Println(key, value) - switch key { /* interface configuration */ @@ -125,16 +122,21 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { } 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} @@ -151,15 +153,10 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError { 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": diff --git a/src/uapi_darwin.go b/src/uapi_darwin.go index ee6ee0b..9eee53c 100644 --- a/src/uapi_darwin.go +++ b/src/uapi_darwin.go @@ -7,12 +7,6 @@ import ( "time" ) -/* TODO: - * This code can be improved by using fsnotify once: - * https://github.com/fsnotify/fsnotify/pull/205 - * Is merged - */ - type UAPIListener struct { listener net.Listener // unix socket listener connNew chan net.Conn -- cgit v1.2.3