mysqlnd_wireprotocol.h 9.6 KB

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