123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <netdissect-stdinc.h>
- #include "netdissect.h"
- #include <limits.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include "extract.h"
- static const char tstr[] = " [|RESP]";
- #define RESP_SIMPLE_STRING '+'
- #define RESP_ERROR '-'
- #define RESP_INTEGER ':'
- #define RESP_BULK_STRING '$'
- #define RESP_ARRAY '*'
- #define resp_print_empty(ndo) ND_PRINT((ndo, " empty"))
- #define resp_print_null(ndo) ND_PRINT((ndo, " null"))
- #define resp_print_length_too_large(ndo) ND_PRINT((ndo, " length too large"))
- #define resp_print_length_negative(ndo) ND_PRINT((ndo, " length negative and not -1"))
- #define resp_print_invalid(ndo) ND_PRINT((ndo, " invalid"))
- void resp_print(netdissect_options *, const u_char *, u_int);
- static int resp_parse(netdissect_options *, register const u_char *, int);
- static int resp_print_string_error_integer(netdissect_options *, register const u_char *, int);
- static int resp_print_simple_string(netdissect_options *, register const u_char *, int);
- static int resp_print_integer(netdissect_options *, register const u_char *, int);
- static int resp_print_error(netdissect_options *, register const u_char *, int);
- static int resp_print_bulk_string(netdissect_options *, register const u_char *, int);
- static int resp_print_bulk_array(netdissect_options *, register const u_char *, int);
- static int resp_print_inline(netdissect_options *, register const u_char *, int);
- static int resp_get_length(netdissect_options *, register const u_char *, int, const u_char **);
- #define LCHECK2(_tot_len, _len) \
- { \
- if (_tot_len < _len) \
- goto trunc; \
- }
- #define LCHECK(_tot_len) LCHECK2(_tot_len, 1)
- #define FIND_CRLF(_ptr, _len) \
- for (;;) { \
- LCHECK2(_len, 2); \
- ND_TCHECK2(*_ptr, 2); \
- if (*_ptr == '\r' && *(_ptr+1) == '\n') \
- break; \
- _ptr++; \
- _len--; \
- }
- #define CONSUME_CRLF(_ptr, _len) \
- _ptr += 2; \
- _len -= 2;
- #define FIND_CR_OR_LF(_ptr, _len) \
- for (;;) { \
- LCHECK(_len); \
- ND_TCHECK(*_ptr); \
- if (*_ptr == '\r' || *_ptr == '\n') \
- break; \
- _ptr++; \
- _len--; \
- }
- #define CONSUME_CR_OR_LF(_ptr, _len) \
- { \
- int _found_cr_or_lf = 0; \
- for (;;) { \
-
- \
- if (_len == 0 || !ND_TTEST(*_ptr)) { \
-
- \
- if (_found_cr_or_lf) { \
-
- \
- break; \
- } \
-
- \
- goto trunc; \
- } \
- if (*_ptr != '\r' && *_ptr != '\n') \
- break; \
- _found_cr_or_lf = 1; \
- _ptr++; \
- _len--; \
- } \
- }
- #define SKIP_OPCODE(_ptr, _tot_len) \
- _ptr++; \
- _tot_len--;
- #define GET_LENGTH(_ndo, _tot_len, _ptr, _len) \
- { \
- const u_char *_endp; \
- _len = resp_get_length(_ndo, _ptr, _tot_len, &_endp); \
- _tot_len -= (_endp - _ptr); \
- _ptr = _endp; \
- }
- #define TEST_RET_LEN(rl) \
- if (rl < 0) { goto trunc; } else { return rl; }
- #define TEST_RET_LEN_NORETURN(rl) \
- if (rl < 0) { goto trunc; }
- #define RESP_PRINT_SEGMENT(_ndo, _bp, _len) \
- ND_PRINT((_ndo, " \"")); \
- if (fn_printn(_ndo, _bp, _len, _ndo->ndo_snapend)) \
- goto trunc; \
- fn_print_char(_ndo, '"');
- void
- resp_print(netdissect_options *ndo, const u_char *bp, u_int length)
- {
- int ret_len = 0, length_cur = length;
- if(!bp || length <= 0)
- return;
- ND_PRINT((ndo, ": RESP"));
- while (length_cur > 0) {
-
- ret_len = resp_parse(ndo, bp, length_cur);
- TEST_RET_LEN_NORETURN(ret_len);
- bp += ret_len;
- length_cur -= ret_len;
- }
- return;
- trunc:
- ND_PRINT((ndo, "%s", tstr));
- }
- static int
- resp_parse(netdissect_options *ndo, register const u_char *bp, int length)
- {
- u_char op;
- int ret_len;
- LCHECK2(length, 1);
- ND_TCHECK(*bp);
- op = *bp;
-
- switch(op) {
- case RESP_SIMPLE_STRING: ret_len = resp_print_simple_string(ndo, bp, length); break;
- case RESP_INTEGER: ret_len = resp_print_integer(ndo, bp, length); break;
- case RESP_ERROR: ret_len = resp_print_error(ndo, bp, length); break;
- case RESP_BULK_STRING: ret_len = resp_print_bulk_string(ndo, bp, length); break;
- case RESP_ARRAY: ret_len = resp_print_bulk_array(ndo, bp, length); break;
- default: ret_len = resp_print_inline(ndo, bp, length); break;
- }
-
- TEST_RET_LEN(ret_len);
- trunc:
- return (-1);
- }
- static int
- resp_print_simple_string(netdissect_options *ndo, register const u_char *bp, int length) {
- return resp_print_string_error_integer(ndo, bp, length);
- }
- static int
- resp_print_integer(netdissect_options *ndo, register const u_char *bp, int length) {
- return resp_print_string_error_integer(ndo, bp, length);
- }
- static int
- resp_print_error(netdissect_options *ndo, register const u_char *bp, int length) {
- return resp_print_string_error_integer(ndo, bp, length);
- }
- static int
- resp_print_string_error_integer(netdissect_options *ndo, register const u_char *bp, int length) {
- int length_cur = length, len, ret_len;
- const u_char *bp_ptr;
-
- SKIP_OPCODE(bp, length_cur);
- bp_ptr = bp;
-
- FIND_CRLF(bp_ptr, length_cur);
-
- len = (bp_ptr - bp);
- RESP_PRINT_SEGMENT(ndo, bp, len);
- ret_len = 1 + len + 2 ;
- TEST_RET_LEN(ret_len);
- trunc:
- return (-1);
- }
- static int
- resp_print_bulk_string(netdissect_options *ndo, register const u_char *bp, int length) {
- int length_cur = length, string_len;
-
- SKIP_OPCODE(bp, length_cur);
-
- GET_LENGTH(ndo, length_cur, bp, string_len);
- if (string_len >= 0) {
-
- if (string_len == 0)
- resp_print_empty(ndo);
- else {
- LCHECK2(length_cur, string_len);
- ND_TCHECK2(*bp, string_len);
- RESP_PRINT_SEGMENT(ndo, bp, string_len);
- bp += string_len;
- length_cur -= string_len;
- }
-
- FIND_CRLF(bp, length_cur);
- CONSUME_CRLF(bp, length_cur);
- } else {
-
- switch(string_len) {
- case (-1): resp_print_null(ndo); break;
- case (-2): goto trunc;
- case (-3): resp_print_length_too_large(ndo); break;
- case (-4): resp_print_length_negative(ndo); break;
- default: resp_print_invalid(ndo); break;
- }
- }
- return (length - length_cur);
- trunc:
- return (-1);
- }
- static int
- resp_print_bulk_array(netdissect_options *ndo, register const u_char *bp, int length) {
- u_int length_cur = length;
- int array_len, i, ret_len;
-
- SKIP_OPCODE(bp, length_cur);
-
- GET_LENGTH(ndo, length_cur, bp, array_len);
- if (array_len > 0) {
-
- for (i = 0; i < array_len; i++) {
- ret_len = resp_parse(ndo, bp, length_cur);
- TEST_RET_LEN_NORETURN(ret_len);
- bp += ret_len;
- length_cur -= ret_len;
- }
- } else {
-
- switch(array_len) {
- case 0: resp_print_empty(ndo); break;
- case (-1): resp_print_null(ndo); break;
- case (-2): goto trunc;
- case (-3): resp_print_length_too_large(ndo); break;
- case (-4): resp_print_length_negative(ndo); break;
- default: resp_print_invalid(ndo); break;
- }
- }
- return (length - length_cur);
- trunc:
- return (-1);
- }
- static int
- resp_print_inline(netdissect_options *ndo, register const u_char *bp, int length) {
- int length_cur = length;
- int len;
- const u_char *bp_ptr;
-
-
- CONSUME_CR_OR_LF(bp, length_cur);
- bp_ptr = bp;
-
- FIND_CR_OR_LF(bp_ptr, length_cur);
-
- len = (bp_ptr - bp);
- RESP_PRINT_SEGMENT(ndo, bp, len);
-
- CONSUME_CR_OR_LF(bp_ptr, length_cur);
-
- return (length - length_cur);
- trunc:
- return (-1);
- }
- static int
- resp_get_length(netdissect_options *ndo, register const u_char *bp, int len, const u_char **endp)
- {
- int result;
- u_char c;
- int saw_digit;
- int neg;
- int too_large;
- if (len == 0)
- goto trunc;
- ND_TCHECK(*bp);
- too_large = 0;
- neg = 0;
- if (*bp == '-') {
- neg = 1;
- bp++;
- len--;
- }
- result = 0;
- saw_digit = 0;
- for (;;) {
- if (len == 0)
- goto trunc;
- ND_TCHECK(*bp);
- c = *bp;
- if (!(c >= '0' && c <= '9')) {
- if (!saw_digit) {
- bp++;
- goto invalid;
- }
- break;
- }
- c -= '0';
- if (result > (INT_MAX / 10)) {
-
- too_large = 1;
- } else {
- result *= 10;
- if (result == ((INT_MAX / 10) * 10) && c > (INT_MAX % 10)) {
-
- too_large = 1;
- } else
- result += c;
- }
- bp++;
- len--;
- saw_digit = 1;
- }
-
- if (*bp != '\r') {
- bp++;
- goto invalid;
- }
- bp++;
- len--;
- if (len == 0)
- goto trunc;
- ND_TCHECK(*bp);
- if (*bp != '\n') {
- bp++;
- goto invalid;
- }
- bp++;
- len--;
- *endp = bp;
- if (neg) {
-
- if (too_large || result != 1)
- return (-4);
- result = -1;
- }
- return (too_large ? -3 : result);
- trunc:
- *endp = bp;
- return (-2);
- invalid:
- *endp = bp;
- return (-5);
- }
|