diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index 3010fae4..4b72b465 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -5,7 +5,6 @@ package tuntap import ( "encoding/hex" "errors" - "fmt" "net" "sync" "time" @@ -245,26 +244,22 @@ func (tun *TunAdapter) ifaceReader() error { dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask() // Do we have an active connection for this node ID? if conn, isIn := tun.conns[*dstNodeID]; isIn { - fmt.Println("We have a connection for", *dstNodeID) w, err := conn.Write(bs) if err != nil { - fmt.Println("Unable to write to remote:", err) + tun.log.Println("Unable to write to remote:", err) continue } if w != n { continue } } else { - fmt.Println("Opening connection for", *dstNodeID) + tun.log.Println("Opening connection for", *dstNodeID) tun.connsMutex.Lock() - maskstr := hex.EncodeToString(dstNodeID[:]) - masklen := dstNodeIDMask.PrefixLength() - cidr := fmt.Sprintf("%s/%d", maskstr, masklen) - if conn, err := tun.dialer.Dial("nodeid", cidr); err == nil { + if conn, err := tun.dialer.DialByNodeIDandMask(dstNodeID, dstNodeIDMask); err == nil { tun.conns[*dstNodeID] = conn go tun.connReader(&conn) } else { - fmt.Println("Error dialing:", err) + tun.log.Println("Error dialing:", err) } tun.connsMutex.Unlock() } diff --git a/src/yggdrasil/conn.go b/src/yggdrasil/conn.go index 6334eefa..9566cae3 100644 --- a/src/yggdrasil/conn.go +++ b/src/yggdrasil/conn.go @@ -27,6 +27,9 @@ func (c *Conn) startSearch() { searchCompleted := func(sinfo *sessionInfo, err error) { if err != nil { c.core.log.Debugln("DHT search failed:", err) + c.mutex.Lock() + c.expired = true + c.mutex.Unlock() return } if sinfo != nil { diff --git a/src/yggdrasil/dialer.go b/src/yggdrasil/dialer.go index 7042bd0e..fee355e4 100644 --- a/src/yggdrasil/dialer.go +++ b/src/yggdrasil/dialer.go @@ -3,7 +3,6 @@ package yggdrasil import ( "encoding/hex" "errors" - "fmt" "strconv" "strings" "sync" @@ -20,11 +19,8 @@ type Dialer struct { // and the second parameter should contain a hexadecimal representation of the // target node ID. func (d *Dialer) Dial(network, address string) (Conn, error) { - conn := Conn{ - mutex: &sync.RWMutex{}, - } - nodeID := crypto.NodeID{} - nodeMask := crypto.NodeID{} + var nodeID crypto.NodeID + var nodeMask crypto.NodeID // Process switch network { case "nodeid": @@ -42,8 +38,6 @@ func (d *Dialer) Dial(network, address string) (Conn, error) { for idx := 0; idx < len; idx++ { nodeMask[idx/8] |= 0x80 >> byte(idx%8) } - fmt.Println(nodeID) - fmt.Println(nodeMask) } else { dest, err := hex.DecodeString(tokens[0]) if err != nil { @@ -54,13 +48,22 @@ func (d *Dialer) Dial(network, address string) (Conn, error) { nodeMask[i] = 0xFF } } + return d.DialByNodeIDandMask(&nodeID, &nodeMask) default: // An unexpected address type was given, so give up return Conn{}, errors.New("unexpected address type") } +} + +// DialByNodeIDandMask opens a session to the given node based on raw +// NodeID parameters. +func (d *Dialer) DialByNodeIDandMask(nodeID, nodeMask *crypto.NodeID) (Conn, error) { + conn := Conn{ + mutex: &sync.RWMutex{}, + } conn.core = d.core - conn.nodeID = &nodeID - conn.nodeMask = &nodeMask + conn.nodeID = nodeID + conn.nodeMask = nodeMask conn.core.router.doAdmin(func() { conn.startSearch() }) diff --git a/src/yggdrasil/search.go b/src/yggdrasil/search.go index e81a9723..dcf0c81f 100644 --- a/src/yggdrasil/search.go +++ b/src/yggdrasil/search.go @@ -212,7 +212,9 @@ func (s *searches) checkDHTRes(info *searchInfo, res *dhtRes) bool { } } // FIXME (!) replay attacks could mess with coords? Give it a handle (tstamp)? + sinfo.coordsMutex.Lock() sinfo.coords = res.Coords + sinfo.coordsMutex.Unlock() sinfo.packet = info.packet s.core.sessions.ping(sinfo) info.callback(sinfo, nil)