resolve_address_posix.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 #ifdef GRPC_POSIX_SOCKET_RESOLVE_ADDRESS
23 
24 #include <string.h>
25 #include <sys/types.h>
26 
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
35 #include "src/core/lib/gprpp/thd.h"
44 
45 namespace grpc_core {
46 namespace {
47 
48 class NativeDNSRequest {
49  public:
50  NativeDNSRequest(
52  std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
53  on_done)
54  : name_(name), default_port_(default_port), on_done_(std::move(on_done)) {
55  GRPC_CLOSURE_INIT(&request_closure_, DoRequestThread, this, nullptr);
57  }
58 
59  private:
60  // Callback to be passed to grpc Executor to asynch-ify
61  // ResolveNameBlocking
62  static void DoRequestThread(void* rp, grpc_error_handle /*error*/) {
63  NativeDNSRequest* r = static_cast<NativeDNSRequest*>(rp);
64  auto result =
65  GetDNSResolver()->ResolveNameBlocking(r->name_, r->default_port_);
66  // running inline is safe since we've already been scheduled on the executor
67  r->on_done_(std::move(result));
68  delete r;
69  }
70 
71  const std::string name_;
72  const std::string default_port_;
73  const std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
74  on_done_;
75  grpc_closure request_closure_;
76 };
77 
78 } // namespace
79 
80 NativeDNSResolver* NativeDNSResolver::GetOrCreate() {
81  static NativeDNSResolver* instance = new NativeDNSResolver();
82  return instance;
83 }
84 
87  grpc_pollset_set* /* interested_parties */,
88  std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
89  on_done) {
90  // self-deleting class
91  new NativeDNSRequest(name, default_port, std::move(on_done));
92  return kNullHandle;
93 }
94 
97  absl::string_view default_port) {
98  ExecCtx exec_ctx;
99  struct addrinfo hints;
100  struct addrinfo *result = nullptr, *resp;
101  int s;
102  size_t i;
104  std::vector<grpc_resolved_address> addresses;
105  std::string host;
107  // parse name, splitting it into host and port parts
108  SplitHostPort(name, &host, &port);
109  if (host.empty()) {
111  GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
113  goto done;
114  }
115  if (port.empty()) {
116  if (default_port.empty()) {
118  GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
120  goto done;
121  }
122  port = std::string(default_port);
123  }
124  // Call getaddrinfo
125  memset(&hints, 0, sizeof(hints));
126  hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
127  hints.ai_socktype = SOCK_STREAM; /* stream socket */
128  hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
130  s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
132  if (s != 0) {
133  // Retry if well-known service name is recognized
134  const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
135  for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
136  if (port == svc[i][0]) {
138  s = getaddrinfo(host.c_str(), svc[i][1], &hints, &result);
140  break;
141  }
142  }
143  }
144  if (s != 0) {
149  GRPC_ERROR_CREATE_FROM_STATIC_STRING(gai_strerror(s)),
151  GRPC_ERROR_STR_OS_ERROR, gai_strerror(s)),
152  GRPC_ERROR_STR_SYSCALL, "getaddrinfo"),
154  goto done;
155  }
156  // Success path: fill in addrs
157  for (resp = result; resp != nullptr; resp = resp->ai_next) {
159  memcpy(&addr.addr, resp->ai_addr, resp->ai_addrlen);
160  addr.len = resp->ai_addrlen;
161  addresses.push_back(addr);
162  }
164 done:
165  if (result) {
166  freeaddrinfo(result);
167  }
168  if (GRPC_ERROR_IS_NONE(err)) {
169  return addresses;
170  }
171  auto error_result = grpc_error_to_absl_status(err);
173  return error_result;
174 }
175 
176 bool NativeDNSResolver::Cancel(TaskHandle /*handle*/) { return false; }
177 
178 } // namespace grpc_core
179 
180 #endif
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GRPC_ERROR_STR_SYSCALL
@ GRPC_ERROR_STR_SYSCALL
syscall that generated this error
Definition: error.h:114
block_annotate.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
memset
return memset(p, 0, total)
iomgr_internal.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
string.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
useful.h
resolve_address.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error_ref_leak.err
err
Definition: error_ref_leak.py:35
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
instance
RefCountedPtr< grpc_tls_certificate_provider > instance
Definition: xds_server_config_fetcher.cc:224
setup.name
name
Definition: setup.py:542
absl::FormatConversionChar::s
@ s
time.h
name_
const std::string name_
Definition: priority.cc:233
sockaddr.h
on_done_
grpc_closure on_done_
Definition: google_c2p_resolver.cc:102
grpc_core::Executor::Run
static void Run(grpc_closure *closure, grpc_error_handle error, ExecutorType executor_type=ExecutorType::DEFAULT, ExecutorJobType job_type=ExecutorJobType::SHORT)
Definition: executor.cc:398
grpc_error_set_str
grpc_error_handle grpc_error_set_str(grpc_error_handle src, grpc_error_strs which, absl::string_view str)
Definition: error.cc:650
GRPC_ERROR_STR_TARGET_ADDRESS
@ GRPC_ERROR_STR_TARGET_ADDRESS
peer that we were trying to communicate when this error occurred
Definition: error.h:117
string_util.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::DNSResolver::kNullHandle
static constexpr TaskHandle kNullHandle
Definition: resolve_address.h:46
http2_server_health_check.resp
resp
Definition: http2_server_health_check.py:31
resolve_address_posix.h
grpc_core::NativeDNSResolver::ResolveName
TaskHandle ResolveName(absl::string_view name, absl::string_view default_port, grpc_pollset_set *, std::function< void(absl::StatusOr< std::vector< grpc_resolved_address >>)> on_done) override
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
GRPC_SCHEDULING_END_BLOCKING_REGION
#define GRPC_SCHEDULING_END_BLOCKING_REGION
Definition: block_annotate.h:48
host_port.h
grpc_core::NativeDNSResolver::Cancel
bool Cancel(TaskHandle handle) override
executor.h
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
GRPC_ERROR_STR_OS_ERROR
@ GRPC_ERROR_STR_OS_ERROR
operating system description of this error
Definition: error.h:111
unix_sockets_posix.h
grpc_core::DNSResolver::TaskHandle
::grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTaskHandle TaskHandle
Definition: resolve_address.h:45
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
grpc_error_set_int
grpc_error_handle grpc_error_set_int(grpc_error_handle src, grpc_error_ints which, intptr_t value)
Definition: error.cc:613
grpc_core::GetDNSResolver
DNSResolver * GetDNSResolver()
Definition: resolve_address.cc:38
port.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
GRPC_ERROR_INT_ERRNO
@ GRPC_ERROR_INT_ERRNO
'errno' from the operating system
Definition: error.h:56
thd.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecutorType::RESOLVER
@ RESOLVER
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_error
Definition: error_internal.h:42
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_error_to_absl_status
absl::Status grpc_error_to_absl_status(grpc_error_handle error)
Definition: error_utils.cc:156
grpc_closure
Definition: closure.h:56
grpc_core::DNSResolver::ResolveNameBlocking
virtual absl::StatusOr< std::vector< grpc_resolved_address > > ResolveNameBlocking(absl::string_view name, absl::string_view default_port)=0
addrinfo
Definition: ares_ipv6.h:43
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
grpc_core::NativeDNSResolver::ResolveNameBlocking
absl::StatusOr< std::vector< grpc_resolved_address > > ResolveNameBlocking(absl::string_view name, absl::string_view default_port) override
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::NativeDNSResolver::GetOrCreate
static NativeDNSResolver * GetOrCreate()
GRPC_SCHEDULING_START_BLOCKING_REGION
#define GRPC_SCHEDULING_START_BLOCKING_REGION
Definition: block_annotate.h:45
error_utils.h
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:04