diff --git a/src/Makefile b/src/Makefile index 8480580..0003998 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,7 +14,8 @@ HEADERS = kiss.h ax25.h ax25/if.h ax25/macros.h ax25/proto.h \ OBJS = kiss.o ax25.o if.o call.o frame.o sock.o route.o server.o \ list.o hash.o dict.o print.o -EXAMPLES = testserver testclient-connect testclient-listen decode +EXAMPLES = testserver testclient-connect testclient-listen testclient-promisc \ + decode HEADERS_BUILD = $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/, $(HEADERS)) diff --git a/src/testclient-promisc.c b/src/testclient-promisc.c new file mode 100644 index 0000000..4fe24ab --- /dev/null +++ b/src/testclient-promisc.c @@ -0,0 +1,114 @@ +#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\n", argv[0]); + + exit(1); +} + +int main(int argc, char **argv) { + struct sockaddr_un addr; + + int fd, + sock, + pty; + + patty_ax25_addr peer; + char path[PATTY_AX25_SOCK_PATH_SIZE]; + + uint8_t buf[4096]; + ssize_t readlen; + + if (argc != 2) { + usage(argc, argv, "No patty socket provided"); + } + + patty_ax25_pton("KZ3ROX", 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_openif(fd, "kiss0", path, sizeof(path))) < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "patty_ax25_call_openif()", strerror(errno)); + + goto error_call_socket; + } + + if ((pty = open(path, O_RDWR)) < 0) { + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], path, "open()", strerror(errno)); + + goto error_open_pty; + } + + while ((readlen = read(pty, buf, sizeof(buf))) > 0) { + patty_ax25_frame frame; + + if (patty_ax25_frame_decode(&frame, PATTY_AX25_FRAME_NORMAL, buf, readlen) < 0) { + perror("Unable to decode frame"); + + goto error_ax25_frame_decode; + } + + if (patty_print_frame(stdout, &frame, buf, readlen) < 0) { + perror("Unable to print frame"); + + goto error_print_frame; + } + } + + close(pty); + + patty_ax25_call_close(fd, sock); + + close(fd); + + return 0; + +error_print_frame: +error_ax25_frame_decode: +error_open_pty: + patty_ax25_call_close(fd, sock); + +error_call_socket: + close(fd); + +error_connect: +error_socket: + return 1; +}