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