/contrib/bind9/lib/lwres/lwpacket.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 129 lines · 48 code · 19 blank · 62 comment · 7 complexity · e8d29e05dbe7a03454a5d15492962b6e MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 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: lwpacket.c,v 1.18 2007/06/19 23:47:22 tbox Exp $ */
  18. /*! \file */
  19. /**
  20. * These functions rely on a struct lwres_lwpacket which is defined in
  21. * \link lwpacket.h lwres/lwpacket.h.\endlink
  22. *
  23. * The following opcodes are currently defined:
  24. *
  25. * \li #LWRES_OPCODE_NOOP
  26. * Success is always returned and the packet contents are
  27. * echoed. The \link lwres_noop.c lwres_noop_*()\endlink functions should be used for this
  28. * type.
  29. *
  30. * \li #LWRES_OPCODE_GETADDRSBYNAME
  31. * returns all known addresses for a given name. The
  32. * \link lwres_gabn.c lwres_gabn_*()\endlink functions should be used for this type.
  33. *
  34. * \li #LWRES_OPCODE_GETNAMEBYADDR
  35. * return the hostname for the given address. The
  36. * \link lwres_gnba.c lwres_gnba_*() \endlink functions should be used for this type.
  37. *
  38. * lwres_lwpacket_renderheader() transfers the contents of lightweight
  39. * resolver packet structure #lwres_lwpacket_t *pkt in network byte
  40. * order to the lightweight resolver buffer, *b.
  41. *
  42. * lwres_lwpacket_parseheader() performs the converse operation. It
  43. * transfers data in network byte order from buffer *b to resolver
  44. * packet *pkt. The contents of the buffer b should correspond to a
  45. * #lwres_lwpacket_t.
  46. *
  47. * \section lwpacket_return Return Values
  48. *
  49. * Successful calls to lwres_lwpacket_renderheader() and
  50. * lwres_lwpacket_parseheader() return #LWRES_R_SUCCESS. If there is
  51. * insufficient space to copy data between the buffer *b and
  52. * lightweight resolver packet *pkt both functions return
  53. * #LWRES_R_UNEXPECTEDEND.
  54. */
  55. #include <config.h>
  56. #include <assert.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <lwres/lwbuffer.h>
  60. #include <lwres/lwpacket.h>
  61. #include <lwres/result.h>
  62. #include "assert_p.h"
  63. /*% Length of Packet */
  64. #define LWPACKET_LENGTH \
  65. (sizeof(lwres_uint16_t) * 4 + sizeof(lwres_uint32_t) * 5)
  66. /*% transfers the contents of lightweight resolver packet structure lwres_lwpacket_t *pkt in network byte order to the lightweight resolver buffer, *b. */
  67. lwres_result_t
  68. lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
  69. REQUIRE(b != NULL);
  70. REQUIRE(pkt != NULL);
  71. if (!SPACE_OK(b, LWPACKET_LENGTH))
  72. return (LWRES_R_UNEXPECTEDEND);
  73. lwres_buffer_putuint32(b, pkt->length);
  74. lwres_buffer_putuint16(b, pkt->version);
  75. lwres_buffer_putuint16(b, pkt->pktflags);
  76. lwres_buffer_putuint32(b, pkt->serial);
  77. lwres_buffer_putuint32(b, pkt->opcode);
  78. lwres_buffer_putuint32(b, pkt->result);
  79. lwres_buffer_putuint32(b, pkt->recvlength);
  80. lwres_buffer_putuint16(b, pkt->authtype);
  81. lwres_buffer_putuint16(b, pkt->authlength);
  82. return (LWRES_R_SUCCESS);
  83. }
  84. /*% transfers data in network byte order from buffer *b to resolver packet *pkt. The contents of the buffer b should correspond to a lwres_lwpacket_t. */
  85. lwres_result_t
  86. lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
  87. lwres_uint32_t space;
  88. REQUIRE(b != NULL);
  89. REQUIRE(pkt != NULL);
  90. space = LWRES_BUFFER_REMAINING(b);
  91. if (space < LWPACKET_LENGTH)
  92. return (LWRES_R_UNEXPECTEDEND);
  93. pkt->length = lwres_buffer_getuint32(b);
  94. /*
  95. * XXXBEW/MLG Checking that the buffer is long enough probably
  96. * shouldn't be done here, since this function is supposed to just
  97. * parse the header.
  98. */
  99. if (pkt->length > space)
  100. return (LWRES_R_UNEXPECTEDEND);
  101. pkt->version = lwres_buffer_getuint16(b);
  102. pkt->pktflags = lwres_buffer_getuint16(b);
  103. pkt->serial = lwres_buffer_getuint32(b);
  104. pkt->opcode = lwres_buffer_getuint32(b);
  105. pkt->result = lwres_buffer_getuint32(b);
  106. pkt->recvlength = lwres_buffer_getuint32(b);
  107. pkt->authtype = lwres_buffer_getuint16(b);
  108. pkt->authlength = lwres_buffer_getuint16(b);
  109. return (LWRES_R_SUCCESS);
  110. }