From: Dave Watson Date: Mon, 2 Feb 2015 22:10:44 +0000 (-0800) Subject: kill asyncsslserversocket X-Git-Tag: v0.24.0~6 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=46fd8dca2750d9064e85f7c593d89d4fe7e4e079;p=folly.git kill asyncsslserversocket Summary: This was only used in a unittest, every real use case uses AsyncServerSocket, and decides on its own to do SSL or not. Test Plan: fbconfig -r thrift/lib/cpp/test; fbmake runtests Reviewed By: alandau@fb.com Subscribers: fugalh, trunkagent, doug, alandau, bmatheny, ssl-diffs@, mshneer, jsedgwick, folly-diffs@ FB internal diff: D1806807 Signature: t1:1806807:1422396209:02333c736e1ef10963e3fe0b4ed6ecf6122475bb --- diff --git a/folly/Makefile.am b/folly/Makefile.am index 223206f0..72e1d159 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -138,7 +138,6 @@ nobase_follyinclude_HEADERS = \ io/async/AsyncUDPServerSocket.h \ io/async/AsyncUDPSocket.h \ io/async/AsyncServerSocket.h \ - io/async/AsyncSSLServerSocket.h \ io/async/AsyncSocket.h \ io/async/AsyncSSLSocket.h \ io/async/AsyncSocketException.h \ @@ -308,7 +307,6 @@ libfolly_la_SOURCES = \ io/async/AsyncTimeout.cpp \ io/async/AsyncUDPSocket.cpp \ io/async/AsyncServerSocket.cpp \ - io/async/AsyncSSLServerSocket.cpp \ io/async/AsyncSocket.cpp \ io/async/AsyncSSLSocket.cpp \ io/async/EventBase.cpp \ diff --git a/folly/io/async/AsyncSSLServerSocket.cpp b/folly/io/async/AsyncSSLServerSocket.cpp deleted file mode 100644 index 31ae9c8d..00000000 --- a/folly/io/async/AsyncSSLServerSocket.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2014 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include - -using std::shared_ptr; - -namespace folly { - -AsyncSSLServerSocket::AsyncSSLServerSocket( - const shared_ptr& ctx, - EventBase* eventBase) - : eventBase_(eventBase) - , serverSocket_(new AsyncServerSocket(eventBase)) - , ctx_(ctx) - , sslCallback_(nullptr) { -} - -AsyncSSLServerSocket::~AsyncSSLServerSocket() { -} - -void AsyncSSLServerSocket::destroy() { - // Stop accepting on the underlying socket as soon as destroy is called - if (sslCallback_ != nullptr) { - serverSocket_->pauseAccepting(); - serverSocket_->removeAcceptCallback(this, nullptr); - } - serverSocket_->destroy(); - serverSocket_ = nullptr; - sslCallback_ = nullptr; - - DelayedDestruction::destroy(); -} - -void AsyncSSLServerSocket::setSSLAcceptCallback(SSLAcceptCallback* callback) { - SSLAcceptCallback *oldCallback = sslCallback_; - sslCallback_ = callback; - if (callback != nullptr && oldCallback == nullptr) { - serverSocket_->addAcceptCallback(this, nullptr); - serverSocket_->startAccepting(); - } else if (callback == nullptr && oldCallback != nullptr) { - serverSocket_->removeAcceptCallback(this, nullptr); - serverSocket_->pauseAccepting(); - } -} - -void AsyncSSLServerSocket::attachEventBase(EventBase* eventBase) { - assert(sslCallback_ == nullptr); - eventBase_ = eventBase; - serverSocket_->attachEventBase(eventBase); -} - -void AsyncSSLServerSocket::detachEventBase() { - serverSocket_->detachEventBase(); - eventBase_ = nullptr; -} - -void -AsyncSSLServerSocket::connectionAccepted( - int fd, - const folly::SocketAddress& clientAddr) noexcept { - shared_ptr sslSock; - try { - // Create a AsyncSSLSocket object with the fd. The socket should be - // added to the event base and in the state of accepting SSL connection. - sslSock = AsyncSSLSocket::newSocket(ctx_, eventBase_, fd); - } catch (const std::exception &e) { - LOG(ERROR) << "Exception %s caught while creating a AsyncSSLSocket " - "object with socket " << e.what() << fd; - ::close(fd); - sslCallback_->acceptError(e); - return; - } - - // TODO: Perform the SSL handshake before invoking the callback - sslCallback_->connectionAccepted(sslSock); -} - -void AsyncSSLServerSocket::acceptError(const std::exception& ex) - noexcept { - LOG(ERROR) << "AsyncSSLServerSocket accept error: " << ex.what(); - sslCallback_->acceptError(ex); -} - -} // namespace diff --git a/folly/io/async/AsyncSSLServerSocket.h b/folly/io/async/AsyncSSLServerSocket.h deleted file mode 100644 index 56d59493..00000000 --- a/folly/io/async/AsyncSSLServerSocket.h +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -#pragma once - -#include -#include - - -namespace folly { -class SocketAddress; -class AsyncSSLSocket; - -class AsyncSSLServerSocket : public DelayedDestruction, - private AsyncServerSocket::AcceptCallback { - public: - class SSLAcceptCallback { - public: - virtual ~SSLAcceptCallback() {} - - /** - * connectionAccepted() is called whenever a new client connection is - * received. - * - * The SSLAcceptCallback will remain installed after connectionAccepted() - * returns. - * - * @param sock The newly accepted client socket. The - * SSLAcceptCallback - * assumes ownership of this socket, and is responsible - * for closing it when done. - */ - virtual void connectionAccepted( - const std::shared_ptr &sock) - noexcept = 0; - - /** - * acceptError() is called if an error occurs while accepting. - * - * The SSLAcceptCallback will remain installed even after an accept error. - * If the callback wants to uninstall itself and stop trying to accept new - * connections, it must explicit call setAcceptCallback(nullptr). - * - * @param ex An exception representing the error. - */ - virtual void acceptError(const std::exception& ex) noexcept = 0; - }; - - /** - * Create a new TAsyncSSLServerSocket with the specified EventBase. - * - * @param eventBase The EventBase to use for driving the asynchronous I/O. - * If this parameter is nullptr, attachEventBase() must be - * called before this socket can begin accepting - * connections. All TAsyncSSLSocket objects accepted by - * this server socket will be attached to this EventBase - * when they are created. - */ - explicit AsyncSSLServerSocket( - const std::shared_ptr& ctx, - EventBase* eventBase = nullptr); - - /** - * Destroy the socket. - * - * destroy() must be called to destroy the socket. The normal destructor is - * private, and should not be invoked directly. This prevents callers from - * deleting a TAsyncSSLServerSocket while it is invoking a callback. - */ - virtual void destroy(); - - virtual void bind(const folly::SocketAddress& address) { - serverSocket_->bind(address); - } - virtual void bind(uint16_t port) { - serverSocket_->bind(port); - } - void getAddress(folly::SocketAddress* addressReturn) { - serverSocket_->getAddress(addressReturn); - } - virtual void listen(int backlog) { - serverSocket_->listen(backlog); - } - - /** - * Helper function to create a shared_ptr. - * - * This passes in the correct destructor object, since TAsyncSSLServerSocket's - * destructor is protected and cannot be invoked directly. - */ - static std::shared_ptr newSocket( - const std::shared_ptr& ctx, - EventBase* evb) { - return std::shared_ptr( - new AsyncSSLServerSocket(ctx, evb), - Destructor()); - } - - /** - * Set the accept callback. - * - * This method may only be invoked from the EventBase's loop thread. - * - * @param callback The callback to invoke when a new socket - * connection is accepted and a new TAsyncSSLSocket is - * created. - * - * Throws TTransportException on error. - */ - void setSSLAcceptCallback(SSLAcceptCallback* callback); - - SSLAcceptCallback *getSSLAcceptCallback() const { - return sslCallback_; - } - - void attachEventBase(EventBase* eventBase); - void detachEventBase(); - - /** - * Returns the EventBase that the handler is currently attached to. - */ - EventBase* getEventBase() const { - return eventBase_; - } - - protected: - /** - * Protected destructor. - * - * Invoke destroy() instead to destroy the TAsyncSSLServerSocket. - */ - virtual ~AsyncSSLServerSocket(); - - protected: - virtual void connectionAccepted(int fd, - const folly::SocketAddress& clientAddr) - noexcept; - virtual void acceptError(const std::exception& ex) noexcept; - - EventBase* eventBase_; - AsyncServerSocket* serverSocket_; - // SSL context - std::shared_ptr ctx_; - // The accept callback - SSLAcceptCallback* sslCallback_; -}; - -} // namespace diff --git a/folly/io/async/README.md b/folly/io/async/README.md index e116c83b..e613bfc9 100644 --- a/folly/io/async/README.md +++ b/folly/io/async/README.md @@ -173,10 +173,8 @@ a lock on accept()ing from a port, preventing more than ~20k accepts / sec. There are various workarounds (SO_REUSEPORT), but generally clients should be using connection pooling instead when possible. -#### AsyncSSLServerSocket - -Similar to AsyncServerSocket, but provides callbacks for SSL -handshaking. +Since AsyncServerSocket provides an fd, an AsyncSSLSocket or +AsyncSocket can be made using the same codepath #### TAsyncUDPServerSocket