]>
The Tcpdump Group git mirrors - tcpdump/blob - print-resp.c
2 * Copyright (c) 2015 The TCPDUMP project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 * Initial contribution by Andrew Darqui (andrew.darqui@gmail.com).
30 /* \summary: REdis Serialization Protocol (RESP) printer */
36 #include "netdissect-stdinc.h"
37 #include "netdissect.h"
44 * For information regarding RESP, see: https://redis.io/topics/protocol
47 #define RESP_SIMPLE_STRING '+'
48 #define RESP_ERROR '-'
49 #define RESP_INTEGER ':'
50 #define RESP_BULK_STRING '$'
51 #define RESP_ARRAY '*'
53 #define resp_print_empty(ndo) ND_PRINT(" empty")
54 #define resp_print_null(ndo) ND_PRINT(" null")
55 #define resp_print_length_too_large(ndo) ND_PRINT(" length too large")
56 #define resp_print_length_negative(ndo) ND_PRINT(" length negative and not -1")
57 #define resp_print_invalid(ndo) ND_PRINT(" invalid")
59 static int resp_parse(netdissect_options
*, const u_char
*, int);
60 static int resp_print_string_error_integer(netdissect_options
*, const u_char
*, int);
61 static int resp_print_simple_string(netdissect_options
*, const u_char
*, int);
62 static int resp_print_integer(netdissect_options
*, const u_char
*, int);
63 static int resp_print_error(netdissect_options
*, const u_char
*, int);
64 static int resp_print_bulk_string(netdissect_options
*, const u_char
*, int);
65 static int resp_print_bulk_array(netdissect_options
*, const u_char
*, int);
66 static int resp_print_inline(netdissect_options
*, const u_char
*, int);
67 static int resp_get_length(netdissect_options
*, const u_char
*, int, const u_char
**);
69 #define LCHECK2(_tot_len, _len) \
71 if (_tot_len < _len) \
75 #define LCHECK(_tot_len) LCHECK2(_tot_len, 1)
79 * Attempts to move our 'ptr' forward until a \r\n is found,
80 * while also making sure we don't exceed the buffer '_len'
81 * or go past the end of the captured data.
82 * If we exceed or go past the end of the captured data,
85 #define FIND_CRLF(_ptr, _len) \
89 if (GET_U_1(_ptr) == '\r' && \
90 GET_U_1(_ptr+1) == '\n') \
98 * Consume a CRLF that we've just found.
100 #define CONSUME_CRLF(_ptr, _len) \
106 * Attempts to move our '_ptr' forward until a \r or \n is found,
107 * while also making sure we don't exceed the buffer '_len'
108 * or go past the end of the captured data.
109 * If we exceed or go past the end of the captured data,
112 #define FIND_CR_OR_LF(_ptr, _len) \
115 if (GET_U_1(_ptr) == '\r' || \
116 GET_U_1(_ptr) == '\n') \
124 * Consume all consecutive \r and \n bytes.
125 * If we exceed '_len' or go past the end of the captured data,
128 #define CONSUME_CR_OR_LF(_ptr, _len) \
130 int _found_cr_or_lf = 0; \
133 * Have we hit the end of data? \
135 if (_len == 0 || !ND_TTEST_1(_ptr)) {\
137 * Yes. Have we seen a \r \
140 if (_found_cr_or_lf) { \
147 * No. We ran out of packet. \
151 if (GET_U_1(_ptr) != '\r' && \
152 GET_U_1(_ptr) != '\n') \
154 _found_cr_or_lf = 1; \
162 * Skip over the opcode character.
163 * The opcode has already been fetched, so we know it's there, and don't
164 * need to do any checks.
166 #define SKIP_OPCODE(_ptr, _tot_len) \
172 * Get a bulk string or array length.
174 #define GET_LENGTH(_ndo, _tot_len, _ptr, _len) \
176 const u_char *_endp; \
177 _len = resp_get_length(_ndo, _ptr, _tot_len, &_endp); \
178 _tot_len -= (_endp - _ptr); \
184 * If ret_len is < 0, jump to the trunc tag which returns (-1)
185 * and 'bubbles up' to printing tstr. Otherwise, return ret_len.
187 #define TEST_RET_LEN(rl) \
188 if (rl < 0) { goto trunc; } else { return rl; }
191 * TEST_RET_LEN_NORETURN
192 * If ret_len is < 0, jump to the trunc tag which returns (-1)
193 * and 'bubbles up' to printing tstr. Otherwise, continue onward.
195 #define TEST_RET_LEN_NORETURN(rl) \
196 if (rl < 0) { goto trunc; }
200 * Prints a segment in the form of: ' "<stuff>"\n"
201 * Assumes the data has already been verified as present.
203 #define RESP_PRINT_SEGMENT(_ndo, _bp, _len) \
205 if (nd_printn(_ndo, _bp, _len, _ndo->ndo_snapend)) \
207 fn_print_char(_ndo, '"');
210 resp_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
212 int ret_len
= 0, length_cur
= length
;
214 ndo
->ndo_protocol
= "resp";
215 if(!bp
|| length
== 0)
219 while (length_cur
> 0) {
221 * This block supports redis pipelining.
222 * For example, multiple operations can be pipelined within the same string:
223 * "*2\r\n\$4\r\nINCR\r\n\$1\r\nz\r\n*2\r\n\$4\r\nINCR\r\n\$1\r\nz\r\n*2\r\n\$4\r\nINCR\r\n\$1\r\nz\r\n"
225 * "PING\r\nPING\r\nPING\r\n"
226 * In order to handle this case, we must try and parse 'bp' until
227 * 'length' bytes have been processed or we reach a trunc condition.
229 ret_len
= resp_parse(ndo
, bp
, length_cur
);
230 TEST_RET_LEN_NORETURN(ret_len
);
232 length_cur
-= ret_len
;
242 resp_parse(netdissect_options
*ndo
, const u_char
*bp
, int length
)
250 /* bp now points to the op, so these routines must skip it */
252 case RESP_SIMPLE_STRING
: ret_len
= resp_print_simple_string(ndo
, bp
, length
); break;
253 case RESP_INTEGER
: ret_len
= resp_print_integer(ndo
, bp
, length
); break;
254 case RESP_ERROR
: ret_len
= resp_print_error(ndo
, bp
, length
); break;
255 case RESP_BULK_STRING
: ret_len
= resp_print_bulk_string(ndo
, bp
, length
); break;
256 case RESP_ARRAY
: ret_len
= resp_print_bulk_array(ndo
, bp
, length
); break;
257 default: ret_len
= resp_print_inline(ndo
, bp
, length
); break;
261 * This gives up with a "truncated" indicator for all errors,
262 * including invalid packet errors; that's what we want, as
263 * we have to give up on further parsing in that case.
265 TEST_RET_LEN(ret_len
);
272 resp_print_simple_string(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
273 return resp_print_string_error_integer(ndo
, bp
, length
);
277 resp_print_integer(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
278 return resp_print_string_error_integer(ndo
, bp
, length
);
282 resp_print_error(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
283 return resp_print_string_error_integer(ndo
, bp
, length
);
287 resp_print_string_error_integer(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
288 int length_cur
= length
, len
, ret_len
;
289 const u_char
*bp_ptr
;
291 /* bp points to the op; skip it */
292 SKIP_OPCODE(bp
, length_cur
);
296 * bp now prints past the (+-;) opcode, so it's pointing to the first
297 * character of the string (which could be numeric).
302 * Find the \r\n with FIND_CRLF().
304 FIND_CRLF(bp_ptr
, length_cur
);
307 * bp_ptr points to the \r\n, so bp_ptr - bp is the length of text
308 * preceding the \r\n. That includes the opcode, so don't print
311 len
= ND_BYTES_BETWEEN(bp_ptr
, bp
);
312 RESP_PRINT_SEGMENT(ndo
, bp
, len
);
313 ret_len
= 1 /*<opcode>*/ + len
/*<string>*/ + 2 /*<CRLF>*/;
315 TEST_RET_LEN(ret_len
);
322 resp_print_bulk_string(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
323 int length_cur
= length
, string_len
;
325 /* bp points to the op; skip it */
326 SKIP_OPCODE(bp
, length_cur
);
329 GET_LENGTH(ndo
, length_cur
, bp
, string_len
);
331 if (string_len
>= 0) {
332 /* Byte string of length string_len, starting at bp */
334 resp_print_empty(ndo
);
336 LCHECK2(length_cur
, string_len
);
337 ND_TCHECK_LEN(bp
, string_len
);
338 RESP_PRINT_SEGMENT(ndo
, bp
, string_len
);
340 length_cur
-= string_len
;
344 * Find the \r\n at the end of the string and skip past it.
345 * XXX - report an error if the \r\n isn't immediately after
348 FIND_CRLF(bp
, length_cur
);
349 CONSUME_CRLF(bp
, length_cur
);
351 /* null, truncated, or invalid for some reason */
353 case (-1): resp_print_null(ndo
); break;
354 case (-2): goto trunc
;
355 case (-3): resp_print_length_too_large(ndo
); break;
356 case (-4): resp_print_length_negative(ndo
); break;
357 default: resp_print_invalid(ndo
); break;
361 return (length
- length_cur
);
368 resp_print_bulk_array(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
369 u_int length_cur
= length
;
370 int array_len
, i
, ret_len
;
372 /* bp points to the op; skip it */
373 SKIP_OPCODE(bp
, length_cur
);
375 /* <array_length>\r\n */
376 GET_LENGTH(ndo
, length_cur
, bp
, array_len
);
379 /* non empty array */
380 for (i
= 0; i
< array_len
; i
++) {
381 ret_len
= resp_parse(ndo
, bp
, length_cur
);
383 TEST_RET_LEN_NORETURN(ret_len
);
386 length_cur
-= ret_len
;
389 /* empty, null, truncated, or invalid */
391 case 0: resp_print_empty(ndo
); break;
392 case (-1): resp_print_null(ndo
); break;
393 case (-2): goto trunc
;
394 case (-3): resp_print_length_too_large(ndo
); break;
395 case (-4): resp_print_length_negative(ndo
); break;
396 default: resp_print_invalid(ndo
); break;
400 return (length
- length_cur
);
407 resp_print_inline(netdissect_options
*ndo
, const u_char
*bp
, int length
) {
408 int length_cur
= length
;
410 const u_char
*bp_ptr
;
413 * Inline commands are simply 'strings' followed by \r or \n or both.
414 * Redis will do its best to split/parse these strings.
415 * This feature of redis is implemented to support the ability of
416 * command parsing from telnet/nc sessions etc.
418 * <string><\r||\n||\r\n...>
422 * Skip forward past any leading \r, \n, or \r\n.
424 CONSUME_CR_OR_LF(bp
, length_cur
);
428 * Scan forward looking for \r or \n.
430 FIND_CR_OR_LF(bp_ptr
, length_cur
);
433 * Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the
434 * Length of the line text that precedes it. Print it.
436 len
= ND_BYTES_BETWEEN(bp_ptr
, bp
);
437 RESP_PRINT_SEGMENT(ndo
, bp
, len
);
440 * Skip forward past the \r, \n, or \r\n.
442 CONSUME_CR_OR_LF(bp_ptr
, length_cur
);
445 * Return the number of bytes we processed.
447 return (length
- length_cur
);
454 resp_get_length(netdissect_options
*ndo
, const u_char
*bp
, int len
, const u_char
**endp
)
466 if (GET_U_1(bp
) == '-') {
478 if (!(c
>= '0' && c
<= '9')) {
486 if (result
> (INT_MAX
/ 10)) {
487 /* This will overflow an int when we multiply it by 10. */
491 if (result
== ((INT_MAX
/ 10) * 10) && c
> (INT_MAX
% 10)) {
492 /* This will overflow an int when we add c */
503 * OK, we found a non-digit character. It should be a \r, followed
506 if (GET_U_1(bp
) != '\r') {
514 if (GET_U_1(bp
) != '\n') {
522 /* -1 means "null", anything else is invalid */
523 if (too_large
|| result
!= 1)
527 return (too_large
? -3 : result
);