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 32ee67461eSJoseph Mingrone #include <config.h> 333340d773SGleb Smirnoff 34ee67461eSJoseph Mingrone #include "netdissect-stdinc.h" 353340d773SGleb Smirnoff #include "netdissect.h" 363340d773SGleb Smirnoff #include <limits.h> 373340d773SGleb Smirnoff 383340d773SGleb Smirnoff #include "extract.h" 393340d773SGleb Smirnoff 403340d773SGleb Smirnoff 413340d773SGleb Smirnoff /* 42ee67461eSJoseph Mingrone * For information regarding RESP, see: https://redis.io/topics/protocol 433340d773SGleb Smirnoff */ 443340d773SGleb Smirnoff 453340d773SGleb Smirnoff #define RESP_SIMPLE_STRING '+' 463340d773SGleb Smirnoff #define RESP_ERROR '-' 473340d773SGleb Smirnoff #define RESP_INTEGER ':' 483340d773SGleb Smirnoff #define RESP_BULK_STRING '$' 493340d773SGleb Smirnoff #define RESP_ARRAY '*' 503340d773SGleb Smirnoff 51ee67461eSJoseph Mingrone #define resp_print_empty(ndo) ND_PRINT(" empty") 52ee67461eSJoseph Mingrone #define resp_print_null(ndo) ND_PRINT(" null") 53ee67461eSJoseph Mingrone #define resp_print_length_too_large(ndo) ND_PRINT(" length too large") 54ee67461eSJoseph Mingrone #define resp_print_length_negative(ndo) ND_PRINT(" length negative and not -1") 55ee67461eSJoseph Mingrone #define resp_print_invalid(ndo) ND_PRINT(" invalid") 563340d773SGleb Smirnoff 57ee67461eSJoseph Mingrone static int resp_parse(netdissect_options *, const u_char *, int); 58ee67461eSJoseph Mingrone static int resp_print_string_error_integer(netdissect_options *, const u_char *, int); 59ee67461eSJoseph Mingrone static int resp_print_simple_string(netdissect_options *, const u_char *, int); 60ee67461eSJoseph Mingrone static int resp_print_integer(netdissect_options *, const u_char *, int); 61ee67461eSJoseph Mingrone static int resp_print_error(netdissect_options *, const u_char *, int); 62ee67461eSJoseph Mingrone static int resp_print_bulk_string(netdissect_options *, const u_char *, int); 63ee67461eSJoseph Mingrone static int resp_print_bulk_array(netdissect_options *, const u_char *, int); 64ee67461eSJoseph Mingrone static int resp_print_inline(netdissect_options *, const u_char *, int); 65ee67461eSJoseph Mingrone static int resp_get_length(netdissect_options *, const u_char *, int, const u_char **); 663340d773SGleb Smirnoff 673340d773SGleb Smirnoff #define LCHECK2(_tot_len, _len) \ 683340d773SGleb Smirnoff { \ 693340d773SGleb Smirnoff if (_tot_len < _len) \ 703340d773SGleb Smirnoff goto trunc; \ 713340d773SGleb Smirnoff } 723340d773SGleb Smirnoff 733340d773SGleb Smirnoff #define LCHECK(_tot_len) LCHECK2(_tot_len, 1) 743340d773SGleb Smirnoff 753340d773SGleb Smirnoff /* 763340d773SGleb Smirnoff * FIND_CRLF: 773340d773SGleb Smirnoff * Attempts to move our 'ptr' forward until a \r\n is found, 783340d773SGleb Smirnoff * while also making sure we don't exceed the buffer '_len' 793340d773SGleb Smirnoff * or go past the end of the captured data. 803340d773SGleb Smirnoff * If we exceed or go past the end of the captured data, 813340d773SGleb Smirnoff * jump to trunc. 823340d773SGleb Smirnoff */ 833340d773SGleb Smirnoff #define FIND_CRLF(_ptr, _len) \ 843340d773SGleb Smirnoff for (;;) { \ 853340d773SGleb Smirnoff LCHECK2(_len, 2); \ 86ee67461eSJoseph Mingrone ND_TCHECK_2(_ptr); \ 87ee67461eSJoseph Mingrone if (GET_U_1(_ptr) == '\r' && \ 88ee67461eSJoseph Mingrone GET_U_1(_ptr+1) == '\n') \ 893340d773SGleb Smirnoff break; \ 903340d773SGleb Smirnoff _ptr++; \ 913340d773SGleb Smirnoff _len--; \ 923340d773SGleb Smirnoff } 933340d773SGleb Smirnoff 943340d773SGleb Smirnoff /* 953340d773SGleb Smirnoff * CONSUME_CRLF 963340d773SGleb Smirnoff * Consume a CRLF that we've just found. 973340d773SGleb Smirnoff */ 983340d773SGleb Smirnoff #define CONSUME_CRLF(_ptr, _len) \ 993340d773SGleb Smirnoff _ptr += 2; \ 1003340d773SGleb Smirnoff _len -= 2; 1013340d773SGleb Smirnoff 1023340d773SGleb Smirnoff /* 1033340d773SGleb Smirnoff * FIND_CR_OR_LF 1043340d773SGleb Smirnoff * Attempts to move our '_ptr' forward until a \r or \n is found, 1053340d773SGleb Smirnoff * while also making sure we don't exceed the buffer '_len' 1063340d773SGleb Smirnoff * or go past the end of the captured data. 1073340d773SGleb Smirnoff * If we exceed or go past the end of the captured data, 1083340d773SGleb Smirnoff * jump to trunc. 1093340d773SGleb Smirnoff */ 1103340d773SGleb Smirnoff #define FIND_CR_OR_LF(_ptr, _len) \ 1113340d773SGleb Smirnoff for (;;) { \ 1123340d773SGleb Smirnoff LCHECK(_len); \ 113ee67461eSJoseph Mingrone if (GET_U_1(_ptr) == '\r' || \ 114ee67461eSJoseph Mingrone GET_U_1(_ptr) == '\n') \ 1153340d773SGleb Smirnoff break; \ 1163340d773SGleb Smirnoff _ptr++; \ 1173340d773SGleb Smirnoff _len--; \ 1183340d773SGleb Smirnoff } 1193340d773SGleb Smirnoff 1203340d773SGleb Smirnoff /* 1213340d773SGleb Smirnoff * CONSUME_CR_OR_LF 1223340d773SGleb Smirnoff * Consume all consecutive \r and \n bytes. 1233340d773SGleb Smirnoff * If we exceed '_len' or go past the end of the captured data, 1243340d773SGleb Smirnoff * jump to trunc. 1253340d773SGleb Smirnoff */ 1263340d773SGleb Smirnoff #define CONSUME_CR_OR_LF(_ptr, _len) \ 1273340d773SGleb Smirnoff { \ 1283340d773SGleb Smirnoff int _found_cr_or_lf = 0; \ 1293340d773SGleb Smirnoff for (;;) { \ 1303340d773SGleb Smirnoff /* \ 1313340d773SGleb Smirnoff * Have we hit the end of data? \ 1323340d773SGleb Smirnoff */ \ 133ee67461eSJoseph Mingrone if (_len == 0 || !ND_TTEST_1(_ptr)) {\ 1343340d773SGleb Smirnoff /* \ 1353340d773SGleb Smirnoff * Yes. Have we seen a \r \ 1363340d773SGleb Smirnoff * or \n? \ 1373340d773SGleb Smirnoff */ \ 1383340d773SGleb Smirnoff if (_found_cr_or_lf) { \ 1393340d773SGleb Smirnoff /* \ 1403340d773SGleb Smirnoff * Yes. Just stop. \ 1413340d773SGleb Smirnoff */ \ 1423340d773SGleb Smirnoff break; \ 1433340d773SGleb Smirnoff } \ 1443340d773SGleb Smirnoff /* \ 1453340d773SGleb Smirnoff * No. We ran out of packet. \ 1463340d773SGleb Smirnoff */ \ 1473340d773SGleb Smirnoff goto trunc; \ 1483340d773SGleb Smirnoff } \ 149ee67461eSJoseph Mingrone if (GET_U_1(_ptr) != '\r' && \ 150ee67461eSJoseph Mingrone GET_U_1(_ptr) != '\n') \ 1513340d773SGleb Smirnoff break; \ 1523340d773SGleb Smirnoff _found_cr_or_lf = 1; \ 1533340d773SGleb Smirnoff _ptr++; \ 1543340d773SGleb Smirnoff _len--; \ 1553340d773SGleb Smirnoff } \ 1563340d773SGleb Smirnoff } 1573340d773SGleb Smirnoff 1583340d773SGleb Smirnoff /* 1593340d773SGleb Smirnoff * SKIP_OPCODE 1603340d773SGleb Smirnoff * Skip over the opcode character. 1613340d773SGleb Smirnoff * The opcode has already been fetched, so we know it's there, and don't 1623340d773SGleb Smirnoff * need to do any checks. 1633340d773SGleb Smirnoff */ 1643340d773SGleb Smirnoff #define SKIP_OPCODE(_ptr, _tot_len) \ 1653340d773SGleb Smirnoff _ptr++; \ 1663340d773SGleb Smirnoff _tot_len--; 1673340d773SGleb Smirnoff 1683340d773SGleb Smirnoff /* 1693340d773SGleb Smirnoff * GET_LENGTH 1703340d773SGleb Smirnoff * Get a bulk string or array length. 1713340d773SGleb Smirnoff */ 1723340d773SGleb Smirnoff #define GET_LENGTH(_ndo, _tot_len, _ptr, _len) \ 1733340d773SGleb Smirnoff { \ 1743340d773SGleb Smirnoff const u_char *_endp; \ 1753340d773SGleb Smirnoff _len = resp_get_length(_ndo, _ptr, _tot_len, &_endp); \ 1763340d773SGleb Smirnoff _tot_len -= (_endp - _ptr); \ 1773340d773SGleb Smirnoff _ptr = _endp; \ 1783340d773SGleb Smirnoff } 1793340d773SGleb Smirnoff 1803340d773SGleb Smirnoff /* 1813340d773SGleb Smirnoff * TEST_RET_LEN 1823340d773SGleb Smirnoff * If ret_len is < 0, jump to the trunc tag which returns (-1) 1833340d773SGleb Smirnoff * and 'bubbles up' to printing tstr. Otherwise, return ret_len. 1843340d773SGleb Smirnoff */ 1853340d773SGleb Smirnoff #define TEST_RET_LEN(rl) \ 1863340d773SGleb Smirnoff if (rl < 0) { goto trunc; } else { return rl; } 1873340d773SGleb Smirnoff 1883340d773SGleb Smirnoff /* 1893340d773SGleb Smirnoff * TEST_RET_LEN_NORETURN 1903340d773SGleb Smirnoff * If ret_len is < 0, jump to the trunc tag which returns (-1) 1913340d773SGleb Smirnoff * and 'bubbles up' to printing tstr. Otherwise, continue onward. 1923340d773SGleb Smirnoff */ 1933340d773SGleb Smirnoff #define TEST_RET_LEN_NORETURN(rl) \ 1943340d773SGleb Smirnoff if (rl < 0) { goto trunc; } 1953340d773SGleb Smirnoff 1963340d773SGleb Smirnoff /* 1973340d773SGleb Smirnoff * RESP_PRINT_SEGMENT 1983340d773SGleb Smirnoff * Prints a segment in the form of: ' "<stuff>"\n" 1993340d773SGleb Smirnoff * Assumes the data has already been verified as present. 2003340d773SGleb Smirnoff */ 2013340d773SGleb Smirnoff #define RESP_PRINT_SEGMENT(_ndo, _bp, _len) \ 202ee67461eSJoseph Mingrone ND_PRINT(" \""); \ 203ee67461eSJoseph Mingrone if (nd_printn(_ndo, _bp, _len, _ndo->ndo_snapend)) \ 2043340d773SGleb Smirnoff goto trunc; \ 2053340d773SGleb Smirnoff fn_print_char(_ndo, '"'); 2063340d773SGleb Smirnoff 2073340d773SGleb Smirnoff void 2083340d773SGleb Smirnoff resp_print(netdissect_options *ndo, const u_char *bp, u_int length) 2093340d773SGleb Smirnoff { 210ee67461eSJoseph Mingrone int ret_len = 0; 2113340d773SGleb Smirnoff 212ee67461eSJoseph Mingrone ndo->ndo_protocol = "resp"; 2133340d773SGleb Smirnoff 214ee67461eSJoseph Mingrone ND_PRINT(": RESP"); 215ee67461eSJoseph Mingrone while (length > 0) { 2163340d773SGleb Smirnoff /* 2173340d773SGleb Smirnoff * This block supports redis pipelining. 2183340d773SGleb Smirnoff * For example, multiple operations can be pipelined within the same string: 2193340d773SGleb 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" 2203340d773SGleb Smirnoff * or 2213340d773SGleb Smirnoff * "PING\r\nPING\r\nPING\r\n" 2223340d773SGleb Smirnoff * In order to handle this case, we must try and parse 'bp' until 2233340d773SGleb Smirnoff * 'length' bytes have been processed or we reach a trunc condition. 2243340d773SGleb Smirnoff */ 225ee67461eSJoseph Mingrone ret_len = resp_parse(ndo, bp, length); 2263340d773SGleb Smirnoff TEST_RET_LEN_NORETURN(ret_len); 2273340d773SGleb Smirnoff bp += ret_len; 228ee67461eSJoseph Mingrone length -= ret_len; 2293340d773SGleb Smirnoff } 2303340d773SGleb Smirnoff 2313340d773SGleb Smirnoff return; 2323340d773SGleb Smirnoff 2333340d773SGleb Smirnoff trunc: 234ee67461eSJoseph Mingrone nd_print_trunc(ndo); 2353340d773SGleb Smirnoff } 2363340d773SGleb Smirnoff 2373340d773SGleb Smirnoff static int 238ee67461eSJoseph Mingrone resp_parse(netdissect_options *ndo, const u_char *bp, int length) 2393340d773SGleb Smirnoff { 2403340d773SGleb Smirnoff u_char op; 2413340d773SGleb Smirnoff int ret_len; 2423340d773SGleb Smirnoff 2433340d773SGleb Smirnoff LCHECK2(length, 1); 244ee67461eSJoseph Mingrone op = GET_U_1(bp); 2453340d773SGleb Smirnoff 2463340d773SGleb Smirnoff /* bp now points to the op, so these routines must skip it */ 2473340d773SGleb Smirnoff switch(op) { 2483340d773SGleb Smirnoff case RESP_SIMPLE_STRING: ret_len = resp_print_simple_string(ndo, bp, length); break; 2493340d773SGleb Smirnoff case RESP_INTEGER: ret_len = resp_print_integer(ndo, bp, length); break; 2503340d773SGleb Smirnoff case RESP_ERROR: ret_len = resp_print_error(ndo, bp, length); break; 2513340d773SGleb Smirnoff case RESP_BULK_STRING: ret_len = resp_print_bulk_string(ndo, bp, length); break; 2523340d773SGleb Smirnoff case RESP_ARRAY: ret_len = resp_print_bulk_array(ndo, bp, length); break; 2533340d773SGleb Smirnoff default: ret_len = resp_print_inline(ndo, bp, length); break; 2543340d773SGleb Smirnoff } 2553340d773SGleb Smirnoff 2563340d773SGleb Smirnoff /* 2573340d773SGleb Smirnoff * This gives up with a "truncated" indicator for all errors, 2583340d773SGleb Smirnoff * including invalid packet errors; that's what we want, as 2593340d773SGleb Smirnoff * we have to give up on further parsing in that case. 2603340d773SGleb Smirnoff */ 2613340d773SGleb Smirnoff TEST_RET_LEN(ret_len); 2623340d773SGleb Smirnoff 2633340d773SGleb Smirnoff trunc: 2643340d773SGleb Smirnoff return (-1); 2653340d773SGleb Smirnoff } 2663340d773SGleb Smirnoff 2673340d773SGleb Smirnoff static int 268ee67461eSJoseph Mingrone resp_print_simple_string(netdissect_options *ndo, const u_char *bp, int length) { 2693340d773SGleb Smirnoff return resp_print_string_error_integer(ndo, bp, length); 2703340d773SGleb Smirnoff } 2713340d773SGleb Smirnoff 2723340d773SGleb Smirnoff static int 273ee67461eSJoseph Mingrone resp_print_integer(netdissect_options *ndo, const u_char *bp, int length) { 2743340d773SGleb Smirnoff return resp_print_string_error_integer(ndo, bp, length); 2753340d773SGleb Smirnoff } 2763340d773SGleb Smirnoff 2773340d773SGleb Smirnoff static int 278ee67461eSJoseph Mingrone resp_print_error(netdissect_options *ndo, const u_char *bp, int length) { 2793340d773SGleb Smirnoff return resp_print_string_error_integer(ndo, bp, length); 2803340d773SGleb Smirnoff } 2813340d773SGleb Smirnoff 2823340d773SGleb Smirnoff static int 283ee67461eSJoseph Mingrone resp_print_string_error_integer(netdissect_options *ndo, const u_char *bp, int length) { 2843340d773SGleb Smirnoff int length_cur = length, len, ret_len; 2853340d773SGleb Smirnoff const u_char *bp_ptr; 2863340d773SGleb Smirnoff 2873340d773SGleb Smirnoff /* bp points to the op; skip it */ 2883340d773SGleb Smirnoff SKIP_OPCODE(bp, length_cur); 2893340d773SGleb Smirnoff bp_ptr = bp; 2903340d773SGleb Smirnoff 2913340d773SGleb Smirnoff /* 2923340d773SGleb Smirnoff * bp now prints past the (+-;) opcode, so it's pointing to the first 2933340d773SGleb Smirnoff * character of the string (which could be numeric). 2943340d773SGleb Smirnoff * +OK\r\n 2953340d773SGleb Smirnoff * -ERR ...\r\n 2963340d773SGleb Smirnoff * :02912309\r\n 2973340d773SGleb Smirnoff * 2983340d773SGleb Smirnoff * Find the \r\n with FIND_CRLF(). 2993340d773SGleb Smirnoff */ 3003340d773SGleb Smirnoff FIND_CRLF(bp_ptr, length_cur); 3013340d773SGleb Smirnoff 3023340d773SGleb Smirnoff /* 3033340d773SGleb Smirnoff * bp_ptr points to the \r\n, so bp_ptr - bp is the length of text 3043340d773SGleb Smirnoff * preceding the \r\n. That includes the opcode, so don't print 3053340d773SGleb Smirnoff * that. 3063340d773SGleb Smirnoff */ 307*0a7e5f1fSJoseph Mingrone len = ND_BYTES_BETWEEN(bp, bp_ptr); 3083340d773SGleb Smirnoff RESP_PRINT_SEGMENT(ndo, bp, len); 3093340d773SGleb Smirnoff ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/; 3103340d773SGleb Smirnoff 3113340d773SGleb Smirnoff TEST_RET_LEN(ret_len); 3123340d773SGleb Smirnoff 3133340d773SGleb Smirnoff trunc: 3143340d773SGleb Smirnoff return (-1); 3153340d773SGleb Smirnoff } 3163340d773SGleb Smirnoff 3173340d773SGleb Smirnoff static int 318ee67461eSJoseph Mingrone resp_print_bulk_string(netdissect_options *ndo, const u_char *bp, int length) { 3193340d773SGleb Smirnoff int length_cur = length, string_len; 3203340d773SGleb Smirnoff 3213340d773SGleb Smirnoff /* bp points to the op; skip it */ 3223340d773SGleb Smirnoff SKIP_OPCODE(bp, length_cur); 3233340d773SGleb Smirnoff 3243340d773SGleb Smirnoff /* <length>\r\n */ 3253340d773SGleb Smirnoff GET_LENGTH(ndo, length_cur, bp, string_len); 3263340d773SGleb Smirnoff 3273340d773SGleb Smirnoff if (string_len >= 0) { 3283340d773SGleb Smirnoff /* Byte string of length string_len, starting at bp */ 3293340d773SGleb Smirnoff if (string_len == 0) 3303340d773SGleb Smirnoff resp_print_empty(ndo); 3313340d773SGleb Smirnoff else { 3323340d773SGleb Smirnoff LCHECK2(length_cur, string_len); 333ee67461eSJoseph Mingrone ND_TCHECK_LEN(bp, string_len); 3343340d773SGleb Smirnoff RESP_PRINT_SEGMENT(ndo, bp, string_len); 3353340d773SGleb Smirnoff bp += string_len; 3363340d773SGleb Smirnoff length_cur -= string_len; 3373340d773SGleb Smirnoff } 3383340d773SGleb Smirnoff 3393340d773SGleb Smirnoff /* 3403340d773SGleb Smirnoff * Find the \r\n at the end of the string and skip past it. 3413340d773SGleb Smirnoff * XXX - report an error if the \r\n isn't immediately after 3423340d773SGleb Smirnoff * the item? 3433340d773SGleb Smirnoff */ 3443340d773SGleb Smirnoff FIND_CRLF(bp, length_cur); 3453340d773SGleb Smirnoff CONSUME_CRLF(bp, length_cur); 3463340d773SGleb Smirnoff } else { 3473340d773SGleb Smirnoff /* null, truncated, or invalid for some reason */ 3483340d773SGleb Smirnoff switch(string_len) { 3493340d773SGleb Smirnoff case (-1): resp_print_null(ndo); break; 3503340d773SGleb Smirnoff case (-2): goto trunc; 3513340d773SGleb Smirnoff case (-3): resp_print_length_too_large(ndo); break; 3523340d773SGleb Smirnoff case (-4): resp_print_length_negative(ndo); break; 3533340d773SGleb Smirnoff default: resp_print_invalid(ndo); break; 3543340d773SGleb Smirnoff } 3553340d773SGleb Smirnoff } 3563340d773SGleb Smirnoff 3573340d773SGleb Smirnoff return (length - length_cur); 3583340d773SGleb Smirnoff 3593340d773SGleb Smirnoff trunc: 3603340d773SGleb Smirnoff return (-1); 3613340d773SGleb Smirnoff } 3623340d773SGleb Smirnoff 3633340d773SGleb Smirnoff static int 364ee67461eSJoseph Mingrone resp_print_bulk_array(netdissect_options *ndo, const u_char *bp, int length) { 3653340d773SGleb Smirnoff u_int length_cur = length; 3663340d773SGleb Smirnoff int array_len, i, ret_len; 3673340d773SGleb Smirnoff 3683340d773SGleb Smirnoff /* bp points to the op; skip it */ 3693340d773SGleb Smirnoff SKIP_OPCODE(bp, length_cur); 3703340d773SGleb Smirnoff 3713340d773SGleb Smirnoff /* <array_length>\r\n */ 3723340d773SGleb Smirnoff GET_LENGTH(ndo, length_cur, bp, array_len); 3733340d773SGleb Smirnoff 3743340d773SGleb Smirnoff if (array_len > 0) { 3753340d773SGleb Smirnoff /* non empty array */ 3763340d773SGleb Smirnoff for (i = 0; i < array_len; i++) { 3773340d773SGleb Smirnoff ret_len = resp_parse(ndo, bp, length_cur); 3783340d773SGleb Smirnoff 3793340d773SGleb Smirnoff TEST_RET_LEN_NORETURN(ret_len); 3803340d773SGleb Smirnoff 3813340d773SGleb Smirnoff bp += ret_len; 3823340d773SGleb Smirnoff length_cur -= ret_len; 3833340d773SGleb Smirnoff } 3843340d773SGleb Smirnoff } else { 3853340d773SGleb Smirnoff /* empty, null, truncated, or invalid */ 3863340d773SGleb Smirnoff switch(array_len) { 3873340d773SGleb Smirnoff case 0: resp_print_empty(ndo); break; 3883340d773SGleb Smirnoff case (-1): resp_print_null(ndo); break; 3893340d773SGleb Smirnoff case (-2): goto trunc; 3903340d773SGleb Smirnoff case (-3): resp_print_length_too_large(ndo); break; 3913340d773SGleb Smirnoff case (-4): resp_print_length_negative(ndo); break; 3923340d773SGleb Smirnoff default: resp_print_invalid(ndo); break; 3933340d773SGleb Smirnoff } 3943340d773SGleb Smirnoff } 3953340d773SGleb Smirnoff 3963340d773SGleb Smirnoff return (length - length_cur); 3973340d773SGleb Smirnoff 3983340d773SGleb Smirnoff trunc: 3993340d773SGleb Smirnoff return (-1); 4003340d773SGleb Smirnoff } 4013340d773SGleb Smirnoff 4023340d773SGleb Smirnoff static int 403ee67461eSJoseph Mingrone resp_print_inline(netdissect_options *ndo, const u_char *bp, int length) { 4043340d773SGleb Smirnoff int length_cur = length; 4053340d773SGleb Smirnoff int len; 4063340d773SGleb Smirnoff const u_char *bp_ptr; 4073340d773SGleb Smirnoff 4083340d773SGleb Smirnoff /* 4093340d773SGleb Smirnoff * Inline commands are simply 'strings' followed by \r or \n or both. 4103340d773SGleb Smirnoff * Redis will do its best to split/parse these strings. 4113340d773SGleb Smirnoff * This feature of redis is implemented to support the ability of 4123340d773SGleb Smirnoff * command parsing from telnet/nc sessions etc. 4133340d773SGleb Smirnoff * 4143340d773SGleb Smirnoff * <string><\r||\n||\r\n...> 4153340d773SGleb Smirnoff */ 4163340d773SGleb Smirnoff 4173340d773SGleb Smirnoff /* 4183340d773SGleb Smirnoff * Skip forward past any leading \r, \n, or \r\n. 4193340d773SGleb Smirnoff */ 4203340d773SGleb Smirnoff CONSUME_CR_OR_LF(bp, length_cur); 4213340d773SGleb Smirnoff bp_ptr = bp; 4223340d773SGleb Smirnoff 4233340d773SGleb Smirnoff /* 4243340d773SGleb Smirnoff * Scan forward looking for \r or \n. 4253340d773SGleb Smirnoff */ 4263340d773SGleb Smirnoff FIND_CR_OR_LF(bp_ptr, length_cur); 4273340d773SGleb Smirnoff 4283340d773SGleb Smirnoff /* 4293340d773SGleb Smirnoff * Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the 430ee67461eSJoseph Mingrone * Length of the line text that precedes it. Print it. 4313340d773SGleb Smirnoff */ 432*0a7e5f1fSJoseph Mingrone len = ND_BYTES_BETWEEN(bp, bp_ptr); 4333340d773SGleb Smirnoff RESP_PRINT_SEGMENT(ndo, bp, len); 4343340d773SGleb Smirnoff 4353340d773SGleb Smirnoff /* 4363340d773SGleb Smirnoff * Skip forward past the \r, \n, or \r\n. 4373340d773SGleb Smirnoff */ 4383340d773SGleb Smirnoff CONSUME_CR_OR_LF(bp_ptr, length_cur); 4393340d773SGleb Smirnoff 4403340d773SGleb Smirnoff /* 4413340d773SGleb Smirnoff * Return the number of bytes we processed. 4423340d773SGleb Smirnoff */ 4433340d773SGleb Smirnoff return (length - length_cur); 4443340d773SGleb Smirnoff 4453340d773SGleb Smirnoff trunc: 4463340d773SGleb Smirnoff return (-1); 4473340d773SGleb Smirnoff } 4483340d773SGleb Smirnoff 4493340d773SGleb Smirnoff static int 450ee67461eSJoseph Mingrone resp_get_length(netdissect_options *ndo, const u_char *bp, int len, const u_char **endp) 4513340d773SGleb Smirnoff { 4523340d773SGleb Smirnoff int result; 4533340d773SGleb Smirnoff u_char c; 4543340d773SGleb Smirnoff int saw_digit; 4553340d773SGleb Smirnoff int neg; 4563340d773SGleb Smirnoff int too_large; 4573340d773SGleb Smirnoff 4583340d773SGleb Smirnoff if (len == 0) 4593340d773SGleb Smirnoff goto trunc; 4603340d773SGleb Smirnoff too_large = 0; 4613340d773SGleb Smirnoff neg = 0; 462ee67461eSJoseph Mingrone if (GET_U_1(bp) == '-') { 4633340d773SGleb Smirnoff neg = 1; 4643340d773SGleb Smirnoff bp++; 4653340d773SGleb Smirnoff len--; 4663340d773SGleb Smirnoff } 4673340d773SGleb Smirnoff result = 0; 4683340d773SGleb Smirnoff saw_digit = 0; 4693340d773SGleb Smirnoff 4703340d773SGleb Smirnoff for (;;) { 4713340d773SGleb Smirnoff if (len == 0) 4723340d773SGleb Smirnoff goto trunc; 473ee67461eSJoseph Mingrone c = GET_U_1(bp); 4743340d773SGleb Smirnoff if (!(c >= '0' && c <= '9')) { 4750bff6a5aSEd Maste if (!saw_digit) { 4760bff6a5aSEd Maste bp++; 4773340d773SGleb Smirnoff goto invalid; 4780bff6a5aSEd Maste } 4793340d773SGleb Smirnoff break; 4803340d773SGleb Smirnoff } 4813340d773SGleb Smirnoff c -= '0'; 4823340d773SGleb Smirnoff if (result > (INT_MAX / 10)) { 4833340d773SGleb Smirnoff /* This will overflow an int when we multiply it by 10. */ 4843340d773SGleb Smirnoff too_large = 1; 4853340d773SGleb Smirnoff } else { 4863340d773SGleb Smirnoff result *= 10; 4870bff6a5aSEd Maste if (result == ((INT_MAX / 10) * 10) && c > (INT_MAX % 10)) { 4883340d773SGleb Smirnoff /* This will overflow an int when we add c */ 4893340d773SGleb Smirnoff too_large = 1; 4903340d773SGleb Smirnoff } else 4913340d773SGleb Smirnoff result += c; 4923340d773SGleb Smirnoff } 4933340d773SGleb Smirnoff bp++; 4943340d773SGleb Smirnoff len--; 4953340d773SGleb Smirnoff saw_digit = 1; 4963340d773SGleb Smirnoff } 4973340d773SGleb Smirnoff 4983340d773SGleb Smirnoff /* 4990bff6a5aSEd Maste * OK, we found a non-digit character. It should be a \r, followed 5000bff6a5aSEd Maste * by a \n. 5013340d773SGleb Smirnoff */ 502ee67461eSJoseph Mingrone if (GET_U_1(bp) != '\r') { 5030bff6a5aSEd Maste bp++; 5043340d773SGleb Smirnoff goto invalid; 5050bff6a5aSEd Maste } 5063340d773SGleb Smirnoff bp++; 5073340d773SGleb Smirnoff len--; 5083340d773SGleb Smirnoff if (len == 0) 5093340d773SGleb Smirnoff goto trunc; 510ee67461eSJoseph Mingrone if (GET_U_1(bp) != '\n') { 5110bff6a5aSEd Maste bp++; 5123340d773SGleb Smirnoff goto invalid; 5130bff6a5aSEd Maste } 5143340d773SGleb Smirnoff bp++; 5153340d773SGleb Smirnoff len--; 5163340d773SGleb Smirnoff *endp = bp; 5173340d773SGleb Smirnoff if (neg) { 5183340d773SGleb Smirnoff /* -1 means "null", anything else is invalid */ 5193340d773SGleb Smirnoff if (too_large || result != 1) 5203340d773SGleb Smirnoff return (-4); 5213340d773SGleb Smirnoff result = -1; 5223340d773SGleb Smirnoff } 5233340d773SGleb Smirnoff return (too_large ? -3 : result); 5243340d773SGleb Smirnoff 5253340d773SGleb Smirnoff trunc: 5260bff6a5aSEd Maste *endp = bp; 5273340d773SGleb Smirnoff return (-2); 5283340d773SGleb Smirnoff 5293340d773SGleb Smirnoff invalid: 5300bff6a5aSEd Maste *endp = bp; 5313340d773SGleb Smirnoff return (-5); 5323340d773SGleb Smirnoff } 533