From 4c2f92af2267067c8d4a27fea558bed920571daf Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Tue, 8 Sep 2020 22:49:08 -0400 Subject: [PATCH] Move examples/ax25dump.c to bin/ Changes: * Move examples/ax25dump.c to bin/ax25dump.c; deprecate the other similar program, examples/decode.c * Implement support for reading KISS frames from a file, much in the manner of examples/decode.c --- bin/Makefile | 7 +- {examples => bin}/ax25dump.c | 82 ++++++++++++-------- examples/Makefile | 2 +- examples/decode.c | 140 ----------------------------------- 4 files changed, 58 insertions(+), 173 deletions(-) rename {examples => bin}/ax25dump.c (57%) delete mode 100644 examples/decode.c diff --git a/bin/Makefile b/bin/Makefile index 068e0ad..53d2171 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -7,10 +7,10 @@ INCLUDE_PATH = ../include CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lpatty -PROGRAMS = pattyd +PROGRAMS = pattyd ax25dump MANPAGES = pattyd.8 -OBJS = pattyd.o +OBJS = pattyd.o ax25dump.o all: $(PROGRAMS) @@ -22,6 +22,9 @@ install: $(PROGRAMS) $(MANPAGES) pattyd: pattyd.o $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) +ax25dump: ax25dump.o + $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) + $(OBJS): %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/examples/ax25dump.c b/bin/ax25dump.c similarity index 57% rename from examples/ax25dump.c rename to bin/ax25dump.c index 9515d27..afa4cba 100644 --- a/examples/ax25dump.c +++ b/bin/ax25dump.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -23,60 +24,76 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s /var/run/patty/patty.sock\n", argv[0]); + fprintf(stderr, "usage: %s /var/run/patty/patty.sock ifname\n" + " %s file.cap\n", argv[0], argv[0]); - exit(1); + exit(EX_USAGE); } int main(int argc, char **argv) { patty_client *client; - patty_client_setsockopt_if ifreq; uint8_t buf[4096]; ssize_t readlen; + patty_kiss_tnc_info info; patty_kiss_tnc *raw; - patty_kiss_tnc_info info = { - .flags = PATTY_KISS_TNC_FD - }; + struct stat st; if (argc < 2) { - usage(argc, argv, "No patty socket provided"); - } else if (argc > 2) { - usage(argc, argv, "Too many arguments provided"); + usage(argc, argv, "Not enough arguments provided"); } - if ((client = patty_client_new(argv[1])) == NULL) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "patty_client_new()", argv[1], strerror(errno)); + if (stat(argv[1], &st) < 0) { + fprintf(stderr, "%s: %s: %s\n", argv[0], "stat()", strerror(errno)); - goto error_client_new; + goto error_stat; } - 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)); + if ((st.st_mode & S_IFMT) == S_IFSOCK) { + patty_client_setsockopt_if ifreq; - goto error_client_socket; - } + if (argc < 3) { + usage(argc, argv, "No interface name provided"); + } - strncpy(ifreq.name, "kiss0", sizeof(ifreq.name)); + info.flags = PATTY_KISS_TNC_FD; - ifreq.state = PATTY_AX25_SOCK_PROMISC; + if ((client = patty_client_new(argv[1])) == NULL) { + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], "patty_client_new()", argv[1], strerror(errno)); - 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_new; + } - goto error_client_setsockopt; + 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 { + info.flags = PATTY_KISS_TNC_DEVICE; + info.device = argv[1]; } if ((raw = patty_kiss_tnc_new(&info)) == NULL) { fprintf(stderr, "%s: fd %d: %s: %s\n", - argv[0], info.fd, "patty_kiss_tnc_new_fd()", strerror(errno)); + argv[0], info.fd, "patty_kiss_tnc_new()", strerror(errno)); - goto error_kiss_tnc_new_fd; + goto error_kiss_tnc_new; } while ((readlen = patty_kiss_tnc_recv(raw, buf, sizeof(buf), NULL)) > 0) { @@ -139,20 +156,25 @@ error_ax25_frame_decode_address: patty_kiss_tnc_destroy(raw); - patty_client_close(client, info.fd); + if ((st.st_mode & S_IFMT) == S_IFSOCK) { + patty_client_close(client, info.fd); - patty_client_destroy(client); + patty_client_destroy(client); + } return 0; error_io: patty_kiss_tnc_destroy(raw); -error_kiss_tnc_new_fd: +error_kiss_tnc_new: error_client_setsockopt: error_client_socket: - (void)patty_client_close(client, info.fd); + if ((st.st_mode & S_IFMT) == S_IFSOCK) patty_client_close(client, info.fd); error_client_new: + if ((st.st_mode & S_IFMT) == S_IFSOCK) patty_client_destroy(client); + +error_stat: return 1; } diff --git a/examples/Makefile b/examples/Makefile index a48fe20..4ba28c0 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,7 +7,7 @@ INCLUDE_PATH = ../include CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lpatty -lutil -EXAMPLES = connect listen login ax25dump decode +EXAMPLES = connect listen login all: $(EXAMPLES) diff --git a/examples/decode.c b/examples/decode.c deleted file mode 100644 index dda5299..0000000 --- a/examples/decode.c +++ /dev/null @@ -1,140 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include - -static void usage(int argc, char **argv, const char *message, ...) { - if (message) { - va_list args; - - va_start(args, message); - vfprintf(stderr, message, args); - fprintf(stderr, "\n"); - va_end(args); - } - - fprintf(stderr, "usage: %s [/dev/ttyXX|kiss.cap]\n", argv[0]); - - exit(1); -} - -int main(int argc, char **argv) { - patty_kiss_tnc *tnc; - void *buf; - int port; - - patty_kiss_tnc_info info; - - if (argc > 2) { - usage(argc, argv, NULL); - } - - if (argc == 2) { - info.flags |= PATTY_KISS_TNC_DEVICE; - info.device = argv[1]; - } else { - info.flags |= PATTY_KISS_TNC_FD; - info.fd = 0; - } - - if ((tnc = patty_kiss_tnc_new(&info)) == NULL) { - perror("Unable to open TNC"); - - goto error_kiss_tnc_open; - } - - if ((buf = malloc(PATTY_KISS_BUFSZ)) == NULL) { - perror("malloc()"); - - goto error_malloc_buf; - } - - while (1) { - ssize_t len, - decoded, - offset = 0; - - patty_ax25_frame frame; - - if ((len = patty_kiss_tnc_recv(tnc, buf, PATTY_KISS_BUFSZ, &port)) < 0) { - fprintf(stderr, "%s: %s: %s\n", - argv[0], "patty_kiss_tnc_recv()", strerror(errno)); - - goto error_kiss_tnc_recv; - } else if (len == 0) { - break; - } - - if ((decoded = patty_ax25_frame_decode_address(&frame, buf, len)) < 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, offset, len)) < 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(¶ms, - buf, - offset, - len) < 0) { - printf("Invalid XID parameters\n"); - - goto error_ax25_frame_decode_xid; - } else { - if (patty_print_params(stdout, ¶ms) < 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, len) < 0) { - goto error_io; - } - - if (fflush(stdout) < 0) { - goto error_io; - } - } - - free(buf); - - patty_kiss_tnc_destroy(tnc); - - return 0; - -error_io: -error_kiss_tnc_recv: - free(buf); - -error_malloc_buf: - patty_kiss_tnc_destroy(tnc); - -error_kiss_tnc_open: - return 127; -}