wfb-ng/rx.hpp

120 lines
3.3 KiB
C++
Raw Normal View History

2017-01-23 04:37:26 -04:00
// -*- C++ -*-
//
2017-01-24 08:37:19 -04:00
// Copyright (C) 2017 Vasily Evseenko <svpcom@p2ptech.org>
/*
* 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
2017-05-05 13:45:17 -03:00
* the Free Software Foundation; version 3.
2017-01-24 08:37:19 -04:00
*
* 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.
*/
2017-01-23 04:37:26 -04:00
typedef enum {
LOCAL,
FORWARDER,
AGGREGATOR
} rx_mode_t;
2017-01-23 04:37:26 -04:00
class BaseAggregator
2017-01-23 04:37:26 -04:00
{
public:
virtual void process_packet(const uint8_t *buf, size_t size) = 0;
protected:
int open_udp_socket_for_tx(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;
}
};
class Forwarder : public BaseAggregator
{
public:
Forwarder(const string &client_addr, int client_port);
~Forwarder();
virtual void process_packet(const uint8_t *buf, size_t size);
2017-01-23 04:37:26 -04:00
private:
int sockfd;
};
typedef struct {
uint64_t block_idx;
uint8_t** fragments;
uint8_t *fragment_map;
uint8_t send_fragment_idx;
uint8_t has_fragments;
} rx_ring_item_t;
#define RX_RING_SIZE 40
2017-04-07 07:31:03 -03:00
static inline int modN(int x, int base)
{
2017-04-07 07:31:03 -03:00
return (base + (x % base)) % base;
}
class Aggregator : public BaseAggregator
{
public:
Aggregator(const string &client_addr, int client_port, int k, int n, const string &keypair);
~Aggregator();
virtual void process_packet(const uint8_t *buf, size_t size);
private:
void send_packet(int ring_idx, int fragment_idx);
void apply_fec(int ring_idx);
int get_block_ring_idx(uint64_t block_idx);
int rx_ring_push(void);
2017-01-23 04:37:26 -04:00
fec_t* fec_p;
int fec_k; // RS number of primary fragments in block
int fec_n; // RS total number of fragments in block
int sockfd;
2017-01-23 04:37:26 -04:00
uint32_t seq;
rx_ring_item_t rx_ring[RX_RING_SIZE];
int rx_ring_front; // current packet
int rx_ring_alloc; // number of allocated entries
uint64_t last_known_block; //id of last known block
2017-05-04 06:33:57 -03:00
// rx->tx keypair
uint8_t rx_secretkey[crypto_box_SECRETKEYBYTES];
uint8_t tx_publickey[crypto_box_PUBLICKEYBYTES];
uint8_t session_key[crypto_aead_chacha20poly1305_KEYBYTES];
2017-01-23 04:37:26 -04:00
};
class Receiver
{
public:
Receiver(const char* wlan, int port, BaseAggregator* agg);
2017-01-23 04:37:26 -04:00
~Receiver();
void loop_iter(void);
int getfd(void){ return fd; }
private:
BaseAggregator *agg;
2017-01-23 04:37:26 -04:00
int fd;
pcap_t *ppcap;
};