resolve_localhost_ip46.cc
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2020 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 
21 #include <grpc/support/log.h>
22 
26 
27 namespace grpc_core {
28 namespace {
29 
30 bool localhost_to_ipv4 = false;
31 bool localhost_to_ipv6 = false;
32 gpr_once g_resolve_localhost_ipv46 = GPR_ONCE_INIT;
33 
34 void InitResolveLocalhost() {
36  GetDNSResolver()->ResolveNameBlocking("localhost", "https");
37  GPR_ASSERT(addresses_or.ok());
38  for (const auto& addr : *addresses_or) {
39  const grpc_sockaddr* sock_addr =
40  reinterpret_cast<const grpc_sockaddr*>(&addr);
41  if (sock_addr->sa_family == GRPC_AF_INET) {
42  localhost_to_ipv4 = true;
43  } else if (sock_addr->sa_family == GRPC_AF_INET6) {
44  localhost_to_ipv6 = true;
45  }
46  }
47 }
48 } // namespace
49 
50 void LocalhostResolves(bool* ipv4, bool* ipv6) {
51  gpr_once_init(&g_resolve_localhost_ipv46, InitResolveLocalhost);
52  *ipv4 = localhost_to_ipv4;
53  *ipv6 = localhost_to_ipv6;
54 }
55 
56 } // namespace grpc_core
grpc_core::LocalhostResolves
void LocalhostResolves(bool *ipv4, bool *ipv6)
Definition: resolve_localhost_ip46.cc:50
log.h
resolve_localhost_ip46.h
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
grpc_core
Definition: call_metric_recorder.h:31
resolve_address.h
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: impl/codegen/sync_posix.h:52
sockaddr.h
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
grpc_core::GetDNSResolver
DNSResolver * GetDNSResolver()
Definition: resolve_address.cc:38
port.h
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::DNSResolver::ResolveNameBlocking
virtual absl::StatusOr< std::vector< grpc_resolved_address > > ResolveNameBlocking(absl::string_view name, absl::string_view default_port)=0
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10


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