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.

98 lines
2.9 KiB

  1. /* udns_rr_txt.c
  2. parse/query TXT records
  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 <stdlib.h>
  20. #include <assert.h>
  21. #include "udns.h"
  22. int
  23. dns_parse_txt(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  24. void **result) {
  25. struct dns_rr_txt *ret;
  26. struct dns_parse p;
  27. struct dns_rr rr;
  28. int r, l;
  29. dnsc_t *sp;
  30. dnscc_t *cp, *ep;
  31. assert(dns_get16(cur+0) == DNS_T_TXT);
  32. /* first, validate the answer and count size of the result */
  33. l = 0;
  34. dns_initparse(&p, qdn, pkt, cur, end);
  35. while((r = dns_nextrr(&p, &rr)) > 0) {
  36. cp = rr.dnsrr_dptr; ep = rr.dnsrr_dend;
  37. while(cp < ep) {
  38. r = *cp++;
  39. if (cp + r > ep)
  40. return DNS_E_PROTOCOL;
  41. l += r;
  42. cp += r;
  43. }
  44. }
  45. if (r < 0)
  46. return DNS_E_PROTOCOL;
  47. if (!p.dnsp_nrr)
  48. return DNS_E_NODATA;
  49. /* next, allocate and set up result */
  50. l += (sizeof(struct dns_txt) + 1) * p.dnsp_nrr + dns_stdrr_size(&p);
  51. ret = malloc(sizeof(*ret) + l);
  52. if (!ret)
  53. return DNS_E_NOMEM;
  54. ret->dnstxt_nrr = p.dnsp_nrr;
  55. ret->dnstxt_txt = (struct dns_txt *)(ret+1);
  56. /* and 3rd, fill in result, finally */
  57. sp = (dnsc_t*)(ret->dnstxt_txt + p.dnsp_nrr);
  58. for(dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr) > 0; ++r) {
  59. ret->dnstxt_txt[r].txt = sp;
  60. cp = rr.dnsrr_dptr; ep = rr.dnsrr_dend;
  61. while(cp < ep) {
  62. l = *cp++;
  63. memcpy(sp, cp, l);
  64. sp += l;
  65. cp += l;
  66. }
  67. ret->dnstxt_txt[r].len = sp - ret->dnstxt_txt[r].txt;
  68. *sp++ = '\0';
  69. }
  70. dns_stdrr_finish((struct dns_rr_null *)ret, (char*)sp, &p);
  71. *result = ret;
  72. return 0;
  73. }
  74. struct dns_query *
  75. dns_submit_txt(struct dns_ctx *ctx, const char *name, int qcls, int flags,
  76. dns_query_txt_fn *cbck, void *data) {
  77. return
  78. dns_submit_p(ctx, name, qcls, DNS_T_TXT, flags,
  79. dns_parse_txt, (dns_query_fn *)cbck, data);
  80. }
  81. struct dns_rr_txt *
  82. dns_resolve_txt(struct dns_ctx *ctx, const char *name, int qcls, int flags) {
  83. return (struct dns_rr_txt *)
  84. dns_resolve_p(ctx, name, qcls, DNS_T_TXT, flags, dns_parse_txt);
  85. }