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