You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.4 KiB

  1. /* udns_parse.c
  2. raw DNS packet parsing routines
  3. Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
  4. This file is part of UDNS library, an async DNS stub resolver.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library, in file named COPYING.LGPL; if not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place,
  16. Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <string.h>
  19. #include <assert.h>
  20. #include "udns.h"
  21. dnscc_t *dns_skipdn(dnscc_t *cur, dnscc_t *end) {
  22. unsigned c;
  23. for(;;) {
  24. if (cur >= end)
  25. return NULL;
  26. c = *cur++;
  27. if (!c)
  28. return cur;
  29. if (c & 192) /* jump */
  30. return cur + 1 >= end ? NULL : cur + 1;
  31. cur += c;
  32. }
  33. }
  34. int
  35. dns_getdn(dnscc_t *pkt, dnscc_t **cur, dnscc_t *end,
  36. register dnsc_t *dn, unsigned dnsiz) {
  37. unsigned c;
  38. dnscc_t *pp = *cur; /* current packet pointer */
  39. dnsc_t *dp = dn; /* current dn pointer */
  40. dnsc_t *const de /* end of the DN dest */
  41. = dn + (dnsiz < DNS_MAXDN ? dnsiz : DNS_MAXDN);
  42. dnscc_t *jump = NULL; /* ptr after first jump if any */
  43. unsigned loop = 100; /* jump loop counter */
  44. for(;;) { /* loop by labels */
  45. if (pp >= end) /* reached end of packet? */
  46. return -1;
  47. c = *pp++; /* length of the label */
  48. if (!c) { /* empty label: terminate */
  49. if (dn >= de) /* can't fit terminator */
  50. goto noroom;
  51. *dp++ = 0;
  52. /* return next pos: either after the first jump or current */
  53. *cur = jump ? jump : pp;
  54. return dp - dn;
  55. }
  56. if (c & 192) { /* jump */
  57. if (pp >= end) /* eop instead of jump pos */
  58. return -1;
  59. if (!jump) jump = pp + 1; /* remember first jump */
  60. else if (!--loop) return -1; /* too many jumps */
  61. c = ((c & ~192) << 8) | *pp; /* new pos */
  62. if (c < DNS_HSIZE) /* don't allow jump into the header */
  63. return -1;
  64. pp = pkt + c;
  65. continue;
  66. }
  67. if (c > DNS_MAXLABEL) /* too long label? */
  68. return -1;
  69. if (pp + c > end) /* label does not fit in packet? */
  70. return -1;
  71. if (dp + c + 1 > de) /* if enouth room for the label */
  72. goto noroom;
  73. *dp++ = c; /* label length */
  74. memcpy(dp, pp, c); /* and the label itself */
  75. dp += c;
  76. pp += c; /* advance to the next label */
  77. }
  78. noroom:
  79. return dnsiz < DNS_MAXDN ? 0 : -1;
  80. }
  81. void dns_rewind(struct dns_parse *p, dnscc_t *qdn) {
  82. p->dnsp_qdn = qdn;
  83. p->dnsp_cur = p->dnsp_ans;
  84. p->dnsp_rrl = dns_numan(p->dnsp_pkt);
  85. p->dnsp_ttl = 0xffffffffu;
  86. p->dnsp_nrr = 0;
  87. }
  88. void
  89. dns_initparse(struct dns_parse *p, dnscc_t *qdn,
  90. dnscc_t *pkt, dnscc_t *cur, dnscc_t *end) {
  91. p->dnsp_pkt = pkt;
  92. p->dnsp_end = end;
  93. p->dnsp_rrl = dns_numan(pkt);
  94. p->dnsp_qdn = qdn;
  95. assert(cur + 4 <= end);
  96. if ((p->dnsp_qtyp = dns_get16(cur+0)) == DNS_T_ANY) p->dnsp_qtyp = 0;
  97. if ((p->dnsp_qcls = dns_get16(cur+2)) == DNS_C_ANY) p->dnsp_qcls = 0;
  98. p->dnsp_cur = p->dnsp_ans = cur + 4;
  99. p->dnsp_ttl = 0xffffffffu;
  100. p->dnsp_nrr = 0;
  101. }
  102. int dns_nextrr(struct dns_parse *p, struct dns_rr *rr) {
  103. dnscc_t *cur = p->dnsp_cur;
  104. while(p->dnsp_rrl > 0) {
  105. --p->dnsp_rrl;
  106. if (dns_getdn(p->dnsp_pkt, &cur, p->dnsp_end,
  107. rr->dnsrr_dn, sizeof(rr->dnsrr_dn)) <= 0)
  108. return -1;
  109. if (cur + 10 > p->dnsp_end)
  110. return -1;
  111. rr->dnsrr_typ = dns_get16(cur);
  112. rr->dnsrr_cls = dns_get16(cur+2);
  113. rr->dnsrr_ttl = dns_get32(cur+4);
  114. rr->dnsrr_dsz = dns_get16(cur+8);
  115. rr->dnsrr_dptr = cur = cur + 10;
  116. rr->dnsrr_dend = cur = cur + rr->dnsrr_dsz;
  117. if (cur > p->dnsp_end)
  118. return -1;
  119. if (p->dnsp_qdn && !dns_dnequal(p->dnsp_qdn, rr->dnsrr_dn))
  120. continue;
  121. if ((!p->dnsp_qcls || p->dnsp_qcls == rr->dnsrr_cls) &&
  122. (!p->dnsp_qtyp || p->dnsp_qtyp == rr->dnsrr_typ)) {
  123. p->dnsp_cur = cur;
  124. ++p->dnsp_nrr;
  125. if (p->dnsp_ttl > rr->dnsrr_ttl) p->dnsp_ttl = rr->dnsrr_ttl;
  126. return 1;
  127. }
  128. if (p->dnsp_qdn && rr->dnsrr_typ == DNS_T_CNAME && !p->dnsp_nrr) {
  129. if (dns_getdn(p->dnsp_pkt, &rr->dnsrr_dptr, p->dnsp_end,
  130. p->dnsp_dnbuf, sizeof(p->dnsp_dnbuf)) <= 0 ||
  131. rr->dnsrr_dptr != rr->dnsrr_dend)
  132. return -1;
  133. p->dnsp_qdn = p->dnsp_dnbuf;
  134. if (p->dnsp_ttl > rr->dnsrr_ttl) p->dnsp_ttl = rr->dnsrr_ttl;
  135. }
  136. }
  137. p->dnsp_cur = cur;
  138. return 0;
  139. }
  140. int dns_stdrr_size(const struct dns_parse *p) {
  141. return
  142. dns_dntop_size(p->dnsp_qdn) +
  143. (p->dnsp_qdn == dns_payload(p->dnsp_pkt) ? 0 :
  144. dns_dntop_size(dns_payload(p->dnsp_pkt)));
  145. }
  146. void *dns_stdrr_finish(struct dns_rr_null *ret, char *cp,
  147. const struct dns_parse *p) {
  148. cp += dns_dntop(p->dnsp_qdn, (ret->dnsn_cname = cp), DNS_MAXNAME);
  149. if (p->dnsp_qdn == dns_payload(p->dnsp_pkt))
  150. ret->dnsn_qname = ret->dnsn_cname;
  151. else
  152. dns_dntop(dns_payload(p->dnsp_pkt), (ret->dnsn_qname = cp), DNS_MAXNAME);
  153. ret->dnsn_ttl = p->dnsp_ttl;
  154. return ret;
  155. }