diff options
author | Aaron Ball <nullspoon@oper.io> | 2022-06-28 17:24:46 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2022-06-28 17:24:46 -0600 |
commit | b4cf60ac026b61f48287c1dbfc021edca7e8abe0 (patch) | |
tree | d6ab5ef1b4d461ebc0f049400655758f7dac204d | |
parent | e5ac2786534cd456e5a2acc8efbb9b6fb473d6ed (diff) | |
parent | 2cc6b1f841df303f0e83b9e0bf53a669bbe01d1a (diff) | |
download | react-socket-down-b4cf60ac026b61f48287c1dbfc021edca7e8abe0.tar.gz react-socket-down-b4cf60ac026b61f48287c1dbfc021edca7e8abe0.tar.xz |
Merge branch 'organize'
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/main.c | 40 | ||||
-rw-r--r-- | src/net.c | 35 | ||||
-rw-r--r-- | src/net.h | 23 |
4 files changed, 75 insertions, 27 deletions
@@ -1,4 +1,6 @@ CCOPTS = -std=gnu18 -Wall -Werror -O2 all: - cc $(CCOPTS) src/main.c -o react-socket-down + if [ ! -d obj ]; then mkdir obj; fi + cc $(CCOPTS) src/net.c -c -o obj/net.o + cc $(CCOPTS) src/main.c obj/*.o -o react-socket-down @@ -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); |