#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void usage(int argc, char **argv, const char *message, ...) { if (message != NULL) { va_list args; va_start(args, message); vfprintf(stderr, message, args); fprintf(stderr, "\n"); va_end(args); } fprintf(stderr, "usage: %s /var/run/patty/patty.sock ifname\n" " %s /dev/ttyXYZ [tioargs ...]\n" " %s file.cap\n", argv[0], argv[0], argv[0]); exit(EX_USAGE); } int main(int argc, char **argv) { patty_client *client; struct option opts[] = { { NULL, 0, NULL, 0 } }; uint8_t buf[4096]; ssize_t readlen; patty_bin_kiss_data data; patty_kiss_tnc_info *info = &data.info; patty_kiss_tnc *raw; struct stat st; int ch; if ((ch = getopt_long(argc, argv, "", opts, NULL)) >= 0) { usage(argc, argv, NULL); } if (argc < 2) { usage(argc, argv, "Not enough arguments provided"); } if (stat(argv[1], &st) < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], "stat()", strerror(errno)); goto error_stat; } memset(&data, '\0', sizeof(data)); if ((st.st_mode & S_IFMT) == S_IFSOCK) { patty_client_setsockopt_if ifreq; if (argc < 3) { usage(argc, argv, "No interface name provided"); } info->flags = PATTY_KISS_TNC_FD; if ((client = patty_client_new(argv[1])) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "patty_client_new()", argv[1], strerror(errno)); goto error_client_new; } if ((info->fd = patty_client_socket(client, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_RAW)) < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], "patty_client_socket()", strerror(errno)); goto error_client_socket; } strncpy(ifreq.name, argv[2], sizeof(ifreq.name)); ifreq.state = PATTY_AX25_SOCK_PROMISC; if (patty_client_setsockopt(client, info->fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], "patty_client_setsockopt()", strerror(errno)); goto error_client_setsockopt; } } else { if (patty_bin_kiss_config(&data, argc - 1, argv + 1) < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], data.err); goto error_kiss_config; } } if ((raw = patty_kiss_tnc_new(info)) == NULL) { fprintf(stderr, "%s: fd %d: %s: %s\n", argv[0], info->fd, "patty_kiss_tnc_new()", strerror(errno)); goto error_kiss_tnc_new; } while ((readlen = patty_kiss_tnc_recv(raw, buf, sizeof(buf), NULL)) > 0) { ssize_t decoded, offset = 0; patty_ax25_frame frame; if ((decoded = patty_ax25_frame_decode_address(&frame, buf, readlen)) < 0) { printf("Invalid frame address\n"); goto error_ax25_frame_decode_address; } else { offset += decoded; } if ((decoded = patty_ax25_frame_decode_control(&frame, PATTY_AX25_FRAME_NORMAL, buf, decoded, readlen)) < 0) { printf("Invalid frame control\n"); goto error_ax25_frame_decode_control; } else { offset += decoded; } if (patty_print_frame_header(stdout, &frame) < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], "patty_print_frame_header()", strerror(errno)); goto error_io; } if (frame.type == PATTY_AX25_FRAME_XID) { patty_ax25_params params; if (patty_ax25_frame_decode_xid(¶ms, buf, offset, readlen) < 0) { printf("Invalid XID parameters\n"); goto error_ax25_frame_decode_xid; } else { if (patty_print_params(stdout, ¶ms) < 0) { goto error_io; } } } error_ax25_frame_decode_xid: error_ax25_frame_decode_control: error_ax25_frame_decode_address: if (patty_print_hexdump(stdout, buf, readlen) < 0) { goto error_io; } if (fflush(stdout) < 0) { goto error_io; } } patty_kiss_tnc_destroy(raw); if ((st.st_mode & S_IFMT) == S_IFSOCK) { patty_client_close(client, info->fd); patty_client_destroy(client); } return 0; error_io: patty_kiss_tnc_destroy(raw); error_kiss_tnc_new: error_kiss_config: error_client_setsockopt: error_client_socket: if ((st.st_mode & S_IFMT) == S_IFSOCK) patty_client_close(client, info->fd); error_client_new: if ((st.st_mode & S_IFMT) == S_IFSOCK) patty_client_destroy(client); error_stat: return 1; }