proxygen
PeekingAcceptorHandshakeHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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 #pragma once
17 
20 
21 namespace wangle {
22 
33  public SocketPeeker::Callback {
34  public:
35  class PeekCallback {
36  public:
37  explicit PeekCallback(size_t bytesRequired):
38  bytesRequired_(bytesRequired) {}
39  virtual ~PeekCallback() = default;
40 
41  size_t getBytesRequired() const {
42  return bytesRequired_;
43  }
44 
46  const std::vector<uint8_t>& peekedBytes,
47  const folly::SocketAddress& clientAddr,
48  std::chrono::steady_clock::time_point acceptTime,
49  TransportInfo& tinfo) = 0;
50 
51  private:
52  const size_t bytesRequired_;
53  };
54 
56  const folly::SocketAddress& clientAddr,
57  std::chrono::steady_clock::time_point acceptTime,
58  TransportInfo& tinfo,
59  const std::vector<PeekCallback*>& peekCallbacks,
60  size_t numBytes)
61  : clientAddr_(clientAddr),
62  acceptTime_(acceptTime),
63  tinfo_(tinfo),
64  peekCallbacks_(peekCallbacks),
65  numBytes_(numBytes) {}
66 
67  // From AcceptorHandshakeHelper
68  void start(
70  AcceptorHandshakeHelper::Callback* callback) noexcept override {
71  socket_ = std::move(sock);
72  callback_ = callback;
73  CHECK_EQ(
74  socket_->getSSLState(),
75  folly::AsyncSSLSocket::SSLStateEnum::STATE_UNENCRYPTED);
76  peeker_.reset(new SocketPeeker(*socket_, this, numBytes_));
77  peeker_->start();
78  }
79 
81  CHECK_NE(socket_.get() == nullptr, helper_.get() == nullptr);
82  if (socket_) {
83  socket_->closeNow();
84  } else if (helper_) {
85  helper_->dropConnection(reason);
86  }
87  }
88 
89  void peekSuccess(std::vector<uint8_t> peekBytes) noexcept override {
91  peeker_ = nullptr;
92 
93  for (auto& peekCallback : peekCallbacks_) {
94  helper_ = peekCallback->getHelper(
95  peekBytes, clientAddr_, acceptTime_, tinfo_);
96  if (helper_) {
97  break;
98  }
99  }
100 
101  if (!helper_) {
102  // could not get a helper, report error.
103  auto type =
104  folly::AsyncSocketException::AsyncSocketExceptionType::CORRUPTED_DATA;
105  return peekError(
106  folly::AsyncSocketException(type, "Unrecognized protocol"));
107  }
108 
109  auto callback = callback_;
110  callback_ = nullptr;
111  helper_->start(std::move(socket_), callback);
112  CHECK(!socket_);
113  }
114 
115  void peekError(const folly::AsyncSocketException& ex) noexcept override {
116  peeker_ = nullptr;
117  auto callback = callback_;
118  callback_ = nullptr;
119  callback->connectionError(
121  }
122 
123  private:
124  ~PeekingAcceptorHandshakeHelper() override = default;
125 
129 
132  std::chrono::steady_clock::time_point acceptTime_;
134  const std::vector<PeekCallback*>& peekCallbacks_;
135  size_t numBytes_;
136 };
137 
139 
141  public:
143  Acceptor* acceptor,
144  const folly::SocketAddress& clientAddr,
145  std::chrono::steady_clock::time_point acceptTime,
146  TransportInfo tinfo,
147  const std::vector<PeekingCallbackPtr>& peekCallbacks,
148  size_t numBytes):
150  acceptor,
151  clientAddr,
152  acceptTime,
153  std::move(tinfo)),
154  peekCallbacks_(peekCallbacks),
155  numBytes_(numBytes) {}
156 
157  protected:
160  clientAddr_,
161  acceptTime_,
162  tinfo_,
164  numBytes_));
165  helper_->start(std::move(sock), this);
166  }
167 
168  const std::vector<PeekingCallbackPtr>& peekCallbacks_;
169  size_t numBytes_;
170 };
171 
172 }
void peekError(const folly::AsyncSocketException &ex) noexceptoverride
AcceptorHandshakeHelper::Callback * callback_
void startHelper(folly::AsyncSSLSocket::UniquePtr sock) override
SSLErrorEnum
Definition: SSLUtil.h:42
PskType type
void start(folly::AsyncSSLSocket::UniquePtr sock, AcceptorHandshakeHelper::Callback *callback) noexceptoverride
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
const std::vector< PeekingCallbackPtr > & peekCallbacks_
requires E e noexcept(noexcept(s.error(std::move(e))))
std::unique_ptr< AsyncSSLSocket, Destructor > UniquePtr
void dropConnection(SSLErrorEnum reason=SSLErrorEnum::NO_ERROR) override
~PeekingAcceptorHandshakeHelper() override=default
void peekSuccess(std::vector< uint8_t > peekBytes) noexceptoverride
std::chrono::steady_clock::time_point acceptTime_
virtual void connectionError(folly::AsyncTransportWrapper *transport, folly::exception_wrapper ex, folly::Optional< SSLErrorEnum > sslErr) noexcept=0
const std::vector< PeekCallback * > & peekCallbacks_
PeekingAcceptorHandshakeManager(Acceptor *acceptor, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo tinfo, const std::vector< PeekingCallbackPtr > &peekCallbacks, size_t numBytes)
std::unique_ptr< AcceptorHandshakeHelper, folly::DelayedDestruction::Destructor > UniquePtr
PeekingAcceptorHandshakeHelper(const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo &tinfo, const std::vector< PeekCallback * > &peekCallbacks, size_t numBytes)
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
virtual AcceptorHandshakeHelper::UniquePtr getHelper(const std::vector< uint8_t > &peekedBytes, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo &tinfo)=0
constexpr None none
Definition: Optional.h:87