2024-09-22 14:32:49 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* Copyright (C) 2024 Thomas Kolb
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <liquid/liquid.h>
|
|
|
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
#include "layer2/ham64.h"
|
2024-09-22 14:32:49 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define LOGGER_MODULE_NAME "main"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "jsonlogger.h"
|
|
|
|
#include "debug_structs.h"
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
#include "layer2/connection.h"
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
#include "layer2/tundev.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#define RESULT_CHECK(stmt) { \
|
|
|
|
result_t res = stmt; \
|
|
|
|
if(res != OK) { \
|
|
|
|
LOG(LVL_FATAL, "Error %d in %s:%d!", res, __FILE__, __LINE__); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BROADCAST_PORT 3737
|
|
|
|
|
|
|
|
static int m_tunfd = -1;
|
|
|
|
static bool m_running = true;
|
|
|
|
|
|
|
|
static int m_bcast_sock = -1;
|
|
|
|
|
2024-11-02 16:17:52 +01:00
|
|
|
static uint64_t next_tx_switch_time = 0;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
static rx_stats_t m_rx_stats;
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
static connection_ctx_t l2conn;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
static void signal_handler(int signal, siginfo_t *info, void *ctx)
|
|
|
|
{
|
|
|
|
(void)signal;
|
|
|
|
(void)info;
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
LOG(LVL_INFO, "\nGracefully shutting down on signal %d.", signal);
|
|
|
|
|
|
|
|
m_running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void block_tx_for(unsigned offset_ms)
|
|
|
|
{
|
2024-11-02 16:17:52 +01:00
|
|
|
next_tx_switch_time = get_hires_time() + HRTIME_MS(offset_ms);
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static result_t transmit(const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
result_t result = OK;
|
|
|
|
|
|
|
|
struct sockaddr_in bcast_addr = {0};
|
|
|
|
|
|
|
|
bcast_addr.sin_family = AF_INET;
|
|
|
|
bcast_addr.sin_port = htons(BROADCAST_PORT);
|
|
|
|
bcast_addr.sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
|
|
|
|
int ret = sendto(m_bcast_sock, data, len, 0, (struct sockaddr*)&bcast_addr, sizeof(bcast_addr));
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "sendto: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "t");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
void rx_data_to_tun(const layer2_data_packet_t *data_packet)
|
2024-09-22 15:34:50 +02:00
|
|
|
{
|
2024-11-17 18:26:18 +01:00
|
|
|
uint8_t tun_packet[4 + data_packet->payload_len];
|
2024-09-22 15:34:50 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
// flags
|
|
|
|
tun_packet[0] = 0;
|
|
|
|
tun_packet[1] = 0;
|
|
|
|
|
|
|
|
switch(data_packet->payload_type) {
|
|
|
|
case L2_PAYLOAD_TYPE_IPV6:
|
|
|
|
*(uint16_t*)(tun_packet+2) = 0x86dd;
|
|
|
|
break;
|
|
|
|
case L2_PAYLOAD_TYPE_IPV4:
|
|
|
|
*(uint16_t*)(tun_packet+2) = 0x0800;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(LVL_ERR, "Unsupported payload type: 0x%08x.", data_packet->payload_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tun_packet+4, data_packet->payload, data_packet->payload_len);
|
|
|
|
|
|
|
|
int ret = write(m_tunfd, tun_packet, sizeof(tun_packet));
|
2024-09-22 15:34:50 +02:00
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "write(tun): %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
|
2024-11-10 21:40:07 +01:00
|
|
|
void conn_evt_cb(struct connection_ctx_s *conn, connection_evt_t evt, void *user_ctx)
|
|
|
|
{
|
|
|
|
(void)conn;
|
|
|
|
(void)evt;
|
|
|
|
(void)user_ctx;
|
|
|
|
}
|
|
|
|
|
2024-09-22 15:34:50 +02:00
|
|
|
|
2024-12-07 20:39:16 +01:00
|
|
|
int main(void)
|
2024-09-22 14:32:49 +02:00
|
|
|
{
|
|
|
|
// initialize the console logger
|
|
|
|
logger_init();
|
|
|
|
|
|
|
|
if(!jsonlogger_init("jsonlog.fifo")) {
|
|
|
|
LOG(LVL_FATAL, "Could not initialize JSON logger.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2024-11-02 16:17:52 +01:00
|
|
|
srand(get_hires_time());
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
// ** Initialize **
|
|
|
|
|
|
|
|
char devname[IFNAMSIZ] = "hamnet70";
|
|
|
|
m_tunfd = tundev_open(devname);
|
|
|
|
|
|
|
|
if(m_tunfd < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
ham64_t my_address, peer_address;
|
|
|
|
ham64_encode(MY_CALL, &my_address);
|
|
|
|
ham64_encode(PEER_CALL, &peer_address);
|
2024-12-03 22:18:46 +01:00
|
|
|
RESULT_CHECK(connection_init(&l2conn, &my_address, &peer_address));
|
2024-09-22 15:24:20 +02:00
|
|
|
|
|
|
|
// force connection into the established state
|
|
|
|
l2conn.conn_state = CONN_STATE_ESTABLISHED;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
// ** Set up signal handling
|
|
|
|
|
|
|
|
struct sigaction term_action = {0};
|
|
|
|
term_action.sa_sigaction = signal_handler;
|
|
|
|
|
|
|
|
if(sigaction(SIGTERM, &term_action, NULL) < 0) {
|
|
|
|
LOG(LVL_ERR, "sigaction: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sigaction(SIGINT, &term_action, NULL) < 0) {
|
|
|
|
LOG(LVL_ERR, "sigaction: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ** Set up UDP socket
|
|
|
|
|
|
|
|
m_bcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if(m_bcast_sock < 0) {
|
|
|
|
LOG(LVL_ERR, "socket: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int broadcastEnable=1;
|
|
|
|
int ret = setsockopt(m_bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "setsockopt: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in bind_addr = {0};
|
|
|
|
|
|
|
|
bind_addr.sin_family = AF_INET;
|
|
|
|
bind_addr.sin_port = htons(BROADCAST_PORT);
|
|
|
|
bind_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
ret = bind(m_bcast_sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr));
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "bind: %s", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ** Process packets **
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
struct pollfd pfd_tun;
|
|
|
|
memset(&pfd_tun, 0, sizeof(pfd_tun));
|
|
|
|
|
|
|
|
pfd_tun.fd = m_tunfd;
|
|
|
|
pfd_tun.events = POLLIN;
|
|
|
|
|
2024-09-22 14:32:49 +02:00
|
|
|
struct pollfd pfd_bcast;
|
|
|
|
memset(&pfd_bcast, 0, sizeof(pfd_bcast));
|
|
|
|
|
|
|
|
pfd_bcast.fd = m_bcast_sock;
|
|
|
|
pfd_bcast.events = POLLIN;
|
|
|
|
|
2024-11-02 16:17:52 +01:00
|
|
|
uint64_t old = get_hires_time();
|
2024-09-22 14:32:49 +02:00
|
|
|
size_t total_bytes = 0;
|
2024-11-02 16:17:52 +01:00
|
|
|
uint64_t next_stats_print_time = old + HRTIME_MS(500);
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-10 17:02:14 +01:00
|
|
|
// TODO: wait for beacon
|
|
|
|
// TODO: send connection request
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
while(m_running) {
|
2024-12-03 22:18:46 +01:00
|
|
|
connection_evt_t evt;
|
|
|
|
RESULT_CHECK(connection_maintain(&l2conn, &evt));
|
|
|
|
|
|
|
|
if(evt == CONN_EVT_TIMEOUT) {
|
|
|
|
LOG(LVL_ERR, "Connection timed out. Shutting down.");
|
|
|
|
break;
|
|
|
|
}
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
// fill the TX queue from the TUN device
|
2024-09-22 15:24:20 +02:00
|
|
|
while(connection_can_enqueue_packet(&l2conn)) {
|
|
|
|
int ret = poll(&pfd_tun, 1, 0 /* timeout */);
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "poll: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
} else if(ret == 0) {
|
|
|
|
// no more packets
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// a packet is available -> move it to the queue
|
|
|
|
static const size_t packetbuf_size = 2048;
|
|
|
|
uint8_t packetbuf[packetbuf_size];
|
|
|
|
ret = read(m_tunfd, packetbuf, packetbuf_size);
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "read: %s", strerror(errno));
|
|
|
|
return ERR_SYSCALL;
|
|
|
|
} else if(ret == 0) {
|
|
|
|
// no more data, should not happen
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-11-10 23:13:31 +01:00
|
|
|
LOG(LVL_DUMP, "TUN Flags: 0x%04x", *(uint16_t*)packetbuf);
|
|
|
|
LOG(LVL_DUMP, "TUN Proto: 0x%04x", *((uint16_t*)packetbuf + 1));
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
RESULT_CHECK(connection_enqueue_packet(&l2conn, packetbuf, ret));
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
// transmit one burst
|
|
|
|
if(connection_can_transmit(&l2conn)) {
|
|
|
|
// there is a packet to be (re)transmitted.
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
LOG(LVL_DEBUG, "Starting new burst.");
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
size_t burst_len = 0;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
// add packets to the burst until only 50000 samples remain free in the SDR buffer
|
|
|
|
while(true) {
|
|
|
|
uint8_t packet_buf[2048];
|
|
|
|
size_t packet_size;
|
|
|
|
bool end_burst;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
packet_size = connection_encode_next_packet(&l2conn,
|
|
|
|
packet_buf, sizeof(packet_buf), &end_burst);
|
2024-11-10 21:40:07 +01:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
if(packet_size == 0) {
|
|
|
|
// no more packets available
|
|
|
|
LOG(LVL_DEBUG, "Ending burst due to empty packet queue.");
|
|
|
|
break;
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
LOG(LVL_DEBUG, "Adding packet with %d bytes to burst.", packet_size);
|
2024-09-22 18:46:32 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
burst_len++;
|
|
|
|
RESULT_CHECK(transmit(packet_buf, packet_size));
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
if(end_burst) {
|
|
|
|
LOG(LVL_DEBUG, "Ending burst on request.");
|
|
|
|
break;
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
2024-11-17 18:26:18 +01:00
|
|
|
}
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
connection_tx_clean_empty_packet(&l2conn);
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
LOG(LVL_DEBUG, "Burst finished: %zd packets sent.", burst_len);
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
// make sure the receiver runs for a minimum amount of time
|
|
|
|
block_tx_for(TX_SWITCH_BACKOFF_AFTER_RX_ON);
|
|
|
|
|
|
|
|
while(get_hires_time() < next_tx_switch_time) {
|
2024-09-22 14:32:49 +02:00
|
|
|
// ** Receive signal **
|
|
|
|
|
|
|
|
int ret = poll(&pfd_bcast, 1, 10);
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "poll: %s", strerror(errno));
|
2024-11-17 18:26:18 +01:00
|
|
|
return EXIT_FAILURE;
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ret == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t packetbuf[65536];
|
|
|
|
ret = recv(m_bcast_sock, packetbuf, sizeof(packetbuf), 0);
|
|
|
|
if(ret < 0) {
|
|
|
|
LOG(LVL_ERR, "recv: %s", strerror(errno));
|
2024-11-17 18:26:18 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else if(ret == 0) {
|
|
|
|
LOG(LVL_ERR, "recv() returned zero.");
|
|
|
|
return EXIT_FAILURE;
|
2024-09-22 14:32:49 +02:00
|
|
|
}
|
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
block_tx_for(TX_SWITCH_BACKOFF_END_OF_PACKET_MS);
|
|
|
|
|
|
|
|
layer2_data_packet_t data_packet;
|
2024-09-22 14:32:49 +02:00
|
|
|
|
2024-11-17 18:26:18 +01:00
|
|
|
result_t result = connection_handle_packet(&l2conn, packetbuf, ret, &data_packet);
|
|
|
|
switch(result) {
|
|
|
|
case OK:
|
|
|
|
m_rx_stats.successful_decodes++;
|
|
|
|
|
|
|
|
if(data_packet.payload_len != 0) {
|
|
|
|
rx_data_to_tun(&data_packet);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERR_INTEGRITY:
|
|
|
|
LOG(LVL_ERR, "Packet could not be decoded by Layer 2.");
|
|
|
|
m_rx_stats.failed_decodes++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERR_SEQUENCE:
|
|
|
|
LOG(LVL_ERR, "Packet not in the expected sequence.");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // all other errors
|
|
|
|
LOG(LVL_ERR, "layer2_rx_handle_packet() returned error code %u.", result);
|
|
|
|
break;
|
|
|
|
}
|
2024-09-22 14:45:24 +02:00
|
|
|
|
2024-09-22 14:32:49 +02:00
|
|
|
total_bytes += ret;
|
|
|
|
|
2024-11-02 16:17:52 +01:00
|
|
|
uint64_t new = get_hires_time();
|
2024-09-22 14:32:49 +02:00
|
|
|
if(new >= next_stats_print_time) {
|
2024-11-02 16:17:52 +01:00
|
|
|
double rate = total_bytes * 1e9 / (new - old);
|
2024-09-22 14:32:49 +02:00
|
|
|
LOG(LVL_INFO, "\nEstimated rate: %.3f kB/s", rate / 1e3);
|
|
|
|
LOG(LVL_INFO, "Receiver statistics:");
|
|
|
|
LOG(LVL_INFO, " Preambles found: %8zd", m_rx_stats.preambles_found);
|
|
|
|
LOG(LVL_INFO, " Successful decodes: %8zd (%6.2f %%)",
|
|
|
|
m_rx_stats.successful_decodes, m_rx_stats.successful_decodes * 100.0f / m_rx_stats.preambles_found);
|
|
|
|
LOG(LVL_INFO, " Header errors: %8zd (%6.2f %%)",
|
|
|
|
m_rx_stats.header_errors, m_rx_stats.header_errors * 100.0f / m_rx_stats.preambles_found);
|
|
|
|
LOG(LVL_INFO, " Failed decodes: %8zd (%6.2f %%)",
|
|
|
|
m_rx_stats.failed_decodes, m_rx_stats.failed_decodes * 100.0f / m_rx_stats.preambles_found);
|
2024-11-02 16:17:52 +01:00
|
|
|
next_stats_print_time += HRTIME_MS(500);
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
total_bytes = 0;
|
|
|
|
old = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ** Cleanup **
|
|
|
|
|
|
|
|
close(m_bcast_sock);
|
|
|
|
|
2024-09-22 15:24:20 +02:00
|
|
|
connection_destroy(&l2conn);
|
2024-09-22 14:32:49 +02:00
|
|
|
|
|
|
|
jsonlogger_shutdown();
|
|
|
|
|
|
|
|
LOG(LVL_INFO, "Done.");
|
|
|
|
|
|
|
|
logger_shutdown();
|
|
|
|
}
|
|
|
|
|