Fix headers
[m6w6/libmemcached] / libmemcached / protocol / pedantic.c
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
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.
34 *
35 */
36
37 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
38 #include <libmemcached/protocol/common.h>
39
40 #include <sys/types.h>
41 #include <inttypes.h>
42
43 #define ensure(a) if (!(a)) { return false; }
44
45 bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header *request)
46 {
47 ensure(request->request.magic == PROTOCOL_BINARY_REQ);
48 ensure(request->request.datatype == PROTOCOL_BINARY_RAW_BYTES);
49
50 ensure(request->bytes[6] == 0);
51 ensure(request->bytes[7] == 0);
52
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);
57
58 ensure(bodylen >= (keylen + extlen));
59
60 switch (opcode) {
61 case PROTOCOL_BINARY_CMD_GET:
62 case PROTOCOL_BINARY_CMD_GETK:
63 case PROTOCOL_BINARY_CMD_GETKQ:
64 case PROTOCOL_BINARY_CMD_GETQ:
65 ensure(extlen == 0);
66 ensure(keylen > 0);
67 ensure(keylen == bodylen);
68 ensure(request->request.cas == 0);
69 break;
70
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);
75 /* FALLTHROUGH */
76 case PROTOCOL_BINARY_CMD_SET:
77 case PROTOCOL_BINARY_CMD_SETQ:
78 case PROTOCOL_BINARY_CMD_REPLACE:
79 case PROTOCOL_BINARY_CMD_REPLACEQ:
80 ensure(keylen > 0);
81 ensure(extlen == 8);
82 break;
83
84 case PROTOCOL_BINARY_CMD_DELETE:
85 case PROTOCOL_BINARY_CMD_DELETEQ:
86 ensure(extlen == 0);
87 ensure(keylen > 0);
88 ensure(keylen == bodylen);
89 break;
90
91 case PROTOCOL_BINARY_CMD_INCREMENT:
92 case PROTOCOL_BINARY_CMD_INCREMENTQ:
93 case PROTOCOL_BINARY_CMD_DECREMENT:
94 case PROTOCOL_BINARY_CMD_DECREMENTQ:
95 ensure(extlen == 20);
96 ensure(keylen > 0);
97 ensure(keylen + extlen == bodylen);
98 break;
99
100 case PROTOCOL_BINARY_CMD_QUIT:
101 case PROTOCOL_BINARY_CMD_QUITQ:
102 case PROTOCOL_BINARY_CMD_NOOP:
103 case PROTOCOL_BINARY_CMD_VERSION:
104 ensure(extlen == 0);
105 ensure(keylen == 0);
106 ensure(bodylen == 0);
107 break;
108
109 case PROTOCOL_BINARY_CMD_FLUSH:
110 case PROTOCOL_BINARY_CMD_FLUSHQ:
111 ensure(extlen == 0 || extlen == 4);
112 ensure(keylen == 0);
113 ensure(bodylen == extlen);
114 break;
115
116 case PROTOCOL_BINARY_CMD_STAT:
117 ensure(extlen == 0);
118 /* May have key, but not value */
119 ensure(keylen == bodylen);
120 break;
121
122 case PROTOCOL_BINARY_CMD_APPEND:
123 case PROTOCOL_BINARY_CMD_APPENDQ:
124 case PROTOCOL_BINARY_CMD_PREPEND:
125 case PROTOCOL_BINARY_CMD_PREPENDQ:
126 ensure(extlen == 0);
127 ensure(keylen > 0);
128 break;
129 default:
130 /* Unknown command */
131 ;
132 }
133
134 return true;
135 }
136
137 bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header *request,
138 const protocol_binary_response_header *response)
139 {
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);
143
144 uint16_t status= ntohs(response->response.status);
145 uint8_t opcode= response->response.opcode;
146
147 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
148 {
149 switch (opcode) {
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 */
161 return false;
162 default:
163 break;
164 }
165
166 switch (opcode) {
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);
176 break;
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);
185 break;
186
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);
193 break;
194
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);
199 break;
200
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);
206 break;
207
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);
213 break;
214
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);
220 break;
221
222 default:
223 /* Undefined command code */
224 break;
225 }
226 }
227 else
228 {
229 ensure(response->response.cas == 0);
230 ensure(response->response.extlen == 0);
231 if (opcode != PROTOCOL_BINARY_CMD_GETK)
232 {
233 ensure(response->response.keylen == 0);
234 }
235 }
236
237 return true;
238 }