/contrib/bind9/lib/lwres/context.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 503 lines · 290 code · 70 blank · 143 comment · 79 complexity · a8ae65d17a81b8878a6f9469c1e443b9 MD5 · raw file

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