aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/c-ares/src/lib/ares_gethostbyaddr.c
blob: a7acf3c45c9e6d3e8ef893e733e7261fdc110d28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* MIT License
 *
 * Copyright (c) 1998 Massachusetts Institute of Technology
 * Copyright (c) The c-ares project and its contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * SPDX-License-Identifier: MIT
 */

#include "ares_private.h"

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#  include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
#  include <arpa/inet.h>
#endif

#include "ares_nameser.h"
#include "ares_inet_net_pton.h"

struct addr_query {
  /* Arguments passed to ares_gethostbyaddr() */
  ares_channel_t    *channel;
  struct ares_addr   addr;
  ares_host_callback callback;
  void              *arg;
  char       *lookups; /* duplicate memory from channel for ares_reinit() */
  const char *remaining_lookups;
  size_t      timeouts;
};

static void next_lookup(struct addr_query *aquery);
static void addr_callback(void *arg, ares_status_t status, size_t timeouts,
                          const ares_dns_record_t *dnsrec);
static void end_aquery(struct addr_query *aquery, ares_status_t status,
                       struct hostent *host);
static ares_status_t file_lookup(ares_channel_t         *channel,
                                 const struct ares_addr *addr,
                                 struct hostent        **host);

void ares_gethostbyaddr_nolock(ares_channel_t *channel, const void *addr,
                               int addrlen, int family,
                               ares_host_callback callback, void *arg)
{
  struct addr_query *aquery;

  if (family != AF_INET && family != AF_INET6) {
    callback(arg, ARES_ENOTIMP, 0, NULL);
    return;
  }

  if ((family == AF_INET && addrlen != sizeof(aquery->addr.addr.addr4)) ||
      (family == AF_INET6 && addrlen != sizeof(aquery->addr.addr.addr6))) {
    callback(arg, ARES_ENOTIMP, 0, NULL);
    return;
  }

  aquery = ares_malloc(sizeof(struct addr_query));
  if (!aquery) {
    callback(arg, ARES_ENOMEM, 0, NULL);
    return;
  }
  aquery->lookups = ares_strdup(channel->lookups);
  if (aquery->lookups == NULL) {
    /* LCOV_EXCL_START: OutOfMemory */
    ares_free(aquery);
    callback(arg, ARES_ENOMEM, 0, NULL);
    return;
    /* LCOV_EXCL_STOP */
  }
  aquery->channel = channel;
  if (family == AF_INET) {
    memcpy(&aquery->addr.addr.addr4, addr, sizeof(aquery->addr.addr.addr4));
  } else {
    memcpy(&aquery->addr.addr.addr6, addr, sizeof(aquery->addr.addr.addr6));
  }
  aquery->addr.family       = family;
  aquery->callback          = callback;
  aquery->arg               = arg;
  aquery->remaining_lookups = aquery->lookups;
  aquery->timeouts          = 0;

  next_lookup(aquery);
}

void ares_gethostbyaddr(ares_channel_t *channel, const void *addr, int addrlen,
                        int family, ares_host_callback callback, void *arg)
{
  if (channel == NULL) {
    return;
  }
  ares_channel_lock(channel);
  ares_gethostbyaddr_nolock(channel, addr, addrlen, family, callback, arg);
  ares_channel_unlock(channel);
}

static void next_lookup(struct addr_query *aquery)
{
  const char     *p;
  ares_status_t   status;
  struct hostent *host;
  char           *name;

  for (p = aquery->remaining_lookups; *p; p++) {
    switch (*p) {
      case 'b':
        name = ares_dns_addr_to_ptr(&aquery->addr);
        if (name == NULL) {
          end_aquery(aquery, ARES_ENOMEM,
                     NULL); /* LCOV_EXCL_LINE: OutOfMemory */
          return;           /* LCOV_EXCL_LINE: OutOfMemory */
        }
        aquery->remaining_lookups = p + 1;
        ares_query_nolock(aquery->channel, name, ARES_CLASS_IN,
                          ARES_REC_TYPE_PTR, addr_callback, aquery, NULL);
        ares_free(name);
        return;
      case 'f':
        status = file_lookup(aquery->channel, &aquery->addr, &host);

        /* this status check below previously checked for !ARES_ENOTFOUND,
           but we should not assume that this single error code is the one
           that can occur, as that is in fact no longer the case */
        if (status == ARES_SUCCESS) {
          end_aquery(aquery, status, host);
          return;
        }
        break;
      default:
        break;
    }
  }
  end_aquery(aquery, ARES_ENOTFOUND, NULL);
}

static void addr_callback(void *arg, ares_status_t status, size_t timeouts,
                          const ares_dns_record_t *dnsrec)
{
  struct addr_query *aquery = (struct addr_query *)arg;
  struct hostent    *host;
  size_t             addrlen;

  aquery->timeouts += timeouts;
  if (status == ARES_SUCCESS) {
    if (aquery->addr.family == AF_INET) {
      addrlen = sizeof(aquery->addr.addr.addr4);
      status  = ares_parse_ptr_reply_dnsrec(dnsrec, &aquery->addr.addr.addr4,
                                            (int)addrlen, AF_INET, &host);
    } else {
      addrlen = sizeof(aquery->addr.addr.addr6);
      status  = ares_parse_ptr_reply_dnsrec(dnsrec, &aquery->addr.addr.addr6,
                                            (int)addrlen, AF_INET6, &host);
    }
    end_aquery(aquery, status, host);
  } else if (status == ARES_EDESTRUCTION || status == ARES_ECANCELLED) {
    end_aquery(aquery, status, NULL);
  } else {
    next_lookup(aquery);
  }
}

static void end_aquery(struct addr_query *aquery, ares_status_t status,
                       struct hostent *host)
{
  aquery->callback(aquery->arg, (int)status, (int)aquery->timeouts, host);
  if (host) {
    ares_free_hostent(host);
  }
  ares_free(aquery->lookups);
  ares_free(aquery);
}

static ares_status_t file_lookup(ares_channel_t         *channel,
                                 const struct ares_addr *addr,
                                 struct hostent        **host)
{
  char                      ipaddr[INET6_ADDRSTRLEN];
  const void               *ptr = NULL;
  const ares_hosts_entry_t *entry;
  ares_status_t             status;

  if (addr->family == AF_INET) {
    ptr = &addr->addr.addr4;
  } else if (addr->family == AF_INET6) {
    ptr = &addr->addr.addr6;
  }

  if (ptr == NULL) {
    return ARES_ENOTFOUND;
  }

  if (!ares_inet_ntop(addr->family, ptr, ipaddr, sizeof(ipaddr))) {
    return ARES_ENOTFOUND;
  }

  status = ares_hosts_search_ipaddr(channel, ARES_FALSE, ipaddr, &entry);
  if (status != ARES_SUCCESS) {
    return status;
  }

  status = ares_hosts_entry_to_hostent(entry, addr->family, host);
  if (status != ARES_SUCCESS) {
    return status; /* LCOV_EXCL_LINE: OutOfMemory */
  }

  return ARES_SUCCESS;
}