From 1cccfca835938fa8a56a8eafaeb78f46891d914c Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 15 Jul 2015 21:16:39 -0500 Subject: [PATCH] KEEN REFACTORING YES VERY MUCH THANK YOU MA'AM --- src/kiss.c | 76 ++++++++++++++++++++++++++++++++++-------------------- src/test.c | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 src/test.c diff --git a/src/kiss.c b/src/kiss.c index 79a92ca..903ba77 100644 --- a/src/kiss.c +++ b/src/kiss.c @@ -92,19 +92,10 @@ error_io: return -1; } -ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { +static ssize_t tnc_decode(patty_kiss_tnc *tnc, void *frame, size_t *len, int *port) { size_t i, b; int flags = KISS_NONE; - if (tnc_buffer(tnc) < 0) { - goto error_io; - } - - /* - * Initialize the frame to be returned to the caller. - */ - memset(tnc->frame, '\0', tnc->bufsz); - /* * The default port is always zero, unless a command byte issued from the * TNC in the first byte of the frame body. @@ -144,9 +135,9 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { if (flags & KISS_ESCAPE) { if (c == PATTY_KISS_TFEND) { - ((unsigned char *)tnc->frame)[b++] = PATTY_KISS_FEND; + ((unsigned char *)frame)[b++] = PATTY_KISS_FEND; } else if (c == PATTY_KISS_TFESC) { - ((unsigned char *)tnc->frame)[b++] = PATTY_KISS_FESC; + ((unsigned char *)frame)[b++] = PATTY_KISS_FESC; } else { errno = EIO; @@ -167,7 +158,7 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { break; } - ((unsigned char *)tnc->frame)[b++] = c; + ((unsigned char *)frame)[b++] = c; } } @@ -175,32 +166,61 @@ ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { * Return empty-handed if we do not have a fully book-ended frame. */ if ((flags & KISS_FRAME) == 0) { - *frame = NULL; + *len = 0; + *port = 0; return 0; } - if (i) { - /* - * As we have indeed found a frame at this point, move everything from - * the buffer not processed up to this point, to the beginning of the - * buffer. - */ - memmove(tnc->buf, ((unsigned char *)tnc->buf) + i, tnc->buflen - i); + *len = b; - /* - * And decrement the buffer length by the number of bytes already - * processed. - */ - tnc->buflen -= i; + return i; + +error_io: + return -1; +} + +static void tnc_flush(patty_kiss_tnc *tnc, size_t len) { + /* + * Move everything from the buffer not processed up to this point, to the + * beginning of the buffer. + */ + memmove(tnc->buf, ((unsigned char *)tnc->buf) + len, tnc->buflen - len); + + /* + * Then, decrement the buffer length by the number of bytes already + * processed. + */ + tnc->buflen -= len; +} + +ssize_t patty_kiss_tnc_recv(patty_kiss_tnc *tnc, void **frame, int *port) { + ssize_t decoded; + size_t framelen; + + /* + * Fill the buffer with something to parse! + */ + if (tnc_buffer(tnc) < 0) { + goto error_io; } /* - * Set the output parameter to the frame buffer. + * Initialize the frame to be returned to the caller. */ + memset(tnc->frame, '\0', tnc->bufsz); + + if ((decoded = tnc_decode(tnc, tnc->frame, &framelen, port)) < 0) { + goto error_io; + } + + if (decoded) { + tnc_flush(tnc, decoded); + } + *frame = tnc->frame; - return b; + return framelen; error_io: return -1; diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..6df0558 --- /dev/null +++ b/src/test.c @@ -0,0 +1,49 @@ +#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 kiss.cap\n", argv[0]); + + exit(1); +} + +int main(int argc, char **argv) { + patty_kiss_tnc *tnc; + ssize_t len; + void *frame; + int port; + + if (argc != 2) { + usage(argc, argv, "No TNC device provided"); + } + + if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) { + perror("Unable to open TNC"); + + exit(127); + } + + while ((len = patty_kiss_tnc_recv(tnc, &frame, &port)) > 0) { + write(fileno(stdout), frame, len); + + fprintf(stderr, "Got %ld byte frame\n", len); + } + + patty_kiss_tnc_close(tnc); + + return 0; +}