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