3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
19 * ares_parse_ns_reply created by Vlad Dinulescu <vlad.dinulescu@avira.com>
20 * on behalf of AVIRA Gmbh - http://www.avira.com
23 #include "ares_setup.h"
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
28 #ifdef HAVE_NETINET_IN_H
29 # include <netinet/in.h>
34 #ifdef HAVE_ARPA_INET_H
35 # include <arpa/inet.h>
37 #ifdef HAVE_ARPA_NAMESER_H
38 # include <arpa/nameser.h>
42 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
43 # include <arpa/nameser_compat.h>
50 #include "ares_private.h"
52 int ares_parse_ns_reply( const unsigned char* abuf, int alen,
53 struct hostent** host )
55 unsigned int qdcount, ancount;
56 int status, i, rr_type, rr_class, rr_len;
59 const unsigned char *aptr;
60 char* hostname, *rr_name, *rr_data, **nameservers;
61 struct hostent *hostent;
63 /* Set *host to NULL for all failure cases. */
66 /* Give up if abuf doesn't have room for a header. */
67 if ( alen < HFIXEDSZ )
70 /* Fetch the question and answer count from the header. */
71 qdcount = DNS_HEADER_QDCOUNT( abuf );
72 ancount = DNS_HEADER_ANCOUNT( abuf );
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 )
81 if ( aptr + len + QFIXEDSZ > abuf + alen )
86 aptr += len + QFIXEDSZ;
88 /* Allocate nameservers array; ancount gives an upper bound */
89 nameservers = malloc( ( ancount + 1 ) * sizeof( char * ) );
97 /* Examine each answer resource record (RR) in turn. */
98 for ( i = 0; i < ( int ) ancount; i++ )
100 /* Decode the RR up to the data field. */
101 status = ares__expand_name_for_response( aptr, abuf, alen, &rr_name, &len );
102 if ( status != ARES_SUCCESS )
105 if ( aptr + RRFIXEDSZ > abuf + alen )
107 status = ARES_EBADRESP;
110 rr_type = DNS_RR_TYPE( aptr );
111 rr_class = DNS_RR_CLASS( aptr );
112 rr_len = DNS_RR_LEN( aptr );
115 if ( rr_class == C_IN && rr_type == T_NS )
117 /* Decode the RR data and add it to the nameservers list */
118 status = ares__expand_name_for_response( aptr, abuf, alen, &rr_data,
120 if ( status != ARES_SUCCESS )
125 nameservers[nameservers_num] = malloc(strlen(rr_data)+1);
127 if (nameservers[nameservers_num]==NULL)
134 strcpy(nameservers[nameservers_num],rr_data);
143 if ( aptr > abuf + alen )
145 status = ARES_EBADRESP;
150 if ( status == ARES_SUCCESS && nameservers_num == 0 )
152 status = ARES_ENODATA;
154 if ( status == ARES_SUCCESS )
156 /* We got our answer. Allocate memory to build the host entry. */
157 nameservers[nameservers_num] = NULL;
158 hostent = malloc( sizeof( struct hostent ) );
161 hostent->h_addr_list = malloc( 1 * sizeof( char * ) );
162 if ( hostent->h_addr_list )
164 /* Fill in the hostent and return successfully. */
165 hostent->h_name = hostname;
166 hostent->h_aliases = nameservers;
167 hostent->h_addrtype = AF_INET;
168 hostent->h_length = sizeof( struct in_addr );
169 hostent->h_addr_list[0] = NULL;
175 status = ARES_ENOMEM;
177 for ( i = 0; i < nameservers_num; i++ )
178 free( nameservers[i] );