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