aboutsummaryrefslogtreecommitdiffstats
path: root/src/device.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2017-12-29 17:42:09 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2017-12-29 17:42:09 +0100
commitd73f960aab86b9a12b0b7d18aa80ce1d4f130695 (patch)
treeec4b6683c1c0b6b28a95b4bdb718dcc3faa0f222 /src/device.go
parentRemoved IFF_NO_PI from TUN linux (diff)
downloadwireguard-go-d73f960aab86b9a12b0b7d18aa80ce1d4f130695.tar.xz
wireguard-go-d73f960aab86b9a12b0b7d18aa80ce1d4f130695.zip
Peer timer teardown
Diffstat (limited to 'src/device.go')
-rw-r--r--src/device.go44
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")