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