Debian Codegraph @

src/core/lib/transport/handshaker.h

raw
1 /*
2 *
3 * Copyright 2016 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
19 #ifndef GRPC_CORE_LIB_TRANSPORT_HANDSHAKER_H
20 #define GRPC_CORE_LIB_TRANSPORT_HANDSHAKER_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stddef.h>
25
26 #include "absl/container/inlined_vector.h"
27
28 #include <grpc/slice.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gprpp/ref_counted.h"
32 #include "src/core/lib/gprpp/ref_counted_ptr.h"
33 #include "src/core/lib/gprpp/sync.h"
34 #include "src/core/lib/gprpp/time.h"
35 #include "src/core/lib/iomgr/closure.h"
36 #include "src/core/lib/iomgr/endpoint.h"
37 #include "src/core/lib/iomgr/error.h"
38 #include "src/core/lib/iomgr/tcp_server.h"
39 #include "src/core/lib/iomgr/timer.h"
40
41 namespace grpc_core {
42
43 /// Handshakers are used to perform initial handshakes on a connection
44 /// before the client sends the initial request. Some examples of what
45 /// a handshaker can be used for includes support for HTTP CONNECT on
46 /// the client side and various types of security initialization.
47 ///
48 /// In general, handshakers should be used via a handshake manager.
49
50 /// Arguments passed through handshakers and to the on_handshake_done callback.
51 ///
52 /// For handshakers, all members are input/output parameters; for
53 /// example, a handshaker may read from or write to \a endpoint and
54 /// then later replace it with a wrapped endpoint. Similarly, a
55 /// handshaker may modify \a args.
56 ///
57 /// A handshaker takes ownership of the members while a handshake is in
58 /// progress. Upon failure or shutdown of an in-progress handshaker,
59 /// the handshaker is responsible for destroying the members and setting
60 /// them to NULL before invoking the on_handshake_done callback.
61 ///
62 /// For the on_handshake_done callback, all members are input arguments,
63 /// which the callback takes ownership of.
64 struct HandshakerArgs {
65 grpc_endpoint* endpoint = nullptr;
66 ChannelArgs args;
67 grpc_slice_buffer* read_buffer = nullptr;
68 // A handshaker may set this to true before invoking on_handshake_done
69 // to indicate that subsequent handshakers should be skipped.
70 bool exit_early = false;
71 // User data passed through the handshake manager. Not used by
72 // individual handshakers.
73 void* user_data = nullptr;
74 // Deadline associated with the handshake.
75 // TODO(anramach): Move this out of handshake args after event engine
76 // is the default.
77 Timestamp deadline;
78 };
79
80 ///
81 /// Handshaker
82 ///
83
84 class Handshaker : public RefCounted<Handshaker> {
85 public:
86 ~Handshaker() override = default;
87 virtual void Shutdown(grpc_error_handle why) = 0;
88 virtual void DoHandshake(grpc_tcp_server_acceptor* acceptor,
89 grpc_closure* on_handshake_done,
90 HandshakerArgs* args) = 0;
91 virtual const char* name() const = 0;
92 };
93
94 //
95 // HandshakeManager
96 //
97
98 class HandshakeManager : public RefCounted<HandshakeManager> {
99 public:
100 HandshakeManager();
101 ~HandshakeManager() override;
102
103 /// Adds a handshaker to the handshake manager.
104 /// Takes ownership of \a handshaker.
105 void Add(RefCountedPtr<Handshaker> handshaker);
106
107 /// Shuts down the handshake manager (e.g., to clean up when the operation is
108 /// aborted in the middle).
109 void Shutdown(grpc_error_handle why);
110
111 /// Invokes handshakers in the order they were added.
112 /// Takes ownership of \a endpoint, and then passes that ownership to
113 /// the \a on_handshake_done callback.
114 /// Does NOT take ownership of \a channel_args. Instead, makes a copy before
115 /// invoking the first handshaker.
116 /// \a acceptor will be nullptr for client-side handshakers.
117 ///
118 /// When done, invokes \a on_handshake_done with a HandshakerArgs
119 /// object as its argument. If the callback is invoked with error !=
120 /// absl::OkStatus(), then handshaking failed and the handshaker has done
121 /// the necessary clean-up. Otherwise, the callback takes ownership of
122 /// the arguments.
123 void DoHandshake(grpc_endpoint* endpoint, const ChannelArgs& channel_args,
124 Timestamp deadline, grpc_tcp_server_acceptor* acceptor,
125 grpc_iomgr_cb_func on_handshake_done, void* user_data);
126
127 private:
128 bool CallNextHandshakerLocked(grpc_error_handle error);
129
130 // A function used as the handshaker-done callback when chaining
131 // handshakers together.
132 static void CallNextHandshakerFn(void* arg, grpc_error_handle error);
133
134 // Callback invoked when deadline is exceeded.
135 static void OnTimeoutFn(void* arg, grpc_error_handle error);
136
137 static const size_t HANDSHAKERS_INIT_SIZE = 2;
138
139 Mutex mu_;
140 bool is_shutdown_ = false;
141 // An array of handshakers added via grpc_handshake_manager_add().
142 absl::InlinedVector<RefCountedPtr<Handshaker>, HANDSHAKERS_INIT_SIZE>
143 handshakers_;
144 // The index of the handshaker to invoke next and closure to invoke it.
145 size_t index_ = 0;
146 grpc_closure call_next_handshaker_;
147 // The acceptor to call the handshakers with.
148 grpc_tcp_server_acceptor* acceptor_;
149 // Deadline timer across all handshakers.
150 grpc_timer deadline_timer_;
151 grpc_closure on_timeout_;
152 // The final callback and user_data to invoke after the last handshaker.
153 grpc_closure on_handshake_done_;
154 // Handshaker args.
155 HandshakerArgs args_;
156 };
157
158 } // namespace grpc_core
159
160 // TODO(arjunroy): These are transitional to account for the new handshaker API
161 // and will eventually be removed entirely.
162 typedef grpc_core::HandshakeManager grpc_handshake_manager;
163 typedef grpc_core::Handshaker grpc_handshaker;
164 void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
165 grpc_handshaker* handshaker);
166
167 #endif /* GRPC_CORE_LIB_TRANSPORT_HANDSHAKER_H */