1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00

Removed exponential backoff

This commit is contained in:
Mathias Hall-Andersen 2017-06-30 23:05:03 +02:00
parent ba3e486667
commit a4cc0a30fa

View File

@ -65,17 +65,16 @@ func (peer *Peer) KeepKeyFreshSending() {
* *
* Associated with this routine is the signal "handshakeBegin" * Associated with this routine is the signal "handshakeBegin"
* The routine will read from the "handshakeBegin" channel * The routine will read from the "handshakeBegin" channel
* at most every RekeyTimeout or with exponential backoff * at most every RekeyTimeout seconds
*
* Implements exponential backoff for retries
*/ */
func (peer *Peer) RoutineHandshakeInitiator() { func (peer *Peer) RoutineHandshakeInitiator() {
work := new(QueueOutboundElement)
device := peer.device device := peer.device
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
logger := device.log.Debug logger := device.log.Debug
timeout := time.NewTimer(time.Hour) timeout := time.NewTimer(time.Hour)
var work *QueueOutboundElement
logger.Println("Routine, handshake initator, started for peer", peer.id) logger.Println("Routine, handshake initator, started for peer", peer.id)
func() { func() {
@ -83,6 +82,8 @@ func (peer *Peer) RoutineHandshakeInitiator() {
var attempts uint var attempts uint
var deadline time.Time var deadline time.Time
// wait for signal
select { select {
case <-peer.signal.handshakeBegin: case <-peer.signal.handshakeBegin:
case <-peer.signal.stop: case <-peer.signal.stop:
@ -90,7 +91,7 @@ func (peer *Peer) RoutineHandshakeInitiator() {
} }
HandshakeLoop: HandshakeLoop:
for run := true; run; { for {
// clear completed signal // clear completed signal
select { select {
@ -100,32 +101,28 @@ func (peer *Peer) RoutineHandshakeInitiator() {
default: default:
} }
// queue handshake // create initiation
err := func() error { if work != nil {
work.mutex.Lock() work.mutex.Lock()
defer work.mutex.Unlock() work.packet = nil
work.mutex.Unlock()
// create initiation }
work = new(QueueOutboundElement)
msg, err := device.CreateMessageInitiation(peer) msg, err := device.CreateMessageInitiation(peer)
if err != nil {
return err
}
// marshal
writer := bytes.NewBuffer(buffer[:0])
binary.Write(writer, binary.LittleEndian, msg)
work.packet = writer.Bytes()
peer.mac.AddMacs(work.packet)
peer.InsertOutbound(work)
return nil
}()
if err != nil { if err != nil {
device.log.Error.Println("Failed to create initiation message:", err) device.log.Error.Println("Failed to create initiation message:", err)
break break
} }
// schedule for sending
writer := bytes.NewBuffer(buffer[:0])
binary.Write(writer, binary.LittleEndian, msg)
work.packet = writer.Bytes()
peer.mac.AddMacs(work.packet)
peer.InsertOutbound(work)
if attempts == 0 { if attempts == 0 {
deadline = time.Now().Add(MaxHandshakeAttemptTime) deadline = time.Now().Add(MaxHandshakeAttemptTime)
} }
@ -138,10 +135,9 @@ func (peer *Peer) RoutineHandshakeInitiator() {
default: default:
} }
} }
timeout.Reset((1 << attempts) * RekeyTimeout)
attempts += 1 attempts += 1
timeout.Reset(RekeyTimeout)
device.log.Debug.Println("Handshake initiation attempt", attempts, "queued for peer", peer.id) device.log.Debug.Println("Handshake initiation attempt", attempts, "queued for peer", peer.id)
time.Sleep(RekeyTimeout)
// wait for handshake or timeout // wait for handshake or timeout
@ -152,25 +148,13 @@ func (peer *Peer) RoutineHandshakeInitiator() {
case <-peer.signal.handshakeCompleted: case <-peer.signal.handshakeCompleted:
break HandshakeLoop break HandshakeLoop
default: case <-timeout.C:
select { if deadline.Before(time.Now().Add(RekeyTimeout)) {
peer.signal.flushNonceQueue <- struct{}{}
case <-peer.signal.stop: if !peer.timer.sendKeepalive.Stop() {
return <-peer.timer.sendKeepalive.C
case <-peer.signal.handshakeCompleted:
break HandshakeLoop
case <-timeout.C:
nextTimeout := (1 << attempts) * RekeyTimeout
if deadline.Before(time.Now().Add(nextTimeout)) {
// we do not have time for another attempt
peer.signal.flushNonceQueue <- struct{}{}
if !peer.timer.sendKeepalive.Stop() {
<-peer.timer.sendKeepalive.C
}
break HandshakeLoop
} }
break HandshakeLoop
} }
} }
} }