xref: /freebsd/contrib/tcpdump/print-resp.c (revision ee67461e)
13340d773SGleb Smirnoff /*
23340d773SGleb Smirnoff  * Copyright (c) 2015 The TCPDUMP project
33340d773SGleb Smirnoff  * All rights reserved.
43340d773SGleb Smirnoff  *
53340d773SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
63340d773SGleb Smirnoff  * modification, are permitted provided that the following conditions
73340d773SGleb Smirnoff  * are met:
83340d773SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
93340d773SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
103340d773SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
113340d773SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
123340d773SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
133340d773SGleb Smirnoff  *
143340d773SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
153340d773SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
163340d773SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
173340d773SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
183340d773SGleb Smirnoff  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
193340d773SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
203340d773SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
213340d773SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
223340d773SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233340d773SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
243340d773SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
253340d773SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
263340d773SGleb Smirnoff  *
273340d773SGleb Smirnoff  * Initial contribution by Andrew Darqui (andrew.darqui@gmail.com).
283340d773SGleb Smirnoff  */
293340d773SGleb Smirnoff 
303340d773SGleb Smirnoff /* \summary: REdis Serialization Protocol (RESP) printer */
313340d773SGleb Smirnoff 
323340d773SGleb Smirnoff #ifdef HAVE_CONFIG_H
33*ee67461eSJoseph Mingrone #include <config.h>
343340d773SGleb Smirnoff #endif
353340d773SGleb Smirnoff 
36*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
373340d773SGleb Smirnoff #include "netdissect.h"
383340d773SGleb Smirnoff #include <limits.h>
393340d773SGleb Smirnoff 
403340d773SGleb Smirnoff #include "extract.h"
413340d773SGleb Smirnoff 
423340d773SGleb Smirnoff 
433340d773SGleb Smirnoff /*
44*ee67461eSJoseph Mingrone  * For information regarding RESP, see: https://redis.io/topics/protocol
453340d773SGleb Smirnoff  */
463340d773SGleb Smirnoff 
473340d773SGleb Smirnoff #define RESP_SIMPLE_STRING    '+'
483340d773SGleb Smirnoff #define RESP_ERROR            '-'
493340d773SGleb Smirnoff #define RESP_INTEGER          ':'
503340d773SGleb Smirnoff #define RESP_BULK_STRING      '$'
513340d773SGleb Smirnoff #define RESP_ARRAY            '*'
523340d773SGleb Smirnoff 
53*ee67461eSJoseph Mingrone #define resp_print_empty(ndo)            ND_PRINT(" empty")
54*ee67461eSJoseph Mingrone #define resp_print_null(ndo)             ND_PRINT(" null")
55*ee67461eSJoseph Mingrone #define resp_print_length_too_large(ndo) ND_PRINT(" length too large")
56*ee67461eSJoseph Mingrone #define resp_print_length_negative(ndo)  ND_PRINT(" length negative and not -1")
57*ee67461eSJoseph Mingrone #define resp_print_invalid(ndo)          ND_PRINT(" invalid")
583340d773SGleb Smirnoff 
59*ee67461eSJoseph Mingrone static int resp_parse(netdissect_options *, const u_char *, int);
60*ee67461eSJoseph Mingrone static int resp_print_string_error_integer(netdissect_options *, const u_char *, int);
61*ee67461eSJoseph Mingrone static int resp_print_simple_string(netdissect_options *, const u_char *, int);
62*ee67461eSJoseph Mingrone static int resp_print_integer(netdissect_options *, const u_char *, int);
63*ee67461eSJoseph Mingrone static int resp_print_error(netdissect_options *, const u_char *, int);
64*ee67461eSJoseph Mingrone static int resp_print_bulk_string(netdissect_options *, const u_char *, int);
65*ee67461eSJoseph Mingrone static int resp_print_bulk_array(netdissect_options *, const u_char *, int);
66*ee67461eSJoseph Mingrone static int resp_print_inline(netdissect_options *, const u_char *, int);
67*ee67461eSJoseph Mingrone static int resp_get_length(netdissect_options *, const u_char *, int, const u_char **);
683340d773SGleb Smirnoff 
693340d773SGleb Smirnoff #define LCHECK2(_tot_len, _len) \
703340d773SGleb Smirnoff     {                           \
713340d773SGleb Smirnoff         if (_tot_len < _len)    \
723340d773SGleb Smirnoff             goto trunc;         \
733340d773SGleb Smirnoff     }
743340d773SGleb Smirnoff 
753340d773SGleb Smirnoff #define LCHECK(_tot_len) LCHECK2(_tot_len, 1)
763340d773SGleb Smirnoff 
773340d773SGleb Smirnoff /*
783340d773SGleb Smirnoff  * FIND_CRLF:
793340d773SGleb Smirnoff  * Attempts to move our 'ptr' forward until a \r\n is found,
803340d773SGleb Smirnoff  * while also making sure we don't exceed the buffer '_len'
813340d773SGleb Smirnoff  * or go past the end of the captured data.
823340d773SGleb Smirnoff  * If we exceed or go past the end of the captured data,
833340d773SGleb Smirnoff  * jump to trunc.
843340d773SGleb Smirnoff  */
853340d773SGleb Smirnoff #define FIND_CRLF(_ptr, _len)                   \
863340d773SGleb Smirnoff     for (;;) {                                  \
873340d773SGleb Smirnoff         LCHECK2(_len, 2);                       \
88*ee67461eSJoseph Mingrone         ND_TCHECK_2(_ptr);                      \
89*ee67461eSJoseph Mingrone         if (GET_U_1(_ptr) == '\r' &&            \
90*ee67461eSJoseph Mingrone             GET_U_1(_ptr+1) == '\n')            \
913340d773SGleb Smirnoff             break;                              \
923340d773SGleb Smirnoff         _ptr++;                                 \
933340d773SGleb Smirnoff         _len--;                                 \
943340d773SGleb Smirnoff     }
953340d773SGleb Smirnoff 
963340d773SGleb Smirnoff /*
973340d773SGleb Smirnoff  * CONSUME_CRLF
983340d773SGleb Smirnoff  * Consume a CRLF that we've just found.
993340d773SGleb Smirnoff  */
1003340d773SGleb Smirnoff #define CONSUME_CRLF(_ptr, _len) \
1013340d773SGleb Smirnoff     _ptr += 2;                   \
1023340d773SGleb Smirnoff     _len -= 2;
1033340d773SGleb Smirnoff 
1043340d773SGleb Smirnoff /*
1053340d773SGleb Smirnoff  * FIND_CR_OR_LF
1063340d773SGleb Smirnoff  * Attempts to move our '_ptr' forward until a \r or \n is found,
1073340d773SGleb Smirnoff  * while also making sure we don't exceed the buffer '_len'
1083340d773SGleb Smirnoff  * or go past the end of the captured data.
1093340d773SGleb Smirnoff  * If we exceed or go past the end of the captured data,
1103340d773SGleb Smirnoff  * jump to trunc.
1113340d773SGleb Smirnoff  */
1123340d773SGleb Smirnoff #define FIND_CR_OR_LF(_ptr, _len)           \
1133340d773SGleb Smirnoff     for (;;) {                              \
1143340d773SGleb Smirnoff         LCHECK(_len);                       \
115*ee67461eSJoseph Mingrone         if (GET_U_1(_ptr) == '\r' ||        \
116*ee67461eSJoseph Mingrone             GET_U_1(_ptr) == '\n')          \
1173340d773SGleb Smirnoff             break;                          \
1183340d773SGleb Smirnoff         _ptr++;                             \
1193340d773SGleb Smirnoff         _len--;                             \
1203340d773SGleb Smirnoff     }
1213340d773SGleb Smirnoff 
1223340d773SGleb Smirnoff /*
1233340d773SGleb Smirnoff  * CONSUME_CR_OR_LF
1243340d773SGleb Smirnoff  * Consume all consecutive \r and \n bytes.
1253340d773SGleb Smirnoff  * If we exceed '_len' or go past the end of the captured data,
1263340d773SGleb Smirnoff  * jump to trunc.
1273340d773SGleb Smirnoff  */
1283340d773SGleb Smirnoff #define CONSUME_CR_OR_LF(_ptr, _len)             \
1293340d773SGleb Smirnoff     {                                            \
1303340d773SGleb Smirnoff         int _found_cr_or_lf = 0;                 \
1313340d773SGleb Smirnoff         for (;;) {                               \
1323340d773SGleb Smirnoff             /*                                   \
1333340d773SGleb Smirnoff              * Have we hit the end of data?      \
1343340d773SGleb Smirnoff              */                                  \
135*ee67461eSJoseph Mingrone             if (_len == 0 || !ND_TTEST_1(_ptr)) {\
1363340d773SGleb Smirnoff                 /*                               \
1373340d773SGleb Smirnoff                  * Yes.  Have we seen a \r       \
1383340d773SGleb Smirnoff                  * or \n?                        \
1393340d773SGleb Smirnoff                  */                              \
1403340d773SGleb Smirnoff                 if (_found_cr_or_lf) {           \
1413340d773SGleb Smirnoff                     /*                           \
1423340d773SGleb Smirnoff                      * Yes.  Just stop.          \
1433340d773SGleb Smirnoff                      */                          \
1443340d773SGleb Smirnoff                     break;                       \
1453340d773SGleb Smirnoff                 }                                \
1463340d773SGleb Smirnoff                 /*                               \
1473340d773SGleb Smirnoff                  * No.  We ran out of packet.    \
1483340d773SGleb Smirnoff                  */                              \
1493340d773SGleb Smirnoff                 goto trunc;                      \
1503340d773SGleb Smirnoff             }                                    \
151*ee67461eSJoseph Mingrone             if (GET_U_1(_ptr) != '\r' &&         \
152*ee67461eSJoseph Mingrone                 GET_U_1(_ptr) != '\n')           \
1533340d773SGleb Smirnoff                 break;                           \
1543340d773SGleb Smirnoff             _found_cr_or_lf = 1;                 \
1553340d773SGleb Smirnoff             _ptr++;                              \
1563340d773SGleb Smirnoff             _len--;                              \
1573340d773SGleb Smirnoff         }                                        \
1583340d773SGleb Smirnoff     }
1593340d773SGleb Smirnoff 
1603340d773SGleb Smirnoff /*
1613340d773SGleb Smirnoff  * SKIP_OPCODE
1623340d773SGleb Smirnoff  * Skip over the opcode character.
1633340d773SGleb Smirnoff  * The opcode has already been fetched, so we know it's there, and don't
1643340d773SGleb Smirnoff  * need to do any checks.
1653340d773SGleb Smirnoff  */
1663340d773SGleb Smirnoff #define SKIP_OPCODE(_ptr, _tot_len) \
1673340d773SGleb Smirnoff     _ptr++;                         \
1683340d773SGleb Smirnoff     _tot_len--;
1693340d773SGleb Smirnoff 
1703340d773SGleb Smirnoff /*
1713340d773SGleb Smirnoff  * GET_LENGTH
1723340d773SGleb Smirnoff  * Get a bulk string or array length.
1733340d773SGleb Smirnoff  */
1743340d773SGleb Smirnoff #define GET_LENGTH(_ndo, _tot_len, _ptr, _len)                \
1753340d773SGleb Smirnoff     {                                                         \
1763340d773SGleb Smirnoff         const u_char *_endp;                                  \
1773340d773SGleb Smirnoff         _len = resp_get_length(_ndo, _ptr, _tot_len, &_endp); \
1783340d773SGleb Smirnoff         _tot_len -= (_endp - _ptr);                           \
1793340d773SGleb Smirnoff         _ptr = _endp;                                         \
1803340d773SGleb Smirnoff     }
1813340d773SGleb Smirnoff 
1823340d773SGleb Smirnoff /*
1833340d773SGleb Smirnoff  * TEST_RET_LEN
1843340d773SGleb Smirnoff  * If ret_len is < 0, jump to the trunc tag which returns (-1)
1853340d773SGleb Smirnoff  * and 'bubbles up' to printing tstr. Otherwise, return ret_len.
1863340d773SGleb Smirnoff  */
1873340d773SGleb Smirnoff #define TEST_RET_LEN(rl) \
1883340d773SGleb Smirnoff     if (rl < 0) { goto trunc; } else { return rl; }
1893340d773SGleb Smirnoff 
1903340d773SGleb Smirnoff /*
1913340d773SGleb Smirnoff  * TEST_RET_LEN_NORETURN
1923340d773SGleb Smirnoff  * If ret_len is < 0, jump to the trunc tag which returns (-1)
1933340d773SGleb Smirnoff  * and 'bubbles up' to printing tstr. Otherwise, continue onward.
1943340d773SGleb Smirnoff  */
1953340d773SGleb Smirnoff #define TEST_RET_LEN_NORETURN(rl) \
1963340d773SGleb Smirnoff     if (rl < 0) { goto trunc; }
1973340d773SGleb Smirnoff 
1983340d773SGleb Smirnoff /*
1993340d773SGleb Smirnoff  * RESP_PRINT_SEGMENT
2003340d773SGleb Smirnoff  * Prints a segment in the form of: ' "<stuff>"\n"
2013340d773SGleb Smirnoff  * Assumes the data has already been verified as present.
2023340d773SGleb Smirnoff  */
2033340d773SGleb Smirnoff #define RESP_PRINT_SEGMENT(_ndo, _bp, _len)            \
204*ee67461eSJoseph Mingrone     ND_PRINT(" \"");                                   \
205*ee67461eSJoseph Mingrone     if (nd_printn(_ndo, _bp, _len, _ndo->ndo_snapend)) \
2063340d773SGleb Smirnoff         goto trunc;                                    \
2073340d773SGleb Smirnoff     fn_print_char(_ndo, '"');
2083340d773SGleb Smirnoff 
2093340d773SGleb Smirnoff void
resp_print(netdissect_options * ndo,const u_char * bp,u_int length)2103340d773SGleb Smirnoff resp_print(netdissect_options *ndo, const u_char *bp, u_int length)
2113340d773SGleb Smirnoff {
212*ee67461eSJoseph Mingrone     int ret_len = 0;
2133340d773SGleb Smirnoff 
214*ee67461eSJoseph Mingrone     ndo->ndo_protocol = "resp";
2153340d773SGleb Smirnoff 
216*ee67461eSJoseph Mingrone     ND_PRINT(": RESP");
217*ee67461eSJoseph Mingrone     while (length > 0) {
2183340d773SGleb Smirnoff         /*
2193340d773SGleb Smirnoff          * This block supports redis pipelining.
2203340d773SGleb Smirnoff          * For example, multiple operations can be pipelined within the same string:
2213340d773SGleb Smirnoff          * "*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"
2223340d773SGleb Smirnoff          * or
2233340d773SGleb Smirnoff          * "PING\r\nPING\r\nPING\r\n"
2243340d773SGleb Smirnoff          * In order to handle this case, we must try and parse 'bp' until
2253340d773SGleb Smirnoff          * 'length' bytes have been processed or we reach a trunc condition.
2263340d773SGleb Smirnoff          */
227*ee67461eSJoseph Mingrone         ret_len = resp_parse(ndo, bp, length);
2283340d773SGleb Smirnoff         TEST_RET_LEN_NORETURN(ret_len);
2293340d773SGleb Smirnoff         bp += ret_len;
230*ee67461eSJoseph Mingrone         length -= ret_len;
2313340d773SGleb Smirnoff     }
2323340d773SGleb Smirnoff 
2333340d773SGleb Smirnoff     return;
2343340d773SGleb Smirnoff 
2353340d773SGleb Smirnoff trunc:
236*ee67461eSJoseph Mingrone     nd_print_trunc(ndo);
2373340d773SGleb Smirnoff }
2383340d773SGleb Smirnoff 
2393340d773SGleb Smirnoff static int
resp_parse(netdissect_options * ndo,const u_char * bp,int length)240*ee67461eSJoseph Mingrone resp_parse(netdissect_options *ndo, const u_char *bp, int length)
2413340d773SGleb Smirnoff {
2423340d773SGleb Smirnoff     u_char op;
2433340d773SGleb Smirnoff     int ret_len;
2443340d773SGleb Smirnoff 
2453340d773SGleb Smirnoff     LCHECK2(length, 1);
246*ee67461eSJoseph Mingrone     op = GET_U_1(bp);
2473340d773SGleb Smirnoff 
2483340d773SGleb Smirnoff     /* bp now points to the op, so these routines must skip it */
2493340d773SGleb Smirnoff     switch(op) {
2503340d773SGleb Smirnoff         case RESP_SIMPLE_STRING:  ret_len = resp_print_simple_string(ndo, bp, length);   break;
2513340d773SGleb Smirnoff         case RESP_INTEGER:        ret_len = resp_print_integer(ndo, bp, length);         break;
2523340d773SGleb Smirnoff         case RESP_ERROR:          ret_len = resp_print_error(ndo, bp, length);           break;
2533340d773SGleb Smirnoff         case RESP_BULK_STRING:    ret_len = resp_print_bulk_string(ndo, bp, length);     break;
2543340d773SGleb Smirnoff         case RESP_ARRAY:          ret_len = resp_print_bulk_array(ndo, bp, length);      break;
2553340d773SGleb Smirnoff         default:                  ret_len = resp_print_inline(ndo, bp, length);          break;
2563340d773SGleb Smirnoff     }
2573340d773SGleb Smirnoff 
2583340d773SGleb Smirnoff     /*
2593340d773SGleb Smirnoff      * This gives up with a "truncated" indicator for all errors,
2603340d773SGleb Smirnoff      * including invalid packet errors; that's what we want, as
2613340d773SGleb Smirnoff      * we have to give up on further parsing in that case.
2623340d773SGleb Smirnoff      */
2633340d773SGleb Smirnoff     TEST_RET_LEN(ret_len);
2643340d773SGleb Smirnoff 
2653340d773SGleb Smirnoff trunc:
2663340d773SGleb Smirnoff     return (-1);
2673340d773SGleb Smirnoff }
2683340d773SGleb Smirnoff 
2693340d773SGleb Smirnoff static int
resp_print_simple_string(netdissect_options * ndo,const u_char * bp,int length)270*ee67461eSJoseph Mingrone resp_print_simple_string(netdissect_options *ndo, const u_char *bp, int length) {
2713340d773SGleb Smirnoff     return resp_print_string_error_integer(ndo, bp, length);
2723340d773SGleb Smirnoff }
2733340d773SGleb Smirnoff 
2743340d773SGleb Smirnoff static int
resp_print_integer(netdissect_options * ndo,const u_char * bp,int length)275*ee67461eSJoseph Mingrone resp_print_integer(netdissect_options *ndo, const u_char *bp, int length) {
2763340d773SGleb Smirnoff     return resp_print_string_error_integer(ndo, bp, length);
2773340d773SGleb Smirnoff }
2783340d773SGleb Smirnoff 
2793340d773SGleb Smirnoff static int
resp_print_error(netdissect_options * ndo,const u_char * bp,int length)280*ee67461eSJoseph Mingrone resp_print_error(netdissect_options *ndo, const u_char *bp, int length) {
2813340d773SGleb Smirnoff     return resp_print_string_error_integer(ndo, bp, length);
2823340d773SGleb Smirnoff }
2833340d773SGleb Smirnoff 
2843340d773SGleb Smirnoff static int
resp_print_string_error_integer(netdissect_options * ndo,const u_char * bp,int length)285*ee67461eSJoseph Mingrone resp_print_string_error_integer(netdissect_options *ndo, const u_char *bp, int length) {
2863340d773SGleb Smirnoff     int length_cur = length, len, ret_len;
2873340d773SGleb Smirnoff     const u_char *bp_ptr;
2883340d773SGleb Smirnoff 
2893340d773SGleb Smirnoff     /* bp points to the op; skip it */
2903340d773SGleb Smirnoff     SKIP_OPCODE(bp, length_cur);
2913340d773SGleb Smirnoff     bp_ptr = bp;
2923340d773SGleb Smirnoff 
2933340d773SGleb Smirnoff     /*
2943340d773SGleb Smirnoff      * bp now prints past the (+-;) opcode, so it's pointing to the first
2953340d773SGleb Smirnoff      * character of the string (which could be numeric).
2963340d773SGleb Smirnoff      * +OK\r\n
2973340d773SGleb Smirnoff      * -ERR ...\r\n
2983340d773SGleb Smirnoff      * :02912309\r\n
2993340d773SGleb Smirnoff      *
3003340d773SGleb Smirnoff      * Find the \r\n with FIND_CRLF().
3013340d773SGleb Smirnoff      */
3023340d773SGleb Smirnoff     FIND_CRLF(bp_ptr, length_cur);
3033340d773SGleb Smirnoff 
3043340d773SGleb Smirnoff     /*
3053340d773SGleb Smirnoff      * bp_ptr points to the \r\n, so bp_ptr - bp is the length of text
3063340d773SGleb Smirnoff      * preceding the \r\n.  That includes the opcode, so don't print
3073340d773SGleb Smirnoff      * that.
3083340d773SGleb Smirnoff      */
309*ee67461eSJoseph Mingrone     len = ND_BYTES_BETWEEN(bp_ptr, bp);
3103340d773SGleb Smirnoff     RESP_PRINT_SEGMENT(ndo, bp, len);
3113340d773SGleb Smirnoff     ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/;
3123340d773SGleb Smirnoff 
3133340d773SGleb Smirnoff     TEST_RET_LEN(ret_len);
3143340d773SGleb Smirnoff 
3153340d773SGleb Smirnoff trunc:
3163340d773SGleb Smirnoff     return (-1);
3173340d773SGleb Smirnoff }
3183340d773SGleb Smirnoff 
3193340d773SGleb Smirnoff static int
resp_print_bulk_string(netdissect_options * ndo,const u_char * bp,int length)320*ee67461eSJoseph Mingrone resp_print_bulk_string(netdissect_options *ndo, const u_char *bp, int length) {
3213340d773SGleb Smirnoff     int length_cur = length, string_len;
3223340d773SGleb Smirnoff 
3233340d773SGleb Smirnoff     /* bp points to the op; skip it */
3243340d773SGleb Smirnoff     SKIP_OPCODE(bp, length_cur);
3253340d773SGleb Smirnoff 
3263340d773SGleb Smirnoff     /* <length>\r\n */
3273340d773SGleb Smirnoff     GET_LENGTH(ndo, length_cur, bp, string_len);
3283340d773SGleb Smirnoff 
3293340d773SGleb Smirnoff     if (string_len >= 0) {
3303340d773SGleb Smirnoff         /* Byte string of length string_len, starting at bp */
3313340d773SGleb Smirnoff         if (string_len == 0)
3323340d773SGleb Smirnoff             resp_print_empty(ndo);
3333340d773SGleb Smirnoff         else {
3343340d773SGleb Smirnoff             LCHECK2(length_cur, string_len);
335*ee67461eSJoseph Mingrone             ND_TCHECK_LEN(bp, string_len);
3363340d773SGleb Smirnoff             RESP_PRINT_SEGMENT(ndo, bp, string_len);
3373340d773SGleb Smirnoff             bp += string_len;
3383340d773SGleb Smirnoff             length_cur -= string_len;
3393340d773SGleb Smirnoff         }
3403340d773SGleb Smirnoff 
3413340d773SGleb Smirnoff         /*
3423340d773SGleb Smirnoff          * Find the \r\n at the end of the string and skip past it.
3433340d773SGleb Smirnoff          * XXX - report an error if the \r\n isn't immediately after
3443340d773SGleb Smirnoff          * the item?
3453340d773SGleb Smirnoff          */
3463340d773SGleb Smirnoff         FIND_CRLF(bp, length_cur);
3473340d773SGleb Smirnoff         CONSUME_CRLF(bp, length_cur);
3483340d773SGleb Smirnoff     } else {
3493340d773SGleb Smirnoff         /* null, truncated, or invalid for some reason */
3503340d773SGleb Smirnoff         switch(string_len) {
3513340d773SGleb Smirnoff             case (-1):  resp_print_null(ndo);             break;
3523340d773SGleb Smirnoff             case (-2):  goto trunc;
3533340d773SGleb Smirnoff             case (-3):  resp_print_length_too_large(ndo); break;
3543340d773SGleb Smirnoff             case (-4):  resp_print_length_negative(ndo);  break;
3553340d773SGleb Smirnoff             default:    resp_print_invalid(ndo);          break;
3563340d773SGleb Smirnoff         }
3573340d773SGleb Smirnoff     }
3583340d773SGleb Smirnoff 
3593340d773SGleb Smirnoff     return (length - length_cur);
3603340d773SGleb Smirnoff 
3613340d773SGleb Smirnoff trunc:
3623340d773SGleb Smirnoff     return (-1);
3633340d773SGleb Smirnoff }
3643340d773SGleb Smirnoff 
3653340d773SGleb Smirnoff static int
resp_print_bulk_array(netdissect_options * ndo,const u_char * bp,int length)366*ee67461eSJoseph Mingrone resp_print_bulk_array(netdissect_options *ndo, const u_char *bp, int length) {
3673340d773SGleb Smirnoff     u_int length_cur = length;
3683340d773SGleb Smirnoff     int array_len, i, ret_len;
3693340d773SGleb Smirnoff 
3703340d773SGleb Smirnoff     /* bp points to the op; skip it */
3713340d773SGleb Smirnoff     SKIP_OPCODE(bp, length_cur);
3723340d773SGleb Smirnoff 
3733340d773SGleb Smirnoff     /* <array_length>\r\n */
3743340d773SGleb Smirnoff     GET_LENGTH(ndo, length_cur, bp, array_len);
3753340d773SGleb Smirnoff 
3763340d773SGleb Smirnoff     if (array_len > 0) {
3773340d773SGleb Smirnoff         /* non empty array */
3783340d773SGleb Smirnoff         for (i = 0; i < array_len; i++) {
3793340d773SGleb Smirnoff             ret_len = resp_parse(ndo, bp, length_cur);
3803340d773SGleb Smirnoff 
3813340d773SGleb Smirnoff             TEST_RET_LEN_NORETURN(ret_len);
3823340d773SGleb Smirnoff 
3833340d773SGleb Smirnoff             bp += ret_len;
3843340d773SGleb Smirnoff             length_cur -= ret_len;
3853340d773SGleb Smirnoff         }
3863340d773SGleb Smirnoff     } else {
3873340d773SGleb Smirnoff         /* empty, null, truncated, or invalid */
3883340d773SGleb Smirnoff         switch(array_len) {
3893340d773SGleb Smirnoff             case 0:     resp_print_empty(ndo);            break;
3903340d773SGleb Smirnoff             case (-1):  resp_print_null(ndo);             break;
3913340d773SGleb Smirnoff             case (-2):  goto trunc;
3923340d773SGleb Smirnoff             case (-3):  resp_print_length_too_large(ndo); break;
3933340d773SGleb Smirnoff             case (-4):  resp_print_length_negative(ndo);  break;
3943340d773SGleb Smirnoff             default:    resp_print_invalid(ndo);          break;
3953340d773SGleb Smirnoff         }
3963340d773SGleb Smirnoff     }
3973340d773SGleb Smirnoff 
3983340d773SGleb Smirnoff     return (length - length_cur);
3993340d773SGleb Smirnoff 
4003340d773SGleb Smirnoff trunc:
4013340d773SGleb Smirnoff     return (-1);
4023340d773SGleb Smirnoff }
4033340d773SGleb Smirnoff 
4043340d773SGleb Smirnoff static int
resp_print_inline(netdissect_options * ndo,const u_char * bp,int length)405*ee67461eSJoseph Mingrone resp_print_inline(netdissect_options *ndo, const u_char *bp, int length) {
4063340d773SGleb Smirnoff     int length_cur = length;
4073340d773SGleb Smirnoff     int len;
4083340d773SGleb Smirnoff     const u_char *bp_ptr;
4093340d773SGleb Smirnoff 
4103340d773SGleb Smirnoff     /*
4113340d773SGleb Smirnoff      * Inline commands are simply 'strings' followed by \r or \n or both.
4123340d773SGleb Smirnoff      * Redis will do its best to split/parse these strings.
4133340d773SGleb Smirnoff      * This feature of redis is implemented to support the ability of
4143340d773SGleb Smirnoff      * command parsing from telnet/nc sessions etc.
4153340d773SGleb Smirnoff      *
4163340d773SGleb Smirnoff      * <string><\r||\n||\r\n...>
4173340d773SGleb Smirnoff      */
4183340d773SGleb Smirnoff 
4193340d773SGleb Smirnoff     /*
4203340d773SGleb Smirnoff      * Skip forward past any leading \r, \n, or \r\n.
4213340d773SGleb Smirnoff      */
4223340d773SGleb Smirnoff     CONSUME_CR_OR_LF(bp, length_cur);
4233340d773SGleb Smirnoff     bp_ptr = bp;
4243340d773SGleb Smirnoff 
4253340d773SGleb Smirnoff     /*
4263340d773SGleb Smirnoff      * Scan forward looking for \r or \n.
4273340d773SGleb Smirnoff      */
4283340d773SGleb Smirnoff     FIND_CR_OR_LF(bp_ptr, length_cur);
4293340d773SGleb Smirnoff 
4303340d773SGleb Smirnoff     /*
4313340d773SGleb Smirnoff      * Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the
432*ee67461eSJoseph Mingrone      * Length of the line text that precedes it.  Print it.
4333340d773SGleb Smirnoff      */
434*ee67461eSJoseph Mingrone     len = ND_BYTES_BETWEEN(bp_ptr, bp);
4353340d773SGleb Smirnoff     RESP_PRINT_SEGMENT(ndo, bp, len);
4363340d773SGleb Smirnoff 
4373340d773SGleb Smirnoff     /*
4383340d773SGleb Smirnoff      * Skip forward past the \r, \n, or \r\n.
4393340d773SGleb Smirnoff      */
4403340d773SGleb Smirnoff     CONSUME_CR_OR_LF(bp_ptr, length_cur);
4413340d773SGleb Smirnoff 
4423340d773SGleb Smirnoff     /*
4433340d773SGleb Smirnoff      * Return the number of bytes we processed.
4443340d773SGleb Smirnoff      */
4453340d773SGleb Smirnoff     return (length - length_cur);
4463340d773SGleb Smirnoff 
4473340d773SGleb Smirnoff trunc:
4483340d773SGleb Smirnoff     return (-1);
4493340d773SGleb Smirnoff }
4503340d773SGleb Smirnoff 
4513340d773SGleb Smirnoff static int
resp_get_length(netdissect_options * ndo,const u_char * bp,int len,const u_char ** endp)452*ee67461eSJoseph Mingrone resp_get_length(netdissect_options *ndo, const u_char *bp, int len, const u_char **endp)
4533340d773SGleb Smirnoff {
4543340d773SGleb Smirnoff     int result;
4553340d773SGleb Smirnoff     u_char c;
4563340d773SGleb Smirnoff     int saw_digit;
4573340d773SGleb Smirnoff     int neg;
4583340d773SGleb Smirnoff     int too_large;
4593340d773SGleb Smirnoff 
4603340d773SGleb Smirnoff     if (len == 0)
4613340d773SGleb Smirnoff         goto trunc;
4623340d773SGleb Smirnoff     too_large = 0;
4633340d773SGleb Smirnoff     neg = 0;
464*ee67461eSJoseph Mingrone     if (GET_U_1(bp) == '-') {
4653340d773SGleb Smirnoff         neg = 1;
4663340d773SGleb Smirnoff         bp++;
4673340d773SGleb Smirnoff         len--;
4683340d773SGleb Smirnoff     }
4693340d773SGleb Smirnoff     result = 0;
4703340d773SGleb Smirnoff     saw_digit = 0;
4713340d773SGleb Smirnoff 
4723340d773SGleb Smirnoff     for (;;) {
4733340d773SGleb Smirnoff         if (len == 0)
4743340d773SGleb Smirnoff             goto trunc;
475*ee67461eSJoseph Mingrone         c = GET_U_1(bp);
4763340d773SGleb Smirnoff         if (!(c >= '0' && c <= '9')) {
4770bff6a5aSEd Maste             if (!saw_digit) {
4780bff6a5aSEd Maste                 bp++;
4793340d773SGleb Smirnoff                 goto invalid;
4800bff6a5aSEd Maste             }
4813340d773SGleb Smirnoff             break;
4823340d773SGleb Smirnoff         }
4833340d773SGleb Smirnoff         c -= '0';
4843340d773SGleb Smirnoff         if (result > (INT_MAX / 10)) {
4853340d773SGleb Smirnoff             /* This will overflow an int when we multiply it by 10. */
4863340d773SGleb Smirnoff             too_large = 1;
4873340d773SGleb Smirnoff         } else {
4883340d773SGleb Smirnoff             result *= 10;
4890bff6a5aSEd Maste             if (result == ((INT_MAX / 10) * 10) && c > (INT_MAX % 10)) {
4903340d773SGleb Smirnoff                 /* This will overflow an int when we add c */
4913340d773SGleb Smirnoff                 too_large = 1;
4923340d773SGleb Smirnoff             } else
4933340d773SGleb Smirnoff                 result += c;
4943340d773SGleb Smirnoff         }
4953340d773SGleb Smirnoff         bp++;
4963340d773SGleb Smirnoff         len--;
4973340d773SGleb Smirnoff         saw_digit = 1;
4983340d773SGleb Smirnoff     }
4993340d773SGleb Smirnoff 
5003340d773SGleb Smirnoff     /*
5010bff6a5aSEd Maste      * OK, we found a non-digit character.  It should be a \r, followed
5020bff6a5aSEd Maste      * by a \n.
5033340d773SGleb Smirnoff      */
504*ee67461eSJoseph Mingrone     if (GET_U_1(bp) != '\r') {
5050bff6a5aSEd Maste         bp++;
5063340d773SGleb Smirnoff         goto invalid;
5070bff6a5aSEd Maste     }
5083340d773SGleb Smirnoff     bp++;
5093340d773SGleb Smirnoff     len--;
5103340d773SGleb Smirnoff     if (len == 0)
5113340d773SGleb Smirnoff         goto trunc;
512*ee67461eSJoseph Mingrone     if (GET_U_1(bp) != '\n') {
5130bff6a5aSEd Maste         bp++;
5143340d773SGleb Smirnoff         goto invalid;
5150bff6a5aSEd Maste     }
5163340d773SGleb Smirnoff     bp++;
5173340d773SGleb Smirnoff     len--;
5183340d773SGleb Smirnoff     *endp = bp;
5193340d773SGleb Smirnoff     if (neg) {
5203340d773SGleb Smirnoff         /* -1 means "null", anything else is invalid */
5213340d773SGleb Smirnoff         if (too_large || result != 1)
5223340d773SGleb Smirnoff             return (-4);
5233340d773SGleb Smirnoff         result = -1;
5243340d773SGleb Smirnoff     }
5253340d773SGleb Smirnoff     return (too_large ? -3 : result);
5263340d773SGleb Smirnoff 
5273340d773SGleb Smirnoff trunc:
5280bff6a5aSEd Maste     *endp = bp;
5293340d773SGleb Smirnoff     return (-2);
5303340d773SGleb Smirnoff 
5313340d773SGleb Smirnoff invalid:
5320bff6a5aSEd Maste     *endp = bp;
5333340d773SGleb Smirnoff     return (-5);
5343340d773SGleb Smirnoff }
535