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