Skip to content

Commit

Permalink
device: add a HandshakeDone callback
Browse files Browse the repository at this point in the history
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 <apenwarr@tailscale.com>
  • Loading branch information
apenwarr authored and crawshaw committed Mar 31, 2020
1 parent 41ad63a commit e2104a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -277,6 +278,13 @@ type DeviceOptions struct {
//
// TODO(crawshaw): remove this, it isn't useful externally.
SkipBindUpdate bool

// HandshakeDone is called every time we complete a peer handshake.
//
// TODO(crawshaw): This isn't quite right. Library users don't care
// about the handshake, per se, they want link status.
// Evolve this in that direction.
HandshakeDone func()
}

// TODO move logger into DeviceOptions
Expand Down Expand Up @@ -320,6 +328,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()
Expand Down
3 changes: 3 additions & 0 deletions device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e2104a9

Please sign in to comment.