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