mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
Try to convert TUN/TAP to use new yggdrasil.Conn, search masks are still broken
This commit is contained in:
parent
319366513c
commit
d01662c1fb
14 changed files with 502 additions and 366 deletions
70
src/yggdrasil/dialer.go
Normal file
70
src/yggdrasil/dialer.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package yggdrasil
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
)
|
||||
|
||||
// Dialer represents an Yggdrasil connection dialer.
|
||||
type Dialer struct {
|
||||
core *Core
|
||||
}
|
||||
|
||||
// Dial opens a session to the given node. The first paramter should be "nodeid"
|
||||
// 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{}
|
||||
// Process
|
||||
switch network {
|
||||
case "nodeid":
|
||||
// A node ID was provided - we don't need to do anything special with it
|
||||
if tokens := strings.Split(address, "/"); len(tokens) == 2 {
|
||||
len, err := strconv.Atoi(tokens[1])
|
||||
if err != nil {
|
||||
return Conn{}, err
|
||||
}
|
||||
dest, err := hex.DecodeString(tokens[0])
|
||||
if err != nil {
|
||||
return Conn{}, err
|
||||
}
|
||||
copy(nodeID[:], dest)
|
||||
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 {
|
||||
return Conn{}, err
|
||||
}
|
||||
copy(nodeID[:], dest)
|
||||
for i := range nodeMask {
|
||||
nodeMask[i] = 0xFF
|
||||
}
|
||||
}
|
||||
default:
|
||||
// An unexpected address type was given, so give up
|
||||
return Conn{}, errors.New("unexpected address type")
|
||||
}
|
||||
conn.core = d.core
|
||||
conn.nodeID = &nodeID
|
||||
conn.nodeMask = &nodeMask
|
||||
conn.core.router.doAdmin(func() {
|
||||
conn.startSearch()
|
||||
})
|
||||
conn.mutex.Lock()
|
||||
defer conn.mutex.Unlock()
|
||||
return conn, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue