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 static const char rcsid[] = "$Id: ares_gethostbyaddr.c,v 1.1 1998/08/13 18:06:29 ghudson Exp $";
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
27 #include "ares_private.h"
30 /* Arguments passed to ares_gethostbyaddr() */
33 ares_host_callback callback;
36 const char *remaining_lookups;
39 static void next_lookup(struct addr_query *aquery);
40 static void addr_callback(void *arg, int status, unsigned char *abuf,
42 static void end_aquery(struct addr_query *aquery, int status,
43 struct hostent *host);
44 static int file_lookup(struct in_addr *addr, struct hostent **host);
46 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
47 int family, ares_host_callback callback, void *arg)
49 struct addr_query *aquery;
51 if (family != AF_INET || addrlen != sizeof(struct in_addr))
53 callback(arg, ARES_ENOTIMP, NULL);
57 aquery = malloc(sizeof(struct addr_query));
60 callback(arg, ARES_ENOMEM, NULL);
63 aquery->channel = channel;
64 memcpy(&aquery->addr, addr, sizeof(aquery->addr));
65 aquery->callback = callback;
67 aquery->remaining_lookups = channel->lookups;
72 static void next_lookup(struct addr_query *aquery)
76 int a1, a2, a3, a4, status;
80 for (p = aquery->remaining_lookups; *p; p++)
85 addr = ntohl(aquery->addr.s_addr);
87 a2 = (addr >> 16) & 0xff;
88 a3 = (addr >> 8) & 0xff;
90 sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
91 aquery->remaining_lookups = p + 1;
92 ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
96 status = file_lookup(&aquery->addr, &host);
97 if (status != ARES_ENOTFOUND)
99 end_aquery(aquery, status, host);
105 end_aquery(aquery, ARES_ENOTFOUND, NULL);
108 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
110 struct addr_query *aquery = (struct addr_query *) arg;
111 struct hostent *host;
113 if (status == ARES_SUCCESS)
115 status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
116 sizeof(struct in_addr), AF_INET, &host);
117 end_aquery(aquery, status, host);
119 else if (status == ARES_EDESTRUCTION)
120 end_aquery(aquery, status, NULL);
125 static void end_aquery(struct addr_query *aquery, int status,
126 struct hostent *host)
128 aquery->callback(aquery->arg, status, host);
130 ares_free_hostent(host);
134 static int file_lookup(struct in_addr *addr, struct hostent **host)
139 fp = fopen(PATH_HOSTS, "r");
141 return ARES_ENOTFOUND;
143 while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
145 if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
147 ares_free_hostent(*host);
150 if (status == ARES_EOF)
151 status = ARES_ENOTFOUND;
152 if (status != ARES_SUCCESS)