summaryrefslogtreecommitdiffhomepage
path: root/src/send.go
blob: 97903206d99a78cb9e9530f75bb885e9b6fdbc61 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main

import (
	"net"
	"sync"
	"sync/atomic"
)

/* Handles outbound flow
 *
 * 1. TUN queue
 * 2. Routing
 * 3. Per peer queuing
 * 4. (work queuing)
 *
 */

type OutboundWorkQueueElement struct {
	wg      sync.WaitGroup
	packet  []byte
	nonce   uint64
	keyPair *KeyPair
}

func (device *Device) SendPacket(packet []byte) {

	// lookup peer

	var peer *Peer
	switch packet[0] >> 4 {
	case IPv4version:
		dst := packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len]
		peer = device.routingTable.LookupIPv4(dst)

	case IPv6version:
		dst := packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len]
		peer = device.routingTable.LookupIPv6(dst)

	default:
		device.logger.Println("unknown IP version")
		return
	}

	if peer == nil {
		return
	}

	// insert into peer queue

	for {
		select {
		case peer.queueOutboundRouting <- packet:
		default:
			select {
			case <-peer.queueOutboundRouting:
			default:
			}
			continue
		}
		break
	}
}

/* Go routine
 *
 *
 * 1. waits for handshake.
 * 2. assigns key pair & nonce
 * 3. inserts to working queue
 *
 * TODO: avoid dynamic allocation of work queue elements
 */
func (peer *Peer) ConsumeOutboundPackets() {
	for {
		// wait for key pair
		keyPair := func() *KeyPair {
			peer.keyPairs.mutex.RLock()
			defer peer.keyPairs.mutex.RUnlock()
			return peer.keyPairs.current
		}()
		if keyPair == nil {
			if len(peer.queueOutboundRouting) > 0 {
				// TODO: start handshake
				<-peer.keyPairs.newKeyPair
			}
			continue
		}

		// assign packets key pair
		for {
			select {
			case <-peer.keyPairs.newKeyPair:
			default:
			case <-peer.keyPairs.newKeyPair:
			case packet := <-peer.queueOutboundRouting:

				// create new work element

				work := new(OutboundWorkQueueElement)
				work.wg.Add(1)
				work.keyPair = keyPair
				work.packet = packet
				work.nonce = atomic.AddUint64(&keyPair.sendNonce, 1) - 1

				peer.queueOutbound <- work

				// drop packets until there is room

				for {
					select {
					case peer.device.queueWorkOutbound <- work:
						break
					default:
						drop := <-peer.device.queueWorkOutbound
						drop.packet = nil
						drop.wg.Done()
					}
				}
			}
		}
	}
}

func (peer *Peer) RoutineSequential() {
	for work := range peer.queueOutbound {
		work.wg.Wait()
		if work.packet == nil {
			continue
		}
	}
}

func (device *Device) EncryptionWorker() {
	for {
		work := <-device.queueWorkOutbound

		func() {
			defer work.wg.Done()

			// pad packet
			padding := device.mtu - len(work.packet)
			if padding < 0 {
				work.packet = nil
				return
			}
			for n := 0; n < padding; n += 1 {
				work.packet = append(work.packet, 0) // TODO: gotta be a faster way
			}

			//

		}()
	}
}