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