summaryrefslogtreecommitdiffhomepage
path: root/peer.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 23:14:43 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 23:20:13 +0200
commitb56af1829d0368c893f8e9e14894f9563afb60ef (patch)
treebf8aaeefe196ff6672819c1c858c7b5d19352577 /peer.go
parent729773fdf3cbec5f75d5981fc1fe6f7f21b0e00c (diff)
More refactoring
Diffstat (limited to 'peer.go')
-rw-r--r--peer.go70
1 files changed, 35 insertions, 35 deletions
diff --git a/peer.go b/peer.go
index d574c71..1151341 100644
--- a/peer.go
+++ b/peer.go
@@ -19,7 +19,7 @@ const (
type Peer struct {
isRunning AtomicBool
- mutex sync.RWMutex
+ mutex sync.RWMutex // Mostly protects endpoint, but is generally taken whenever we modify peer
keypairs Keypairs
handshake Handshake
device *Device
@@ -42,7 +42,6 @@ type Peer struct {
handshakeAttempts uint
needAnotherKeepalive bool
sentLastMinuteHandshake bool
- lastSentHandshake time.Time
}
signals struct {
@@ -64,7 +63,7 @@ type Peer struct {
stop chan struct{} // size 0, stop all go routines in peer
}
- mac CookieGenerator
+ cookieGenerator CookieGenerator
}
func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
@@ -75,11 +74,8 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
// lock resources
- device.state.mutex.Lock()
- defer device.state.mutex.Unlock()
-
- device.noise.mutex.RLock()
- defer device.noise.mutex.RUnlock()
+ device.staticIdentity.mutex.RLock()
+ defer device.staticIdentity.mutex.RUnlock()
device.peers.mutex.Lock()
defer device.peers.mutex.Unlock()
@@ -96,7 +92,7 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
peer.mutex.Lock()
defer peer.mutex.Unlock()
- peer.mac.Init(pk)
+ peer.cookieGenerator.Init(pk)
peer.device = device
peer.isRunning.Set(false)
@@ -113,7 +109,7 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
handshake := &peer.handshake
handshake.mutex.Lock()
handshake.remoteStatic = pk
- handshake.precomputedStaticStatic = device.noise.privateKey.sharedSecret(pk)
+ handshake.precomputedStaticStatic = device.staticIdentity.privateKey.sharedSecret(pk)
handshake.mutex.Unlock()
// reset endpoint
@@ -191,6 +187,7 @@ func (peer *Peer) Start() {
peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize)
peer.timersInit()
+ peer.handshake.lastSentHandshake = time.Now().Add(-(RekeyTimeout + time.Second))
peer.signals.newKeypairArrived = make(chan struct{}, 1)
peer.signals.flushNonceQueue = make(chan struct{}, 1)
@@ -204,6 +201,32 @@ func (peer *Peer) Start() {
peer.isRunning.Set(true)
}
+func (peer *Peer) ZeroAndFlushAll() {
+ device := peer.device
+
+ // clear key pairs
+
+ keypairs := &peer.keypairs
+ keypairs.mutex.Lock()
+ device.DeleteKeypair(keypairs.previous)
+ device.DeleteKeypair(keypairs.current)
+ device.DeleteKeypair(keypairs.next)
+ keypairs.previous = nil
+ keypairs.current = nil
+ keypairs.next = nil
+ keypairs.mutex.Unlock()
+
+ // clear handshake state
+
+ handshake := &peer.handshake
+ handshake.mutex.Lock()
+ device.indexTable.Delete(handshake.localIndex)
+ handshake.Clear()
+ handshake.mutex.Unlock()
+
+ peer.FlushNonceQueue()
+}
+
func (peer *Peer) Stop() {
// prevent simultaneous start/stop operations
@@ -215,8 +238,7 @@ func (peer *Peer) Stop() {
return
}
- device := peer.device
- device.log.Debug.Println(peer, ": Stopping...")
+ peer.device.log.Debug.Println(peer, ": Stopping...")
peer.timersStop()
@@ -232,27 +254,5 @@ func (peer *Peer) Stop() {
close(peer.queue.outbound)
close(peer.queue.inbound)
- // clear key pairs
-
- kp := &peer.keypairs
- kp.mutex.Lock()
-
- device.DeleteKeypair(kp.previous)
- device.DeleteKeypair(kp.current)
- device.DeleteKeypair(kp.next)
-
- kp.previous = nil
- kp.current = nil
- kp.next = nil
- kp.mutex.Unlock()
-
- // clear handshake state
-
- hs := &peer.handshake
- hs.mutex.Lock()
- device.indexTable.Delete(hs.localIndex)
- hs.Clear()
- hs.mutex.Unlock()
-
- peer.FlushNonceQueue()
+ peer.ZeroAndFlushAll()
}