ares_parse_aaaa_reply.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright 1998 by the Massachusetts Institute of Technology.
  2. * Copyright 2005 Dominick Meglio
  3. * Copyright (C) 2019 by Andrew Selivanov
  4. *
  5. * Permission to use, copy, modify, and distribute this
  6. * software and its documentation for any purpose and without
  7. * fee is hereby granted, provided that the above copyright
  8. * notice appear in all copies and that both that copyright
  9. * notice and this permission notice appear in supporting
  10. * documentation, and that the name of M.I.T. not be used in
  11. * advertising or publicity pertaining to distribution of the
  12. * software without specific, written prior permission.
  13. * M.I.T. makes no representations about the suitability of
  14. * this software for any purpose. It is provided "as is"
  15. * without express or implied warranty.
  16. */
  17. #include "ares_setup.h"
  18. #ifdef HAVE_NETINET_IN_H
  19. # include <netinet/in.h>
  20. #endif
  21. #ifdef HAVE_NETDB_H
  22. # include <netdb.h>
  23. #endif
  24. #ifdef HAVE_ARPA_INET_H
  25. # include <arpa/inet.h>
  26. #endif
  27. #ifdef HAVE_ARPA_NAMESER_H
  28. # include <arpa/nameser.h>
  29. #else
  30. # include "nameser.h"
  31. #endif
  32. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  33. # include <arpa/nameser_compat.h>
  34. #endif
  35. #ifdef HAVE_STRINGS_H
  36. # include <strings.h>
  37. #endif
  38. #ifdef HAVE_LIMITS_H
  39. # include <limits.h>
  40. #endif
  41. #include "ares.h"
  42. #include "ares_dns.h"
  43. #include "ares_inet_net_pton.h"
  44. #include "ares_private.h"
  45. int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
  46. struct hostent **host, struct ares_addr6ttl *addrttls,
  47. int *naddrttls)
  48. {
  49. struct ares_addrinfo ai;
  50. struct ares_addrinfo_node *next;
  51. struct ares_addrinfo_cname *next_cname;
  52. char **aliases = NULL;
  53. char *question_hostname = NULL;
  54. struct hostent *hostent = NULL;
  55. struct ares_in6_addr *addrs = NULL;
  56. int naliases = 0, naddrs = 0, alias = 0, i;
  57. int cname_ttl = INT_MAX;
  58. int status;
  59. memset(&ai, 0, sizeof(ai));
  60. status = ares__parse_into_addrinfo2(abuf, alen, &question_hostname, &ai);
  61. if (status != ARES_SUCCESS)
  62. {
  63. ares_free(question_hostname);
  64. if (naddrttls)
  65. {
  66. *naddrttls = 0;
  67. }
  68. return status;
  69. }
  70. hostent = ares_malloc(sizeof(struct hostent));
  71. if (!hostent)
  72. {
  73. goto enomem;
  74. }
  75. next = ai.nodes;
  76. while (next)
  77. {
  78. if(next->ai_family == AF_INET6)
  79. {
  80. ++naddrs;
  81. }
  82. next = next->ai_next;
  83. }
  84. next_cname = ai.cnames;
  85. while (next_cname)
  86. {
  87. if(next_cname->alias)
  88. ++naliases;
  89. next_cname = next_cname->next;
  90. }
  91. aliases = ares_malloc((naliases + 1) * sizeof(char *));
  92. if (!aliases)
  93. {
  94. goto enomem;
  95. }
  96. if (naliases)
  97. {
  98. next_cname = ai.cnames;
  99. while (next_cname)
  100. {
  101. if(next_cname->alias)
  102. aliases[alias++] = strdup(next_cname->alias);
  103. if(next_cname->ttl < cname_ttl)
  104. cname_ttl = next_cname->ttl;
  105. next_cname = next_cname->next;
  106. }
  107. }
  108. aliases[alias] = NULL;
  109. hostent->h_addr_list = ares_malloc((naddrs + 1) * sizeof(char *));
  110. if (!hostent->h_addr_list)
  111. {
  112. goto enomem;
  113. }
  114. for (i = 0; i < naddrs + 1; ++i)
  115. {
  116. hostent->h_addr_list[i] = NULL;
  117. }
  118. if (ai.cnames)
  119. {
  120. hostent->h_name = strdup(ai.cnames->name);
  121. ares_free(question_hostname);
  122. }
  123. else
  124. {
  125. hostent->h_name = question_hostname;
  126. }
  127. hostent->h_aliases = aliases;
  128. hostent->h_addrtype = AF_INET6;
  129. hostent->h_length = sizeof(struct ares_in6_addr);
  130. if (naddrs)
  131. {
  132. addrs = ares_malloc(naddrs * sizeof(struct ares_in6_addr));
  133. if (!addrs)
  134. {
  135. goto enomem;
  136. }
  137. i = 0;
  138. next = ai.nodes;
  139. while (next)
  140. {
  141. if(next->ai_family == AF_INET6)
  142. {
  143. hostent->h_addr_list[i] = (char*)&addrs[i];
  144. memcpy(hostent->h_addr_list[i],
  145. &(CARES_INADDR_CAST(struct sockaddr_in6 *, next->ai_addr)->sin6_addr),
  146. sizeof(struct ares_in6_addr));
  147. if (naddrttls && i < *naddrttls)
  148. {
  149. if(next->ai_ttl > cname_ttl)
  150. addrttls[i].ttl = cname_ttl;
  151. else
  152. addrttls[i].ttl = next->ai_ttl;
  153. memcpy(&addrttls[i].ip6addr,
  154. &(CARES_INADDR_CAST(struct sockaddr_in6 *, next->ai_addr)->sin6_addr),
  155. sizeof(struct ares_in6_addr));
  156. }
  157. ++i;
  158. }
  159. next = next->ai_next;
  160. }
  161. if (i == 0)
  162. {
  163. ares_free(addrs);
  164. }
  165. }
  166. if (host)
  167. {
  168. *host = hostent;
  169. }
  170. else
  171. {
  172. ares_free_hostent(hostent);
  173. }
  174. if (naddrttls)
  175. {
  176. *naddrttls = naddrs;
  177. }
  178. ares__freeaddrinfo_cnames(ai.cnames);
  179. ares__freeaddrinfo_nodes(ai.nodes);
  180. return ARES_SUCCESS;
  181. enomem:
  182. ares_free(aliases);
  183. ares_free(hostent);
  184. ares__freeaddrinfo_cnames(ai.cnames);
  185. ares__freeaddrinfo_nodes(ai.nodes);
  186. ares_free(question_hostname);
  187. return ARES_ENOMEM;
  188. }