summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9d6415a36fcf78e5e02970a5d63ab1b8b5c3b7d1 (plain)
    1 /**
    2  * A program to execute automation when a socket is unreachable
    3  * Copyright (C) 2022  Aaron Ball <nullspoon@oper.io>
    4  *
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17  */
   18 #include <stdio.h>
   19 #include <string.h>
   20 
   21 #include "net.h"
   22 #include "runconfig.h"
   23 
   24 void usage() {
   25   fputs(
   26     "Usage:\n"
   27     "  react-tcp-down --ip <host_ip> --port <port> --cmd <command>\n\n"
   28     "Arguments:\n"
   29     "  -i,--ip    IP Address to check for port availability\n"
   30     "  -p,--port  Port to check\n"
   31     "  -c,--cmd   Command to run if port is down\n"
   32     "  -h,--help  Print this help text\n"
   33   , stderr);
   34 }
   35 
   36 int parse_args(struct runconfig* cfg, int argc, char* argv[]) {
   37   int i = 0;
   38   int status = 0;
   39   while(i < argc) {
   40     if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
   41       usage();
   42       return 1;
   43     } else if(strcmp(argv[i], "--ip") == 0 || strcmp(argv[i], "-i") == 0) {
   44       i++;
   45       strcpy(cfg->ip, argv[i]);
   46 
   47     } else if(strcmp(argv[i], "--port") == 0 || strcmp(argv[i], "-p") == 0) {
   48       i++;
   49       cfg->port = strtol(argv[i], NULL, 10);
   50 
   51     } else if(strcmp(argv[i], "--cmd") == 0 || strcmp(argv[i], "-c") == 0) {
   52       i++;
   53       strncpy(cfg->cmd, argv[i], RUNCONFIG_CMD_MAX);
   54       cfg->cmd[RUNCONFIG_CMD_MAX - 1] = '\0';
   55     }
   56     i++;
   57   }
   58 
   59   if(cfg->ip[0] == '\0') {
   60     fputs("Host IP address required (--ip, -i)\n", stderr);
   61     status = -1;
   62   } else if(cfg->port <= 0 || cfg->port > 65535) {
   63     fputs("Valid port required (--port, -p)\n", stderr);
   64     status = -1;
   65   } else if(cfg->cmd[0] == '\0') {
   66     fputs("Command to execute on port down required (--cmd,-c)\n", stderr);
   67     status = -1;
   68   }
   69 
   70   return status;
   71 }
   72 
   73 int main(int argc, char* argv[]) {
   74   struct runconfig cfg;
   75 
   76   runconfig_init(&cfg);
   77   if(parse_args(&cfg, argc, argv) != 0)
   78     return 1;
   79 
   80   if(net_tcp_is_up(cfg.ip, cfg.port) == -1)
   81     system(cfg.cmd);
   82   return 0;
   83 }

Generated by cgit