summaryrefslogtreecommitdiff
path: root/src/net.c
blob: 59944e9269d889b5563c85facf5b0cc7edb6f1f1 (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 "net.h"
   19 
   20 int net_tcp_is_up(char* ip, int port) {
   21   struct sockaddr_in addr;
   22   int sock   = socket(AF_INET, SOCK_STREAM, 0);
   23   int status = -1;
   24 
   25   // Instantiate the sockaddr_in
   26   addr.sin_family      = AF_INET;
   27   addr.sin_port        = htons(port);
   28   addr.sin_addr.s_addr = inet_addr(ip);
   29 
   30   status = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
   31 
   32   close(sock);
   33   return status;
   34 }

Generated by cgit