patty/examples/decode.c

134 lines
3.2 KiB
C
Raw Normal View History

2015-09-17 21:52:09 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2015-09-17 21:52:09 -05:00
#include <patty/ax25.h>
#include <patty/print.h>
2015-09-17 21:52:09 -05:00
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]);
2015-09-17 21:52:09 -05:00
exit(1);
}
int main(int argc, char **argv) {
patty_kiss_tnc *tnc;
void *buf;
2015-09-17 21:52:09 -05:00
int port;
if (argc > 2) {
usage(argc, argv, NULL);
2015-09-17 21:52:09 -05:00
}
tnc = (argc == 2)?
patty_kiss_tnc_new(argv[1]):
patty_kiss_tnc_new_fd(0);
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
}
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,
decoded,
offset = 0;
2015-09-17 21:52:09 -05:00
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));
2020-05-27 00:22:50 -04:00
goto error_kiss_tnc_recv;
2020-05-27 00:22:50 -04:00
} else if (len == 0) {
break;
}
if ((decoded = patty_ax25_frame_decode_address(&frame, buf, len)) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_frame_decode_address()", strerror(errno));
2015-09-17 21:52:09 -05:00
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) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_frame_decode_control()", strerror(errno));
goto error_ax25_frame_decode_control;
} else {
offset += decoded;
2015-09-17 21:52:09 -05:00
}
if (patty_print_frame_header(stdout, &frame) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_print_frame_header()", strerror(errno));
goto error_print;
}
if (frame.type == PATTY_AX25_FRAME_XID) {
patty_ax25_params params;
if (patty_ax25_frame_decode_xid(&params,
buf,
offset,
len) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_frame_decode_xid()", strerror(errno));
goto error_ax25_frame_decode_xid;
}
if (patty_print_params(stdout, &params) < 0) {
goto error_print;
}
}
2015-09-17 21:52:09 -05:00
if (patty_print_hexdump(stdout, buf, len) < 0) {
goto error_print;
2015-09-17 21:52:09 -05:00
}
}
free(buf);
patty_kiss_tnc_destroy(tnc);
2015-09-17 21:52:09 -05:00
return 0;
2020-05-23 11:05:32 -04:00
error_print:
error_ax25_frame_decode_xid:
error_ax25_frame_decode_control:
error_ax25_frame_decode_address:
error_kiss_tnc_recv:
free(buf);
error_malloc_buf:
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
}