5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-29 22:31:37 +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 ( import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"net" "net"
"sync" "sync"
"time" "time"
@ -245,26 +244,22 @@ func (tun *TunAdapter) ifaceReader() error {
dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask() dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask()
// Do we have an active connection for this node ID? // Do we have an active connection for this node ID?
if conn, isIn := tun.conns[*dstNodeID]; isIn { if conn, isIn := tun.conns[*dstNodeID]; isIn {
fmt.Println("We have a connection for", *dstNodeID)
w, err := conn.Write(bs) w, err := conn.Write(bs)
if err != nil { if err != nil {
fmt.Println("Unable to write to remote:", err) tun.log.Println("Unable to write to remote:", err)
continue continue
} }
if w != n { if w != n {
continue continue
} }
} else { } else {
fmt.Println("Opening connection for", *dstNodeID) tun.log.Println("Opening connection for", *dstNodeID)
tun.connsMutex.Lock() tun.connsMutex.Lock()
maskstr := hex.EncodeToString(dstNodeID[:]) if conn, err := tun.dialer.DialByNodeIDandMask(dstNodeID, dstNodeIDMask); err == nil {
masklen := dstNodeIDMask.PrefixLength()
cidr := fmt.Sprintf("%s/%d", maskstr, masklen)
if conn, err := tun.dialer.Dial("nodeid", cidr); err == nil {
tun.conns[*dstNodeID] = conn tun.conns[*dstNodeID] = conn
go tun.connReader(&conn) go tun.connReader(&conn)
} else { } else {
fmt.Println("Error dialing:", err) tun.log.Println("Error dialing:", err)
} }
tun.connsMutex.Unlock() tun.connsMutex.Unlock()
} }

View File

@ -27,6 +27,9 @@ func (c *Conn) startSearch() {
searchCompleted := func(sinfo *sessionInfo, err error) { searchCompleted := func(sinfo *sessionInfo, err error) {
if err != nil { if err != nil {
c.core.log.Debugln("DHT search failed:", err) c.core.log.Debugln("DHT search failed:", err)
c.mutex.Lock()
c.expired = true
c.mutex.Unlock()
return return
} }
if sinfo != nil { if sinfo != nil {

View File

@ -3,7 +3,6 @@ package yggdrasil
import ( import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -20,11 +19,8 @@ type Dialer struct {
// and the second parameter should contain a hexadecimal representation of the // and the second parameter should contain a hexadecimal representation of the
// target node ID. // target node ID.
func (d *Dialer) Dial(network, address string) (Conn, error) { func (d *Dialer) Dial(network, address string) (Conn, error) {
conn := Conn{ var nodeID crypto.NodeID
mutex: &sync.RWMutex{}, var nodeMask crypto.NodeID
}
nodeID := crypto.NodeID{}
nodeMask := crypto.NodeID{}
// Process // Process
switch network { switch network {
case "nodeid": case "nodeid":
@ -42,8 +38,6 @@ func (d *Dialer) Dial(network, address string) (Conn, error) {
for idx := 0; idx < len; idx++ { for idx := 0; idx < len; idx++ {
nodeMask[idx/8] |= 0x80 >> byte(idx%8) nodeMask[idx/8] |= 0x80 >> byte(idx%8)
} }
fmt.Println(nodeID)
fmt.Println(nodeMask)
} else { } else {
dest, err := hex.DecodeString(tokens[0]) dest, err := hex.DecodeString(tokens[0])
if err != nil { if err != nil {
@ -54,13 +48,22 @@ func (d *Dialer) Dial(network, address string) (Conn, error) {
nodeMask[i] = 0xFF nodeMask[i] = 0xFF
} }
} }
return d.DialByNodeIDandMask(&nodeID, &nodeMask)
default: default:
// An unexpected address type was given, so give up // An unexpected address type was given, so give up
return Conn{}, errors.New("unexpected address type") 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.core = d.core
conn.nodeID = &nodeID conn.nodeID = nodeID
conn.nodeMask = &nodeMask conn.nodeMask = nodeMask
conn.core.router.doAdmin(func() { conn.core.router.doAdmin(func() {
conn.startSearch() 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)? // FIXME (!) replay attacks could mess with coords? Give it a handle (tstamp)?
sinfo.coordsMutex.Lock()
sinfo.coords = res.Coords sinfo.coords = res.Coords
sinfo.coordsMutex.Unlock()
sinfo.packet = info.packet sinfo.packet = info.packet
s.core.sessions.ping(sinfo) s.core.sessions.ping(sinfo)
info.callback(sinfo, nil) info.callback(sinfo, nil)