Removed remaining signals from peer

1. Removed remaining signals from peer struct
2. Made needAnotherKeepalive local
3. Removed environment check from warning text (annoying when debugging)
This commit is contained in:
Mathias Hall-Andersen 2018-05-05 22:07:58 +02:00
parent 34891d92cd
commit abe2651ad5
7 changed files with 37 additions and 70 deletions

View File

@ -29,7 +29,7 @@ func (e *Event) Clear() {
} }
func (e *Event) Fire() { func (e *Event) Fire() {
if atomic.SwapInt32(&e.guard, 1) != 0 { if e == nil || atomic.SwapInt32(&e.guard, 1) != 0 {
return return
} }
if now := time.Now(); now.After(e.next) { if now := time.Now(); now.After(e.next) {

15
main.go
View File

@ -30,8 +30,6 @@ func printUsage() {
} }
func warning() { func warning() {
shouldQuit := false
fmt.Fprintln(os.Stderr, "WARNING WARNING WARNING WARNING WARNING WARNING WARNING") fmt.Fprintln(os.Stderr, "WARNING WARNING WARNING WARNING WARNING WARNING WARNING")
fmt.Fprintln(os.Stderr, "W G") fmt.Fprintln(os.Stderr, "W G")
fmt.Fprintln(os.Stderr, "W This is alpha software. It will very likely not G") fmt.Fprintln(os.Stderr, "W This is alpha software. It will very likely not G")
@ -39,8 +37,6 @@ func warning() {
fmt.Fprintln(os.Stderr, "W horribly wrong. You have been warned. Proceed G") fmt.Fprintln(os.Stderr, "W horribly wrong. You have been warned. Proceed G")
fmt.Fprintln(os.Stderr, "W at your own risk. G") fmt.Fprintln(os.Stderr, "W at your own risk. G")
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
shouldQuit = os.Getenv("WG_I_PREFER_BUGGY_USERSPACE_TO_POLISHED_KMOD") != "1"
fmt.Fprintln(os.Stderr, "W G") fmt.Fprintln(os.Stderr, "W G")
fmt.Fprintln(os.Stderr, "W Furthermore, you are running this software on a G") fmt.Fprintln(os.Stderr, "W Furthermore, you are running this software on a G")
fmt.Fprintln(os.Stderr, "W Linux kernel, which is probably unnecessary and G") fmt.Fprintln(os.Stderr, "W Linux kernel, which is probably unnecessary and G")
@ -50,20 +46,9 @@ func warning() {
fmt.Fprintln(os.Stderr, "W program. For more information on installing the G") fmt.Fprintln(os.Stderr, "W program. For more information on installing the G")
fmt.Fprintln(os.Stderr, "W kernel module, please visit: G") fmt.Fprintln(os.Stderr, "W kernel module, please visit: G")
fmt.Fprintln(os.Stderr, "W https://www.wireguard.com/install G") fmt.Fprintln(os.Stderr, "W https://www.wireguard.com/install G")
if shouldQuit {
fmt.Fprintln(os.Stderr, "W G")
fmt.Fprintln(os.Stderr, "W If you still want to use this program, against G")
fmt.Fprintln(os.Stderr, "W the sage advice here, please first export this G")
fmt.Fprintln(os.Stderr, "W environment variable: G")
fmt.Fprintln(os.Stderr, "W WG_I_PREFER_BUGGY_USERSPACE_TO_POLISHED_KMOD=1 G")
}
} }
fmt.Fprintln(os.Stderr, "W G") fmt.Fprintln(os.Stderr, "W G")
fmt.Fprintln(os.Stderr, "WARNING WARNING WARNING WARNING WARNING WARNING WARNING") fmt.Fprintln(os.Stderr, "WARNING WARNING WARNING WARNING WARNING WARNING WARNING")
if shouldQuit {
os.Exit(1)
}
} }
func main() { func main() {

54
peer.go
View File

@ -51,15 +51,11 @@ type Peer struct {
handshakeBegin *Event handshakeBegin *Event
ephemeralKeyCreated *Event ephemeralKeyCreated *Event
newKeyPair *Event newKeyPair *Event
} flushNonceQueue *Event
signal struct {
flushNonceQueue chan struct{} // size 0, empty queued packets
} }
timer struct { timer struct {
sendLastMinuteHandshake AtomicBool sendLastMinuteHandshake AtomicBool
needAnotherKeepalive AtomicBool
} }
queue struct { queue struct {
@ -72,7 +68,7 @@ type Peer struct {
mutex sync.Mutex // held when stopping / starting routines mutex sync.Mutex // held when stopping / starting routines
starting sync.WaitGroup // routines pending start starting sync.WaitGroup // routines pending start
stopping sync.WaitGroup // routines pending stop stopping sync.WaitGroup // routines pending stop
stop Signal // size 0, stop all go-routines in peer stop chan struct{} // size 0, stop all go-routines in peer
} }
mac CookieGenerator mac CookieGenerator
@ -111,18 +107,6 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
peer.device = device peer.device = device
peer.isRunning.Set(false) peer.isRunning.Set(false)
// events
peer.event.dataSent = newEvent(EventInterval)
peer.event.dataReceived = newEvent(EventInterval)
peer.event.anyAuthenticatedPacketReceived = newEvent(EventInterval)
peer.event.anyAuthenticatedPacketTraversal = newEvent(EventInterval)
peer.event.handshakeCompleted = newEvent(EventInterval)
peer.event.handshakePushDeadline = newEvent(EventInterval)
peer.event.handshakeBegin = newEvent(EventInterval)
peer.event.ephemeralKeyCreated = newEvent(EventInterval)
peer.event.newKeyPair = newEvent(EventInterval)
// map public key // map public key
_, ok := device.peers.keyMap[pk] _, ok := device.peers.keyMap[pk]
@ -143,12 +127,6 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
peer.endpoint = nil peer.endpoint = nil
// prepare signaling & routines
peer.routines.mutex.Lock()
peer.routines.stop = NewSignal()
peer.routines.mutex.Unlock()
// start peer // start peer
if peer.device.isUp.Get() { if peer.device.isUp.Get() {
@ -205,20 +183,31 @@ func (peer *Peer) Start() {
device := peer.device device := peer.device
device.log.Debug.Println(peer, ": Starting...") device.log.Debug.Println(peer, ": Starting...")
// sanity check : these should be 0 // reset routine state
peer.routines.starting.Wait() peer.routines.starting.Wait()
peer.routines.stopping.Wait() peer.routines.stopping.Wait()
peer.routines.stop = make(chan struct{})
// prepare queues and signals // prepare queues
peer.signal.flushNonceQueue = make(chan struct{})
peer.queue.nonce = make(chan *QueueOutboundElement, QueueOutboundSize) peer.queue.nonce = make(chan *QueueOutboundElement, QueueOutboundSize)
peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize) peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize)
peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize) peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize)
peer.routines.stop = NewSignal() // events
peer.event.dataSent = newEvent(EventInterval)
peer.event.dataReceived = newEvent(EventInterval)
peer.event.anyAuthenticatedPacketReceived = newEvent(EventInterval)
peer.event.anyAuthenticatedPacketTraversal = newEvent(EventInterval)
peer.event.handshakeCompleted = newEvent(EventInterval)
peer.event.handshakePushDeadline = newEvent(EventInterval)
peer.event.handshakeBegin = newEvent(EventInterval)
peer.event.ephemeralKeyCreated = newEvent(EventInterval)
peer.event.newKeyPair = newEvent(EventInterval)
peer.event.flushNonceQueue = newEvent(EventInterval)
peer.isRunning.Set(true) peer.isRunning.Set(true)
// wait for routines to start // wait for routines to start
@ -252,7 +241,7 @@ func (peer *Peer) Stop() {
// stop & wait for ongoing peer routines // stop & wait for ongoing peer routines
peer.routines.starting.Wait() peer.routines.starting.Wait()
peer.routines.stop.Broadcast() close(peer.routines.stop)
peer.routines.stopping.Wait() peer.routines.stopping.Wait()
// close queues // close queues
@ -261,11 +250,6 @@ func (peer *Peer) Stop() {
close(peer.queue.outbound) close(peer.queue.outbound)
close(peer.queue.inbound) close(peer.queue.inbound)
// close signals
close(peer.signal.flushNonceQueue)
peer.signal.flushNonceQueue = nil
// clear key pairs // clear key pairs
kp := &peer.keyPairs kp := &peer.keyPairs

View File

@ -561,7 +561,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
select { select {
case <-peer.routines.stop.Wait(): case <-peer.routines.stop:
return return
case elem, ok := <-peer.queue.inbound: case elem, ok := <-peer.queue.inbound:

11
send.go
View File

@ -209,8 +209,11 @@ func (peer *Peer) RoutineNonce() {
for { for {
NextPacket: NextPacket:
peer.event.flushNonceQueue.Clear()
select { select {
case <-peer.routines.stop.Wait(): case <-peer.routines.stop:
return return
case elem, ok := <-peer.queue.nonce: case elem, ok := <-peer.queue.nonce:
@ -239,9 +242,9 @@ func (peer *Peer) RoutineNonce() {
select { select {
case <-peer.event.newKeyPair.C: case <-peer.event.newKeyPair.C:
logDebug.Println(peer, ": Obtained awaited key-pair") logDebug.Println(peer, ": Obtained awaited key-pair")
case <-peer.signal.flushNonceQueue: case <-peer.event.flushNonceQueue.C:
goto NextPacket goto NextPacket
case <-peer.routines.stop.Wait(): case <-peer.routines.stop:
return return
} }
} }
@ -368,7 +371,7 @@ func (peer *Peer) RoutineSequentialSender() {
for { for {
select { select {
case <-peer.routines.stop.Wait(): case <-peer.routines.stop:
return return
case elem, ok := <-peer.queue.outbound: case elem, ok := <-peer.queue.outbound:

View File

@ -148,9 +148,9 @@ func (peer *Peer) RoutineTimerHandler() {
// reset all timers // reset all timers
enableHandshake := true enableHandshake := true
pendingHandshakeNew := false pendingHandshakeNew := false
pendingKeepalivePassive := false pendingKeepalivePassive := false
needAnotherKeepalive := false
timerKeepalivePassive := newTimer() timerKeepalivePassive := newTimer()
timerHandshakeDeadline := newTimer() timerHandshakeDeadline := newTimer()
@ -176,7 +176,7 @@ func (peer *Peer) RoutineTimerHandler() {
/* stopping */ /* stopping */
case <-peer.routines.stop.Wait(): case <-peer.routines.stop:
return return
/* events */ /* events */
@ -189,7 +189,7 @@ func (peer *Peer) RoutineTimerHandler() {
case <-peer.event.dataReceived.C: case <-peer.event.dataReceived.C:
if pendingKeepalivePassive { if pendingKeepalivePassive {
peer.timer.needAnotherKeepalive.Set(true) // TODO: make local needAnotherKeepalive = true
} else { } else {
timerKeepalivePassive.Reset(KeepaliveTimeout) timerKeepalivePassive.Reset(KeepaliveTimeout)
} }
@ -250,8 +250,6 @@ func (peer *Peer) RoutineTimerHandler() {
/* timers */ /* timers */
// keep-alive
case <-timerKeepalivePersistent.C: case <-timerKeepalivePersistent.C:
interval := peer.persistentKeepaliveInterval interval := peer.persistentKeepaliveInterval
@ -267,12 +265,11 @@ func (peer *Peer) RoutineTimerHandler() {
peer.SendKeepAlive() peer.SendKeepAlive()
if peer.timer.needAnotherKeepalive.Swap(false) { if needAnotherKeepalive {
timerKeepalivePassive.Reset(KeepaliveTimeout) timerKeepalivePassive.Reset(KeepaliveTimeout)
needAnotherKeepalive = false
} }
// clear key material timer
case <-timerZeroAllKeys.C: case <-timerZeroAllKeys.C:
logDebug.Println(peer, ": Clear all key-material (timer event)") logDebug.Println(peer, ": Clear all key-material (timer event)")
@ -305,8 +302,6 @@ func (peer *Peer) RoutineTimerHandler() {
hs.Clear() hs.Clear()
hs.mutex.Unlock() hs.mutex.Unlock()
// handshake timers
case <-timerHandshakeTimeout.C: case <-timerHandshakeTimeout.C:
// allow new handshake to be send // allow new handshake to be send
@ -349,14 +344,12 @@ func (peer *Peer) RoutineTimerHandler() {
logInfo.Println(peer, ": Handshake negotiation timed-out") logInfo.Println(peer, ": Handshake negotiation timed-out")
peer.flushNonceQueue() peer.flushNonceQueue()
signalSend(peer.signal.flushNonceQueue) peer.event.flushNonceQueue.Fire()
timerKeepalivePersistent.Stop()
// disable further handshakes // renable further handshakes
peer.event.handshakeBegin.Clear() peer.event.handshakeBegin.Clear()
enableHandshake = true enableHandshake = true
} }
} }
} }

View File

@ -288,6 +288,8 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
return &IPCError{Code: ipcErrorInvalid} return &IPCError{Code: ipcErrorInvalid}
} }
peer.event.handshakePushDeadline.Fire()
case "endpoint": case "endpoint":
// set endpoint destination // set endpoint destination