TUN status hack was causing spam during shutdown

This commit is contained in:
Mathias Hall-Andersen 2018-02-11 23:26:54 +01:00
parent 04ded4c631
commit 6cba91999c
4 changed files with 39 additions and 36 deletions

13
peer.go
View File

@ -54,8 +54,8 @@ type Peer struct {
handshakeDeadline Timer // complete handshake timeout handshakeDeadline Timer // complete handshake timeout
handshakeTimeout Timer // current handshake message timeout handshakeTimeout Timer // current handshake message timeout
sendLastMinuteHandshake bool sendLastMinuteHandshake AtomicBool
needAnotherKeepalive bool needAnotherKeepalive AtomicBool
} }
queue struct { queue struct {
@ -170,15 +170,8 @@ func (peer *Peer) SendBuffer(buffer []byte) error {
/* Returns a short string identifier for logging /* Returns a short string identifier for logging
*/ */
func (peer *Peer) String() string { func (peer *Peer) String() string {
if peer.endpoint == nil {
return fmt.Sprintf(
"peer(unknown %s)",
base64.StdEncoding.EncodeToString(peer.handshake.remoteStatic[:]),
)
}
return fmt.Sprintf( return fmt.Sprintf(
"peer(%s %s)", "peer(%s)",
peer.endpoint.DstToString(),
base64.StdEncoding.EncodeToString(peer.handshake.remoteStatic[:]), base64.StdEncoding.EncodeToString(peer.handshake.remoteStatic[:]),
) )
} }

View File

@ -1,44 +1,52 @@
package main package main
import ( import (
"sync"
"time" "time"
) )
type Timer struct { type Timer struct {
pending AtomicBool mutex sync.Mutex
pending bool
timer *time.Timer timer *time.Timer
} }
/* Starts the timer if not already pending /* Starts the timer if not already pending
*/ */
func (t *Timer) Start(dur time.Duration) bool { func (t *Timer) Start(dur time.Duration) bool {
if !t.pending.Swap(true) { t.mutex.Lock()
defer t.mutex.Unlock()
started := !t.pending
if started {
t.timer.Reset(dur) t.timer.Reset(dur)
return true
} }
return false return started
} }
/* Stops the timer
*/
func (t *Timer) Stop() { func (t *Timer) Stop() {
if t.pending.Swap(true) { t.mutex.Lock()
t.timer.Stop() defer t.mutex.Unlock()
select {
case <-t.timer.C: t.timer.Stop()
default: select {
} case <-t.timer.C:
default:
} }
t.pending.Set(false) t.pending = false
} }
func (t *Timer) Pending() bool { func (t *Timer) Pending() bool {
return t.pending.Get() t.mutex.Lock()
defer t.mutex.Unlock()
return t.pending
} }
func (t *Timer) Reset(dur time.Duration) { func (t *Timer) Reset(dur time.Duration) {
t.pending.Set(false) t.mutex.Lock()
t.Start(dur) defer t.mutex.Unlock()
t.timer.Reset(dur)
} }
func (t *Timer) Wait() <-chan time.Time { func (t *Timer) Wait() <-chan time.Time {
@ -46,8 +54,8 @@ func (t *Timer) Wait() <-chan time.Time {
} }
func NewTimer() (t Timer) { func NewTimer() (t Timer) {
t.pending.Set(false) t.pending = false
t.timer = time.NewTimer(0) t.timer = time.NewTimer(time.Hour)
t.timer.Stop() t.timer.Stop()
select { select {
case <-t.timer.C: case <-t.timer.C:

View File

@ -36,7 +36,7 @@ func (peer *Peer) KeepKeyFreshSending() {
* NOTE: Not thread safe, but called by sequential receiver! * NOTE: Not thread safe, but called by sequential receiver!
*/ */
func (peer *Peer) KeepKeyFreshReceiving() { func (peer *Peer) KeepKeyFreshReceiving() {
if peer.timer.sendLastMinuteHandshake { if peer.timer.sendLastMinuteHandshake.Get() {
return return
} }
kp := peer.keyPairs.Current() kp := peer.keyPairs.Current()
@ -50,7 +50,7 @@ func (peer *Peer) KeepKeyFreshReceiving() {
send := nonce > RekeyAfterMessages || time.Now().Sub(kp.created) > RekeyAfterTimeReceiving send := nonce > RekeyAfterMessages || time.Now().Sub(kp.created) > RekeyAfterTimeReceiving
if send { if send {
// do a last minute attempt at initiating a new handshake // do a last minute attempt at initiating a new handshake
peer.timer.sendLastMinuteHandshake = true peer.timer.sendLastMinuteHandshake.Set(true)
peer.signal.handshakeBegin.Send() peer.signal.handshakeBegin.Send()
} }
} }
@ -87,7 +87,7 @@ func (peer *Peer) TimerDataSent() {
*/ */
func (peer *Peer) TimerDataReceived() { func (peer *Peer) TimerDataReceived() {
if !peer.timer.keepalivePassive.Start(KeepaliveTimeout) { if !peer.timer.keepalivePassive.Start(KeepaliveTimeout) {
peer.timer.needAnotherKeepalive = true peer.timer.needAnotherKeepalive.Set(true)
} }
} }
@ -238,8 +238,7 @@ func (peer *Peer) RoutineTimerHandler() {
peer.SendKeepAlive() peer.SendKeepAlive()
if peer.timer.needAnotherKeepalive { if peer.timer.needAnotherKeepalive.Swap(false) {
peer.timer.needAnotherKeepalive = false
peer.timer.keepalivePassive.Reset(KeepaliveTimeout) peer.timer.keepalivePassive.Reset(KeepaliveTimeout)
} }
@ -342,7 +341,7 @@ func (peer *Peer) RoutineTimerHandler() {
peer.timer.handshakeDeadline.Stop() peer.timer.handshakeDeadline.Stop()
peer.signal.handshakeBegin.Enable() peer.signal.handshakeBegin.Enable()
peer.timer.sendLastMinuteHandshake = false peer.timer.sendLastMinuteHandshake.Set(false)
} }
} }
} }

7
tun.go
View File

@ -26,6 +26,7 @@ type TUNDevice interface {
} }
func (device *Device) RoutineTUNEventReader() { func (device *Device) RoutineTUNEventReader() {
setUp := false
logInfo := device.log.Info logInfo := device.log.Info
logError := device.log.Error logError := device.log.Error
@ -45,13 +46,15 @@ func (device *Device) RoutineTUNEventReader() {
} }
} }
if event&TUNEventUp != 0 && !device.isUp.Get() { if event&TUNEventUp != 0 && !setUp {
logInfo.Println("Interface set up") logInfo.Println("Interface set up")
setUp = true
device.Up() device.Up()
} }
if event&TUNEventDown != 0 && device.isUp.Get() { if event&TUNEventDown != 0 && setUp {
logInfo.Println("Interface set down") logInfo.Println("Interface set down")
setUp = false
device.Down() device.Down()
} }
} }