ares_parse_ns_reply.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright 1998 by the Massachusetts Institute of Technology.
  2. *
  3. * Permission to use, copy, modify, and distribute this
  4. * software and its documentation for any purpose and without
  5. * fee is hereby granted, provided that the above copyright
  6. * notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting
  8. * documentation, and that the name of M.I.T. not be used in
  9. * advertising or publicity pertaining to distribution of the
  10. * software without specific, written prior permission.
  11. * M.I.T. makes no representations about the suitability of
  12. * this software for any purpose. It is provided "as is"
  13. * without express or implied warranty.
  14. */
  15. /*
  16. * ares_parse_ns_reply created by Vlad Dinulescu <vlad.dinulescu@avira.com>
  17. * on behalf of AVIRA Gmbh - http://www.avira.com
  18. */
  19. #include "ares_setup.h"
  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. #include "ares.h"
  38. #include "ares_dns.h"
  39. #include "ares_private.h"
  40. int ares_parse_ns_reply( const unsigned char* abuf, int alen,
  41. struct hostent** host )
  42. {
  43. unsigned int qdcount, ancount;
  44. int status, i, rr_type, rr_class, rr_len;
  45. int nameservers_num;
  46. long len;
  47. const unsigned char *aptr;
  48. char* hostname, *rr_name, *rr_data, **nameservers;
  49. struct hostent *hostent;
  50. /* Set *host to NULL for all failure cases. */
  51. *host = NULL;
  52. /* Give up if abuf doesn't have room for a header. */
  53. if ( alen < HFIXEDSZ )
  54. return ARES_EBADRESP;
  55. /* Fetch the question and answer count from the header. */
  56. qdcount = DNS_HEADER_QDCOUNT( abuf );
  57. ancount = DNS_HEADER_ANCOUNT( abuf );
  58. if ( qdcount != 1 )
  59. return ARES_EBADRESP;
  60. /* Expand the name from the question, and skip past the question. */
  61. aptr = abuf + HFIXEDSZ;
  62. status = ares__expand_name_for_response( aptr, abuf, alen, &hostname, &len);
  63. if ( status != ARES_SUCCESS )
  64. return status;
  65. if ( aptr + len + QFIXEDSZ > abuf + alen )
  66. {
  67. ares_free( hostname );
  68. return ARES_EBADRESP;
  69. }
  70. aptr += len + QFIXEDSZ;
  71. /* Allocate nameservers array; ancount gives an upper bound */
  72. nameservers = ares_malloc( ( ancount + 1 ) * sizeof( char * ) );
  73. if ( !nameservers )
  74. {
  75. ares_free( hostname );
  76. return ARES_ENOMEM;
  77. }
  78. nameservers_num = 0;
  79. /* Examine each answer resource record (RR) in turn. */
  80. for ( i = 0; i < ( int ) ancount; i++ )
  81. {
  82. /* Decode the RR up to the data field. */
  83. status = ares__expand_name_for_response( aptr, abuf, alen, &rr_name, &len );
  84. if ( status != ARES_SUCCESS )
  85. break;
  86. aptr += len;
  87. if ( aptr + RRFIXEDSZ > abuf + alen )
  88. {
  89. status = ARES_EBADRESP;
  90. ares_free(rr_name);
  91. break;
  92. }
  93. rr_type = DNS_RR_TYPE( aptr );
  94. rr_class = DNS_RR_CLASS( aptr );
  95. rr_len = DNS_RR_LEN( aptr );
  96. aptr += RRFIXEDSZ;
  97. if (aptr + rr_len > abuf + alen)
  98. {
  99. ares_free(rr_name);
  100. status = ARES_EBADRESP;
  101. break;
  102. }
  103. if ( rr_class == C_IN && rr_type == T_NS )
  104. {
  105. /* Decode the RR data and add it to the nameservers list */
  106. status = ares__expand_name_for_response( aptr, abuf, alen, &rr_data,
  107. &len);
  108. if ( status != ARES_SUCCESS )
  109. {
  110. ares_free(rr_name);
  111. break;
  112. }
  113. nameservers[nameservers_num] = ares_malloc(strlen(rr_data)+1);
  114. if (nameservers[nameservers_num]==NULL)
  115. {
  116. ares_free(rr_name);
  117. ares_free(rr_data);
  118. status=ARES_ENOMEM;
  119. break;
  120. }
  121. strcpy(nameservers[nameservers_num],rr_data);
  122. ares_free(rr_data);
  123. nameservers_num++;
  124. }
  125. ares_free( rr_name );
  126. aptr += rr_len;
  127. if ( aptr > abuf + alen )
  128. { /* LCOV_EXCL_START: already checked above */
  129. status = ARES_EBADRESP;
  130. break;
  131. } /* LCOV_EXCL_STOP */
  132. }
  133. if ( status == ARES_SUCCESS && nameservers_num == 0 )
  134. {
  135. status = ARES_ENODATA;
  136. }
  137. if ( status == ARES_SUCCESS )
  138. {
  139. /* We got our answer. Allocate memory to build the host entry. */
  140. nameservers[nameservers_num] = NULL;
  141. hostent = ares_malloc( sizeof( struct hostent ) );
  142. if ( hostent )
  143. {
  144. hostent->h_addr_list = ares_malloc( 1 * sizeof( char * ) );
  145. if ( hostent->h_addr_list )
  146. {
  147. /* Fill in the hostent and return successfully. */
  148. hostent->h_name = hostname;
  149. hostent->h_aliases = nameservers;
  150. hostent->h_addrtype = AF_INET;
  151. hostent->h_length = sizeof( struct in_addr );
  152. hostent->h_addr_list[0] = NULL;
  153. *host = hostent;
  154. return ARES_SUCCESS;
  155. }
  156. ares_free( hostent );
  157. }
  158. status = ARES_ENOMEM;
  159. }
  160. for ( i = 0; i < nameservers_num; i++ )
  161. ares_free( nameservers[i] );
  162. ares_free( nameservers );
  163. ares_free( hostname );
  164. return status;
  165. }