]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - libares/ares_gethostbyaddr.c
need automake as a build-dep, even though we don't use most of it
[1ts-debian.git] / libares / ares_gethostbyaddr.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
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.
14  */
15
16 static const char rcsid[] = "$Id: ares_gethostbyaddr.c,v 1.1 1998/08/13 18:06:29 ghudson Exp $";
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <netdb.h>
26 #include "ares.h"
27 #include "ares_private.h"
28
29 struct addr_query {
30   /* Arguments passed to ares_gethostbyaddr() */
31   ares_channel channel;
32   struct in_addr addr;
33   ares_host_callback callback;
34   void *arg;
35
36   const char *remaining_lookups;
37 };
38
39 static void next_lookup(struct addr_query *aquery);
40 static void addr_callback(void *arg, int status, unsigned char *abuf,
41                           int alen);
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);
45
46 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
47                         int family, ares_host_callback callback, void *arg)
48 {
49   struct addr_query *aquery;
50
51   if (family != AF_INET || addrlen != sizeof(struct in_addr))
52     {
53       callback(arg, ARES_ENOTIMP, NULL);
54       return;
55     }
56
57   aquery = malloc(sizeof(struct addr_query));
58   if (!aquery)
59     {
60       callback(arg, ARES_ENOMEM, NULL);
61       return;
62     }
63   aquery->channel = channel;
64   memcpy(&aquery->addr, addr, sizeof(aquery->addr));
65   aquery->callback = callback;
66   aquery->arg = arg;
67   aquery->remaining_lookups = channel->lookups;
68
69   next_lookup(aquery);
70 }
71
72 static void next_lookup(struct addr_query *aquery)
73 {
74   const char *p;
75   char name[64];
76   int a1, a2, a3, a4, status;
77   struct hostent *host;
78   unsigned long addr;
79
80   for (p = aquery->remaining_lookups; *p; p++)
81     {
82       switch (*p)
83         {
84         case 'b':
85           addr = ntohl(aquery->addr.s_addr);
86           a1 = addr >> 24;
87           a2 = (addr >> 16) & 0xff;
88           a3 = (addr >> 8) & 0xff;
89           a4 = addr & 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,
93                      aquery);
94           return;
95         case 'f':
96           status = file_lookup(&aquery->addr, &host);
97           if (status != ARES_ENOTFOUND)
98             {
99               end_aquery(aquery, status, host);
100               return;
101             }
102           break;
103         }
104     }
105   end_aquery(aquery, ARES_ENOTFOUND, NULL);
106 }
107
108 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
109 {
110   struct addr_query *aquery = (struct addr_query *) arg;
111   struct hostent *host;
112
113   if (status == ARES_SUCCESS)
114     {
115       status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
116                                     sizeof(struct in_addr), AF_INET, &host);
117       end_aquery(aquery, status, host);
118     }
119   else if (status == ARES_EDESTRUCTION)
120     end_aquery(aquery, status, NULL);
121   else
122     next_lookup(aquery);
123 }
124
125 static void end_aquery(struct addr_query *aquery, int status,
126                        struct hostent *host)
127 {
128   aquery->callback(aquery->arg, status, host);
129   if (host)
130     ares_free_hostent(host);
131   free(aquery);
132 }
133
134 static int file_lookup(struct in_addr *addr, struct hostent **host)
135 {
136   FILE *fp;
137   int status;
138
139   fp = fopen(PATH_HOSTS, "r");
140   if (!fp)
141     return ARES_ENOTFOUND;
142
143   while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
144     {
145       if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
146         break;
147       ares_free_hostent(*host);
148     }
149   fclose(fp);
150   if (status == ARES_EOF)
151     status = ARES_ENOTFOUND;
152   if (status != ARES_SUCCESS)
153     *host = NULL;
154   return status;
155 }