2010-12-31 18:03:23 +00:00
|
|
|
/* packet-json.c
|
|
|
|
* Routines for JSON dissection
|
|
|
|
* References:
|
|
|
|
* RFC 4627: http://tools.ietf.org/html/rfc4627
|
|
|
|
* Website: http://json.org/
|
|
|
|
*
|
2011-06-29 11:03:41 +00:00
|
|
|
* Copyright 2010, Jakub Zawadzki <darkjames-ws@darkjames.pl>
|
2010-12-31 18:03:23 +00:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 23:18:38 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-12-31 18:03:23 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-16 21:52:35 +00:00
|
|
|
#define NEW_PROTO_TREE_API
|
|
|
|
|
2010-12-31 18:03:23 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
#include <epan/wmem/wmem.h>
|
2010-12-31 18:03:23 +00:00
|
|
|
#include <epan/packet.h>
|
|
|
|
#include <epan/tvbparse.h>
|
|
|
|
|
2014-01-08 00:28:13 +00:00
|
|
|
#include <wsutil/str_util.h>
|
2014-01-07 21:55:49 +00:00
|
|
|
#include <wsutil/unicode-utils.h>
|
|
|
|
|
2013-12-14 10:29:26 +00:00
|
|
|
void proto_register_json(void);
|
|
|
|
void proto_reg_handoff_json(void);
|
|
|
|
|
2013-11-16 01:10:05 +00:00
|
|
|
static dissector_handle_t json_handle;
|
|
|
|
|
2010-12-31 18:03:23 +00:00
|
|
|
static gint ett_json = -1;
|
|
|
|
static gint ett_json_array = -1;
|
|
|
|
static gint ett_json_object = -1;
|
|
|
|
static gint ett_json_member = -1;
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info *hfi_json = NULL;
|
|
|
|
|
|
|
|
#define JSON_HFI_INIT HFI_INIT(proto_json)
|
|
|
|
|
|
|
|
static header_field_info hfi_json_array JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Array", "json.array", FT_NONE, BASE_NONE, NULL, 0x00, "JSON array", HFILL };
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_object JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Object", "json.object", FT_NONE, BASE_NONE, NULL, 0x00, "JSON object", HFILL };
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_member JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Member", "json.member", FT_NONE, BASE_NONE, NULL, 0x00, "JSON object member", HFILL };
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX */
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_member_key JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Key", "json.member.key", FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL };
|
|
|
|
#endif
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_value_string JSON_HFI_INIT = /* FT_STRINGZ? */
|
2013-12-07 15:48:35 +00:00
|
|
|
{ "String value", "json.value.string", FT_STRING, STR_UNICODE, NULL, 0x00, "JSON string value", HFILL };
|
2013-08-16 21:52:35 +00:00
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_value_number JSON_HFI_INIT = /* FT_DOUBLE/ FT_INT64? */
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Number value", "json.value.number", FT_STRING, BASE_NONE, NULL, 0x00, "JSON number value", HFILL };
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_value_false JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "False value", "json.value.false", FT_NONE, BASE_NONE, NULL, 0x00, "JSON false value", HFILL };
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_value_null JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "Null value", "json.value.null", FT_NONE, BASE_NONE, NULL, 0x00, "JSON null value", HFILL };
|
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
static header_field_info hfi_json_value_true JSON_HFI_INIT =
|
2013-08-16 21:52:35 +00:00
|
|
|
{ "True value", "json.value.true", FT_NONE, BASE_NONE, NULL, 0x00, "JSON true value", HFILL };
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
static tvbparse_wanted_t* want;
|
|
|
|
static tvbparse_wanted_t* want_ignore;
|
|
|
|
|
|
|
|
static dissector_handle_t text_lines_handle;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JSON_TOKEN_INVALID = -1,
|
|
|
|
JSON_TOKEN_NUMBER = 0,
|
|
|
|
JSON_TOKEN_STRING,
|
|
|
|
JSON_TOKEN_FALSE,
|
|
|
|
JSON_TOKEN_NULL,
|
|
|
|
JSON_TOKEN_TRUE,
|
|
|
|
|
|
|
|
/* not really tokens ... */
|
|
|
|
JSON_OBJECT,
|
|
|
|
JSON_ARRAY
|
|
|
|
|
|
|
|
} json_token_type_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_t *stack;
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
} json_parser_data_t;
|
|
|
|
|
2013-11-02 02:12:36 +00:00
|
|
|
static int
|
|
|
|
dissect_json(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
|
2010-12-31 18:03:23 +00:00
|
|
|
{
|
|
|
|
proto_tree *json_tree = NULL;
|
|
|
|
proto_item *ti = NULL;
|
|
|
|
|
|
|
|
json_parser_data_t parser_data;
|
|
|
|
tvbparse_t *tt;
|
|
|
|
|
|
|
|
const char *data_name;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
data_name = pinfo->match_string;
|
2013-11-02 02:12:36 +00:00
|
|
|
if (! (data_name && data_name[0])) {
|
2010-12-31 18:03:23 +00:00
|
|
|
/*
|
|
|
|
* No information from "match_string"
|
|
|
|
*/
|
2013-11-02 02:12:36 +00:00
|
|
|
data_name = (char *)data;
|
|
|
|
if (! (data_name && data_name[0])) {
|
2010-12-31 18:03:23 +00:00
|
|
|
/*
|
2013-11-02 02:12:36 +00:00
|
|
|
* No information from dissector data
|
2010-12-31 18:03:23 +00:00
|
|
|
*/
|
2013-11-02 02:12:36 +00:00
|
|
|
data_name = (char *)(pinfo->private_data);
|
|
|
|
if (! (data_name && data_name[0])) {
|
|
|
|
/*
|
|
|
|
* No information from "private_data"
|
|
|
|
*/
|
|
|
|
data_name = NULL;
|
|
|
|
}
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree) {
|
2013-08-17 07:16:12 +00:00
|
|
|
ti = proto_tree_add_item(tree, hfi_json, tvb, 0, -1, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
json_tree = proto_item_add_subtree(ti, ett_json);
|
|
|
|
|
|
|
|
if (data_name)
|
|
|
|
proto_item_append_text(ti, ": %s", data_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = 0;
|
2013-09-15 13:46:13 +00:00
|
|
|
|
|
|
|
parser_data.stack = wmem_stack_new(wmem_packet_scope());
|
|
|
|
wmem_stack_push(parser_data.stack, json_tree);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
tt = tvbparse_init(tvb, offset, -1, &parser_data, want_ignore);
|
|
|
|
|
|
|
|
/* XXX, only one json in packet? */
|
|
|
|
while ((tvbparse_get(tt, want)))
|
|
|
|
;
|
|
|
|
|
|
|
|
offset = tvbparse_curr_offset(tt);
|
|
|
|
|
|
|
|
proto_item_set_len(ti, offset);
|
|
|
|
|
|
|
|
/* if we have some unparsed data, pass to data-text-lines dissector (?) */
|
2012-07-15 14:29:06 +00:00
|
|
|
if (tvb_length_remaining(tvb, offset) > 0) {
|
2010-12-31 18:03:23 +00:00
|
|
|
int datalen, reported_datalen;
|
|
|
|
tvbuff_t *next_tvb;
|
2013-10-13 19:56:52 +00:00
|
|
|
|
2010-12-31 18:03:23 +00:00
|
|
|
datalen = tvb_length_remaining(tvb, offset);
|
|
|
|
reported_datalen = tvb_reported_length_remaining(tvb, offset);
|
|
|
|
|
|
|
|
next_tvb = tvb_new_subset(tvb, offset, datalen, reported_datalen);
|
|
|
|
|
|
|
|
call_dissector(text_lines_handle, next_tvb, pinfo, tree);
|
|
|
|
} else if (data_name) {
|
|
|
|
col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(%s)", data_name);
|
|
|
|
}
|
2013-11-02 02:12:36 +00:00
|
|
|
|
|
|
|
return tvb_length(tvb);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void before_object(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *tok) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
proto_tree *tree = (proto_tree *)wmem_stack_peek(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
proto_tree *subtree;
|
|
|
|
proto_item *ti;
|
|
|
|
|
2013-08-16 21:52:35 +00:00
|
|
|
ti = proto_tree_add_item(tree, &hfi_json_object, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
subtree = proto_item_add_subtree(ti, ett_json_object);
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_push(data->stack, subtree);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void after_object(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *elem _U_) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_pop(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void before_member(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *tok) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
proto_tree *tree = (proto_tree *)wmem_stack_peek(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
proto_tree *subtree;
|
|
|
|
proto_item *ti;
|
|
|
|
|
2013-08-16 21:52:35 +00:00
|
|
|
ti = proto_tree_add_item(tree, &hfi_json_member, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
subtree = proto_item_add_subtree(ti, ett_json_member);
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_push(data->stack, subtree);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void after_member(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *tok) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
proto_tree *tree = (proto_tree *)wmem_stack_pop(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
tvbparse_elem_t *key_tok = tok->sub;
|
|
|
|
|
|
|
|
if (key_tok && key_tok->id == JSON_TOKEN_STRING) {
|
2013-09-22 15:50:55 +00:00
|
|
|
char *key = tvb_get_string(wmem_packet_scope(), key_tok->tvb, key_tok->offset, key_tok->len);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
proto_item_append_text(tree, " Key: %s", key);
|
|
|
|
}
|
2013-08-16 21:52:35 +00:00
|
|
|
/* XXX, &hfi_json_member_key */
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void before_array(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *tok) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
proto_tree *tree = (proto_tree *)wmem_stack_peek(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
proto_tree *subtree;
|
|
|
|
proto_item *ti;
|
|
|
|
|
2013-08-16 21:52:35 +00:00
|
|
|
ti = proto_tree_add_item(tree, &hfi_json_array, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
subtree = proto_item_add_subtree(ti, ett_json_array);
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_push(data->stack, subtree);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void after_array(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *elem _U_) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
wmem_stack_pop(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 22:17:32 +00:00
|
|
|
static int
|
|
|
|
json_tvb_memcpy_utf8(char *buf, tvbuff_t *tvb, int offset, int offset_max)
|
|
|
|
{
|
|
|
|
int len = ws_utf8_char_len((guint8) *buf);
|
|
|
|
|
|
|
|
/* XXX, before moving to core API check if it's off-by-one safe.
|
2014-02-25 16:27:32 +00:00
|
|
|
* For JSON analyzer it's not a problem
|
2014-01-07 22:17:32 +00:00
|
|
|
* (string always terminated by ", which is not valid UTF-8 continuation character) */
|
|
|
|
if (len == -1 || ((guint) (offset + len)) >= (guint) offset_max) {
|
|
|
|
*buf = '?';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assume it's valid UTF-8 */
|
|
|
|
tvb_memcpy(tvb, buf + 1, offset + 1, len - 1);
|
|
|
|
|
|
|
|
if (!g_utf8_validate(buf, len, NULL)) {
|
|
|
|
*buf = '?';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-10-07 19:57:21 +00:00
|
|
|
static char *json_string_unescape(tvbparse_elem_t *tok)
|
|
|
|
{
|
2013-09-15 13:46:13 +00:00
|
|
|
char *str = (char *)wmem_alloc(wmem_packet_scope(), tok->len - 1);
|
2012-10-07 19:57:21 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for (i = 1; i < tok->len - 1; i++) {
|
|
|
|
guint8 ch = tvb_get_guint8(tok->tvb, tok->offset + i);
|
2014-01-08 00:28:13 +00:00
|
|
|
int bin;
|
2012-10-07 19:57:21 +00:00
|
|
|
|
|
|
|
if (ch == '\\') {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
ch = tvb_get_guint8(tok->tvb, tok->offset + i);
|
|
|
|
switch (ch) {
|
|
|
|
case '\"':
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
str[j++] = ch;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
str[j++] = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
str[j++] = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
str[j++] = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
str[j++] = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
str[j++] = '\t';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
{
|
2013-02-12 16:20:24 +00:00
|
|
|
guint32 unicode_hex = 0;
|
2012-10-07 19:57:21 +00:00
|
|
|
gboolean valid = TRUE;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
i++;
|
|
|
|
unicode_hex <<= 4;
|
|
|
|
|
|
|
|
ch = tvb_get_guint8(tok->tvb, tok->offset + i);
|
2014-01-08 00:28:13 +00:00
|
|
|
bin = ws_xton(ch);
|
|
|
|
if (bin == -1) {
|
2012-10-07 19:57:21 +00:00
|
|
|
valid = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-08 00:28:13 +00:00
|
|
|
unicode_hex |= bin;
|
2012-10-07 19:57:21 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 16:20:24 +00:00
|
|
|
if ((IS_LEAD_SURROGATE(unicode_hex))) {
|
|
|
|
ch = tvb_get_guint8(tok->tvb, tok->offset + i + 1);
|
|
|
|
|
|
|
|
if (ch == '\\') {
|
|
|
|
i++;
|
|
|
|
ch = tvb_get_guint8(tok->tvb, tok->offset + i + 1);
|
|
|
|
if (ch == 'u') {
|
|
|
|
guint16 lead_surrogate = unicode_hex;
|
|
|
|
guint16 trail_surrogate = 0;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
i++;
|
|
|
|
trail_surrogate <<= 4;
|
|
|
|
|
|
|
|
ch = tvb_get_guint8(tok->tvb, tok->offset + i);
|
2014-01-08 00:28:13 +00:00
|
|
|
bin = ws_xton(ch);
|
|
|
|
if (bin == -1) {
|
2013-02-12 16:20:24 +00:00
|
|
|
valid = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-08 00:28:13 +00:00
|
|
|
trail_surrogate |= bin;
|
2013-02-12 16:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((IS_TRAIL_SURROGATE(trail_surrogate))) {
|
2014-01-07 21:55:49 +00:00
|
|
|
unicode_hex = SURROGATE_VALUE(lead_surrogate,trail_surrogate);
|
2013-02-12 16:20:24 +00:00
|
|
|
} else {
|
|
|
|
valid = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valid = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valid = FALSE;
|
|
|
|
}
|
|
|
|
} else if ((IS_TRAIL_SURROGATE(unicode_hex))) {
|
|
|
|
i++;
|
|
|
|
valid = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid && g_unichar_validate(unicode_hex) && g_unichar_isprint(unicode_hex)) {
|
2012-10-07 19:57:21 +00:00
|
|
|
/* \uXXXX => 6 bytes */
|
|
|
|
int charlen = g_unichar_to_utf8(unicode_hex, &str[j]);
|
|
|
|
j += charlen;
|
|
|
|
} else
|
|
|
|
str[j++] = '?';
|
|
|
|
break;
|
|
|
|
}
|
2014-01-07 22:17:32 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* not valid by JSON grammar (also tvbparse rules should not allow it) */
|
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
|
|
|
break;
|
2012-10-07 19:57:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 22:17:32 +00:00
|
|
|
} else {
|
|
|
|
int utf_len;
|
|
|
|
|
|
|
|
str[j] = ch;
|
|
|
|
/* XXX if it's not valid UTF-8 character, add some expert info? (it violates JSON grammar) */
|
|
|
|
utf_len = json_tvb_memcpy_utf8(&str[j], tok->tvb, i, tok->len);
|
|
|
|
j += utf_len;
|
|
|
|
i += (utf_len - 1);
|
|
|
|
}
|
2012-10-07 19:57:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
str[j] = '\0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-12-31 18:03:23 +00:00
|
|
|
static void after_value(void *tvbparse_data, const void *wanted_data _U_, tvbparse_elem_t *tok) {
|
|
|
|
json_parser_data_t *data = (json_parser_data_t *) tvbparse_data;
|
|
|
|
|
2013-09-15 13:46:13 +00:00
|
|
|
proto_tree *tree = (proto_tree *)wmem_stack_peek(data->stack);
|
2010-12-31 18:03:23 +00:00
|
|
|
json_token_type_t value_id = JSON_TOKEN_INVALID;
|
|
|
|
|
|
|
|
if (tok->sub)
|
2013-03-19 20:00:52 +00:00
|
|
|
value_id = (json_token_type_t)tok->sub->id;
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
switch (value_id) {
|
|
|
|
case JSON_TOKEN_STRING:
|
2012-10-07 19:57:21 +00:00
|
|
|
if (tok->len >= 2)
|
2013-12-07 15:48:35 +00:00
|
|
|
proto_tree_add_string(tree, &hfi_json_value_string, tok->tvb, tok->offset, tok->len, json_string_unescape(tok));
|
2012-10-07 19:57:21 +00:00
|
|
|
else
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_tree_add_item(tree, &hfi_json_value_string, tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_TOKEN_NUMBER:
|
|
|
|
/* XXX, convert to number */
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_tree_add_item(tree, &hfi_json_value_number, tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_TOKEN_FALSE:
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_tree_add_item(tree, &hfi_json_value_false, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_TOKEN_NULL:
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_tree_add_item(tree, &hfi_json_value_null, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_TOKEN_TRUE:
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_tree_add_item(tree, &hfi_json_value_true, tok->tvb, tok->offset, tok->len, ENC_NA);
|
2010-12-31 18:03:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_OBJECT:
|
|
|
|
case JSON_ARRAY:
|
|
|
|
/* already added */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
proto_tree_add_text(tree, tok->tvb, tok->offset, tok->len, "%s", tvb_format_text(tok->tvb, tok->offset, tok->len));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_json_parser(void) {
|
|
|
|
static tvbparse_wanted_t _want_object;
|
|
|
|
static tvbparse_wanted_t _want_array;
|
|
|
|
|
|
|
|
tvbparse_wanted_t *want_object, *want_array;
|
|
|
|
tvbparse_wanted_t *want_member;
|
|
|
|
tvbparse_wanted_t *want_string;
|
|
|
|
tvbparse_wanted_t *want_number, *want_int;
|
|
|
|
tvbparse_wanted_t *want_value;
|
|
|
|
tvbparse_wanted_t *want_value_separator;
|
|
|
|
|
|
|
|
#define tvbparse_optional(id, private_data, before_cb, after_cb, wanted) \
|
|
|
|
tvbparse_some(id, 0, 1, private_data, before_cb, after_cb, wanted)
|
|
|
|
|
2012-10-07 19:33:47 +00:00
|
|
|
tvbparse_wanted_t *want_quot = tvbparse_char(-1,"\"",NULL,NULL,NULL);
|
|
|
|
|
|
|
|
want_string = tvbparse_set_seq(JSON_TOKEN_STRING, NULL, NULL, NULL,
|
|
|
|
want_quot,
|
|
|
|
tvbparse_some(-1, 0, G_MAXINT, NULL, NULL, NULL,
|
|
|
|
tvbparse_set_oneof(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_not_chars(-1, 0, 0, "\"" "\\", NULL, NULL, NULL), /* XXX, without invalid unicode characters */
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_char(-1, "\\", NULL, NULL, NULL),
|
|
|
|
tvbparse_set_oneof(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_chars(-1, 0, 1, "\"" "\\" "/bfnrt", NULL, NULL, NULL),
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_char(-1, "u", NULL, NULL, NULL),
|
2012-10-08 09:48:38 +00:00
|
|
|
tvbparse_chars(-1, 4, 4, "0123456789abcdefABCDEF", NULL, NULL, NULL),
|
2012-10-07 19:33:47 +00:00
|
|
|
NULL),
|
|
|
|
NULL),
|
|
|
|
NULL),
|
|
|
|
NULL)
|
|
|
|
),
|
|
|
|
want_quot,
|
|
|
|
NULL);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
want_value_separator = tvbparse_char(-1, ",", NULL, NULL, NULL);
|
|
|
|
|
|
|
|
/* int = zero / ( digit1-9 *DIGIT ) */
|
|
|
|
want_int = tvbparse_set_oneof(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_char(-1, "0", NULL, NULL, NULL),
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_chars(-1, 1, 1, "123456789", NULL, NULL, NULL),
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL, /* tvbparse_chars() don't respect 0 as min_len ;/ */
|
|
|
|
tvbparse_chars(-1, 0, 0, "0123456789", NULL, NULL, NULL)),
|
|
|
|
NULL),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* number = [ minus ] int [ frac ] [ exp ] */
|
|
|
|
want_number = tvbparse_set_seq(JSON_TOKEN_NUMBER, NULL, NULL, NULL,
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL, /* tvbparse_chars() don't respect 0 as min_len ;/ */
|
|
|
|
tvbparse_chars(-1, 0, 1, "-", NULL, NULL, NULL)),
|
|
|
|
want_int,
|
|
|
|
/* frac = decimal-point 1*DIGIT */
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_char(-1, ".", NULL, NULL, NULL),
|
|
|
|
tvbparse_chars(-1, 1, 0, "0123456789", NULL, NULL, NULL),
|
|
|
|
NULL)),
|
|
|
|
/* exp = e [ minus / plus ] 1*DIGIT */
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_char(-1, "eE", NULL, NULL, NULL),
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL, /* tvbparse_chars() don't respect 0 as min_len ;/ */
|
|
|
|
tvbparse_chars(-1, 0, 1, "-+", NULL, NULL, NULL)),
|
|
|
|
tvbparse_chars(-1, 1, 0, "0123456789", NULL, NULL, NULL),
|
|
|
|
NULL)),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* value = false / null / true / object / array / number / string */
|
|
|
|
want_value = tvbparse_set_oneof(-1, NULL, NULL, after_value,
|
|
|
|
tvbparse_string(JSON_TOKEN_FALSE, "false", NULL, NULL, NULL),
|
|
|
|
tvbparse_string(JSON_TOKEN_NULL, "null", NULL, NULL, NULL),
|
|
|
|
tvbparse_string(JSON_TOKEN_TRUE, "true", NULL, NULL, NULL),
|
|
|
|
&_want_object,
|
|
|
|
&_want_array,
|
|
|
|
want_number,
|
|
|
|
want_string,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* array = begin-array [ value *( value-separator value ) ] end-array */
|
|
|
|
want_array = tvbparse_set_seq(JSON_ARRAY, NULL, before_array, after_array,
|
|
|
|
tvbparse_char(-1, "[", NULL, NULL, NULL),
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
want_value,
|
2013-10-13 19:56:52 +00:00
|
|
|
tvbparse_some(-1, 0, G_MAXINT, NULL, NULL, NULL,
|
2010-12-31 18:03:23 +00:00
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
want_value_separator,
|
|
|
|
want_value,
|
|
|
|
NULL)),
|
|
|
|
NULL)
|
|
|
|
),
|
|
|
|
tvbparse_char(-1, "]", NULL, NULL, NULL),
|
|
|
|
NULL);
|
|
|
|
_want_array = *want_array;
|
|
|
|
|
|
|
|
/* member = string name-separator value */
|
|
|
|
want_member = tvbparse_set_seq(-1, NULL, before_member, after_member,
|
|
|
|
want_string,
|
|
|
|
tvbparse_char(-1, ":", NULL, NULL, NULL),
|
|
|
|
want_value,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* object = begin-object [ member *( value-separator member ) ] end-object */
|
|
|
|
want_object = tvbparse_set_seq(JSON_OBJECT, NULL, before_object, after_object,
|
|
|
|
tvbparse_char(-1, "{", NULL, NULL, NULL),
|
|
|
|
tvbparse_optional(-1, NULL, NULL, NULL,
|
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
want_member,
|
2013-10-13 19:56:52 +00:00
|
|
|
tvbparse_some(-1, 0, G_MAXINT, NULL, NULL, NULL,
|
2010-12-31 18:03:23 +00:00
|
|
|
tvbparse_set_seq(-1, NULL, NULL, NULL,
|
|
|
|
want_value_separator,
|
|
|
|
want_member,
|
|
|
|
NULL)),
|
|
|
|
NULL)
|
|
|
|
),
|
|
|
|
tvbparse_char(-1, "}", NULL, NULL, NULL),
|
|
|
|
NULL);
|
|
|
|
_want_object = *want_object;
|
|
|
|
|
|
|
|
want_ignore = tvbparse_chars(-1, 1, 0, " \t\r\n", NULL, NULL, NULL);
|
|
|
|
|
|
|
|
/* JSON-text = object / array */
|
|
|
|
want = tvbparse_set_oneof(-1, NULL, NULL, NULL,
|
|
|
|
want_object,
|
|
|
|
want_array,
|
|
|
|
/* tvbparse_not_chars(-1, 1, 0, " \t\r\n", NULL, NULL, NULL), */
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* XXX, heur? */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-14 10:29:26 +00:00
|
|
|
proto_register_json(void)
|
|
|
|
{
|
2010-12-31 18:03:23 +00:00
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_json,
|
|
|
|
&ett_json_array,
|
|
|
|
&ett_json_object,
|
|
|
|
&ett_json_member
|
|
|
|
};
|
|
|
|
|
2013-11-07 20:14:18 +00:00
|
|
|
#ifndef HAVE_HFI_SECTION_INIT
|
2013-08-16 21:52:35 +00:00
|
|
|
static header_field_info *hfi[] = {
|
|
|
|
&hfi_json_array,
|
|
|
|
&hfi_json_object,
|
|
|
|
&hfi_json_member,
|
|
|
|
/* &hfi_json_member_key, */
|
|
|
|
&hfi_json_value_string,
|
|
|
|
&hfi_json_value_number,
|
|
|
|
&hfi_json_value_false,
|
|
|
|
&hfi_json_value_null,
|
|
|
|
&hfi_json_value_true,
|
2010-12-31 18:03:23 +00:00
|
|
|
};
|
2013-11-07 20:14:18 +00:00
|
|
|
#endif
|
2010-12-31 18:03:23 +00:00
|
|
|
|
2013-08-17 07:16:12 +00:00
|
|
|
int proto_json;
|
|
|
|
|
2010-12-31 18:03:23 +00:00
|
|
|
proto_json = proto_register_protocol("JavaScript Object Notation", "JSON", "json");
|
2013-08-17 07:16:12 +00:00
|
|
|
hfi_json = proto_registrar_get_nth(proto_json);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
2013-08-16 21:52:35 +00:00
|
|
|
proto_register_fields(proto_json, hfi, array_length(hfi));
|
2010-12-31 18:03:23 +00:00
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
2013-11-16 01:10:05 +00:00
|
|
|
json_handle = new_register_dissector("json", dissect_json, proto_json);
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
init_json_parser();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_reg_handoff_json(void)
|
|
|
|
{
|
|
|
|
dissector_add_string("media_type", "application/json", json_handle); /* RFC 4627 */
|
2013-02-25 06:48:35 +00:00
|
|
|
dissector_add_string("media_type", "application/json-rpc", json_handle); /* JSON-RPC over HTTP */
|
|
|
|
dissector_add_string("media_type", "application/jsonrequest", json_handle); /* JSON-RPC over HTTP */
|
2010-12-31 18:03:23 +00:00
|
|
|
|
|
|
|
text_lines_handle = find_dissector("data-text-lines");
|
|
|
|
}
|
|
|
|
|