xref: /libevent-2.1.12/sample/dns-example.c (revision bbf55150)
1 /*
2   This example code shows how to use the high-level, low-level, and
3   server-level interfaces of evdns.
4 
5   XXX It's pretty ugly and should probably be cleaned up.
6  */
7 
8 #include <event2/event-config.h>
9 
10 #include "../ipv6-internal.h"
11 
12 #include <sys/types.h>
13 
14 #ifdef WIN32
15 #include <winsock2.h>
16 #include <ws2tcpip.h>
17 #else
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #endif
22 
23 #include <event2/event.h>
24 #include <event2/dns.h>
25 #include <event2/dns_struct.h>
26 #include <event2/util.h>
27 
28 #ifdef _EVENT_HAVE_NETINET_IN6_H
29 #include <netinet/in6.h>
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #define u32 ev_uint32_t
37 #define u8 ev_uint8_t
38 
39 static const char *
40 debug_ntoa(u32 address)
41 {
42 	static char buf[32];
43 	u32 a = ntohl(address);
44 	evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
45 					(int)(u8)((a>>24)&0xff),
46 					(int)(u8)((a>>16)&0xff),
47 					(int)(u8)((a>>8 )&0xff),
48 					(int)(u8)((a	)&0xff));
49 	return buf;
50 }
51 
52 static void
53 main_callback(int result, char type, int count, int ttl,
54 			  void *addrs, void *orig) {
55 	char *n = (char*)orig;
56 	int i;
57 	for (i = 0; i < count; ++i) {
58 		if (type == DNS_IPv4_A) {
59 			printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
60 		} else if (type == DNS_PTR) {
61 			printf("%s: %s\n", n, ((char**)addrs)[i]);
62 		}
63 	}
64 	if (!count) {
65 		printf("%s: No answer (%d)\n", n, result);
66 	}
67 	fflush(stdout);
68 }
69 
70 static void
71 gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
72 {
73 	const char *name = arg;
74 	struct evutil_addrinfo *ai_first = NULL;
75 	int i;
76 	if (err) {
77 		printf("%s: %s\n", name, evutil_gai_strerror(err));
78 	}
79 	if (ai && ai->ai_canonname)
80 		printf("    %s ==> %s\n", name, ai->ai_canonname);
81 	for (i=0; ai; ai = ai->ai_next, ++i) {
82 		char buf[128];
83 		if (ai->ai_family == PF_INET) {
84 			struct sockaddr_in *sin =
85 			    (struct sockaddr_in*)ai->ai_addr;
86 			evutil_inet_ntop(AF_INET, &sin->sin_addr, buf,
87 			    sizeof(buf));
88 			printf("[%d] %s: %s\n",i,name,buf);
89 		} else {
90 			struct sockaddr_in6 *sin6 =
91 			    (struct sockaddr_in6*)ai->ai_addr;
92 			evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
93 			    sizeof(buf));
94 			printf("[%d] %s: %s\n",i,name,buf);
95 		}
96 	}
97 	if (ai_first)
98 		evutil_freeaddrinfo(ai_first);
99 }
100 
101 static void
102 evdns_server_callback(struct evdns_server_request *req, void *data)
103 {
104 	int i, r;
105 	(void)data;
106 	/* dummy; give 192.168.11.11 as an answer for all A questions,
107 	 *	give foo.bar.example.com as an answer for all PTR questions. */
108 	for (i = 0; i < req->nquestions; ++i) {
109 		u32 ans = htonl(0xc0a80b0bUL);
110 		if (req->questions[i]->type == EVDNS_TYPE_A &&
111 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
112 			printf(" -- replying for %s (A)\n", req->questions[i]->name);
113 			r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
114 										  1, &ans, 10);
115 			if (r<0)
116 				printf("eeep, didn't work.\n");
117 		} else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
118 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
119 			printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
120 			r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
121 											"foo.bar.example.com", 10);
122 		} else {
123 			printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
124 				   req->questions[i]->type, req->questions[i]->dns_question_class);
125 		}
126 	}
127 
128 	r = evdns_server_request_respond(req, 0);
129 	if (r<0)
130 		printf("eeek, couldn't send reply.\n");
131 }
132 
133 static int verbose = 0;
134 
135 static void
136 logfn(int is_warn, const char *msg) {
137 	if (!is_warn && !verbose)
138 		return;
139 	fprintf(stderr, "%s: %s\n", is_warn?"WARN":"INFO", msg);
140 }
141 
142 int
143 main(int c, char **v) {
144 	int idx;
145 	int reverse = 0, servertest = 0, use_getaddrinfo = 0;
146 	struct event_base *event_base = NULL;
147 	struct evdns_base *evdns_base = NULL;
148 	if (c<2) {
149 		fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
150 		fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
151 		return 1;
152 	}
153 	idx = 1;
154 	while (idx < c && v[idx][0] == '-') {
155 		if (!strcmp(v[idx], "-x"))
156 			reverse = 1;
157 		else if (!strcmp(v[idx], "-v"))
158 			verbose = 1;
159 		else if (!strcmp(v[idx], "-g"))
160 			use_getaddrinfo = 1;
161 		else if (!strcmp(v[idx], "-servertest"))
162 			servertest = 1;
163 		else
164 			fprintf(stderr, "Unknown option %s\n", v[idx]);
165 		++idx;
166 	}
167 
168 	event_base = event_base_new();
169 	evdns_base = evdns_base_new(event_base, 0);
170 	evdns_set_log_fn(logfn);
171 
172 	if (servertest) {
173 		evutil_socket_t sock;
174 		struct sockaddr_in my_addr;
175 		sock = socket(PF_INET, SOCK_DGRAM, 0);
176 		evutil_make_socket_nonblocking(sock);
177 		my_addr.sin_family = AF_INET;
178 		my_addr.sin_port = htons(10053);
179 		my_addr.sin_addr.s_addr = INADDR_ANY;
180 		if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
181 			perror("bind");
182 			exit(1);
183 		}
184 		evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL);
185 	}
186 	if (idx < c) {
187 #ifdef WIN32
188 		evdns_base_config_windows_nameservers(evdns_base);
189 #else
190 		evdns_base_resolv_conf_parse(evdns_base, DNS_OPTION_NAMESERVERS,
191 		    "/etc/resolv.conf");
192 #endif
193 	}
194 
195 	printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME);
196 	for (; idx < c; ++idx) {
197 		if (reverse) {
198 			struct in_addr addr;
199 			if (evutil_inet_pton(AF_INET, v[idx], &addr)!=1) {
200 				fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
201 				continue;
202 			}
203 			fprintf(stderr, "resolving %s...\n",v[idx]);
204 			evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[idx]);
205 		} else if (use_getaddrinfo) {
206 			struct evutil_addrinfo hints;
207 			memset(&hints, 0, sizeof(hints));
208 			hints.ai_family = PF_UNSPEC;
209 			hints.ai_protocol = IPPROTO_TCP;
210 			hints.ai_flags = EVUTIL_AI_CANONNAME;
211 			fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
212 			evdns_getaddrinfo(evdns_base, v[idx], NULL, &hints,
213 			    gai_callback, v[idx]);
214 		} else {
215 			fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
216 			evdns_base_resolve_ipv4(evdns_base, v[idx], 0, main_callback, v[idx]);
217 		}
218 	}
219 	fflush(stdout);
220 	event_base_dispatch(event_base);
221 	return 0;
222 }
223 
224