diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-04 21:48:15 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-04 21:48:15 +0200 |
commit | 1868d15914d6cd7cd57b90b7644b008ec16361b9 (patch) | |
tree | dbc788f49f433a5837db3c022facb19be38e4ea1 /src/routing.go | |
parent | dbc3ee3e9dc50e01dab9ae789a44f90502542335 (diff) |
Beginning work on TUN interface
And outbound routing
I am not entirely convinced the use of net.IP is a good idea,
since the internal representation of net.IP is a byte slice
and all constructor functions in "net" return 16 byte slices
(padded for IPv4), while the use in this project uses 4 byte slices.
Which may be confusing.
Diffstat (limited to 'src/routing.go')
-rw-r--r-- | src/routing.go | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/src/routing.go b/src/routing.go index 99b180c..0aa111c 100644 --- a/src/routing.go +++ b/src/routing.go @@ -1,13 +1,12 @@ package main import ( + "errors" + "fmt" + "net" "sync" ) -/* Thread-safe high level functions for cryptkey routing. - * - */ - type RoutingTable struct { IPv4 *Trie IPv6 *Trie @@ -20,3 +19,51 @@ func (table *RoutingTable) RemovePeer(peer *Peer) { table.IPv4 = table.IPv4.RemovePeer(peer) table.IPv6 = table.IPv6.RemovePeer(peer) } + +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) +} + +func OutgoingRoutingWorker(device *Device, queue chan []byte) { + for { + packet := <-queue + switch packet[0] >> 4 { + + case IPv4version: + dst := packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len] + peer := device.routingTable.LookupIPv4(dst) + fmt.Println("IPv4", peer) + + case IPv6version: + dst := packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len] + peer := device.routingTable.LookupIPv6(dst) + fmt.Println("IPv6", peer) + + default: + // todo: log + fmt.Println("Unknown IP version") + } + } +} |