protocol.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_PROTOCOL_HPP
  8. #define BHO_MYSQL_IMPL_INTERNAL_PROTOCOL_PROTOCOL_HPP
  9. #include <asio2/bho/mysql/column_type.hpp>
  10. #include <asio2/bho/mysql/diagnostics.hpp>
  11. #include <asio2/bho/mysql/error_code.hpp>
  12. #include <asio2/bho/mysql/field_view.hpp>
  13. #include <asio2/bho/mysql/metadata_collection_view.hpp>
  14. #include <asio2/bho/mysql/string_view.hpp>
  15. #include <asio2/bho/mysql/detail/coldef_view.hpp>
  16. #include <asio2/bho/mysql/detail/config.hpp>
  17. #include <asio2/bho/mysql/detail/ok_view.hpp>
  18. #include <asio2/bho/mysql/detail/resultset_encoding.hpp>
  19. #include <asio2/bho/mysql/impl/internal/protocol/capabilities.hpp>
  20. #include <asio2/bho/mysql/impl/internal/protocol/constants.hpp>
  21. #include <asio2/bho/mysql/impl/internal/protocol/db_flavor.hpp>
  22. #include <asio2/bho/mysql/impl/internal/protocol/static_buffer.hpp>
  23. #include <asio2/bho/config.hpp>
  24. #include <asio2/bho/core/span.hpp>
  25. #include <cstddef>
  26. #include <cstdint>
  27. #include <type_traits>
  28. namespace bho {
  29. namespace mysql {
  30. namespace detail {
  31. // Frame header
  32. constexpr std::size_t frame_header_size = 4;
  33. struct frame_header
  34. {
  35. std::uint32_t size;
  36. std::uint8_t sequence_number;
  37. };
  38. BHO_MYSQL_DECL
  39. void serialize_frame_header(frame_header, span<std::uint8_t, frame_header_size> buffer) noexcept;
  40. BHO_MYSQL_DECL
  41. frame_header deserialize_frame_header(span<const std::uint8_t, frame_header_size> buffer) noexcept;
  42. // OK packets (views because strings are non-owning)
  43. BHO_MYSQL_DECL
  44. error_code deserialize_ok_packet(span<const std::uint8_t> msg, ok_view& output) noexcept; // for testing
  45. // Error packets (exposed for testing)
  46. struct err_view
  47. {
  48. std::uint16_t error_code;
  49. string_view error_message;
  50. };
  51. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code
  52. deserialize_error_packet(span<const std::uint8_t> message, err_view& pack) noexcept;
  53. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code
  54. process_error_packet(span<const std::uint8_t> message, db_flavor flavor, diagnostics& diag);
  55. // Column definition
  56. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code
  57. deserialize_column_definition(span<const std::uint8_t> input, coldef_view& output) noexcept;
  58. // Quit
  59. struct quit_command
  60. {
  61. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  62. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  63. };
  64. // Ping
  65. struct ping_command
  66. {
  67. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  68. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  69. };
  70. // Reset connection
  71. struct reset_connection_command
  72. {
  73. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  74. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  75. };
  76. // Deserializes a response that may be an OK or an error packet.
  77. // Applicable for ping and reset connection
  78. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code
  79. deserialize_ok_response(span<const std::uint8_t> message, db_flavor flavor, diagnostics& diag);
  80. // Query
  81. struct query_command
  82. {
  83. string_view query;
  84. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  85. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  86. };
  87. // Prepare statement
  88. struct prepare_stmt_command
  89. {
  90. string_view stmt;
  91. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  92. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  93. };
  94. struct prepare_stmt_response
  95. {
  96. std::uint32_t id;
  97. std::uint16_t num_columns;
  98. std::uint16_t num_params;
  99. };
  100. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code deserialize_prepare_stmt_response_impl(
  101. span<const std::uint8_t> message,
  102. prepare_stmt_response& output
  103. ) noexcept; // exposed for testing, doesn't take header into account
  104. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code deserialize_prepare_stmt_response(
  105. span<const std::uint8_t> message,
  106. db_flavor flavor,
  107. prepare_stmt_response& output,
  108. diagnostics& diag
  109. );
  110. // Execute statement
  111. struct execute_stmt_command
  112. {
  113. std::uint32_t statement_id;
  114. span<const field_view> params;
  115. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  116. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  117. };
  118. // Close statement
  119. struct close_stmt_command
  120. {
  121. std::uint32_t statement_id{};
  122. constexpr close_stmt_command() = default;
  123. constexpr close_stmt_command(std::uint32_t statement_id) noexcept : statement_id(statement_id) {}
  124. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  125. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  126. };
  127. // Execution messages
  128. static_assert(std::is_trivially_destructible<error_code>::value, "");
  129. struct execute_response
  130. {
  131. enum class type_t
  132. {
  133. num_fields,
  134. ok_packet,
  135. error
  136. } type;
  137. union data_t
  138. {
  139. std::size_t num_fields;
  140. ok_view ok_pack;
  141. error_code err;
  142. data_t(size_t v) noexcept : num_fields(v) {}
  143. data_t(const ok_view& v) noexcept : ok_pack(v) {}
  144. data_t(error_code v) noexcept : err(v) {}
  145. } data;
  146. execute_response(std::size_t v) noexcept : type(type_t::num_fields), data(v) {}
  147. execute_response(const ok_view& v) noexcept : type(type_t::ok_packet), data(v) {}
  148. execute_response(error_code v) noexcept : type(type_t::error), data(v) {}
  149. };
  150. BHO_MYSQL_DECL
  151. execute_response deserialize_execute_response(
  152. span<const std::uint8_t> msg,
  153. db_flavor flavor,
  154. diagnostics& diag
  155. ) noexcept;
  156. struct row_message
  157. {
  158. enum class type_t
  159. {
  160. row,
  161. ok_packet,
  162. error
  163. } type;
  164. union data_t
  165. {
  166. span<const std::uint8_t> row;
  167. ok_view ok_pack;
  168. error_code err;
  169. data_t(span<const std::uint8_t> row) noexcept : row(row) {}
  170. data_t(const ok_view& ok_pack) noexcept : ok_pack(ok_pack) {}
  171. data_t(error_code err) noexcept : err(err) {}
  172. } data;
  173. row_message(span<const std::uint8_t> row) noexcept : type(type_t::row), data(row) {}
  174. row_message(const ok_view& ok_pack) noexcept : type(type_t::ok_packet), data(ok_pack) {}
  175. row_message(error_code v) noexcept : type(type_t::error), data(v) {}
  176. };
  177. BHO_MYSQL_DECL
  178. row_message deserialize_row_message(span<const std::uint8_t> msg, db_flavor flavor, diagnostics& diag);
  179. BHO_MYSQL_DECL
  180. error_code deserialize_row(
  181. resultset_encoding encoding,
  182. span<const std::uint8_t> message,
  183. metadata_collection_view meta,
  184. span<field_view> output // Should point to meta.size() field_view objects
  185. );
  186. // Server hello
  187. struct server_hello
  188. {
  189. using auth_buffer_type = static_buffer<8 + 0xff>;
  190. db_flavor server;
  191. auth_buffer_type auth_plugin_data;
  192. capabilities server_capabilities{};
  193. string_view auth_plugin_name;
  194. };
  195. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code deserialize_server_hello_impl(
  196. span<const std::uint8_t> msg,
  197. server_hello& output
  198. ); // exposed for testing, doesn't take message header into account
  199. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code
  200. deserialize_server_hello(span<const std::uint8_t> msg, server_hello& output, diagnostics& diag);
  201. // Login & ssl requests
  202. struct login_request
  203. {
  204. capabilities negotiated_capabilities; // capabilities
  205. std::uint32_t max_packet_size;
  206. std::uint32_t collation_id;
  207. string_view username;
  208. span<const std::uint8_t> auth_response;
  209. string_view database;
  210. string_view auth_plugin_name;
  211. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  212. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  213. };
  214. struct ssl_request
  215. {
  216. capabilities negotiated_capabilities;
  217. std::uint32_t max_packet_size;
  218. std::uint32_t collation_id;
  219. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  220. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  221. };
  222. // Auth switch
  223. struct auth_switch
  224. {
  225. string_view plugin_name;
  226. span<const std::uint8_t> auth_data;
  227. };
  228. BHO_ATTRIBUTE_NODISCARD BHO_MYSQL_DECL error_code deserialize_auth_switch(
  229. span<const std::uint8_t> msg,
  230. auth_switch& output
  231. ) noexcept; // exposed for testing
  232. struct handhake_server_response
  233. {
  234. struct ok_follows_t
  235. {
  236. };
  237. enum class type_t
  238. {
  239. ok,
  240. error,
  241. ok_follows,
  242. auth_switch,
  243. auth_more_data
  244. } type;
  245. union data_t
  246. {
  247. ok_view ok;
  248. error_code err;
  249. ok_follows_t ok_follows;
  250. auth_switch auth_sw;
  251. span<const std::uint8_t> more_data;
  252. data_t(const ok_view& ok) noexcept : ok(ok) {}
  253. data_t(error_code err) noexcept : err(err) {}
  254. data_t(ok_follows_t) noexcept : ok_follows({}) {}
  255. data_t(auth_switch msg) noexcept : auth_sw(msg) {}
  256. data_t(span<const std::uint8_t> more_data) noexcept : more_data(more_data) {}
  257. } data;
  258. handhake_server_response(const ok_view& ok) noexcept : type(type_t::ok), data(ok) {}
  259. handhake_server_response(error_code err) noexcept : type(type_t::error), data(err) {}
  260. handhake_server_response(ok_follows_t) noexcept : type(type_t::ok_follows), data(ok_follows_t{}) {}
  261. handhake_server_response(auth_switch auth_switch) noexcept : type(type_t::auth_switch), data(auth_switch)
  262. {
  263. }
  264. handhake_server_response(span<const std::uint8_t> more_data) noexcept
  265. : type(type_t::auth_more_data), data(more_data)
  266. {
  267. }
  268. };
  269. BHO_MYSQL_DECL
  270. handhake_server_response deserialize_handshake_server_response(
  271. span<const std::uint8_t> buff,
  272. db_flavor flavor,
  273. diagnostics& diag
  274. );
  275. struct auth_switch_response
  276. {
  277. span<const std::uint8_t> auth_plugin_data;
  278. BHO_MYSQL_DECL std::size_t get_size() const noexcept;
  279. BHO_MYSQL_DECL void serialize(span<std::uint8_t> buffer) const noexcept;
  280. };
  281. } // namespace detail
  282. } // namespace mysql
  283. } // namespace bho
  284. #ifdef BHO_MYSQL_HEADER_ONLY
  285. #include <asio2/bho/mysql/impl/internal/protocol/protocol.ipp>
  286. #endif
  287. #endif