summaryrefslogtreecommitdiff
path: root/grpc/src/core/lib/iomgr/resolve_address_custom.cc
blob: f89eab0591c31615fff10dec4c9eddbdb3d63666 (plain)
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
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <grpc/support/port_platform.h>

#include "src/core/lib/iomgr/resolve_address_custom.h"

#include <string.h>

#include <string>

#include "absl/strings/str_format.h"

#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

#include "src/core/lib/address_utils/sockaddr_utils.h"
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/host_port.h"
#include "src/core/lib/iomgr/iomgr_custom.h"
#include "src/core/lib/iomgr/port.h"

struct grpc_custom_resolver {
  grpc_closure* on_done = nullptr;
  grpc_resolved_addresses** addresses = nullptr;
  std::string host;
  std::string port;
};

static grpc_custom_resolver_vtable* resolve_address_vtable = nullptr;

static int retry_named_port_failure(grpc_custom_resolver* r,
                                    grpc_resolved_addresses** res) {
  // This loop is copied from resolve_address_posix.c
  const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
  for (size_t i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
    if (r->port == svc[i][0]) {
      r->port = svc[i][1];
      if (res) {
        grpc_error_handle error = resolve_address_vtable->resolve(
            r->host.c_str(), r->port.c_str(), res);
        if (error != GRPC_ERROR_NONE) {
          GRPC_ERROR_UNREF(error);
          return 0;
        }
      } else {
        resolve_address_vtable->resolve_async(r, r->host.c_str(),
                                              r->port.c_str());
      }
      return 1;
    }
  }
  return 0;
}

void grpc_custom_resolve_callback(grpc_custom_resolver* r,
                                  grpc_resolved_addresses* result,
                                  grpc_error_handle error) {
  GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  grpc_core::ExecCtx exec_ctx;
  if (error == GRPC_ERROR_NONE) {
    *r->addresses = result;
  } else if (retry_named_port_failure(r, nullptr)) {
    return;
  }
  if (r->on_done) {
    grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
  }
  delete r;
}

static grpc_error_handle try_split_host_port(const char* name,
                                             const char* default_port,
                                             std::string* host,
                                             std::string* port) {
  /* parse name, splitting it into host and port parts */
  grpc_core::SplitHostPort(name, host, port);
  if (host->empty()) {
    return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
        absl::StrFormat("unparseable host:port: '%s'", name).c_str());
  }
  if (port->empty()) {
    // TODO(murgatroid99): add tests for this case
    if (default_port == nullptr) {
      return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
          absl::StrFormat("no port in name '%s'", name).c_str());
    }
    *port = default_port;
  }
  return GRPC_ERROR_NONE;
}

static grpc_error_handle blocking_resolve_address_impl(
    const char* name, const char* default_port,
    grpc_resolved_addresses** addresses) {
  GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();

  grpc_custom_resolver resolver;
  grpc_error_handle err =
      try_split_host_port(name, default_port, &resolver.host, &resolver.port);
  if (err != GRPC_ERROR_NONE) {
    return err;
  }

  /* Call getaddrinfo */
  grpc_resolved_addresses* addrs;
  grpc_core::ExecCtx* curr = grpc_core::ExecCtx::Get();
  grpc_core::ExecCtx::Set(nullptr);
  err = resolve_address_vtable->resolve(resolver.host.c_str(),
                                        resolver.port.c_str(), &addrs);
  if (err != GRPC_ERROR_NONE) {
    if (retry_named_port_failure(&resolver, &addrs)) {
      GRPC_ERROR_UNREF(err);
      err = GRPC_ERROR_NONE;
    }
  }
  grpc_core::ExecCtx::Set(curr);
  if (err == GRPC_ERROR_NONE) {
    *addresses = addrs;
  }
  return err;
}

static void resolve_address_impl(const char* name, const char* default_port,
                                 grpc_pollset_set* /*interested_parties*/,
                                 grpc_closure* on_done,
                                 grpc_resolved_addresses** addrs) {
  GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
  std::string host;
  std::string port;
  grpc_error_handle err = try_split_host_port(name, default_port, &host, &port);
  if (err != GRPC_ERROR_NONE) {
    grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, err);
    return;
  }
  grpc_custom_resolver* r = new grpc_custom_resolver();
  r->on_done = on_done;
  r->addresses = addrs;
  r->host = std::move(host);
  r->port = std::move(port);

  /* Call getaddrinfo */
  resolve_address_vtable->resolve_async(r, r->host.c_str(), r->port.c_str());
}

static grpc_address_resolver_vtable custom_resolver_vtable = {
    resolve_address_impl, blocking_resolve_address_impl};

void grpc_custom_resolver_init(grpc_custom_resolver_vtable* impl) {
  resolve_address_vtable = impl;
  grpc_set_resolver_impl(&custom_resolver_vtable);
}