mirror of
https://github.com/libevent/libevent.git
synced 2025-01-31 09:12:55 +08:00
dns-example: convert to getopt()
This commit is contained in:
parent
07b5e45ba5
commit
32f8592c8b
@ -12,9 +12,14 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifdef EVENT__HAVE_UNISTD_H
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
|
#include <getopt.h>
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
@ -141,34 +146,31 @@ logfn(int is_warn, const char *msg) {
|
|||||||
|
|
||||||
int
|
int
|
||||||
main(int c, char **v) {
|
main(int c, char **v) {
|
||||||
int idx;
|
struct options {
|
||||||
int reverse = 0, servertest = 0, use_getaddrinfo = 0;
|
int reverse;
|
||||||
|
int use_getaddrinfo;
|
||||||
|
int servertest;
|
||||||
|
const char *resolv_conf;
|
||||||
|
} o = { };
|
||||||
|
char opt;
|
||||||
struct event_base *event_base = NULL;
|
struct event_base *event_base = NULL;
|
||||||
struct evdns_base *evdns_base = NULL;
|
struct evdns_base *evdns_base = NULL;
|
||||||
const char *resolv_conf = NULL;
|
|
||||||
if (c<2) {
|
if (c < 2) {
|
||||||
fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] hostname\n", v[0]);
|
fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] hostname\n", v[0]);
|
||||||
fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
|
fprintf(stderr, "syntax: %s [-T]\n", v[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
idx = 1;
|
|
||||||
while (idx < c && v[idx][0] == '-') {
|
while ((opt = getopt(c, v, "xvc:T")) != -1) {
|
||||||
if (!strcmp(v[idx], "-x"))
|
switch (opt) {
|
||||||
reverse = 1;
|
case 'x': o.reverse = 1; break;
|
||||||
else if (!strcmp(v[idx], "-v"))
|
case 'v': ++verbose; break;
|
||||||
verbose = 1;
|
case 'g': o.use_getaddrinfo = 1; break;
|
||||||
else if (!strcmp(v[idx], "-g"))
|
case 'T': o.servertest = 1; break;
|
||||||
use_getaddrinfo = 1;
|
case 'c': o.resolv_conf = optarg; break;
|
||||||
else if (!strcmp(v[idx], "-servertest"))
|
default : fprintf(stderr, "Unknown option %c\n", opt); break;
|
||||||
servertest = 1;
|
}
|
||||||
else if (!strcmp(v[idx], "-c")) {
|
|
||||||
if (idx + 1 < c)
|
|
||||||
resolv_conf = v[++idx];
|
|
||||||
else
|
|
||||||
fprintf(stderr, "-c needs an argument\n");
|
|
||||||
} else
|
|
||||||
fprintf(stderr, "Unknown option %s\n", v[idx]);
|
|
||||||
++idx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -182,7 +184,7 @@ main(int c, char **v) {
|
|||||||
evdns_base = evdns_base_new(event_base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
|
evdns_base = evdns_base_new(event_base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
|
||||||
evdns_set_log_fn(logfn);
|
evdns_set_log_fn(logfn);
|
||||||
|
|
||||||
if (servertest) {
|
if (o.servertest) {
|
||||||
evutil_socket_t sock;
|
evutil_socket_t sock;
|
||||||
struct sockaddr_in my_addr;
|
struct sockaddr_in my_addr;
|
||||||
sock = socket(PF_INET, SOCK_DGRAM, 0);
|
sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
@ -200,16 +202,15 @@ main(int c, char **v) {
|
|||||||
}
|
}
|
||||||
evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL);
|
evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL);
|
||||||
}
|
}
|
||||||
if (idx < c) {
|
if (optind < c) {
|
||||||
int res;
|
int res;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
if (resolv_conf == NULL)
|
if (o.resolv_conf == NULL)
|
||||||
res = evdns_base_config_windows_nameservers(evdns_base);
|
res = evdns_base_config_windows_nameservers(evdns_base);
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
res = evdns_base_resolv_conf_parse(evdns_base,
|
res = evdns_base_resolv_conf_parse(evdns_base,
|
||||||
DNS_OPTION_NAMESERVERS,
|
DNS_OPTION_NAMESERVERS, o.resolv_conf);
|
||||||
resolv_conf ? resolv_conf : "/etc/resolv.conf");
|
|
||||||
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
fprintf(stderr, "Couldn't configure nameservers");
|
fprintf(stderr, "Couldn't configure nameservers");
|
||||||
@ -218,27 +219,27 @@ main(int c, char **v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME);
|
printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME);
|
||||||
for (; idx < c; ++idx) {
|
for (; optind < c; ++optind) {
|
||||||
if (reverse) {
|
if (o.reverse) {
|
||||||
struct in_addr addr;
|
struct in_addr addr;
|
||||||
if (evutil_inet_pton(AF_INET, v[idx], &addr)!=1) {
|
if (evutil_inet_pton(AF_INET, v[optind], &addr)!=1) {
|
||||||
fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
|
fprintf(stderr, "Skipping non-IP %s\n", v[optind]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "resolving %s...\n",v[idx]);
|
fprintf(stderr, "resolving %s...\n",v[optind]);
|
||||||
evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[idx]);
|
evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[optind]);
|
||||||
} else if (use_getaddrinfo) {
|
} else if (o.use_getaddrinfo) {
|
||||||
struct evutil_addrinfo hints;
|
struct evutil_addrinfo hints;
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = PF_UNSPEC;
|
hints.ai_family = PF_UNSPEC;
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
hints.ai_flags = EVUTIL_AI_CANONNAME;
|
hints.ai_flags = EVUTIL_AI_CANONNAME;
|
||||||
fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
|
fprintf(stderr, "resolving (fwd) %s...\n",v[optind]);
|
||||||
evdns_getaddrinfo(evdns_base, v[idx], NULL, &hints,
|
evdns_getaddrinfo(evdns_base, v[optind], NULL, &hints,
|
||||||
gai_callback, v[idx]);
|
gai_callback, v[optind]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
|
fprintf(stderr, "resolving (fwd) %s...\n",v[optind]);
|
||||||
evdns_base_resolve_ipv4(evdns_base, v[idx], 0, main_callback, v[idx]);
|
evdns_base_resolve_ipv4(evdns_base, v[optind], 0, main_callback, v[optind]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user