1 /* Copyright (c) 2014, 2021, Oracle and/or its affiliates. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License, version 2.0, 5 as published by the Free Software Foundation. 6 7 This program is also distributed with certain software (including 8 but not limited to OpenSSL) that is licensed under separate terms, 9 as designated in a particular file or component or in included license 10 documentation. The authors of MySQL hereby grant you an additional 11 permission to link the program and your derivative works with the 12 separately licensed software that they have included with MySQL. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License, version 2.0, for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software Foundation, 21 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ 22 23 #ifndef GCS_PLUGIN_MESSAGES_INCLUDED 24 #define GCS_PLUGIN_MESSAGES_INCLUDED 25 26 #include <string> 27 #include <vector> 28 29 /* 30 Since this file is used on unit tests, through member_info.h, 31 includes must set here and not through plugin_server_include.h. 32 */ 33 #include <my_global.h> 34 35 /** 36 This is the base GCS plugin message. 37 It is composed by a fixed header and 1 or more payload items. 38 39 The on-the-wire layout looks like this: 40 41 +-----------------------------------+ 42 | fixed header | payload | 43 +-----------------------------------+ 44 45 The on-the-wire representation of the message is: 46 47 +-------------------+-----------+--------------------------------------+ 48 | field | wire size | description | 49 +===================+===========+======================================+ 50 | version | 4 bytes | protocol version | 51 | fixed_hdr_len | 2 bytes | length of the fixed header | 52 | message_len | 8 bytes | length of the message | 53 | cargo_type | 2 bytes | the cargo type in the payload | 54 +-------------------+-----------+--------------------------------------+ 55 | payload_item_type | 2 bytes | the item type in the payload | 56 | payload_item_len | 8 bytes | length of the payload item | 57 | payload_item | X bytes | payload item | 58 +-------------------+-----------+--------------------------------------+ 59 60 The last tree lines can occur one or more times. 61 */ 62 63 class Plugin_gcs_message 64 { 65 public: 66 /** 67 The protocol version number. 68 */ 69 static const int PLUGIN_GCS_MESSAGE_VERSION; 70 71 /** 72 The protocol version number. 73 */ 74 static const unsigned int WIRE_VERSION_SIZE; 75 76 /** 77 The on-the-wire size of the header length field. 78 */ 79 static const unsigned int WIRE_HD_LEN_SIZE; 80 81 /** 82 The on-the-wire size of the message size field. 83 */ 84 static const unsigned int WIRE_MSG_LEN_SIZE; 85 86 /** 87 The on-the-wire size of the cargo type field. 88 */ 89 static const unsigned int WIRE_CARGO_TYPE_SIZE; 90 91 /** 92 The on-the-wire size of the fixed header. 93 */ 94 static const unsigned int WIRE_FIXED_HEADER_SIZE; 95 96 /** 97 The on-the-wire size of the each payload item type field. 98 */ 99 static const unsigned int WIRE_PAYLOAD_ITEM_TYPE_SIZE; 100 101 /** 102 The on-the-wire size of the each payload item size field. 103 */ 104 static const unsigned int WIRE_PAYLOAD_ITEM_LEN_SIZE; 105 106 /** 107 The on-the-wire size of the payload item header. 108 */ 109 static const unsigned int WIRE_PAYLOAD_ITEM_HEADER_SIZE; 110 111 /** 112 The different cargo type codes. 113 114 NOTE: all type values must fit into WIRE_CARGO_TYPE_SIZE bytes storage. 115 */ 116 enum enum_cargo_type 117 { 118 // This type should not be used anywhere. 119 CT_UNKNOWN= 0, 120 121 // This cargo type is used for certification events, GTID_EXECUTED broadcast. 122 CT_CERTIFICATION_MESSAGE= 1, 123 124 // This cargo type is used for transaction data. 125 CT_TRANSACTION_MESSAGE= 2, 126 127 // This cargo type is used for recovery events, signal when a given member 128 // becomes online. 129 CT_RECOVERY_MESSAGE= 3, 130 131 // This cargo type is used for messaging related to stage exchanges, 132 // on which it represents one member. 133 CT_MEMBER_INFO_MESSAGE= 4, 134 135 // This cargo type is used for messaging related to stage exchanges, 136 // on which it represents a set of members. 137 CT_MEMBER_INFO_MANAGER_MESSAGE= 5, 138 139 // This cargo type is used for messaging related to members pipeline 140 // stats. 141 CT_PIPELINE_STATS_MEMBER_MESSAGE= 6, 142 143 // This cargo type is used for messaging related to single primary 144 // mode. 145 CT_SINGLE_PRIMARY_MESSAGE= 7, 146 147 // No valid type codes can appear after this one. 148 CT_MAX= 8 149 }; 150 151 private: 152 /** 153 This header instance protocol version. 154 */ 155 int m_version; 156 157 /** 158 This header instance length. 159 */ 160 unsigned short m_fixed_header_len; 161 162 /** 163 This is the message length field. 164 */ 165 unsigned long long m_msg_len; 166 167 /** 168 The cargo type code. 169 */ 170 enum_cargo_type m_cargo_type; 171 172 public: ~Plugin_gcs_message()173 virtual ~Plugin_gcs_message() {} 174 175 /** 176 @return the value of the version field. 177 */ get_version()178 int get_version() { return m_version; } 179 180 /** 181 @return the value of the header length field value. 182 */ get_header_length()183 unsigned short get_header_length() { return m_fixed_header_len; } 184 185 /** 186 @return the cargo type. 187 */ get_cargo_type()188 enum_cargo_type get_cargo_type() { return m_cargo_type; } 189 190 /** 191 @return the message length field value. 192 */ get_msg_length()193 unsigned long long get_msg_length() { return m_msg_len; } 194 195 /** 196 Encodes the contents of this instance into the buffer. 197 198 @param[out] buffer the buffer to encode to. 199 */ 200 void encode(std::vector<unsigned char>* buffer) const; 201 202 /** 203 Decodes the contents of the buffer and sets the field values 204 according to the values decoded. 205 206 @param[in] buffer the buffer to decode from. 207 @param[in] length the length of the buffer. 208 */ 209 void decode(const unsigned char* buffer, uint64 length); 210 211 /** 212 Return the cargo type of a given message buffer, without decode 213 the complete message. 214 215 @param[in] buffer the buffer to decode from. 216 217 @return the cargo type of a given message buffer 218 */ 219 static enum_cargo_type get_cargo_type(const unsigned char* buffer); 220 221 /** 222 Return the raw data of the first payload item of a given message buffer, 223 without decode the complete message. 224 225 @param[out] buffer the buffer to decode from. 226 @param[out] payload_item_data the data. 227 @param[out] payload_item_length the length of the data. 228 229 @return the raw data of the first payload item 230 */ 231 static void get_first_payload_item_raw_data(const unsigned char* buffer, 232 const unsigned char** payload_item_data, 233 uint64* payload_item_length); 234 235 protected: 236 /** 237 Plugin_gcs_message constructor. Only to be called by derivative classes 238 239 @param[in] cargo_type Message type to be sent 240 */ 241 explicit Plugin_gcs_message(enum_cargo_type cargo_type); 242 243 /** 244 Encodes the contents of this instance payload into the buffer. 245 246 @param[out] buffer the buffer to encode to. 247 */ 248 virtual void encode_payload(std::vector<unsigned char>* buffer) const = 0; 249 250 /** 251 Decodes the contents of the buffer and sets the payload field 252 values according to the values decoded. 253 254 @param[in] buffer the buffer to decode from. 255 @param[in] end the end of the buffer. 256 */ 257 virtual void decode_payload(const unsigned char* buffer, 258 const unsigned char* end)= 0; 259 260 /** 261 Encodes the given payload item type and length into the buffer. 262 263 @param[out] buffer the buffer to encode to 264 @param[in] payload_item_type the type of the payload item 265 @param[in] payload_item_length the length of the payload item 266 */ 267 void encode_payload_item_type_and_length(std::vector<unsigned char>* buffer, 268 uint16 payload_item_type, 269 unsigned long long payload_item_length) 270 const; 271 272 /** 273 Decodes the given payload item type and length from the buffer. 274 275 @param[in] buffer the buffer to encode from 276 @param[out] payload_item_type the type of the payload item 277 @param[out] payload_item_length the length of the payload item 278 */ 279 void decode_payload_item_type_and_length(const unsigned char** buffer, 280 uint16* payload_item_type, 281 unsigned long long* payload_item_length); 282 283 /** 284 Encodes the given payload item (type, length and value) into the buffer as 285 a char (1 byte). 286 287 @param[out] buffer the buffer to encode to 288 @param[in] type the type of the payload item 289 @param[in] value the value of the payload item 290 */ 291 void encode_payload_item_char(std::vector<unsigned char>* buffer, 292 uint16 type, 293 unsigned char value) 294 const; 295 296 /** 297 Decodes the given payload item (type, length and value) from the buffer as 298 a char (1 byte). 299 300 @param[in] buffer the buffer to encode from 301 @param[out] type the type of the payload item 302 @param[out] value the value of the payload item 303 */ 304 void decode_payload_item_char(const unsigned char** buffer, 305 uint16* type, 306 unsigned char* value); 307 308 /** 309 Encodes the given payload item (type, length and value) into the buffer as 310 a 2 bytes integer. 311 312 @param[out] buffer the buffer to encode to 313 @param[in] type the type of the payload item 314 @param[in] value the value of the payload item 315 */ 316 void encode_payload_item_int2(std::vector<unsigned char>* buffer, 317 uint16 type, 318 uint16 value) 319 const; 320 321 /** 322 Decodes the given payload item (type, length and value) from the buffer as 323 a 2 bytes integer. 324 325 @param[in] buffer the buffer to encode from 326 @param[out] type the type of the payload item 327 @param[out] value the value of the payload item 328 */ 329 void decode_payload_item_int2(const unsigned char** buffer, 330 uint16* type, 331 uint16* value); 332 333 /** 334 Encodes the given payload item (type, length and value) into the buffer as 335 a 4 bytes integer. 336 337 @param[out] buffer the buffer to encode to 338 @param[in] type the type of the payload item 339 @param[in] value the value of the payload item 340 */ 341 void encode_payload_item_int4(std::vector<unsigned char>* buffer, 342 uint16 type, 343 uint32 value) 344 const; 345 346 /** 347 Decodes the given payload item (type, length and value) from the buffer as 348 a 4 bytes integer. 349 350 @param[in] buffer the buffer to encode from 351 @param[out] type the type of the payload item 352 @param[out] value the value of the payload item 353 */ 354 void decode_payload_item_int4(const unsigned char** buffer, 355 uint16* type, 356 uint32* value); 357 358 /** 359 Encodes the given payload item (type, length and value) into the buffer as 360 a 8 bytes integer. 361 362 @param[out] buffer the buffer to encode to 363 @param[in] type the type of the payload item 364 @param[in] value the value of the payload item 365 */ 366 void encode_payload_item_int8(std::vector<unsigned char>* buffer, 367 uint16 type, 368 ulonglong value) 369 const; 370 371 /** 372 Decodes the given payload item (type, length and value) from the buffer as 373 a 8 bytes integer. 374 375 @param[in] buffer the buffer to encode from 376 @param[out] type the type of the payload item 377 @param[out] value the value of the payload item 378 */ 379 void decode_payload_item_int8(const unsigned char** buffer, 380 uint16* type, 381 ulonglong* value); 382 383 /** 384 Encodes the given payload item (type, length and value) into the buffer as 385 a char array (variable size). 386 387 @param[out] buffer the buffer to encode to 388 @param[in] type the type of the payload item 389 @param[in] value the value of the payload item 390 @param[in] length the length of the payload item 391 */ 392 void encode_payload_item_string(std::vector<unsigned char>* buffer, 393 uint16 type, 394 const char* value, 395 unsigned long long length) 396 const; 397 398 /** 399 Decodes the given payload item (type, length and value) from the buffer as 400 a char array (variable size). 401 402 @param[in] buffer the buffer to encode from 403 @param[out] type the type of the payload item 404 @param[out] value the value of the payload item 405 @param[out] length the length of the payload item 406 */ 407 void decode_payload_item_string(const unsigned char** buffer, 408 uint16* type, 409 std::string* value, 410 unsigned long long* length); 411 }; 412 413 #endif /* GCS_PLUGIN_MESSAGES_INCLUDED */ 414