resolve_address_windows.cc
Go to the documentation of this file.
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
18 
20 #ifdef GRPC_WINSOCK_SOCKET
21 
22 #include <inttypes.h>
23 #include <string.h>
24 #include <sys/types.h>
25 
26 #include <string>
27 
28 #include "absl/strings/str_format.h"
29 
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
34 #include <grpc/support/time.h>
35 
39 #include "src/core/lib/gprpp/thd.h"
47 
48 namespace grpc_core {
49 namespace {
50 
51 class NativeDNSRequest {
52  public:
53  NativeDNSRequest(
55  std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
56  on_done)
57  : name_(name), default_port_(default_port), on_done_(std::move(on_done)) {
58  GRPC_CLOSURE_INIT(&request_closure_, DoRequestThread, this, nullptr);
60  }
61 
62  private:
63  // Callback to be passed to grpc Executor to asynch-ify
64  // ResolveNameBlocking
65  static void DoRequestThread(void* rp, grpc_error_handle /*error*/) {
66  NativeDNSRequest* r = static_cast<NativeDNSRequest*>(rp);
67  auto result =
68  GetDNSResolver()->ResolveNameBlocking(r->name_, r->default_port_);
69  // running inline is safe since we've already been scheduled on the executor
70  r->on_done_(std::move(result));
71  delete r;
72  }
73 
74  const std::string name_;
75  const std::string default_port_;
76  const std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
77  on_done_;
78  grpc_closure request_closure_;
79 };
80 
81 } // namespace
82 
83 NativeDNSResolver* NativeDNSResolver::GetOrCreate() {
84  static NativeDNSResolver* instance = new NativeDNSResolver();
85  return instance;
86 }
87 
90  grpc_pollset_set* /* interested_parties */,
91  std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
92  on_done) {
93  new NativeDNSRequest(name, default_port, std::move(on_done));
94  return kNullHandle;
95 }
96 
99  absl::string_view default_port) {
100  ExecCtx exec_ctx;
101  struct addrinfo hints;
102  struct addrinfo *result = NULL, *resp;
103  int s;
104  size_t i;
106  std::vector<grpc_resolved_address> addresses;
107 
108  // parse name, splitting it into host and port parts
109  std::string host;
111  SplitHostPort(name, &host, &port);
112  if (host.empty()) {
114  absl::StrFormat("unparseable host:port: '%s'", name));
115  goto done;
116  }
117  if (port.empty()) {
118  if (default_port == NULL) {
120  absl::StrFormat("no port in name '%s'", name));
121  goto done;
122  }
123  port = std::string(default_port);
124  }
125 
126  // Call getaddrinfo
127  memset(&hints, 0, sizeof(hints));
128  hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
129  hints.ai_socktype = SOCK_STREAM; /* stream socket */
130  hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
131 
133  s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
135  if (s != 0) {
136  error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
137  goto done;
138  }
139 
140  // Success path: set addrs non-NULL, fill it in
141  for (resp = result; resp != NULL; resp = resp->ai_next) {
143  memcpy(&addr.addr, resp->ai_addr, resp->ai_addrlen);
144  addr.len = resp->ai_addrlen;
145  addresses.push_back(addr);
146  }
147 
148 done:
149  if (result) {
150  freeaddrinfo(result);
151  }
152  if (GRPC_ERROR_IS_NONE(error)) {
153  return addresses;
154  }
155  auto error_result = grpc_error_to_absl_status(error);
157  return error_result;
158 }
159 
160 bool NativeDNSResolver::Cancel(TaskHandle /*handle*/) { return false; }
161 
162 } // namespace grpc_core
163 
164 #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
block_annotate.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
sockaddr_utils.h
memset
return memset(p, 0, total)
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
resolve_address_windows.h
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
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
grpc_error_handle error
Definition: retry_filter.cc:499
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_WSA_ERROR
#define GRPC_WSA_ERROR(err, call_name)
windows only: create an error associated with WSAGetLastError()!=0
Definition: error.h:357
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
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
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
grpc_core::DNSResolver::TaskHandle
::grpc_event_engine::experimental::EventEngine::DNSResolver::LookupTaskHandle TaskHandle
Definition: resolve_address.h:45
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
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
alloc.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
thd.h
log_windows.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecutorType::RESOLVER
@ RESOLVER
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