From 51097c5c2b60558b4b3419cd5c5fe0dbedca5935 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Tue, 28 Jun 2022 17:53:17 -0600 Subject: Add runconfig code This adds a new struct, runconfig, which handles runtime configurations. It essentially stores the validated arguments passed by the user at runtime. Right now, this separation of code is a bit overkill, but later this will support things like unix and udp sockets, as well as additional configurations such as running in foreground and retry cooldown times. --- Makefile | 1 + src/main.c | 62 ++++++++++++++++++++++++++++++++++++++------------------- src/runconfig.c | 24 ++++++++++++++++++++++ src/runconfig.h | 29 +++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 src/runconfig.c create mode 100644 src/runconfig.h diff --git a/Makefile b/Makefile index 1d12d2c..566b06b 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,5 @@ CCOPTS = -std=gnu18 -Wall -Werror -O2 all: if [ ! -d obj ]; then mkdir obj; fi cc $(CCOPTS) src/net.c -c -o obj/net.o + cc $(CCOPTS) src/runconfig.c -c -o obj/runconfig.o cc $(CCOPTS) src/main.c obj/*.o -o react-socket-down diff --git a/src/main.c b/src/main.c index 6535d5c..3a9245d 100644 --- a/src/main.c +++ b/src/main.c @@ -16,37 +16,57 @@ * along with this program. If not, see . */ #include +#include #include "net.h" +#include "runconfig.h" -int parse_args(int argc, char* argv[]) { - if(argc < 4) { - fputs("Usage:\n react-tcp-down \n\n", stderr); - if(argc == 1) { - fputs("Host IP address required\n", stderr); - } else if(argc == 2) { - fputs("Port required\n", stderr); - } else if(argc == 3) { - fputs("Command to execute on port down required\n", stderr); +void usage() { + fputs("Usage:\n react-tcp-down \n\n", stderr); +} + +int parse_args(struct runconfig* cfg, int argc, char* argv[]) { + int i = 0; + int status = 0; + while(i < argc) { + if(strcmp(argv[i], "--ip") == 0 || strcmp(argv[i], "-i") == 0) { + i++; + strcpy(cfg->ip, argv[i]); + + } else if(strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-p") == 0) { + i++; + cfg->port = strtol(argv[i], NULL, 10); + + } else if(strcmp(argv[i], "--cmd") == 0 || strcmp(argv[i], "-c") == 0) { + i++; + strncpy(cfg->cmd, argv[i], RUNCONFIG_CMD_MAX); + cfg->cmd[RUNCONFIG_CMD_MAX - 1] = '\0'; } - return 0; + i++; } - return 1; + + if(cfg->ip[0] == '\0') { + fputs("Host IP address required (--ip, -i)\n", stderr); + status = -1; + } else if(cfg->port <= 0 || cfg->port > 65535) { + fputs("Valid port required (--port, -p)\n", stderr); + status = -1; + } else if(cfg->cmd[0] == '\0') { + fputs("Command to execute on port down required (--cmd,-c)\n", stderr); + status = -1; + } + + return status; } int main(int argc, char* argv[]) { - char* host = NULL; - int port = -1; - char* cmd = NULL; + struct runconfig cfg; - if(!parse_args(argc, argv)) + runconfig_init(&cfg); + if(parse_args(&cfg, argc, argv) != 0) return 1; - host = argv[1]; - port = strtol(argv[2], NULL, 10); - cmd = argv[3]; - - if(net_tcp_is_up(host, port) == -1) - system(cmd); + if(net_tcp_is_up(cfg.ip, cfg.port) == -1) + system(cfg.cmd); return 0; } diff --git a/src/runconfig.c b/src/runconfig.c new file mode 100644 index 0000000..31d9e99 --- /dev/null +++ b/src/runconfig.c @@ -0,0 +1,24 @@ +/** + * A program to execute automation when a socket is unreachable + * Copyright (C) 2022 Aaron Ball + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "runconfig.h" + +void runconfig_init(struct runconfig* cfg) { + memset(cfg->ip, '\0', RUNCONFIG_IP_MAX); + memset(cfg->cmd, '\0', RUNCONFIG_CMD_MAX); + cfg->port = -1; +} diff --git a/src/runconfig.h b/src/runconfig.h new file mode 100644 index 0000000..9d8d80d --- /dev/null +++ b/src/runconfig.h @@ -0,0 +1,29 @@ +/** + * A program to execute automation when a socket is unreachable + * Copyright (C) 2022 Aaron Ball + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +#define RUNCONFIG_IP_MAX 64 // 64 to handle ipv4 and ipv6 +#define RUNCONFIG_CMD_MAX 2048 // Seems a reasonable max length + +struct runconfig { + char ip[RUNCONFIG_IP_MAX]; + int port; + char cmd[RUNCONFIG_CMD_MAX]; +}; + +void runconfig_init(struct runconfig*); -- cgit v1.2.3