|
| 1 | +/* |
| 2 | +* (C) 2015,2016,2017 Jack Lloyd |
| 3 | +* (C) 2016 Daniel Neus |
| 4 | +* (C) 2019 Nuno Goncalves <[email protected]> |
| 5 | +* (C) 2025 Kagan Can Sit |
| 6 | +* |
| 7 | +* Botan is released under the Simplified BSD License (see license.txt) |
| 8 | +*/ |
| 9 | + |
| 10 | +#ifndef BOTAN_SOCKET_ASIO_H_ |
| 11 | +#define BOTAN_SOCKET_ASIO_H_ |
| 12 | + |
| 13 | +#include <botan/exceptn.h> |
| 14 | +#include <botan/internal/fmt.h> |
| 15 | +#include <botan/internal/socket.h> |
| 16 | +#include <botan/internal/socket_udp.h> |
| 17 | +#include <chrono> |
| 18 | + |
| 19 | +#if defined(BOTAN_HAS_BOOST_ASIO) |
| 20 | + /* |
| 21 | + * We don't need serial port support anyway, and asking for it causes |
| 22 | + * macro conflicts with termios.h when this file is included in the |
| 23 | + * amalgamation. |
| 24 | + */ |
| 25 | + #define BOOST_ASIO_DISABLE_SERIAL_PORT |
| 26 | + #include <boost/asio.hpp> |
| 27 | + #include <boost/asio/system_timer.hpp> |
| 28 | +#endif |
| 29 | + |
| 30 | +namespace Botan::OS { |
| 31 | + |
| 32 | +#if defined(BOTAN_HAS_BOOST_ASIO) |
| 33 | + |
| 34 | +/* |
| 35 | + * A template-based implementation of Asio sockets that can be used |
| 36 | + * for both TCP and UDP protocols, reducing code duplication. |
| 37 | + * |
| 38 | + * @param BaseSocketType The base socket class (Socket for TCP, SocketUDP for UDP) |
| 39 | + * @param ProtocolType The Asio protocol type (boost::asio::ip::tcp or boost::asio::ip::udp) |
| 40 | + */ |
| 41 | +template <typename BaseSocketType, typename ProtocolType> |
| 42 | +class Asio_Socket_Base : public BaseSocketType { |
| 43 | + public: |
| 44 | + Asio_Socket_Base(std::string_view hostname, std::string_view service, std::chrono::milliseconds timeout) : |
| 45 | + // Convert milliseconds to microseconds for consistent timing operations |
| 46 | + m_timeout(std::chrono::duration_cast<std::chrono::microseconds>(timeout)), m_timer(m_io), m_socket(m_io) { |
| 47 | + m_timer.expires_after(m_timeout); |
| 48 | + check_timeout(); |
| 49 | + |
| 50 | + // Resolve the DNS |
| 51 | + typename ProtocolType::resolver resolver(m_io); |
| 52 | + auto endPoints = resolver.resolve(std::string{hostname}, std::string{service}); |
| 53 | + |
| 54 | + boost::system::error_code ec = boost::asio::error::would_block; |
| 55 | + |
| 56 | + auto connect_cb = [&ec](const boost::system::error_code& e, const typename ProtocolType::endpoint&) { |
| 57 | + ec = e; |
| 58 | + }; |
| 59 | + |
| 60 | + boost::asio::async_connect(m_socket, endPoints, connect_cb); |
| 61 | + |
| 62 | + while(ec == boost::asio::error::would_block) { |
| 63 | + m_io.run_one(); |
| 64 | + } |
| 65 | + |
| 66 | + if(ec) { |
| 67 | + throw boost::system::system_error(ec); |
| 68 | + } |
| 69 | + |
| 70 | + if(m_socket.is_open() == false) { |
| 71 | + throw System_Error(fmt("Connection to host {} failed", hostname)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void write(const uint8_t buf[], size_t len) override { |
| 76 | + m_timer.expires_after(m_timeout); |
| 77 | + |
| 78 | + boost::system::error_code ec = boost::asio::error::would_block; |
| 79 | + |
| 80 | + m_socket.async_send(boost::asio::buffer(buf, len), [&ec](boost::system::error_code e, size_t) { ec = e; }); |
| 81 | + |
| 82 | + while(ec == boost::asio::error::would_block) { |
| 83 | + m_io.run_one(); |
| 84 | + } |
| 85 | + |
| 86 | + if(ec) { |
| 87 | + throw boost::system::system_error(ec); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + size_t read(uint8_t buf[], size_t len) override { |
| 92 | + m_timer.expires_after(m_timeout); |
| 93 | + |
| 94 | + boost::system::error_code ec = boost::asio::error::would_block; |
| 95 | + size_t got = 0; |
| 96 | + |
| 97 | + // Use different read methods based on protocol type |
| 98 | + if constexpr(std::is_same_v<ProtocolType, boost::asio::ip::tcp>) { |
| 99 | + m_socket.async_read_some(boost::asio::buffer(buf, len), |
| 100 | + [&](boost::system::error_code cb_ec, size_t cb_got) { |
| 101 | + ec = cb_ec; |
| 102 | + got = cb_got; |
| 103 | + }); |
| 104 | + } else if constexpr(std::is_same_v<ProtocolType, boost::asio::ip::udp>) { |
| 105 | + m_socket.async_receive(boost::asio::buffer(buf, len), [&](boost::system::error_code cb_ec, size_t cb_got) { |
| 106 | + ec = cb_ec; |
| 107 | + got = cb_got; |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + while(ec == boost::asio::error::would_block) { |
| 112 | + m_io.run_one(); |
| 113 | + } |
| 114 | + |
| 115 | + if(ec) { |
| 116 | + if(ec == boost::asio::error::eof) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + throw boost::system::system_error(ec); // Some other error. |
| 120 | + } |
| 121 | + |
| 122 | + return got; |
| 123 | + } |
| 124 | + |
| 125 | + private: |
| 126 | + void check_timeout() { |
| 127 | + if(m_socket.is_open() && m_timer.expiry() < std::chrono::system_clock::now()) { |
| 128 | + boost::system::error_code err; |
| 129 | + |
| 130 | + // NOLINTNEXTLINE(bugprone-unused-return-value,cert-err33-c) |
| 131 | + m_socket.close(err); |
| 132 | + } |
| 133 | + |
| 134 | + m_timer.async_wait(std::bind(&Asio_Socket_Base::check_timeout, this)); |
| 135 | + } |
| 136 | + |
| 137 | + const std::chrono::microseconds m_timeout; |
| 138 | + boost::asio::io_context m_io; |
| 139 | + boost::asio::system_timer m_timer; |
| 140 | + typename ProtocolType::socket m_socket; |
| 141 | +}; |
| 142 | + |
| 143 | +// Convenience type aliases for common socket types |
| 144 | +using Asio_Socket = Asio_Socket_Base<Socket, boost::asio::ip::tcp>; |
| 145 | +using Asio_SocketUDP = Asio_Socket_Base<SocketUDP, boost::asio::ip::udp>; |
| 146 | + |
| 147 | +#endif // BOTAN_HAS_BOOST_ASIO |
| 148 | + |
| 149 | +} // namespace Botan::OS |
| 150 | + |
| 151 | +#endif // BOTAN_SOCKET_ASIO_H_ |
0 commit comments