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 
15 #include <grpc/support/port_platform.h>
16 
17 #ifndef GRPC_NO_BINDER
18 
19 #include "src/core/ext/transport/binder/client/binder_connector.h"
20 #include "src/core/lib/iomgr/port.h"
21 
22 #ifdef GRPC_HAVE_UNIX_SOCKET
23 #include <sys/un.h>
24 #endif
25 
26 #include <functional>
27 #include <map>
28 
29 #include <grpcpp/security/binder_security_policy.h>
30 
31 #include "src/core/ext/filters/client_channel/connector.h"
32 #include "src/core/ext/filters/client_channel/subchannel.h"
33 #include "src/core/ext/transport/binder/client/endpoint_binder_pool.h"
34 #include "src/core/ext/transport/binder/client/security_policy_setting.h"
35 #include "src/core/ext/transport/binder/transport/binder_transport.h"
36 #include "src/core/ext/transport/binder/wire_format/binder.h"
37 
38 namespace {
39 
40 // TODO(mingcl): Currently this does no error handling and assumes the
41 // connection always succeeds in reasonable amount of time.
42 class BinderConnector : public grpc_core::SubchannelConnector {
43  public:
BinderConnector()44   BinderConnector() {}
~BinderConnector()45   ~BinderConnector() override {}
Connect(const Args & args,Result * result,grpc_closure * notify)46   void Connect(const Args& args, Result* result,
47                grpc_closure* notify) override {
48 #ifdef GRPC_HAVE_UNIX_SOCKET
49     {
50       struct sockaddr_un* un =
51           reinterpret_cast<struct sockaddr_un*>(args.address->addr);
52       // length of identifier, including null terminator
53       size_t id_length = args.address->len - sizeof(un->sun_family);
54       // The c-style string at least will have a null terminator, and the
55       // connection id itself should not be empty
56       GPR_ASSERT(id_length >= 2);
57       // Make sure there is null terminator at the expected location before
58       // reading from it
59       GPR_ASSERT(un->sun_path[id_length - 1] == '\0');
60       conn_id_ = un->sun_path;
61     }
62 #else
63     GPR_ASSERT(0);
64 #endif
65     gpr_log(GPR_INFO, "BinderConnector %p conn_id_ = %s", this,
66             conn_id_.c_str());
67 
68     args_ = args;
69     GPR_ASSERT(notify_ == nullptr);
70     GPR_ASSERT(notify != nullptr);
71     notify_ = notify;
72     result_ = result;
73 
74     Ref().release();  // Ref held by the following callback
75 
76     grpc_binder::GetEndpointBinderPool()->GetEndpointBinder(
77         conn_id_,
78         std::bind(&BinderConnector::OnConnected, this, std::placeholders::_1));
79   }
80 
OnConnected(std::unique_ptr<grpc_binder::Binder> endpoint_binder)81   void OnConnected(std::unique_ptr<grpc_binder::Binder> endpoint_binder) {
82     GPR_ASSERT(endpoint_binder != nullptr);
83     grpc_transport* transport = grpc_create_binder_transport_client(
84         std::move(endpoint_binder),
85         grpc_binder::GetSecurityPolicySetting()->Get(conn_id_));
86     GPR_ASSERT(transport != nullptr);
87     result_->channel_args = args_.channel_args;
88     result_->transport = transport;
89 
90     GPR_ASSERT(notify_ != nullptr);
91     // ExecCtx is required here for running grpc_closure because this callback
92     // might be invoked from non-gRPC code
93     if (grpc_core::ExecCtx::Get() == nullptr) {
94       grpc_core::ExecCtx exec_ctx;
95       grpc_core::ExecCtx::Run(DEBUG_LOCATION, notify_, absl::OkStatus());
96     } else {
97       grpc_core::ExecCtx::Run(DEBUG_LOCATION, notify_, absl::OkStatus());
98     }
99 
100     Unref();  // Was referenced in BinderConnector::Connect
101   }
Shutdown(grpc_error_handle)102   void Shutdown(grpc_error_handle /*error*/) override {}
103 
104  private:
105   Args args_;
106   grpc_closure* notify_ = nullptr;
107   Result* result_ = nullptr;
108 
109   std::string conn_id_;
110 };
111 
112 }  // namespace
113 
114 namespace grpc_core {
115 
CreateSubchannel(const grpc_resolved_address & address,const ChannelArgs & args)116 RefCountedPtr<Subchannel> BinderClientChannelFactory::CreateSubchannel(
117     const grpc_resolved_address& address, const ChannelArgs& args) {
118   gpr_log(GPR_INFO, "BinderClientChannelFactory creating subchannel %p", this);
119   return Subchannel::Create(
120       MakeOrphanable<BinderConnector>(), address,
121       args.Set(GRPC_ARG_DEFAULT_AUTHORITY, "binder.authority"));
122 }
123 
124 }  // namespace grpc_core
125 
126 #endif  // GRPC_NO_BINDER
127