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 <libmemcachedprotocol/common.h>
40 #include <sys/types.h>
42 #define ensure(a) if (!(a)) { return false; }
44 bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header
*request
)
46 ensure(request
->request
.magic
== PROTOCOL_BINARY_REQ
);
47 ensure(request
->request
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
49 ensure(request
->bytes
[6] == 0);
50 ensure(request
->bytes
[7] == 0);
52 uint8_t opcode
= request
->request
.opcode
;
53 uint16_t keylen
= ntohs(request
->request
.keylen
);
54 uint8_t extlen
= request
->request
.extlen
;
55 uint32_t bodylen
= ntohl(request
->request
.bodylen
);
57 ensure(bodylen
>= (keylen
+ extlen
));
60 case PROTOCOL_BINARY_CMD_GET
:
61 case PROTOCOL_BINARY_CMD_GETK
:
62 case PROTOCOL_BINARY_CMD_GETKQ
:
63 case PROTOCOL_BINARY_CMD_GETQ
:
66 ensure(keylen
== bodylen
);
67 ensure(request
->request
.cas
== 0);
70 case PROTOCOL_BINARY_CMD_ADD
:
71 case PROTOCOL_BINARY_CMD_ADDQ
:
72 /* it makes no sense to run add with a cas value */
73 ensure(request
->request
.cas
== 0);
75 case PROTOCOL_BINARY_CMD_SET
:
76 case PROTOCOL_BINARY_CMD_SETQ
:
77 case PROTOCOL_BINARY_CMD_REPLACE
:
78 case PROTOCOL_BINARY_CMD_REPLACEQ
:
83 case PROTOCOL_BINARY_CMD_DELETE
:
84 case PROTOCOL_BINARY_CMD_DELETEQ
:
87 ensure(keylen
== bodylen
);
90 case PROTOCOL_BINARY_CMD_INCREMENT
:
91 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
92 case PROTOCOL_BINARY_CMD_DECREMENT
:
93 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
96 ensure(keylen
+ extlen
== bodylen
);
99 case PROTOCOL_BINARY_CMD_QUIT
:
100 case PROTOCOL_BINARY_CMD_QUITQ
:
101 case PROTOCOL_BINARY_CMD_NOOP
:
102 case PROTOCOL_BINARY_CMD_VERSION
:
105 ensure(bodylen
== 0);
108 case PROTOCOL_BINARY_CMD_FLUSH
:
109 case PROTOCOL_BINARY_CMD_FLUSHQ
:
110 ensure(extlen
== 0 || extlen
== 4);
112 ensure(bodylen
== extlen
);
115 case PROTOCOL_BINARY_CMD_STAT
:
117 /* May have key, but not value */
118 ensure(keylen
== bodylen
);
121 case PROTOCOL_BINARY_CMD_APPEND
:
122 case PROTOCOL_BINARY_CMD_APPENDQ
:
123 case PROTOCOL_BINARY_CMD_PREPEND
:
124 case PROTOCOL_BINARY_CMD_PREPENDQ
:
129 /* Unknown command */
136 bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header
*request
,
137 const protocol_binary_response_header
*response
)
139 ensure(response
->response
.magic
== PROTOCOL_BINARY_RES
);
140 ensure(response
->response
.datatype
== PROTOCOL_BINARY_RAW_BYTES
);
141 ensure(response
->response
.opaque
== request
->request
.opaque
);
143 uint16_t status
= ntohs(response
->response
.status
);
144 uint8_t opcode
= response
->response
.opcode
;
146 if (status
== PROTOCOL_BINARY_RESPONSE_SUCCESS
)
149 case PROTOCOL_BINARY_CMD_ADDQ
:
150 case PROTOCOL_BINARY_CMD_APPENDQ
:
151 case PROTOCOL_BINARY_CMD_DECREMENTQ
:
152 case PROTOCOL_BINARY_CMD_DELETEQ
:
153 case PROTOCOL_BINARY_CMD_FLUSHQ
:
154 case PROTOCOL_BINARY_CMD_INCREMENTQ
:
155 case PROTOCOL_BINARY_CMD_PREPENDQ
:
156 case PROTOCOL_BINARY_CMD_QUITQ
:
157 case PROTOCOL_BINARY_CMD_REPLACEQ
:
158 case PROTOCOL_BINARY_CMD_SETQ
:
159 /* Quiet command shouldn't return on success */
166 case PROTOCOL_BINARY_CMD_ADD
:
167 case PROTOCOL_BINARY_CMD_REPLACE
:
168 case PROTOCOL_BINARY_CMD_SET
:
169 case PROTOCOL_BINARY_CMD_APPEND
:
170 case PROTOCOL_BINARY_CMD_PREPEND
:
171 ensure(response
->response
.keylen
== 0);
172 ensure(response
->response
.extlen
== 0);
173 ensure(response
->response
.bodylen
== 0);
174 ensure(response
->response
.cas
!= 0);
176 case PROTOCOL_BINARY_CMD_FLUSH
:
177 case PROTOCOL_BINARY_CMD_NOOP
:
178 case PROTOCOL_BINARY_CMD_QUIT
:
179 case PROTOCOL_BINARY_CMD_DELETE
:
180 ensure(response
->response
.keylen
== 0);
181 ensure(response
->response
.extlen
== 0);
182 ensure(response
->response
.bodylen
== 0);
183 ensure(response
->response
.cas
== 0);
186 case PROTOCOL_BINARY_CMD_DECREMENT
:
187 case PROTOCOL_BINARY_CMD_INCREMENT
:
188 ensure(response
->response
.keylen
== 0);
189 ensure(response
->response
.extlen
== 0);
190 ensure(ntohl(response
->response
.bodylen
) == 8);
191 ensure(response
->response
.cas
!= 0);
194 case PROTOCOL_BINARY_CMD_STAT
:
195 ensure(response
->response
.extlen
== 0);
196 /* key and value exists in all packets except in the terminating */
197 ensure(response
->response
.cas
== 0);
200 case PROTOCOL_BINARY_CMD_VERSION
:
201 ensure(response
->response
.keylen
== 0);
202 ensure(response
->response
.extlen
== 0);
203 ensure(response
->response
.bodylen
!= 0);
204 ensure(response
->response
.cas
== 0);
207 case PROTOCOL_BINARY_CMD_GET
:
208 case PROTOCOL_BINARY_CMD_GETQ
:
209 ensure(response
->response
.keylen
== 0);
210 ensure(response
->response
.extlen
== 4);
211 ensure(response
->response
.cas
!= 0);
214 case PROTOCOL_BINARY_CMD_GETK
:
215 case PROTOCOL_BINARY_CMD_GETKQ
:
216 ensure(response
->response
.keylen
!= 0);
217 ensure(response
->response
.extlen
== 4);
218 ensure(response
->response
.cas
!= 0);
222 /* Undefined command code */
228 ensure(response
->response
.cas
== 0);
229 ensure(response
->response
.extlen
== 0);
230 if (opcode
!= PROTOCOL_BINARY_CMD_GETK
)
232 ensure(response
->response
.keylen
== 0);