Removed inclusion of <sys/types.h> in .c-files
[platform/upstream/c-ares.git] / ares_gethostbyaddr.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 #include "setup.h"
18
19 #if defined(WIN32) && !defined(WATT32)
20 #include "nameser.h"
21 #else
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <arpa/nameser.h>
26 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
27 #include <arpa/nameser_compat.h>
28 #endif
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "ares.h"
36 #include "ares_private.h"
37 #include "inet_net_pton.h"
38
39 #ifdef WATT32
40 #undef WIN32
41 #endif
42
43 struct addr_query {
44   /* Arguments passed to ares_gethostbyaddr() */
45   ares_channel channel;
46   union ares_addr addr;
47   int family;
48   ares_host_callback callback;
49   void *arg;
50
51   const char *remaining_lookups;
52 };
53
54 static void next_lookup(struct addr_query *aquery);
55 static void addr_callback(void *arg, int status, unsigned char *abuf,
56                           int alen);
57 static void end_aquery(struct addr_query *aquery, int status,
58                        struct hostent *host);
59 static int file_lookup(union ares_addr *addr, int family, struct hostent **host);
60
61 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
62                         int family, ares_host_callback callback, void *arg)
63 {
64   struct addr_query *aquery;
65
66   if (family != AF_INET && family != AF_INET6)
67     {
68       callback(arg, ARES_ENOTIMP, NULL);
69       return;
70     }
71
72   if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
73       (family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
74     {
75       callback(arg, ARES_ENOTIMP, NULL);
76       return;
77     }
78
79   aquery = malloc(sizeof(struct addr_query));
80   if (!aquery)
81     {
82       callback(arg, ARES_ENOMEM, NULL);
83       return;
84     }
85   aquery->channel = channel;
86   if (family == AF_INET)
87     memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr));
88   else
89     memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr));
90   aquery->family = family;
91   aquery->callback = callback;
92   aquery->arg = arg;
93   aquery->remaining_lookups = channel->lookups;
94
95   next_lookup(aquery);
96 }
97
98 static void next_lookup(struct addr_query *aquery)
99 {
100   const char *p;
101   char name[128];
102   int a1, a2, a3, a4, status;
103   struct hostent *host;
104   unsigned long addr;
105
106   for (p = aquery->remaining_lookups; *p; p++)
107     {
108       switch (*p)
109         {
110         case 'b':
111           if (aquery->family == AF_INET)
112             {
113               addr = ntohl(aquery->addr.addr4.s_addr);
114               a1 = (int)((addr >> 24) & 0xff);
115               a2 = (int)((addr >> 16) & 0xff);
116               a3 = (int)((addr >> 8) & 0xff);
117               a4 = (int)(addr & 0xff);
118               sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
119               aquery->remaining_lookups = p + 1;
120               ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
121                          aquery);
122             }
123           else
124             {
125               unsigned char *bytes;
126               bytes = (unsigned char *)&aquery->addr.addr6.s6_addr;
127               sprintf(name, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
128                       bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
129                       bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
130                       bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
131                       bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4,
132                       bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
133                       bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
134                       bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
135                       bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);
136               aquery->remaining_lookups = p + 1;
137               ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
138                          aquery);
139             }
140           return;
141         case 'f':
142           status = file_lookup(&aquery->addr, aquery->family, &host);
143           if (status != ARES_ENOTFOUND)
144             {
145               end_aquery(aquery, status, host);
146               return;
147             }
148           break;
149         }
150     }
151   end_aquery(aquery, ARES_ENOTFOUND, NULL);
152 }
153
154 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
155 {
156   struct addr_query *aquery = (struct addr_query *) arg;
157   struct hostent *host;
158
159   if (status == ARES_SUCCESS)
160     {
161       if (aquery->family == AF_INET)
162         status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4,
163                                       sizeof(struct in_addr), AF_INET, &host);
164       else
165         status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr6,
166                                       sizeof(struct in6_addr), AF_INET6, &host);
167       end_aquery(aquery, status, host);
168     }
169   else if (status == ARES_EDESTRUCTION)
170     end_aquery(aquery, status, NULL);
171   else
172     next_lookup(aquery);
173 }
174
175 static void end_aquery(struct addr_query *aquery, int status,
176                        struct hostent *host)
177 {
178   aquery->callback(aquery->arg, status, host);
179   if (host)
180     ares_free_hostent(host);
181   free(aquery);
182 }
183
184 static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
185 {
186   FILE *fp;
187   int status;
188   int error;
189
190 #ifdef WIN32
191   char PATH_HOSTS[MAX_PATH];
192   if (IS_NT()) {
193     char tmp[MAX_PATH];
194     HKEY hkeyHosts;
195
196     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
197         == ERROR_SUCCESS)
198     {
199       DWORD dwLength = MAX_PATH;
200       RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
201                       &dwLength);
202       ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
203       RegCloseKey(hkeyHosts);
204     }
205   }
206   else
207     GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
208
209   strcat(PATH_HOSTS, WIN_PATH_HOSTS);
210
211 #elif defined(WATT32)
212   extern const char *_w32_GetHostsFile (void);
213   const char *PATH_HOSTS = _w32_GetHostsFile();
214
215   if (!PATH_HOSTS)
216     return ARES_ENOTFOUND;
217 #endif
218
219   fp = fopen(PATH_HOSTS, "r");
220   if (!fp)
221     {
222       error = ERRNO;
223       switch(error)
224         {
225         case ENOENT:
226         case ESRCH:
227           return ARES_ENOTFOUND;
228         default:
229           DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
230                          error, strerror(error)));
231           DEBUGF(fprintf(stderr, "Error opening file: %s\n",
232                          PATH_HOSTS));
233           *host = NULL;
234           return ARES_EFILE;
235         }
236     }
237   while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
238     {
239       if (family != (*host)->h_addrtype)
240         {
241           ares_free_hostent(*host);
242           continue;
243         }
244       if (family == AF_INET)
245         {
246           if (memcmp((*host)->h_addr, &addr->addr4, sizeof(struct in_addr)) == 0)
247             break;
248         }
249       else if (family == AF_INET6)
250         {
251           if (memcmp((*host)->h_addr, &addr->addr6, sizeof(struct in6_addr)) == 0)
252             break;
253         }
254       ares_free_hostent(*host);
255     }
256   fclose(fp);
257   if (status == ARES_EOF)
258     status = ARES_ENOTFOUND;
259   if (status != ARES_SUCCESS)
260     *host = NULL;
261   return status;
262 }