ares_parse_a_reply.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* $Id$ */
  2. /* Copyright 1998 by the Massachusetts Institute of Technology.
  3. *
  4. * Permission to use, copy, modify, and distribute this
  5. * software and its documentation for any purpose and without
  6. * fee is hereby granted, provided that the above copyright
  7. * notice appear in all copies and that both that copyright
  8. * notice and this permission notice appear in supporting
  9. * documentation, and that the name of M.I.T. not be used in
  10. * advertising or publicity pertaining to distribution of the
  11. * software without specific, written prior permission.
  12. * M.I.T. makes no representations about the suitability of
  13. * this software for any purpose. It is provided "as is"
  14. * without express or implied warranty.
  15. */
  16. #include "setup.h"
  17. #ifdef HAVE_SYS_SOCKET_H
  18. # include <sys/socket.h>
  19. #endif
  20. #ifdef HAVE_NETINET_IN_H
  21. # include <netinet/in.h>
  22. #endif
  23. #ifdef HAVE_NETDB_H
  24. # include <netdb.h>
  25. #endif
  26. #ifdef HAVE_ARPA_INET_H
  27. # include <arpa/inet.h>
  28. #endif
  29. #ifdef HAVE_ARPA_NAMESER_H
  30. # include <arpa/nameser.h>
  31. #else
  32. # include "nameser.h"
  33. #endif
  34. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  35. # include <arpa/nameser_compat.h>
  36. #endif
  37. #ifdef HAVE_STRINGS_H
  38. # include <strings.h>
  39. #endif
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #ifdef HAVE_LIMITS_H
  43. # include <limits.h>
  44. #endif
  45. #include "ares.h"
  46. #include "ares_dns.h"
  47. #include "ares_private.h"
  48. int ares_parse_a_reply(const unsigned char *abuf, int alen,
  49. struct hostent **host,
  50. struct addrttl *addrttls, int *naddrttls)
  51. {
  52. unsigned int qdcount, ancount;
  53. int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
  54. int cname_ttl = INT_MAX; /* the TTL imposed by the CNAME chain */
  55. int naliases;
  56. long len;
  57. const unsigned char *aptr;
  58. char *hostname, *rr_name, *rr_data, **aliases;
  59. struct in_addr *addrs;
  60. struct hostent *hostent;
  61. const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
  62. /* Set *host to NULL for all failure cases. */
  63. if (host)
  64. *host = NULL;
  65. /* Same with *naddrttls. */
  66. if (naddrttls)
  67. *naddrttls = 0;
  68. /* Give up if abuf doesn't have room for a header. */
  69. if (alen < HFIXEDSZ)
  70. return ARES_EBADRESP;
  71. /* Fetch the question and answer count from the header. */
  72. qdcount = DNS_HEADER_QDCOUNT(abuf);
  73. ancount = DNS_HEADER_ANCOUNT(abuf);
  74. if (qdcount != 1)
  75. return ARES_EBADRESP;
  76. /* Expand the name from the question, and skip past the question. */
  77. aptr = abuf + HFIXEDSZ;
  78. status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
  79. if (status != ARES_SUCCESS)
  80. return status;
  81. if (aptr + len + QFIXEDSZ > abuf + alen)
  82. {
  83. free(hostname);
  84. return ARES_EBADRESP;
  85. }
  86. aptr += len + QFIXEDSZ;
  87. if (host)
  88. {
  89. /* Allocate addresses and aliases; ancount gives an upper bound for
  90. both. */
  91. addrs = malloc(ancount * sizeof(struct in_addr));
  92. if (!addrs)
  93. {
  94. free(hostname);
  95. return ARES_ENOMEM;
  96. }
  97. aliases = malloc((ancount + 1) * sizeof(char *));
  98. if (!aliases)
  99. {
  100. free(hostname);
  101. free(addrs);
  102. return ARES_ENOMEM;
  103. }
  104. }
  105. else
  106. {
  107. addrs = NULL;
  108. aliases = NULL;
  109. }
  110. naddrs = 0;
  111. naliases = 0;
  112. /* Examine each answer resource record (RR) in turn. */
  113. for (i = 0; i < (int)ancount; i++)
  114. {
  115. /* Decode the RR up to the data field. */
  116. status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
  117. if (status != ARES_SUCCESS)
  118. break;
  119. aptr += len;
  120. if (aptr + RRFIXEDSZ > abuf + alen)
  121. {
  122. status = ARES_EBADRESP;
  123. break;
  124. }
  125. rr_type = DNS_RR_TYPE(aptr);
  126. rr_class = DNS_RR_CLASS(aptr);
  127. rr_len = DNS_RR_LEN(aptr);
  128. rr_ttl = DNS_RR_TTL(aptr);
  129. aptr += RRFIXEDSZ;
  130. if (rr_class == C_IN && rr_type == T_A
  131. && rr_len == sizeof(struct in_addr)
  132. && strcasecmp(rr_name, hostname) == 0)
  133. {
  134. if (addrs)
  135. {
  136. if (aptr + sizeof(struct in_addr) > abuf + alen)
  137. {
  138. status = ARES_EBADRESP;
  139. break;
  140. }
  141. memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
  142. }
  143. if (naddrs < max_addr_ttls)
  144. {
  145. struct addrttl * const at = &addrttls[naddrs];
  146. if (aptr + sizeof(struct in_addr) > abuf + alen)
  147. {
  148. status = ARES_EBADRESP;
  149. break;
  150. }
  151. memcpy(&at->ipaddr, aptr, sizeof(struct in_addr));
  152. at->ttl = rr_ttl;
  153. }
  154. naddrs++;
  155. status = ARES_SUCCESS;
  156. }
  157. if (rr_class == C_IN && rr_type == T_CNAME)
  158. {
  159. /* Record the RR name as an alias. */
  160. if (aliases)
  161. aliases[naliases] = rr_name;
  162. else
  163. free(rr_name);
  164. naliases++;
  165. /* Decode the RR data and replace the hostname with it. */
  166. status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
  167. &len);
  168. if (status != ARES_SUCCESS)
  169. break;
  170. free(hostname);
  171. hostname = rr_data;
  172. /* Take the min of the TTLs we see in the CNAME chain. */
  173. if (cname_ttl > rr_ttl)
  174. cname_ttl = rr_ttl;
  175. }
  176. else
  177. free(rr_name);
  178. aptr += rr_len;
  179. if (aptr > abuf + alen)
  180. {
  181. status = ARES_EBADRESP;
  182. break;
  183. }
  184. }
  185. if (status == ARES_SUCCESS && naddrs == 0)
  186. status = ARES_ENODATA;
  187. if (status == ARES_SUCCESS)
  188. {
  189. /* We got our answer. */
  190. if (naddrttls)
  191. {
  192. const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
  193. for (i = 0; i < n; i++)
  194. {
  195. /* Ensure that each A TTL is no larger than the CNAME TTL. */
  196. if (addrttls[i].ttl > cname_ttl)
  197. addrttls[i].ttl = cname_ttl;
  198. }
  199. *naddrttls = n;
  200. }
  201. if (aliases)
  202. aliases[naliases] = NULL;
  203. if (host)
  204. {
  205. /* Allocate memory to build the host entry. */
  206. hostent = malloc(sizeof(struct hostent));
  207. if (hostent)
  208. {
  209. hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
  210. if (hostent->h_addr_list)
  211. {
  212. /* Fill in the hostent and return successfully. */
  213. hostent->h_name = hostname;
  214. hostent->h_aliases = aliases;
  215. hostent->h_addrtype = AF_INET;
  216. hostent->h_length = sizeof(struct in_addr);
  217. for (i = 0; i < naddrs; i++)
  218. hostent->h_addr_list[i] = (char *) &addrs[i];
  219. hostent->h_addr_list[naddrs] = NULL;
  220. *host = hostent;
  221. return ARES_SUCCESS;
  222. }
  223. free(hostent);
  224. }
  225. status = ARES_ENOMEM;
  226. }
  227. }
  228. if (aliases)
  229. {
  230. for (i = 0; i < naliases; i++)
  231. free(aliases[i]);
  232. free(aliases);
  233. }
  234. free(addrs);
  235. free(hostname);
  236. return status;
  237. }