8 # define _WIN32_IE 0x400
11 # include <sys/socket.h>
12 # include <netinet/in.h>
13 # include <arpa/inet.h>
19 #include <event2/event.h>
20 #include <event2/dns.h>
21 #include <event2/util.h>
26 static AppContext app_context
;
28 #ifndef INET6_ADDRSTRLEN
29 # define INET6_ADDRSTRLEN 46U
33 query_cb_err_print(const int err
)
35 fprintf(stderr
, "[%s]\n", evdns_err_to_string(err
));
40 ipv4_query_cb(int result
, char type
, int count
, int ttl
,
41 void * const ips_
, void * const app_context_
)
43 char ip_s_buf
[INET6_ADDRSTRLEN
+ 1U];
44 AppContext
*app_context
= app_context_
;
45 struct in_addr
*ips
= ips_
;
50 if (result
!= DNS_ERR_NONE
) {
51 query_cb_err_print(result
);
53 assert(type
== DNS_IPv4_A
);
56 ip_s
= evutil_inet_ntop(AF_INET
, &ips
[i
], ip_s_buf
, sizeof ip_s_buf
);
62 event_base_loopexit(app_context
->event_loop
, NULL
);
66 ipv6_query_cb(int result
, char type
, int count
, int ttl
,
67 void * const ips_
, void * const app_context_
)
69 char ip_s_buf
[INET6_ADDRSTRLEN
+ 1U];
70 AppContext
*app_context
= app_context_
;
71 struct in6_addr
*ips
= ips_
;
76 if (result
!= DNS_ERR_NONE
) {
77 query_cb_err_print(result
);
79 assert(type
== DNS_IPv6_AAAA
);
82 ip_s
= evutil_inet_ntop(AF_INET6
, &ips
[i
], ip_s_buf
, sizeof ip_s_buf
);
88 event_base_loopexit(app_context
->event_loop
, NULL
);
91 int main(int argc
, char *argv
[])
93 struct evdns_base
*evdns_base
;
95 if (options_parse(&app_context
, argc
, argv
) != 0) {
100 WSAStartup(MAKEWORD(2, 2), &wsa_data
);
102 if ((app_context
.event_loop
= event_base_new()) == NULL
) {
103 perror("event_base_new");
106 if ((evdns_base
= evdns_base_new(app_context
.event_loop
, 0)) == NULL
) {
107 perror("evdns_base");
110 if (evdns_base_nameserver_ip_add(evdns_base
,
111 app_context
.resolver_ip
) != 0) {
112 fprintf(stderr
, "Unable to use [%s] as a resolver\n",
113 app_context
.resolver_ip
);
116 if (app_context
.want_ipv6
!= 0) {
117 evdns_base_resolve_ipv6(evdns_base
, app_context
.host_name
,
119 ipv6_query_cb
, &app_context
);
121 evdns_base_resolve_ipv4(evdns_base
, app_context
.host_name
,
123 ipv4_query_cb
, &app_context
);
125 event_base_dispatch(app_context
.event_loop
);
126 event_base_free(app_context
.event_loop
);