Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_parse_ptr_reply.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
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.
16  */
17
18 #include "setup.h"
19
20 #if defined(WIN32) && !defined(WATT32)
21 #include "nameser.h"
22 #else
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netdb.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
29 #endif
30 #endif
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include "ares.h"
38 #include "ares_dns.h"
39 #include "ares_private.h"
40
41 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
42                          int addrlen, int family, struct hostent **host)
43 {
44   unsigned int qdcount, ancount;
45   int status, i, rr_type, rr_class, rr_len;
46   long len;
47   const unsigned char *aptr;
48   char *ptrname, *hostname, *rr_name, *rr_data;
49   struct hostent *hostent;
50   int aliascnt = 0;
51   char ** aliases;
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, &ptrname, &len);
69   if (status != ARES_SUCCESS)
70     return status;
71   if (aptr + len + QFIXEDSZ > abuf + alen)
72     {
73       free(ptrname);
74       return ARES_EBADRESP;
75     }
76   aptr += len + QFIXEDSZ;
77
78   /* Examine each answer resource record (RR) in turn. */
79   hostname = NULL;
80   aliases = (char **) malloc(8 * sizeof(char *));
81   if (!aliases)
82     {
83       free(ptrname);
84       return ARES_ENOMEM;
85     }
86   for (i = 0; i < (int)ancount; i++)
87     {
88       /* Decode the RR up to the data field. */
89       status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
90       if (status != ARES_SUCCESS)
91         break;
92       aptr += len;
93       if (aptr + RRFIXEDSZ > abuf + alen)
94         {
95           status = ARES_EBADRESP;
96           break;
97         }
98       rr_type = DNS_RR_TYPE(aptr);
99       rr_class = DNS_RR_CLASS(aptr);
100       rr_len = DNS_RR_LEN(aptr);
101       aptr += RRFIXEDSZ;
102
103       if (rr_class == C_IN && rr_type == T_PTR
104           && strcasecmp(rr_name, ptrname) == 0)
105         {
106           /* Decode the RR data and set hostname to it. */
107           status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
108           if (status != ARES_SUCCESS)
109             break;
110           if (hostname)
111             free(hostname);
112           hostname = rr_data;
113           aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char *));
114           if (!aliases[aliascnt])
115             {
116               status = ARES_ENOMEM;
117               break;
118             }
119           strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
120           aliascnt++;
121           if ((aliascnt%8)==0)
122             aliases = (char **) realloc(aliases, (aliascnt/16+1) * sizeof(char *));
123         }
124
125       if (rr_class == C_IN && rr_type == T_CNAME)
126         {
127           /* Decode the RR data and replace ptrname with it. */
128           status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
129           if (status != ARES_SUCCESS)
130             break;
131           free(ptrname);
132           ptrname = rr_data;
133         }
134
135       free(rr_name);
136       aptr += rr_len;
137       if (aptr > abuf + alen)
138         {
139           status = ARES_EBADRESP;
140           break;
141         }
142     }
143
144   if (status == ARES_SUCCESS && !hostname)
145     status = ARES_ENODATA;
146   if (status == ARES_SUCCESS)
147     {
148       /* We got our answer.  Allocate memory to build the host entry. */
149       hostent = malloc(sizeof(struct hostent));
150       if (hostent)
151         {
152           hostent->h_addr_list = malloc(2 * sizeof(char *));
153           if (hostent->h_addr_list)
154             {
155               hostent->h_addr_list[0] = malloc(addrlen);
156               if (hostent->h_addr_list[0])
157                 {
158                   hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
159                   if (hostent->h_aliases)
160                     {
161                       /* Fill in the hostent and return successfully. */
162                       hostent->h_name = hostname;
163                       for (i=0 ; i<aliascnt ; i++)
164                         hostent->h_aliases[i] = aliases[i];
165                       hostent->h_aliases[aliascnt] = NULL;
166                       hostent->h_addrtype = family;
167                       hostent->h_length = addrlen;
168                       memcpy(hostent->h_addr_list[0], addr, addrlen);
169                       hostent->h_addr_list[1] = NULL;
170                       *host = hostent;
171                       free(aliases);
172                       free(ptrname);
173                       return ARES_SUCCESS;
174                     }
175                   free(hostent->h_addr_list[0]);
176                 }
177               free(hostent->h_addr_list);
178             }
179           free(hostent);
180         }
181       status = ARES_ENOMEM;
182     }
183   for (i=0 ; i<aliascnt ; i++)
184     if (aliases[i]) 
185       free(aliases[i]);
186   free(aliases);
187   if (hostname)
188     free(hostname);
189   free(ptrname);
190   return status;
191 }