-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouting.c
399 lines (326 loc) · 9.12 KB
/
routing.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include <math.h>
#include "../ext/uthash.h"
#include "../log.h"
#include "../utils.h"
#include "../tun.h"
#include "../net.h"
#include "../unix.h"
#include "../console.h"
#include "../main.h"
#include "../interfaces.h"
// dimensions
#define DIM 4
#define TIMEOUT_NEIGHBOR_SEC 5
#define COMM_SEND_INTERVAL_SEC 1
enum {
TYPE_DATA,
TYPE_COMM,
};
typedef struct {
uint32_t sender_id;
Address addr;
float pos[DIM];
uint64_t last_updated;
UT_hash_handle hh;
} Neighbor;
// only travels one hop to the neighbors
typedef struct __attribute__((__packed__)) {
uint8_t type;
uint32_t sender_id;
float pos[DIM];
} COMM;
typedef struct __attribute__((__packed__)) {
uint8_t type;
uint32_t sender_id;
uint8_t hop_count;
float dst_pos[DIM];
uint16_t length; // might not be needed
uint8_t payload[2000];
} DATA;
static float g_own_pos[DIM];
static Neighbor *g_neighbors = NULL;
static void vec_mul(float *ret, float b, const float *a)
{
for (size_t i = 0; i < DIM; ++i) {
ret[i] = a[i] * b;
}
}
static void vec_sub(float *ret, const float *a, const float *b)
{
for (size_t i = 0; i < DIM; ++i) {
ret[i] = a[i] - b[i];
}
}
static void vec_add(float *ret, const float *a, const float *b)
{
for (size_t i = 0; i < DIM; ++i) {
ret[i] = a[i] + b[i];
}
}
static void vec_copy(float *ret, const float *a)
{
for (size_t i = 0; i < DIM; ++i) {
ret[i] = a[i];
}
}
static float vec_len(const float *a)
{
float sum = 0;
for (size_t i = 0; i < DIM; ++i) {
sum += a[i] * a[i];
}
return sqrt(sum);
}
static float vec_dist(const float *a, const float *b)
{
float d[DIM];
vec_sub(&d[0], a, b);
return vec_len(&d[0]);
}
static int is_near_null(const float *v, float eta)
{
for (size_t i = 0; i < DIM; ++i) {
if (v[i] >= eta && v[i] <= -eta) {
return 0;
}
}
return 1;
}
static void vec_unit(float *unit, const float *a, const float *b)
{
float dir[DIM];
vec_sub(&dir[0], a, b);
float len = vec_len(dir);
vec_mul(unit, 1.0f / len, dir);
}
static void vec_random_unit(float *ret)
{
for (size_t i = 0; i < DIM; ++i) {
ret[i] = (float) (double) rand();
}
float len = vec_len(ret);
for (size_t i = 0; i < DIM; ++i) {
ret[i] /= len;
}
}
static void neighbor_timeout()
{
Neighbor *tmp;
Neighbor *cur;
HASH_ITER(hh, g_neighbors, cur, tmp) {
if ((cur->last_updated + 1000 * TIMEOUT_NEIGHBOR_SEC) < gstate.time_now) {
log_debug("timeout neighbor 0x%08x", cur->sender_id);
HASH_DEL(g_neighbors, cur);
free(cur);
}
}
}
static Neighbor *neighbor_find(uint32_t sender_id)
{
Neighbor *cur;
HASH_FIND(hh, g_neighbors, &sender_id, sizeof(uint32_t), cur);
return cur;
}
static Neighbor *neighbor_add(uint32_t sender_id, float *pos, const Address *addr)
{
Neighbor *e = (Neighbor*) malloc(sizeof(Neighbor));
e->sender_id = sender_id;
memcpy(&e->pos, pos, sizeof(e->pos));
memcpy(&e->addr, addr, sizeof(Address));
e->last_updated = gstate.time_now;
HASH_ADD(hh, g_neighbors, sender_id, sizeof(uint32_t), e);
return e;
}
static void vivaldi_update_simple(float *local_pos, const float *remote_pos, float expected_error)
{
const float eta = 0.001;
const float delta = 0.25;
const float error = expected_error - vec_dist(local_pos, remote_pos);
// create unit vector in the direction of the error
float direction[DIM];
vec_unit(direction, remote_pos, local_pos);
// use random direction
if (is_near_null(direction, eta)) {
vec_random_unit(direction);
}
float force[DIM];
vec_mul(force, error, direction);
// move a small step in the direction of the force
for (size_t i = 0; i < DIM; i++) {
local_pos[i] += delta * force[i];
}
}
static void handle_COMM(const Address *rcv, const Address *src, const Address *dst, COMM *p, size_t length)
{
if (length != sizeof(COMM)) {
log_debug("COMM: invalid packet size => drop");
return;
}
log_debug("COMM: got packet: %s / 0x%08x", str_addr(src), p->sender_id);
if (p->sender_id == gstate.own_id) {
log_debug("COMM: recevied own packet => drop");
return;
}
float new[DIM];
float old[DIM];
Neighbor *neighbor = neighbor_find(p->sender_id);
if (neighbor) {
memcpy(&new[0], &p->pos[0], sizeof(new));
memcpy(&old[0], &neighbor->pos[0], sizeof(old));
memcpy(&neighbor->pos[0], &new[0], sizeof(neighbor->pos));
neighbor->last_updated = gstate.time_now;
} else {
memcpy(&new[0], &p->pos[0], sizeof(new));
memcpy(&old[0], &p->pos[0], sizeof(old));
neighbor = neighbor_add(p->sender_id, &new[0], src);
}
vivaldi_update_simple(&g_own_pos[0], &neighbor->pos[0], 1.5f);
}
static void forward_DATA(const DATA *p, size_t length)
{
uint32_t send_counter = 0;
float dst_pos[DIM];
memcpy(&dst_pos[0], &p->dst_pos[0], sizeof(p->dst_pos));
const float dist_own = vec_dist(&g_own_pos[0], &dst_pos[0]);
log_debug("dist_own: %.2f", dist_own);
Neighbor *tmp;
Neighbor *cur;
HASH_ITER(hh, g_neighbors, cur, tmp) {
// propability to transmit from neighbor to destination
const float dist_neighbor = vec_dist(&cur->pos[0], &dst_pos[0]);
log_debug("dist_neighbor: %.2f", dist_neighbor);
if (dist_neighbor > dist_own) {
send_ucast_l2(&cur->addr, p, length);
send_counter += 1;
}
}
log_debug("forward data packet to %u neighbors", send_counter);
}
static void handle_DATA(const Address *rcv, const Address *src, const Address *dst, DATA *p, size_t length)
{
if (length < offsetof(DATA, payload) || length != (offsetof(DATA, payload) + p->length)) {
log_debug("DATA: invalid packet size => drop");
return;
}
if (p->sender_id == gstate.own_id) {
log_debug("DATA: own packet => drop");
return;
}
if (p->hop_count > 200) {
log_warning("max hop count reached (200)");
return;
}
p->sender_id = gstate.own_id;
p->hop_count += 1;
forward_DATA(p, length);
}
// receive traffic from tun0 and send to peers
static void tun_handler(uint32_t dst_id, uint8_t *packet, size_t packet_length)
{
// TODO:
//forward_DATA();
}
static void ext_handler_l2(const Address *rcv, const Address *src, const Address *dst, uint8_t *packet, size_t packet_length)
{
if (!address_is_broadcast(dst) && !address_equal(dst, rcv)) {
// packet is not for us (possible e.g. when device is in monitor mode)
return;
}
switch (packet[0]) {
case TYPE_COMM:
handle_COMM(rcv, src, dst, (COMM*) packet, packet_length);
break;
case TYPE_DATA:
handle_DATA(rcv, src, dst, (DATA*) packet, packet_length);
break;
default:
log_warning("unknown packet type 0x%02x from %s (%s)", packet[0], str_addr(src));
}
}
static void send_COMMs()
{
static uint64_t g_last_send = 0;
if (g_last_send != 0 && (g_last_send + 1000 * COMM_SEND_INTERVAL_SEC) > gstate.time_now) {
return;
} else {
g_last_send = gstate.time_now;
}
COMM data = {
.type = TYPE_COMM,
.sender_id = gstate.own_id,
};
memcpy(&data.pos[0], &g_own_pos[0], sizeof(data.pos));
send_bcast_l2(0, &data, sizeof(data));
}
static void periodic_handler(int _events, int _fd)
{
static uint64_t g_every_second = 0;
if (g_every_second == gstate.time_now) {
return;
} else {
g_every_second = gstate.time_now;
}
send_COMMs();
}
static char *str_pos(char *buf, const float *pos)
{
char *cur = buf;
for (size_t i = 0; i < DIM; i++) {
if (i == 0) {
cur += sprintf(cur, "%f", pos[i]);
} else {
cur += sprintf(cur, " %f", pos[i]);
}
}
return buf;
}
static bool console_handler(FILE *fp, int argc, const char *argv[])
{
char buf_pos[8 * DIM];
if (match(argv, "h")) {
fprintf(fp, "n: print neighbor table\n");
} else if (match(argv, "i")) {
fprintf(fp, " own pos: %s\n", str_pos(buf_pos, g_own_pos));
} else if (match(argv, "n")) {
uint32_t counter = 0;
Neighbor *cur;
Neighbor *tmp;
fprintf(fp, " sender_id addr updated bloom\n");
HASH_ITER(hh, g_neighbors, cur, tmp) {
fprintf(fp, " 0x%08x %s %s %s\n",
cur->sender_id,
str_addr(&cur->addr),
str_since(cur->last_updated),
str_pos(buf_pos, cur->pos)
);
counter += 1;
}
fprintf(fp, "%u entries\n", counter);
} else {
return true;
}
return false;
}
static void init()
{
vec_random_unit(&g_own_pos[0]);
net_add_handler(-1, &periodic_handler);
}
void vivaldi_0_register()
{
static const Protocol p = {
.name = "vivaldi-0",
.init_handler = &init,
.tun_handler = &tun_handler,
.ext_handler_l2 = &ext_handler_l2,
.console_handler = &console_handler,
};
protocols_register(&p);
}