Peer timer teardown

This commit is contained in:
Mathias Hall-Andersen
2017-12-29 17:42:09 +01:00
parent 996c7c4d8a
commit d73f960aab
7 changed files with 163 additions and 102 deletions

View File

@@ -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")