• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16 #include "ares_setup.h"
17 
18 #ifdef HAVE_NETINET_IN_H
19 #  include <netinet/in.h>
20 #endif
21 #ifdef HAVE_NETDB_H
22 #  include <netdb.h>
23 #endif
24 #ifdef HAVE_ARPA_INET_H
25 #  include <arpa/inet.h>
26 #endif
27 
28 #include "ares_nameser.h"
29 
30 #include "ares.h"
31 #include "ares_inet_net_pton.h"
32 #include "ares_platform.h"
33 #include "ares_private.h"
34 
35 #ifdef WATT32
36 #undef WIN32
37 #endif
38 
39 struct addr_query {
40   /* Arguments passed to ares_gethostbyaddr() */
41   ares_channel channel;
42   struct ares_addr addr;
43   ares_host_callback callback;
44   void *arg;
45 
46   const char *remaining_lookups;
47   int timeouts;
48 };
49 
50 static void next_lookup(struct addr_query *aquery);
51 static void addr_callback(void *arg, int status, int timeouts,
52                           unsigned char *abuf, int alen);
53 static void end_aquery(struct addr_query *aquery, int status,
54                        struct hostent *host);
55 static int file_lookup(struct ares_addr *addr, struct hostent **host);
56 static void ptr_rr_name(char *name, const struct ares_addr *addr);
57 
ares_gethostbyaddr(ares_channel channel,const void * addr,int addrlen,int family,ares_host_callback callback,void * arg)58 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
59                         int family, ares_host_callback callback, void *arg)
60 {
61   struct addr_query *aquery;
62 
63   if (family != AF_INET && family != AF_INET6)
64     {
65       callback(arg, ARES_ENOTIMP, 0, NULL);
66       return;
67     }
68 
69   if ((family == AF_INET && addrlen != sizeof(aquery->addr.addrV4)) ||
70       (family == AF_INET6 && addrlen != sizeof(aquery->addr.addrV6)))
71     {
72       callback(arg, ARES_ENOTIMP, 0, NULL);
73       return;
74     }
75 
76   aquery = ares_malloc(sizeof(struct addr_query));
77   if (!aquery)
78     {
79       callback(arg, ARES_ENOMEM, 0, NULL);
80       return;
81     }
82   aquery->channel = channel;
83   if (family == AF_INET)
84     memcpy(&aquery->addr.addrV4, addr, sizeof(aquery->addr.addrV4));
85   else
86     memcpy(&aquery->addr.addrV6, addr, sizeof(aquery->addr.addrV6));
87   aquery->addr.family = family;
88   aquery->callback = callback;
89   aquery->arg = arg;
90   aquery->remaining_lookups = channel->lookups;
91   aquery->timeouts = 0;
92 
93   next_lookup(aquery);
94 }
95 
next_lookup(struct addr_query * aquery)96 static void next_lookup(struct addr_query *aquery)
97 {
98   const char *p;
99   char name[128];
100   int status;
101   struct hostent *host;
102 
103   for (p = aquery->remaining_lookups; *p; p++)
104     {
105       switch (*p)
106         {
107         case 'b':
108           ptr_rr_name(name, &aquery->addr);
109           aquery->remaining_lookups = p + 1;
110           ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
111                      aquery);
112           return;
113         case 'f':
114           status = file_lookup(&aquery->addr, &host);
115 
116           /* this status check below previously checked for !ARES_ENOTFOUND,
117              but we should not assume that this single error code is the one
118              that can occur, as that is in fact no longer the case */
119           if (status == ARES_SUCCESS)
120             {
121               end_aquery(aquery, status, host);
122               return;
123             }
124           break;
125         }
126     }
127   end_aquery(aquery, ARES_ENOTFOUND, NULL);
128 }
129 
addr_callback(void * arg,int status,int timeouts,unsigned char * abuf,int alen)130 static void addr_callback(void *arg, int status, int timeouts,
131                           unsigned char *abuf, int alen)
132 {
133   struct addr_query *aquery = (struct addr_query *) arg;
134   struct hostent *host;
135   size_t addrlen;
136 
137   aquery->timeouts += timeouts;
138   if (status == ARES_SUCCESS)
139     {
140       if (aquery->addr.family == AF_INET)
141         {
142           addrlen = sizeof(aquery->addr.addrV4);
143           status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
144                                         (int)addrlen, AF_INET, &host);
145         }
146       else
147         {
148           addrlen = sizeof(aquery->addr.addrV6);
149           status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6,
150                                         (int)addrlen, AF_INET6, &host);
151         }
152       end_aquery(aquery, status, host);
153     }
154   else if (status == ARES_EDESTRUCTION || status == ARES_ECANCELLED)
155     end_aquery(aquery, status, NULL);
156   else
157     next_lookup(aquery);
158 }
159 
end_aquery(struct addr_query * aquery,int status,struct hostent * host)160 static void end_aquery(struct addr_query *aquery, int status,
161                        struct hostent *host)
162 {
163   aquery->callback(aquery->arg, status, aquery->timeouts, host);
164   if (host)
165     ares_free_hostent(host);
166   ares_free(aquery);
167 }
168 
file_lookup(struct ares_addr * addr,struct hostent ** host)169 static int file_lookup(struct ares_addr *addr, struct hostent **host)
170 {
171   FILE *fp;
172   int status;
173   int error;
174 
175 #ifdef WIN32
176   char PATH_HOSTS[MAX_PATH];
177   win_platform platform;
178 
179   PATH_HOSTS[0] = '\0';
180 
181   platform = ares__getplatform();
182 
183   if (platform == WIN_NT) {
184     char tmp[MAX_PATH];
185     HKEY hkeyHosts;
186 
187     if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
188                      &hkeyHosts) == ERROR_SUCCESS)
189     {
190       DWORD dwLength = MAX_PATH;
191       RegQueryValueExA(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
192                       &dwLength);
193       ExpandEnvironmentStringsA(tmp, PATH_HOSTS, MAX_PATH);
194       RegCloseKey(hkeyHosts);
195     }
196   }
197   else if (platform == WIN_9X)
198     GetWindowsDirectoryA(PATH_HOSTS, MAX_PATH);
199   else
200     return ARES_ENOTFOUND;
201 
202   strcat(PATH_HOSTS, WIN_PATH_HOSTS);
203 
204 #elif defined(WATT32)
205   const char *PATH_HOSTS = _w32_GetHostsFile();
206 
207   if (!PATH_HOSTS)
208     return ARES_ENOTFOUND;
209 #endif
210 
211   fp = fopen(PATH_HOSTS, "r");
212   if (!fp)
213     {
214       error = ERRNO;
215       switch(error)
216         {
217         case ENOENT:
218         case ESRCH:
219           return ARES_ENOTFOUND;
220         default:
221           DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
222                          error, strerror(error)));
223           DEBUGF(fprintf(stderr, "Error opening file: %s\n",
224                          PATH_HOSTS));
225           *host = NULL;
226           return ARES_EFILE;
227         }
228     }
229   while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS)
230     {
231       if (addr->family != (*host)->h_addrtype)
232         {
233           ares_free_hostent(*host);
234           continue;
235         }
236       if (addr->family == AF_INET)
237         {
238           if (memcmp((*host)->h_addr, &addr->addrV4,
239                      sizeof(addr->addrV4)) == 0)
240             break;
241         }
242       else if (addr->family == AF_INET6)
243         {
244           if (memcmp((*host)->h_addr, &addr->addrV6,
245                      sizeof(addr->addrV6)) == 0)
246             break;
247         }
248       ares_free_hostent(*host);
249     }
250   fclose(fp);
251   if (status == ARES_EOF)
252     status = ARES_ENOTFOUND;
253   if (status != ARES_SUCCESS)
254     *host = NULL;
255   return status;
256 }
257 
ptr_rr_name(char * name,const struct ares_addr * addr)258 static void ptr_rr_name(char *name, const struct ares_addr *addr)
259 {
260   if (addr->family == AF_INET)
261     {
262        unsigned long laddr = ntohl(addr->addrV4.s_addr);
263        unsigned long a1 = (laddr >> 24UL) & 0xFFUL;
264        unsigned long a2 = (laddr >> 16UL) & 0xFFUL;
265        unsigned long a3 = (laddr >>  8UL) & 0xFFUL;
266        unsigned long a4 = laddr & 0xFFUL;
267        sprintf(name, "%lu.%lu.%lu.%lu.in-addr.arpa", a4, a3, a2, a1);
268     }
269   else
270     {
271        unsigned char *bytes = (unsigned char *)&addr->addrV6;
272        /* There are too many arguments to do this in one line using
273         * minimally C89-compliant compilers */
274        sprintf(name,
275                 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.",
276                 bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
277                 bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
278                 bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
279                 bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4);
280        sprintf(name+strlen(name),
281                 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
282                 bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
283                 bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
284                 bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
285                 bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);
286     }
287 }
288