patty/src/kiss.c

95 lines
2.1 KiB
C
Raw Normal View History

#define _GNU_SOURCE
#include <stdlib.h>
2015-07-16 00:31:27 +00:00
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
2015-07-14 16:45:03 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
2015-07-14 16:45:03 +00:00
#include <errno.h>
#include <patty/kiss.h>
Implement bin/pattyd.c Changes: * Implement src/conf.c, patty_conf_read(), to read a configuration file to support a OpenBSD-style configuration file format * Implement bin/pattyd.c to use patty_conf_read() to read a configuration file and apply its settings to a patty_daemon object as it is read; also implement a --standalone|-s flag to allow the user to start a patty server without having to write a configuration file * Refactor patty_daemon_if_add() to accept a patty_ax25_if object; this is necessary as bin/pattyd.c needs to be able to validate the addresses given in a configuration file 'if' statement * Refactor patty_ax25_server_new() to no longer accept a client socket path; instead, patty_ax25_server_start() now accepts the client socket path * Remove the client socket 'path' member from patty_ax25_server in src/server.c * Refactor patty_kiss_tnc_new() to accept only one argument, a patty_kiss_tnc_info object containing flags and settings needed to open a device, use an existing file descriptor, or change termios settings as appropriate * Remove patty_kiss_tnc_new_fd(), as its functionality now exists in patty_kiss_tnc_new() itself * Add a 'flags' field to patty_kiss_tnc_info; use this bit field to determine whether a path or file descriptor is provided by the caller * Make patty_ax25_if_new() accept an interface name argument, as names are explicitly required when declaring new interfaces in configuration files * Make patty_kiss_tnc_new() able to accept /dev/ptmx as a device name, regardless of whether this character device exists on a given platform; when provided, a pseudo TTY pair is allocated with openpty() * Refactor examples/ax25dump.c to use the new patty_kiss_tnc_new() calling convention * Refactor examples/decode.c to use the new patty_kiss_tnc_new() calling convention * Remove examples/daemon.c in favor of bin/pattyd.c * Rename examples/patty.conf to examples/pattyd.conf; modify to provide values which would actually function
2020-09-01 16:38:02 -05:00
#include "config.h"
static inline ssize_t write_byte(int fd, uint8_t c) {
return write(fd, &c, sizeof(c));
}
static inline ssize_t write_start(int fd,
enum patty_kiss_command command,
int port) {
uint8_t start[2] = {
PATTY_KISS_FEND,
((port & 0x0f) << 4) | (command & 0x0f)
};
return write(fd, start, sizeof(start));
}
static uint8_t escape_fend[2] = { PATTY_KISS_FESC, PATTY_KISS_TFEND };
static uint8_t escape_fesc[2] = { PATTY_KISS_FESC, PATTY_KISS_TFESC };
ssize_t patty_kiss_frame_send(int fd,
const void *buf,
size_t len,
int port) {
size_t i, start = 0, end = 0;
if (write_start(fd, PATTY_KISS_DATA, port) < 0) {
goto error_io;
}
for (i=0; i<len; i++) {
uint8_t c = ((uint8_t *)buf)[i];
uint8_t *escape = NULL;
switch (c) {
case PATTY_KISS_FEND:
escape = escape_fend;
break;
case PATTY_KISS_FESC:
escape = escape_fesc;
break;
default:
end = i + 1;
break;
}
if (escape) {
if (write(fd, ((uint8_t *)buf) + start, end - start) < 0) {
goto error_io;
}
if (write(fd, escape, 2) < 0) {
goto error_io;
}
escape = NULL;
start = i + 1;
end = start;
}
}
if (end - start) {
if (write(fd, ((uint8_t *)buf) + start, end - start) < 0) {
goto error_io;
}
}
if (write_byte(fd, PATTY_KISS_FEND) < 0) {
goto error_io;
}
return len;
error_io:
return -1;
}