summaryrefslogtreecommitdiffhomepage
path: root/src/routing.go
blob: 0aa111ce604f4509a59945365830d301fbd9f0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"errors"
	"fmt"
	"net"
	"sync"
)

type RoutingTable struct {
	IPv4  *Trie
	IPv6  *Trie
	mutex sync.RWMutex
}

func (table *RoutingTable) RemovePeer(peer *Peer) {
	table.mutex.Lock()
	defer table.mutex.Unlock()
	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")
		}
	}
}