diff options
Diffstat (limited to 'src/device.go')
-rw-r--r-- | src/device.go | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/src/device.go b/src/device.go index a3461ad..f4a087c 100644 --- a/src/device.go +++ b/src/device.go @@ -8,13 +8,13 @@ import ( ) type Device struct { - closed AtomicBool // device is closed? (acting as guard) + isUp AtomicBool // device is up (TUN interface up)? + isClosed AtomicBool // device is closed? (acting as guard) log *Logger // collection of loggers for levels idCounter uint // for assigning debug ids to peers fwMark uint32 tun struct { device TUNDevice - isUp AtomicBool mtu int32 } pool struct { @@ -45,6 +45,28 @@ type Device struct { mac CookieChecker } +func (device *Device) Up() { + device.mutex.Lock() + defer device.mutex.Unlock() + + device.isUp.Set(true) + updateBind(device) + for _, peer := range device.peers { + peer.Start() + } +} + +func (device *Device) Down() { + device.mutex.Lock() + defer device.mutex.Unlock() + + device.isUp.Set(false) + closeBind(device) + for _, peer := range device.peers { + peer.Stop() + } +} + /* Warning: * The caller must hold the device mutex (write lock) */ @@ -54,9 +76,9 @@ func removePeerUnsafe(device *Device, key NoisePublicKey) { return } peer.mutex.Lock() + peer.Stop() device.routingTable.RemovePeer(peer) delete(device.peers, key) - peer.Close() } func (device *Device) IsUnderLoad() bool { @@ -98,7 +120,7 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error { device.publicKey = publicKey device.mac.Init(publicKey) - // do DH precomputations + // do DH pre-computations rmKey := device.privateKey.IsZero() @@ -132,10 +154,12 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device { device.mutex.Lock() defer device.mutex.Unlock() + device.isUp.Set(false) + device.isClosed.Set(false) + device.log = logger device.peers = make(map[NoisePublicKey]*Peer) device.tun.device = tun - device.tun.isUp.Set(false) device.indices.Init() device.ratelimiter.Init() @@ -196,17 +220,13 @@ func (device *Device) RemovePeer(key NoisePublicKey) { func (device *Device) RemoveAllPeers() { device.mutex.Lock() defer device.mutex.Unlock() - - for key, peer := range device.peers { - peer.mutex.Lock() - delete(device.peers, key) - peer.Close() - peer.mutex.Unlock() + for key := range device.peers { + removePeerUnsafe(device, key) } } func (device *Device) Close() { - if device.closed.Swap(true) { + if device.isClosed.Swap(true) { return } device.log.Info.Println("Closing device") |