binder_resolver.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <algorithm>
18 
19 #include "src/core/lib/iomgr/port.h" // IWYU pragma: keep
20 
21 #ifdef GRPC_HAVE_UNIX_SOCKET
22 
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 
27 #include <memory>
28 #include <string>
29 #include <type_traits>
30 #include <utility>
31 
32 #include "absl/memory/memory.h"
33 #include "absl/status/statusor.h"
34 #include "absl/strings/str_cat.h"
35 #include "absl/strings/string_view.h"
36 #include "absl/strings/strip.h"
37 
39 #include <grpc/support/log.h>
40 
52 
53 namespace grpc_core {
54 namespace {
55 
56 class BinderResolver : public Resolver {
57  public:
58  BinderResolver(ServerAddressList addresses, ResolverArgs args)
59  : result_handler_(std::move(args.result_handler)),
60  addresses_(std::move(addresses)),
62 
63  ~BinderResolver() override { grpc_channel_args_destroy(channel_args_); };
64 
65  void StartLocked() override {
66  Result result;
67  result.addresses = std::move(addresses_);
68  result.args = channel_args_;
69  channel_args_ = nullptr;
70  result_handler_->ReportResult(std::move(result));
71  }
72 
73  void ShutdownLocked() override {}
74 
75  private:
76  std::unique_ptr<ResultHandler> result_handler_;
78  const grpc_channel_args* channel_args_ = nullptr;
79 };
80 
81 class BinderResolverFactory : public ResolverFactory {
82  public:
83  absl::string_view scheme() const override { return "binder"; }
84 
85  bool IsValidUri(const URI& uri) const override {
86  return ParseUri(uri, nullptr);
87  }
88 
89  OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
90  ServerAddressList addresses;
91  if (!ParseUri(args.uri, &addresses)) return nullptr;
92  return MakeOrphanable<BinderResolver>(std::move(addresses),
93  std::move(args));
94  }
95 
96  private:
97  static grpc_error_handle BinderAddrPopulate(
99  path = absl::StripPrefix(path, "/");
100  if (path.empty()) {
101  return GRPC_ERROR_CREATE_FROM_CPP_STRING("path is empty");
102  }
103  // Store parsed path in a unix socket so it can be reinterpreted as
104  // sockaddr. An invalid address family (AF_MAX) is set to make sure it won't
105  // be accidentally used.
106  memset(resolved_addr, 0, sizeof(*resolved_addr));
107  struct sockaddr_un* un =
108  reinterpret_cast<struct sockaddr_un*>(resolved_addr->addr);
109  un->sun_family = AF_MAX;
110  static_assert(sizeof(un->sun_path) >= 101,
111  "unix socket path size is unexpectedly short");
112  if (path.size() + 1 > sizeof(un->sun_path)) {
114  absl::StrCat(path, " is too long to be handled"));
115  }
116  // `un` has already be set to zero, no need to append null after the string
117  memcpy(un->sun_path, path.data(), path.size());
118  resolved_addr->len =
119  static_cast<socklen_t>(sizeof(un->sun_family) + path.size() + 1);
120  return GRPC_ERROR_NONE;
121  }
122 
123  static bool ParseUri(const URI& uri, ServerAddressList* addresses) {
125  {
126  if (!uri.authority().empty()) {
127  gpr_log(GPR_ERROR, "authority is not supported in binder scheme");
128  return false;
129  }
130  grpc_error_handle error = BinderAddrPopulate(uri.path(), &addr);
131  if (!GRPC_ERROR_IS_NONE(error)) {
134  return false;
135  }
136  }
137  if (addresses != nullptr) {
138  addresses->emplace_back(addr, nullptr /* args */);
139  }
140  return true;
141  }
142 };
143 
144 } // namespace
145 
146 void RegisterBinderResolver(CoreConfiguration::Builder* builder) {
147  builder->resolver_registry()->RegisterResolverFactory(
148  absl::make_unique<BinderResolverFactory>());
149 }
150 
151 } // namespace grpc_core
152 
153 #endif
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
orphanable.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
core_configuration.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
memset
return memset(p, 0, total)
grpc_core
Definition: call_metric_recorder.h:31
string.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
error
grpc_error_handle error
Definition: retry_filter.cc:499
addresses_
absl::StatusOr< HierarchicalAddressMap > addresses_
Definition: priority.cc:287
grpc_resolved_address
Definition: resolved_address.h:34
check_documentation.path
path
Definition: check_documentation.py:57
absl::StripPrefix
ABSL_MUST_USE_RESULT absl::string_view StripPrefix(absl::string_view str, absl::string_view prefix)
Definition: abseil-cpp/absl/strings/strip.h:73
resolved_address.h
grpc_channel_args
Definition: grpc_types.h:132
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
grpc_types.h
resolver_factory.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
channel_args_
const grpc_channel_args * channel_args_
Definition: rls.cc:710
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
server_address.h
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_resolved_address::len
socklen_t len
Definition: resolved_address.h:36
error.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::ServerAddressList
std::vector< ServerAddress > ServerAddressList
Definition: server_address.h:120
result_handler_
std::unique_ptr< ResultHandler > result_handler_
Definition: sockaddr_resolver.cc:59
resolver_registry.h
resolver.h
port.h
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
Builder
struct Builder Builder
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:68
channel_args.h
uri_parser.h
grpc_error
Definition: error_internal.h:42
grpc_resolved_address::addr
char addr[GRPC_MAX_SOCKADDR_SIZE]
Definition: resolved_address.h:35
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
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 02:57:48