1 /* 2 * Copyright (c) <2008>, Sun Microsystems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of the nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 /* 28 * Summary: Constants used by to implement the binary protocol. 29 * 30 * Copy: See Copyright for the status of this software. 31 * 32 * Author: Trond Norbye <[email protected]> 33 */ 34 35 #ifndef PROTOCOL_BINARY_H 36 #define PROTOCOL_BINARY_H 37 38 #include <stdint.h> 39 40 /** 41 * This file contains definitions of the constants and packet formats 42 * defined in the binary specification. Please note that you _MUST_ remember 43 * to convert each multibyte field to / from network byte order to / from 44 * host order. 45 */ 46 #ifdef __cplusplus 47 extern "C" 48 { 49 #endif 50 51 /** 52 * Definition of the legal "magic" values used in a packet. 53 * See section 3.1 Magic byte 54 */ 55 typedef enum { 56 PROTOCOL_BINARY_REQ = 0x80, 57 PROTOCOL_BINARY_RES = 0x81 58 } protocol_binary_magic; 59 60 /** 61 * Definition of the valid response status numbers. 62 * See section 3.2 Response Status 63 */ 64 typedef enum { 65 PROTOCOL_BINARY_RESPONSE_SUCCESS = 0x00, 66 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT = 0x01, 67 PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS = 0x02, 68 PROTOCOL_BINARY_RESPONSE_E2BIG = 0x03, 69 PROTOCOL_BINARY_RESPONSE_EINVAL = 0x04, 70 PROTOCOL_BINARY_RESPONSE_NOT_STORED = 0x05, 71 PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND = 0x81, 72 PROTOCOL_BINARY_RESPONSE_ENOMEM = 0x82 73 } protocol_binary_response_status; 74 75 /** 76 * Defintion of the different command opcodes. 77 * See section 3.3 Command Opcodes 78 */ 79 typedef enum { 80 PROTOCOL_BINARY_CMD_GET = 0x00, 81 PROTOCOL_BINARY_CMD_SET = 0x01, 82 PROTOCOL_BINARY_CMD_ADD = 0x02, 83 PROTOCOL_BINARY_CMD_REPLACE = 0x03, 84 PROTOCOL_BINARY_CMD_DELETE = 0x04, 85 PROTOCOL_BINARY_CMD_INCREMENT = 0x05, 86 PROTOCOL_BINARY_CMD_DECREMENT = 0x06, 87 PROTOCOL_BINARY_CMD_QUIT = 0x07, 88 PROTOCOL_BINARY_CMD_FLUSH = 0x08, 89 PROTOCOL_BINARY_CMD_GETQ = 0x09, 90 PROTOCOL_BINARY_CMD_NOOP = 0x0a, 91 PROTOCOL_BINARY_CMD_VERSION = 0x0b, 92 PROTOCOL_BINARY_CMD_GETK = 0x0c, 93 PROTOCOL_BINARY_CMD_GETKQ = 0x0d, 94 PROTOCOL_BINARY_CMD_APPEND = 0x0e, 95 PROTOCOL_BINARY_CMD_PREPEND = 0x0f, 96 PROTOCOL_BINARY_CMD_STAT = 0x10 97 } protocol_binary_command; 98 99 /** 100 * Definition of the data types in the packet 101 * See section 3.4 Data Types 102 */ 103 typedef enum { 104 PROTOCOL_BINARY_RAW_BYTES = 0x00 105 } protocol_binary_datatypes; 106 107 /** 108 * Definition of the header structure for a request packet. 109 * See section 2 110 */ 111 typedef union { 112 struct { 113 uint8_t magic; 114 uint8_t opcode; 115 uint16_t keylen; 116 uint8_t extlen; 117 uint8_t datatype; 118 uint16_t reserved; 119 uint32_t bodylen; 120 uint32_t opaque; 121 uint64_t cas; 122 } request; 123 uint8_t bytes[24]; 124 } protocol_binary_request_header; 125 126 /** 127 * Definition of the header structure for a response packet. 128 * See section 2 129 */ 130 typedef union { 131 struct { 132 uint8_t magic; 133 uint8_t opcode; 134 uint16_t keylen; 135 uint8_t extlen; 136 uint8_t datatype; 137 uint16_t status; 138 uint32_t bodylen; 139 uint32_t opaque; 140 uint64_t cas; 141 } response; 142 uint8_t bytes[24]; 143 } protocol_binary_response_header; 144 145 /** 146 * Definition of a request-packet containing no extras 147 */ 148 typedef union { 149 struct { 150 protocol_binary_request_header header; 151 } message; 152 uint8_t bytes[sizeof(protocol_binary_request_header)]; 153 } protocol_binary_request_no_extras; 154 155 /** 156 * Definition of a response-packet containing no extras 157 */ 158 typedef union { 159 struct { 160 protocol_binary_response_header header; 161 } message; 162 uint8_t bytes[sizeof(protocol_binary_response_header)]; 163 } protocol_binary_response_no_extras; 164 165 /** 166 * Definition of the packet used by the get, getq, getk and getkq command. 167 * See section 4 168 */ 169 typedef protocol_binary_request_no_extras protocol_binary_request_get; 170 typedef protocol_binary_request_no_extras protocol_binary_request_getq; 171 typedef protocol_binary_request_no_extras protocol_binary_request_getk; 172 typedef protocol_binary_request_no_extras protocol_binary_request_getkq; 173 174 /** 175 * Definition of the packet returned from a successful get, getq, getk and 176 * getkq. 177 * See section 4 178 */ 179 typedef union { 180 struct { 181 protocol_binary_response_header header; 182 struct { 183 uint32_t flags; 184 } body; 185 } message; 186 uint8_t bytes[sizeof(protocol_binary_response_header) + 4]; 187 } protocol_binary_response_get; 188 189 typedef protocol_binary_response_get protocol_binary_response_getq; 190 typedef protocol_binary_response_get protocol_binary_response_getk; 191 typedef protocol_binary_response_get protocol_binary_response_getkq; 192 193 /** 194 * Definition of the packet used by the delete command 195 * See section 4 196 */ 197 typedef protocol_binary_request_no_extras protocol_binary_request_delete; 198 199 /** 200 * Definition of the packet returned by the delete command 201 * See section 4 202 */ 203 typedef protocol_binary_response_no_extras protocol_binary_response_delete; 204 205 /** 206 * Definition of the packet used by the flush command 207 * See section 4 208 * Please note that the expiration field is optional, so remember to see 209 * check the header.bodysize to see if it is present. 210 */ 211 typedef union { 212 struct { 213 protocol_binary_request_header header; 214 struct { 215 uint32_t expiration; 216 } body; 217 } message; 218 uint8_t bytes[sizeof(protocol_binary_request_header) + 4]; 219 } protocol_binary_request_flush; 220 221 /** 222 * Definition of the packet returned by the flush command 223 * See section 4 224 */ 225 typedef protocol_binary_response_no_extras protocol_binary_response_flush; 226 227 /** 228 * Definition of the packet used by set, add and replace 229 * See section 4 230 */ 231 typedef union { 232 struct { 233 protocol_binary_request_header header; 234 struct { 235 uint32_t flags; 236 uint32_t expiration; 237 } body; 238 } message; 239 uint8_t bytes[sizeof(protocol_binary_request_header) + 8]; 240 } protocol_binary_request_set; 241 typedef protocol_binary_request_set protocol_binary_request_add; 242 typedef protocol_binary_request_set protocol_binary_request_replace; 243 244 /** 245 * Definition of the packet returned by set, add and replace 246 * See section 4 247 */ 248 typedef protocol_binary_response_no_extras protocol_binary_response_set; 249 typedef protocol_binary_response_no_extras protocol_binary_response_add; 250 typedef protocol_binary_response_no_extras protocol_binary_response_replace; 251 252 /** 253 * Definition of the noop packet 254 * See section 4 255 */ 256 typedef protocol_binary_request_no_extras protocol_binary_request_noop; 257 258 /** 259 * Definition of the packet returned by the noop command 260 * See section 4 261 */ 262 typedef protocol_binary_response_no_extras protocol_binary_response_nnoop; 263 264 /** 265 * Definition of the structure used by the increment and decrement 266 * command. 267 * See section 4 268 */ 269 typedef union { 270 struct { 271 protocol_binary_request_header header; 272 struct { 273 uint64_t delta; 274 uint64_t initial; 275 uint32_t expiration; 276 } body; 277 } message; 278 uint8_t bytes[sizeof(protocol_binary_request_header) + 20]; 279 } protocol_binary_request_incr; 280 typedef protocol_binary_request_incr protocol_binary_request_decr; 281 282 /** 283 * Definition of the response from an incr or decr command 284 * command. 285 * See section 4 286 */ 287 typedef union { 288 struct { 289 protocol_binary_response_header header; 290 struct { 291 uint64_t value; 292 } body; 293 } message; 294 uint8_t bytes[sizeof(protocol_binary_response_header) + 8]; 295 } protocol_binary_response_incr; 296 typedef protocol_binary_response_incr protocol_binary_response_decr; 297 298 /** 299 * Definition of the quit 300 * See section 4 301 */ 302 typedef protocol_binary_request_no_extras protocol_binary_request_quit; 303 304 /** 305 * Definition of the packet returned by the quit command 306 * See section 4 307 */ 308 typedef protocol_binary_response_no_extras protocol_binary_response_quit; 309 310 /** 311 * Definition of the packet used by append and prepend command 312 * See section 4 313 */ 314 typedef protocol_binary_request_no_extras protocol_binary_request_append; 315 typedef protocol_binary_request_no_extras protocol_binary_request_prepend; 316 317 /** 318 * Definition of the packet returned from a successful append or prepend 319 * See section 4 320 */ 321 typedef protocol_binary_response_no_extras protocol_binary_response_append; 322 typedef protocol_binary_response_no_extras protocol_binary_response_prepend; 323 324 /** 325 * Definition of the packet used by the version command 326 * See section 4 327 */ 328 typedef protocol_binary_request_no_extras protocol_binary_request_version; 329 330 /** 331 * Definition of the packet returned from a successful version command 332 * See section 4 333 */ 334 typedef protocol_binary_response_no_extras protocol_binary_response_version; 335 336 337 /** 338 * Definition of the packet used by the stats command. 339 * See section 4 340 */ 341 typedef protocol_binary_request_no_extras protocol_binary_request_stats; 342 343 /** 344 * Definition of the packet returned from a successful stats command 345 * See section 4 346 */ 347 typedef protocol_binary_response_no_extras protocol_binary_response_stats; 348 #ifdef __cplusplus 349 } 350 #endif 351 #endif /* PROTOCOL_BINARY_H */ 352