Merge branch 'valentintintin-tcp-kiss'

This commit is contained in:
Mark Qvist 2020-06-24 12:31:17 +02:00
commit 5c8ddcd992
5 changed files with 517 additions and 437 deletions

View File

@ -2,6 +2,9 @@ TNC Attach
==========
Attach KISS TNC devices as network interfaces in Linux. This program allows you to attach TNCs or any KISS-compatible device as a network interface. This program does not need any kernel modules, and has no external dependencies outside the standard Linux and GNU C libraries.
## Version edited
Add capability of using TCP Kiss.
## Installation
Currently it is recommended to compile and install __tncattach__ from source with the below commands.
@ -30,10 +33,11 @@ sudo make install
Using __tncattach__ is simple. Run the program from the command line, specifying which serial port the TNC is connected to, and the serial port baud-rate, and __tncattach__ takes care of the rest. In most cases, depending on what you intend to do, you probably want to use some of the options, though. See the examples section below for usage examples.
```
Usage: tncattach [OPTION...] port baudrate
Usage: tncattach [OPTION...] (serial_port|host) (baudrate|port)
Attach TNC devices as system network interfaces
-o, --kisstcp Use TCP Kiss (such as Direwolf port 8001)
-d, --daemon Run tncattach as a daemon
-e, --ethernet Create a full ethernet device
-i, --ipv4=IP_ADDRESS Configure an IPv4 address on interface
@ -73,6 +77,7 @@ Create an ethernet device with a USB-connected TNC, set the MTU, filter IPv6 tra
```sh
# Attach interface
sudo tncattach /dev/ttyUSB0 115200 --ethernet --mtu 576 --noipv6 --ipv4 10.92.0.10/24
sudo tncattach localhost 8001 -o --ethernet --mtu 576 --noipv6 --ipv4 10.92.0.10/24
```
You can interact with the interface like any other using the __ip__ or __ifconfig__ utilities:
@ -95,6 +100,7 @@ Create a point-to-point link:
```sh
# Attach interface
sudo tncattach /dev/ttyUSB0 115200 --mtu 400 --noipv6 --noup
sudo tncattach localhost 8001 -o --mtu 400 --noipv6 --noup
# Configure IP addresses for point-to-point link
sudo ifconfig tnc0 10.93.0.1 pointopoint 10.93.0.2

36
Tcp.c Normal file
View File

@ -0,0 +1,36 @@
#include "Tcp.h"
int open_tcp(char* ip, int port) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
struct hostent *server;
struct sockaddr_in serv_addr;
server = gethostbyname(ip);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
exit(1);
}
return sockfd;
}
int close_tcp(int fd) {
return close(fd);
}

18
Tcp.h Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include "Constants.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
int open_tcp(char* ip, int port);
int close_tcp(int fd);

View File

@ -14,7 +14,7 @@ clean:
tncattach:
@echo "Making tncattach..."
@echo "Compiling with: ${compiler}"
${compiler} ${flags} tncattach.c Serial.c KISS.c TAP.c -o tncattach -Wall
${compiler} ${flags} tncattach.c Serial.c Tcp.c KISS.c TAP.c -o tncattach -Wall
install:
@echo "Installing tncattach..."

View File

@ -8,6 +8,7 @@
#include <time.h>
#include "Constants.h"
#include "Serial.h"
#include "Tcp.h"
#include "KISS.h"
#include "TAP.h"
@ -34,6 +35,7 @@ bool noup = false;
bool daemonize = false;
bool set_ipv4 = false;
bool set_netmask = false;
bool use_net_kiss = false;
char* ipv4_addr;
char* netmask;
int mtu;
@ -45,7 +47,11 @@ time_t last_id = 0;
bool tx_since_last_id = false;
void cleanup(void) {
if (use_net_kiss) {
close_tcp(attached_tnc);
} else {
close_port(attached_tnc);
}
close_tap(attached_if);
}
@ -254,7 +260,7 @@ void read_loop(void) {
const char *argp_program_version = "tncattach 0.1.7";
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 args_doc[] = "port baudrate";
static char args_doc[] = "port baudrateOrPort";
static struct argp_option options[] = {
{ "mtu", 'm', "MTU", 0, "Specify interface MTU"},
{ "daemon", 'd', 0, 0, "Run tncattach as a daemon"},
@ -265,6 +271,7 @@ static struct argp_option options[] = {
{ "interval", 't', "SECONDS", 0, "Maximum interval between station identifications"},
{ "id", 's', "CALLSIGN", 0, "Station identification data"},
{ "verbose", 'v', 0, 0, "Enable verbose output"},
{ "kisstcp", 'o', 0, 0, "Does not use Serial but TCP connexion"},
{ 0 }
};
@ -275,7 +282,7 @@ struct arguments {
char *id;
bool valid_id;
int id_interval;
int baudrate;
int baudrateOrPort;
int mtu;
bool tap;
bool daemon;
@ -284,6 +291,7 @@ struct arguments {
bool set_netmask;
bool noipv6;
bool noup;
bool useNetKiss;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
@ -459,6 +467,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
arguments->verbose = false;
break;
case 'o':
arguments->useNetKiss = true;
break;
case 1:
arguments->noup = true;
break;
@ -516,7 +528,7 @@ int main(int argc, char **argv) {
struct arguments arguments;
signal(SIGINT, signal_handler);
arguments.baudrate = BAUDRATE_DEFAULT;
arguments.baudrateOrPort = BAUDRATE_DEFAULT;
arguments.mtu = MTU_DEFAULT;
arguments.tap = false;
arguments.verbose = false;
@ -529,7 +541,7 @@ int main(int argc, char **argv) {
arguments.valid_id = false;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
arguments.baudrate = atoi(arguments.args[1]);
arguments.baudrateOrPort = atoi(arguments.args[1]);
if (arguments.daemon) daemonize = true;
if (arguments.verbose) verbose = true;
@ -538,6 +550,7 @@ int main(int argc, char **argv) {
if (arguments.set_ipv4) set_ipv4 = true;
if (arguments.set_netmask) set_netmask = true;
if (arguments.noup) noup = true;
if (arguments.useNetKiss) use_net_kiss = true;
mtu = arguments.mtu;
if (arguments.id_interval >= 0) {
@ -557,8 +570,16 @@ int main(int argc, char **argv) {
}
attached_if = open_tap();
if (arguments.useNetKiss) {
attached_tnc = open_tcp(arguments.args[0], arguments.baudrateOrPort);
} else {
attached_tnc = open_port(arguments.args[0]);
if (setup_port(attached_tnc, arguments.baudrate)) {
if (!setup_port(attached_tnc, arguments.baudrateOrPort)) {
printf("Error during serial port setup");
return 0;
}
}
printf("TNC interface configured as %s\r\n", if_name);
fds[IF_FD_INDEX].fd = attached_if;
fds[IF_FD_INDEX].events = POLLIN;
@ -569,7 +590,6 @@ int main(int argc, char **argv) {
syslog(LOG_NOTICE, "tncattach daemon running");
}
read_loop();
}
return 0;
}