Cleaned up TCP options

This commit is contained in:
Mark Qvist 2020-06-24 14:15:49 +02:00
parent eae91f349b
commit c2beeee944
1 changed files with 510 additions and 471 deletions

View File

@ -8,8 +8,8 @@
#include <time.h> #include <time.h>
#include "Constants.h" #include "Constants.h"
#include "Serial.h" #include "Serial.h"
#include "Tcp.h"
#include "KISS.h" #include "KISS.h"
#include "TCP.h"
#include "TAP.h" #include "TAP.h"
#define BAUDRATE_DEFAULT 0 #define BAUDRATE_DEFAULT 0
@ -35,9 +35,13 @@ bool noup = false;
bool daemonize = false; bool daemonize = false;
bool set_ipv4 = false; bool set_ipv4 = false;
bool set_netmask = false; bool set_netmask = false;
bool use_net_kiss = false; bool kiss_over_tcp = false;
char* ipv4_addr; char* ipv4_addr;
char* netmask; char* netmask;
char* tcp_host;
int tcp_port;
int mtu; int mtu;
int device_type = IF_TUN; int device_type = IF_TUN;
@ -47,7 +51,7 @@ time_t last_id = 0;
bool tx_since_last_id = false; bool tx_since_last_id = false;
void cleanup(void) { void cleanup(void) {
if (use_net_kiss) { if (kiss_over_tcp) {
close_tcp(attached_tnc); close_tcp(attached_tnc);
} else { } else {
close_port(attached_tnc); close_port(attached_tnc);
@ -257,21 +261,23 @@ void read_loop(void) {
exit(1); exit(1);
} }
const char *argp_program_version = "tncattach 0.1.7"; const char *argp_program_version = "tncattach 0.1.8";
const char *argp_program_bug_address = "<mark@unsigned.io>"; const char *argp_program_bug_address = "<mark@unsigned.io>";
static char doc[] = "\r\nAttach TNC devices as system network interfaces\vTo attach the TNC connected to /dev/ttyUSB0 as an ethernet device with an MTU of 512 bytes and assign an IPv4 address, while filtering IPv6 traffic, use:\r\n\r\n\ttncattach /dev/ttyUSB0 115200 -m 512 -e --noipv6 --ipv4 10.0.0.1/24\r\n\r\nStation identification can be performed automatically to comply with Part 97 rules. See the README for a complete description. Use the --id and --interval options, which should commonly be set to your callsign, and 600 seconds."; static char doc[] = "\r\nAttach TNC devices as system network interfaces\vTo attach the TNC connected to /dev/ttyUSB0 as an ethernet device with an MTU of 512 bytes and assign an IPv4 address, while filtering IPv6 traffic, use:\r\n\r\n\ttncattach /dev/ttyUSB0 115200 -m 512 -e --noipv6 --ipv4 10.0.0.1/24\r\n\r\nStation identification can be performed automatically to comply with Part 97 rules. See the README for a complete description. Use the --id and --interval options, which should commonly be set to your callsign, and 600 seconds.";
static char args_doc[] = "port baudrateOrPort"; static char args_doc[] = "port baudrate";
static struct argp_option options[] = { static struct argp_option options[] = {
{ "mtu", 'm', "MTU", 0, "Specify interface MTU"}, { "mtu", 'm', "MTU", 0, "Specify interface MTU", 1},
{ "daemon", 'd', 0, 0, "Run tncattach as a daemon"}, { "ethernet", 'e', 0, 0, "Create a full ethernet device", 2},
{ "ethernet", 'e', 0, 0, "Create a full ethernet device"}, { "ipv4", 'i', "IP_ADDRESS", 0, "Configure an IPv4 address on interface", 3},
{ "ipv4", 'i', "IP_ADDRESS", 0, "Configure an IPv4 address on interface"}, { "noipv6", 'n', 0, 0, "Filter IPv6 traffic from reaching TNC", 4},
{ "noipv6", 'n', 0, 0, "Filter IPv6 traffic from reaching TNC"}, { "noup", 1, 0, 0, "Only create interface, don't bring it up", 5},
{ "noup", 1, 0, 0, "Only create interface, don't bring it up"}, { "kisstcp", 'T', 0, 0, "Use KISS over TCP instead of serial port", 6},
{ "interval", 't', "SECONDS", 0, "Maximum interval between station identifications"}, { "tcphost", 'H', "TCP_HOST", 0, "Host to connect to when using KISS over TCP", 7},
{ "id", 's', "CALLSIGN", 0, "Station identification data"}, { "tcpport", 'P', "TCP_PORT", 0, "TCP port when using KISS over TCP", 8},
{ "verbose", 'v', 0, 0, "Enable verbose output"}, { "interval", 't', "SECONDS", 0, "Maximum interval between station identifications", 9},
{ "kisstcp", 'o', 0, 0, "Does not use Serial but TCP connexion"}, { "id", 's', "CALLSIGN", 0, "Station identification data", 10},
{ "daemon", 'd', 0, 0, "Run tncattach as a daemon", 11},
{ "verbose", 'v', 0, 0, "Enable verbose output", 12},
{ 0 } { 0 }
}; };
@ -282,7 +288,8 @@ struct arguments {
char *id; char *id;
bool valid_id; bool valid_id;
int id_interval; int id_interval;
int baudrateOrPort; int baudrate;
int tcpport;
int mtu; int mtu;
bool tap; bool tap;
bool daemon; bool daemon;
@ -291,7 +298,9 @@ struct arguments {
bool set_netmask; bool set_netmask;
bool noipv6; bool noipv6;
bool noup; bool noup;
bool useNetKiss; bool kiss_over_tcp;
bool set_tcp_host;
bool set_tcp_port;
}; };
static error_t parse_opt(int key, char *arg, struct argp_state *state) { static error_t parse_opt(int key, char *arg, struct argp_state *state) {
@ -467,8 +476,19 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
arguments->verbose = false; arguments->verbose = false;
break; break;
case 'o': case 'T':
arguments->useNetKiss = true; arguments->kiss_over_tcp = true;
break;
case 'H':
arguments->set_tcp_host = true;
tcp_host = (char*)malloc(strlen(arg)+1);
strcpy(tcp_host, arg);
break;
case 'P':
arguments->set_tcp_port = true;
tcp_port = atoi(arg);
break; break;
case 1: case 1:
@ -485,7 +505,12 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
case ARGP_KEY_END: case ARGP_KEY_END:
// Check if there's too few text arguments // Check if there's too few text arguments
if (state->arg_num < N_ARGS) argp_usage(state); if (!arguments->kiss_over_tcp && state->arg_num < N_ARGS) argp_usage(state);
// Check if text arguments were given when
// KISS over TCP was specified
if (arguments->kiss_over_tcp && state->arg_num != 0) argp_usage(state);
break; break;
default: default:
@ -528,7 +553,7 @@ int main(int argc, char **argv) {
struct arguments arguments; struct arguments arguments;
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
arguments.baudrateOrPort = BAUDRATE_DEFAULT; arguments.baudrate = BAUDRATE_DEFAULT;
arguments.mtu = MTU_DEFAULT; arguments.mtu = MTU_DEFAULT;
arguments.tap = false; arguments.tap = false;
arguments.verbose = false; arguments.verbose = false;
@ -541,7 +566,18 @@ int main(int argc, char **argv) {
arguments.valid_id = false; arguments.valid_id = false;
argp_parse(&argp, argc, argv, 0, 0, &arguments); argp_parse(&argp, argc, argv, 0, 0, &arguments);
arguments.baudrateOrPort = atoi(arguments.args[1]);
if (arguments.kiss_over_tcp) kiss_over_tcp = true;
if (!kiss_over_tcp) {
arguments.baudrate = atoi(arguments.args[1]);
} else {
if (!(arguments.set_tcp_host && arguments.set_tcp_port)) {
if (!arguments.set_tcp_host) printf("Error: KISS over TCP was requested, but no host was specified\r\n");
if (!arguments.set_tcp_port) printf("Error: KISS over TCP was requested, but no port was specified\r\n");
exit(1);
}
}
if (arguments.daemon) daemonize = true; if (arguments.daemon) daemonize = true;
if (arguments.verbose) verbose = true; if (arguments.verbose) verbose = true;
@ -550,7 +586,6 @@ int main(int argc, char **argv) {
if (arguments.set_ipv4) set_ipv4 = true; if (arguments.set_ipv4) set_ipv4 = true;
if (arguments.set_netmask) set_netmask = true; if (arguments.set_netmask) set_netmask = true;
if (arguments.noup) noup = true; if (arguments.noup) noup = true;
if (arguments.useNetKiss) use_net_kiss = true;
mtu = arguments.mtu; mtu = arguments.mtu;
if (arguments.id_interval >= 0) { if (arguments.id_interval >= 0) {
@ -571,24 +606,28 @@ int main(int argc, char **argv) {
attached_if = open_tap(); attached_if = open_tap();
if (arguments.useNetKiss) { if (!arguments.kiss_over_tcp) {
attached_tnc = open_tcp(arguments.args[0], arguments.baudrateOrPort);
} else {
attached_tnc = open_port(arguments.args[0]); attached_tnc = open_port(arguments.args[0]);
if (!setup_port(attached_tnc, arguments.baudrateOrPort)) { if (!setup_port(attached_tnc, arguments.baudrate)) {
printf("Error during serial port setup"); printf("Error during serial port setup");
return 0; return 0;
} }
} else {
attached_tnc = open_tcp(tcp_host, tcp_port);
} }
printf("TNC interface configured as %s\r\n", if_name); printf("TNC interface configured as %s\r\n", if_name);
fds[IF_FD_INDEX].fd = attached_if; fds[IF_FD_INDEX].fd = attached_if;
fds[IF_FD_INDEX].events = POLLIN; fds[IF_FD_INDEX].events = POLLIN;
fds[TNC_FD_INDEX].fd = attached_tnc; fds[TNC_FD_INDEX].fd = attached_tnc;
fds[TNC_FD_INDEX].events = POLLIN; fds[TNC_FD_INDEX].events = POLLIN;
if (daemonize) { if (daemonize) {
become_daemon(); become_daemon();
syslog(LOG_NOTICE, "tncattach daemon running"); syslog(LOG_NOTICE, "tncattach daemon running");
} }
read_loop(); read_loop();
return 0; return 0;