forked from svpcom/wfb-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.cpp
374 lines (323 loc) · 11.9 KB
/
tx.cpp
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
// -*- C++ -*-
//
// Copyright (C) 2017 Vasily Evseenko <[email protected]>
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <time.h>
#include <sys/resource.h>
#include <pcap/pcap.h>
#include <assert.h>
#include <string>
#include <memory>
extern "C"
{
#include "fec.h"
}
#include "wifibroadcast.hpp"
#include "tx.hpp"
Transmitter::Transmitter(int k, int n, const string &keypair): fec_k(k), fec_n(n), block_idx(0),
fragment_idx(0),
max_packet_size(0)
{
fec_p = fec_new(fec_k, fec_n);
block = new uint8_t*[fec_n];
for(int i=0; i < fec_n; i++)
{
block[i] = new uint8_t[MAX_FEC_PAYLOAD];
}
FILE *fp;
if((fp = fopen(keypair.c_str(), "r")) == NULL)
{
throw runtime_error(string_format("Unable to open %s: %s", keypair.c_str(), strerror(errno)));
}
if (fread(tx_secretkey, crypto_box_SECRETKEYBYTES, 1, fp) != 1) throw runtime_error(string_format("Unable to read tx secret key: %s", strerror(errno)));
if (fread(rx_publickey, crypto_box_PUBLICKEYBYTES, 1, fp) != 1) throw runtime_error(string_format("Unable to read rx public key: %s", strerror(errno)));
fclose(fp);
make_session_key();
}
Transmitter::~Transmitter()
{
for(int i=0; i < fec_n; i++)
{
delete block[i];
}
delete block;
fec_free(fec_p);
}
void Transmitter::make_session_key(void)
{
randombytes_buf(session_key, sizeof(session_key));
session_key_packet.packet_type = WFB_PACKET_KEY;
randombytes_buf(session_key_packet.session_key_nonce, sizeof(session_key_packet.session_key_nonce));
if (crypto_box_easy(session_key_packet.session_key_data, session_key, sizeof(session_key),
session_key_packet.session_key_nonce, rx_publickey, tx_secretkey) != 0)
{
throw runtime_error("Unable to make session key!");
}
}
PcapTransmitter::PcapTransmitter(int k, int n, const string &keypair, uint8_t radio_port, const char *wlan) : Transmitter(k, n, keypair),
radio_port(radio_port), wlan(wlan),
ieee80211_seq(0)
{
char errbuf[PCAP_ERRBUF_SIZE];
ppcap = pcap_create(wlan, errbuf);
if (ppcap == NULL){
throw runtime_error(string_format("Unable to open interface %s in pcap: %s", wlan, errbuf));
}
if (pcap_set_snaplen(ppcap, 4096) !=0) throw runtime_error("set_snaplen failed");
if (pcap_set_promisc(ppcap, 1) != 0) throw runtime_error("set_promisc failed");
//if (pcap_set_rfmon(ppcap, 1) !=0) throw runtime_error("set_rfmon failed");
if (pcap_set_timeout(ppcap, -1) !=0) throw runtime_error("set_timeout failed");
//if (pcap_set_buffer_size(ppcap, 2048) !=0) throw runtime_error("set_buffer_size failed");
if (pcap_activate(ppcap) !=0) throw runtime_error(string_format("pcap_activate failed: %s", pcap_geterr(ppcap)));
//if (pcap_setnonblock(ppcap, 1, errbuf) != 0) throw runtime_error(string_format("set_nonblock failed: %s", errbuf));
}
void PcapTransmitter::inject_packet(const uint8_t *buf, size_t size)
{
uint8_t txbuf[MAX_PACKET_SIZE];
uint8_t *p = txbuf;
assert(size <= MAX_FORWARDER_PACKET_SIZE);
// radiotap header
memcpy(p, radiotap_header, sizeof(radiotap_header));
p += sizeof(radiotap_header);
// ieee80211 header
memcpy(p, ieee80211_header, sizeof(ieee80211_header));
p[SRC_MAC_LASTBYTE] = radio_port;
p[DST_MAC_LASTBYTE] = radio_port;
p[FRAME_SEQ_LB] = ieee80211_seq & 0xff;
p[FRAME_SEQ_HB] = (ieee80211_seq >> 8) & 0xff;
ieee80211_seq += 16;
p += sizeof(ieee80211_header);
// FEC data
memcpy(p, buf, size);
p += size;
if (pcap_inject(ppcap, txbuf, p - txbuf) != p - txbuf)
{
throw runtime_error(string_format("Unable to inject packet"));
}
}
PcapTransmitter::~PcapTransmitter()
{
pcap_close(ppcap);
}
void Transmitter::send_block_fragment(size_t packet_size)
{
uint8_t ciphertext[MAX_FORWARDER_PACKET_SIZE];
wblock_hdr_t *block_hdr = (wblock_hdr_t*)ciphertext;
long long unsigned int ciphertext_len;
assert(packet_size <= MAX_FEC_PAYLOAD);
block_hdr->packet_type = WFB_PACKET_DATA;
block_hdr->nonce = htobe64(((block_idx & BLOCK_IDX_MASK) << 8) + fragment_idx);
// encrypted payload
crypto_aead_chacha20poly1305_encrypt(ciphertext + sizeof(wblock_hdr_t), &ciphertext_len,
block[fragment_idx], packet_size,
(uint8_t*)block_hdr, sizeof(wblock_hdr_t),
NULL, (uint8_t*)(&(block_hdr->nonce)), session_key);
inject_packet(ciphertext, sizeof(wblock_hdr_t) + ciphertext_len);
}
void Transmitter::send_session_key(void)
{
//fprintf(stderr, "Announce session key\n");
inject_packet((uint8_t*)&session_key_packet, sizeof(session_key_packet));
}
void Transmitter::send_packet(const uint8_t *buf, size_t size)
{
wpacket_hdr_t packet_hdr;
assert(size <= MAX_PAYLOAD_SIZE);
packet_hdr.packet_size = htobe16(size);
memset(block[fragment_idx], '\0', MAX_FEC_PAYLOAD);
memcpy(block[fragment_idx], &packet_hdr, sizeof(packet_hdr));
memcpy(block[fragment_idx] + sizeof(packet_hdr), buf, size);
send_block_fragment(sizeof(packet_hdr) + size);
max_packet_size = max(max_packet_size, sizeof(packet_hdr) + size);
fragment_idx += 1;
if (fragment_idx < fec_k) return;
fec_encode(fec_p, (const uint8_t**)block, block + fec_k, max_packet_size);
while (fragment_idx < fec_n)
{
send_block_fragment(max_packet_size);
fragment_idx += 1;
}
block_idx += 1;
fragment_idx = 0;
max_packet_size = 0;
// Generate new session key after MAX_BLOCK_IDX blocks
if (block_idx > MAX_BLOCK_IDX)
{
make_session_key();
send_session_key();
block_idx = 0;
}
}
void video_source(Transmitter *t, int fd)
{
uint8_t buf[MAX_PAYLOAD_SIZE];
uint64_t session_key_announce_ts = 0;
for(;;)
{
ssize_t rsize = recv(fd, buf, sizeof(buf), 0);
if (rsize < 0) throw runtime_error(string_format("Error receiving packet: %s", strerror(errno)));
uint64_t cur_ts = get_time_ms();
if (cur_ts >= session_key_announce_ts)
{
// Announce session key
t->send_session_key();
session_key_announce_ts = cur_ts + SESSION_KEY_ANNOUNCE_MSEC;
}
t->send_packet(buf, rsize);
}
}
void mavlink_source(Transmitter *t, int fd, int agg_latency)
{
struct pollfd fds[1];
if(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0)
{
throw runtime_error(string_format("Unable to set socket into nonblocked mode: %s", strerror(errno)));
}
memset(fds, '\0', sizeof(fds));
fds[0].fd = fd;
fds[0].events = POLLIN;
size_t agg_size = 0;
uint8_t agg_buf[MAX_PAYLOAD_SIZE];
uint64_t expire_ts = get_time_ms() + agg_latency;
uint64_t session_key_announce_ts = 0;
for(;;)
{
uint64_t cur_ts = get_time_ms();
int rc = poll(fds, 1, expire_ts > cur_ts ? expire_ts - cur_ts : 0);
if (rc < 0){
if (errno == EINTR || errno == EAGAIN) continue;
throw runtime_error(string_format("poll error: %s", strerror(errno)));
}
if (rc == 0) // timeout expired
{
if(agg_size > 0)
{
cur_ts = get_time_ms();
if (cur_ts >= session_key_announce_ts)
{
// Announce session key
t->send_session_key();
session_key_announce_ts = cur_ts + SESSION_KEY_ANNOUNCE_MSEC;
}
t->send_packet(agg_buf, agg_size);
agg_size = 0;
}
expire_ts = get_time_ms() + agg_latency;
continue;
}
// some events detected
if (fds[0].revents & (POLLERR | POLLNVAL))
{
throw runtime_error(string_format("socket error: %s", strerror(errno)));
}
if (fds[0].revents & POLLIN)
{
uint8_t buf[MAX_PAYLOAD_SIZE];
ssize_t rsize;
while((rsize = recv(fd, buf, sizeof(buf), 0)) >= 0)
{
if (rsize + agg_size > sizeof(agg_buf)) // new packet doesn't fit to agg buffer
{
if(agg_size > 0)
{
cur_ts = get_time_ms();
if (cur_ts >= session_key_announce_ts)
{
// Announce session key
t->send_session_key();
session_key_announce_ts = cur_ts + SESSION_KEY_ANNOUNCE_MSEC;
}
t->send_packet(agg_buf, agg_size);
agg_size = 0;
}
expire_ts = get_time_ms() + agg_latency;
}
memcpy(agg_buf + agg_size, buf, rsize);
agg_size += rsize;
}
if(errno != EWOULDBLOCK) throw runtime_error(string_format("Error receiving packet: %s", strerror(errno)));
}
}
}
int main(int argc, char * const *argv)
{
int opt;
uint8_t k=8, n=12, radio_port=1;
int udp_port=5600;
bool mavlink_mode = false;
int mavlink_agg_latency = 0;
string keypair = "tx.key";
while ((opt = getopt(argc, argv, "K:m:k:n:u:r:p:")) != -1) {
switch (opt) {
case 'K':
keypair = optarg;
break;
case 'm':
mavlink_mode = true;
mavlink_agg_latency = atoi(optarg);
break;
case 'k':
k = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'u':
udp_port = atoi(optarg);
break;
case 'p':
radio_port = atoi(optarg);
break;
default: /* '?' */
show_usage:
fprintf(stderr, "Usage: %s [-K tx_key] [-m mavlink_agg_in_ms] [-k RS_K] [-n RS_N] [-u udp_port] [-p radio_port] interface\n",
argv[0]);
fprintf(stderr, "Default: K='%s', k=%d, n=%d, udp_port=%d, radio_port=%d\n", keypair.c_str(), k, n, udp_port, radio_port);
fprintf(stderr, "Radio MTU: %lu\n", (unsigned long)MAX_PAYLOAD_SIZE);
exit(1);
}
}
if (optind >= argc) {
goto show_usage;
}
try
{
int fd = open_udp_socket_for_rx(udp_port);
#ifdef DEBUG_TX
shared_ptr<Transmitter>t = shared_ptr<UdpTransmitter>(new UdpTransmitter(k, n, keypair, "127.0.0.1", 5601));
#else
shared_ptr<Transmitter>t = shared_ptr<PcapTransmitter>(new PcapTransmitter(k, n, keypair, radio_port, argv[optind]));
#endif
if (mavlink_mode)
{
mavlink_source(t.get(), fd, mavlink_agg_latency);
}else
{
video_source(t.get(), fd);
}
}catch(runtime_error &e)
{
fprintf(stderr, "Error: %s\n", e.what());
exit(1);
}
return 0;
}