176 lines
4.5 KiB
C
176 lines
4.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
|
|
#include <patty/ax25.h>
|
|
#include <patty/print.h>
|
|
|
|
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 localcall\n", argv[0]);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
struct sockaddr_un addr;
|
|
patty_ax25_call_setsockopt_if ifreq;
|
|
|
|
int fd,
|
|
sock;
|
|
|
|
patty_ax25_addr peer;
|
|
char path[PATTY_AX25_SOCK_PATH_SIZE];
|
|
|
|
uint8_t buf[4096];
|
|
ssize_t readlen;
|
|
|
|
patty_kiss_tnc *raw;
|
|
|
|
if (argc < 2) {
|
|
usage(argc, argv, "No patty socket provided");
|
|
} else if (argc < 3) {
|
|
usage(argc, argv, "No local callsign provided");
|
|
} else if (argc > 3) {
|
|
usage(argc, argv, "Too many arguments provided");
|
|
}
|
|
|
|
patty_ax25_pton(argv[2], 0, &peer);
|
|
|
|
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
|
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
argv[0], "socket()", argv[1], strerror(errno));
|
|
|
|
goto error_socket;
|
|
}
|
|
|
|
memset(&addr, '\0', sizeof(addr));
|
|
addr.sun_family = AF_UNIX;
|
|
strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
goto error_connect;
|
|
}
|
|
|
|
if ((sock = patty_ax25_call_socket(fd, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_RAW, path, sizeof(path))) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_socket()", strerror(errno));
|
|
|
|
goto error_call_socket;
|
|
}
|
|
|
|
strncpy(ifreq.name, "kiss0", sizeof(ifreq.name));
|
|
|
|
if (patty_ax25_call_setsockopt(fd, sock, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_setsockopt()", strerror(errno));
|
|
|
|
goto error_call_setsockopt;
|
|
}
|
|
|
|
if ((raw = patty_kiss_tnc_new(path)) == NULL) {
|
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
argv[0], path, "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) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_frame_decode_address()", strerror(errno));
|
|
|
|
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) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_frame_decode_control()", strerror(errno));
|
|
|
|
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) {
|
|
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, ¶ms) < 0) {
|
|
goto error_io;
|
|
}
|
|
}
|
|
|
|
if (patty_print_hexdump(stdout, buf, readlen) < 0) {
|
|
goto error_io;
|
|
}
|
|
|
|
if (fflush(stdout) < 0) {
|
|
goto error_io;
|
|
}
|
|
}
|
|
|
|
patty_kiss_tnc_destroy(raw);
|
|
|
|
patty_ax25_call_close(fd, sock);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
error_io:
|
|
error_ax25_frame_decode_xid:
|
|
error_ax25_frame_decode_control:
|
|
error_ax25_frame_decode_address:
|
|
patty_kiss_tnc_destroy(raw);
|
|
|
|
error_kiss_tnc_new:
|
|
patty_ax25_call_close(fd, sock);
|
|
|
|
error_call_setsockopt:
|
|
error_call_socket:
|
|
close(fd);
|
|
|
|
error_connect:
|
|
error_socket:
|
|
return 1;
|
|
}
|