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

Merge pull request #526 from Arceliar/cleanup

Cleanup and possible bugfixes
This commit is contained in:
Arceliar 2019-09-01 13:45:17 -05:00 committed by GitHub
commit 730fd08954
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 8 deletions

View File

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

View File

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

View File

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