#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); } static int callsign_fprint(FILE *stream, const patty_ax25_addr *addr) { char buf[7]; uint8_t ssid; if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) { goto error_ntop; } fprintf(stream, "%s", buf); if (ssid) { fprintf(stream, "/%d", ssid); } return 0; error_ntop: return -1; } #define printable(c) \ (c >= 0x20 && c < 0x7f) static int hexdump_fprint(FILE *stream, void *data, size_t len) { size_t i; for (i=0; isrc); fprintf(stream, " > "); for (i=0; ihops; i++) { callsign_fprint(stream, &frame->repeaters[i]); fprintf(stream, " > "); } callsign_fprint(stream, &frame->dest); return 0; } static int frame_fprint(FILE *stream, const patty_ax25_frame *frame, void *data, size_t len) { if (address_fprint(stream, frame) < 0) { goto error_io; } if (frame->type == PATTY_AX25_FRAME_INFO) { if (fprintf(stream, " type I poll %d ns %d nr %d info %zu", PATTY_AX25_CONTROL_POLL(frame->control), PATTY_AX25_CONTROL_SEQ_SEND(frame->control), PATTY_AX25_CONTROL_SEQ_RECV(frame->control), frame->len) < 0) { goto error_io; } } else if (frame->type == PATTY_AX25_FRAME_UNNUMBERED) { if (fprintf(stream, " type U info %zu", frame->len) < 0) { goto error_io; } } if (fprintf(stream, " total %zu bytes\n", len) < 0) { goto error_io; } if (frame->info) { if (hexdump_fprint(stream, frame->info, frame->len) < 0) { goto error_io; } } return 0; error_io: return -1; } int main(int argc, char **argv) { patty_kiss_tnc *tnc; void *buf; int port; if (argc > 2) { usage(argc, argv, NULL); } tnc = (argc == 2)? patty_kiss_tnc_new(argv[1]): patty_kiss_tnc_new_fd(0); if (tnc == 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; patty_ax25_frame frame; if ((len = patty_kiss_tnc_recv(tnc, buf, PATTY_KISS_BUFSZ, &port)) < 0) { perror("Unable to read frame"); goto error_kiss_tnc_recv; } else if (len == 0) { break; } if (patty_ax25_frame_decode(&frame, buf, len) < 0) { perror("Unable to decode frame"); goto error_ax25_frame_decode; } if (frame_fprint(stdout, &frame, buf, len) < 0) { perror("Unable to print frame"); goto error_frame_fprint; } } free(buf); patty_kiss_tnc_destroy(tnc); return 0; error_frame_fprint: error_ax25_frame_decode: error_kiss_tnc_recv: free(buf); error_malloc_buf: patty_kiss_tnc_destroy(tnc); error_kiss_tnc_open: return 127; }