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.

128 lines
3.9 KiB

  1. /* udns_rr_naptr.c
  2. parse/query NAPTR IN records
  3. Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
  4. Copyright (C) 2006 Mikael Magnusson <mikma@users.sourceforge.net>
  5. This file is part of UDNS library, an async DNS stub resolver.
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library, in file named COPYING.LGPL; if not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place,
  17. Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include "udns.h"
  23. /* Get a single string for NAPTR record, pretty much like a DN label.
  24. * String length is in first byte in *cur, so it can't be >255.
  25. */
  26. static int dns_getstr(dnscc_t **cur, dnscc_t *ep, char *buf)
  27. {
  28. unsigned l;
  29. dnscc_t *cp = *cur;
  30. l = *cp++;
  31. if (cp + l > ep)
  32. return DNS_E_PROTOCOL;
  33. if (buf) {
  34. memcpy(buf, cp, l);
  35. buf[l] = '\0';
  36. }
  37. cp += l;
  38. *cur = cp;
  39. return l + 1;
  40. }
  41. int
  42. dns_parse_naptr(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  43. void **result) {
  44. struct dns_rr_naptr *ret;
  45. struct dns_parse p;
  46. struct dns_rr rr;
  47. int r, l;
  48. char *sp;
  49. dnsc_t dn[DNS_MAXDN];
  50. assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_NAPTR);
  51. /* first, validate the answer and count size of the result */
  52. l = 0;
  53. dns_initparse(&p, qdn, pkt, cur, end);
  54. while((r = dns_nextrr(&p, &rr)) > 0) {
  55. int i;
  56. dnscc_t *ep = rr.dnsrr_dend;
  57. /* first 4 bytes: order & preference */
  58. cur = rr.dnsrr_dptr + 4;
  59. /* flags, services and regexp */
  60. for (i = 0; i < 3; i++) {
  61. r = dns_getstr(&cur, ep, NULL);
  62. if (r < 0)
  63. return r;
  64. l += r;
  65. }
  66. /* replacement */
  67. r = dns_getdn(pkt, &cur, end, dn, sizeof(dn));
  68. if (r <= 0 || cur != rr.dnsrr_dend)
  69. return DNS_E_PROTOCOL;
  70. l += dns_dntop_size(dn);
  71. }
  72. if (r < 0)
  73. return DNS_E_PROTOCOL;
  74. if (!p.dnsp_nrr)
  75. return DNS_E_NODATA;
  76. /* next, allocate and set up result */
  77. l += dns_stdrr_size(&p);
  78. ret = malloc(sizeof(*ret) + sizeof(struct dns_naptr) * p.dnsp_nrr + l);
  79. if (!ret)
  80. return DNS_E_NOMEM;
  81. ret->dnsnaptr_nrr = p.dnsp_nrr;
  82. ret->dnsnaptr_naptr = (struct dns_naptr *)(ret+1);
  83. /* and 3rd, fill in result, finally */
  84. sp = (char*)(&ret->dnsnaptr_naptr[p.dnsp_nrr]);
  85. for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) {
  86. cur = rr.dnsrr_dptr;
  87. ret->dnsnaptr_naptr[r].order = dns_get16(cur); cur += 2;
  88. ret->dnsnaptr_naptr[r].preference = dns_get16(cur); cur += 2;
  89. sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].flags = sp));
  90. sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].service = sp));
  91. sp += dns_getstr(&cur, end, (ret->dnsnaptr_naptr[r].regexp = sp));
  92. dns_getdn(pkt, &cur, end, dn, sizeof(dn));
  93. sp += dns_dntop(dn, (ret->dnsnaptr_naptr[r].replacement = sp), DNS_MAXNAME);
  94. }
  95. dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p);
  96. *result = ret;
  97. return 0;
  98. }
  99. struct dns_query *
  100. dns_submit_naptr(struct dns_ctx *ctx, const char *name, int flags,
  101. dns_query_naptr_fn *cbck, void *data) {
  102. return
  103. dns_submit_p(ctx, name, DNS_C_IN, DNS_T_NAPTR, flags,
  104. dns_parse_naptr, (dns_query_fn *)cbck, data);
  105. }
  106. struct dns_rr_naptr *
  107. dns_resolve_naptr(struct dns_ctx *ctx, const char *name, int flags) {
  108. return (struct dns_rr_naptr *)
  109. dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_NAPTR, flags, dns_parse_naptr);
  110. }