forked from svpcom/wfb-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.hpp
127 lines (105 loc) · 3.92 KB
/
tx.hpp
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
// -*- 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 <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
class Transmitter
{
public:
Transmitter(int k, int m, const string &keypair);
virtual ~Transmitter();
void send_packet(const uint8_t *buf, size_t size);
void send_session_key(void);
protected:
virtual void inject_packet(const uint8_t *buf, size_t size) = 0;
private:
void send_block_fragment(size_t packet_size);
void make_session_key(void);
fec_t* fec_p;
int fec_k; // RS number of primary fragments in block
int fec_n; // RS total number of fragments in block
uint64_t block_idx; //block_idx << 8 + fragment_idx = nonce (64bit)
uint8_t fragment_idx;
uint8_t** block;
size_t max_packet_size;
// tx->rx keypair
uint8_t tx_secretkey[crypto_box_SECRETKEYBYTES];
uint8_t rx_publickey[crypto_box_PUBLICKEYBYTES];
uint8_t session_key[crypto_aead_chacha20poly1305_KEYBYTES];
wsession_key_t session_key_packet;
};
class PcapTransmitter : public Transmitter
{
public:
PcapTransmitter(int k, int m, const string &keypair, uint8_t radio_port, const char* wlan);
virtual ~PcapTransmitter();
private:
virtual void inject_packet(const uint8_t *buf, size_t size);
uint8_t radio_port;
string wlan;
uint16_t ieee80211_seq;
pcap_t *ppcap;
};
class UdpTransmitter : public Transmitter
{
public:
UdpTransmitter(int k, int m, const string &keypair, const string &client_addr, int client_port) : Transmitter(k, m, keypair)
{
sockfd = open_udp_socket(client_addr, client_port);
}
virtual ~UdpTransmitter()
{
close(sockfd);
}
private:
virtual void inject_packet(const uint8_t *buf, size_t size)
{
wrxfwd_t fwd_hdr = { .wlan_idx = (uint8_t)(rand() % 2),
.antenna = (uint8_t)(rand() % 2),
.rssi = (uint8_t)(rand() & 0xff) };
struct iovec iov[2] = {{ .iov_base = (void*)&fwd_hdr,
.iov_len = sizeof(fwd_hdr)},
{ .iov_base = (void*)buf,
.iov_len = size }};
struct msghdr msghdr = { .msg_name = NULL,
.msg_namelen = 0,
.msg_iov = iov,
.msg_iovlen = 2,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0};
sendmsg(sockfd, &msghdr, 0);
}
int open_udp_socket(const string &client_addr, int client_port)
{
struct sockaddr_in saddr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) throw runtime_error(string_format("Error opening socket: %s", strerror(errno)));
bzero((char *) &saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(client_addr.c_str());
saddr.sin_port = htons((unsigned short)client_port);
if (connect(fd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
{
throw runtime_error(string_format("Connect error: %s", strerror(errno)));
}
return fd;
}
int sockfd;
};