summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 6535d5c6d08060827bf9a51a2f95fc4039191dda (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 
   20 #include "net.h"
   21 
   22 int parse_args(int argc, char* argv[]) {
   23   if(argc < 4) {
   24     fputs("Usage:\n  react-tcp-down <host_ip> <port> <command>\n\n", stderr);
   25     if(argc == 1) {
   26       fputs("Host IP address required\n", stderr);
   27     } else if(argc == 2) {
   28       fputs("Port required\n", stderr);
   29     } else if(argc == 3) {
   30       fputs("Command to execute on port down required\n", stderr);
   31     }
   32     return 0;
   33   }
   34   return 1;
   35 }
   36 
   37 int main(int argc, char* argv[]) {
   38   char* host = NULL;
   39   int port   = -1;
   40   char* cmd  = NULL;
   41 
   42   if(!parse_args(argc, argv))
   43     return 1;
   44 
   45   host = argv[1];
   46   port = strtol(argv[2], NULL, 10);
   47   cmd  = argv[3];
   48 
   49   if(net_tcp_is_up(host, port) == -1)
   50     system(cmd);
   51   return 0;
   52 }

Generated by cgit