Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:41

0001 //
0002 // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_INTERNAL_PROTOCOL_PROTOCOL_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_PROTOCOL_PROTOCOL_HPP
0010 
0011 #include <boost/mysql/column_type.hpp>
0012 #include <boost/mysql/diagnostics.hpp>
0013 #include <boost/mysql/error_code.hpp>
0014 #include <boost/mysql/field_view.hpp>
0015 #include <boost/mysql/metadata_collection_view.hpp>
0016 #include <boost/mysql/string_view.hpp>
0017 
0018 #include <boost/mysql/detail/coldef_view.hpp>
0019 #include <boost/mysql/detail/config.hpp>
0020 #include <boost/mysql/detail/ok_view.hpp>
0021 #include <boost/mysql/detail/resultset_encoding.hpp>
0022 
0023 #include <boost/mysql/impl/internal/protocol/capabilities.hpp>
0024 #include <boost/mysql/impl/internal/protocol/constants.hpp>
0025 #include <boost/mysql/impl/internal/protocol/db_flavor.hpp>
0026 #include <boost/mysql/impl/internal/protocol/static_buffer.hpp>
0027 
0028 #include <boost/config.hpp>
0029 #include <boost/core/span.hpp>
0030 
0031 #include <cstddef>
0032 #include <cstdint>
0033 #include <type_traits>
0034 
0035 namespace boost {
0036 namespace mysql {
0037 namespace detail {
0038 
0039 // Frame header
0040 constexpr std::size_t frame_header_size = 4;
0041 
0042 struct frame_header
0043 {
0044     std::uint32_t size;
0045     std::uint8_t sequence_number;
0046 };
0047 
0048 BOOST_MYSQL_DECL
0049 void serialize_frame_header(frame_header, span<std::uint8_t, frame_header_size> buffer) noexcept;
0050 
0051 BOOST_MYSQL_DECL
0052 frame_header deserialize_frame_header(span<const std::uint8_t, frame_header_size> buffer) noexcept;
0053 
0054 // OK packets (views because strings are non-owning)
0055 BOOST_MYSQL_DECL
0056 error_code deserialize_ok_packet(span<const std::uint8_t> msg, ok_view& output) noexcept;  // for testing
0057 
0058 // Error packets (exposed for testing)
0059 struct err_view
0060 {
0061     std::uint16_t error_code;
0062     string_view error_message;
0063 };
0064 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code
0065 deserialize_error_packet(span<const std::uint8_t> message, err_view& pack) noexcept;
0066 
0067 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code
0068 process_error_packet(span<const std::uint8_t> message, db_flavor flavor, diagnostics& diag);
0069 
0070 // Column definition
0071 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code
0072 deserialize_column_definition(span<const std::uint8_t> input, coldef_view& output) noexcept;
0073 
0074 // Quit
0075 struct quit_command
0076 {
0077     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0078     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0079 };
0080 
0081 // Ping
0082 struct ping_command
0083 {
0084     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0085     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0086 };
0087 
0088 // Reset connection
0089 struct reset_connection_command
0090 {
0091     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0092     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0093 };
0094 
0095 // Deserializes a response that may be an OK or an error packet.
0096 // Applicable for ping and reset connection
0097 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code
0098 deserialize_ok_response(span<const std::uint8_t> message, db_flavor flavor, diagnostics& diag);
0099 
0100 // Query
0101 struct query_command
0102 {
0103     string_view query;
0104 
0105     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0106     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0107 };
0108 
0109 // Prepare statement
0110 struct prepare_stmt_command
0111 {
0112     string_view stmt;
0113 
0114     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0115     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0116 };
0117 struct prepare_stmt_response
0118 {
0119     std::uint32_t id;
0120     std::uint16_t num_columns;
0121     std::uint16_t num_params;
0122 };
0123 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code deserialize_prepare_stmt_response_impl(
0124     span<const std::uint8_t> message,
0125     prepare_stmt_response& output
0126 ) noexcept;  // exposed for testing, doesn't take header into account
0127 
0128 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code deserialize_prepare_stmt_response(
0129     span<const std::uint8_t> message,
0130     db_flavor flavor,
0131     prepare_stmt_response& output,
0132     diagnostics& diag
0133 );
0134 
0135 // Execute statement
0136 struct execute_stmt_command
0137 {
0138     std::uint32_t statement_id;
0139     span<const field_view> params;
0140 
0141     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0142     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0143 };
0144 
0145 // Close statement
0146 struct close_stmt_command
0147 {
0148     std::uint32_t statement_id{};
0149 
0150     constexpr close_stmt_command() = default;
0151     constexpr close_stmt_command(std::uint32_t statement_id) noexcept : statement_id(statement_id) {}
0152 
0153     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0154     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0155 };
0156 
0157 // Execution messages
0158 static_assert(std::is_trivially_destructible<error_code>::value, "");
0159 struct execute_response
0160 {
0161     enum class type_t
0162     {
0163         num_fields,
0164         ok_packet,
0165         error
0166     } type;
0167     union data_t
0168     {
0169         std::size_t num_fields;
0170         ok_view ok_pack;
0171         error_code err;
0172 
0173         data_t(size_t v) noexcept : num_fields(v) {}
0174         data_t(const ok_view& v) noexcept : ok_pack(v) {}
0175         data_t(error_code v) noexcept : err(v) {}
0176     } data;
0177 
0178     execute_response(std::size_t v) noexcept : type(type_t::num_fields), data(v) {}
0179     execute_response(const ok_view& v) noexcept : type(type_t::ok_packet), data(v) {}
0180     execute_response(error_code v) noexcept : type(type_t::error), data(v) {}
0181 };
0182 BOOST_MYSQL_DECL
0183 execute_response deserialize_execute_response(
0184     span<const std::uint8_t> msg,
0185     db_flavor flavor,
0186     diagnostics& diag
0187 ) noexcept;
0188 
0189 struct row_message
0190 {
0191     enum class type_t
0192     {
0193         row,
0194         ok_packet,
0195         error
0196     } type;
0197     union data_t
0198     {
0199         span<const std::uint8_t> row;
0200         ok_view ok_pack;
0201         error_code err;
0202 
0203         data_t(span<const std::uint8_t> row) noexcept : row(row) {}
0204         data_t(const ok_view& ok_pack) noexcept : ok_pack(ok_pack) {}
0205         data_t(error_code err) noexcept : err(err) {}
0206     } data;
0207 
0208     row_message(span<const std::uint8_t> row) noexcept : type(type_t::row), data(row) {}
0209     row_message(const ok_view& ok_pack) noexcept : type(type_t::ok_packet), data(ok_pack) {}
0210     row_message(error_code v) noexcept : type(type_t::error), data(v) {}
0211 };
0212 BOOST_MYSQL_DECL
0213 row_message deserialize_row_message(span<const std::uint8_t> msg, db_flavor flavor, diagnostics& diag);
0214 
0215 BOOST_MYSQL_DECL
0216 error_code deserialize_row(
0217     resultset_encoding encoding,
0218     span<const std::uint8_t> message,
0219     metadata_collection_view meta,
0220     span<field_view> output  // Should point to meta.size() field_view objects
0221 );
0222 
0223 // Server hello
0224 struct server_hello
0225 {
0226     using auth_buffer_type = static_buffer<8 + 0xff>;
0227     db_flavor server;
0228     auth_buffer_type auth_plugin_data;
0229     capabilities server_capabilities{};
0230     string_view auth_plugin_name;
0231 };
0232 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code deserialize_server_hello_impl(
0233     span<const std::uint8_t> msg,
0234     server_hello& output
0235 );  // exposed for testing, doesn't take message header into account
0236 
0237 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code
0238 deserialize_server_hello(span<const std::uint8_t> msg, server_hello& output, diagnostics& diag);
0239 
0240 // Login & ssl requests
0241 struct login_request
0242 {
0243     capabilities negotiated_capabilities;  // capabilities
0244     std::uint32_t max_packet_size;
0245     std::uint32_t collation_id;
0246     string_view username;
0247     span<const std::uint8_t> auth_response;
0248     string_view database;
0249     string_view auth_plugin_name;
0250 
0251     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0252     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0253 };
0254 
0255 struct ssl_request
0256 {
0257     capabilities negotiated_capabilities;
0258     std::uint32_t max_packet_size;
0259     std::uint32_t collation_id;
0260 
0261     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0262     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0263 };
0264 
0265 // Auth switch
0266 struct auth_switch
0267 {
0268     string_view plugin_name;
0269     span<const std::uint8_t> auth_data;
0270 };
0271 
0272 BOOST_ATTRIBUTE_NODISCARD BOOST_MYSQL_DECL error_code deserialize_auth_switch(
0273     span<const std::uint8_t> msg,
0274     auth_switch& output
0275 ) noexcept;  // exposed for testing
0276 
0277 struct handhake_server_response
0278 {
0279     struct ok_follows_t
0280     {
0281     };
0282 
0283     enum class type_t
0284     {
0285         ok,
0286         error,
0287         ok_follows,
0288         auth_switch,
0289         auth_more_data
0290     } type;
0291 
0292     union data_t
0293     {
0294         ok_view ok;
0295         error_code err;
0296         ok_follows_t ok_follows;
0297         auth_switch auth_sw;
0298         span<const std::uint8_t> more_data;
0299 
0300         data_t(const ok_view& ok) noexcept : ok(ok) {}
0301         data_t(error_code err) noexcept : err(err) {}
0302         data_t(ok_follows_t) noexcept : ok_follows({}) {}
0303         data_t(auth_switch msg) noexcept : auth_sw(msg) {}
0304         data_t(span<const std::uint8_t> more_data) noexcept : more_data(more_data) {}
0305     } data;
0306 
0307     handhake_server_response(const ok_view& ok) noexcept : type(type_t::ok), data(ok) {}
0308     handhake_server_response(error_code err) noexcept : type(type_t::error), data(err) {}
0309     handhake_server_response(ok_follows_t) noexcept : type(type_t::ok_follows), data(ok_follows_t{}) {}
0310     handhake_server_response(auth_switch auth_switch) noexcept : type(type_t::auth_switch), data(auth_switch)
0311     {
0312     }
0313     handhake_server_response(span<const std::uint8_t> more_data) noexcept
0314         : type(type_t::auth_more_data), data(more_data)
0315     {
0316     }
0317 };
0318 BOOST_MYSQL_DECL
0319 handhake_server_response deserialize_handshake_server_response(
0320     span<const std::uint8_t> buff,
0321     db_flavor flavor,
0322     diagnostics& diag
0323 );
0324 
0325 struct auth_switch_response
0326 {
0327     span<const std::uint8_t> auth_plugin_data;
0328 
0329     BOOST_MYSQL_DECL std::size_t get_size() const noexcept;
0330     BOOST_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
0331 };
0332 
0333 }  // namespace detail
0334 }  // namespace mysql
0335 }  // namespace boost
0336 
0337 #ifdef BOOST_MYSQL_HEADER_ONLY
0338 #include <boost/mysql/impl/internal/protocol/protocol.ipp>
0339 #endif
0340 
0341 #endif