/contrib/bind9/lib/lwres/lwres_gnba.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 415 lines · 230 code · 63 blank · 122 comment · 79 complexity · 3bca7f170bc72e04034274af324b097d MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000-2002 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: lwres_gnba.c,v 1.28 2007/09/24 17:18:25 each Exp $ */
  18. /*! \file lwres_gnba.c
  19. These are low-level routines for creating and parsing lightweight
  20. resolver address-to-name lookup request and response messages.
  21. There are four main functions for the getnamebyaddr opcode. One
  22. render function converts a getnamebyaddr request structure --
  23. lwres_gnbarequest_t -- to the lightweight resolver's canonical
  24. format. It is complemented by a parse function that converts a
  25. packet in this canonical format to a getnamebyaddr request
  26. structure. Another render function converts the getnamebyaddr
  27. response structure -- lwres_gnbaresponse_t to the canonical format.
  28. This is complemented by a parse function which converts a packet in
  29. canonical format to a getnamebyaddr response structure.
  30. These structures are defined in \link lwres.h <lwres/lwres.h.>\endlink They are shown
  31. below.
  32. \code
  33. #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
  34. typedef struct {
  35. lwres_uint32_t flags;
  36. lwres_addr_t addr;
  37. } lwres_gnbarequest_t;
  38. typedef struct {
  39. lwres_uint32_t flags;
  40. lwres_uint16_t naliases;
  41. char *realname;
  42. char **aliases;
  43. lwres_uint16_t realnamelen;
  44. lwres_uint16_t *aliaslen;
  45. void *base;
  46. size_t baselen;
  47. } lwres_gnbaresponse_t;
  48. \endcode
  49. lwres_gnbarequest_render() uses resolver context ctx to convert
  50. getnamebyaddr request structure req to canonical format. The packet
  51. header structure pkt is initialised and transferred to buffer b.
  52. The contents of *req are then appended to the buffer in canonical
  53. format. lwres_gnbaresponse_render() performs the same task, except
  54. it converts a getnamebyaddr response structure lwres_gnbaresponse_t
  55. to the lightweight resolver's canonical format.
  56. lwres_gnbarequest_parse() uses context ctx to convert the contents
  57. of packet pkt to a lwres_gnbarequest_t structure. Buffer b provides
  58. space to be used for storing this structure. When the function
  59. succeeds, the resulting lwres_gnbarequest_t is made available
  60. through *structp. lwres_gnbaresponse_parse() offers the same
  61. semantics as lwres_gnbarequest_parse() except it yields a
  62. lwres_gnbaresponse_t structure.
  63. lwres_gnbaresponse_free() and lwres_gnbarequest_free() release the
  64. memory in resolver context ctx that was allocated to the
  65. lwres_gnbaresponse_t or lwres_gnbarequest_t structures referenced
  66. via structp. Any memory associated with ancillary buffers and
  67. strings for those structures is also discarded.
  68. \section lwres_gbna_return Return Values
  69. The getnamebyaddr opcode functions lwres_gnbarequest_render(),
  70. lwres_gnbaresponse_render() lwres_gnbarequest_parse() and
  71. lwres_gnbaresponse_parse() all return #LWRES_R_SUCCESS on success.
  72. They return #LWRES_R_NOMEMORY if memory allocation fails.
  73. #LWRES_R_UNEXPECTEDEND is returned if the available space in the
  74. buffer b is too small to accommodate the packet header or the
  75. lwres_gnbarequest_t and lwres_gnbaresponse_t structures.
  76. lwres_gnbarequest_parse() and lwres_gnbaresponse_parse() will
  77. return #LWRES_R_UNEXPECTEDEND if the buffer is not empty after
  78. decoding the received packet. These functions will return
  79. #LWRES_R_FAILURE if pktflags in the packet header structure
  80. #lwres_lwpacket_t indicate that the packet is not a response to an
  81. earlier query.
  82. \section lwres_gbna_see See Also
  83. \link lwpacket.c lwres_packet\endlink
  84. */
  85. #include <config.h>
  86. #include <assert.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. #include <lwres/lwbuffer.h>
  90. #include <lwres/lwpacket.h>
  91. #include <lwres/lwres.h>
  92. #include <lwres/result.h>
  93. #include "context_p.h"
  94. #include "assert_p.h"
  95. /*% Uses resolver context ctx to convert getnamebyaddr request structure req to canonical format. */
  96. lwres_result_t
  97. lwres_gnbarequest_render(lwres_context_t *ctx, lwres_gnbarequest_t *req,
  98. lwres_lwpacket_t *pkt, lwres_buffer_t *b)
  99. {
  100. unsigned char *buf;
  101. size_t buflen;
  102. int ret;
  103. size_t payload_length;
  104. REQUIRE(ctx != NULL);
  105. REQUIRE(req != NULL);
  106. REQUIRE(req->addr.family != 0);
  107. REQUIRE(req->addr.length != 0);
  108. REQUIRE(pkt != NULL);
  109. REQUIRE(b != NULL);
  110. payload_length = 4 + 4 + 2 + + req->addr.length;
  111. buflen = LWRES_LWPACKET_LENGTH + payload_length;
  112. buf = CTXMALLOC(buflen);
  113. if (buf == NULL)
  114. return (LWRES_R_NOMEMORY);
  115. lwres_buffer_init(b, buf, buflen);
  116. pkt->length = buflen;
  117. pkt->version = LWRES_LWPACKETVERSION_0;
  118. pkt->pktflags &= ~LWRES_LWPACKETFLAG_RESPONSE;
  119. pkt->opcode = LWRES_OPCODE_GETNAMEBYADDR;
  120. pkt->result = 0;
  121. pkt->authtype = 0;
  122. pkt->authlength = 0;
  123. ret = lwres_lwpacket_renderheader(b, pkt);
  124. if (ret != LWRES_R_SUCCESS) {
  125. lwres_buffer_invalidate(b);
  126. CTXFREE(buf, buflen);
  127. return (ret);
  128. }
  129. INSIST(SPACE_OK(b, payload_length));
  130. /*
  131. * Put the length and the data. We know this will fit because we
  132. * just checked for it.
  133. */
  134. lwres_buffer_putuint32(b, req->flags);
  135. lwres_buffer_putuint32(b, req->addr.family);
  136. lwres_buffer_putuint16(b, req->addr.length);
  137. lwres_buffer_putmem(b, (unsigned char *)req->addr.address,
  138. req->addr.length);
  139. INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
  140. return (LWRES_R_SUCCESS);
  141. }
  142. /*% Converts a getnamebyaddr response structure lwres_gnbaresponse_t to the lightweight resolver's canonical format. */
  143. lwres_result_t
  144. lwres_gnbaresponse_render(lwres_context_t *ctx, lwres_gnbaresponse_t *req,
  145. lwres_lwpacket_t *pkt, lwres_buffer_t *b)
  146. {
  147. unsigned char *buf;
  148. size_t buflen;
  149. int ret;
  150. size_t payload_length;
  151. lwres_uint16_t datalen;
  152. int x;
  153. REQUIRE(ctx != NULL);
  154. REQUIRE(req != NULL);
  155. REQUIRE(pkt != NULL);
  156. REQUIRE(b != NULL);
  157. /*
  158. * Calculate packet size.
  159. */
  160. payload_length = 4; /* flags */
  161. payload_length += 2; /* naliases */
  162. payload_length += 2 + req->realnamelen + 1; /* real name encoding */
  163. for (x = 0; x < req->naliases; x++) /* each alias */
  164. payload_length += 2 + req->aliaslen[x] + 1;
  165. buflen = LWRES_LWPACKET_LENGTH + payload_length;
  166. buf = CTXMALLOC(buflen);
  167. if (buf == NULL)
  168. return (LWRES_R_NOMEMORY);
  169. lwres_buffer_init(b, buf, buflen);
  170. pkt->length = buflen;
  171. pkt->version = LWRES_LWPACKETVERSION_0;
  172. pkt->pktflags |= LWRES_LWPACKETFLAG_RESPONSE;
  173. pkt->opcode = LWRES_OPCODE_GETNAMEBYADDR;
  174. pkt->authtype = 0;
  175. pkt->authlength = 0;
  176. ret = lwres_lwpacket_renderheader(b, pkt);
  177. if (ret != LWRES_R_SUCCESS) {
  178. lwres_buffer_invalidate(b);
  179. CTXFREE(buf, buflen);
  180. return (ret);
  181. }
  182. INSIST(SPACE_OK(b, payload_length));
  183. lwres_buffer_putuint32(b, req->flags);
  184. /* encode naliases */
  185. lwres_buffer_putuint16(b, req->naliases);
  186. /* encode the real name */
  187. datalen = req->realnamelen;
  188. lwres_buffer_putuint16(b, datalen);
  189. lwres_buffer_putmem(b, (unsigned char *)req->realname, datalen);
  190. lwres_buffer_putuint8(b, 0);
  191. /* encode the aliases */
  192. for (x = 0; x < req->naliases; x++) {
  193. datalen = req->aliaslen[x];
  194. lwres_buffer_putuint16(b, datalen);
  195. lwres_buffer_putmem(b, (unsigned char *)req->aliases[x],
  196. datalen);
  197. lwres_buffer_putuint8(b, 0);
  198. }
  199. INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
  200. return (LWRES_R_SUCCESS);
  201. }
  202. /*% Uses context ctx to convert the contents of packet pkt to a lwres_gnbarequest_t structure. */
  203. lwres_result_t
  204. lwres_gnbarequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
  205. lwres_lwpacket_t *pkt, lwres_gnbarequest_t **structp)
  206. {
  207. int ret;
  208. lwres_gnbarequest_t *gnba;
  209. REQUIRE(ctx != NULL);
  210. REQUIRE(pkt != NULL);
  211. REQUIRE(b != NULL);
  212. REQUIRE(structp != NULL && *structp == NULL);
  213. if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) != 0)
  214. return (LWRES_R_FAILURE);
  215. if (!SPACE_REMAINING(b, 4))
  216. return (LWRES_R_UNEXPECTEDEND);
  217. gnba = CTXMALLOC(sizeof(lwres_gnbarequest_t));
  218. if (gnba == NULL)
  219. return (LWRES_R_NOMEMORY);
  220. gnba->flags = lwres_buffer_getuint32(b);
  221. ret = lwres_addr_parse(b, &gnba->addr);
  222. if (ret != LWRES_R_SUCCESS)
  223. goto out;
  224. if (LWRES_BUFFER_REMAINING(b) != 0) {
  225. ret = LWRES_R_TRAILINGDATA;
  226. goto out;
  227. }
  228. *structp = gnba;
  229. return (LWRES_R_SUCCESS);
  230. out:
  231. if (gnba != NULL)
  232. lwres_gnbarequest_free(ctx, &gnba);
  233. return (ret);
  234. }
  235. /*% Offers the same semantics as lwres_gnbarequest_parse() except it yields a lwres_gnbaresponse_t structure. */
  236. lwres_result_t
  237. lwres_gnbaresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
  238. lwres_lwpacket_t *pkt, lwres_gnbaresponse_t **structp)
  239. {
  240. int ret;
  241. unsigned int x;
  242. lwres_uint32_t flags;
  243. lwres_uint16_t naliases;
  244. lwres_gnbaresponse_t *gnba;
  245. REQUIRE(ctx != NULL);
  246. REQUIRE(pkt != NULL);
  247. REQUIRE(b != NULL);
  248. REQUIRE(structp != NULL && *structp == NULL);
  249. gnba = NULL;
  250. if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) == 0)
  251. return (LWRES_R_FAILURE);
  252. /*
  253. * Pull off flags & naliases
  254. */
  255. if (!SPACE_REMAINING(b, 4 + 2))
  256. return (LWRES_R_UNEXPECTEDEND);
  257. flags = lwres_buffer_getuint32(b);
  258. naliases = lwres_buffer_getuint16(b);
  259. gnba = CTXMALLOC(sizeof(lwres_gnbaresponse_t));
  260. if (gnba == NULL)
  261. return (LWRES_R_NOMEMORY);
  262. gnba->base = NULL;
  263. gnba->aliases = NULL;
  264. gnba->aliaslen = NULL;
  265. gnba->flags = flags;
  266. gnba->naliases = naliases;
  267. if (naliases > 0) {
  268. gnba->aliases = CTXMALLOC(sizeof(char *) * naliases);
  269. if (gnba->aliases == NULL) {
  270. ret = LWRES_R_NOMEMORY;
  271. goto out;
  272. }
  273. gnba->aliaslen = CTXMALLOC(sizeof(lwres_uint16_t) * naliases);
  274. if (gnba->aliaslen == NULL) {
  275. ret = LWRES_R_NOMEMORY;
  276. goto out;
  277. }
  278. }
  279. /*
  280. * Now, pull off the real name.
  281. */
  282. ret = lwres_string_parse(b, &gnba->realname, &gnba->realnamelen);
  283. if (ret != LWRES_R_SUCCESS)
  284. goto out;
  285. /*
  286. * Parse off the aliases.
  287. */
  288. for (x = 0; x < gnba->naliases; x++) {
  289. ret = lwres_string_parse(b, &gnba->aliases[x],
  290. &gnba->aliaslen[x]);
  291. if (ret != LWRES_R_SUCCESS)
  292. goto out;
  293. }
  294. if (LWRES_BUFFER_REMAINING(b) != 0) {
  295. ret = LWRES_R_TRAILINGDATA;
  296. goto out;
  297. }
  298. *structp = gnba;
  299. return (LWRES_R_SUCCESS);
  300. out:
  301. if (gnba != NULL) {
  302. if (gnba->aliases != NULL)
  303. CTXFREE(gnba->aliases, sizeof(char *) * naliases);
  304. if (gnba->aliaslen != NULL)
  305. CTXFREE(gnba->aliaslen,
  306. sizeof(lwres_uint16_t) * naliases);
  307. CTXFREE(gnba, sizeof(lwres_gnbaresponse_t));
  308. }
  309. return (ret);
  310. }
  311. /*% Release the memory in resolver context ctx that was allocated to the lwres_gnbarequest_t. */
  312. void
  313. lwres_gnbarequest_free(lwres_context_t *ctx, lwres_gnbarequest_t **structp)
  314. {
  315. lwres_gnbarequest_t *gnba;
  316. REQUIRE(ctx != NULL);
  317. REQUIRE(structp != NULL && *structp != NULL);
  318. gnba = *structp;
  319. *structp = NULL;
  320. CTXFREE(gnba, sizeof(lwres_gnbarequest_t));
  321. }
  322. /*% Release the memory in resolver context ctx that was allocated to the lwres_gnbaresponse_t. */
  323. void
  324. lwres_gnbaresponse_free(lwres_context_t *ctx, lwres_gnbaresponse_t **structp)
  325. {
  326. lwres_gnbaresponse_t *gnba;
  327. REQUIRE(ctx != NULL);
  328. REQUIRE(structp != NULL && *structp != NULL);
  329. gnba = *structp;
  330. *structp = NULL;
  331. if (gnba->naliases > 0) {
  332. CTXFREE(gnba->aliases, sizeof(char *) * gnba->naliases);
  333. CTXFREE(gnba->aliaslen,
  334. sizeof(lwres_uint16_t) * gnba->naliases);
  335. }
  336. if (gnba->base != NULL)
  337. CTXFREE(gnba->base, gnba->baselen);
  338. CTXFREE(gnba, sizeof(lwres_gnbaresponse_t));
  339. }