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