xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/transport/endpoint_info_handshaker.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2024 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 
17 #include <grpc/support/port_platform.h>
18 
19 #include "src/core/lib/transport/endpoint_info_handshaker.h"
20 
21 #include <memory>
22 
23 #include "absl/status/status.h"
24 
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gprpp/debug_location.h"
27 #include "src/core/lib/gprpp/ref_counted_ptr.h"
28 #include "src/core/lib/iomgr/closure.h"
29 #include "src/core/lib/iomgr/endpoint.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/transport/handshaker.h"
32 #include "src/core/lib/transport/handshaker_factory.h"
33 #include "src/core/lib/transport/handshaker_registry.h"
34 
35 namespace grpc_core {
36 
37 namespace {
38 
39 class EndpointInfoHandshaker : public Handshaker {
40  public:
name() const41   const char* name() const override { return "endpoint_info"; }
42 
DoHandshake(grpc_tcp_server_acceptor *,grpc_closure * on_handshake_done,HandshakerArgs * args)43   void DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/,
44                    grpc_closure* on_handshake_done,
45                    HandshakerArgs* args) override {
46     args->args = args->args
47                      .Set(GRPC_ARG_ENDPOINT_LOCAL_ADDRESS,
48                           grpc_endpoint_get_local_address(args->endpoint))
49                      .Set(GRPC_ARG_ENDPOINT_PEER_ADDRESS,
50                           grpc_endpoint_get_peer(args->endpoint));
51     ExecCtx::Run(DEBUG_LOCATION, on_handshake_done, absl::OkStatus());
52   }
53 
Shutdown(grpc_error_handle)54   void Shutdown(grpc_error_handle /*why*/) override {}
55 };
56 
57 class EndpointInfoHandshakerFactory : public HandshakerFactory {
58  public:
AddHandshakers(const ChannelArgs &,grpc_pollset_set *,HandshakeManager * handshake_mgr)59   void AddHandshakers(const ChannelArgs& /*args*/,
60                       grpc_pollset_set* /*interested_parties*/,
61                       HandshakeManager* handshake_mgr) override {
62     handshake_mgr->Add(MakeRefCounted<EndpointInfoHandshaker>());
63   }
64 
Priority()65   HandshakerPriority Priority() override {
66     // Needs to be after kTCPConnectHandshakers.
67     return HandshakerPriority::kSecurityHandshakers;
68   }
69 };
70 
71 }  // namespace
72 
RegisterEndpointInfoHandshaker(CoreConfiguration::Builder * builder)73 void RegisterEndpointInfoHandshaker(CoreConfiguration::Builder* builder) {
74   builder->handshaker_registry()->RegisterHandshakerFactory(
75       HANDSHAKER_CLIENT, std::make_unique<EndpointInfoHandshakerFactory>());
76   builder->handshaker_registry()->RegisterHandshakerFactory(
77       HANDSHAKER_SERVER, std::make_unique<EndpointInfoHandshakerFactory>());
78 }
79 
80 }  // namespace grpc_core
81