1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
38 #include <libmemcached/protocol/common.h>
40 #include <sys/types.h>
43 #define ensure(a) if (!(a)) { return false; }
45 bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header
*request
)
47 ensure(request
->request
.magic
== PROTOCOL_BINARY_REQ
);
48 ensure(request
->request
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
50 ensure(request
->bytes
[6] == 0);
51 ensure(request
->bytes
[7] == 0);
53 uint8_t opcode
= request
->request
.opcode
;
54 uint16_t keylen
= ntohs(request
->request
.keylen
);
55 uint8_t extlen
= request
->request
.extlen
;
56 uint32_t bodylen
= ntohl(request
->request
.bodylen
);
58 ensure(bodylen
>= (keylen
+ extlen
));
61 case PROTOCOL_BINARY_CMD_GET
:
62 case PROTOCOL_BINARY_CMD_GETK
:
63 case PROTOCOL_BINARY_CMD_GETKQ
:
64 case PROTOCOL_BINARY_CMD_GETQ
:
67 ensure(keylen
== bodylen
);
68 ensure(request
->request
.cas
== 0);
71 case PROTOCOL_BINARY_CMD_ADD
:
72 case PROTOCOL_BINARY_CMD_ADDQ
:
73 /* it makes no sense to run add with a cas value */
74 ensure(request
->request
.cas
== 0);
76 case PROTOCOL_BINARY_CMD_SET
:
77 case PROTOCOL_BINARY_CMD_SETQ
:
78 case PROTOCOL_BINARY_CMD_REPLACE
:
79 case PROTOCOL_BINARY_CMD_REPLACEQ
:
84 case PROTOCOL_BINARY_CMD_DELETE
:
85 case PROTOCOL_BINARY_CMD_DELETEQ
:
88 ensure(keylen
== bodylen
);
91 case PROTOCOL_BINARY_CMD_INCREMENT
:
92 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
93 case PROTOCOL_BINARY_CMD_DECREMENT
:
94 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
97 ensure(keylen
+ extlen
== bodylen
);
100 case PROTOCOL_BINARY_CMD_QUIT
:
101 case PROTOCOL_BINARY_CMD_QUITQ
:
102 case PROTOCOL_BINARY_CMD_NOOP
:
103 case PROTOCOL_BINARY_CMD_VERSION
:
106 ensure(bodylen
== 0);
109 case PROTOCOL_BINARY_CMD_FLUSH
:
110 case PROTOCOL_BINARY_CMD_FLUSHQ
:
111 ensure(extlen
== 0 || extlen
== 4);
113 ensure(bodylen
== extlen
);
116 case PROTOCOL_BINARY_CMD_STAT
:
118 /* May have key, but not value */
119 ensure(keylen
== bodylen
);
122 case PROTOCOL_BINARY_CMD_APPEND
:
123 case PROTOCOL_BINARY_CMD_APPENDQ
:
124 case PROTOCOL_BINARY_CMD_PREPEND
:
125 case PROTOCOL_BINARY_CMD_PREPENDQ
:
130 /* Unknown command */
137 bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header
*request
,
138 const protocol_binary_response_header
*response
)
140 ensure(response
->response
.magic
== PROTOCOL_BINARY_RES
);
141 ensure(response
->response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
142 ensure(response
->response
.opaque
== request
->request
.opaque
);
144 uint16_t status
= ntohs(response
->response
.status
);
145 uint8_t opcode
= response
->response
.opcode
;
147 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
150 case PROTOCOL_BINARY_CMD_ADDQ
:
151 case PROTOCOL_BINARY_CMD_APPENDQ
:
152 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
153 case PROTOCOL_BINARY_CMD_DELETEQ
:
154 case PROTOCOL_BINARY_CMD_FLUSHQ
:
155 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
156 case PROTOCOL_BINARY_CMD_PREPENDQ
:
157 case PROTOCOL_BINARY_CMD_QUITQ
:
158 case PROTOCOL_BINARY_CMD_REPLACEQ
:
159 case PROTOCOL_BINARY_CMD_SETQ
:
160 /* Quiet command shouldn't return on success */
167 case PROTOCOL_BINARY_CMD_ADD
:
168 case PROTOCOL_BINARY_CMD_REPLACE
:
169 case PROTOCOL_BINARY_CMD_SET
:
170 case PROTOCOL_BINARY_CMD_APPEND
:
171 case PROTOCOL_BINARY_CMD_PREPEND
:
172 ensure(response
->response
.keylen
== 0);
173 ensure(response
->response
.extlen
== 0);
174 ensure(response
->response
.bodylen
== 0);
175 ensure(response
->response
.cas
!= 0);
177 case PROTOCOL_BINARY_CMD_FLUSH
:
178 case PROTOCOL_BINARY_CMD_NOOP
:
179 case PROTOCOL_BINARY_CMD_QUIT
:
180 case PROTOCOL_BINARY_CMD_DELETE
:
181 ensure(response
->response
.keylen
== 0);
182 ensure(response
->response
.extlen
== 0);
183 ensure(response
->response
.bodylen
== 0);
184 ensure(response
->response
.cas
== 0);
187 case PROTOCOL_BINARY_CMD_DECREMENT
:
188 case PROTOCOL_BINARY_CMD_INCREMENT
:
189 ensure(response
->response
.keylen
== 0);
190 ensure(response
->response
.extlen
== 0);
191 ensure(ntohl(response
->response
.bodylen
) == 8);
192 ensure(response
->response
.cas
!= 0);
195 case PROTOCOL_BINARY_CMD_STAT
:
196 ensure(response
->response
.extlen
== 0);
197 /* key and value exists in all packets except in the terminating */
198 ensure(response
->response
.cas
== 0);
201 case PROTOCOL_BINARY_CMD_VERSION
:
202 ensure(response
->response
.keylen
== 0);
203 ensure(response
->response
.extlen
== 0);
204 ensure(response
->response
.bodylen
!= 0);
205 ensure(response
->response
.cas
== 0);
208 case PROTOCOL_BINARY_CMD_GET
:
209 case PROTOCOL_BINARY_CMD_GETQ
:
210 ensure(response
->response
.keylen
== 0);
211 ensure(response
->response
.extlen
== 4);
212 ensure(response
->response
.cas
!= 0);
215 case PROTOCOL_BINARY_CMD_GETK
:
216 case PROTOCOL_BINARY_CMD_GETKQ
:
217 ensure(response
->response
.keylen
!= 0);
218 ensure(response
->response
.extlen
== 4);
219 ensure(response
->response
.cas
!= 0);
223 /* Undefined command code */
229 ensure(response
->response
.cas
== 0);
230 ensure(response
->response
.extlen
== 0);
231 if (opcode
!= PROTOCOL_BINARY_CMD_GETK
)
233 ensure(response
->response
.keylen
== 0);