From 881f5b63c5adc288ae71b67dee72df6c87a4f55e Mon Sep 17 00:00:00 2001 From: lxsang Date: Fri, 17 Sep 2021 17:36:51 +0200 Subject: [PATCH] add syslog backend support --- Makefile.am | 2 +- build.json | 73 ++++++++++++++ configure.ac | 1 + syslog/Makefile.am | 12 +++ syslog/syslog.c | 237 +++++++++++++++++++++++++++++++++++++++++++++ wfifo/wfifo.c | 2 +- 6 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 build.json create mode 100644 syslog/Makefile.am create mode 100644 syslog/syslog.c diff --git a/Makefile.am b/Makefile.am index 3e5948e..52cff3d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,7 +15,7 @@ install-data-local: EXTRA_DIST = runner.ini runnerd tunnel.h antd-tunnel-publisher.service log.h -SUBDIRS = . vterm wfifo +SUBDIRS = . vterm wfifo syslog if ENABLE_CAM SUBDIRS += v4l2cam diff --git a/build.json b/build.json new file mode 100644 index 0000000..c14297d --- /dev/null +++ b/build.json @@ -0,0 +1,73 @@ +{ + "name": "antd-tunnel-publishers", + "targets":{ + "configure": { + "require": ["linux"], + "jobs": [ + { + "name": "linux-exec", + "data": { + "cmd":"libtoolize", + "pwd": "home://workspace/antd-tunnel-publishers" + } + }, + { + "name": "linux-exec", + "data": { + "cmd":"aclocal", + "pwd": "home://workspace/antd-tunnel-publishers" + } + }, + { + "name": "linux-exec", + "data": { + "cmd":"autoconf", + "pwd": "home://workspace/antd-tunnel-publishers" + } + }, + { + "name": "linux-exec", + "data": { + "cmd":"automake --add-missing", + "pwd": "home://workspace/antd-tunnel-publishers" + } + }, + { + "name": "linux-exec", + "data": { + "cmd":"./configure --prefix=/usr --enable-debug=no", + "pwd": "home://workspace/antd-tunnel-publishers" + } + } + ] + }, + "clean": { + "require": ["linux"], + "jobs": [ + { + "name": "linux-exec", + "data": { + "cmd":"make clean", + "pwd": "home://workspace/antd-tunnel-publishers" + } + } + ] + }, + "build": { + "require": ["linux"], + "jobs": [ + { + "name": "linux-exec", + "data": { + "cmd":"make", + "pwd": "home://workspace/antd-tunnel-publishers" + } + } + ] + }, + "clean and build": { + "depend": ["clean", "build"], + "jobs": [] + } + } +} \ No newline at end of file diff --git a/configure.ac b/configure.ac index 9f42580..809aac0 100644 --- a/configure.ac +++ b/configure.ac @@ -88,6 +88,7 @@ AC_CONFIG_FILES([ Makefile vterm/Makefile wfifo/Makefile + syslog/Makefile ]) if test x"${cam_enable}" == x"yes" ; then diff --git a/syslog/Makefile.am b/syslog/Makefile.am new file mode 100644 index 0000000..ed12551 --- /dev/null +++ b/syslog/Makefile.am @@ -0,0 +1,12 @@ +AUTOMAKE_OPTIONS = foreign + + + +AM_CPPFLAGS = -W -Wall -g -std=c99 + +# bin +bin_PROGRAMS = syslogb +# source files +syslogb_SOURCES = syslog.c ../tunnel.c +syslogb_CPPFLAGS= -I../ +# antd_LDADD = libantd.la diff --git a/syslog/syslog.c b/syslog/syslog.c new file mode 100644 index 0000000..7059ec0 --- /dev/null +++ b/syslog/syslog.c @@ -0,0 +1,237 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../tunnel.h" + +#define MODULE_NAME "syslogb" + +static bst_node_t *clients = NULL; + +static volatile int running = 1; + +static void int_handler(int dummy) +{ + (void)dummy; + running = 0; +} +static void send_data(bst_node_t *node, void **argv, int argc) +{ + (void)argc; + tunnel_msg_t *msg = (tunnel_msg_t *)argv[0]; + int *fd = (int *)argv[1]; + msg->header.client_id = node->key; + if (msg_write(*fd, msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to write data message to client %d", node->key); + } +} +static void unsubscribe(bst_node_t *node, void **args, int argc) +{ + (void)argc; + tunnel_msg_t msg; + int *ufd = (int *)args[0]; + msg.header.type = CHANNEL_UNSUBSCRIBE; + msg.header.client_id = node->key; + msg.header.size = 0; + if (msg_write(*ufd, &msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to request unsubscribe to client %d", node->key); + } +} +int main(int argc, char **argv) +{ + int fd, sock_fd; + tunnel_msg_t msg; + fd_set fd_in; + int status, maxfd, length; + char buff[BUFFLEN + 1]; + void *fargv[2]; + struct sockaddr_un saddr, caddr; + uint8_t *tmp; + LOG_INIT(MODULE_NAME); + + if (argc != 4) + { + printf("Usage: %s path/to/hotline/socket channel_name input_file\n", argv[0]); + return -1; + } + signal(SIGPIPE, SIG_IGN); + signal(SIGABRT, SIG_IGN); + signal(SIGINT, int_handler); + // create the unix domain socket + (void)unlink(argv[3]); + if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { + M_ERROR(MODULE_NAME, "Unable to create socket %s: %s", argv[3], strerror(errno)); + return -1; + } + + /* bind to the socket path */ + saddr.sun_family = AF_UNIX; + snprintf(saddr.sun_path, (strlen(argv[3])+1), "%s", argv[3]); + + if (0 != (bind(sock_fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_un)))) { + M_ERROR(MODULE_NAME, "Unable to bind socket %s: %s", argv[3], strerror(errno)); + (void) close(sock_fd); + return -1; + } + (void)chmod(argv[3], 0777); + + M_LOG(MODULE_NAME, "Unix domain socket: %s created", argv[3]); + M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]); + // now try to request new channel from hotline + fd = open_unix_socket(argv[1]); + if (fd == -1) + { + M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]); + (void)close(sock_fd); + return -1; + } + msg.header.type = CHANNEL_OPEN; + msg.header.channel_id = 0; + msg.header.client_id = 0; + M_LOG(MODULE_NAME, "Request to open the channel %s", argv[2]); + (void)strncpy(buff, argv[2], MAX_CHANNEL_NAME); + msg.header.size = strlen(buff); + msg.data = (uint8_t *)buff; + if (msg_write(fd, &msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to write message to hotline"); + (void)close(fd); + (void)close(sock_fd); + return -1; + } + M_LOG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]); + // now wait for message + if (msg_read(fd, &msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to read message from hotline"); + (void)close(fd); + (void)close(sock_fd); + return -1; + } + if (msg.header.type == CHANNEL_OK) + { + M_LOG(MODULE_NAME, "Channel created: %s", argv[2]); + if (msg.data) + free(msg.data); + } + else + { + M_ERROR(MODULE_NAME, "Channel is not created: %s. Tunnel service responds with msg of type %d", argv[2], msg.header.type); + if (msg.data) + free(msg.data); + running = 0; + } + + length = sizeof(struct sockaddr_un); + // now read data + while (running) + { + FD_ZERO(&fd_in); + FD_SET(fd, &fd_in); + FD_SET(sock_fd, &fd_in); + maxfd = fd > sock_fd ? fd : sock_fd; + + status = select(maxfd + 1, &fd_in, NULL, NULL, NULL); + + switch (status) + { + case -1: + M_LOG(MODULE_NAME, "Error %d on select()\n", errno); + running = 0; + break; + case 0: + break; + // we have data + default: + if (FD_ISSET(fd, &fd_in)) + { + if (msg_read(fd, &msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to read message from channel. quit"); + running = 0; + } + else + { + switch (msg.header.type) + { + case CHANNEL_SUBSCRIBE: + M_LOG(MODULE_NAME, "Client %d subscribes to the chanel", msg.header.client_id); + clients = bst_insert(clients, msg.header.client_id, NULL); + break; + + case CHANNEL_UNSUBSCRIBE: + M_LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", msg.header.client_id); + clients = bst_delete(clients, msg.header.client_id); + break; + + default: + M_LOG(MODULE_NAME, "Client %d send message of type %d", + msg.header.client_id, msg.header.type); + break; + } + if (msg.data) + { + free(msg.data); + } + } + } + else if (FD_ISSET(sock_fd, &fd_in)) + { + // read data to buffer + memset(buff, 0, sizeof(buff)); + status = recvfrom(sock_fd, buff, sizeof(buff) -1 , 0, (struct sockaddr *)&caddr, &length); + // on the fifo side + if (status == -1) + { + M_ERROR(MODULE_NAME, "Unable to read data from the UDP socket %s: %s", argv[3], strerror(errno)); + running = 0; + } + else + { + msg.header.type = CHANNEL_DATA; + msg.header.size = status; + msg.data = (uint8_t *)buff; + fargv[0] = (void *)&msg; + fargv[1] = (void *)&fd; + bst_for_each(clients, send_data, fargv, 2); + } + } + } + } + // unsubscribe all client + fargv[0] = (void *)&fd; + bst_for_each(clients, unsubscribe, fargv, 1); + + // close the channel + M_LOG(MODULE_NAME, "Close the channel %s (%d)", argv[2], fd); + msg.header.type = CHANNEL_CLOSE; + msg.header.size = 0; + msg.data = NULL; + if (msg_write(fd, &msg) == -1) + { + M_ERROR(MODULE_NAME, "Unable to request channel close"); + } + // close all opened terminal + + (void)msg_read(fd, &msg); + (void)close(fd); + (void)close(sock_fd); + return 0; +} \ No newline at end of file diff --git a/wfifo/wfifo.c b/wfifo/wfifo.c index b87c248..cdca67d 100644 --- a/wfifo/wfifo.c +++ b/wfifo/wfifo.c @@ -65,7 +65,7 @@ int main(int argc, char **argv) if (argc != 5) { - printf("Usage: %s path/to/hotline/socket channel_name input_file\n", argv[0]); + printf("Usage: %s path/to/hotline/socket channel_name input_file r/w\n", argv[0]); return -1; } signal(SIGPIPE, SIG_IGN);