2017-05-30 20:36:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-23 11:41:59 +00:00
|
|
|
"math/rand"
|
2017-05-30 20:36:49 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2017-06-23 11:41:59 +00:00
|
|
|
/* TODO: Locking may be a little broad here
|
|
|
|
*/
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
type Device struct {
|
2017-06-01 19:31:30 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
peers map[NoisePublicKey]*Peer
|
2017-06-23 11:41:59 +00:00
|
|
|
sessions map[uint32]*Handshake
|
2017-06-01 19:31:30 +00:00
|
|
|
privateKey NoisePrivateKey
|
|
|
|
publicKey NoisePublicKey
|
|
|
|
fwMark uint32
|
|
|
|
listenPort uint16
|
|
|
|
routingTable RoutingTable
|
|
|
|
}
|
|
|
|
|
2017-06-23 11:41:59 +00:00
|
|
|
func (dev *Device) NewID(h *Handshake) uint32 {
|
|
|
|
dev.mutex.Lock()
|
|
|
|
defer dev.mutex.Unlock()
|
|
|
|
for {
|
|
|
|
id := rand.Uint32()
|
|
|
|
_, ok := dev.sessions[id]
|
|
|
|
if !ok {
|
|
|
|
dev.sessions[id] = h
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
func (dev *Device) RemovePeer(key NoisePublicKey) {
|
|
|
|
dev.mutex.Lock()
|
|
|
|
defer dev.mutex.Unlock()
|
|
|
|
peer, ok := dev.peers[key]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
peer.mutex.Lock()
|
|
|
|
dev.routingTable.RemovePeer(peer)
|
|
|
|
delete(dev.peers, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dev *Device) RemoveAllAllowedIps(peer *Peer) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dev *Device) RemoveAllPeers() {
|
|
|
|
dev.mutex.Lock()
|
|
|
|
defer dev.mutex.Unlock()
|
|
|
|
|
|
|
|
for key, peer := range dev.peers {
|
|
|
|
peer.mutex.Lock()
|
|
|
|
dev.routingTable.RemovePeer(peer)
|
|
|
|
delete(dev.peers, key)
|
|
|
|
peer.mutex.Unlock()
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|