diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index 746293d4..3bdbde25 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -260,9 +260,9 @@ func (tun *TunAdapter) ifaceReader() error { } dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask() // Do we have an active connection for this node ID? - tun.mutex.Lock() + tun.mutex.RLock() if conn, isIn := tun.conns[*dstNodeID]; isIn { - tun.mutex.Unlock() + tun.mutex.RUnlock() w, err := conn.Write(bs[:n]) if err != nil { tun.log.Errorln("TUN/TAP conn write error:", err) @@ -273,14 +273,20 @@ func (tun *TunAdapter) ifaceReader() error { continue } } else { - if conn, err := tun.dialer.DialByNodeIDandMask(dstNodeID, dstNodeIDMask); err == nil { - tun.conns[*dstNodeID] = &conn - tun.mutex.Unlock() - go tun.connReader(&conn) - } else { - tun.mutex.Unlock() - tun.log.Errorln("TUN/TAP dial error:", err) - } + tun.mutex.RUnlock() + func() { + tun.mutex.Lock() + defer tun.mutex.Unlock() + if conn, err := tun.dialer.DialByNodeIDandMask(dstNodeID, dstNodeIDMask); err == nil { + tun.log.Debugln("Opening new session connection") + tun.log.Debugln("Node:", dstNodeID) + tun.log.Debugln("Mask:", dstNodeIDMask) + tun.conns[*dstNodeID] = &conn + go tun.connReader(&conn) + } else { + tun.log.Errorln("TUN/TAP dial error:", err) + } + }() } /*if !r.cryptokey.isValidSource(srcAddr, addrlen) { diff --git a/src/yggdrasil/dialer.go b/src/yggdrasil/dialer.go index 49ce0a98..bd4b87e2 100644 --- a/src/yggdrasil/dialer.go +++ b/src/yggdrasil/dialer.go @@ -35,7 +35,7 @@ func (d *Dialer) Dial(network, address string) (Conn, error) { return Conn{}, err } copy(nodeID[:], dest) - for idx := 0; idx <= len; idx++ { + for idx := 0; idx < len; idx++ { nodeMask[idx/8] |= 0x80 >> byte(idx%8) } } else { @@ -65,8 +65,8 @@ func (d *Dialer) DialByNodeIDandMask(nodeID, nodeMask *crypto.NodeID) (Conn, err nodeMask: nodeMask, recv: make(chan *wire_trafficPacket, 32), } - conn.core.router.doAdmin(func() { + conn.core.router.admin <- func() { conn.startSearch() - }) + } return conn, nil }