5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-30 00:51:36 +00:00

Remove stillAlive code from TUN/TAP conn as it is no longer required with the new deadlines

This commit is contained in:
Neil Alexander 2019-07-17 13:53:16 +01:00
parent 7d1c03d2ac
commit eec70bf2f2
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 10 additions and 36 deletions

View File

@ -64,16 +64,19 @@ func (s *tunConn) reader() error {
s.conn.SetReadDeadline(time.Now().Add(tunConnTimeout)) s.conn.SetReadDeadline(time.Now().Add(tunConnTimeout))
if n, err = s.conn.Read(b); err != nil { if n, err = s.conn.Read(b); err != nil {
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn read error:", err) s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn read error:", err)
if e, eok := err.(yggdrasil.ConnError); eok { if e, eok := err.(yggdrasil.ConnError); eok && !e.Temporary() {
s.tun.log.Debugln("Conn reader helper", s, "error:", e) s.tun.log.Debugln("Conn reader helper", s, "error:", e)
switch { switch {
case e.Temporary(): // The timeout probably means we've waited for the timeout period and
fallthrough // nothing has happened so close the connection
case e.Timeout(): case e.Timeout():
read <- false s.close()
continue continue
// The connection is already closed, so we anticipate that the main
// reader goroutine has already exited. Also stop in that case
case e.Closed(): case e.Closed():
fallthrough return
// Some other case that we don't know about - close the connection
default: default:
s.close() s.close()
return return
@ -86,7 +89,7 @@ func (s *tunConn) reader() error {
}() }()
for { for {
select { select {
case r, ok := <-read: case r := <-read:
if r && n > 0 { if r && n > 0 {
bs := append(util.GetBytes(), b[:n]...) bs := append(util.GetBytes(), b[:n]...)
select { select {
@ -95,9 +98,6 @@ func (s *tunConn) reader() error {
util.PutBytes(bs) util.PutBytes(bs)
} }
} }
if ok {
s.stillAlive() // TODO? Only stay alive if we read >0 bytes?
}
case <-s.stop: case <-s.stop:
return nil return nil
} }
@ -123,6 +123,7 @@ func (s *tunConn) writer() error {
return errors.New("send closed") return errors.New("send closed")
} }
// TODO write timeout and close // TODO write timeout and close
s.conn.SetWriteDeadline(time.Now().Add(tunConnTimeout))
if _, err := s.conn.Write(b); err != nil { if _, err := s.conn.Write(b); err != nil {
e, eok := err.(yggdrasil.ConnError) e, eok := err.(yggdrasil.ConnError)
if !eok { if !eok {
@ -141,32 +142,6 @@ func (s *tunConn) writer() error {
} }
} }
util.PutBytes(b) util.PutBytes(b)
s.stillAlive()
}
}
}
func (s *tunConn) stillAlive() {
select {
case s.alive <- struct{}{}:
default:
}
}
func (s *tunConn) checkForTimeouts() error {
timer := time.NewTimer(tunConnTimeout)
defer util.TimerStop(timer)
defer s.close()
for {
select {
case _, ok := <-s.alive:
if !ok {
return errors.New("connection closed")
}
util.TimerStop(timer)
timer.Reset(tunConnTimeout)
case <-timer.C:
return errors.New("timed out")
} }
} }
} }

View File

@ -259,7 +259,6 @@ func (tun *TunAdapter) wrap(conn *yggdrasil.Conn) (c *tunConn, err error) {
// Start the connection goroutines // Start the connection goroutines
go s.reader() go s.reader()
go s.writer() go s.writer()
go s.checkForTimeouts()
// Return // Return
return c, err return c, err
} }