xref: /memcached-1.4.29/protocol_binary.h (revision a85a6e15)
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_command;
95 
96   /**
97    * Definition of the data types in the packet
98    * See section 3.4 Data Types
99    */
100   typedef enum {
101     PROTOCOL_BINARY_RAW_BYTES = 0x00,
102   } protocol_binary_datatypes;
103 
104   /**
105    * Definition of the header structure for a request packet.
106    * See section 2
107    */
108   typedef union {
109     struct {
110       uint8_t magic;
111       uint8_t opcode;
112       uint16_t keylen;
113       uint8_t extlen;
114       uint8_t datatype;
115       uint16_t reserved;
116       uint32_t bodylen;
117       uint32_t opaque;
118       uint64_t cas;
119     } request;
120     uint8_t bytes[24];
121   } protocol_binary_request_header;
122 
123   /**
124    * Definition of the header structure for a response packet.
125    * See section 2
126    */
127   typedef union {
128     struct {
129       uint8_t magic;
130       uint8_t opcode;
131       uint16_t keylen;
132       uint8_t extlen;
133       uint8_t datatype;
134       uint16_t status;
135       uint32_t bodylen;
136       uint32_t opaque;
137       uint64_t cas;
138     } response;
139     uint8_t bytes[24];
140   } protocol_binary_response_header;
141 
142   /**
143    * Definition of a request-packet containing no extras
144    */
145   typedef union {
146     struct {
147       protocol_binary_request_header header;
148     } message;
149     uint8_t bytes[sizeof(protocol_binary_request_header)];
150   } protocol_binary_request_no_extras;
151 
152   /**
153    * Definition of a response-packet containing no extras
154    */
155   typedef union {
156     struct {
157       protocol_binary_response_header header;
158     } message;
159     uint8_t bytes[sizeof(protocol_binary_response_header)];
160   } protocol_binary_response_no_extras;
161 
162   /**
163    * Definition of the packet used by the get, getq, getk and getkq command.
164    * See section 4.1
165    */
166   typedef protocol_binary_request_no_extras protocol_binary_request_get;
167   typedef protocol_binary_request_no_extras protocol_binary_request_getq;
168   typedef protocol_binary_request_no_extras protocol_binary_request_getk;
169   typedef protocol_binary_request_no_extras protocol_binary_request_getkq;
170 
171   /**
172    * Definition of the packet returned from a successful get, getq, getk and
173    * getkq.
174    * See section 4.1
175    */
176   typedef union {
177     struct {
178       protocol_binary_response_header header;
179       struct {
180     uint32_t flags;
181       } body;
182     } message;
183     uint8_t bytes[sizeof(protocol_binary_response_header) + 4];
184   } protocol_binary_response_get;
185 
186   typedef protocol_binary_response_get protocol_binary_response_getq;
187   typedef protocol_binary_response_get protocol_binary_response_getk;
188   typedef protocol_binary_response_get protocol_binary_response_getkq;
189 
190   /**
191    * Definition of the packet used by the delete command
192    * See section 4.2
193    * Please note that the expiration field is optional, so remember to see
194    * check the header.bodysize to see if it is present.
195    */
196   typedef union {
197     struct {
198       protocol_binary_request_header header;
199       struct {
200     uint32_t expiration;
201       } body;
202     } message;
203     uint8_t bytes[sizeof(protocol_binary_request_header) + 4];
204   } protocol_binary_request_delete;
205 
206   /**
207    * Definition of the packet returned by the delete command
208    * See section 4.2
209    */
210   typedef protocol_binary_response_no_extras protocol_binary_response_delete;
211 
212   /**
213    * Definition of the packet used by the flush command
214    * See section 4.3
215    * Please note that the expiration field is optional, so remember to see
216    * check the header.bodysize to see if it is present.
217    */
218   typedef union {
219     struct {
220       protocol_binary_request_header header;
221       struct {
222     uint32_t expiration;
223       } body;
224     } message;
225     uint8_t bytes[sizeof(protocol_binary_request_header) + 4];
226   } protocol_binary_request_flush;
227 
228   /**
229    * Definition of the packet returned by the flush command
230    * See section 4.3
231    */
232   typedef protocol_binary_response_no_extras protocol_binary_response_flush;
233 
234   /**
235    * Definition of the packet used by set, add and replace
236    * See section 4.4
237    */
238   typedef union {
239     struct {
240       protocol_binary_request_header header;
241       struct {
242     uint32_t flags;
243     uint32_t expiration;
244       } body;
245     } message;
246     uint8_t bytes[sizeof(protocol_binary_request_header) + 8];
247   } protocol_binary_request_set;
248   typedef protocol_binary_request_set protocol_binary_request_add;
249   typedef protocol_binary_request_set protocol_binary_request_replace;
250 
251   /**
252    * Definition of the packet returned by set, add and replace
253    * See section 4.4
254    */
255   typedef protocol_binary_response_no_extras protocol_binary_response_set;
256   typedef protocol_binary_response_no_extras protocol_binary_response_add;
257   typedef protocol_binary_response_no_extras protocol_binary_response_replace;
258 
259   /**
260    * Definition of the noop packet
261    * See section 4.5
262    */
263   typedef protocol_binary_request_no_extras protocol_binary_request_noop;
264 
265   /**
266    * Definition of the packet returned by the noop command
267    * See section 4.5
268    */
269   typedef protocol_binary_response_no_extras protocol_binary_response_nnoop;
270 
271   /**
272    * Definition of the structure used by the increment and decrement
273    * command.
274    * See section 4.6
275    */
276   typedef union {
277     struct {
278       protocol_binary_request_header header;
279       struct {
280     uint64_t delta;
281     uint64_t initial;
282     uint32_t expiration;
283       } body;
284     } message;
285     uint8_t bytes[sizeof(protocol_binary_request_header) + 20];
286   } protocol_binary_request_incr;
287   typedef protocol_binary_request_incr protocol_binary_request_decr;
288 
289   /**
290    * Definition of the response from an incr or decr command
291    * command.
292    * See section 4.6
293    */
294   typedef union {
295     struct {
296       protocol_binary_response_header header;
297       struct {
298     uint64_t value;
299       } body;
300     } message;
301     uint8_t bytes[sizeof(protocol_binary_response_header) + 8];
302   } protocol_binary_response_incr;
303   typedef protocol_binary_response_incr protocol_binary_response_decr;
304 
305   /**
306    * Definition of the quit
307    * See section 4.7
308    */
309   typedef protocol_binary_request_no_extras protocol_binary_request_quit;
310 
311   /**
312    * Definition of the packet returned by the quit command
313    * See section 4.7
314    */
315   typedef protocol_binary_response_no_extras protocol_binary_response_quit;
316 
317   /**
318    * Definition of the packet used by append and prepend command
319    * See section 4.8
320    */
321   typedef protocol_binary_request_no_extras protocol_binary_request_append;
322   typedef protocol_binary_request_no_extras protocol_binary_request_prepend;
323 
324   /**
325    * Definition of the packet returned from a successful append or prepend
326    * See section 4.8
327    */
328   typedef protocol_binary_response_no_extras protocol_binary_response_append;
329   typedef protocol_binary_response_no_extras protocol_binary_response_prepend;
330 
331 #ifdef __cplusplus
332 }
333 #endif
334 #endif /* PROTOCOL_BINARY_H */
335