summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2022-06-28 17:23:10 -0600
committerAaron Ball <nullspoon@oper.io>2022-06-28 17:23:10 -0600
commit2cc6b1f841df303f0e83b9e0bf53a669bbe01d1a (patch)
treed6ab5ef1b4d461ebc0f049400655758f7dac204d /src
parente5ac2786534cd456e5a2acc8efbb9b6fb473d6ed (diff)
downloadreact-socket-down-2cc6b1f841df303f0e83b9e0bf53a669bbe01d1a.tar.gz
react-socket-down-2cc6b1f841df303f0e83b9e0bf53a669bbe01d1a.tar.xz
Better organize code
Move network code into new `net` library. This will allow better organization of code when udp and unix socket support is added. This also cleans up the main includes as it doesn't need any of the networking libraries. This also moves the argument parsing into the new `parse_args` function to clean up main a bit.
Diffstat (limited to 'src')
-rw-r--r--src/main.c40
-rw-r--r--src/net.c35
-rw-r--r--src/net.h23
3 files changed, 72 insertions, 26 deletions
diff --git a/src/main.c b/src/main.c
index 4023646..6535d5c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,33 +15,11 @@
* 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 <stdio.h>
-#include <stdlib.h> // system
-#include <sys/socket.h> // socket
-#include <arpa/inet.h> // inet_addr
-#include <unistd.h> // close()
-
-int tcp_is_up(char* ip, int port) {
- struct sockaddr_in addr;
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- int status = -1;
-
- // Instantiate the sockaddr_in
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(ip);
- status = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
-
- close(sock);
- return status;
-}
+#include "net.h"
-int main(int argc, char* argv[]) {
- char* host = NULL;
- int port = -1;
- char* cmd = NULL;
+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) {
@@ -51,14 +29,24 @@ int main(int argc, char* argv[]) {
} else if(argc == 3) {
fputs("Command to execute on port down required\n", stderr);
}
- return 1;
+ return 0;
}
+ return 1;
+}
+
+int main(int argc, char* argv[]) {
+ char* host = NULL;
+ int port = -1;
+ char* cmd = NULL;
+
+ if(!parse_args(argc, argv))
+ return 1;
host = argv[1];
port = strtol(argv[2], NULL, 10);
cmd = argv[3];
- if(tcp_is_up(host, port) == -1)
+ if(net_tcp_is_up(host, port) == -1)
system(cmd);
return 0;
}
diff --git a/src/net.c b/src/net.c
new file mode 100644
index 0000000..59944e9
--- /dev/null
+++ b/src/net.c
@@ -0,0 +1,35 @@
+/**
+ * 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 "net.h"
+
+int net_tcp_is_up(char* ip, int port) {
+ struct sockaddr_in addr;
+ int sock = socket(AF_INET, SOCK_STREAM, 0);
+ int status = -1;
+
+ // Instantiate the sockaddr_in
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ addr.sin_addr.s_addr = inet_addr(ip);
+
+ status = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
+
+ close(sock);
+ return status;
+}
+
diff --git a/src/net.h b/src/net.h
new file mode 100644
index 0000000..4fcfd8d
--- /dev/null
+++ b/src/net.h
@@ -0,0 +1,23 @@
+/**
+ * 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 <stdlib.h> // system
+#include <sys/socket.h> // socket
+#include <arpa/inet.h> // inet_addr
+#include <unistd.h> // close()
+
+int net_tcp_is_up(char*, int);

Generated by cgit