patty/examples/decode.c
XANTRONIX Development a4518a839a Implement better modulo-128 I and S frame support
Changes:

    * Split patty_ax25_frame_decode() into the following:

      - patty_ax25_frame_decode_address()
      - patty_ax25_frame_decode_control()

      This allows for look up an established socket for a given address
      pair, to determine if an SABME session is in progress,
      necessitating modulo-128 control decoding

    * Decode I and S N(R), N(S) sequence numbers, poll/final
      bits properly in both modulo-8 and modulo-128 mode

    * Perform better frame control validation

    * Implement the following functions in src/sock.c:

      - patty_ax25_sock_send_rr()
      - patty_ax25_sock_send_rnr()
      - patty_ax25_sock_send_rej()
      - patty_ax25_sock_send_srej()

      Corresponding functions have been removed from src/server.c

    * Implement better functions in src/sock.c for encoding frame
      control, with send/receive sequences associated with the socket,
      with modulo-128 encoding for SABME sessions

Other changes:

    * Move or delete macros from include/patty/ax25/macros.h into
      include/patty/ax25.h and include/patty/ax25/frame.h

    * Move definitions from include/patty/ax25/proto.h to header file
      include/patty/ax25.h

    * Perform better bounds checking while decoding frames

    * Improve frame control printing in src/print.c; display frame
      type names, N(R), (NS) sequence numbers, and poll/final bits
2024-03-01 00:20:46 -05:00

104 lines
2.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <patty/ax25.h>
#include <patty/print.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);
}
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;
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,
decoded;
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) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_frame_decode_address()", strerror(errno));
goto error_ax25_frame_decode_address;
}
if (patty_ax25_frame_decode_control(&frame, PATTY_AX25_FRAME_NORMAL, buf, decoded, len) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_frame_decode_control()", strerror(errno));
goto error_ax25_frame_decode_control;
}
if (patty_print_frame(stdout, &frame, buf, len) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_print_frame()", strerror(errno));
goto error_print_frame;
}
}
free(buf);
patty_kiss_tnc_destroy(tnc);
return 0;
error_print_frame:
error_ax25_frame_decode_control:
error_ax25_frame_decode_address:
error_kiss_tnc_recv:
free(buf);
error_malloc_buf:
patty_kiss_tnc_destroy(tnc);
error_kiss_tnc_open:
return 127;
}