summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2022-06-28 17:55:13 -0600
committerAaron Ball <nullspoon@oper.io>2022-06-28 17:55:13 -0600
commit650f7ba01614f59a3e1e862d9f05b6866072c913 (patch)
tree6d03b309d66c7b0ea955469dcc6b638524ee2848
parentb4cf60ac026b61f48287c1dbfc021edca7e8abe0 (diff)
parent51097c5c2b60558b4b3419cd5c5fe0dbedca5935 (diff)
downloadreact-socket-down-650f7ba01614f59a3e1e862d9f05b6866072c913.tar.gz
react-socket-down-650f7ba01614f59a3e1e862d9f05b6866072c913.tar.xz
Merge branch 'flexible-argv'
-rw-r--r--Makefile1
-rw-r--r--src/main.c62
-rw-r--r--src/runconfig.c24
-rw-r--r--src/runconfig.h29
4 files changed, 95 insertions, 21 deletions
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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
+#include <string.h>
#include "net.h"
+#include "runconfig.h"
-int parse_args(int argc, char* argv[]) {
- if(argc < 4) {
- fputs("Usage:\n react-tcp-down <host_ip> <port> <command>\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 <host_ip> <port> <command>\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 <nullspoon@oper.io>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+#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 <nullspoon@oper.io>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+#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*);

Generated by cgit