Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
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 : : #pragma once
18 : :
19 : : #ifdef WIRESTEAD_TLS_ENABLED
20 : :
21 : : #include <boost/asio.hpp>
22 : : #include <boost/asio/ssl.hpp>
23 : : #include <functional>
24 : : #include <memory>
25 : : #include <vector>
26 : :
27 : : #include "wirestead/base/visibility.hpp"
28 : : #include "wirestead/interface/itcp_socket.hpp"
29 : :
30 : : namespace wirestead {
31 : : namespace transport {
32 : :
33 : : namespace net = boost::asio;
34 : : namespace ssl = boost::asio::ssl;
35 : : using tcp = net::ip::tcp;
36 : :
37 : : /**
38 : : * @brief TLS implementation of TcpSocketInterface for accepted server sockets.
39 : : *
40 : : * The session above this only ever reads, writes, handshakes, shuts down and
41 : : * closes, so wrapping ssl::stream is enough to give it TLS without the session
42 : : * knowing. Only built when WIRESTEAD_ENABLE_TLS is on.
43 : : */
44 : : class WIRESTEAD_API SslTcpSocket : public interface::TcpSocketInterface {
45 : : public:
46 : : // The context is shared across every accepted connection and must outlive
47 : : // them, which is why it arrives as a shared_ptr rather than a reference.
48 : : SslTcpSocket(tcp::socket sock, std::shared_ptr<ssl::context> context);
49 : 64 : ~SslTcpSocket() override = default;
50 : :
51 : : void async_read_some(const net::mutable_buffer& buffer,
52 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override;
53 : : void async_write(const net::const_buffer& buffer,
54 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override;
55 : :
56 : : // ssl::stream has no scatter-gather write of its own - the record layer has
57 : : // to see one contiguous plaintext run anyway - so this hands the sequence to
58 : : // net::async_write, which coalesces it into as few TLS records as it can.
59 : : // The syscall-per-message saving from #572 survives; the writev does not.
60 : : void async_write(const std::vector<net::const_buffer>& buffers,
61 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override;
62 : :
63 : : void shutdown(tcp::socket::shutdown_type what, boost::system::error_code& ec) override;
64 : : void close(boost::system::error_code& ec) override;
65 : : tcp::endpoint remote_endpoint(boost::system::error_code& ec) const override;
66 : : void async_handshake(std::function<void(const boost::system::error_code&)> handler) override;
67 : :
68 : : private:
69 : : // Held so the context cannot be destroyed while a connection still uses it.
70 : : std::shared_ptr<ssl::context> context_;
71 : : ssl::stream<tcp::socket> stream_;
72 : : };
73 : :
74 : : } // namespace transport
75 : : } // namespace wirestead
76 : :
77 : : #endif // WIRESTEAD_TLS_ENABLED
|