summaryrefslogtreecommitdiff
path: root/netcheck.c
blob: 4900e0f3f66d9a0e014fbf31bca2dcb4be238585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Created by Keuin on 2021/12/29.
//

#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "logging.h"
#include "netcheck.h"

/**
 * Check network availability.
 * @return Zero if success, non-zero if failed.
 */
int check_network(void *logger) {
#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
}