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 /** 39 * This file contains definitions of the constants and packet formats 40 * defined in the binary specification. Please note that you _MUST_ remember 41 * to convert each multibyte field to / from network byte order to / from 42 * host order. 43 */ 44 #ifdef __cplusplus 45 extern "C" 46 { 47 #endif 48 49 /** 50 * Definition of the legal "magic" values used in a packet. 51 * See section 3.1 Magic byte 52 */ 53 typedef enum { 54 PROTOCOL_BINARY_REQ = 0x80, 55 PROTOCOL_BINARY_RES = 0x81, 56 } protocol_binary_magic; 57 58 /** 59 * Definition of the valid response status numbers. 60 * See section 3.2 Response Status 61 */ 62 typedef enum { 63 PROTOCOL_BINARY_RESPONSE_SUCCESS = 0x00, 64 PROTOCOL_BINARY_RESPONSE_KEY_ENOENT = 0x01, 65 PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS = 0x02, 66 PROTOCOL_BINARY_RESPONSE_E2BIG = 0x03, 67 PROTOCOL_BINARY_RESPONSE_EINVAL = 0x04, 68 PROTOCOL_BINARY_RESPONSE_NOT_STORED = 0x05, 69 PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND = 0x81, 70 PROTOCOL_BINARY_RESPONSE_ENOMEM = 0x82, 71 } protocol_binary_response_status; 72 73 /** 74 * Defintion of the different command opcodes. 75 * See section 3.3 Command Opcodes 76 */ 77 typedef enum { 78 PROTOCOL_BINARY_CMD_GET = 0x00, 79 PROTOCOL_BINARY_CMD_SET = 0x01, 80 PROTOCOL_BINARY_CMD_ADD = 0x02, 81 PROTOCOL_BINARY_CMD_REPLACE = 0x03, 82 PROTOCOL_BINARY_CMD_DELETE = 0x04, 83 PROTOCOL_BINARY_CMD_INCREMENT = 0x05, 84 PROTOCOL_BINARY_CMD_DECREMENT = 0x06, 85 PROTOCOL_BINARY_CMD_QUIT = 0x07, 86 PROTOCOL_BINARY_CMD_FLUSH = 0x08, 87 PROTOCOL_BINARY_CMD_GETQ = 0x09, 88 PROTOCOL_BINARY_CMD_NOOP = 0x0a, 89 PROTOCOL_BINARY_CMD_VERSION = 0x0b, 90 PROTOCOL_BINARY_CMD_GETK = 0x0c, 91 PROTOCOL_BINARY_CMD_GETKQ = 0x0d, 92 PROTOCOL_BINARY_CMD_APPEND = 0x0e, 93 PROTOCOL_BINARY_CMD_PREPEND = 0x0f, 94 PROTOCOL_BINARY_CMD_STAT = 0x10, 95 } protocol_binary_command; 96 97 /** 98 * Definition of the data types in the packet 99 * See section 3.4 Data Types 100 */ 101 typedef enum { 102 PROTOCOL_BINARY_RAW_BYTES = 0x00, 103 } protocol_binary_datatypes; 104 105 /** 106 * Definition of the header structure for a request packet. 107 * See section 2 108 */ 109 typedef union { 110 struct { 111 uint8_t magic; 112 uint8_t opcode; 113 uint16_t keylen; 114 uint8_t extlen; 115 uint8_t datatype; 116 uint16_t reserved; 117 uint32_t bodylen; 118 uint32_t opaque; 119 uint64_t cas; 120 } request; 121 uint8_t bytes[24]; 122 } protocol_binary_request_header; 123 124 /** 125 * Definition of the header structure for a response packet. 126 * See section 2 127 */ 128 typedef union { 129 struct { 130 uint8_t magic; 131 uint8_t opcode; 132 uint16_t keylen; 133 uint8_t extlen; 134 uint8_t datatype; 135 uint16_t status; 136 uint32_t bodylen; 137 uint32_t opaque; 138 uint64_t cas; 139 } response; 140 uint8_t bytes[24]; 141 } protocol_binary_response_header; 142 143 /** 144 * Definition of a request-packet containing no extras 145 */ 146 typedef union { 147 struct { 148 protocol_binary_request_header header; 149 } message; 150 uint8_t bytes[sizeof(protocol_binary_request_header)]; 151 } protocol_binary_request_no_extras; 152 153 /** 154 * Definition of a response-packet containing no extras 155 */ 156 typedef union { 157 struct { 158 protocol_binary_response_header header; 159 } message; 160 uint8_t bytes[sizeof(protocol_binary_response_header)]; 161 } protocol_binary_response_no_extras; 162 163 /** 164 * Definition of the packet used by the get, getq, getk and getkq command. 165 * See section 4.1 166 */ 167 typedef protocol_binary_request_no_extras protocol_binary_request_get; 168 typedef protocol_binary_request_no_extras protocol_binary_request_getq; 169 typedef protocol_binary_request_no_extras protocol_binary_request_getk; 170 typedef protocol_binary_request_no_extras protocol_binary_request_getkq; 171 172 /** 173 * Definition of the packet used by the stats command. 174 * See section <undecided yet> 175 */ 176 typedef protocol_binary_request_no_extras protocol_binary_request_stats; 177 178 /** 179 * Definition of the packet returned from a successful get, getq, getk and 180 * getkq. 181 * See section 4.1 182 */ 183 typedef union { 184 struct { 185 protocol_binary_response_header header; 186 struct { 187 uint32_t flags; 188 } body; 189 } message; 190 uint8_t bytes[sizeof(protocol_binary_response_header) + 4]; 191 } protocol_binary_response_get; 192 193 typedef protocol_binary_response_get protocol_binary_response_getq; 194 typedef protocol_binary_response_get protocol_binary_response_getk; 195 typedef protocol_binary_response_get protocol_binary_response_getkq; 196 197 /** 198 * Definition of the packet used by the delete command 199 * See section 4.2 200 * Please note that the expiration field is optional, so remember to see 201 * check the header.bodysize to see if it is present. 202 */ 203 typedef union { 204 struct { 205 protocol_binary_request_header header; 206 struct { 207 uint32_t expiration; 208 } body; 209 } message; 210 uint8_t bytes[sizeof(protocol_binary_request_header) + 4]; 211 } protocol_binary_request_delete; 212 213 /** 214 * Definition of the packet returned by the delete command 215 * See section 4.2 216 */ 217 typedef protocol_binary_response_no_extras protocol_binary_response_delete; 218 219 /** 220 * Definition of the packet used by the flush command 221 * See section 4.3 222 * Please note that the expiration field is optional, so remember to see 223 * check the header.bodysize to see if it is present. 224 */ 225 typedef union { 226 struct { 227 protocol_binary_request_header header; 228 struct { 229 uint32_t expiration; 230 } body; 231 } message; 232 uint8_t bytes[sizeof(protocol_binary_request_header) + 4]; 233 } protocol_binary_request_flush; 234 235 /** 236 * Definition of the packet returned by the flush command 237 * See section 4.3 238 */ 239 typedef protocol_binary_response_no_extras protocol_binary_response_flush; 240 241 /** 242 * Definition of the packet used by set, add and replace 243 * See section 4.4 244 */ 245 typedef union { 246 struct { 247 protocol_binary_request_header header; 248 struct { 249 uint32_t flags; 250 uint32_t expiration; 251 } body; 252 } message; 253 uint8_t bytes[sizeof(protocol_binary_request_header) + 8]; 254 } protocol_binary_request_set; 255 typedef protocol_binary_request_set protocol_binary_request_add; 256 typedef protocol_binary_request_set protocol_binary_request_replace; 257 258 /** 259 * Definition of the packet returned by set, add and replace 260 * See section 4.4 261 */ 262 typedef protocol_binary_response_no_extras protocol_binary_response_set; 263 typedef protocol_binary_response_no_extras protocol_binary_response_add; 264 typedef protocol_binary_response_no_extras protocol_binary_response_replace; 265 266 /** 267 * Definition of the noop packet 268 * See section 4.5 269 */ 270 typedef protocol_binary_request_no_extras protocol_binary_request_noop; 271 272 /** 273 * Definition of the packet returned by the noop command 274 * See section 4.5 275 */ 276 typedef protocol_binary_response_no_extras protocol_binary_response_nnoop; 277 278 /** 279 * Definition of the structure used by the increment and decrement 280 * command. 281 * See section 4.6 282 */ 283 typedef union { 284 struct { 285 protocol_binary_request_header header; 286 struct { 287 uint64_t delta; 288 uint64_t initial; 289 uint32_t expiration; 290 } body; 291 } message; 292 uint8_t bytes[sizeof(protocol_binary_request_header) + 20]; 293 } protocol_binary_request_incr; 294 typedef protocol_binary_request_incr protocol_binary_request_decr; 295 296 /** 297 * Definition of the response from an incr or decr command 298 * command. 299 * See section 4.6 300 */ 301 typedef union { 302 struct { 303 protocol_binary_response_header header; 304 struct { 305 uint64_t value; 306 } body; 307 } message; 308 uint8_t bytes[sizeof(protocol_binary_response_header) + 8]; 309 } protocol_binary_response_incr; 310 typedef protocol_binary_response_incr protocol_binary_response_decr; 311 312 /** 313 * Definition of the quit 314 * See section 4.7 315 */ 316 typedef protocol_binary_request_no_extras protocol_binary_request_quit; 317 318 /** 319 * Definition of the packet returned by the quit command 320 * See section 4.7 321 */ 322 typedef protocol_binary_response_no_extras protocol_binary_response_quit; 323 324 /** 325 * Definition of the packet used by append and prepend command 326 * See section 4.8 327 */ 328 typedef protocol_binary_request_no_extras protocol_binary_request_append; 329 typedef protocol_binary_request_no_extras protocol_binary_request_prepend; 330 331 /** 332 * Definition of the packet returned from a successful append or prepend 333 * See section 4.8 334 */ 335 typedef protocol_binary_response_no_extras protocol_binary_response_append; 336 typedef protocol_binary_response_no_extras protocol_binary_response_prepend; 337 338 #ifdef __cplusplus 339 } 340 #endif 341 #endif /* PROTOCOL_BINARY_H */ 342