2015-09-17 21:52:09 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <patty/kiss.h>
|
|
|
|
#include <patty/ax25.h>
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:22:42 -04:00
|
|
|
fprintf(stderr, "usage: %s [/dev/ttyXX|kiss.cap]\n", argv[0]);
|
2015-09-17 21:52:09 -05:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-06-07 02:46:12 -04:00
|
|
|
static int callsign_fprint(FILE *stream, const patty_ax25_addr *addr) {
|
|
|
|
char buf[7];
|
|
|
|
uint8_t ssid;
|
2015-09-17 21:52:09 -05:00
|
|
|
|
2020-06-07 02:46:12 -04:00
|
|
|
if (patty_ax25_ntop(addr, buf, &ssid, sizeof(buf)) < 0) {
|
|
|
|
goto error_ntop;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stream, "%s", buf);
|
|
|
|
|
|
|
|
if (ssid) {
|
|
|
|
fprintf(stream, "/%d", ssid);
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-06-07 02:46:12 -04:00
|
|
|
|
|
|
|
error_ntop:
|
|
|
|
return -1;
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
#define printable(c) \
|
|
|
|
(c >= 0x20 && c < 0x7f)
|
|
|
|
|
2020-05-22 21:14:22 -04:00
|
|
|
static int hexdump_fprint(FILE *stream, void *data, size_t len) {
|
|
|
|
size_t i;
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
for (i=0; i<len; i+=16) {
|
|
|
|
size_t x;
|
|
|
|
|
|
|
|
if (fprintf(stream, "%08lx:", i) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x=0; x<16; x++) {
|
|
|
|
if (!(x & 1)) {
|
|
|
|
if (fprintf(stream, " ") < 0) {
|
|
|
|
goto error_io;
|
2020-05-22 21:14:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
if (i+x < len) {
|
|
|
|
if (fprintf(stream, "%02x", ((uint8_t *)data)[i+x]) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fprintf(stream, " ") < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
2020-05-22 21:14:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
if (fprintf(stream, " ") < 0) {
|
|
|
|
goto error_io;
|
2020-05-22 21:14:22 -04:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
for (x=0; x<16 && i+x<len; x++) {
|
|
|
|
uint8_t c = ((uint8_t *)data)[i+x];
|
|
|
|
|
|
|
|
if (fputc(printable(c)? c: '.', stream) < 0) {
|
|
|
|
goto error_io;
|
2020-05-22 21:14:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
if (fprintf(stream, "\n") < 0) {
|
|
|
|
goto error_io;
|
2020-05-22 21:14:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-05-22 22:31:04 -04:00
|
|
|
error_io:
|
2020-05-22 21:14:22 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-17 21:52:09 -05:00
|
|
|
static int address_fprint(FILE *stream, const patty_ax25_frame *frame) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
callsign_fprint(stream, &frame->src);
|
2020-05-22 20:18:59 -04:00
|
|
|
|
2015-09-17 21:52:09 -05:00
|
|
|
fprintf(stream, " > ");
|
|
|
|
|
|
|
|
for (i=0; i<frame->hops; i++) {
|
|
|
|
callsign_fprint(stream, &frame->repeaters[i]);
|
2020-05-22 20:18:59 -04:00
|
|
|
fprintf(stream, " > ");
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
2020-05-22 20:18:59 -04:00
|
|
|
callsign_fprint(stream, &frame->dest);
|
|
|
|
|
2015-09-17 21:52:09 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-23 11:05:32 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-27 23:54:09 -04:00
|
|
|
if (PATTY_AX25_CONTROL_INFO(frame->control)) {
|
2020-05-30 23:54:11 -04:00
|
|
|
if (fprintf(stream, " type I poll %d ns %d nr %d info %zu",
|
2020-05-23 11:05:32 -04:00
|
|
|
PATTY_AX25_CONTROL_POLL(frame->control),
|
|
|
|
PATTY_AX25_CONTROL_SEQ_SEND(frame->control),
|
2020-05-27 18:16:06 -04:00
|
|
|
PATTY_AX25_CONTROL_SEQ_RECV(frame->control),
|
2020-06-27 23:54:09 -04:00
|
|
|
frame->infolen) < 0) {
|
2020-05-27 18:16:06 -04:00
|
|
|
goto error_io;
|
|
|
|
}
|
2020-06-27 23:54:09 -04:00
|
|
|
} else if (PATTY_AX25_CONTROL_UNNUMBERED(frame->control)) {
|
2020-05-30 23:54:11 -04:00
|
|
|
if (fprintf(stream, " type U info %zu",
|
2020-06-27 23:54:09 -04:00
|
|
|
frame->infolen) < 0) {
|
2020-05-23 11:05:32 -04:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:16:06 -04:00
|
|
|
if (fprintf(stream, " total %zu bytes\n", len) < 0) {
|
2020-05-23 11:05:32 -04:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
2020-05-31 00:43:17 -04:00
|
|
|
if (frame->info) {
|
2020-06-27 23:54:09 -04:00
|
|
|
if (hexdump_fprint(stream, frame->info, frame->infolen) < 0) {
|
2020-05-31 00:43:17 -04:00
|
|
|
goto error_io;
|
|
|
|
}
|
2020-05-23 11:05:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_io:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-17 21:52:09 -05:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
patty_kiss_tnc *tnc;
|
2020-05-27 19:11:25 -04:00
|
|
|
void *buf;
|
2015-09-17 21:52:09 -05:00
|
|
|
int port;
|
|
|
|
|
2020-05-27 22:22:42 -04:00
|
|
|
if (argc > 2) {
|
|
|
|
usage(argc, argv, NULL);
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 22:22:42 -04:00
|
|
|
tnc = (argc == 2)?
|
2020-06-26 22:44:19 -04:00
|
|
|
patty_kiss_tnc_new(argv[1]):
|
|
|
|
patty_kiss_tnc_new_fd(0);
|
2020-05-27 22:22:42 -04:00
|
|
|
|
|
|
|
if (tnc == NULL) {
|
2015-09-17 21:52:09 -05:00
|
|
|
perror("Unable to open TNC");
|
|
|
|
|
2020-05-23 11:05:32 -04:00
|
|
|
goto error_kiss_tnc_open;
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 19:11:25 -04:00
|
|
|
if ((buf = malloc(PATTY_KISS_BUFSZ)) == NULL) {
|
|
|
|
perror("malloc()");
|
|
|
|
|
|
|
|
goto error_malloc_buf;
|
|
|
|
}
|
|
|
|
|
2020-05-27 00:22:50 -04:00
|
|
|
while (1) {
|
|
|
|
ssize_t len;
|
2015-09-17 21:52:09 -05:00
|
|
|
patty_ax25_frame frame;
|
|
|
|
|
2020-05-27 19:11:25 -04:00
|
|
|
if ((len = patty_kiss_tnc_recv(tnc, buf, PATTY_KISS_BUFSZ, &port)) < 0) {
|
2020-05-27 00:22:50 -04:00
|
|
|
perror("Unable to read frame");
|
|
|
|
|
2020-05-27 19:11:25 -04:00
|
|
|
goto error_kiss_tnc_recv;
|
2020-05-27 00:22:50 -04:00
|
|
|
} else if (len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-27 23:54:09 -04:00
|
|
|
if (patty_ax25_frame_decode(&frame, PATTY_AX25_FRAME_NORMAL, buf, len) < 0) {
|
2015-09-17 21:52:09 -05:00
|
|
|
perror("Unable to decode frame");
|
|
|
|
|
2020-05-23 11:05:32 -04:00
|
|
|
goto error_ax25_frame_decode;
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 19:11:25 -04:00
|
|
|
if (frame_fprint(stdout, &frame, buf, len) < 0) {
|
2020-05-23 11:05:32 -04:00
|
|
|
perror("Unable to print frame");
|
2015-09-17 21:52:09 -05:00
|
|
|
|
2020-05-23 11:05:32 -04:00
|
|
|
goto error_frame_fprint;
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:11:25 -04:00
|
|
|
free(buf);
|
|
|
|
|
2020-06-26 22:44:19 -04:00
|
|
|
patty_kiss_tnc_destroy(tnc);
|
2015-09-17 21:52:09 -05:00
|
|
|
|
|
|
|
return 0;
|
2020-05-23 11:05:32 -04:00
|
|
|
|
|
|
|
error_frame_fprint:
|
|
|
|
error_ax25_frame_decode:
|
2020-05-27 19:11:25 -04:00
|
|
|
error_kiss_tnc_recv:
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
error_malloc_buf:
|
2020-06-26 22:44:19 -04:00
|
|
|
patty_kiss_tnc_destroy(tnc);
|
2020-05-23 11:05:32 -04:00
|
|
|
|
|
|
|
error_kiss_tnc_open:
|
|
|
|
return 127;
|
2015-09-17 21:52:09 -05:00
|
|
|
}
|