mysqlnd_wireprotocol.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrey Hristov <andrey@php.net> |
  14. | Ulf Wendel <uw@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef MYSQLND_WIREPROTOCOL_H
  18. #define MYSQLND_WIREPROTOCOL_H
  19. #define MYSQLND_HEADER_SIZE 4
  20. #define COMPRESSED_HEADER_SIZE 3
  21. #define MYSQLND_NULL_LENGTH (zend_ulong) ~0
  22. /* Used in mysqlnd_debug.c */
  23. PHPAPI extern const char mysqlnd_read_header_name[];
  24. PHPAPI extern const char mysqlnd_read_body_name[];
  25. /* Packet handling */
  26. #define PACKET_WRITE(conn, packet) ((packet)->header.m->write_to_net((conn), (packet)))
  27. #define PACKET_READ(conn, packet) ((packet)->header.m->read_from_net((conn), (packet)))
  28. #define PACKET_FREE(packet) \
  29. do { \
  30. DBG_INF_FMT("PACKET_FREE(%p)", packet); \
  31. if ((packet)->header.m->free_mem) { \
  32. ((packet)->header.m->free_mem((packet))); \
  33. } \
  34. } while (0);
  35. PHPAPI extern const char * const mysqlnd_command_to_text[COM_END];
  36. /* Low-level extraction functionality */
  37. typedef struct st_mysqlnd_packet_methods {
  38. enum_func_status (*read_from_net)(MYSQLND_CONN_DATA * conn, void * packet);
  39. size_t (*write_to_net)(MYSQLND_CONN_DATA * conn, void * packet);
  40. void (*free_mem)(void *packet);
  41. } mysqlnd_packet_methods;
  42. typedef struct st_mysqlnd_packet_header {
  43. size_t size;
  44. zend_uchar packet_no;
  45. mysqlnd_packet_methods *m;
  46. } MYSQLND_PACKET_HEADER;
  47. /* Server greets the client */
  48. typedef struct st_mysqlnd_packet_greet {
  49. MYSQLND_PACKET_HEADER header;
  50. uint8_t protocol_version;
  51. char *server_version;
  52. uint32_t thread_id;
  53. char intern_auth_plugin_data[SCRAMBLE_LENGTH];
  54. MYSQLND_STRING authentication_plugin_data;
  55. /* 1 byte pad */
  56. uint32_t server_capabilities;
  57. uint8_t charset_no;
  58. uint16_t server_status;
  59. /* 13 byte pad, in 5.5 first 2 bytes are more capabilities followed by 1 byte scramble_length */
  60. bool pre41;
  61. /* If error packet, we use these */
  62. char error[MYSQLND_ERRMSG_SIZE+1];
  63. char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1];
  64. unsigned int error_no;
  65. char *auth_protocol;
  66. } MYSQLND_PACKET_GREET;
  67. /* Client authenticates */
  68. typedef struct st_mysqlnd_packet_auth {
  69. MYSQLND_PACKET_HEADER header;
  70. const char *user;
  71. const zend_uchar *auth_data;
  72. size_t auth_data_len;
  73. const char *db;
  74. const char *auth_plugin_name;
  75. uint32_t client_flags;
  76. uint32_t max_packet_size;
  77. uint8_t charset_no;
  78. /* Here the packet ends. This is user supplied data */
  79. bool send_auth_data;
  80. bool is_change_user_packet;
  81. bool silent;
  82. HashTable *connect_attr;
  83. size_t db_len;
  84. } MYSQLND_PACKET_AUTH;
  85. /* Auth response packet */
  86. typedef struct st_mysqlnd_packet_auth_response {
  87. MYSQLND_PACKET_HEADER header;
  88. uint8_t response_code;
  89. uint64_t affected_rows;
  90. uint64_t last_insert_id;
  91. uint16_t server_status;
  92. uint16_t warning_count;
  93. char *message;
  94. size_t message_len;
  95. /* If error packet, we use these */
  96. char error[MYSQLND_ERRMSG_SIZE+1];
  97. char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1];
  98. unsigned int error_no;
  99. char *new_auth_protocol;
  100. size_t new_auth_protocol_len;
  101. zend_uchar *new_auth_protocol_data;
  102. size_t new_auth_protocol_data_len;
  103. } MYSQLND_PACKET_AUTH_RESPONSE;
  104. /* Auth response packet */
  105. typedef struct st_mysqlnd_packet_change_auth_response {
  106. MYSQLND_PACKET_HEADER header;
  107. const zend_uchar *auth_data;
  108. size_t auth_data_len;
  109. } MYSQLND_PACKET_CHANGE_AUTH_RESPONSE;
  110. /* OK packet */
  111. typedef struct st_mysqlnd_packet_ok {
  112. MYSQLND_PACKET_HEADER header;
  113. uint8_t field_count; /* always 0x0 */
  114. uint64_t affected_rows;
  115. uint64_t last_insert_id;
  116. uint16_t server_status;
  117. uint16_t warning_count;
  118. char *message;
  119. size_t message_len;
  120. /* If error packet, we use these */
  121. char error[MYSQLND_ERRMSG_SIZE+1];
  122. char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1];
  123. unsigned int error_no;
  124. } MYSQLND_PACKET_OK;
  125. /* Command packet */
  126. typedef struct st_mysqlnd_packet_command {
  127. MYSQLND_PACKET_HEADER header;
  128. enum php_mysqlnd_server_command command;
  129. MYSQLND_CSTRING argument;
  130. } MYSQLND_PACKET_COMMAND;
  131. /* EOF packet */
  132. typedef struct st_mysqlnd_packet_eof {
  133. MYSQLND_PACKET_HEADER header;
  134. uint8_t field_count; /* 0xFE */
  135. uint16_t warning_count;
  136. uint16_t server_status;
  137. /* If error packet, we use these */
  138. char error[MYSQLND_ERRMSG_SIZE+1];
  139. char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1];
  140. unsigned int error_no;
  141. } MYSQLND_PACKET_EOF;
  142. /* EOF packet */
  143. /* Result Set header*/
  144. typedef struct st_mysqlnd_packet_rset_header {
  145. MYSQLND_PACKET_HEADER header;
  146. /*
  147. 0x00 => ok
  148. ~0 => LOAD DATA LOCAL
  149. error_no != 0 => error
  150. others => result set -> Read res_field packets up to field_count
  151. */
  152. zend_ulong field_count;
  153. /*
  154. These are filled if no SELECT query. For SELECT warning_count
  155. and server status are in the last row packet, the EOF packet.
  156. */
  157. uint16_t warning_count;
  158. uint16_t server_status;
  159. uint64_t affected_rows;
  160. uint64_t last_insert_id;
  161. /* This is for both LOAD DATA or info, when no result set */
  162. MYSQLND_STRING info_or_local_file;
  163. /* If error packet, we use these */
  164. MYSQLND_ERROR_INFO error_info;
  165. } MYSQLND_PACKET_RSET_HEADER;
  166. /* Result set field packet */
  167. typedef struct st_mysqlnd_packet_res_field {
  168. MYSQLND_PACKET_HEADER header;
  169. MYSQLND_MEMORY_POOL *memory_pool;
  170. MYSQLND_FIELD *metadata;
  171. /* For table definitions, empty for result sets */
  172. bool skip_parsing;
  173. MYSQLND_ERROR_INFO error_info;
  174. } MYSQLND_PACKET_RES_FIELD;
  175. /* Row packet */
  176. typedef struct st_mysqlnd_packet_row {
  177. MYSQLND_PACKET_HEADER header;
  178. uint32_t field_count;
  179. bool eof;
  180. /*
  181. These are, of course, only for SELECT in the EOF packet,
  182. which is detected by this packet
  183. */
  184. uint16_t warning_count;
  185. uint16_t server_status;
  186. MYSQLND_ROW_BUFFER row_buffer;
  187. MYSQLND_MEMORY_POOL * result_set_memory_pool;
  188. bool binary_protocol;
  189. MYSQLND_FIELD *fields_metadata;
  190. /* If error packet, we use these */
  191. MYSQLND_ERROR_INFO error_info;
  192. } MYSQLND_PACKET_ROW;
  193. /* Statistics packet */
  194. typedef struct st_mysqlnd_packet_stats {
  195. MYSQLND_PACKET_HEADER header;
  196. MYSQLND_STRING message;
  197. } MYSQLND_PACKET_STATS;
  198. /* COM_PREPARE response packet */
  199. typedef struct st_mysqlnd_packet_prepare_response {
  200. MYSQLND_PACKET_HEADER header;
  201. /* also known as field_count 0x00=OK , 0xFF=error */
  202. unsigned char error_code;
  203. zend_ulong stmt_id;
  204. unsigned int field_count;
  205. unsigned int param_count;
  206. unsigned int warning_count;
  207. /* present in case of error */
  208. MYSQLND_ERROR_INFO error_info;
  209. } MYSQLND_PACKET_PREPARE_RESPONSE;
  210. /* Statistics packet */
  211. typedef struct st_mysqlnd_packet_chg_user_resp {
  212. MYSQLND_PACKET_HEADER header;
  213. uint32_t response_code;
  214. /* message_len is not part of the packet*/
  215. uint16_t server_capabilities;
  216. /* If error packet, we use these */
  217. MYSQLND_ERROR_INFO error_info;
  218. bool server_asked_323_auth;
  219. char *new_auth_protocol;
  220. size_t new_auth_protocol_len;
  221. zend_uchar *new_auth_protocol_data;
  222. size_t new_auth_protocol_data_len;
  223. } MYSQLND_PACKET_CHG_USER_RESPONSE;
  224. /* Command packet */
  225. typedef struct st_mysqlnd_packet_sha256_pk_request {
  226. MYSQLND_PACKET_HEADER header;
  227. } MYSQLND_PACKET_SHA256_PK_REQUEST;
  228. typedef struct st_mysqlnd_packet_sha256_pk_request_response {
  229. MYSQLND_PACKET_HEADER header;
  230. zend_uchar *public_key;
  231. size_t public_key_len;
  232. } MYSQLND_PACKET_SHA256_PK_REQUEST_RESPONSE;
  233. typedef struct st_mysqlnd_packet_cached_sha2_result {
  234. MYSQLND_PACKET_HEADER header;
  235. uint8_t response_code;
  236. uint8_t result;
  237. uint8_t request;
  238. zend_uchar * password;
  239. size_t password_len;
  240. /* Used for auth switch request */
  241. char *new_auth_protocol;
  242. size_t new_auth_protocol_len;
  243. zend_uchar *new_auth_protocol_data;
  244. size_t new_auth_protocol_data_len;
  245. /* Used for error result */
  246. char error[MYSQLND_ERRMSG_SIZE+1];
  247. char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1];
  248. unsigned int error_no;
  249. } MYSQLND_PACKET_CACHED_SHA2_RESULT;
  250. zend_ulong php_mysqlnd_net_field_length(const zend_uchar **packet);
  251. zend_uchar * php_mysqlnd_net_store_length(zend_uchar *packet, const uint64_t length);
  252. size_t php_mysqlnd_net_store_length_size(uint64_t length);
  253. PHPAPI extern const char * const mysqlnd_empty_string;
  254. enum_func_status php_mysqlnd_rowp_read_binary_protocol(MYSQLND_ROW_BUFFER * row_buffer, zval * fields,
  255. unsigned int field_count, const MYSQLND_FIELD * fields_metadata,
  256. bool as_int_or_float, MYSQLND_STATS * stats);
  257. enum_func_status php_mysqlnd_rowp_read_text_protocol(MYSQLND_ROW_BUFFER * row_buffer, zval * fields,
  258. unsigned int field_count, const MYSQLND_FIELD * fields_metadata,
  259. bool as_int_or_float, MYSQLND_STATS * stats);
  260. PHPAPI MYSQLND_PROTOCOL_PAYLOAD_DECODER_FACTORY * mysqlnd_protocol_payload_decoder_factory_init(MYSQLND_CONN_DATA * conn, const bool persistent);
  261. PHPAPI void mysqlnd_protocol_payload_decoder_factory_free(MYSQLND_PROTOCOL_PAYLOAD_DECODER_FACTORY * const payload_decoder_factory);
  262. #endif /* MYSQLND_WIREPROTOCOL_H */