1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
17 * ares_parse_ns_reply created by Vlad Dinulescu <vlad.dinulescu@avira.com>
18 * on behalf of AVIRA Gmbh - http://www.avira.com
21 #include "ares_setup.h"
23 #ifdef HAVE_NETINET_IN_H
24 # include <netinet/in.h>
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
33 #include "ares_nameser.h"
37 #include "ares_private.h"
39 int ares_parse_ns_reply( const unsigned char* abuf, int alen,
40 struct hostent** host )
42 unsigned int qdcount, ancount;
43 int status, i, rr_type, rr_class, rr_len;
46 const unsigned char *aptr;
47 char* hostname, *rr_name, *rr_data, **nameservers;
48 struct hostent *hostent;
50 /* Set *host to NULL for all failure cases. */
53 /* Give up if abuf doesn't have room for a header. */
54 if ( alen < HFIXEDSZ )
57 /* Fetch the question and answer count from the header. */
58 qdcount = DNS_HEADER_QDCOUNT( abuf );
59 ancount = DNS_HEADER_ANCOUNT( abuf );
63 /* Expand the name from the question, and skip past the question. */
64 aptr = abuf + HFIXEDSZ;
65 status = ares__expand_name_for_response( aptr, abuf, alen, &hostname, &len, 0);
66 if ( status != ARES_SUCCESS )
68 if ( aptr + len + QFIXEDSZ > abuf + alen )
70 ares_free( hostname );
73 aptr += len + QFIXEDSZ;
75 /* Allocate nameservers array; ancount gives an upper bound */
76 nameservers = ares_malloc( ( ancount + 1 ) * sizeof( char * ) );
79 ares_free( hostname );
84 /* Examine each answer resource record (RR) in turn. */
85 for ( i = 0; i < ( int ) ancount; i++ )
87 /* Decode the RR up to the data field. */
88 status = ares__expand_name_for_response( aptr, abuf, alen, &rr_name, &len, 0);
89 if ( status != ARES_SUCCESS )
92 if ( aptr + RRFIXEDSZ > abuf + alen )
94 status = ARES_EBADRESP;
98 rr_type = DNS_RR_TYPE( aptr );
99 rr_class = DNS_RR_CLASS( aptr );
100 rr_len = DNS_RR_LEN( aptr );
102 if (aptr + rr_len > abuf + alen)
105 status = ARES_EBADRESP;
109 if ( rr_class == C_IN && rr_type == T_NS )
111 /* Decode the RR data and add it to the nameservers list */
112 status = ares__expand_name_for_response( aptr, abuf, alen, &rr_data,
114 if ( status != ARES_SUCCESS )
120 nameservers[nameservers_num] = ares_malloc(strlen(rr_data)+1);
122 if (nameservers[nameservers_num]==NULL)
129 strcpy(nameservers[nameservers_num],rr_data);
135 ares_free( rr_name );
138 if ( aptr > abuf + alen )
139 { /* LCOV_EXCL_START: already checked above */
140 status = ARES_EBADRESP;
142 } /* LCOV_EXCL_STOP */
145 if ( status == ARES_SUCCESS && nameservers_num == 0 )
147 status = ARES_ENODATA;
149 if ( status == ARES_SUCCESS )
151 /* We got our answer. Allocate memory to build the host entry. */
152 nameservers[nameservers_num] = NULL;
153 hostent = ares_malloc( sizeof( struct hostent ) );
156 hostent->h_addr_list = ares_malloc( 1 * sizeof( char * ) );
157 if ( hostent->h_addr_list )
159 /* Fill in the hostent and return successfully. */
160 hostent->h_name = hostname;
161 hostent->h_aliases = nameservers;
162 hostent->h_addrtype = AF_INET;
163 hostent->h_length = sizeof( struct in_addr );
164 hostent->h_addr_list[0] = NULL;
168 ares_free( hostent );
170 status = ARES_ENOMEM;
172 for ( i = 0; i < nameservers_num; i++ )
173 ares_free( nameservers[i] );
174 ares_free( nameservers );
175 ares_free( hostname );