diff options
author | Keuin <[email protected]> | 2021-12-29 23:57:56 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2021-12-29 23:57:56 +0800 |
commit | 5651bc231389ebfa05e3750a431b33c28f4ed205 (patch) | |
tree | 7da4fe5de375454a721cef839d33bac284284bb5 /netmon.c | |
parent | fdad8c8db14c87d5a64226e9372b0db7d79bcbfc (diff) |
Move `check_network` to separate files.
Diffstat (limited to 'netmon.c')
-rw-r--r-- | netmon.c | 89 |
1 files changed, 2 insertions, 87 deletions
@@ -1,13 +1,11 @@ #include "logging.h" +#include "netcheck.h" #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/stat.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> const char *logfile = "netmon.log"; int check_interval_seconds = 30; // seconds to sleep between checks @@ -88,89 +86,6 @@ void daemonize() { // } //} -/** - * Check network availability. - * @return Zero if success, non-zero if failed. - */ -int check_network() { -#define RETURN(r) do { rv = (r); goto RET; } while(0) -#define READ_SIZE 32 - - int sock = -1, rv = 0; - struct sockaddr_in serv_addr; - const char *msg = "GET / HTTP/1.1\r\n" - "Host: www.gov.cn\r\n" - "\r\n"; - - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("socket()"); - log_error(logger, "socket() failed."); - RETURN(-1); - } - - const struct hostent *host = gethostbyname("www.gov.cn"); // TODO cache this - - if (!host) { - herror("gethostbyname()"); - log_error(logger, "Cannot resolve test host."); - RETURN(-1); - } - - if (host->h_length <= 0) { - log_error(logger, "Test host does not have at least one address."); - RETURN(-1); - } - - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(80); - serv_addr.sin_addr = **((struct in_addr **) host->h_addr_list); - - if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - log_error(logger, "connect() failed."); - RETURN(-1); - } - - ssize_t ss = 0; // send size - while (ss < strlen(msg)) { - ssize_t rd = send(sock, msg, strlen(msg), 0); - if (rd < 0) { - perror("send()"); - log_error(logger, "send() failed."); - RETURN(-1); - } - ss += rd; - } - - log_debug(logger, "HTTP request is sent. Reading response."); - char response[READ_SIZE] = {0}; - ssize_t rs = 0; // receive size - while (rs < READ_SIZE) { - ssize_t rd = read(sock, response, READ_SIZE - rs); - if (rd < 0) { - perror("read()"); - log_error(logger, "read() failed."); - RETURN(-1); - } else { - rs += rd; - } - } - - const char *expected = "HTTP/1.1 200 OK\r\n"; - if (memcmp(response, expected, strlen(expected)) != 0) { - char buf[64]; - snprintf(buf, 63, "Unexpected response: %s", response); - log_error(logger, buf); - RETURN(-1); - } - RETURN(0); - - RET: - if (sock >= 0) close(sock); - return rv; -#undef RETURN -#undef READ_SIZE -} - void loop() { // check network and sleep // if fails too many times continuously, set require_reboot flag and return @@ -179,7 +94,7 @@ void loop() { #pragma ide diagnostic ignored "EndlessLoop" while (1) { log_info(logger, "Check network."); - if (check_network() != 0) { + if (check_network(logger) != 0) { ++failures; char buf[64]; snprintf(buf, 63, "Network failure detected. counter=%d", failures); |