5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 16:31:41 +00:00
This commit is contained in:
Neil Alexander 2019-04-21 12:28:46 +01:00
parent 781cd7571f
commit 0b8f5b5dda
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 19 additions and 16 deletions

View File

@ -170,7 +170,7 @@ func (tun *TunAdapter) handler() error {
// Accept the incoming connection // Accept the incoming connection
conn, err := tun.listener.Accept() conn, err := tun.listener.Accept()
if err != nil { if err != nil {
tun.log.Errorln("TUN/TAP error accepting connection:", err) tun.log.Errorln("TUN/TAP connection accept error:", err)
return err return err
} }
go tun.connReader(conn) go tun.connReader(conn)
@ -195,7 +195,7 @@ func (tun *TunAdapter) connReader(conn *yggdrasil.Conn) error {
for { for {
n, err := conn.Read(b) n, err := conn.Read(b)
if err != nil { if err != nil {
tun.log.Errorln("TUN/TAP read error:", err) tun.log.Errorln("TUN/TAP conn read error:", err)
return err return err
} }
if n == 0 { if n == 0 {
@ -203,11 +203,11 @@ func (tun *TunAdapter) connReader(conn *yggdrasil.Conn) error {
} }
w, err := tun.iface.Write(b[:n]) w, err := tun.iface.Write(b[:n])
if err != nil { if err != nil {
tun.log.Errorln("TUN/TAP failed to write to interface:", err) tun.log.Errorln("TUN/TAP iface write error:", err)
continue continue
} }
if w != n { if w != n {
tun.log.Errorln("TUN/TAP wrote", w, "instead of", n, "which is bad") tun.log.Errorln("TUN/TAP iface write len didn't match conn read len")
continue continue
} }
} }
@ -231,7 +231,7 @@ func (tun *TunAdapter) ifaceReader() error {
if bs[0]&0xf0 == 0x60 { if bs[0]&0xf0 == 0x60 {
// Check if we have a fully-sized header // Check if we have a fully-sized header
if len(bs) < 40 { if len(bs) < 40 {
panic("Tried to send a packet shorter than an IPv6 header...") continue
} }
// IPv6 address // IPv6 address
addrlen = 16 addrlen = 16
@ -241,14 +241,14 @@ func (tun *TunAdapter) ifaceReader() error {
} else if bs[0]&0xf0 == 0x40 { } else if bs[0]&0xf0 == 0x40 {
// Check if we have a fully-sized header // Check if we have a fully-sized header
if len(bs) < 20 { if len(bs) < 20 {
panic("Tried to send a packet shorter than an IPv4 header...") continue
} }
// IPv4 address // IPv4 address
addrlen = 4 addrlen = 4
copy(srcAddr[:addrlen], bs[12:]) copy(srcAddr[:addrlen], bs[12:])
copy(dstAddr[:addrlen], bs[16:]) copy(dstAddr[:addrlen], bs[16:])
} else { } else {
// Unknown address length // Unknown address length or protocol
continue continue
} }
dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask() dstNodeID, dstNodeIDMask = dstAddr.GetNodeIDandMask()
@ -258,7 +258,7 @@ func (tun *TunAdapter) ifaceReader() error {
tun.mutex.Unlock() tun.mutex.Unlock()
w, err := conn.Write(bs) w, err := conn.Write(bs)
if err != nil { if err != nil {
tun.log.Println("Unable to write to remote:", err) tun.log.Println("TUN/TAP conn write error:", err)
continue continue
} }
if w != n { if w != n {
@ -271,7 +271,7 @@ func (tun *TunAdapter) ifaceReader() error {
go tun.connReader(&conn) go tun.connReader(&conn)
} else { } else {
tun.mutex.Unlock() tun.mutex.Unlock()
tun.log.Println("Error dialing:", err) tun.log.Println("TUN/TAP dial error:", err)
} }
} }

View File

@ -2,6 +2,7 @@ package yggdrasil
import ( import (
"errors" "errors"
"fmt"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -21,6 +22,10 @@ type Conn struct {
expired bool expired bool
} }
func (c *Conn) String() string {
return fmt.Sprintf("c=%p", c)
}
// This method should only be called from the router goroutine // This method should only be called from the router goroutine
func (c *Conn) startSearch() { func (c *Conn) startSearch() {
searchCompleted := func(sinfo *sessionInfo, err error) { searchCompleted := func(sinfo *sessionInfo, err error) {
@ -76,8 +81,8 @@ func (c *Conn) Read(b []byte) (int, error) {
if c.session == nil { if c.session == nil {
return 0, errors.New("searching for remote side") return 0, errors.New("searching for remote side")
} }
if !c.session.init.Load().(bool) { if init, ok := c.session.init.Load().(bool); !ok || (ok && !init) {
return 0, errors.New("waiting for remote side to accept") return 0, errors.New("waiting for remote side to accept " + c.String())
} }
select { select {
case p, ok := <-c.session.recv: case p, ok := <-c.session.recv:
@ -129,15 +134,12 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
return 0, errors.New("searching for remote side") return 0, errors.New("searching for remote side")
} }
defer util.PutBytes(b) defer util.PutBytes(b)
if !c.session.init.Load().(bool) { if init, ok := c.session.init.Load().(bool); !ok || (ok && !init) {
return 0, errors.New("waiting for remote side to accept") return 0, errors.New("waiting for remote side to accept " + c.String())
} }
// code isn't multithreaded so appending to this is safe
coords := c.session.coords coords := c.session.coords
// Prepare the payload
c.session.myNonceMutex.Lock() c.session.myNonceMutex.Lock()
payload, nonce := crypto.BoxSeal(&c.session.sharedSesKey, b, &c.session.myNonce) payload, nonce := crypto.BoxSeal(&c.session.sharedSesKey, b, &c.session.myNonce)
c.session.myNonceMutex.Unlock()
defer util.PutBytes(payload) defer util.PutBytes(payload)
p := wire_trafficPacket{ p := wire_trafficPacket{
Coords: coords, Coords: coords,
@ -146,6 +148,7 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
Payload: payload, Payload: payload,
} }
packet := p.encode() packet := p.encode()
c.session.myNonceMutex.Unlock()
atomic.AddUint64(&c.session.bytesSent, uint64(len(b))) atomic.AddUint64(&c.session.bytesSent, uint64(len(b)))
select { select {
case c.session.send <- packet: case c.session.send <- packet: