ares_parse_ns_reply.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "setup.h"
  20. #ifdef HAVE_SYS_SOCKET_H
  21. # include <sys/socket.h>
  22. #endif
  23. #ifdef HAVE_NETINET_IN_H
  24. # include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETDB_H
  27. # include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. # include <arpa/inet.h>
  31. #endif
  32. #ifdef HAVE_ARPA_NAMESER_H
  33. # include <arpa/nameser.h>
  34. #else
  35. # include "nameser.h"
  36. #endif
  37. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  38. # include <arpa/nameser_compat.h>
  39. #endif
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "ares.h"
  43. #include "ares_dns.h"
  44. #include "ares_private.h"
  45. int ares_parse_ns_reply( const unsigned char* abuf, int alen,
  46. struct hostent** host )
  47. {
  48. unsigned int qdcount, ancount;
  49. int status, i, rr_type, rr_class, rr_len;
  50. int nameservers_num;
  51. long len;
  52. const unsigned char *aptr;
  53. char* hostname, *rr_name, *rr_data, **nameservers;
  54. struct hostent *hostent;
  55. /* Set *host to NULL for all failure cases. */
  56. *host = NULL;
  57. /* Give up if abuf doesn't have room for a header. */
  58. if ( alen < HFIXEDSZ )
  59. return ARES_EBADRESP;
  60. /* Fetch the question and answer count from the header. */
  61. qdcount = DNS_HEADER_QDCOUNT( abuf );
  62. ancount = DNS_HEADER_ANCOUNT( abuf );
  63. if ( qdcount != 1 )
  64. return ARES_EBADRESP;
  65. /* Expand the name from the question, and skip past the question. */
  66. aptr = abuf + HFIXEDSZ;
  67. status = ares__expand_name_for_response( aptr, abuf, alen, &hostname, &len);
  68. if ( status != ARES_SUCCESS )
  69. return status;
  70. if ( aptr + len + QFIXEDSZ > abuf + alen )
  71. {
  72. free( hostname );
  73. return ARES_EBADRESP;
  74. }
  75. aptr += len + QFIXEDSZ;
  76. /* Allocate nameservers array; ancount gives an upper bound */
  77. nameservers = malloc( ( ancount + 1 ) * sizeof( char * ) );
  78. if ( !nameservers )
  79. {
  80. free( hostname );
  81. return ARES_ENOMEM;
  82. }
  83. nameservers_num = 0;
  84. /* Examine each answer resource record (RR) in turn. */
  85. for ( i = 0; i < ( int ) ancount; i++ )
  86. {
  87. /* Decode the RR up to the data field. */
  88. status = ares__expand_name_for_response( aptr, abuf, alen, &rr_name, &len );
  89. if ( status != ARES_SUCCESS )
  90. break;
  91. aptr += len;
  92. if ( aptr + RRFIXEDSZ > abuf + alen )
  93. {
  94. status = ARES_EBADRESP;
  95. break;
  96. }
  97. rr_type = DNS_RR_TYPE( aptr );
  98. rr_class = DNS_RR_CLASS( aptr );
  99. rr_len = DNS_RR_LEN( aptr );
  100. aptr += RRFIXEDSZ;
  101. if ( rr_class == C_IN && rr_type == T_NS )
  102. {
  103. /* Decode the RR data and add it to the nameservers list */
  104. status = ares__expand_name_for_response( aptr, abuf, alen, &rr_data,
  105. &len);
  106. if ( status != ARES_SUCCESS )
  107. {
  108. break;
  109. }
  110. nameservers[nameservers_num] = malloc(strlen(rr_data)+1);
  111. if (nameservers[nameservers_num]==NULL)
  112. {
  113. free(rr_name);
  114. free(rr_data);
  115. status=ARES_ENOMEM;
  116. break;
  117. }
  118. strcpy(nameservers[nameservers_num],rr_data);
  119. free(rr_data);
  120. nameservers_num++;
  121. }
  122. free( rr_name );
  123. aptr += rr_len;
  124. if ( aptr > abuf + alen )
  125. {
  126. status = ARES_EBADRESP;
  127. break;
  128. }
  129. }
  130. if ( status == ARES_SUCCESS && nameservers_num == 0 )
  131. {
  132. status = ARES_ENODATA;
  133. }
  134. if ( status == ARES_SUCCESS )
  135. {
  136. /* We got our answer. Allocate memory to build the host entry. */
  137. nameservers[nameservers_num] = NULL;
  138. hostent = malloc( sizeof( struct hostent ) );
  139. if ( hostent )
  140. {
  141. hostent->h_addr_list = malloc( 1 * sizeof( char * ) );
  142. if ( hostent->h_addr_list )
  143. {
  144. /* Fill in the hostent and return successfully. */
  145. hostent->h_name = hostname;
  146. hostent->h_aliases = nameservers;
  147. hostent->h_addrtype = AF_INET;
  148. hostent->h_length = sizeof( struct in_addr );
  149. hostent->h_addr_list[0] = NULL;
  150. *host = hostent;
  151. return ARES_SUCCESS;
  152. }
  153. free( hostent );
  154. }
  155. status = ARES_ENOMEM;
  156. }
  157. for ( i = 0; i < nameservers_num; i++ )
  158. free( nameservers[i] );
  159. free( nameservers );
  160. free( hostname );
  161. return status;
  162. }