-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdns_checker.c
More file actions
43 lines (35 loc) · 1.06 KB
/
dns_checker.c
File metadata and controls
43 lines (35 loc) · 1.06 KB
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
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "dns_checker.h"
static struct addrinfo hints, *infoptr;
int check_dns() {
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
for (int try = 0; try < DNS_RETRIES; try++) {
log_printf(LOG_INFO, "Try #%d: Resolving DNS.\n", try + 1);
int result = getaddrinfo("github.com", NULL, &hints, &infoptr);
if (result) {
if ( try < DNS_RETRIES ) {
log_printf(LOG_INFO, "getaddrinfo: %s\n Retrying DNS after 5 seconds.\n", gai_strerror(result));
sleep(5);
} else {
log_printf(LOG_ERROR, "getaddrinfo: %s\n", gai_strerror(result));
return -1;
}
} else {
try = DNS_RETRIES;
}
}
log_printf(LOG_DEBUG, "github.com was resolved to:\n");
struct addrinfo *p;
char host[256];
for (p = infoptr; p != NULL; p = p->ai_next) {
getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof (host), NULL, 0, NI_NUMERICHOST);
log_printf(LOG_DEBUG, "%s\n", host);
}
freeaddrinfo(infoptr);
return 0;
}