/
net.c
75 lines (63 loc) · 2.27 KB
/
net.c
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
/*
* net.c
* Network functions for vpncwatch
* Author: David Cantrell <david.l.cantrell@gmail.com>
*
* Adapted from vpnc-watch.py by Gary Benson <gbenson@redhat.com>
* (Python is TOO BIG for a 16M OpenWRT router.)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include "vpncwatch.h"
int is_network_up(char *chkhost, char *chkport) {
int sfd, s;
struct addrinfo *result, *rp;
struct addrinfo hints;
/* don't do a network check if the user didn't specify a host */
if (chkhost == NULL || chkport == NULL) {
return 1;
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = IPPROTO_TCP;
/* get the server address */
if ((s = getaddrinfo(chkhost, chkport, &hints, &result)) != 0) {
syslog(LOG_ERR, "Error getting addr info: %s", gai_strerror(s));
}
/* try list of addresses returned by getaddrinfo */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sfd);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
syslog(LOG_ERR, "Could not connect");
return 0;
}
return 1;
}