Skip to content

Commit

Permalink
wireguard: peer stop channel
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 4, 2024
1 parent b26d7d3 commit ebce657
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ type Peer struct {
inbound *autodrainingInboundQueue // sequential ordering of tun writing
}

trick bool
trick bool
stopCh chan int

cookieGenerator CookieGenerator
trieEntries list.List
Expand All @@ -79,7 +80,7 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {

// create peer
peer := new(Peer)

peer.stopCh = make(chan int, 1)
peer.trick = true
peer.cookieGenerator.Init(pk)
peer.device = device
Expand Down Expand Up @@ -267,6 +268,10 @@ func (peer *Peer) Stop() {
return
}

select {
case peer.stopCh <- 1:
default:
}
peer.device.log.Verbosef("%v - Stopping", peer)

peer.timersStop()
Expand Down
7 changes: 5 additions & 2 deletions device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ func (peer *Peer) sendRandomPackets() {
return
}

if i < numPackets-1 {
if i < numPackets-1 && peer.isRunning.Load() && !peer.device.isClosed() {
select {
case <-peer.stopCh:
// Wait for a random duration between 20 and 250 milliseconds
time.Sleep(time.Duration(randomInt(20, 250)) * time.Millisecond)
case <-time.After(time.Duration(randomInt(20, 250)) * time.Millisecond):
}
}
}
}
Expand Down

0 comments on commit ebce657

Please sign in to comment.