summaryrefslogtreecommitdiffhomepage
path: root/src/device.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.go')
-rw-r--r--src/device.go37
1 files changed, 30 insertions, 7 deletions
diff --git a/src/device.go b/src/device.go
index 52ac6a4..a33e923 100644
--- a/src/device.go
+++ b/src/device.go
@@ -7,16 +7,21 @@ import (
)
type Device struct {
- mtu int
- fwMark uint32
- address *net.UDPAddr // UDP source address
- conn *net.UDPConn // UDP "connection"
+ mtu int
+ log *Logger // collection of loggers for levels
+ idCounter uint // for assigning debug ids to peers
+ fwMark uint32
+ net struct {
+ // seperate for performance reasons
+ mutex sync.RWMutex
+ addr *net.UDPAddr // UDP source address
+ conn *net.UDPConn // UDP "connection"
+ }
mutex sync.RWMutex
privateKey NoisePrivateKey
publicKey NoisePublicKey
routingTable RoutingTable
indices IndexTable
- log *Logger
queue struct {
encryption chan *QueueOutboundElement // parallel work queue
}
@@ -44,17 +49,29 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) {
}
}
-func NewDevice(tun TUNDevice) *Device {
+func NewDevice(tun TUNDevice, logLevel int) *Device {
device := new(Device)
device.mutex.Lock()
defer device.mutex.Unlock()
- device.log = NewLogger()
+ device.log = NewLogger(logLevel)
device.peers = make(map[NoisePublicKey]*Peer)
device.indices.Init()
device.routingTable.Reset()
+ // listen
+
+ device.net.mutex.Lock()
+ device.net.conn, _ = net.ListenUDP("udp", device.net.addr)
+ addr := device.net.conn.LocalAddr()
+ device.net.addr, _ = net.ResolveUDPAddr(addr.Network(), addr.String())
+ device.net.mutex.Unlock()
+
+ // create queues
+
+ device.queue.encryption = make(chan *QueueOutboundElement, QueueOutboundSize)
+
// start workers
for i := 0; i < runtime.NumCPU(); i += 1 {
@@ -92,5 +109,11 @@ func (device *Device) RemoveAllPeers() {
peer.mutex.Lock()
delete(device.peers, key)
peer.Close()
+ peer.mutex.Unlock()
}
}
+
+func (device *Device) Close() {
+ device.RemoveAllPeers()
+ close(device.queue.encryption)
+}