patty/bin/ax25dump.c
XANTRONIX Development cb33d799ff Implement patty_ax25_if_driver
Changes:

    * Implement patty_ax25_if_driver type, providing a vtable with
      pointers to methods implementing an AX.25 interface PHY

    * Implement patty_ax25_if_name() to return a pointer to the name
      string of an AX.25 interface

    * Decouple patty_kiss_tnc from src/if.c using patty_ax25_if_driver

    * Remove port input/output arguments from patty_kiss_tnc_send()
      and patty_kiss_tnc_recv(), respectively; use 0 as the default,
      but keep the port argument in patty_kiss_frame_send()

    * Implement patty_ax25_if_fd() to return file descriptor backing a
      PHY; use this rather than patty_kiss_tnc_fd() in src/server.c to
      further decouple interfaces from their implementation

    * Remove 'enum patty_ax25_if_type' type; refactor constructor
      patty_ax25_if_new() to no longer take this as an argument, but
      rather a patty_ax25_if_driver to use to instantiate the PHY
      with the information pointer passed

    * Break out patty_kiss_tnc code from src/kiss.c into src/tnc.c,
      leaving only patty_kiss_frame_send() in the original; this is
      needed to prevent a cyclic dependency within patty_ax25_sock and
      other areas

    * Rename patty_bin_if_config() to patty_bin_if_create(); a separate
      patty_bin_if_config() will likely be created later as necessary
      to change an existing interface

    * Split PHY-specific interface configuration code into separate
      delegates in bin/if.c

    * Implement usage of patty_error to better capture internal error
      states not currently representable with semantic error numbers
      while configuring and instantiating PHYs

    * Pass patty_error object to patty_bin_kiss_config() to receive
      detailed error information
2024-03-01 00:20:47 -05:00

203 lines
5.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <sysexits.h>
#include <patty/ax25.h>
#include <patty/print.h>
#include <patty/bin/kiss.h>
static void usage(int argc, char **argv, const char *message, ...) {
if (message != NULL) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
fprintf(stderr, "usage: %s /var/run/patty/patty.sock ifname\n"
" %s /dev/ttyXYZ [tioarg ...]\n"
" %s file.cap\n", argv[0], argv[0], argv[0]);
exit(EX_USAGE);
}
int main(int argc, char **argv) {
patty_client *client = NULL;
struct option opts[] = {
{ NULL, 0, NULL, 0 }
};
uint8_t buf[4096];
ssize_t readlen;
patty_kiss_tnc_info info;
patty_kiss_tnc *raw;
struct stat st;
int ch;
if ((ch = getopt_long(argc, argv, "", opts, NULL)) >= 0) {
usage(argc, argv, NULL);
}
if (argc < 2) {
usage(argc, argv, "Not enough arguments provided");
}
if (stat(argv[1], &st) < 0) {
fprintf(stderr, "%s: %s: %s\n", argv[0], "stat()", strerror(errno));
goto error_stat;
}
memset(&info, '\0', sizeof(info));
if ((st.st_mode & S_IFMT) == S_IFSOCK) {
patty_client_setsockopt_if ifreq;
if (argc < 3) {
usage(argc, argv, "No interface name provided");
}
info.flags = PATTY_KISS_TNC_FD;
if ((client = patty_client_new(argv[1])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "patty_client_new()", argv[1], strerror(errno));
goto error_client_new;
}
if ((info.fd = patty_client_socket(client, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_RAW)) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_socket()", strerror(errno));
goto error_client_socket;
}
strncpy(ifreq.name, argv[2], sizeof(ifreq.name));
ifreq.state = PATTY_AX25_SOCK_PROMISC;
if (patty_client_setsockopt(client, info.fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_setsockopt()", strerror(errno));
goto error_client_setsockopt;
}
} else {
patty_error e;
if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[1], patty_error_string(&e));
goto error_kiss_config;
}
}
if ((raw = patty_kiss_tnc_new(&info)) == NULL) {
fprintf(stderr, "%s: fd %d: %s: %s\n",
argv[0], info.fd, "patty_kiss_tnc_new()", strerror(errno));
goto error_kiss_tnc_new;
}
while ((readlen = patty_kiss_tnc_recv(raw, buf, sizeof(buf))) > 0) {
ssize_t decoded,
offset = 0;
patty_ax25_frame frame;
if ((decoded = patty_ax25_frame_decode_address(&frame, buf, readlen)) < 0) {
printf("Invalid frame address\n");
goto error_ax25_frame_decode_address;
} else {
offset += decoded;
}
if ((decoded = patty_ax25_frame_decode_control(&frame, PATTY_AX25_FRAME_NORMAL, buf, decoded, readlen)) < 0) {
printf("Invalid frame control\n");
goto error_ax25_frame_decode_control;
} else {
offset += decoded;
}
if (patty_print_frame_header(stdout, &frame) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_print_frame_header()", strerror(errno));
goto error_io;
}
if (frame.type == PATTY_AX25_FRAME_XID) {
patty_ax25_params params;
if (patty_ax25_frame_decode_xid(&params,
buf,
offset,
readlen) < 0) {
printf("Invalid XID parameters\n");
goto error_ax25_frame_decode_xid;
} else {
if (patty_print_params(stdout, &params) < 0) {
goto error_io;
}
}
}
error_ax25_frame_decode_xid:
error_ax25_frame_decode_control:
error_ax25_frame_decode_address:
if (patty_print_hexdump(stdout, buf, readlen) < 0) {
goto error_io;
}
if (fflush(stdout) < 0) {
goto error_io;
}
}
patty_kiss_tnc_destroy(raw);
if ((st.st_mode & S_IFMT) == S_IFSOCK) {
patty_client_close(client, info.fd);
patty_client_destroy(client);
}
return 0;
error_io:
patty_kiss_tnc_destroy(raw);
error_kiss_tnc_new:
error_kiss_config:
error_client_setsockopt:
error_client_socket:
if (client) patty_client_close(client, info.fd);
error_client_new:
if (client) patty_client_destroy(client);
error_stat:
return 1;
}