ares_gethostbyaddr.c
Go to the documentation of this file.
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() */
42  struct ares_addr addr;
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 
58 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
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 
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;
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 
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  }
155  end_aquery(aquery, status, NULL);
156  else
157  next_lookup(aquery);
158 }
159 
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 
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)
253  if (status != ARES_SUCCESS)
254  *host = NULL;
255  return status;
256 }
257 
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 }
ARES_ECANCELLED
#define ARES_ECANCELLED
Definition: ares.h:136
ARES_ENOMEM
#define ARES_ENOMEM
Definition: ares.h:117
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
ares_query
CARES_EXTERN void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition: ares_query.c:105
ares_addr
Definition: ares_private.h:133
ares.h
addr_query
Definition: ares_gethostbyaddr.c:39
ares_inet_net_pton.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
ares_host_callback
void(* ares_host_callback)(void *arg, int status, int timeouts, struct hostent *hostent)
Definition: ares.h:296
status
absl::Status status
Definition: rls.cc:251
setup.name
name
Definition: setup.py:542
addr_query::channel
ares_channel channel
Definition: ares_gethostbyaddr.c:41
next_lookup
static void next_lookup(struct addr_query *aquery)
Definition: ares_gethostbyaddr.c:96
xds_manager.p
p
Definition: xds_manager.py:60
ares_gethostbyaddr
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, int family, ares_host_callback callback, void *arg)
Definition: ares_gethostbyaddr.c:58
DEBUGF
#define DEBUGF(x)
Definition: setup_once.h:402
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
addr_callback
static void addr_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
Definition: ares_gethostbyaddr.c:130
addr_query::remaining_lookups
const char * remaining_lookups
Definition: ares_gethostbyaddr.c:46
addr_query::callback
ares_host_callback callback
Definition: ares_gethostbyaddr.c:43
ares_free_hostent
CARES_EXTERN void ares_free_hostent(struct hostent *host)
Definition: ares_free_hostent.c:26
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ARES_ENOTFOUND
#define ARES_ENOTFOUND
Definition: ares.h:104
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
ares_malloc
void *(* ares_malloc)(size_t size)=default_malloc
Definition: ares_library_init.c:58
addr_query::arg
void * arg
Definition: ares_gethostbyaddr.c:44
a2
T::first_type a2
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:307
end_aquery
static void end_aquery(struct addr_query *aquery, int status, struct hostent *host)
Definition: ares_gethostbyaddr.c:160
arg
Definition: cmdline.cc:40
ERRNO
#define ERRNO
Definition: fake_udp_and_tcp_server.cc:40
ares_platform.h
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
a1
T::first_type a1
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:305
ARES_SUCCESS
#define ARES_SUCCESS
Definition: ares.h:98
ares_setup.h
ares_channeldata
Definition: ares_private.h:266
ARES_EFILE
#define ARES_EFILE
Definition: ares.h:116
PATH_HOSTS
#define PATH_HOSTS
Definition: ares_private.h:94
benchmark.FILE
FILE
Definition: benchmark.py:21
C_IN
#define C_IN
Definition: ares_nameser.h:292
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
ares_parse_ptr_reply
CARES_EXTERN int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, int addrlen, int family, struct hostent **host)
Definition: ares_parse_ptr_reply.c:37
ares_addr::family
int family
Definition: ares_private.h:134
grpc::fclose
fclose(creds_file)
ares__get_hostent
int ares__get_hostent(FILE *fp, int family, struct hostent **host)
Definition: ares__get_hostent.c:34
file_lookup
static int file_lookup(struct ares_addr *addr, struct hostent **host)
Definition: ares_gethostbyaddr.c:169
ptr_rr_name
static void ptr_rr_name(char *name, const struct ares_addr *addr)
Definition: ares_gethostbyaddr.c:258
ares_free
void(* ares_free)(void *ptr)=default_free
Definition: ares_library_init.c:60
arg
struct arg arg
ares_private.h
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
addr_query::timeouts
int timeouts
Definition: ares_gethostbyaddr.c:47
ares_nameser.h
addr_query::addr
struct ares_addr addr
Definition: ares_gethostbyaddr.c:42
ARES_EOF
#define ARES_EOF
Definition: ares.h:115
T_PTR
#define T_PTR
Definition: ares_nameser.h:343
ARES_ENOTIMP
#define ARES_ENOTIMP
Definition: ares.h:105
platform
Definition: test_arm_regression.c:18
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
ARES_EDESTRUCTION
#define ARES_EDESTRUCTION
Definition: ares.h:118
generate_build_files.platform
dictionary platform
Definition: generate_build_files.py:990


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:43