ns_parse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1996,1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and 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
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* Import. */
  18. #include <sys/types.h>
  19. #include <netinet/in.h>
  20. #include <arpa/nameser.h>
  21. #include <errno.h>
  22. #include <resolv.h>
  23. #include <string.h>
  24. /* Forward. */
  25. static void setsection(ns_msg *msg, ns_sect sect);
  26. /* Macros. */
  27. #define RETERR(err) do { __set_errno (err); return (-1); } while (0)
  28. /* Public. */
  29. /* These need to be in the same order as the nres.h:ns_flag enum. */
  30. const struct _ns_flagdata _ns_flagdata[16] = {
  31. { 0x8000, 15 }, /*%< qr. */
  32. { 0x7800, 11 }, /*%< opcode. */
  33. { 0x0400, 10 }, /*%< aa. */
  34. { 0x0200, 9 }, /*%< tc. */
  35. { 0x0100, 8 }, /*%< rd. */
  36. { 0x0080, 7 }, /*%< ra. */
  37. { 0x0040, 6 }, /*%< z. */
  38. { 0x0020, 5 }, /*%< ad. */
  39. { 0x0010, 4 }, /*%< cd. */
  40. { 0x000f, 0 }, /*%< rcode. */
  41. { 0x0000, 0 }, /*%< expansion (1/6). */
  42. { 0x0000, 0 }, /*%< expansion (2/6). */
  43. { 0x0000, 0 }, /*%< expansion (3/6). */
  44. { 0x0000, 0 }, /*%< expansion (4/6). */
  45. { 0x0000, 0 }, /*%< expansion (5/6). */
  46. { 0x0000, 0 }, /*%< expansion (6/6). */
  47. };
  48. #undef ns_msg_getflag
  49. int ns_msg_getflag(ns_msg handle, int flag) {
  50. return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
  51. }
  52. int
  53. ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
  54. const u_char *optr = ptr;
  55. for ((void)NULL; count > 0; count--) {
  56. int b, rdlength;
  57. b = dn_skipname(ptr, eom);
  58. if (b < 0)
  59. RETERR(EMSGSIZE);
  60. ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
  61. if (section != ns_s_qd) {
  62. if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
  63. RETERR(EMSGSIZE);
  64. ptr += NS_INT32SZ/*TTL*/;
  65. NS_GET16(rdlength, ptr);
  66. ptr += rdlength/*RData*/;
  67. }
  68. }
  69. if (ptr > eom)
  70. RETERR(EMSGSIZE);
  71. return (ptr - optr);
  72. }
  73. libresolv_hidden_def (ns_skiprr)
  74. int
  75. ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
  76. const u_char *eom = msg + msglen;
  77. int i;
  78. memset(handle, 0x5e, sizeof *handle);
  79. handle->_msg = msg;
  80. handle->_eom = eom;
  81. if (msg + NS_INT16SZ > eom)
  82. RETERR(EMSGSIZE);
  83. NS_GET16(handle->_id, msg);
  84. if (msg + NS_INT16SZ > eom)
  85. RETERR(EMSGSIZE);
  86. NS_GET16(handle->_flags, msg);
  87. for (i = 0; i < ns_s_max; i++) {
  88. if (msg + NS_INT16SZ > eom)
  89. RETERR(EMSGSIZE);
  90. NS_GET16(handle->_counts[i], msg);
  91. }
  92. for (i = 0; i < ns_s_max; i++)
  93. if (handle->_counts[i] == 0)
  94. handle->_sections[i] = NULL;
  95. else {
  96. int b = ns_skiprr(msg, eom, (ns_sect)i,
  97. handle->_counts[i]);
  98. if (b < 0)
  99. return (-1);
  100. handle->_sections[i] = msg;
  101. msg += b;
  102. }
  103. if (msg != eom)
  104. RETERR(EMSGSIZE);
  105. setsection(handle, ns_s_max);
  106. return (0);
  107. }
  108. libresolv_hidden_def (ns_initparse)
  109. int
  110. ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
  111. int b;
  112. int tmp;
  113. /* Make section right. */
  114. tmp = section;
  115. if (tmp < 0 || section >= ns_s_max)
  116. RETERR(ENODEV);
  117. if (section != handle->_sect)
  118. setsection(handle, section);
  119. /* Make rrnum right. */
  120. if (rrnum == -1)
  121. rrnum = handle->_rrnum;
  122. if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
  123. RETERR(ENODEV);
  124. if (rrnum < handle->_rrnum)
  125. setsection(handle, section);
  126. if (rrnum > handle->_rrnum) {
  127. b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
  128. rrnum - handle->_rrnum);
  129. if (b < 0)
  130. return (-1);
  131. handle->_msg_ptr += b;
  132. handle->_rrnum = rrnum;
  133. }
  134. /* Do the parse. */
  135. b = dn_expand(handle->_msg, handle->_eom,
  136. handle->_msg_ptr, rr->name, NS_MAXDNAME);
  137. if (b < 0)
  138. return (-1);
  139. handle->_msg_ptr += b;
  140. if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
  141. RETERR(EMSGSIZE);
  142. NS_GET16(rr->type, handle->_msg_ptr);
  143. NS_GET16(rr->rr_class, handle->_msg_ptr);
  144. if (section == ns_s_qd) {
  145. rr->ttl = 0;
  146. rr->rdlength = 0;
  147. rr->rdata = NULL;
  148. } else {
  149. if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
  150. RETERR(EMSGSIZE);
  151. NS_GET32(rr->ttl, handle->_msg_ptr);
  152. NS_GET16(rr->rdlength, handle->_msg_ptr);
  153. if (handle->_msg_ptr + rr->rdlength > handle->_eom)
  154. RETERR(EMSGSIZE);
  155. rr->rdata = handle->_msg_ptr;
  156. handle->_msg_ptr += rr->rdlength;
  157. }
  158. if (++handle->_rrnum > handle->_counts[(int)section])
  159. setsection(handle, (ns_sect)((int)section + 1));
  160. /* All done. */
  161. return (0);
  162. }
  163. libresolv_hidden_def (ns_parserr)
  164. /* Private. */
  165. static void
  166. setsection(ns_msg *msg, ns_sect sect) {
  167. msg->_sect = sect;
  168. if (sect == ns_s_max) {
  169. msg->_rrnum = -1;
  170. msg->_msg_ptr = NULL;
  171. } else {
  172. msg->_rrnum = 0;
  173. msg->_msg_ptr = msg->_sections[(int)sect];
  174. }
  175. }
  176. /*! \file */