2017-06-01 21:31:30 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-04 21:48:15 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
2017-06-01 21:31:30 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RoutingTable struct {
|
|
|
|
IPv4 *Trie
|
|
|
|
IPv6 *Trie
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:45:45 +02:00
|
|
|
func (table *RoutingTable) AllowedIPs(peer *Peer) []net.IPNet {
|
|
|
|
table.mutex.RLock()
|
|
|
|
defer table.mutex.RUnlock()
|
|
|
|
|
|
|
|
allowed := make([]net.IPNet, 10)
|
|
|
|
table.IPv4.AllowedIPs(peer, allowed)
|
|
|
|
table.IPv6.AllowedIPs(peer, allowed)
|
|
|
|
return allowed
|
|
|
|
}
|
|
|
|
|
2017-06-24 15:34:17 +02:00
|
|
|
func (table *RoutingTable) Reset() {
|
|
|
|
table.mutex.Lock()
|
|
|
|
defer table.mutex.Unlock()
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2017-06-24 15:34:17 +02:00
|
|
|
table.IPv4 = nil
|
|
|
|
table.IPv6 = nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func (table *RoutingTable) RemovePeer(peer *Peer) {
|
|
|
|
table.mutex.Lock()
|
|
|
|
defer table.mutex.Unlock()
|
2017-06-28 23:45:45 +02:00
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
table.IPv4 = table.IPv4.RemovePeer(peer)
|
|
|
|
table.IPv6 = table.IPv6.RemovePeer(peer)
|
|
|
|
}
|
2017-06-04 21:48:15 +02:00
|
|
|
|
|
|
|
func (table *RoutingTable) Insert(ip net.IP, cidr uint, peer *Peer) {
|
|
|
|
table.mutex.Lock()
|
|
|
|
defer table.mutex.Unlock()
|
|
|
|
|
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv6len:
|
|
|
|
table.IPv6 = table.IPv6.Insert(ip, cidr, peer)
|
|
|
|
case net.IPv4len:
|
|
|
|
table.IPv4 = table.IPv4.Insert(ip, cidr, peer)
|
|
|
|
default:
|
|
|
|
panic(errors.New("Inserting unknown address type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *RoutingTable) LookupIPv4(address []byte) *Peer {
|
|
|
|
table.mutex.RLock()
|
|
|
|
defer table.mutex.RUnlock()
|
|
|
|
return table.IPv4.Lookup(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *RoutingTable) LookupIPv6(address []byte) *Peer {
|
|
|
|
table.mutex.RLock()
|
|
|
|
defer table.mutex.RUnlock()
|
|
|
|
return table.IPv6.Lookup(address)
|
|
|
|
}
|