Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / hostip / app.c
blobfdaf7c968e6bb231d541f1a0cc41b59819cce133
2 #include <config.h>
3 #include <sys/types.h>
4 #ifdef _WIN32
5 # include <winsock2.h>
6 # include <ws2tcpip.h>
7 # ifndef _WIN32_IE
8 # define _WIN32_IE 0x400
9 # endif
10 #else
11 # include <sys/socket.h>
12 # include <netinet/in.h>
13 # include <arpa/inet.h>
14 #endif
15 #include <assert.h>
16 #include <stdio.h>
17 #include <stdlib.h>
19 #include <event2/event.h>
20 #include <event2/dns.h>
21 #include <event2/util.h>
23 #include "app.h"
24 #include "options.h"
26 static AppContext app_context;
28 #ifndef INET6_ADDRSTRLEN
29 # define INET6_ADDRSTRLEN 46U
30 #endif
32 static void
33 query_cb_err_print(const int err)
35 fprintf(stderr, "[%s]\n", evdns_err_to_string(err));
36 exit(1);
39 static void
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_;
46 const char *ip_s;
47 int i = 0;
49 (void) ttl;
50 if (result != DNS_ERR_NONE) {
51 query_cb_err_print(result);
53 assert(type == DNS_IPv4_A);
54 assert(count >= 0);
55 while (i < count) {
56 ip_s = evutil_inet_ntop(AF_INET, &ips[i], ip_s_buf, sizeof ip_s_buf);
57 if (ip_s != NULL) {
58 puts(ip_s);
60 i++;
62 event_base_loopexit(app_context->event_loop, NULL);
65 static void
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_;
72 const char *ip_s;
73 int i = 0;
75 (void) ttl;
76 if (result != DNS_ERR_NONE) {
77 query_cb_err_print(result);
79 assert(type == DNS_IPv6_AAAA);
80 assert(count >= 0);
81 while (i < count) {
82 ip_s = evutil_inet_ntop(AF_INET6, &ips[i], ip_s_buf, sizeof ip_s_buf);
83 if (ip_s != NULL) {
84 puts(ip_s);
86 i++;
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) {
96 return 1;
98 #ifdef _WIN32
99 WSADATA wsa_data;
100 WSAStartup(MAKEWORD(2, 2), &wsa_data);
101 #endif
102 if ((app_context.event_loop = event_base_new()) == NULL) {
103 perror("event_base_new");
104 return 1;
106 if ((evdns_base = evdns_base_new(app_context.event_loop, 0)) == NULL) {
107 perror("evdns_base");
108 return 1;
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);
114 return 1;
116 if (app_context.want_ipv6 != 0) {
117 evdns_base_resolve_ipv6(evdns_base, app_context.host_name,
118 DNS_QUERY_NO_SEARCH,
119 ipv6_query_cb, &app_context);
120 } else {
121 evdns_base_resolve_ipv4(evdns_base, app_context.host_name,
122 DNS_QUERY_NO_SEARCH,
123 ipv4_query_cb, &app_context);
125 event_base_dispatch(app_context.event_loop);
126 event_base_free(app_context.event_loop);
128 return 0;