aboutsummaryrefslogtreecommitdiffstats
path: root/src/send.go
blob: da5905d5491aa6960cf7070b210e5939ff0d03e8 (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main

import (
	"encoding/binary"
	"golang.org/x/crypto/chacha20poly1305"
	"net"
	"sync"
	"time"
)

/* 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) RoutineOutboundNonceWorker() {
	var packet []byte
	var keyPair *KeyPair
	var flushTimer time.Timer

	for {

		// wait for packet

		if packet == nil {
			packet = <-peer.queueOutboundRouting
		}

		// wait for key pair

		for keyPair == nil {
			flushTimer.Reset(time.Second * 10)
			// TODO: Handshake or NOP
			select {
			case <-peer.keyPairs.newKeyPair:
				keyPair = peer.keyPairs.Current()
				continue
			case <-flushTimer.C:
				size := len(peer.queueOutboundRouting)
				for i := 0; i < size; i += 1 {
					<-peer.queueOutboundRouting
				}
				packet = nil
			}
			break
		}

		// process current packet

		if packet != nil {

			// create work element

			work := new(OutboundWorkQueueElement)
			work.wg.Add(1)
			work.keyPair = keyPair
			work.packet = packet
			work.nonce = keyPair.sendNonce

			packet = nil
			peer.queueOutbound <- work
			keyPair.sendNonce += 1

			// drop packets until there is space

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

/* Go routine
 *
 * sequentially reads packets from queue and sends to endpoint
 *
 */
func (peer *Peer) RoutineSequential() {
	for work := range peer.queueOutbound {
		work.wg.Wait()

		// check if dropped ("ghost packet")

		if work.packet == nil {
			continue
		}

		//

	}
}

func (device *Device) RoutineEncryptionWorker() {
	var nonce [chacha20poly1305.NonceSize]byte
	for work := range device.queueWorkOutbound {
		// pad packet

		padding := device.mtu - len(work.packet)
		if padding < 0 {
			work.packet = nil
			work.wg.Done()
		}
		for n := 0; n < padding; n += 1 {
			work.packet = append(work.packet, 0)
		}

		// encrypt

		binary.LittleEndian.PutUint64(nonce[4:], work.nonce)
		work.packet = work.keyPair.send.Seal(
			work.packet[:0],
			nonce[:],
			work.packet,
			nil,
		)
		work.wg.Done()
	}
}