5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 07:11:40 +00:00

Some tweaks

This commit is contained in:
Neil Alexander 2019-04-20 20:22:58 +01:00
parent d01662c1fb
commit 62621f2960
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
4 changed files with 22 additions and 19 deletions

View File

@ -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()
}

View File

@ -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 {

View File

@ -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()
})

View File

@ -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)