-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrouting.c
More file actions
136 lines (110 loc) · 3.27 KB
/
routing.c
File metadata and controls
136 lines (110 loc) · 3.27 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include "../ext/seqnum_cache.h"
#include "../ext/uthash.h"
#include "../log.h"
#include "../utils.h"
#include "../net.h"
#include "../tun.h"
#include "../unix.h"
#include "../console.h"
#include "../main.h"
#include "../interfaces.h"
#include "routing.h"
enum {
TYPE_DATA
};
#define SEQNUM_TIMEOUT_SEC 30
typedef struct __attribute__((__packed__)) {
uint8_t type;
uint16_t seq_num; // sequence number
uint32_t src_id;
uint32_t dst_id;
uint16_t payload_length;
//uint8_t payload[ETH_FRAME_LEN];
} DATA;
static uint16_t g_sequence_number = 0;
static uint8_t *get_data_payload(const DATA *data)
{
return ((uint8_t*) data) + sizeof(DATA);
}
static size_t get_data_size(const DATA *data)
{
return sizeof(DATA) + data->payload_length;
}
static void handle_DATA(const Address *rcv, const Address *src, const Address *dst, DATA *p, size_t length)
{
if (!address_is_broadcast(dst)) {
log_trace("unexpected destination (%s) => drop", str_addr(dst));
return;
}
if (length < sizeof(DATA) || length != get_data_size(p)) {
log_debug("DATA: invalid packet size => drop");
return;
}
if (p->src_id == gstate.own_id) {
log_trace("DATA: own source id => drop");
return;
}
uint8_t is_new = seqnum_cache_update(p->src_id, p->seq_num);
log_debug("DATA: got packet from %s / 0x%08x => 0x%08x",
str_addr(src), p->src_id, p->dst_id);
if (!is_new) {
log_trace("DATA: received old packet => drop");
return;
}
if (p->dst_id == gstate.own_id) {
log_debug("DATA: destination reached => accept");
// destination is the local tun0 interface => write packet to tun0
tun_write(get_data_payload(p), p->payload_length);
} else {
log_debug("DATA: destination not reached => rebroadcast");
send_bcast_l2(0, 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)
{
DATA *p = (DATA*) (packet - sizeof(DATA));
p->type = TYPE_DATA;
p->seq_num = g_sequence_number++;
p->src_id = gstate.own_id;
p->dst_id = dst_id;
p->payload_length = packet_length;
seqnum_cache_update(p->src_id, p->seq_num);
log_debug("send DATA packet as broadcast");
send_bcast_l2(0, p, get_data_size(p));
}
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_DATA:
handle_DATA(rcv, src, dst, (DATA*) packet, packet_length);
break;
default:
log_warning("unknown packet type 0x%02x from %s", packet[0], str_addr(src));
}
}
static void init()
{
seqnum_cache_init(SEQNUM_TIMEOUT_SEC);
}
void flood_0_register()
{
static const Protocol p = {
.name = "flood-0",
.init_handler = &init,
.tun_handler = &tun_handler,
.ext_handler_l2 = &ext_handler_l2,
};
protocols_register(&p);
}