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