mysqlnd_wireprotocol.h 10 KB

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