print-resp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (c) 2015 The TCPDUMP project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  26. *
  27. * Initial contribution by Andrew Darqui (andrew.darqui@gmail.com).
  28. */
  29. /* \summary: REdis Serialization Protocol (RESP) printer */
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <netdissect-stdinc.h>
  34. #include "netdissect.h"
  35. #include <limits.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include "extract.h"
  40. static const char tstr[] = " [|RESP]";
  41. /*
  42. * For information regarding RESP, see: http://redis.io/topics/protocol
  43. */
  44. #define RESP_SIMPLE_STRING '+'
  45. #define RESP_ERROR '-'
  46. #define RESP_INTEGER ':'
  47. #define RESP_BULK_STRING '$'
  48. #define RESP_ARRAY '*'
  49. #define resp_print_empty(ndo) ND_PRINT((ndo, " empty"))
  50. #define resp_print_null(ndo) ND_PRINT((ndo, " null"))
  51. #define resp_print_length_too_large(ndo) ND_PRINT((ndo, " length too large"))
  52. #define resp_print_length_negative(ndo) ND_PRINT((ndo, " length negative and not -1"))
  53. #define resp_print_invalid(ndo) ND_PRINT((ndo, " invalid"))
  54. void resp_print(netdissect_options *, const u_char *, u_int);
  55. static int resp_parse(netdissect_options *, register const u_char *, int);
  56. static int resp_print_string_error_integer(netdissect_options *, register const u_char *, int);
  57. static int resp_print_simple_string(netdissect_options *, register const u_char *, int);
  58. static int resp_print_integer(netdissect_options *, register const u_char *, int);
  59. static int resp_print_error(netdissect_options *, register const u_char *, int);
  60. static int resp_print_bulk_string(netdissect_options *, register const u_char *, int);
  61. static int resp_print_bulk_array(netdissect_options *, register const u_char *, int);
  62. static int resp_print_inline(netdissect_options *, register const u_char *, int);
  63. static int resp_get_length(netdissect_options *, register const u_char *, int, const u_char **);
  64. #define LCHECK2(_tot_len, _len) \
  65. { \
  66. if (_tot_len < _len) \
  67. goto trunc; \
  68. }
  69. #define LCHECK(_tot_len) LCHECK2(_tot_len, 1)
  70. /*
  71. * FIND_CRLF:
  72. * Attempts to move our 'ptr' forward until a \r\n is found,
  73. * while also making sure we don't exceed the buffer '_len'
  74. * or go past the end of the captured data.
  75. * If we exceed or go past the end of the captured data,
  76. * jump to trunc.
  77. */
  78. #define FIND_CRLF(_ptr, _len) \
  79. for (;;) { \
  80. LCHECK2(_len, 2); \
  81. ND_TCHECK2(*_ptr, 2); \
  82. if (*_ptr == '\r' && *(_ptr+1) == '\n') \
  83. break; \
  84. _ptr++; \
  85. _len--; \
  86. }
  87. /*
  88. * CONSUME_CRLF
  89. * Consume a CRLF that we've just found.
  90. */
  91. #define CONSUME_CRLF(_ptr, _len) \
  92. _ptr += 2; \
  93. _len -= 2;
  94. /*
  95. * FIND_CR_OR_LF
  96. * Attempts to move our '_ptr' forward until a \r or \n is found,
  97. * while also making sure we don't exceed the buffer '_len'
  98. * or go past the end of the captured data.
  99. * If we exceed or go past the end of the captured data,
  100. * jump to trunc.
  101. */
  102. #define FIND_CR_OR_LF(_ptr, _len) \
  103. for (;;) { \
  104. LCHECK(_len); \
  105. ND_TCHECK(*_ptr); \
  106. if (*_ptr == '\r' || *_ptr == '\n') \
  107. break; \
  108. _ptr++; \
  109. _len--; \
  110. }
  111. /*
  112. * CONSUME_CR_OR_LF
  113. * Consume all consecutive \r and \n bytes.
  114. * If we exceed '_len' or go past the end of the captured data,
  115. * jump to trunc.
  116. */
  117. #define CONSUME_CR_OR_LF(_ptr, _len) \
  118. { \
  119. int _found_cr_or_lf = 0; \
  120. for (;;) { \
  121. /* \
  122. * Have we hit the end of data? \
  123. */ \
  124. if (_len == 0 || !ND_TTEST(*_ptr)) { \
  125. /* \
  126. * Yes. Have we seen a \r \
  127. * or \n? \
  128. */ \
  129. if (_found_cr_or_lf) { \
  130. /* \
  131. * Yes. Just stop. \
  132. */ \
  133. break; \
  134. } \
  135. /* \
  136. * No. We ran out of packet. \
  137. */ \
  138. goto trunc; \
  139. } \
  140. if (*_ptr != '\r' && *_ptr != '\n') \
  141. break; \
  142. _found_cr_or_lf = 1; \
  143. _ptr++; \
  144. _len--; \
  145. } \
  146. }
  147. /*
  148. * SKIP_OPCODE
  149. * Skip over the opcode character.
  150. * The opcode has already been fetched, so we know it's there, and don't
  151. * need to do any checks.
  152. */
  153. #define SKIP_OPCODE(_ptr, _tot_len) \
  154. _ptr++; \
  155. _tot_len--;
  156. /*
  157. * GET_LENGTH
  158. * Get a bulk string or array length.
  159. */
  160. #define GET_LENGTH(_ndo, _tot_len, _ptr, _len) \
  161. { \
  162. const u_char *_endp; \
  163. _len = resp_get_length(_ndo, _ptr, _tot_len, &_endp); \
  164. _tot_len -= (_endp - _ptr); \
  165. _ptr = _endp; \
  166. }
  167. /*
  168. * TEST_RET_LEN
  169. * If ret_len is < 0, jump to the trunc tag which returns (-1)
  170. * and 'bubbles up' to printing tstr. Otherwise, return ret_len.
  171. */
  172. #define TEST_RET_LEN(rl) \
  173. if (rl < 0) { goto trunc; } else { return rl; }
  174. /*
  175. * TEST_RET_LEN_NORETURN
  176. * If ret_len is < 0, jump to the trunc tag which returns (-1)
  177. * and 'bubbles up' to printing tstr. Otherwise, continue onward.
  178. */
  179. #define TEST_RET_LEN_NORETURN(rl) \
  180. if (rl < 0) { goto trunc; }
  181. /*
  182. * RESP_PRINT_SEGMENT
  183. * Prints a segment in the form of: ' "<stuff>"\n"
  184. * Assumes the data has already been verified as present.
  185. */
  186. #define RESP_PRINT_SEGMENT(_ndo, _bp, _len) \
  187. ND_PRINT((_ndo, " \"")); \
  188. if (fn_printn(_ndo, _bp, _len, _ndo->ndo_snapend)) \
  189. goto trunc; \
  190. fn_print_char(_ndo, '"');
  191. void
  192. resp_print(netdissect_options *ndo, const u_char *bp, u_int length)
  193. {
  194. int ret_len = 0, length_cur = length;
  195. if(!bp || length <= 0)
  196. return;
  197. ND_PRINT((ndo, ": RESP"));
  198. while (length_cur > 0) {
  199. /*
  200. * This block supports redis pipelining.
  201. * For example, multiple operations can be pipelined within the same string:
  202. * "*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"
  203. * or
  204. * "PING\r\nPING\r\nPING\r\n"
  205. * In order to handle this case, we must try and parse 'bp' until
  206. * 'length' bytes have been processed or we reach a trunc condition.
  207. */
  208. ret_len = resp_parse(ndo, bp, length_cur);
  209. TEST_RET_LEN_NORETURN(ret_len);
  210. bp += ret_len;
  211. length_cur -= ret_len;
  212. }
  213. return;
  214. trunc:
  215. ND_PRINT((ndo, "%s", tstr));
  216. }
  217. static int
  218. resp_parse(netdissect_options *ndo, register const u_char *bp, int length)
  219. {
  220. u_char op;
  221. int ret_len;
  222. LCHECK2(length, 1);
  223. ND_TCHECK(*bp);
  224. op = *bp;
  225. /* bp now points to the op, so these routines must skip it */
  226. switch(op) {
  227. case RESP_SIMPLE_STRING: ret_len = resp_print_simple_string(ndo, bp, length); break;
  228. case RESP_INTEGER: ret_len = resp_print_integer(ndo, bp, length); break;
  229. case RESP_ERROR: ret_len = resp_print_error(ndo, bp, length); break;
  230. case RESP_BULK_STRING: ret_len = resp_print_bulk_string(ndo, bp, length); break;
  231. case RESP_ARRAY: ret_len = resp_print_bulk_array(ndo, bp, length); break;
  232. default: ret_len = resp_print_inline(ndo, bp, length); break;
  233. }
  234. /*
  235. * This gives up with a "truncated" indicator for all errors,
  236. * including invalid packet errors; that's what we want, as
  237. * we have to give up on further parsing in that case.
  238. */
  239. TEST_RET_LEN(ret_len);
  240. trunc:
  241. return (-1);
  242. }
  243. static int
  244. resp_print_simple_string(netdissect_options *ndo, register const u_char *bp, int length) {
  245. return resp_print_string_error_integer(ndo, bp, length);
  246. }
  247. static int
  248. resp_print_integer(netdissect_options *ndo, register const u_char *bp, int length) {
  249. return resp_print_string_error_integer(ndo, bp, length);
  250. }
  251. static int
  252. resp_print_error(netdissect_options *ndo, register const u_char *bp, int length) {
  253. return resp_print_string_error_integer(ndo, bp, length);
  254. }
  255. static int
  256. resp_print_string_error_integer(netdissect_options *ndo, register const u_char *bp, int length) {
  257. int length_cur = length, len, ret_len;
  258. const u_char *bp_ptr;
  259. /* bp points to the op; skip it */
  260. SKIP_OPCODE(bp, length_cur);
  261. bp_ptr = bp;
  262. /*
  263. * bp now prints past the (+-;) opcode, so it's pointing to the first
  264. * character of the string (which could be numeric).
  265. * +OK\r\n
  266. * -ERR ...\r\n
  267. * :02912309\r\n
  268. *
  269. * Find the \r\n with FIND_CRLF().
  270. */
  271. FIND_CRLF(bp_ptr, length_cur);
  272. /*
  273. * bp_ptr points to the \r\n, so bp_ptr - bp is the length of text
  274. * preceding the \r\n. That includes the opcode, so don't print
  275. * that.
  276. */
  277. len = (bp_ptr - bp);
  278. RESP_PRINT_SEGMENT(ndo, bp, len);
  279. ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/;
  280. TEST_RET_LEN(ret_len);
  281. trunc:
  282. return (-1);
  283. }
  284. static int
  285. resp_print_bulk_string(netdissect_options *ndo, register const u_char *bp, int length) {
  286. int length_cur = length, string_len;
  287. /* bp points to the op; skip it */
  288. SKIP_OPCODE(bp, length_cur);
  289. /* <length>\r\n */
  290. GET_LENGTH(ndo, length_cur, bp, string_len);
  291. if (string_len >= 0) {
  292. /* Byte string of length string_len, starting at bp */
  293. if (string_len == 0)
  294. resp_print_empty(ndo);
  295. else {
  296. LCHECK2(length_cur, string_len);
  297. ND_TCHECK2(*bp, string_len);
  298. RESP_PRINT_SEGMENT(ndo, bp, string_len);
  299. bp += string_len;
  300. length_cur -= string_len;
  301. }
  302. /*
  303. * Find the \r\n at the end of the string and skip past it.
  304. * XXX - report an error if the \r\n isn't immediately after
  305. * the item?
  306. */
  307. FIND_CRLF(bp, length_cur);
  308. CONSUME_CRLF(bp, length_cur);
  309. } else {
  310. /* null, truncated, or invalid for some reason */
  311. switch(string_len) {
  312. case (-1): resp_print_null(ndo); break;
  313. case (-2): goto trunc;
  314. case (-3): resp_print_length_too_large(ndo); break;
  315. case (-4): resp_print_length_negative(ndo); break;
  316. default: resp_print_invalid(ndo); break;
  317. }
  318. }
  319. return (length - length_cur);
  320. trunc:
  321. return (-1);
  322. }
  323. static int
  324. resp_print_bulk_array(netdissect_options *ndo, register const u_char *bp, int length) {
  325. u_int length_cur = length;
  326. int array_len, i, ret_len;
  327. /* bp points to the op; skip it */
  328. SKIP_OPCODE(bp, length_cur);
  329. /* <array_length>\r\n */
  330. GET_LENGTH(ndo, length_cur, bp, array_len);
  331. if (array_len > 0) {
  332. /* non empty array */
  333. for (i = 0; i < array_len; i++) {
  334. ret_len = resp_parse(ndo, bp, length_cur);
  335. TEST_RET_LEN_NORETURN(ret_len);
  336. bp += ret_len;
  337. length_cur -= ret_len;
  338. }
  339. } else {
  340. /* empty, null, truncated, or invalid */
  341. switch(array_len) {
  342. case 0: resp_print_empty(ndo); break;
  343. case (-1): resp_print_null(ndo); break;
  344. case (-2): goto trunc;
  345. case (-3): resp_print_length_too_large(ndo); break;
  346. case (-4): resp_print_length_negative(ndo); break;
  347. default: resp_print_invalid(ndo); break;
  348. }
  349. }
  350. return (length - length_cur);
  351. trunc:
  352. return (-1);
  353. }
  354. static int
  355. resp_print_inline(netdissect_options *ndo, register const u_char *bp, int length) {
  356. int length_cur = length;
  357. int len;
  358. const u_char *bp_ptr;
  359. /*
  360. * Inline commands are simply 'strings' followed by \r or \n or both.
  361. * Redis will do its best to split/parse these strings.
  362. * This feature of redis is implemented to support the ability of
  363. * command parsing from telnet/nc sessions etc.
  364. *
  365. * <string><\r||\n||\r\n...>
  366. */
  367. /*
  368. * Skip forward past any leading \r, \n, or \r\n.
  369. */
  370. CONSUME_CR_OR_LF(bp, length_cur);
  371. bp_ptr = bp;
  372. /*
  373. * Scan forward looking for \r or \n.
  374. */
  375. FIND_CR_OR_LF(bp_ptr, length_cur);
  376. /*
  377. * Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the
  378. * Length of the line text that preceeds it. Print it.
  379. */
  380. len = (bp_ptr - bp);
  381. RESP_PRINT_SEGMENT(ndo, bp, len);
  382. /*
  383. * Skip forward past the \r, \n, or \r\n.
  384. */
  385. CONSUME_CR_OR_LF(bp_ptr, length_cur);
  386. /*
  387. * Return the number of bytes we processed.
  388. */
  389. return (length - length_cur);
  390. trunc:
  391. return (-1);
  392. }
  393. static int
  394. resp_get_length(netdissect_options *ndo, register const u_char *bp, int len, const u_char **endp)
  395. {
  396. int result;
  397. u_char c;
  398. int saw_digit;
  399. int neg;
  400. int too_large;
  401. if (len == 0)
  402. goto trunc;
  403. ND_TCHECK(*bp);
  404. too_large = 0;
  405. neg = 0;
  406. if (*bp == '-') {
  407. neg = 1;
  408. bp++;
  409. len--;
  410. }
  411. result = 0;
  412. saw_digit = 0;
  413. for (;;) {
  414. if (len == 0)
  415. goto trunc;
  416. ND_TCHECK(*bp);
  417. c = *bp;
  418. if (!(c >= '0' && c <= '9')) {
  419. if (!saw_digit) {
  420. bp++;
  421. goto invalid;
  422. }
  423. break;
  424. }
  425. c -= '0';
  426. if (result > (INT_MAX / 10)) {
  427. /* This will overflow an int when we multiply it by 10. */
  428. too_large = 1;
  429. } else {
  430. result *= 10;
  431. if (result == ((INT_MAX / 10) * 10) && c > (INT_MAX % 10)) {
  432. /* This will overflow an int when we add c */
  433. too_large = 1;
  434. } else
  435. result += c;
  436. }
  437. bp++;
  438. len--;
  439. saw_digit = 1;
  440. }
  441. /*
  442. * OK, we found a non-digit character. It should be a \r, followed
  443. * by a \n.
  444. */
  445. if (*bp != '\r') {
  446. bp++;
  447. goto invalid;
  448. }
  449. bp++;
  450. len--;
  451. if (len == 0)
  452. goto trunc;
  453. ND_TCHECK(*bp);
  454. if (*bp != '\n') {
  455. bp++;
  456. goto invalid;
  457. }
  458. bp++;
  459. len--;
  460. *endp = bp;
  461. if (neg) {
  462. /* -1 means "null", anything else is invalid */
  463. if (too_large || result != 1)
  464. return (-4);
  465. result = -1;
  466. }
  467. return (too_large ? -3 : result);
  468. trunc:
  469. *endp = bp;
  470. return (-2);
  471. invalid:
  472. *endp = bp;
  473. return (-5);
  474. }