1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include <arpa/nameser.h>
31 #include "ares_private.h"
34 /* Arguments passed to ares_gethostbyaddr() */
37 ares_host_callback callback;
40 const char *remaining_lookups;
43 static void next_lookup(struct addr_query *aquery);
44 static void addr_callback(void *arg, int status, unsigned char *abuf,
46 static void end_aquery(struct addr_query *aquery, int status,
47 struct hostent *host);
48 static int file_lookup(struct in_addr *addr, struct hostent **host);
50 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
51 int family, ares_host_callback callback, void *arg)
53 struct addr_query *aquery;
55 if (family != AF_INET || addrlen != sizeof(struct in_addr))
57 callback(arg, ARES_ENOTIMP, NULL);
61 aquery = malloc(sizeof(struct addr_query));
64 callback(arg, ARES_ENOMEM, NULL);
67 aquery->channel = channel;
68 memcpy(&aquery->addr, addr, sizeof(aquery->addr));
69 aquery->callback = callback;
71 aquery->remaining_lookups = channel->lookups;
76 static void next_lookup(struct addr_query *aquery)
80 int a1, a2, a3, a4, status;
84 for (p = aquery->remaining_lookups; *p; p++)
89 addr = ntohl(aquery->addr.s_addr);
90 a1 = (int)((addr >> 24) & 0xff);
91 a2 = (int)((addr >> 16) & 0xff);
92 a3 = (int)((addr >> 8) & 0xff);
93 a4 = (int)(addr & 0xff);
94 sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
95 aquery->remaining_lookups = p + 1;
96 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
100 status = file_lookup(&aquery->addr, &host);
101 if (status != ARES_ENOTFOUND)
103 end_aquery(aquery, status, host);
109 end_aquery(aquery, ARES_ENOTFOUND, NULL);
112 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
114 struct addr_query *aquery = (struct addr_query *) arg;
115 struct hostent *host;
117 if (status == ARES_SUCCESS)
119 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
120 sizeof(struct in_addr), AF_INET, &host);
121 end_aquery(aquery, status, host);
123 else if (status == ARES_EDESTRUCTION)
124 end_aquery(aquery, status, NULL);
129 static void end_aquery(struct addr_query *aquery, int status,
130 struct hostent *host)
132 aquery->callback(aquery->arg, status, host);
134 ares_free_hostent(host);
138 static int file_lookup(struct in_addr *addr, struct hostent **host)
145 char PATH_HOSTS[MAX_PATH];
147 GetSystemDirectory(PATH_HOSTS, MAX_PATH);
148 strcat(PATH_HOSTS, PATH_HOSTS_NT);
150 GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
151 strcat(PATH_HOSTS, PATH_HOSTS_9X);
154 #elif defined(WATT32)
155 extern const char *_w32_GetHostsFile (void);
156 const char *PATH_HOSTS = _w32_GetHostsFile();
159 return ARES_ENOTFOUND;
162 fp = fopen(PATH_HOSTS, "r");
164 return ARES_ENOTFOUND;
166 while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
168 if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
170 ares_free_hostent(*host);
173 if (status == ARES_EOF)
174 status = ARES_ENOTFOUND;
175 if (status != ARES_SUCCESS)