djgpp fixes by Gisle
[platform/upstream/c-ares.git] / 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 #include <sys/types.h>
17
18 #ifdef WIN32
19 #include "nameser.h"
20 #else
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <arpa/nameser.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "ares.h"
31 #include "ares_private.h"
32
33 struct addr_query {
34   /* Arguments passed to ares_gethostbyaddr() */
35   ares_channel channel;
36   struct in_addr addr;
37   ares_host_callback callback;
38   void *arg;
39
40   const char *remaining_lookups;
41 };
42
43 static void next_lookup(struct addr_query *aquery);
44 static void addr_callback(void *arg, int status, unsigned char *abuf,
45                           int alen);
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);
49
50 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
51                         int family, ares_host_callback callback, void *arg)
52 {
53   struct addr_query *aquery;
54
55   if (family != AF_INET || addrlen != sizeof(struct in_addr))
56     {
57       callback(arg, ARES_ENOTIMP, NULL);
58       return;
59     }
60
61   aquery = malloc(sizeof(struct addr_query));
62   if (!aquery)
63     {
64       callback(arg, ARES_ENOMEM, NULL);
65       return;
66     }
67   aquery->channel = channel;
68   memcpy(&aquery->addr, addr, sizeof(aquery->addr));
69   aquery->callback = callback;
70   aquery->arg = arg;
71   aquery->remaining_lookups = channel->lookups;
72
73   next_lookup(aquery);
74 }
75
76 static void next_lookup(struct addr_query *aquery)
77 {
78   const char *p;
79   char name[64];
80   int a1, a2, a3, a4, status;
81   struct hostent *host;
82   unsigned long addr;
83
84   for (p = aquery->remaining_lookups; *p; p++)
85     {
86       switch (*p)
87         {
88         case 'b':
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,
97                      aquery);
98           return;
99         case 'f':
100           status = file_lookup(&aquery->addr, &host);
101           if (status != ARES_ENOTFOUND)
102             {
103               end_aquery(aquery, status, host);
104               return;
105             }
106           break;
107         }
108     }
109   end_aquery(aquery, ARES_ENOTFOUND, NULL);
110 }
111
112 static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
113 {
114   struct addr_query *aquery = (struct addr_query *) arg;
115   struct hostent *host;
116
117   if (status == ARES_SUCCESS)
118     {
119       status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
120                                     sizeof(struct in_addr), AF_INET, &host);
121       end_aquery(aquery, status, host);
122     }
123   else if (status == ARES_EDESTRUCTION)
124     end_aquery(aquery, status, NULL);
125   else
126     next_lookup(aquery);
127 }
128
129 static void end_aquery(struct addr_query *aquery, int status,
130                        struct hostent *host)
131 {
132   aquery->callback(aquery->arg, status, host);
133   if (host)
134     ares_free_hostent(host);
135   free(aquery);
136 }
137
138 static int file_lookup(struct in_addr *addr, struct hostent **host)
139 {
140   FILE *fp;
141   int status;
142
143 #ifdef WIN32
144
145   char PATH_HOSTS[MAX_PATH];
146   if (IsNT) {
147     GetSystemDirectory(PATH_HOSTS, MAX_PATH);
148     strcat(PATH_HOSTS, PATH_HOSTS_NT);
149   } else {
150     GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
151     strcat(PATH_HOSTS, PATH_HOSTS_9X);
152   }
153
154 #elif defined(WATT32)
155   extern const char *_w32_GetHostsFile (void);
156   const char *PATH_HOSTS = _w32_GetHostsFile();
157
158   if (!PATH_HOSTS)
159     return ARES_ENOTFOUND;
160 #endif
161
162   fp = fopen(PATH_HOSTS, "r");
163   if (!fp)
164     return ARES_ENOTFOUND;
165
166   while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
167     {
168       if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
169         break;
170       ares_free_hostent(*host);
171     }
172   fclose(fp);
173   if (status == ARES_EOF)
174     status = ARES_ENOTFOUND;
175   if (status != ARES_SUCCESS)
176     *host = NULL;
177   return status;
178 }