From d08c2eb2375b50044b18c61c8dc83e5aa3e0b91a Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 1 Sep 2019 13:04:10 -0500 Subject: [PATCH 1/3] stop exporting ReadNoCopy and WriteNoCopy, since we use the actor functions / callbacks and everything else should use Read and Write instead... --- src/yggdrasil/conn.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/yggdrasil/conn.go b/src/yggdrasil/conn.go index 0a4b84aa..352cf9d9 100644 --- a/src/yggdrasil/conn.go +++ b/src/yggdrasil/conn.go @@ -194,7 +194,7 @@ func (c *Conn) recvMsg(from phony.Actor, msg []byte) { } // Used internally by Read, the caller is responsible for util.PutBytes when they're done. -func (c *Conn) ReadNoCopy() ([]byte, error) { +func (c *Conn) readNoCopy() ([]byte, error) { var cancel util.Cancellation var doCancel bool phony.Block(c, func() { cancel, doCancel = c._getDeadlineCancellation(c.readDeadline) }) @@ -216,7 +216,7 @@ func (c *Conn) ReadNoCopy() ([]byte, error) { // Implements net.Conn.Read func (c *Conn) Read(b []byte) (int, error) { - bs, err := c.ReadNoCopy() + bs, err := c.readNoCopy() if err != nil { return 0, err } @@ -257,7 +257,7 @@ func (c *Conn) _write(msg FlowKeyMessage) error { } // WriteFrom should be called by a phony.Actor, and tells the Conn to send a message. -// This is used internaly by WriteNoCopy and Write. +// This is used internaly by Write. // If the callback is called with a non-nil value, then it is safe to reuse the argument FlowKeyMessage. func (c *Conn) WriteFrom(from phony.Actor, msg FlowKeyMessage, callback func(error)) { c.Act(from, func() { @@ -265,9 +265,9 @@ func (c *Conn) WriteFrom(from phony.Actor, msg FlowKeyMessage, callback func(err }) } -// WriteNoCopy is used internally by Write and makes use of WriteFrom under the hood. +// writeNoCopy is used internally by Write and makes use of WriteFrom under the hood. // The caller must not reuse the argument FlowKeyMessage when a nil error is returned. -func (c *Conn) WriteNoCopy(msg FlowKeyMessage) error { +func (c *Conn) writeNoCopy(msg FlowKeyMessage) error { var cancel util.Cancellation var doCancel bool phony.Block(c, func() { cancel, doCancel = c._getDeadlineCancellation(c.writeDeadline) }) @@ -292,7 +292,7 @@ func (c *Conn) WriteNoCopy(msg FlowKeyMessage) error { func (c *Conn) Write(b []byte) (int, error) { written := len(b) msg := FlowKeyMessage{Message: append(util.GetBytes(), b...)} - err := c.WriteNoCopy(msg) + err := c.writeNoCopy(msg) if err != nil { util.PutBytes(msg.Message) written = 0 From c53831696b5aee61823c0da62a96055d3f0e02fd Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 1 Sep 2019 13:06:25 -0500 Subject: [PATCH 2/3] make tun stop check that iface is not nil, in case it wasn't set for some reason (windows bugs) --- src/tuntap/tun.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index 8e1e5b0c..74d055ee 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -189,9 +189,11 @@ func (tun *TunAdapter) Stop() error { func (tun *TunAdapter) _stop() error { tun.isOpen = false - // TODO: we have nothing that cleanly stops all the various goroutines opened // by TUN/TAP, e.g. readers/writers, sessions - tun.iface.Close() + if tun.iface != nil { + // Just in case we failed to start up the iface for some reason, this can apparently happen on Windows + tun.iface.Close() + } return nil } From 8d2c31d39cbeeb968976f94013933c904e233147 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 1 Sep 2019 13:20:48 -0500 Subject: [PATCH 3/3] add some artifical delay to windows netsh commands, since it seems like maybe they don't take effect immediately, and this was leading to races when setting MTU --- src/tuntap/tun_windows.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/tuntap/tun_windows.go b/src/tuntap/tun_windows.go index a826c7ad..ea1515ff 100644 --- a/src/tuntap/tun_windows.go +++ b/src/tuntap/tun_windows.go @@ -5,6 +5,7 @@ import ( "fmt" "os/exec" "strings" + "time" water "github.com/yggdrasil-network/water" ) @@ -42,6 +43,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int tun.log.Traceln(string(output)) return err } + time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect // Bring the interface back up cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED") tun.log.Debugln("netsh command:", strings.Join(cmd.Args, " ")) @@ -51,6 +53,7 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int tun.log.Traceln(string(output)) return err } + time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect // Get a new iface iface, err = water.New(config) if err != nil { @@ -86,6 +89,7 @@ func (tun *TunAdapter) setupMTU(mtu int) error { tun.log.Traceln(string(output)) return err } + time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect return nil } @@ -106,5 +110,6 @@ func (tun *TunAdapter) setupAddress(addr string) error { tun.log.Traceln(string(output)) return err } + time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect return nil }