Last active
September 20, 2016 14:35
-
-
Save bboozzoo/f9a3021d15410132da17065d5ce8a2d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -D_GNU_SOURCE -o dnstest dnstest.c */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <netdb.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <sys/time.h> | |
#include <arpa/nameser.h> | |
#include <resolv.h> | |
#define log(...) do { \ | |
struct timeval tv; \ | |
gettimeofday(&tv, NULL); \ | |
fprintf(stderr, "%u:%u ", tv.tv_sec, tv.tv_usec); \ | |
fprintf(stderr, __VA_ARGS__); \ | |
} while(0) | |
void show_help(const char *name) { | |
fprintf(stderr, "usage: %s [-w <tmout>] <host>\n", | |
basename(name)); | |
} | |
int main(int argc, char *argv[]) { | |
int tmwait = 500000; | |
const char *host = NULL; | |
int c = 0; | |
while ((c = getopt(argc, argv, "w:h")) != -1) { | |
log("optind %d c %d\n", optind, c); | |
switch (c) { | |
case 'w': | |
tmwait = atoi(optarg); | |
if (tmwait == 0) { | |
log("error: wait time conversion failed\n"); | |
exit(1); | |
} | |
break; | |
case '?': | |
show_help(argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
} | |
if (optind >= argc) { | |
log("error: host not provided\n"); | |
show_help(argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
host = argv[optind]; | |
log("using host: %s\n", host); | |
while (1) { | |
res_init(); | |
struct addrinfo *res = NULL; | |
int err = getaddrinfo(host, NULL, NULL, &res); | |
if (err != 0) { | |
log("failed to resolve: %s\n", gai_strerror(err)); | |
} else { | |
const char *name = inet_ntoa(((struct sockaddr_in *)res->ai_addr)->sin_addr); | |
log("resolved to: %s\n", name); | |
if (res) | |
freeaddrinfo(res); | |
} | |
usleep(tmwait); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment