From 5010f3f66b1f2f82a830147aa460c3b0c3f4ed48 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 14 Oct 2019 21:53:10 -0400 Subject: [PATCH] device: add a HandshakeDone callback Every time a peer handshake completes, we call this function. That lets a GUI immediately notice when the connection has been established. Signed-off-by: Avery Pennarun --- device/device.go | 5 +++++ device/send.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/device/device.go b/device/device.go index 6d1c0e9b8..cc4b21413 100644 --- a/device/device.go +++ b/device/device.go @@ -24,6 +24,7 @@ type Device struct { isUp AtomicBool // device is (going) up isClosed AtomicBool // device is closed? (acting as guard) log *Logger + handshakeDone func() skipBindUpdate bool createBind func(uport uint16) (conn.Bind, uint16, error) createEndpoint func(key [32]byte, s string) (conn.Endpoint, error) @@ -275,6 +276,9 @@ type DeviceOptions struct { // SkipBindUpdate instructs Device to only call CreateBind once. SkipBindUpdate bool + + // HandshakeDone is called every time we complete a peer handshake. + HandshakeDone func() } // TODO move logger into DeviceOptions @@ -318,6 +322,7 @@ func NewDevice(tunDevice tun.Device, logger *Logger, varOpts ...DeviceOptions) * } } device.skipBindUpdate = opts.SkipBindUpdate + device.handshakeDone = opts.HandshakeDone device.tun.device = tunDevice mtu, err := device.tun.device.MTU() diff --git a/device/send.go b/device/send.go index 9e29d7778..607ca9b50 100644 --- a/device/send.go +++ b/device/send.go @@ -409,6 +409,9 @@ func (peer *Peer) RoutineNonce() { select { case <-peer.signals.newKeypairArrived: logDebug.Println(peer, "- Obtained awaited keypair") + if device.handshakeDone != nil { + device.handshakeDone() + } case <-peer.signals.flushNonceQueue: device.PutMessageBuffer(elem.buffer)