xref: /minix/external/bsd/bind/dist/lib/lwres/context.c (revision bb9622b5)
1 /*	$NetBSD: context.c,v 1.7 2014/12/10 04:38:02 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007-2009, 2012-2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: context.c,v 1.55 2009/09/02 23:48:03 tbox Exp  */
21 
22 /*! \file context.c
23    lwres_context_create() creates a #lwres_context_t structure for use in
24    lightweight resolver operations. It holds a socket and other data
25    needed for communicating with a resolver daemon. The new
26    lwres_context_t is returned through contextp, a pointer to a
27    lwres_context_t pointer. This lwres_context_t pointer must initially
28    be NULL, and is modified to point to the newly created
29    lwres_context_t.
30 
31    When the lightweight resolver needs to perform dynamic memory
32    allocation, it will call malloc_function to allocate memory and
33    free_function to free it. If malloc_function and free_function are
34    NULL, memory is allocated using malloc and free. It is not
35    permitted to have a NULL malloc_function and a non-NULL free_function
36    or vice versa. arg is passed as the first parameter to the memory
37    allocation functions. If malloc_function and free_function are NULL,
38    arg is unused and should be passed as NULL.
39 
40    Once memory for the structure has been allocated, it is initialized
41    using lwres_conf_init() and returned via *contextp.
42 
43    lwres_context_destroy() destroys a #lwres_context_t, closing its
44    socket. contextp is a pointer to a pointer to the context that is to
45    be destroyed. The pointer will be set to NULL when the context has
46    been destroyed.
47 
48    The context holds a serial number that is used to identify resolver
49    request packets and associate responses with the corresponding
50    requests. This serial number is controlled using
51    lwres_context_initserial() and lwres_context_nextserial().
52    lwres_context_initserial() sets the serial number for context *ctx to
53    serial. lwres_context_nextserial() increments the serial number and
54    returns the previous value.
55 
56    Memory for a lightweight resolver context is allocated and freed using
57    lwres_context_allocmem() and lwres_context_freemem(). These use
58    whatever allocations were defined when the context was created with
59    lwres_context_create(). lwres_context_allocmem() allocates len bytes
60    of memory and if successful returns a pointer to the allocated
61    storage. lwres_context_freemem() frees len bytes of space starting at
62    location mem.
63 
64    lwres_context_sendrecv() performs I/O for the context ctx. Data are
65    read and written from the context's socket. It writes data from
66    sendbase -- typically a lightweight resolver query packet -- and waits
67    for a reply which is copied to the receive buffer at recvbase. The
68    number of bytes that were written to this receive buffer is returned
69    in *recvd_len.
70 
71 \section context_return Return Values
72 
73    lwres_context_create() returns #LWRES_R_NOMEMORY if memory for the
74    struct lwres_context could not be allocated, #LWRES_R_SUCCESS
75    otherwise.
76 
77    Successful calls to the memory allocator lwres_context_allocmem()
78    return a pointer to the start of the allocated space. It returns NULL
79    if memory could not be allocated.
80 
81    #LWRES_R_SUCCESS is returned when lwres_context_sendrecv() completes
82    successfully. #LWRES_R_IOERROR is returned if an I/O error occurs and
83    #LWRES_R_TIMEOUT is returned if lwres_context_sendrecv() times out
84    waiting for a response.
85 
86 \section context_see See Also
87 
88    lwres_conf_init(), malloc, free.
89  */
90 #include <config.h>
91 
92 #include <fcntl.h>
93 #include <limits.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <time.h>
97 #include <unistd.h>
98 
99 #include <lwres/lwres.h>
100 #include <lwres/net.h>
101 #include <lwres/platform.h>
102 
103 #ifdef LWRES_PLATFORM_NEEDSYSSELECTH
104 #include <sys/select.h>
105 #endif
106 
107 #include "context_p.h"
108 #include "assert_p.h"
109 
110 /*!
111  * Some systems define the socket length argument as an int, some as size_t,
112  * some as socklen_t.  The last is what the current POSIX standard mandates.
113  * This definition is here so it can be portable but easily changed if needed.
114  */
115 #ifndef LWRES_SOCKADDR_LEN_T
116 #define LWRES_SOCKADDR_LEN_T unsigned int
117 #endif
118 
119 /*!
120  * Make a socket nonblocking.
121  */
122 #ifndef MAKE_NONBLOCKING
123 #define MAKE_NONBLOCKING(sd, retval) \
124 do { \
125 	retval = fcntl(sd, F_GETFL, 0); \
126 	if (retval != -1) { \
127 		retval |= O_NONBLOCK; \
128 		retval = fcntl(sd, F_SETFL, retval); \
129 	} \
130 } while (/*CONSTCOND*/0)
131 #endif
132 
133 LIBLWRES_EXTERNAL_DATA lwres_uint16_t lwres_udp_port = LWRES_UDP_PORT;
134 LIBLWRES_EXTERNAL_DATA const char *lwres_resolv_conf = LWRES_RESOLV_CONF;
135 
136 static void *
137 lwres_malloc(void *, size_t);
138 
139 static void
140 lwres_free(void *, void *, size_t);
141 
142 /*!
143  * lwres_result_t
144  */
145 static lwres_result_t
146 context_connect(lwres_context_t *);
147 
148 /*%
149  * Creates a #lwres_context_t structure for use in
150  *  lightweight resolver operations.
151  */
152 lwres_result_t
153 lwres_context_create(lwres_context_t **contextp, void *arg,
154 		     lwres_malloc_t malloc_function,
155 		     lwres_free_t free_function,
156 		     unsigned int flags)
157 {
158 	lwres_context_t *ctx;
159 
160 	REQUIRE(contextp != NULL && *contextp == NULL);
161 
162 	/*
163 	 * If we were not given anything special to use, use our own
164 	 * functions.  These are just wrappers around malloc() and free().
165 	 */
166 	if (malloc_function == NULL || free_function == NULL) {
167 		REQUIRE(malloc_function == NULL);
168 		REQUIRE(free_function == NULL);
169 		malloc_function = lwres_malloc;
170 		free_function = lwres_free;
171 	}
172 
173 	ctx = malloc_function(arg, sizeof(lwres_context_t));
174 	if (ctx == NULL)
175 		return (LWRES_R_NOMEMORY);
176 
177 	/*
178 	 * Set up the context.
179 	 */
180 	ctx->malloc = malloc_function;
181 	ctx->free = free_function;
182 	ctx->arg = arg;
183 	ctx->sock = -1;
184 
185 	ctx->timeout = LWRES_DEFAULT_TIMEOUT;
186 #ifndef WIN32
187 	ctx->serial = time(NULL); /* XXXMLG or BEW */
188 #else
189 	ctx->serial = _time32(NULL);
190 #endif
191 
192 	ctx->use_ipv4 = 1;
193 	ctx->use_ipv6 = 1;
194 	if ((flags & (LWRES_CONTEXT_USEIPV4 | LWRES_CONTEXT_USEIPV6)) ==
195 	    LWRES_CONTEXT_USEIPV6) {
196 		ctx->use_ipv4 = 0;
197 	}
198 	if ((flags & (LWRES_CONTEXT_USEIPV4 | LWRES_CONTEXT_USEIPV6)) ==
199 	    LWRES_CONTEXT_USEIPV4) {
200 		ctx->use_ipv6 = 0;
201 	}
202 
203 	/*
204 	 * Init resolv.conf bits.
205 	 */
206 	lwres_conf_init(ctx);
207 
208 	*contextp = ctx;
209 	return (LWRES_R_SUCCESS);
210 }
211 
212 /*%
213 Destroys a #lwres_context_t, closing its socket.
214 contextp is a pointer to a pointer to the context that is
215 to be destroyed. The pointer will be set to NULL
216 when the context has been destroyed.
217  */
218 void
219 lwres_context_destroy(lwres_context_t **contextp) {
220 	lwres_context_t *ctx;
221 
222 	REQUIRE(contextp != NULL && *contextp != NULL);
223 
224 	ctx = *contextp;
225 	*contextp = NULL;
226 
227 	if (ctx->sock != -1) {
228 #ifdef WIN32
229 		DestroySockets();
230 #endif
231 		(void)close(ctx->sock);
232 		ctx->sock = -1;
233 	}
234 
235 	CTXFREE(ctx, sizeof(lwres_context_t));
236 }
237 /*% Increments the serial number and returns the previous value. */
238 lwres_uint32_t
239 lwres_context_nextserial(lwres_context_t *ctx) {
240 	REQUIRE(ctx != NULL);
241 
242 	return (ctx->serial++);
243 }
244 
245 /*% Sets the serial number for context *ctx to serial. */
246 void
247 lwres_context_initserial(lwres_context_t *ctx, lwres_uint32_t serial) {
248 	REQUIRE(ctx != NULL);
249 
250 	ctx->serial = serial;
251 }
252 
253 /*% Frees len bytes of space starting at location mem. */
254 void
255 lwres_context_freemem(lwres_context_t *ctx, void *mem, size_t len) {
256 	REQUIRE(mem != NULL);
257 	REQUIRE(len != 0U);
258 
259 	CTXFREE(mem, len);
260 }
261 
262 /*% Allocates len bytes of memory and if successful returns a pointer to the allocated storage. */
263 void *
264 lwres_context_allocmem(lwres_context_t *ctx, size_t len) {
265 	REQUIRE(len != 0U);
266 
267 	return (CTXMALLOC(len));
268 }
269 
270 static void *
271 lwres_malloc(void *arg, size_t len) {
272 	void *mem;
273 
274 	UNUSED(arg);
275 
276 	mem = malloc(len);
277 	if (mem == NULL)
278 		return (NULL);
279 
280 	memset(mem, 0xe5, len);
281 
282 	return (mem);
283 }
284 
285 static void
286 lwres_free(void *arg, void *mem, size_t len) {
287 	UNUSED(arg);
288 
289 	memset(mem, 0xa9, len);
290 	free(mem);
291 }
292 
293 static lwres_result_t
294 context_connect(lwres_context_t *ctx) {
295 #ifndef WIN32
296 	int s;
297 #else
298 	SOCKET s;
299 #endif
300 	int ret;
301 	struct sockaddr_in sin;
302 	struct sockaddr_in6 sin6;
303 	struct sockaddr *sa;
304 	LWRES_SOCKADDR_LEN_T salen;
305 	int domain;
306 
307 	if (ctx->confdata.lwnext != 0) {
308 		memmove(&ctx->address, &ctx->confdata.lwservers[0],
309 			sizeof(lwres_addr_t));
310 		LWRES_LINK_INIT(&ctx->address, link);
311 	} else {
312 		/* The default is the IPv4 loopback address 127.0.0.1. */
313 		memset(&ctx->address, 0, sizeof(ctx->address));
314 		ctx->address.family = LWRES_ADDRTYPE_V4;
315 		ctx->address.length = 4;
316 		ctx->address.address[0] = 127;
317 		ctx->address.address[1] = 0;
318 		ctx->address.address[2] = 0;
319 		ctx->address.address[3] = 1;
320 	}
321 
322 	if (ctx->address.family == LWRES_ADDRTYPE_V4) {
323 		memmove(&sin.sin_addr, ctx->address.address,
324 			sizeof(sin.sin_addr));
325 		sin.sin_port = htons(lwres_udp_port);
326 		sin.sin_family = AF_INET;
327 		sa = (struct sockaddr *)&sin;
328 		salen = sizeof(sin);
329 		domain = PF_INET;
330 	} else if (ctx->address.family == LWRES_ADDRTYPE_V6) {
331 		memmove(&sin6.sin6_addr, ctx->address.address,
332 			sizeof(sin6.sin6_addr));
333 		sin6.sin6_port = htons(lwres_udp_port);
334 		sin6.sin6_family = AF_INET6;
335 		sa = (struct sockaddr *)&sin6;
336 		salen = sizeof(sin6);
337 		domain = PF_INET6;
338 	} else
339 		return (LWRES_R_IOERROR);
340 
341 #ifdef WIN32
342 	InitSockets();
343 #endif
344 	s = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
345 #ifndef WIN32
346 	if (s < 0) {
347 		return (LWRES_R_IOERROR);
348 	}
349 #else
350 	if (s == INVALID_SOCKET) {
351 		DestroySockets();
352 		return (LWRES_R_IOERROR);
353 	}
354 #endif
355 
356 	ret = connect(s, sa, salen);
357 	if (ret != 0) {
358 #ifdef WIN32
359 		DestroySockets();
360 #endif
361 		(void)close(s);
362 		return (LWRES_R_IOERROR);
363 	}
364 
365 	MAKE_NONBLOCKING(s, ret);
366 	if (ret < 0) {
367 #ifdef WIN32
368 		DestroySockets();
369 #endif
370 		(void)close(s);
371 		return (LWRES_R_IOERROR);
372 	}
373 
374 	ctx->sock = (int)s;
375 
376 	return (LWRES_R_SUCCESS);
377 }
378 
379 int
380 lwres_context_getsocket(lwres_context_t *ctx) {
381 	return (ctx->sock);
382 }
383 
384 lwres_result_t
385 lwres_context_send(lwres_context_t *ctx,
386 		   void *sendbase, int sendlen) {
387 	int ret;
388 	lwres_result_t lwresult;
389 
390 	if (ctx->sock == -1) {
391 		lwresult = context_connect(ctx);
392 		if (lwresult != LWRES_R_SUCCESS)
393 			return (lwresult);
394 		INSIST(ctx->sock >= 0);
395 	}
396 
397 	ret = sendto(ctx->sock, sendbase, sendlen, 0, NULL, 0);
398 	if (ret < 0)
399 		return (LWRES_R_IOERROR);
400 	if (ret != sendlen)
401 		return (LWRES_R_IOERROR);
402 
403 	return (LWRES_R_SUCCESS);
404 }
405 
406 lwres_result_t
407 lwres_context_recv(lwres_context_t *ctx,
408 		   void *recvbase, int recvlen,
409 		   int *recvd_len)
410 {
411 	LWRES_SOCKADDR_LEN_T fromlen;
412 	struct sockaddr_in sin;
413 	struct sockaddr_in6 sin6;
414 	struct sockaddr *sa;
415 	int ret;
416 
417 	if (ctx->address.family == LWRES_ADDRTYPE_V4) {
418 		sa = (struct sockaddr *)&sin;
419 		fromlen = sizeof(sin);
420 	} else {
421 		sa = (struct sockaddr *)&sin6;
422 		fromlen = sizeof(sin6);
423 	}
424 
425 	/*
426 	 * The address of fromlen is cast to void * to shut up compiler
427 	 * warnings, namely on systems that have the sixth parameter
428 	 * prototyped as a signed int when LWRES_SOCKADDR_LEN_T is
429 	 * defined as unsigned.
430 	 */
431 	ret = recvfrom(ctx->sock, recvbase, recvlen, 0, sa, (void *)&fromlen);
432 
433 	if (ret < 0)
434 		return (LWRES_R_IOERROR);
435 
436 	if (ret == recvlen)
437 		return (LWRES_R_TOOLARGE);
438 
439 	/*
440 	 * If we got something other than what we expect, have the caller
441 	 * wait for another packet.  This can happen if an old result
442 	 * comes in, or if someone is sending us random stuff.
443 	 */
444 	if (ctx->address.family == LWRES_ADDRTYPE_V4) {
445 		if (fromlen != sizeof(sin)
446 		    || memcmp(&sin.sin_addr, ctx->address.address,
447 			      sizeof(sin.sin_addr)) != 0
448 		    || sin.sin_port != htons(lwres_udp_port))
449 			return (LWRES_R_RETRY);
450 	} else {
451 		if (fromlen != sizeof(sin6)
452 		    || memcmp(&sin6.sin6_addr, ctx->address.address,
453 			      sizeof(sin6.sin6_addr)) != 0
454 		    || sin6.sin6_port != htons(lwres_udp_port))
455 			return (LWRES_R_RETRY);
456 	}
457 
458 	if (recvd_len != NULL)
459 		*recvd_len = ret;
460 
461 	return (LWRES_R_SUCCESS);
462 }
463 
464 /*% performs I/O for the context ctx. */
465 lwres_result_t
466 lwres_context_sendrecv(lwres_context_t *ctx,
467 		       void *sendbase, int sendlen,
468 		       void *recvbase, int recvlen,
469 		       int *recvd_len)
470 {
471 	lwres_result_t result;
472 	int ret2;
473 	fd_set readfds;
474 	struct timeval timeout;
475 
476 	/*
477 	 * Type of tv_sec is 32 bits long.
478 	 */
479 	if (ctx->timeout <= 0x7FFFFFFFU)
480 		timeout.tv_sec = (int)ctx->timeout;
481 	else
482 		timeout.tv_sec = 0x7FFFFFFF;
483 
484 	timeout.tv_usec = 0;
485 
486 	result = lwres_context_send(ctx, sendbase, sendlen);
487 	if (result != LWRES_R_SUCCESS)
488 		return (result);
489 
490 	/*
491 	 * If this is not checked, select() can overflow,
492 	 * causing corruption elsewhere.
493 	 */
494 	if (ctx->sock >= (int)FD_SETSIZE) {
495 		close(ctx->sock);
496 		ctx->sock = -1;
497 		return (LWRES_R_IOERROR);
498 	}
499 
500  again:
501 	FD_ZERO(&readfds);
502 	FD_SET(ctx->sock, &readfds);
503 	ret2 = select(ctx->sock + 1, &readfds, NULL, NULL, &timeout);
504 
505 	/*
506 	 * What happened with select?
507 	 */
508 	if (ret2 < 0)
509 		return (LWRES_R_IOERROR);
510 	if (ret2 == 0)
511 		return (LWRES_R_TIMEOUT);
512 
513 	result = lwres_context_recv(ctx, recvbase, recvlen, recvd_len);
514 	if (result == LWRES_R_RETRY)
515 		goto again;
516 
517 	return (result);
518 }
519