Correction for UDP code in ascii mode.
[awesomized/libmemcached] / libmemcachedprotocol / 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 <libmemcachedprotocol/common.h>
39
40 #include <sys/types.h>
41
42 #define ensure(a) if (!(a)) { return false; }
43
44 bool memcached_binary_protocol_pedantic_check_request(const protocol_binary_request_header *request)
45 {
46 ensure(request->request.magic == PROTOCOL_BINARY_REQ);
47 ensure(request->request.datatype == PROTOCOL_BINARY_RAW_BYTES);
48
49 ensure(request->bytes[6] == 0);
50 ensure(request->bytes[7] == 0);
51
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);
56
57 ensure(bodylen >= (keylen + extlen));
58
59 switch (opcode) {
60 case PROTOCOL_BINARY_CMD_GET:
61 case PROTOCOL_BINARY_CMD_GETK:
62 case PROTOCOL_BINARY_CMD_GETKQ:
63 case PROTOCOL_BINARY_CMD_GETQ:
64 ensure(extlen == 0);
65 ensure(keylen > 0);
66 ensure(keylen == bodylen);
67 ensure(request->request.cas == 0);
68 break;
69
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);
74 /* FALLTHROUGH */
75 case PROTOCOL_BINARY_CMD_SET:
76 case PROTOCOL_BINARY_CMD_SETQ:
77 case PROTOCOL_BINARY_CMD_REPLACE:
78 case PROTOCOL_BINARY_CMD_REPLACEQ:
79 ensure(keylen > 0);
80 ensure(extlen == 8);
81 break;
82
83 case PROTOCOL_BINARY_CMD_DELETE:
84 case PROTOCOL_BINARY_CMD_DELETEQ:
85 ensure(extlen == 0);
86 ensure(keylen > 0);
87 ensure(keylen == bodylen);
88 break;
89
90 case PROTOCOL_BINARY_CMD_INCREMENT:
91 case PROTOCOL_BINARY_CMD_INCREMENTQ:
92 case PROTOCOL_BINARY_CMD_DECREMENT:
93 case PROTOCOL_BINARY_CMD_DECREMENTQ:
94 ensure(extlen == 20);
95 ensure(keylen > 0);
96 ensure(keylen + extlen == bodylen);
97 break;
98
99 case PROTOCOL_BINARY_CMD_QUIT:
100 case PROTOCOL_BINARY_CMD_QUITQ:
101 case PROTOCOL_BINARY_CMD_NOOP:
102 case PROTOCOL_BINARY_CMD_VERSION:
103 ensure(extlen == 0);
104 ensure(keylen == 0);
105 ensure(bodylen == 0);
106 break;
107
108 case PROTOCOL_BINARY_CMD_FLUSH:
109 case PROTOCOL_BINARY_CMD_FLUSHQ:
110 ensure(extlen == 0 || extlen == 4);
111 ensure(keylen == 0);
112 ensure(bodylen == extlen);
113 break;
114
115 case PROTOCOL_BINARY_CMD_STAT:
116 ensure(extlen == 0);
117 /* May have key, but not value */
118 ensure(keylen == bodylen);
119 break;
120
121 case PROTOCOL_BINARY_CMD_APPEND:
122 case PROTOCOL_BINARY_CMD_APPENDQ:
123 case PROTOCOL_BINARY_CMD_PREPEND:
124 case PROTOCOL_BINARY_CMD_PREPENDQ:
125 ensure(extlen == 0);
126 ensure(keylen > 0);
127 break;
128 default:
129 /* Unknown command */
130 ;
131 }
132
133 return true;
134 }
135
136 bool memcached_binary_protocol_pedantic_check_response(const protocol_binary_request_header *request,
137 const protocol_binary_response_header *response)
138 {
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);
142
143 uint16_t status= ntohs(response->response.status);
144 uint8_t opcode= response->response.opcode;
145
146 if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS)
147 {
148 switch (opcode) {
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 */
160 return false;
161 default:
162 break;
163 }
164
165 switch (opcode) {
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);
175 break;
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);
184 break;
185
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);
192 break;
193
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);
198 break;
199
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);
205 break;
206
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);
212 break;
213
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);
219 break;
220
221 default:
222 /* Undefined command code */
223 break;
224 }
225 }
226 else
227 {
228 ensure(response->response.cas == 0);
229 ensure(response->response.extlen == 0);
230 if (opcode != PROTOCOL_BINARY_CMD_GETK)
231 {
232 ensure(response->response.keylen == 0);
233 }
234 }
235
236 return true;
237 }