dns_txt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <arpa/nameser.h>
  23. #include <resolv.h>
  24. #include <netdb.h>
  25. #include "dns_txt.h"
  26. #ifndef C_IN
  27. #define C_IN ns_c_in
  28. #endif
  29. #ifndef T_TXT
  30. #define T_TXT ns_t_txt
  31. #endif
  32. static volatile int Anode_resolver_initialized = 0;
  33. int Anode_sync_resolve_txt(const char *host,char *txt,unsigned int txt_len)
  34. {
  35. unsigned char answer[16384],*pptr,*end;
  36. char name[16384];
  37. int len,explen,i;
  38. if (!Anode_resolver_initialized) {
  39. Anode_resolver_initialized = 1;
  40. res_init();
  41. }
  42. /* Do not taunt happy fun ball. */
  43. len = res_search(host,C_IN,T_TXT,answer,sizeof(answer));
  44. if (len > 12) {
  45. pptr = answer + 12;
  46. end = answer + len;
  47. explen = dn_expand(answer,end,pptr,name,sizeof(name));
  48. if (explen > 0) {
  49. pptr += explen;
  50. if ((pptr + 2) >= end) return 2;
  51. if (ntohs(*((uint16_t *)pptr)) == T_TXT) {
  52. pptr += 4;
  53. if (pptr >= end) return 2;
  54. explen = dn_expand(answer,end,pptr,name,sizeof(name));
  55. if (explen > 0) {
  56. pptr += explen;
  57. if ((pptr + 2) >= end) return 2;
  58. if (ntohs(*((uint16_t *)pptr)) == T_TXT) {
  59. pptr += 10;
  60. if (pptr >= end) return 2;
  61. len = *(pptr++);
  62. if (len <= 0) return 2;
  63. if ((pptr + len) > end) return 2;
  64. if (txt_len < (len + 1))
  65. return 4;
  66. else {
  67. for(i=0;i<len;++i)
  68. txt[i] = pptr[i];
  69. txt[len] = (char)0;
  70. return 0;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return 1;
  78. }