Revert "TODO: smb2: simplify preauth_hash calculation..."
[wireshark-sm.git] / sharkd_session.c
blobc902a2ac99df9eb14aef2450298fcc5709667b13
1 /* sharkd_session.c
3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "wtap_opttypes.h"
13 #include <config.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <inttypes.h>
22 #include <glib.h>
24 #include <wsutil/wsjson.h>
25 #include <wsutil/json_dumper.h>
26 #include <wsutil/ws_assert.h>
27 #include <wsutil/wsgcrypt.h>
29 #include <file.h>
30 #include <epan/epan_dissect.h>
31 #include <epan/exceptions.h>
32 #include <epan/color_filters.h>
33 #include <epan/prefs.h>
34 #include <epan/prefs-int.h>
35 #include <epan/uat-int.h>
36 #include <wiretap/wtap.h>
38 #include <epan/column.h>
39 #include <epan/column-info.h>
41 #include <ui/ssl_key_export.h>
43 #include <ui/io_graph_item.h>
44 #include <epan/stats_tree_priv.h>
45 #include <epan/stat_tap_ui.h>
46 #include <epan/conversation_table.h>
47 #include <epan/sequence_analysis.h>
48 #include <epan/expert.h>
49 #include <epan/export_object.h>
50 #include <epan/follow.h>
51 #include <epan/rtd_table.h>
52 #include <epan/srt_table.h>
53 #include <epan/to_str.h>
55 #include <epan/dissectors/packet-h225.h>
56 #include <epan/rtp_pt.h>
57 #include <ui/voip_calls.h>
58 #include <ui/rtp_stream.h>
59 #include <ui/tap-rtp-common.h>
60 #include <ui/tap-rtp-analysis.h>
61 #include <ui/cli/tap-protohierstat.h>
62 #include <ui/cli/tap-voip.h>
63 #include <wsutil/version_info.h>
64 #include <epan/to_str.h>
66 #include <epan/addr_resolv.h>
67 #include <epan/dissectors/packet-rtp.h>
68 #include <ui/rtp_media.h>
69 #include <ui/mcast_stream.h>
70 #include <speex/speex_resampler.h>
72 #include <epan/maxmind_db.h>
74 #include <wsutil/pint.h>
75 #include <wsutil/strnatcmp.h>
76 #include <wsutil/strtoi.h>
78 #include "globals.h"
80 #include "sharkd.h"
82 struct sharkd_filter_item
84 guint8 *filtered; /* can be NULL if all frames are matching for given filter. */
87 static GHashTable *filter_table;
89 static int mode;
90 static guint32 rpcid;
92 static json_dumper dumper;
95 static const char *
96 json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
98 int i;
100 for (i = 0; i < count; i += 2)
102 const char *tok_attr = &buf[tokens[i + 0].start];
103 const char *tok_value = &buf[tokens[i + 1].start];
105 if (!strcmp(tok_attr, attr))
106 return tok_value;
109 return NULL;
112 static void
113 json_print_base64(const guint8 *data, size_t len)
115 json_dumper_begin_base64(&dumper);
116 json_dumper_write_base64(&dumper, data, len);
117 json_dumper_end_base64(&dumper);
120 static void G_GNUC_PRINTF(2, 3)
121 sharkd_json_value_anyf(const char *key, const char *format, ...)
123 if (key)
124 json_dumper_set_member_name(&dumper, key);
126 va_list ap;
127 va_start(ap, format);
128 json_dumper_value_va_list(&dumper, format, ap);
129 va_end(ap);
132 static void
133 sharkd_json_value_string(const char *key, const char *str)
135 if (key)
136 json_dumper_set_member_name(&dumper, key);
137 json_dumper_value_string(&dumper, str);
140 static void
141 sharkd_json_value_base64(const char *key, const guint8 *data, size_t len)
143 if (key)
144 json_dumper_set_member_name(&dumper, key);
145 json_print_base64(data, len);
148 static void G_GNUC_PRINTF(2, 3)
149 sharkd_json_value_stringf(const char *key, const char *format, ...)
151 if (key)
152 json_dumper_set_member_name(&dumper, key);
154 va_list ap;
155 va_start(ap, format);
156 char* sformat = ws_strdup_printf("\"%s\"", format);
157 json_dumper_value_va_list(&dumper, sformat, ap);
158 g_free(sformat);
159 va_end(ap);
162 static void
163 sharkd_json_array_open(const char *key)
165 if (key)
166 json_dumper_set_member_name(&dumper, key);
167 json_dumper_begin_array(&dumper);
170 static void
171 sharkd_json_array_close(void)
173 json_dumper_end_array(&dumper);
176 static void
177 sharkd_json_object_open(const char *key)
179 if (key)
180 json_dumper_set_member_name(&dumper, key);
181 json_dumper_begin_object(&dumper);
184 static void
185 sharkd_json_object_close(void)
187 json_dumper_end_object(&dumper);
190 static void
191 sharkd_json_response_open(guint32 id)
193 json_dumper_begin_object(&dumper); // start the message
194 sharkd_json_value_string("jsonrpc", "2.0");
195 sharkd_json_value_anyf("id", "%d", id);
198 static void
199 sharkd_json_response_close(void)
201 json_dumper_end_object(&dumper); // end the message
203 json_dumper_finish(&dumper);
206 * We do an explicit fflush after every line, because
207 * we want output to be written to the socket as soon
208 * as the line is complete.
210 * The stream is fully-buffered by default, so it's
211 * only flushed when the buffer fills or the FILE *
212 * is closed. On UN*X, we could set it to be line
213 * buffered, but the MSVC standard I/O routines don't
214 * support line buffering - they only support *byte*
215 * buffering, doing a write for every byte written,
216 * which is too inefficient, and full buffering,
217 * which is what you get if you request line buffering.
219 fflush(stdout);
222 static void
223 sharkd_json_result_prologue(guint32 id)
225 sharkd_json_response_open(id);
226 sharkd_json_object_open("result"); // start the result object
229 static void
230 sharkd_json_result_epilogue(void)
232 json_dumper_end_object(&dumper); // end the result object
233 sharkd_json_response_close();
236 static void
237 sharkd_json_result_array_prologue(guint32 id)
239 sharkd_json_response_open(id);
240 sharkd_json_array_open("result"); // start the result array
243 static void
244 sharkd_json_result_array_epilogue(void)
246 sharkd_json_array_close(); // end of result array
247 sharkd_json_response_close();
250 static void
251 sharkd_json_simple_ok(guint32 id)
253 sharkd_json_result_prologue(id);
254 sharkd_json_value_string("status", "OK");
255 sharkd_json_result_epilogue();
258 static void
259 sharkd_json_warning(guint32 id, char *warning)
261 sharkd_json_result_prologue(id);
262 sharkd_json_value_string("status", "Warning");
263 sharkd_json_value_string("warning", warning);
264 sharkd_json_result_epilogue();
267 static void G_GNUC_PRINTF(4, 5)
268 sharkd_json_error(guint32 id, int code, char* data, char* format, ...)
270 sharkd_json_response_open(id);
271 sharkd_json_object_open("error");
272 sharkd_json_value_anyf("code", "%d", code);
274 if (format)
276 // format the text message
277 va_list args;
279 va_start(args, format);
280 char *error_msg = ws_strdup_vprintf(format, args);
281 va_end(args);
283 sharkd_json_value_string("message", error_msg);
285 g_free(error_msg);
288 sharkd_json_object_close();
290 if (data)
291 sharkd_json_value_string("data", data);
293 sharkd_json_response_close();
296 static gboolean
297 is_param_match(const char *param_in, const char *valid_param)
299 char* ptr;
301 if ((ptr = g_strrstr(valid_param, "*")))
303 size_t prefix_len = ptr - valid_param;
304 return !strncmp(param_in, valid_param, prefix_len);
306 else
307 return !strcmp(param_in, valid_param);
311 * json_prep does four things:
313 * 1. check the syntax of the root and parameter members
314 * 2. tokenize the names and values by zero terminating them
315 * 3. unescape the names and values
316 * 4. extracts and saves the rpcid
317 * - we have to do it here as it's needed for the error messages
319 * The objective is to minimise the validation work in the functions
320 * that process each called method.
322 * This gets a little messy as the JSON parser creates a flat list
323 * of all members rather than create a tree.
325 static gboolean
326 json_prep(char* buf, const jsmntok_t* tokens, int count)
328 int i;
329 const char* method = NULL;
330 char* attr_name = NULL;
331 char* attr_value = NULL;
333 #define SHARKD_JSON_ANY 0
334 #define SHARKD_JSON_STRING 1
335 #define SHARKD_JSON_INTEGER 2
336 #define SHARKD_JSON_UINTEGER 3
337 #define SHARKD_JSON_FLOAT 4
338 #define SHARKD_JSON_OBJECT 5
339 #define SHARKD_JSON_ARRAY 6
340 #define SHARKD_JSON_BOOLEAN 7
341 #define SHARKD_ARRAY_END 99
343 struct member_attribute {
344 const char* parent_ctx;
345 const char* name;
346 int level;
347 jsmntype_t type;
348 int value_type;
349 gboolean is_mandatory;
352 #define SHARKD_MANDATORY TRUE
353 #define SHARKD_OPTIONAL FALSE
356 * The member attribute structure is key to the syntax checking. The
357 * array contains all of the root level (1) member names, the data
358 * types permissible for the value and a boolean that indicates whether
359 * or not the member is mandatory.
361 * Once we get into the next layer (2) of the json tree, we need to check
362 * params member names and data types dependent in the context of the method
363 * (parent_ctx).
366 struct member_attribute name_array[] = {
367 // Root members
368 {NULL, "jsonrpc", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
369 {NULL, "userid", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
370 {NULL, "id", 1, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
371 {NULL, "method", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
372 {NULL, "params", 1, JSMN_OBJECT, SHARKD_JSON_OBJECT, SHARKD_OPTIONAL},
374 // Valid methods
375 {"method", "analyse", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
376 {"method", "bye", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
377 {"method", "check", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
378 {"method", "complete", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
379 {"method", "download", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
380 {"method", "dumpconf", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
381 {"method", "follow", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
382 {"method", "frame", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
383 {"method", "frames", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
384 {"method", "info", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
385 {"method", "intervals", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
386 {"method", "iograph", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
387 {"method", "load", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
388 {"method", "setcomment", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
389 {"method", "setconf", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
390 {"method", "status", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
391 {"method", "tap", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
393 // Parameters and their method context
394 {"check", "field", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
395 {"check", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
396 {"complete", "field", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
397 {"complete", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
398 {"download", "token", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
399 {"dumpconf", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
400 {"follow", "follow", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
401 {"follow", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
402 {"follow", "sub_stream", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
403 {"frame", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
404 {"frame", "proto", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
405 {"frame", "ref_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
406 {"frame", "prev_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
407 {"frame", "columns", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
408 {"frame", "color", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
409 {"frame", "bytes", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
410 {"frame", "hidden", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
411 {"frames", "column*", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY, SHARKD_OPTIONAL},
412 {"frames", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
413 {"frames", "skip", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
414 {"frames", "limit", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
415 {"frames", "refs", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
416 {"intervals", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
417 {"intervals", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
418 {"iograph", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
419 {"iograph", "interval_units", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
420 {"iograph", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
421 {"iograph", "graph0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
422 {"iograph", "graph1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
423 {"iograph", "graph2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
424 {"iograph", "graph3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
425 {"iograph", "graph4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
426 {"iograph", "graph5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
427 {"iograph", "graph6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
428 {"iograph", "graph7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
429 {"iograph", "graph8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
430 {"iograph", "graph9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
431 {"iograph", "filter0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
432 {"iograph", "filter1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
433 {"iograph", "filter2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
434 {"iograph", "filter3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
435 {"iograph", "filter4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
436 {"iograph", "filter5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
437 {"iograph", "filter6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
438 {"iograph", "filter7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
439 {"iograph", "filter8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
440 {"iograph", "filter9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
441 {"load", "file", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
442 {"setcomment", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
443 {"setcomment", "comment", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
444 {"setconf", "name", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
445 {"setconf", "value", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY, SHARKD_MANDATORY},
446 {"tap", "tap0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
447 {"tap", "tap1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
448 {"tap", "tap2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
449 {"tap", "tap3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
450 {"tap", "tap4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
451 {"tap", "tap5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
452 {"tap", "tap6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
453 {"tap", "tap7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
454 {"tap", "tap8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
455 {"tap", "tap9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
456 {"tap", "tap10", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
457 {"tap", "tap11", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
458 {"tap", "tap12", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
459 {"tap", "tap13", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
460 {"tap", "tap14", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
461 {"tap", "tap15", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
462 {"tap", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
464 // End of the name_array
465 {NULL, NULL, 0, JSMN_STRING, SHARKD_ARRAY_END, SHARKD_OPTIONAL},
468 rpcid = 0;
470 /* sanity check, and split strings */
471 if (count < 1 || tokens[0].type != JSMN_OBJECT)
473 sharkd_json_error(
474 rpcid, -32600, NULL,
475 "The request must an object"
477 return FALSE;
480 /* don't need [0] token */
481 tokens++;
482 count--;
484 if (count & 1)
486 sharkd_json_error(
487 rpcid, -32600, NULL,
488 "The request must contain name/value pairs"
490 return FALSE;
493 for (i = 0; i < count; i += 2)
495 buf[tokens[i + 0].end] = '\0';
496 buf[tokens[i + 1].end] = '\0';
499 // we must get the id as soon as possible so that it's available in all future error messages
500 attr_value = (char*)json_find_attr(buf, tokens, count, "id");
501 if (attr_value)
503 if (!ws_strtou32(attr_value, NULL, &rpcid))
505 sharkd_json_error(
506 rpcid, -32600, NULL,
507 "The id value must be a positive integer"
509 return FALSE;
513 method = json_find_attr(buf, tokens, count, "method");
515 if (method)
517 gboolean is_supported = FALSE;
518 i = 0; // name array index
520 // check that the request method is good
521 while (name_array[i].value_type != SHARKD_ARRAY_END)
523 if (name_array[i].parent_ctx)
525 if (!strcmp(method, name_array[i].name) && !strcmp(name_array[i].parent_ctx, "method"))
526 is_supported = TRUE; // the method is valid
529 i++;
532 if (!is_supported)
534 sharkd_json_error(
535 rpcid, -32601, NULL,
536 "The method %s is not supported", method
538 return FALSE;
542 for (i = 0; i < count; i += 2)
544 if (tokens[i].type != JSMN_STRING)
546 sharkd_json_error(
547 rpcid, -32600, NULL,
548 "Member names must be a string - member %d is not string", (i / 2) + 1
550 return FALSE;
553 attr_name = &buf[tokens[i + 0].start];
554 attr_value = &buf[tokens[i + 1].start];
556 if (!strcmp(attr_name, "jsonrpc"))
558 if (strcmp(&buf[tokens[i + 1].start], "2.0"))
560 sharkd_json_error(
561 rpcid, -32600, NULL,
562 "Only JSON %s is supported", "2.0"
564 return FALSE;
568 /* unescape only value, as keys are simple strings */
569 if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(attr_value))
571 sharkd_json_error(
572 rpcid, -32600, NULL,
573 "Cannot unescape the value string of member %d", (i / 2) + 1
575 return FALSE;
578 /* Confirm that the member is valid */
579 gboolean match = FALSE;
581 // We need to check root members (level 1) and parameters (level 2), hence the for loop.
583 for (int level = 1; level < 3; level++)
585 size_t j = 0;
587 while (name_array[j].value_type != SHARKD_ARRAY_END) // iterate through the array until we hit the end
589 if (is_param_match(attr_name, name_array[j].name) && name_array[j].level == level)
591 // We need to be sure the match is in the correct context
592 // i.e. is this a match for a root member (level 1) or for a parameter (level 2).
594 if (level == 1)
596 // need to guard against a parameter name matching a method name
597 if (method)
599 if (name_array[j].parent_ctx)
601 j++;
602 continue;
605 if (!strcmp(method, &buf[tokens[i + 0].start]))
607 j++;
608 continue;
612 match = TRUE;
614 else if (method)
616 if (level == 2 && !strcmp(name_array[j].parent_ctx, method))
617 match = TRUE;
618 else
620 j++;
621 continue;
624 else
626 j++;
627 continue;
630 // The match looks good, let's now check the data types
632 if (tokens[i + 1].type != name_array[j].type && name_array[j].type != SHARKD_JSON_ANY)
634 sharkd_json_error(
635 rpcid, -32600, NULL,
636 "The data type for member %s is not valid", attr_name
638 return FALSE;
640 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_UINTEGER)
642 guint32 temp;
643 if (!ws_strtou32(attr_value, NULL, &temp) || temp <= 0)
645 sharkd_json_error(
646 rpcid, -32600, NULL,
647 "The value for %s must be a positive integer", name_array[j].name
649 return FALSE;
652 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_BOOLEAN)
654 if (strcmp(attr_value, "true") && strcmp(attr_value, "false"))
656 sharkd_json_error(
657 rpcid, -32600, NULL,
658 "The value for %s must be a boolean (true or false)", name_array[j].name
660 return FALSE;
664 break; // looks like a valid match
666 j++;
670 if (!match)
672 sharkd_json_error(
673 rpcid, -32600, NULL,
674 "%s is not a valid member name", attr_name
676 return FALSE;
680 /* check for mandatory members */
681 size_t j = 0;
683 while (name_array[j].value_type != SHARKD_ARRAY_END)
685 if (name_array[j].is_mandatory && name_array[j].level == 1)
687 if (!json_find_attr(buf, tokens, count, name_array[j].name))
689 sharkd_json_error(
690 rpcid, -32600, NULL,
691 "Mandatory member %s is missing", name_array[j].name
693 return FALSE;
696 j++;
699 // check that the current request contains the mandatory parameters
700 j = 0;
702 while (name_array[j].value_type != SHARKD_ARRAY_END)
704 if (name_array[j].is_mandatory && name_array[j].level == 2 && !strcmp(method, name_array[j].parent_ctx))
706 if (!json_find_attr(buf, tokens, count, name_array[j].name))
708 sharkd_json_error(
709 rpcid, -32600, NULL,
710 "Mandatory parameter %s is missing", name_array[j].name
712 return FALSE;
715 j++;
719 // check that the parameters for the current request are valid for the method and that the data type for the value is valid
721 return TRUE;
724 static void
725 sharkd_session_filter_free(gpointer data)
727 struct sharkd_filter_item *l = (struct sharkd_filter_item *) data;
729 g_free(l->filtered);
730 g_free(l);
733 static const struct sharkd_filter_item *
734 sharkd_session_filter_data(const char *filter)
736 struct sharkd_filter_item *l;
738 l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter);
739 if (!l)
741 guint8 *filtered = NULL;
743 int ret = sharkd_filter(filter, &filtered);
745 if (ret == -1)
746 return NULL;
748 l = g_new(struct sharkd_filter_item, 1);
749 l->filtered = filtered;
751 g_hash_table_insert(filter_table, g_strdup(filter), l);
754 return l;
757 static gboolean
758 sharkd_rtp_match_init(rtpstream_id_t *id, const char *init_str)
760 gboolean ret = FALSE;
761 char **arr;
762 guint32 tmp_addr_src, tmp_addr_dst;
763 address tmp_src_addr, tmp_dst_addr;
765 memset(id, 0, sizeof(*id));
767 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
768 if (g_strv_length(arr) != 5)
769 goto fail;
771 /* TODO, for now only IPv4 */
772 if (!get_host_ipaddr(arr[0], &tmp_addr_src))
773 goto fail;
775 if (!ws_strtou16(arr[1], NULL, &id->src_port))
776 goto fail;
778 if (!get_host_ipaddr(arr[2], &tmp_addr_dst))
779 goto fail;
781 if (!ws_strtou16(arr[3], NULL, &id->dst_port))
782 goto fail;
784 if (!ws_hexstrtou32(arr[4], NULL, &id->ssrc))
785 goto fail;
787 set_address(&tmp_src_addr, AT_IPv4, 4, &tmp_addr_src);
788 copy_address(&id->src_addr, &tmp_src_addr);
789 set_address(&tmp_dst_addr, AT_IPv4, 4, &tmp_addr_dst);
790 copy_address(&id->dst_addr, &tmp_dst_addr);
792 ret = TRUE;
794 fail:
795 g_strfreev(arr);
796 return ret;
799 static bool
800 sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata _U_)
802 stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
804 json_dumper_begin_object(&dumper);
805 sharkd_json_value_string("name", stat_tap->title);
806 sharkd_json_value_stringf("tap", "nstat:%s", (const char *) key);
807 json_dumper_end_object(&dumper);
809 return FALSE;
812 static bool
813 sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata _U_)
815 struct register_ct *table = (struct register_ct *) value;
817 const char *label = (const char *) key;
819 if (get_conversation_packet_func(table))
821 json_dumper_begin_object(&dumper);
822 sharkd_json_value_stringf("name", "Conversation List/%s", label);
823 sharkd_json_value_stringf("tap", "conv:%s", label);
824 json_dumper_end_object(&dumper);
827 if (get_endpoint_packet_func(table))
829 json_dumper_begin_object(&dumper);
830 sharkd_json_value_stringf("name", "Endpoint/%s", label);
831 sharkd_json_value_stringf("tap", "endpt:%s", label);
832 json_dumper_end_object(&dumper);
834 return FALSE;
837 static bool
838 sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata _U_)
840 register_analysis_t *analysis = (register_analysis_t *) value;
842 json_dumper_begin_object(&dumper);
843 sharkd_json_value_string("name", sequence_analysis_get_ui_name(analysis));
844 sharkd_json_value_stringf("tap", "seqa:%s", (const char *) key);
845 json_dumper_end_object(&dumper);
847 return FALSE;
850 static bool
851 sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data _U_)
853 register_eo_t *eo = (register_eo_t *) value;
855 const int proto_id = get_eo_proto_id(eo);
856 const char *filter = proto_get_protocol_filter_name(proto_id);
857 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
859 json_dumper_begin_object(&dumper);
860 sharkd_json_value_stringf("name", "Export Object/%s", label);
861 sharkd_json_value_stringf("tap", "eo:%s", filter);
862 json_dumper_end_object(&dumper);
864 return FALSE;
867 static bool
868 sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data _U_)
870 register_srt_t *srt = (register_srt_t *) value;
872 const int proto_id = get_srt_proto_id(srt);
873 const char *filter = proto_get_protocol_filter_name(proto_id);
874 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
876 json_dumper_begin_object(&dumper);
877 sharkd_json_value_stringf("name", "Service Response Time/%s", label);
878 sharkd_json_value_stringf("tap", "srt:%s", filter);
879 json_dumper_end_object(&dumper);
881 return FALSE;
884 static bool
885 sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data _U_)
887 register_rtd_t *rtd = (register_rtd_t *) value;
889 const int proto_id = get_rtd_proto_id(rtd);
890 const char *filter = proto_get_protocol_filter_name(proto_id);
891 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
893 json_dumper_begin_object(&dumper);
894 sharkd_json_value_stringf("name", "Response Time Delay/%s", label);
895 sharkd_json_value_stringf("tap", "rtd:%s", filter);
896 json_dumper_end_object(&dumper);
898 return FALSE;
901 static bool
902 sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data _U_)
904 register_follow_t *follower = (register_follow_t *) value;
906 const int proto_id = get_follow_proto_id(follower);
907 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
908 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
910 json_dumper_begin_object(&dumper);
911 sharkd_json_value_stringf("name", "Follow/%s", label);
912 sharkd_json_value_stringf("tap", "follow:%s", filter);
913 json_dumper_end_object(&dumper);
915 return FALSE;
918 static void
919 sharkd_session_print_capture_types(void)
921 guint i;
922 GArray *writable_type_subtypes;
923 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
924 for (i = 0; i < writable_type_subtypes->len; i++) {
925 int ft = g_array_index(writable_type_subtypes, int, i);
926 sharkd_json_object_open(NULL);
927 sharkd_json_value_string("name", wtap_file_type_subtype_name(ft));
928 sharkd_json_value_string("description", wtap_file_type_subtype_description(ft));
929 sharkd_json_object_close();
931 g_array_free(writable_type_subtypes, TRUE);
934 struct encap_type_info
936 const char *name;
937 const char *description;
940 static gint
941 encap_type_info_nat_compare(gconstpointer a, gconstpointer b)
943 return ws_ascii_strnatcmp(((const struct encap_type_info *)a)->name,
944 ((const struct encap_type_info *)b)->name);
947 static void
948 encap_type_info_visit(gpointer data, gpointer user_data _U_)
950 sharkd_json_object_open(NULL);
951 sharkd_json_value_string("name", ((struct encap_type_info *)data)->name);
952 sharkd_json_value_string("description", ((struct encap_type_info *)data)->description);
953 sharkd_json_object_close();
956 static void
957 sharkd_session_print_encap_types(void)
959 int i;
960 struct encap_type_info *encaps;
961 GSList *list = NULL;
962 encaps = g_new(struct encap_type_info, WTAP_NUM_ENCAP_TYPES);
963 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
964 encaps[i].name = wtap_encap_name(i);
965 if (encaps[i].name != NULL) {
966 encaps[i].description = wtap_encap_description(i);
967 list = g_slist_insert_sorted(list, &encaps[i], encap_type_info_nat_compare);
970 g_slist_foreach(list, encap_type_info_visit, NULL);
971 g_slist_free(list);
972 g_free(encaps);
976 * sharkd_session_process_info()
978 * Process info request
980 * Output object with attributes:
981 * (m) version - version number
983 * (m) columns - available column formats, array of object with attributes:
984 * 'name' - column name
985 * 'format' - column format-name
987 * (m) stats - available statistics, array of object with attributes:
988 * 'name' - statistic name
989 * 'tap' - sharkd tap-name for statistic
991 * (m) convs - available conversation list, array of object with attributes:
992 * 'name' - conversation name
993 * 'tap' - sharkd tap-name for conversation
995 * (m) eo - available export object list, array of object with attributes:
996 * 'name' - export object name
997 * 'tap' - sharkd tap-name for eo
999 * (m) srt - available service response time list, array of object with attributes:
1000 * 'name' - service response time name
1001 * 'tap' - sharkd tap-name for srt
1003 * (m) rtd - available response time delay list, array of object with attributes:
1004 * 'name' - response time delay name
1005 * 'tap' - sharkd tap-name for rtd
1007 * (m) seqa - available sequence analysis (flow) list, array of object with attributes:
1008 * 'name' - sequence analysis name
1009 * 'tap' - sharkd tap-name
1011 * (m) taps - available taps, array of object with attributes:
1012 * 'name' - tap name
1013 * 'tap' - sharkd tap-name
1015 * (m) follow - available followers, array of object with attributes:
1016 * 'name' - tap name
1017 * 'tap' - sharkd tap-name
1019 * (m) ftypes - conversation table for FT_ number to string, array of FT_xxx strings.
1021 * (m) nstat - available table-based taps, array of object with attributes:
1022 * 'name' - tap name
1023 * 'tap' - sharkd tap-name
1025 * (m) capture_types - available capture types, array of object with attributes:
1026 * 'name' - capture type name
1027 * 'description' - capture type description
1029 * (m) encap_types - available encapsulation types, array of object with attributes:
1030 * 'name' - encapsulation type name
1031 * 'description' - encapsulation type description
1033 static void
1034 sharkd_session_process_info(void)
1036 int i;
1038 sharkd_json_result_prologue(rpcid);
1040 sharkd_json_array_open("columns");
1041 for (i = 0; i < NUM_COL_FMTS; i++)
1043 const char *col_format = col_format_to_string(i);
1044 const char *col_descr = col_format_desc(i);
1046 json_dumper_begin_object(&dumper);
1047 sharkd_json_value_string("name", col_descr);
1048 sharkd_json_value_string("format", col_format);
1049 json_dumper_end_object(&dumper);
1051 sharkd_json_array_close();
1053 sharkd_json_array_open("stats");
1055 GList *cfg_list = stats_tree_get_cfg_list();
1056 GList *l;
1058 for (l = cfg_list; l; l = l->next)
1060 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
1062 json_dumper_begin_object(&dumper);
1063 sharkd_json_value_string("name", cfg->title);
1064 sharkd_json_value_stringf("tap", "stat:%s", cfg->abbr);
1065 json_dumper_end_object(&dumper);
1068 g_list_free(cfg_list);
1070 sharkd_json_array_close();
1072 sharkd_json_array_open("ftypes");
1073 for (i = 0; i < FT_NUM_TYPES; i++)
1074 sharkd_json_value_string(NULL, ftype_name((ftenum_t) i));
1075 sharkd_json_array_close();
1077 sharkd_json_array_open("capture_types");
1078 sharkd_session_print_capture_types();
1079 sharkd_json_array_close();
1081 sharkd_json_array_open("encap_types");
1082 sharkd_session_print_encap_types();
1083 sharkd_json_array_close();
1085 sharkd_json_value_string("version", get_ws_vcs_version_info_short());
1087 sharkd_json_array_open("nstat");
1088 i = 0;
1089 stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
1090 sharkd_json_array_close();
1092 sharkd_json_array_open("convs");
1093 i = 0;
1094 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
1095 sharkd_json_array_close();
1097 sharkd_json_array_open("seqa");
1098 i = 0;
1099 sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
1100 sharkd_json_array_close();
1102 sharkd_json_array_open("taps");
1104 json_dumper_begin_object(&dumper);
1105 sharkd_json_value_string("name", "UDP Multicast Streams");
1106 sharkd_json_value_string("tap", "multicast");
1107 json_dumper_end_object(&dumper);
1109 json_dumper_begin_object(&dumper);
1110 sharkd_json_value_string("name", "RTP streams");
1111 sharkd_json_value_string("tap", "rtp-streams");
1112 json_dumper_end_object(&dumper);
1114 json_dumper_begin_object(&dumper);
1115 sharkd_json_value_string("name", "Protocol Hierarchy Statistics");
1116 sharkd_json_value_string("tap", "phs");
1117 json_dumper_end_object(&dumper);
1119 json_dumper_begin_object(&dumper);
1120 sharkd_json_value_string("name", "VoIP Calls");
1121 sharkd_json_value_string("tap", "voip-calls");
1122 json_dumper_end_object(&dumper);
1124 json_dumper_begin_object(&dumper);
1125 sharkd_json_value_string("name", "VoIP Conversations");
1126 sharkd_json_value_string("tap", "voip-convs");
1127 json_dumper_end_object(&dumper);
1129 json_dumper_begin_object(&dumper);
1130 sharkd_json_value_string("name", "Expert Information");
1131 sharkd_json_value_string("tap", "expert");
1132 json_dumper_end_object(&dumper);
1134 sharkd_json_array_close();
1136 sharkd_json_array_open("eo");
1137 i = 0;
1138 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
1139 sharkd_json_array_close();
1141 sharkd_json_array_open("srt");
1142 i = 0;
1143 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
1144 sharkd_json_array_close();
1146 sharkd_json_array_open("rtd");
1147 i = 0;
1148 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
1149 sharkd_json_array_close();
1151 sharkd_json_array_open("follow");
1152 i = 0;
1153 follow_iterate_followers(sharkd_follower_visit_cb, &i);
1154 sharkd_json_array_close();
1156 sharkd_json_result_epilogue();
1160 * sharkd_session_process_load()
1162 * Process load request
1164 * Input:
1165 * (m) file - file to be loaded
1167 * Output object with attributes:
1168 * (m) err - error code
1170 static void
1171 sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
1173 const char *tok_file = json_find_attr(buf, tokens, count, "file");
1174 int err = 0;
1176 if (!tok_file)
1177 return;
1179 fprintf(stderr, "load: filename=%s\n", tok_file);
1181 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
1183 sharkd_json_error(
1184 rpcid, -2001, NULL,
1185 "Unable to open the file"
1187 return;
1192 err = sharkd_load_cap_file();
1194 CATCH(OutOfMemoryError)
1196 sharkd_json_error(
1197 rpcid, -32603, NULL,
1198 "Load failed, out of memory"
1200 fprintf(stderr, "load: OutOfMemoryError\n");
1201 err = ENOMEM;
1203 ENDTRY;
1205 if (err == 0)
1207 sharkd_json_simple_ok(rpcid);
1209 else
1211 sharkd_json_result_prologue(rpcid);
1212 sharkd_json_value_string("status", wtap_strerror(err));
1213 sharkd_json_value_anyf("err", "%d", err);
1214 sharkd_json_result_epilogue();
1220 * sharkd_session_process_status()
1222 * Process status request
1224 * Output object with attributes:
1225 * (m) frames - count of currently loaded frames
1226 * (m) duration - time difference between time of first frame, and last loaded frame
1227 * (o) filename - capture filename
1228 * (o) filesize - capture filesize
1229 * (o) columns - array of column titles
1230 * (o) column_info - array of column infos, array of object with attributes:
1231 * 'title' - column title
1232 * 'format' - column format (%x or %Cus:<expr>:<occurrence> if COL_CUSTOM)
1233 * 'visible' - true if column is visible
1234 * 'resolved' - true if column is resolved
1236 static void
1237 sharkd_session_process_status(void)
1239 sharkd_json_result_prologue(rpcid);
1241 sharkd_json_value_anyf("frames", "%u", cfile.count);
1242 sharkd_json_value_anyf("duration", "%.9f", nstime_to_sec(&cfile.elapsed_time));
1244 if (cfile.filename)
1246 char *name = g_path_get_basename(cfile.filename);
1248 sharkd_json_value_string("filename", name);
1249 g_free(name);
1252 if (cfile.provider.wth)
1254 gint64 file_size = wtap_file_size(cfile.provider.wth, NULL);
1256 if (file_size > 0)
1257 sharkd_json_value_anyf("filesize", "%" PRId64, file_size);
1260 if (cfile.cinfo.num_cols > 0)
1262 sharkd_json_array_open("columns");
1263 for (int i = 0; i < cfile.cinfo.num_cols; ++i)
1265 sharkd_json_value_string(NULL, get_column_title(i));
1267 sharkd_json_array_close();
1269 sharkd_json_array_open("column_info");
1270 for (int i = 0; i < cfile.cinfo.num_cols; ++i)
1272 int fmt = get_column_format(i);
1273 sharkd_json_object_open(NULL);
1274 sharkd_json_value_string("title", get_column_title(i));
1275 if (fmt != COL_CUSTOM)
1277 sharkd_json_value_string("format", col_format_to_string(fmt));
1278 } else {
1279 sharkd_json_value_stringf("format", "%s:%s:%d", col_format_to_string(fmt), get_column_custom_fields(i), get_column_custom_occurrence(i));
1281 sharkd_json_value_anyf("visible", get_column_visible(i) ? "true" : "false");
1282 sharkd_json_value_anyf("resolved", get_column_resolved(i) ? "true" : "false");
1283 sharkd_json_object_close();
1285 sharkd_json_array_close();
1288 sharkd_json_result_epilogue();
1291 struct sharkd_analyse_data
1293 GHashTable *protocols_set;
1294 nstime_t *first_time;
1295 nstime_t *last_time;
1298 static void
1299 sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree _U_,
1300 struct epan_column_info *cinfo _U_, const GSList *data_src _U_, void *data)
1302 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
1303 packet_info *pi = &edt->pi;
1304 frame_data *fdata = pi->fd;
1306 if (analyser->first_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
1307 analyser->first_time = &fdata->abs_ts;
1309 if (analyser->last_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
1310 analyser->last_time = &fdata->abs_ts;
1312 if (pi->layers)
1314 wmem_list_frame_t *frame;
1316 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
1318 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame));
1320 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
1322 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
1323 sharkd_json_value_string(NULL, proto_get_protocol_filter_name(proto_id));
1331 * sharkd_session_process_status()
1333 * Process analyse request
1335 * Output object with attributes:
1336 * (m) frames - count of currently loaded frames
1337 * (m) protocols - protocol list
1338 * (m) first - earliest frame time
1339 * (m) last - latest frame time
1341 static void
1342 sharkd_session_process_analyse(void)
1344 struct sharkd_analyse_data analyser;
1345 wtap_rec rec; /* Record metadata */
1346 Buffer rec_buf; /* Record data */
1348 analyser.first_time = NULL;
1349 analyser.last_time = NULL;
1350 analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
1352 sharkd_json_result_prologue(rpcid);
1354 sharkd_json_value_anyf("frames", "%u", cfile.count);
1356 sharkd_json_array_open("protocols");
1358 wtap_rec_init(&rec);
1359 ws_buffer_init(&rec_buf, 1514);
1361 for (guint32 framenum = 1; framenum <= cfile.count; framenum++)
1363 enum dissect_request_status status;
1364 int err;
1365 gchar *err_info;
1367 status = sharkd_dissect_request(framenum,
1368 (framenum != 1) ? 1 : 0, framenum - 1,
1369 &rec, &rec_buf, NULL, SHARKD_DISSECT_FLAG_NULL,
1370 &sharkd_session_process_analyse_cb, &analyser,
1371 &err, &err_info);
1372 switch (status) {
1374 case DISSECT_REQUEST_SUCCESS:
1375 break;
1377 case DISSECT_REQUEST_NO_SUCH_FRAME:
1378 /* XXX - report the error. */
1379 break;
1381 case DISSECT_REQUEST_READ_ERROR:
1383 * Free up the error string.
1384 * XXX - report the error.
1386 g_free(err_info);
1387 break;
1391 sharkd_json_array_close();
1393 if (analyser.first_time)
1394 sharkd_json_value_anyf("first", "%.9f", nstime_to_sec(analyser.first_time));
1396 if (analyser.last_time)
1397 sharkd_json_value_anyf("last", "%.9f", nstime_to_sec(analyser.last_time));
1399 sharkd_json_result_epilogue();
1401 wtap_rec_cleanup(&rec);
1402 ws_buffer_free(&rec_buf);
1404 g_hash_table_destroy(analyser.protocols_set);
1407 static column_info *
1408 sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
1410 const char *columns_custom[32];
1411 guint16 columns_fmt[32];
1412 gint16 columns_occur[32];
1414 int i, cols;
1416 for (i = 0; i < 32; i++)
1418 const char *tok_column;
1419 char tok_column_name[64];
1420 char *custom_sepa;
1422 snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
1423 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
1424 if (tok_column == NULL)
1425 break;
1427 columns_custom[i] = NULL;
1428 columns_occur[i] = 0;
1430 if ((custom_sepa = strchr(tok_column, ':')))
1432 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
1434 columns_fmt[i] = COL_CUSTOM;
1435 columns_custom[i] = tok_column;
1437 if (!ws_strtoi16(custom_sepa + 1, NULL, &columns_occur[i]))
1438 return NULL;
1440 else
1442 if (!ws_strtou16(tok_column, NULL, &columns_fmt[i]))
1443 return NULL;
1445 if (columns_fmt[i] >= NUM_COL_FMTS)
1446 return NULL;
1448 /* if custom, that it shouldn't be just custom number -> error */
1449 if (columns_fmt[i] == COL_CUSTOM)
1450 return NULL;
1454 cols = i;
1456 col_setup(cinfo, cols);
1458 for (i = 0; i < cols; i++)
1460 col_item_t *col_item = &cinfo->columns[i];
1462 col_item->col_fmt = columns_fmt[i];
1463 col_item->col_title = NULL; /* no need for title */
1465 if (col_item->col_fmt == COL_CUSTOM)
1467 col_item->col_custom_fields = g_strdup(columns_custom[i]);
1468 col_item->col_custom_occurrence = columns_occur[i];
1471 col_item->col_fence = 0;
1474 col_finalize(cinfo);
1476 return cinfo;
1479 static void
1480 sharkd_session_process_frames_cb(epan_dissect_t *edt, proto_tree *tree _U_,
1481 struct epan_column_info *cinfo, const GSList *data_src _U_, void *data _U_)
1483 packet_info *pi = &edt->pi;
1484 frame_data *fdata = pi->fd;
1485 wtap_block_t pkt_block = NULL;
1486 char *comment;
1488 json_dumper_begin_object(&dumper);
1490 sharkd_json_array_open("c");
1491 for (int col = 0; col < cinfo->num_cols; ++col)
1493 sharkd_json_value_string(NULL, get_column_text(cinfo, col));
1495 sharkd_json_array_close();
1497 sharkd_json_value_anyf("num", "%u", pi->num);
1500 * Get the block for this record, if it has one.
1502 if (fdata->has_modified_block)
1503 pkt_block = sharkd_get_modified_block(fdata);
1504 else
1505 pkt_block = pi->rec->block;
1508 * Does this record have any comments?
1510 if (pkt_block != NULL &&
1511 WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT, 0, &comment))
1512 sharkd_json_value_anyf("ct", "true");
1514 if (fdata->ignored)
1515 sharkd_json_value_anyf("i", "true");
1517 if (fdata->marked)
1518 sharkd_json_value_anyf("m", "true");
1520 if (fdata->color_filter)
1522 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
1523 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
1526 json_dumper_end_object(&dumper);
1530 * sharkd_session_process_frames()
1532 * Process frames request
1534 * Input:
1535 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurrence>).
1536 * If column0 is not specified default column set will be used.
1537 * (o) filter - filter to be used
1538 * (o) skip=N - skip N frames
1539 * (o) limit=N - show only N frames
1540 * (o) refs - list (comma separated) with sorted time reference frame numbers.
1542 * Output array of frames with attributes:
1543 * (m) c - array of column data
1544 * (m) num - frame number
1545 * (o) i - if frame is ignored
1546 * (o) m - if frame is marked
1547 * (o) ct - if frame is commented
1548 * (o) bg - color filter - background color in hex
1549 * (o) fg - color filter - foreground color in hex
1551 static void
1552 sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
1554 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1555 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
1556 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
1557 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
1558 const char *tok_refs = json_find_attr(buf, tokens, count, "refs");
1560 const guint8 *filter_data = NULL;
1562 guint32 prev_dis_num = 0;
1563 guint32 current_ref_frame = 0, next_ref_frame = G_MAXUINT32;
1564 guint32 skip;
1565 guint32 limit;
1567 wtap_rec rec; /* Record metadata */
1568 Buffer rec_buf; /* Record data */
1569 column_info *cinfo = &cfile.cinfo;
1570 column_info user_cinfo;
1572 if (tok_column)
1574 memset(&user_cinfo, 0, sizeof(user_cinfo));
1575 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
1576 if (!cinfo)
1578 sharkd_json_error(
1579 rpcid, -13001, NULL,
1580 "Column definition invalid - note column 6 requires a custom definition"
1582 return;
1586 if (tok_filter)
1588 const struct sharkd_filter_item *filter_item;
1590 filter_item = sharkd_session_filter_data(tok_filter);
1591 if (!filter_item)
1593 sharkd_json_error(
1594 rpcid, -13002, NULL,
1595 "Filter expression invalid"
1597 return;
1600 filter_data = filter_item->filtered;
1603 skip = 0;
1604 if (tok_skip)
1606 if (!ws_strtou32(tok_skip, NULL, &skip))
1607 return;
1610 limit = 0;
1611 if (tok_limit)
1613 if (!ws_strtou32(tok_limit, NULL, &limit))
1614 return;
1617 if (tok_refs)
1619 if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame))
1620 return;
1623 sharkd_json_result_array_prologue(rpcid);
1625 wtap_rec_init(&rec);
1626 ws_buffer_init(&rec_buf, 1514);
1628 for (guint32 framenum = 1; framenum <= cfile.count; framenum++)
1630 frame_data *fdata;
1631 guint32 ref_frame = (framenum != 1) ? 1 : 0;
1632 enum dissect_request_status status;
1633 int err;
1634 gchar *err_info;
1636 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
1637 continue;
1639 if (skip)
1641 skip--;
1642 prev_dis_num = framenum;
1643 continue;
1646 if (tok_refs)
1648 if (framenum >= next_ref_frame)
1650 current_ref_frame = next_ref_frame;
1652 if (*tok_refs != ',')
1653 next_ref_frame = G_MAXUINT32;
1655 while (*tok_refs == ',' && framenum >= next_ref_frame)
1657 current_ref_frame = next_ref_frame;
1659 if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame))
1661 fprintf(stderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs);
1662 break;
1666 if (*tok_refs == '\0' && framenum >= next_ref_frame)
1668 current_ref_frame = next_ref_frame;
1669 next_ref_frame = G_MAXUINT32;
1673 if (current_ref_frame)
1674 ref_frame = current_ref_frame;
1677 fdata = sharkd_get_frame(framenum);
1678 status = sharkd_dissect_request(framenum,
1679 ref_frame, prev_dis_num,
1680 &rec, &rec_buf, cinfo,
1681 (fdata->color_filter == NULL) ? SHARKD_DISSECT_FLAG_COLOR : SHARKD_DISSECT_FLAG_NULL,
1682 &sharkd_session_process_frames_cb, NULL,
1683 &err, &err_info);
1684 switch (status) {
1686 case DISSECT_REQUEST_SUCCESS:
1687 break;
1689 case DISSECT_REQUEST_NO_SUCH_FRAME:
1690 /* XXX - report the error. */
1691 break;
1693 case DISSECT_REQUEST_READ_ERROR:
1695 * Free up the error string.
1696 * XXX - report the error.
1698 g_free(err_info);
1699 break;
1702 prev_dis_num = framenum;
1704 if (limit && --limit == 0)
1705 break;
1707 sharkd_json_result_array_epilogue();
1709 if (cinfo != &cfile.cinfo)
1710 col_cleanup(cinfo);
1712 wtap_rec_cleanup(&rec);
1713 ws_buffer_free(&rec_buf);
1716 static void
1717 sharkd_session_process_tap_stats_node_cb(const char *key, const stat_node *n)
1719 stat_node *node;
1721 sharkd_json_array_open(key);
1722 for (node = n->children; node; node = node->next)
1724 json_dumper_begin_object(&dumper);
1726 /* code based on stats_tree_get_values_from_node() */
1727 sharkd_json_value_string("name", node->name);
1728 sharkd_json_value_anyf("count", "%d", node->counter);
1729 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
1731 switch(node->datatype)
1733 case STAT_DT_INT:
1734 sharkd_json_value_anyf("avg", "%.2f", ((float)node->total.int_total) / node->counter);
1735 sharkd_json_value_anyf("min", "%d", node->minvalue.int_min);
1736 sharkd_json_value_anyf("max", "%d", node->maxvalue.int_max);
1737 break;
1738 case STAT_DT_FLOAT:
1739 sharkd_json_value_anyf("avg", "%.2f", node->total.float_total / node->counter);
1740 sharkd_json_value_anyf("min", "%f", node->minvalue.float_min);
1741 sharkd_json_value_anyf("max", "%f", node->maxvalue.float_max);
1742 break;
1746 if (node->st->elapsed)
1747 sharkd_json_value_anyf("rate", "%.4f", ((float)node->counter) / node->st->elapsed);
1749 if (node->parent && node->parent->counter)
1750 sharkd_json_value_anyf("perc", "%.2f", (node->counter * 100.0) / node->parent->counter);
1751 else if (node->parent == &(node->st->root))
1752 sharkd_json_value_anyf("perc", "100");
1754 if (prefs.st_enable_burstinfo && node->max_burst)
1756 if (prefs.st_burst_showcount)
1757 sharkd_json_value_anyf("burstcount", "%d", node->max_burst);
1758 else
1759 sharkd_json_value_anyf("burstrate", "%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
1761 sharkd_json_value_anyf("bursttime", "%.3f", (node->burst_time / 1000.0));
1764 if (node->children)
1766 sharkd_session_process_tap_stats_node_cb("sub", node);
1768 json_dumper_end_object(&dumper);
1770 sharkd_json_array_close();
1774 * sharkd_session_process_tap_stats_cb()
1776 * Output stats tap:
1778 * (m) tap - tap name
1779 * (m) type:stats - tap output type
1780 * (m) name - stat name
1781 * (m) stats - array of object with attributes:
1782 * (m) name - stat item name
1783 * (m) count - stat item counter
1784 * (o) avg - stat item averange value
1785 * (o) min - stat item min value
1786 * (o) max - stat item max value
1787 * (o) rate - stat item rate value (ms)
1788 * (o) perc - stat item percentage
1789 * (o) burstrate - stat item burst rate
1790 * (o) burstcount - stat item burst count
1791 * (o) burstttme - stat item burst start
1792 * (o) sub - array of object with attributes like in stats node.
1794 static void
1795 sharkd_session_process_tap_stats_cb(void *psp)
1797 stats_tree *st = (stats_tree *) psp;
1799 json_dumper_begin_object(&dumper);
1801 sharkd_json_value_stringf("tap", "stats:%s", st->cfg->abbr);
1802 sharkd_json_value_string("type", "stats");
1803 sharkd_json_value_string("name", st->cfg->path);
1805 sharkd_session_process_tap_stats_node_cb("stats", &st->root);
1807 json_dumper_end_object(&dumper);
1810 static void
1811 sharkd_session_free_tap_stats_cb(void *psp)
1813 stats_tree *st = (stats_tree *) psp;
1815 stats_tree_free(st);
1818 struct sharkd_expert_tap
1820 GSList *details;
1821 GStringChunk *text;
1825 * sharkd_session_process_tap_expert_cb()
1827 * Output expert tap:
1829 * (m) tap - tap name
1830 * (m) type:expert - tap output type
1831 * (m) details - array of object with attributes:
1832 * (m) f - frame number, which generated expert information
1833 * (o) s - severity
1834 * (o) g - group
1835 * (m) m - expert message
1836 * (o) p - protocol
1838 static void
1839 sharkd_session_process_tap_expert_cb(void *tapdata)
1841 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1842 GSList *list;
1844 json_dumper_begin_object(&dumper);
1846 sharkd_json_value_string("tap", "expert");
1847 sharkd_json_value_string("type", "expert");
1849 sharkd_json_array_open("details");
1850 for (list = etd->details; list; list = list->next)
1852 expert_info_t *ei = (expert_info_t *) list->data;
1853 const char *tmp;
1855 json_dumper_begin_object(&dumper);
1857 sharkd_json_value_anyf("f", "%u", ei->packet_num);
1859 tmp = try_val_to_str(ei->severity, expert_severity_vals);
1860 if (tmp)
1861 sharkd_json_value_string("s", tmp);
1863 tmp = try_val_to_str(ei->group, expert_group_vals);
1864 if (tmp)
1865 sharkd_json_value_string("g", tmp);
1867 sharkd_json_value_string("m", ei->summary);
1869 if (ei->protocol)
1870 sharkd_json_value_string("p", ei->protocol);
1872 json_dumper_end_object(&dumper);
1874 sharkd_json_array_close();
1876 json_dumper_end_object(&dumper);
1879 static tap_packet_status
1880 sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer, tap_flags_t flags _U_)
1882 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1883 const expert_info_t *ei = (const expert_info_t *) pointer;
1884 expert_info_t *ei_copy;
1886 if (ei == NULL)
1887 return TAP_PACKET_DONT_REDRAW;
1889 ei_copy = g_new(expert_info_t, 1);
1890 /* Note: this is a shallow copy */
1891 *ei_copy = *ei;
1893 /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */
1894 ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
1895 ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary);
1897 etd->details = g_slist_prepend(etd->details, ei_copy);
1899 return TAP_PACKET_REDRAW;
1902 static void
1903 sharkd_session_free_tap_expert_cb(void *tapdata)
1905 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1907 g_slist_free_full(etd->details, g_free);
1908 g_string_chunk_free(etd->text);
1909 g_free(etd);
1913 * sharkd_session_process_tap_flow_cb()
1915 * Output flow tap:
1916 * (m) tap - tap name
1917 * (m) type:flow - tap output type
1918 * (m) nodes - array of strings with node address
1919 * (m) flows - array of object with attributes:
1920 * (m) t - frame time string
1921 * (m) n - array of two numbers with source node index and destination node index
1922 * (m) pn - array of two numbers with source and destination port
1923 * (o) c - comment
1925 static void
1926 sharkd_session_process_tap_flow_cb(void *tapdata)
1928 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1929 GList *flow_list;
1930 guint i;
1932 sequence_analysis_get_nodes(graph_analysis);
1934 json_dumper_begin_object(&dumper);
1935 sharkd_json_value_stringf("tap", "seqa:%s", graph_analysis->name);
1936 sharkd_json_value_string("type", "flow");
1938 sharkd_json_array_open("nodes");
1939 for (i = 0; i < graph_analysis->num_nodes; i++)
1941 char *addr_str;
1943 addr_str = address_to_display(NULL, &(graph_analysis->nodes[i]));
1944 sharkd_json_value_string(NULL, addr_str);
1945 wmem_free(NULL, addr_str);
1947 sharkd_json_array_close();
1949 sharkd_json_array_open("flows");
1950 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
1951 while (flow_list)
1953 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
1955 flow_list = g_list_next(flow_list);
1957 if (!sai->display)
1958 continue;
1960 json_dumper_begin_object(&dumper);
1962 sharkd_json_value_string("t", sai->time_str);
1963 sharkd_json_value_anyf("n", "[%u,%u]", sai->src_node, sai->dst_node);
1964 sharkd_json_value_anyf("pn", "[%u,%u]", sai->port_src, sai->port_dst);
1966 if (sai->comment)
1967 sharkd_json_value_string("c", sai->comment);
1969 json_dumper_end_object(&dumper);
1971 sharkd_json_array_close();
1973 json_dumper_end_object(&dumper);
1976 static void
1977 sharkd_session_free_tap_flow_cb(void *tapdata)
1979 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1981 sequence_analysis_info_free(graph_analysis);
1984 struct sharkd_conv_tap_data
1986 const char *type;
1987 conv_hash_t hash;
1988 gboolean resolve_name;
1989 gboolean resolve_port;
1992 static gboolean
1993 sharkd_session_geoip_addr(address *addr, const char *suffix)
1995 const mmdb_lookup_t *lookup = NULL;
1996 gboolean with_geoip = FALSE;
1997 char json_key[64];
1999 if (addr->type == AT_IPv4)
2001 const ws_in4_addr *ip4 = (const ws_in4_addr *) addr->data;
2003 lookup = maxmind_db_lookup_ipv4(ip4);
2005 else if (addr->type == AT_IPv6)
2007 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
2009 lookup = maxmind_db_lookup_ipv6(ip6);
2012 if (!lookup || !lookup->found)
2013 return FALSE;
2015 if (lookup->country)
2017 snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix);
2018 sharkd_json_value_string(json_key, lookup->country);
2019 with_geoip = TRUE;
2022 if (lookup->country_iso)
2024 snprintf(json_key, sizeof(json_key), "geoip_country_iso%s", suffix);
2025 sharkd_json_value_string(json_key, lookup->country_iso);
2026 with_geoip = TRUE;
2029 if (lookup->city)
2031 snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix);
2032 sharkd_json_value_string(json_key, lookup->city);
2033 with_geoip = TRUE;
2036 if (lookup->as_org)
2038 snprintf(json_key, sizeof(json_key), "geoip_as_org%s", suffix);
2039 sharkd_json_value_string(json_key, lookup->as_org);
2040 with_geoip = TRUE;
2043 if (lookup->as_number > 0)
2045 snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix);
2046 sharkd_json_value_anyf(json_key, "%u", lookup->as_number);
2047 with_geoip = TRUE;
2050 if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
2052 snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix);
2053 sharkd_json_value_anyf(json_key, "%f", lookup->latitude);
2054 with_geoip = TRUE;
2057 if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
2059 snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix);
2060 sharkd_json_value_anyf(json_key, "%f", lookup->longitude);
2061 with_geoip = TRUE;
2064 return with_geoip;
2067 struct sharkd_analyse_rtp_items
2069 guint32 frame_num;
2070 guint32 sequence_num;
2072 double delta;
2073 double jitter;
2074 double skew;
2075 double bandwidth;
2076 gboolean marker;
2078 double arrive_offset;
2080 /* from tap_rtp_stat_t */
2081 guint32 flags;
2082 guint16 pt;
2085 struct sharkd_analyse_rtp
2087 const char *tap_name;
2088 rtpstream_id_t id;
2090 GSList *packets;
2091 double start_time;
2092 tap_rtp_stat_t statinfo;
2095 static void
2096 sharkd_session_process_tap_rtp_free_cb(void *tapdata)
2098 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2100 g_slist_free_full(rtp_req->packets, g_free);
2101 g_free(rtp_req);
2104 static tap_packet_status
2105 sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pointer, tap_flags_t flags _U_)
2107 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2108 const struct _rtp_info *rtp_info = (const struct _rtp_info *) pointer;
2110 if (rtpstream_id_equal_pinfo_rtp_info(&rtp_req->id, pinfo, rtp_info))
2112 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
2113 struct sharkd_analyse_rtp_items *item;
2115 rtppacket_analyse(statinfo, pinfo, rtp_info);
2117 item = g_new(struct sharkd_analyse_rtp_items, 1);
2119 if (!rtp_req->packets)
2120 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
2122 item->frame_num = pinfo->num;
2123 item->sequence_num = rtp_info->info_seq_num;
2124 item->delta = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->delta;
2125 item->jitter = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->jitter;
2126 item->skew = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->skew;
2127 item->bandwidth = statinfo->bandwidth;
2128 item->marker = rtp_info->info_marker_set ? TRUE : FALSE;
2129 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
2131 item->flags = statinfo->flags;
2132 item->pt = statinfo->pt;
2134 /* XXX, O(n) optimize */
2135 rtp_req->packets = g_slist_append(rtp_req->packets, item);
2138 return TAP_PACKET_REDRAW;
2142 * sharkd_session_process_tap_rtp_analyse_cb()
2144 * Output rtp analyse tap:
2145 * (m) tap - tap name
2146 * (m) type - tap output type
2147 * (m) ssrc - RTP SSRC
2148 * (m) max_delta - Max delta (ms)
2149 * (m) max_delta_nr - Max delta packet #
2150 * (m) max_jitter - Max jitter (ms)
2151 * (m) mean_jitter - Mean jitter (ms)
2152 * (m) max_skew - Max skew (ms)
2153 * (m) total_nr - Total number of RTP packets
2154 * (m) seq_err - Number of sequence errors
2155 * (m) duration - Duration (ms)
2156 * (m) items - array of object with attributes:
2157 * (m) f - frame number
2158 * (m) o - arrive offset
2159 * (m) sn - sequence number
2160 * (m) d - delta
2161 * (m) j - jitter
2162 * (m) sk - skew
2163 * (m) bw - bandwidth
2164 * (o) s - status string
2165 * (o) t - status type
2166 * (o) mark - rtp mark
2168 static void
2169 sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
2171 const int RTP_TYPE_CN = 1;
2172 const int RTP_TYPE_ERROR = 2;
2173 const int RTP_TYPE_WARN = 3;
2174 const int RTP_TYPE_PT_EVENT = 4;
2176 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2177 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
2179 GSList *l;
2181 json_dumper_begin_object(&dumper);
2183 sharkd_json_value_string("tap", rtp_req->tap_name);
2184 sharkd_json_value_string("type", "rtp-analyse");
2185 sharkd_json_value_stringf("ssrc", "0x%x", rtp_req->id.ssrc);
2187 sharkd_json_value_anyf("max_delta", "%f", statinfo->max_delta);
2188 sharkd_json_value_anyf("max_delta_nr", "%u", statinfo->max_nr);
2189 sharkd_json_value_anyf("max_jitter", "%f", statinfo->max_jitter);
2190 sharkd_json_value_anyf("mean_jitter", "%f", statinfo->mean_jitter);
2191 sharkd_json_value_anyf("max_skew", "%f", statinfo->max_skew);
2192 sharkd_json_value_anyf("total_nr", "%u", statinfo->total_nr);
2193 sharkd_json_value_anyf("seq_err", "%u", statinfo->sequence);
2194 sharkd_json_value_anyf("duration", "%f", statinfo->time - statinfo->start_time);
2196 sharkd_json_array_open("items");
2197 for (l = rtp_req->packets; l; l = l->next)
2199 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
2201 json_dumper_begin_object(&dumper);
2203 sharkd_json_value_anyf("f", "%u", item->frame_num);
2204 sharkd_json_value_anyf("o", "%.9f", item->arrive_offset);
2205 sharkd_json_value_anyf("sn", "%u", item->sequence_num);
2206 sharkd_json_value_anyf("d", "%.2f", item->delta);
2207 sharkd_json_value_anyf("j", "%.2f", item->jitter);
2208 sharkd_json_value_anyf("sk", "%.2f", item->skew);
2209 sharkd_json_value_anyf("bw", "%.2f", item->bandwidth);
2211 if (item->pt == PT_CN)
2213 sharkd_json_value_string("s", "Comfort noise (PT=13, RFC 3389)");
2214 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2216 else if (item->pt == PT_CN_OLD)
2218 sharkd_json_value_string("s", "Comfort noise (PT=19, reserved)");
2219 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2221 else if (item->flags & STAT_FLAG_WRONG_SEQ)
2223 sharkd_json_value_string("s", "Wrong sequence number");
2224 sharkd_json_value_anyf("t", "%d", RTP_TYPE_ERROR);
2226 else if (item->flags & STAT_FLAG_DUP_PKT)
2228 sharkd_json_value_string("s", "Suspected duplicate (MAC address) only delta time calculated");
2229 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2231 else if (item->flags & STAT_FLAG_REG_PT_CHANGE)
2233 sharkd_json_value_stringf("s", "Payload changed to PT=%u%s",
2234 item->pt,
2235 (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "");
2236 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2238 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP)
2240 sharkd_json_value_string("s", "Incorrect timestamp");
2241 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2243 else if ((item->flags & STAT_FLAG_PT_CHANGE)
2244 && !(item->flags & STAT_FLAG_FIRST)
2245 && !(item->flags & STAT_FLAG_PT_CN)
2246 && (item->flags & STAT_FLAG_FOLLOW_PT_CN)
2247 && !(item->flags & STAT_FLAG_MARKER))
2249 sharkd_json_value_string("s", "Marker missing?");
2250 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2252 else if (item->flags & STAT_FLAG_PT_T_EVENT)
2254 sharkd_json_value_stringf("s", "PT=%u telephone/event", item->pt);
2255 sharkd_json_value_anyf("t", "%d", RTP_TYPE_PT_EVENT);
2257 else if (item->flags & STAT_FLAG_MARKER)
2259 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2262 if (item->marker)
2263 sharkd_json_value_anyf("mark", "1");
2265 json_dumper_end_object(&dumper);
2267 sharkd_json_array_close();
2269 json_dumper_end_object(&dumper);
2273 * sharkd_session_process_tap_conv_cb()
2275 * Output conv tap:
2276 * (m) tap - tap name
2277 * (m) type - tap output type
2278 * (m) proto - protocol short name
2279 * (o) filter - filter string
2280 * (o) geoip - whether GeoIP information is available, boolean
2282 * (o) convs - array of object with attributes:
2283 * (m) saddr - source address
2284 * (m) daddr - destination address
2285 * (o) sport - source port
2286 * (o) dport - destination port
2287 * (m) txf - TX frame count
2288 * (m) txb - TX bytes
2289 * (m) rxf - RX frame count
2290 * (m) rxb - RX bytes
2291 * (m) start - (relative) first packet time
2292 * (m) stop - (relative) last packet time
2293 * (o) filter - conversation filter
2295 * (o) hosts - array of object with attributes:
2296 * (m) host - host address
2297 * (o) port - host port
2298 * (m) txf - TX frame count
2299 * (m) txb - TX bytes
2300 * (m) rxf - RX frame count
2301 * (m) rxb - RX bytes
2303 static void
2304 sharkd_session_process_tap_conv_cb(void *arg)
2306 conv_hash_t *hash = (conv_hash_t *) arg;
2307 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2308 const char *proto;
2309 int proto_with_port;
2310 guint i;
2312 int with_geoip = 0;
2314 json_dumper_begin_object(&dumper);
2315 sharkd_json_value_string("tap", iu->type);
2317 if (!strncmp(iu->type, "conv:", 5))
2319 sharkd_json_value_string("type", "conv");
2320 sharkd_json_array_open("convs");
2321 proto = iu->type + 5;
2323 else if (!strncmp(iu->type, "endpt:", 6))
2325 sharkd_json_value_string("type", "host");
2326 sharkd_json_array_open("hosts");
2327 proto = iu->type + 6;
2329 else
2331 sharkd_json_value_string("type", "err");
2332 proto = "";
2335 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
2337 if (iu->hash.conv_array != NULL && !strncmp(iu->type, "conv:", 5))
2339 for (i = 0; i < iu->hash.conv_array->len; i++)
2341 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
2342 char *src_addr, *dst_addr;
2343 char *src_port, *dst_port;
2344 char *filter_str;
2346 json_dumper_begin_object(&dumper);
2348 sharkd_json_value_string("saddr", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
2349 sharkd_json_value_string("daddr", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
2351 if (proto_with_port)
2353 sharkd_json_value_string("sport", (src_port = get_conversation_port(NULL, iui->src_port, iui->ctype, iu->resolve_port)));
2354 sharkd_json_value_string("dport", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->ctype, iu->resolve_port)));
2356 wmem_free(NULL, src_port);
2357 wmem_free(NULL, dst_port);
2360 sharkd_json_value_anyf("rxf", "%" PRIu64, iui->rx_frames);
2361 sharkd_json_value_anyf("rxb", "%" PRIu64, iui->rx_bytes);
2363 sharkd_json_value_anyf("txf", "%" PRIu64, iui->tx_frames);
2364 sharkd_json_value_anyf("txb", "%" PRIu64, iui->tx_bytes);
2366 sharkd_json_value_anyf("start", "%.9f", nstime_to_sec(&iui->start_time));
2367 sharkd_json_value_anyf("stop", "%.9f", nstime_to_sec(&iui->stop_time));
2369 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
2370 if (filter_str)
2372 sharkd_json_value_string("filter", filter_str);
2373 g_free(filter_str);
2376 wmem_free(NULL, src_addr);
2377 wmem_free(NULL, dst_addr);
2379 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
2380 with_geoip = 1;
2381 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
2382 with_geoip = 1;
2384 json_dumper_end_object(&dumper);
2387 else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
2389 for (i = 0; i < iu->hash.conv_array->len; i++)
2391 endpoint_item_t *endpoint = &g_array_index(iu->hash.conv_array, endpoint_item_t, i);
2392 char *host_str, *port_str;
2393 char *filter_str;
2395 json_dumper_begin_object(&dumper);
2397 sharkd_json_value_string("host", (host_str = get_conversation_address(NULL, &endpoint->myaddress, iu->resolve_name)));
2399 if (proto_with_port)
2401 sharkd_json_value_string("port", (port_str = get_endpoint_port(NULL, endpoint, iu->resolve_port)));
2403 wmem_free(NULL, port_str);
2406 sharkd_json_value_anyf("rxf", "%" PRIu64, endpoint->rx_frames);
2407 sharkd_json_value_anyf("rxb", "%" PRIu64, endpoint->rx_bytes);
2409 sharkd_json_value_anyf("txf", "%" PRIu64, endpoint->tx_frames);
2410 sharkd_json_value_anyf("txb", "%" PRIu64, endpoint->tx_bytes);
2412 filter_str = get_endpoint_filter(endpoint);
2413 if (filter_str)
2415 sharkd_json_value_string("filter", filter_str);
2416 g_free(filter_str);
2419 wmem_free(NULL, host_str);
2421 if (sharkd_session_geoip_addr(&(endpoint->myaddress), ""))
2422 with_geoip = 1;
2423 json_dumper_end_object(&dumper);
2426 sharkd_json_array_close();
2428 sharkd_json_value_string("proto", proto);
2429 sharkd_json_value_anyf("geoip", with_geoip ? "true" : "false");
2431 json_dumper_end_object(&dumper);
2434 static void
2435 sharkd_session_free_tap_conv_cb(void *arg)
2437 conv_hash_t *hash = (conv_hash_t *) arg;
2438 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2440 if (!strncmp(iu->type, "conv:", 5))
2442 reset_conversation_table_data(hash);
2444 else if (!strncmp(iu->type, "endpt:", 6))
2446 reset_endpoint_table_data(hash);
2449 g_free(iu);
2453 * sharkd_session_process_tap_nstat_cb()
2455 * Output nstat tap:
2456 * (m) tap - tap name
2457 * (m) type - tap output type
2458 * (m) fields: array of objects with attributes:
2459 * (m) c - name
2461 * (m) tables: array of object with attributes:
2462 * (m) t - table title
2463 * (m) i - array of items
2465 static void
2466 sharkd_session_process_tap_nstat_cb(void *arg)
2468 stat_data_t *stat_data = (stat_data_t *) arg;
2469 guint i, j, k;
2471 json_dumper_begin_object(&dumper);
2472 sharkd_json_value_stringf("tap", "nstat:%s", stat_data->stat_tap_data->cli_string);
2473 sharkd_json_value_string("type", "nstat");
2475 sharkd_json_array_open("fields");
2476 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
2478 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
2480 json_dumper_begin_object(&dumper);
2481 sharkd_json_value_string("c", field->column_name);
2482 json_dumper_end_object(&dumper);
2484 sharkd_json_array_close();
2486 sharkd_json_array_open("tables");
2487 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
2489 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
2491 json_dumper_begin_object(&dumper);
2493 sharkd_json_value_string("t", table->title);
2495 sharkd_json_array_open("i");
2496 for (j = 0; j < table->num_elements; j++)
2498 stat_tap_table_item_type *field_data;
2500 field_data = stat_tap_get_field_data(table, j, 0);
2501 if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
2502 continue;
2504 sharkd_json_array_open(NULL);
2505 for (k = 0; k < table->num_fields; k++)
2507 field_data = stat_tap_get_field_data(table, j, k);
2509 switch (field_data->type)
2511 case TABLE_ITEM_UINT:
2512 sharkd_json_value_anyf(NULL, "%u", field_data->value.uint_value);
2513 break;
2515 case TABLE_ITEM_INT:
2516 sharkd_json_value_anyf(NULL, "%d", field_data->value.int_value);
2517 break;
2519 case TABLE_ITEM_STRING:
2520 sharkd_json_value_string(NULL, field_data->value.string_value);
2521 break;
2523 case TABLE_ITEM_FLOAT:
2524 sharkd_json_value_anyf(NULL, "%f", field_data->value.float_value);
2525 break;
2527 case TABLE_ITEM_ENUM:
2528 sharkd_json_value_anyf(NULL, "%d", field_data->value.enum_value);
2529 break;
2531 case TABLE_ITEM_NONE:
2532 sharkd_json_value_anyf(NULL, "null");
2533 break;
2537 sharkd_json_array_close();
2539 sharkd_json_array_close();
2540 json_dumper_end_object(&dumper);
2542 sharkd_json_array_close();
2544 json_dumper_end_object(&dumper);
2547 static void
2548 sharkd_session_free_tap_nstat_cb(void *arg)
2550 stat_data_t *stat_data = (stat_data_t *) arg;
2552 free_stat_tables(stat_data->stat_tap_data);
2556 * sharkd_session_process_tap_rtd_cb()
2558 * Output rtd tap:
2559 * (m) tap - tap name
2560 * (m) type - tap output type
2561 * (m) stats - statistics rows - array object with attributes:
2562 * (m) type - statistic name
2563 * (m) num - number of messages
2564 * (m) min - minimum SRT time
2565 * (m) max - maximum SRT time
2566 * (m) tot - total SRT time
2567 * (m) min_frame - minimal SRT
2568 * (m) max_frame - maximum SRT
2569 * (o) open_req - Open Requests
2570 * (o) disc_rsp - Discarded Responses
2571 * (o) req_dup - Duplicated Requests
2572 * (o) rsp_dup - Duplicated Responses
2573 * (o) open_req - Open Requests
2574 * (o) disc_rsp - Discarded Responses
2575 * (o) req_dup - Duplicated Requests
2576 * (o) rsp_dup - Duplicated Responses
2578 static void
2579 sharkd_session_process_tap_rtd_cb(void *arg)
2581 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2582 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
2584 guint i, j;
2586 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
2588 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
2589 * some multiple table and single timestat (radius, h225)
2590 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
2591 * I wonder how it will gonna work with multiple timestats and multiple tables...
2592 * (for usage grep for: register_rtd_table)
2594 const value_string *vs = get_rtd_value_string(rtd);
2596 json_dumper_begin_object(&dumper);
2597 sharkd_json_value_stringf("tap", "rtd:%s", filter);
2598 sharkd_json_value_string("type", "rtd");
2600 if (rtd_data->stat_table.num_rtds == 1)
2602 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
2604 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2605 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2606 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2607 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2610 sharkd_json_array_open("stats");
2611 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
2613 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
2615 for (j = 0; j < ms->num_timestat; j++)
2617 const char *type_str;
2619 if (ms->rtd[j].num == 0)
2620 continue;
2622 json_dumper_begin_object(&dumper);
2624 if (rtd_data->stat_table.num_rtds == 1)
2625 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
2626 else
2627 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
2628 sharkd_json_value_string("type", type_str);
2630 sharkd_json_value_anyf("num", "%u", ms->rtd[j].num);
2631 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&(ms->rtd[j].min)));
2632 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&(ms->rtd[j].max)));
2633 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
2634 sharkd_json_value_anyf("min_frame", "%u", ms->rtd[j].min_num);
2635 sharkd_json_value_anyf("max_frame", "%u", ms->rtd[j].max_num);
2637 if (rtd_data->stat_table.num_rtds != 1)
2639 /* like in tshark, display it on every row */
2640 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2641 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2642 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2643 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2646 json_dumper_end_object(&dumper);
2649 sharkd_json_array_close();
2651 json_dumper_end_object(&dumper);
2654 static void
2655 sharkd_session_free_tap_rtd_cb(void *arg)
2657 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2659 free_rtd_table(&rtd_data->stat_table);
2660 g_free(rtd_data);
2664 * sharkd_session_process_tap_srt_cb()
2666 * Output srt tap:
2667 * (m) tap - tap name
2668 * (m) type - tap output type
2670 * (m) tables - array of object with attributes:
2671 * (m) n - table name
2672 * (m) f - table filter
2673 * (o) c - table column name
2674 * (m) r - table rows - array object with attributes:
2675 * (m) n - row name
2676 * (m) idx - procedure index
2677 * (m) num - number of events
2678 * (m) min - minimum SRT time
2679 * (m) max - maximum SRT time
2680 * (m) tot - total SRT time
2682 static void
2683 sharkd_session_process_tap_srt_cb(void *arg)
2685 srt_data_t *srt_data = (srt_data_t *) arg;
2686 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2688 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
2690 guint i;
2692 json_dumper_begin_object(&dumper);
2693 sharkd_json_value_stringf("tap", "srt:%s", filter);
2694 sharkd_json_value_string("type", "srt");
2696 sharkd_json_array_open("tables");
2697 for (i = 0; i < srt_data->srt_array->len; i++)
2699 /* SRT table */
2700 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i);
2702 int j;
2704 json_dumper_begin_object(&dumper);
2706 if (rst->name)
2707 sharkd_json_value_string("n", rst->name);
2708 else if (rst->short_name)
2709 sharkd_json_value_string("n", rst->short_name);
2710 else
2711 sharkd_json_value_stringf("n", "table%u", i);
2713 if (rst->filter_string)
2714 sharkd_json_value_string("f", rst->filter_string);
2716 if (rst->proc_column_name)
2717 sharkd_json_value_string("c", rst->proc_column_name);
2719 sharkd_json_array_open("r");
2720 for (j = 0; j < rst->num_procs; j++)
2722 /* SRT row */
2723 srt_procedure_t *proc = &rst->procedures[j];
2725 if (proc->stats.num == 0)
2726 continue;
2728 json_dumper_begin_object(&dumper);
2730 sharkd_json_value_string("n", proc->procedure);
2732 if (rst->filter_string)
2733 sharkd_json_value_anyf("idx", "%d", proc->proc_index);
2735 sharkd_json_value_anyf("num", "%u", proc->stats.num);
2737 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&proc->stats.min));
2738 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&proc->stats.max));
2739 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&proc->stats.tot));
2741 json_dumper_end_object(&dumper);
2743 sharkd_json_array_close();
2745 json_dumper_end_object(&dumper);
2747 sharkd_json_array_close();
2749 json_dumper_end_object(&dumper);
2752 static void
2753 sharkd_session_free_tap_srt_cb(void *arg)
2755 srt_data_t *srt_data = (srt_data_t *) arg;
2756 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2758 free_srt_table(srt, srt_data->srt_array);
2759 g_array_free(srt_data->srt_array, TRUE);
2760 g_free(srt_data);
2763 static void
2764 sharkd_session_process_tap_phs_cb_aux(phs_t *rs)
2766 for (; rs; rs = rs->sibling) {
2767 if (rs->protocol == -1) {
2768 return;
2770 sharkd_json_object_open(NULL);
2771 sharkd_json_value_string("proto", rs->proto_name);
2772 sharkd_json_value_anyf("frames", "%"PRIu32, rs->frames);
2773 sharkd_json_value_anyf("bytes", "%"PRIu64, rs->bytes);
2774 if (rs->child != NULL && rs->child->protocol != -1) {
2775 sharkd_json_array_open("protos");
2776 sharkd_session_process_tap_phs_cb_aux(rs->child);
2777 sharkd_json_array_close();
2779 sharkd_json_object_close();
2784 * sharkd_session_process_tap_phs_cb()
2786 * Output phs tap:
2787 * (m) tap - tap name
2788 * (m) type - tap output type
2789 * (m) filter - tap filter argument
2790 * (m) protos - array of proto objects
2792 * proto object:
2793 * (m) proto - protocol name
2794 * (m) frames - frame count
2795 * (m) bytes - bytes count
2796 * (o) protos - array of proto objects
2798 static void
2799 sharkd_session_process_tap_phs_cb(void *arg)
2801 phs_t *rs = (phs_t *)arg;
2802 sharkd_json_object_open(NULL);
2803 sharkd_json_value_string("tap", "phs");
2804 sharkd_json_value_string("type", "phs");
2805 sharkd_json_value_string("filter", rs->filter ? rs->filter : "");
2806 sharkd_json_array_open("protos");
2807 sharkd_session_process_tap_phs_cb_aux(rs);
2808 sharkd_json_array_close();
2809 sharkd_json_object_close();
2812 static void
2813 sharkd_session_free_tap_phs_cb(void *arg)
2815 phs_t *rs = (phs_t *)arg;
2816 free_phs(rs);
2819 struct sharkd_export_object_list
2821 struct sharkd_export_object_list *next;
2823 char *type;
2824 const char *proto;
2825 GSList *entries;
2828 static struct sharkd_export_object_list *sharkd_eo_list;
2831 * sharkd_session_process_tap_eo_cb()
2833 * Output eo tap:
2834 * (m) tap - tap name
2835 * (m) type - tap output type
2836 * (m) proto - protocol short name
2837 * (m) objects - array of object with attributes:
2838 * (m) pkt - packet number
2839 * (o) hostname - hostname
2840 * (o) type - content type
2841 * (o) filename - filename
2842 * (m) len - object length
2843 * (m) sha1 - object's sha1 sum
2845 static void
2846 sharkd_session_process_tap_eo_cb(void *tapdata)
2848 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
2849 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data;
2850 GSList *slist;
2851 int i = 0;
2852 char sha1sum_bytes[HASH_SHA1_LENGTH], *sha1sum_str;
2854 json_dumper_begin_object(&dumper);
2855 sharkd_json_value_string("tap", object_list->type);
2856 sharkd_json_value_string("type", "eo");
2858 sharkd_json_value_string("proto", object_list->proto);
2860 sharkd_json_array_open("objects");
2861 for (slist = object_list->entries; slist; slist = slist->next)
2863 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
2865 json_dumper_begin_object(&dumper);
2867 sharkd_json_value_anyf("pkt", "%u", eo_entry->pkt_num);
2869 if (eo_entry->hostname)
2870 sharkd_json_value_string("hostname", eo_entry->hostname);
2872 if (eo_entry->content_type)
2873 sharkd_json_value_string("type", eo_entry->content_type);
2875 if (eo_entry->filename)
2876 sharkd_json_value_string("filename", eo_entry->filename);
2878 sharkd_json_value_stringf("_download", "%s_%d", object_list->type, i);
2880 sharkd_json_value_anyf("len", "%zu", eo_entry->payload_len);
2882 gcry_md_hash_buffer(GCRY_MD_SHA1, sha1sum_bytes, eo_entry->payload_data, eo_entry->payload_len);
2883 sha1sum_str = bytes_to_str(NULL, sha1sum_bytes, HASH_SHA1_LENGTH);
2884 sharkd_json_value_string("sha1", sha1sum_str);
2885 g_free(sha1sum_str);
2887 json_dumper_end_object(&dumper);
2889 i++;
2891 sharkd_json_array_close();
2893 json_dumper_end_object(&dumper);
2896 static void
2897 sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
2899 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2901 object_list->entries = g_slist_append(object_list->entries, entry);
2904 static export_object_entry_t *
2905 sharkd_eo_object_list_get_entry(void *gui_data, int row)
2907 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2909 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
2912 static struct sharkd_export_object_list *
2913 sharkd_eo_object_list_get_entry_by_type(void *gui_data, const char *tap_type)
2915 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2916 for (; object_list; object_list = object_list->next)
2918 if (!strcmp(object_list->type, tap_type))
2919 return object_list;
2921 return NULL;
2926 * sharkd_session_process_tap_rtp_cb()
2928 * Output RTP streams tap:
2929 * (m) tap - tap name
2930 * (m) type - tap output type
2931 * (m) streams - array of object with attributes:
2932 * (m) ssrc - RTP synchronization source identifier
2933 * (m) payload - stream payload
2934 * (m) saddr - source address
2935 * (m) sport - source port
2936 * (m) daddr - destination address
2937 * (m) dport - destination port
2938 * (m) pkts - packets count
2939 * (m) max_delta - max delta (ms)
2940 * (m) max_jitter - max jitter (ms)
2941 * (m) mean_jitter - mean jitter (ms)
2942 * (m) expectednr -
2943 * (m) totalnr -
2944 * (m) problem - if analyser found the problem
2945 * (m) ipver - address IP version (4 or 6)
2947 static void
2948 sharkd_session_process_tap_rtp_cb(void *arg)
2950 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
2952 GList *listx;
2954 json_dumper_begin_object(&dumper);
2955 sharkd_json_value_string("tap", "rtp-streams");
2956 sharkd_json_value_string("type", "rtp-streams");
2958 sharkd_json_array_open("streams");
2959 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
2961 rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data;
2962 rtpstream_info_calc_t calc;
2964 rtpstream_info_calculate(streaminfo, &calc);
2966 json_dumper_begin_object(&dumper);
2968 sharkd_json_value_stringf("ssrc", "0x%x", calc.ssrc);
2969 sharkd_json_value_string("payload", calc.all_payload_type_names);
2971 sharkd_json_value_string("saddr", calc.src_addr_str);
2972 sharkd_json_value_anyf("sport", "%u", calc.src_port);
2973 sharkd_json_value_string("daddr", calc.dst_addr_str);
2974 sharkd_json_value_anyf("dport", "%u", calc.dst_port);
2976 sharkd_json_value_anyf("start_time", "%f", calc.start_time_ms);
2977 sharkd_json_value_anyf("duration", "%f", calc.duration_ms);
2979 sharkd_json_value_anyf("pkts", "%u", calc.packet_count);
2980 sharkd_json_value_anyf("lost", "%u", calc.lost_num);
2981 sharkd_json_value_anyf("lost_percent", "%f", calc.lost_perc);
2983 sharkd_json_value_anyf("max_delta", "%f",calc.max_delta);
2984 sharkd_json_value_anyf("min_delta", "%f",calc.min_delta);
2985 sharkd_json_value_anyf("mean_delta", "%f",calc.mean_delta);
2986 sharkd_json_value_anyf("min_jitter", "%f", calc.min_jitter);
2987 sharkd_json_value_anyf("max_jitter", "%f", calc.max_jitter);
2988 sharkd_json_value_anyf("mean_jitter", "%f", calc.mean_jitter);
2990 sharkd_json_value_anyf("expectednr", "%u", calc.packet_expected);
2991 sharkd_json_value_anyf("totalnr", "%u", calc.total_nr);
2993 sharkd_json_value_anyf("problem", calc.problem ? "true" : "false");
2995 /* for filter */
2996 sharkd_json_value_anyf("ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
2998 rtpstream_info_calc_free(&calc);
3000 json_dumper_end_object(&dumper);
3002 sharkd_json_array_close();
3004 json_dumper_end_object(&dumper);
3008 * sharkd_session_process_tap_multicast_cb()
3010 * Output UDP Multicast streams tap:
3011 * (m) tap - tap name
3012 * (m) type - tap output type
3013 * (m) bufferThresholdBytes - byte count for a stream where a buffer alarm should be reported
3014 * (m) burstIntervalMs - analysis interval in milliseconds
3015 * (m) burstThresholdPackets - count of packets in an interval that should trigger an alarm
3016 * (m) streams - array of streams with metrics:
3017 * (m) saddr - source address
3018 * (m) sport - source port
3019 * (m) daddr - destination address
3020 * (m) dport - destination port
3021 * (m) packets - object group for packet metrics with attributes:
3022 * (m) number - count of packets in the stream
3023 * (m) perSecond - average number of packets per seconds in the stream
3024 * (m) bandwidth - object group for bandwidth metrics with attributes:
3025 * (m) average - average measured bitrate in the stream
3026 * (m) max - max measured bitrate in the stream
3027 * (m) buffer - object group for buffer metrics with attributes:
3028 * (m) alarms - number of times the stream exceeded the buffer threshold
3029 * (m) max - highest stream buffer utilization
3030 * (m) burst - object group for burst metrics with attributes:
3031 * (m) alarms - number of times the stream exceeded the burst threshold
3032 * (m) max - most stream packets measured in a burst interval
3034 static void
3035 sharkd_session_process_tap_multicast_cb(void *arg)
3037 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)arg;
3038 GList *list_item;
3039 char *addr_str;
3041 json_dumper_begin_object(&dumper);
3043 sharkd_json_value_string("tap", "multicast");
3044 sharkd_json_value_string("type", "multicast");
3046 sharkd_json_value_anyf("bufferThresholdBytes", "%u", mcast_stream_bufferalarm);
3047 sharkd_json_value_anyf("burstIntervalMs", "%u", mcast_stream_burstint);
3048 sharkd_json_value_anyf("burstThresholdPackets", "%u", mcast_stream_trigger);
3050 sharkd_json_array_open("streams");
3051 for (list_item = g_list_first(tapinfo->strinfo_list); list_item; list_item = list_item->next) {
3052 mcast_stream_info_t *stream_info = (mcast_stream_info_t *) list_item->data;
3053 sharkd_json_object_open(NULL);
3055 addr_str = address_to_display(NULL, &stream_info->src_addr);
3056 sharkd_json_value_string("saddr", addr_str);
3057 wmem_free(NULL, addr_str);
3058 sharkd_json_value_anyf("sport", "%u", stream_info->src_port);
3059 addr_str = address_to_display(NULL, &stream_info->dest_addr);
3060 sharkd_json_value_string("daddr", addr_str);
3061 wmem_free(NULL, addr_str);
3062 sharkd_json_value_anyf("dport", "%u", stream_info->dest_port);
3063 sharkd_json_object_open("packets");
3065 sharkd_json_value_anyf("number", "%u", stream_info->npackets);
3066 sharkd_json_value_anyf("perSecond", "%f", stream_info->apackets);
3068 sharkd_json_object_close();
3069 sharkd_json_object_open("bandwidth");
3071 sharkd_json_value_anyf("average", "%f", stream_info->average_bw);
3072 sharkd_json_value_anyf("max", "%f", stream_info->element.maxbw);
3074 sharkd_json_object_close();
3075 sharkd_json_object_open("buffer");
3077 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbuffalarms);
3078 sharkd_json_value_anyf("max", "%u", stream_info->element.topbuffusage);
3080 sharkd_json_object_close();
3081 sharkd_json_object_open("burst");
3083 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbursts);
3084 sharkd_json_value_anyf("max", "%u", stream_info->element.topburstsize);
3086 sharkd_json_object_close();
3088 sharkd_json_object_close();
3090 sharkd_json_array_close();
3092 json_dumper_end_object(&dumper);
3095 static void
3096 sharkd_session_process_free_tap_multicast_cb(void *tapdata)
3098 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)tapdata;
3100 mcaststream_reset(tapinfo);
3102 g_free(tapinfo);
3106 * sharkd_session_process_tap_voip_calls_cb()
3108 * Output VoIP Calls tap:
3109 * (m) tap - tap name
3110 * (m) type - tap output type
3111 * (m) calls - array of objects with attributes:
3112 * (m) call - call number
3113 * (m) start_time - start timestamp
3114 * (m) stop_time - stop timestamp
3115 * (m) initial_speaker - address of initial speaker
3116 * (m) from - from address
3117 * (m) to - to address
3118 * (m) protocol - protocol name
3119 * (m) packets - packet count
3120 * (m) state - state string
3121 * (m) comment - comment string
3123 static void
3124 sharkd_session_process_tap_voip_calls_cb(void *arg)
3126 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)arg;
3127 char *addr_str;
3128 GList *cur_call = g_queue_peek_nth_link(tapinfo->callsinfos, 0);
3129 sharkd_json_object_open(NULL);
3130 sharkd_json_value_string("tap", "voip-calls");
3131 sharkd_json_value_string("type", "voip-calls");
3132 sharkd_json_array_open("calls");
3133 while (cur_call && cur_call->data) {
3134 voip_calls_info_t *call_info_ = (voip_calls_info_t*) cur_call->data;
3135 sharkd_json_object_open(NULL);
3136 sharkd_json_value_anyf("call", "%hu", call_info_->call_num);
3137 sharkd_json_value_anyf("start_time", "%.6f", nstime_to_sec(&(call_info_->start_rel_ts)));
3138 sharkd_json_value_anyf("stop_time", "%.6f", nstime_to_sec(&(call_info_->stop_rel_ts)));
3139 addr_str = address_to_display(NULL, &(call_info_->initial_speaker));
3140 sharkd_json_value_string("initial_speaker", addr_str);
3141 wmem_free(NULL, addr_str);
3142 sharkd_json_value_string("from", call_info_->from_identity);
3143 sharkd_json_value_string("to", call_info_->to_identity);
3144 sharkd_json_value_string("protocol", ((call_info_->protocol == VOIP_COMMON) && call_info_->protocol_name) ?
3145 call_info_->protocol_name : voip_protocol_name[call_info_->protocol]);
3146 sharkd_json_value_anyf("packets", "%u", call_info_->npackets);
3147 sharkd_json_value_string("state", voip_call_state_name[call_info_->call_state]);
3148 sharkd_json_value_string("comment", call_info_->call_comment);
3149 sharkd_json_object_close();
3150 cur_call = g_list_next(cur_call);
3152 sharkd_json_array_close();
3153 sharkd_json_object_close();
3156 static void
3157 sharkd_session_free_tap_voip_calls_cb(void *tapdata)
3159 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)tapdata;
3160 voip_calls_remove_all_tap_listeners(tapinfo);
3161 if (tapinfo->callsinfos != NULL) {
3162 g_queue_free(tapinfo->callsinfos);
3164 if (tapinfo->graph_analysis != NULL) {
3165 sequence_analysis_info_free(tapinfo->graph_analysis);
3167 memset(tapinfo, 0, sizeof(*tapinfo));
3171 struct sharkd_voip_convs_req {
3172 voip_calls_tapinfo_t *tapinfo;
3173 const char *tap_name;
3177 * sharkd_session_process_tap_voip_convs_cb()
3179 * Output VoIP Conversations tap:
3180 * (m) tap - tap name
3181 * (m) type - tap output type
3182 * (m) convs - array of objects with attributes:
3183 * (m) frame - frame number
3184 * (m) call - call number
3185 * (m) time - timestamp
3186 * (m) dst_addr - destination address
3187 * (m) dst_port - destination port
3188 * (m) src_addr - source address
3189 * (m) src_port - source port
3190 * (m) label - label string
3191 * (m) comment - comment string
3193 static void
3194 sharkd_session_process_tap_voip_convs_cb(void *arg)
3196 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)arg;
3197 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3198 seq_analysis_info_t *sainfo = tapinfo->graph_analysis;
3199 char *addr_str;
3200 sequence_analysis_list_sort(sainfo);
3201 sharkd_json_object_open(NULL);
3202 sharkd_json_value_string("tap", voip_convs_req->tap_name);
3203 sharkd_json_value_string("type", "voip-convs");
3204 sharkd_json_array_open("convs");
3205 for (GList *cur = g_queue_peek_nth_link(sainfo->items, 0); cur; cur = g_list_next(cur)) {
3206 seq_analysis_item_t *sai = (seq_analysis_item_t *) cur->data;
3207 if ((voip_conv_sel[sai->conv_num / VOIP_CONV_BITS] & (1 << (sai->conv_num % VOIP_CONV_BITS))) == 0)
3208 continue;
3209 sharkd_json_object_open(NULL);
3210 sharkd_json_value_anyf("frame", "%d", sai->frame_number);
3211 sharkd_json_value_anyf("call", "%d", sai->conv_num);
3212 sharkd_json_value_string("time", sai->time_str);
3213 addr_str = address_to_display(NULL, &(sai->dst_addr));
3214 sharkd_json_value_string("dst_addr", addr_str);
3215 wmem_free(NULL, addr_str);
3216 sharkd_json_value_anyf("dst_port", "%d", sai->port_dst);
3217 addr_str = address_to_display(NULL, &(sai->src_addr));
3218 sharkd_json_value_string("src_addr", addr_str);
3219 wmem_free(NULL, addr_str);
3220 sharkd_json_value_anyf("src_port", "%d", sai->port_src);
3221 sharkd_json_value_string("label", sai->frame_label);
3222 sharkd_json_value_string("comment", sai->comment);
3223 sharkd_json_object_close();
3225 sharkd_json_array_close();
3226 sharkd_json_object_close();
3229 static void
3230 sharkd_session_free_tap_voip_convs_cb(void *tapdata)
3232 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)tapdata;
3233 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3234 voip_calls_remove_all_tap_listeners(tapinfo);
3235 if (tapinfo->callsinfos != NULL) {
3236 g_queue_free(tapinfo->callsinfos);
3238 if (tapinfo->graph_analysis != NULL) {
3239 sequence_analysis_info_free(tapinfo->graph_analysis);
3241 memset(tapinfo, 0, sizeof(*tapinfo));
3242 g_free(voip_convs_req);
3245 struct sharkd_hosts_req {
3246 const char *tap_name;
3247 gboolean dump_v4;
3248 gboolean dump_v6;
3251 static gint
3252 sharkd_session_tap_ipv4_host_compare(gconstpointer a, gconstpointer b)
3254 return ws_ascii_strnatcmp(((const hashipv4_t *)a)->name,
3255 ((const hashipv4_t *)b)->name);
3258 static gint
3259 sharkd_session_tap_ipv6_host_compare(gconstpointer a, gconstpointer b)
3261 return ws_ascii_strnatcmp(((const hashipv6_t *)a)->name,
3262 ((const hashipv6_t *)b)->name);
3265 static void
3266 sharkd_session_tap_ipv4_host_print(gpointer data, gpointer user_data _U_)
3268 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)data;
3269 sharkd_json_object_open(NULL);
3270 sharkd_json_value_string("name", ipv4_hash_table_entry->name);
3271 sharkd_json_value_string("addr", ipv4_hash_table_entry->ip);
3272 sharkd_json_object_close();
3275 static void
3276 sharkd_session_tap_ipv6_host_print(gpointer data, gpointer user_data _U_)
3278 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)data;
3279 sharkd_json_object_open(NULL);
3280 sharkd_json_value_string("name", ipv6_hash_table_entry->name);
3281 sharkd_json_value_string("addr", ipv6_hash_table_entry->ip6);
3282 sharkd_json_object_close();
3285 static void
3286 sharkd_session_tap_ipv4_host_insert_sorted(gpointer key _U_, gpointer value, gpointer user_data)
3288 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
3289 GSList **list = (GSList **)user_data;
3290 if ((ipv4_hash_table_entry->flags & NAME_RESOLVED)) {
3291 *list = g_slist_insert_sorted(*list, ipv4_hash_table_entry, sharkd_session_tap_ipv4_host_compare);
3295 static void
3296 sharkd_session_tap_ipv6_host_insert_sorted(gpointer key _U_, gpointer value, gpointer user_data)
3298 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
3299 GSList **list = (GSList **) user_data;
3300 if ((ipv6_hash_table_entry->flags & NAME_RESOLVED)) {
3301 *list = g_slist_insert_sorted(*list, ipv6_hash_table_entry, sharkd_session_tap_ipv6_host_compare);
3305 static void
3306 sharkd_session_tap_ipv4_hosts_print(void)
3308 wmem_map_t *ipv4_hash_table = get_ipv4_hash_table();
3309 if (!ipv4_hash_table)
3310 return;
3311 GSList *list = NULL;
3312 wmem_map_foreach(ipv4_hash_table, sharkd_session_tap_ipv4_host_insert_sorted, &list);
3313 g_slist_foreach(list, sharkd_session_tap_ipv4_host_print, NULL);
3314 g_slist_free(list);
3317 static void
3318 sharkd_session_tap_ipv6_hosts_print(void)
3320 wmem_map_t *ipv6_hash_table = get_ipv6_hash_table();
3321 if (!ipv6_hash_table)
3322 return;
3323 GSList *list = NULL;
3324 wmem_map_foreach(ipv6_hash_table, sharkd_session_tap_ipv6_host_insert_sorted, &list);
3325 g_slist_foreach(list, sharkd_session_tap_ipv6_host_print, NULL);
3326 g_slist_free(list);
3330 * sharkd_session_process_tap_hosts_cb()
3332 * Output Hosts tap:
3333 * (m) tap - tap name
3334 * (m) type - tap output type
3335 * (o) ipv4_hosts - array of objects with attributes:
3336 * (m) addr - ipv4 address
3337 * (m) name - resolved name of address
3338 * (o) ipv6_hosts - array of objects with attributes:
3339 * (m) addr - ipv6 address
3340 * (m) name - resolved name of address
3342 static void
3343 sharkd_session_process_tap_hosts_cb(void *arg)
3345 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)arg;
3346 sharkd_json_object_open(NULL);
3347 sharkd_json_value_string("tap", hosts_req->tap_name);
3348 sharkd_json_value_string("type", "hosts");
3349 if (hosts_req->dump_v4) {
3350 sharkd_json_array_open("ipv4_hosts");
3351 sharkd_session_tap_ipv4_hosts_print();
3352 sharkd_json_array_close();
3354 if (hosts_req->dump_v6) {
3355 sharkd_json_array_open("ipv6_hosts");
3356 sharkd_session_tap_ipv6_hosts_print();
3357 sharkd_json_array_close();
3359 sharkd_json_object_close();
3362 static void
3363 sharkd_session_free_tap_hosts_cb(void *tapdata)
3365 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)tapdata;
3366 g_free(hosts_req);
3369 static GString*
3370 sharkd_session_eo_register_tap_listener(register_eo_t *eo, const char *tap_type, const char *tap_filter, tap_draw_cb tap_draw, void **ptap_data, GFreeFunc* ptap_free)
3372 export_object_list_t *eo_object;
3373 struct sharkd_export_object_list *object_list;
3375 object_list = sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type);
3376 if (object_list)
3378 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
3379 object_list->entries = NULL;
3381 else
3383 object_list = g_new(struct sharkd_export_object_list, 1);
3384 object_list->type = g_strdup(tap_type);
3385 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
3386 object_list->entries = NULL;
3387 object_list->next = sharkd_eo_list;
3388 sharkd_eo_list = object_list;
3391 eo_object = g_new0(export_object_list_t, 1);
3392 eo_object->add_entry = sharkd_eo_object_list_add_entry;
3393 eo_object->get_entry = sharkd_eo_object_list_get_entry;
3394 eo_object->gui_data = (void *) object_list;
3396 *ptap_data = eo_object;
3397 *ptap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
3399 return register_tap_listener(get_eo_tap_listener_name(eo), eo_object, tap_filter, 0, NULL, get_eo_packet_func(eo), tap_draw, NULL);
3403 * sharkd_session_process_tap()
3405 * Process tap request
3407 * Input:
3408 * (m) tap0 - First tap request
3409 * (o) tap1...tap15 - Other tap requests
3411 * Output object with attributes:
3412 * (m) taps - array of object with attributes:
3413 * (m) tap - tap name
3414 * (m) type - tap output type
3415 * ...
3416 * for type:stats see sharkd_session_process_tap_stats_cb()
3417 * for type:nstat see sharkd_session_process_tap_nstat_cb()
3418 * for type:conv see sharkd_session_process_tap_conv_cb()
3419 * for type:host see sharkd_session_process_tap_conv_cb()
3420 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
3421 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
3422 * for type:eo see sharkd_session_process_tap_eo_cb()
3423 * for type:expert see sharkd_session_process_tap_expert_cb()
3424 * for type:rtd see sharkd_session_process_tap_rtd_cb()
3425 * for type:srt see sharkd_session_process_tap_srt_cb()
3426 * for type:flow see sharkd_session_process_tap_flow_cb()
3428 * (m) err - error code
3430 static void
3431 sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
3433 void *taps_data[16];
3434 GFreeFunc taps_free[16];
3435 int taps_count = 0;
3436 int i;
3437 const char *tap_filter = json_find_attr(buf, tokens, count, "filter");
3439 rtpstream_tapinfo_t rtp_tapinfo =
3440 { NULL, NULL, NULL, NULL, 0, NULL, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE, FALSE};
3442 for (i = 0; i < 16; i++)
3444 char tapbuf[32];
3445 const char *tok_tap;
3447 void *tap_data = NULL;
3448 GFreeFunc tap_free = NULL;
3449 GString *tap_error = NULL;
3451 snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
3452 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
3453 if (!tok_tap)
3454 break;
3456 if (!strncmp(tok_tap, "stat:", 5))
3458 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
3459 stats_tree *st;
3461 if (!cfg)
3463 sharkd_json_error(
3464 rpcid, -11001, NULL,
3465 "sharkd_session_process_tap() stat %s not found", tok_tap + 5
3467 return;
3470 st = stats_tree_new(cfg, NULL, tap_filter);
3472 tap_error = register_tap_listener(st->cfg->tapname, st, st->filter, st->cfg->flags, stats_tree_reset, stats_tree_packet, sharkd_session_process_tap_stats_cb, NULL);
3474 if (!tap_error && cfg->init)
3475 cfg->init(st);
3477 tap_data = st;
3478 tap_free = sharkd_session_free_tap_stats_cb;
3480 else if (!strcmp(tok_tap, "expert"))
3482 struct sharkd_expert_tap *expert_tap;
3484 expert_tap = g_new0(struct sharkd_expert_tap, 1);
3485 expert_tap->text = g_string_chunk_new(100);
3487 tap_error = register_tap_listener("expert", expert_tap, tap_filter, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb, NULL);
3489 tap_data = expert_tap;
3490 tap_free = sharkd_session_free_tap_expert_cb;
3492 else if (!strncmp(tok_tap, "seqa:", 5))
3494 seq_analysis_info_t *graph_analysis;
3495 register_analysis_t *analysis;
3496 const char *tap_name;
3497 tap_packet_cb tap_func;
3498 guint tap_flags;
3500 analysis = sequence_analysis_find_by_name(tok_tap + 5);
3501 if (!analysis)
3503 sharkd_json_error(
3504 rpcid, -11002, NULL,
3505 "sharkd_session_process_tap() seq analysis %s not found", tok_tap + 5
3507 return;
3510 graph_analysis = sequence_analysis_info_new();
3511 graph_analysis->name = tok_tap + 5;
3512 /* TODO, make configurable */
3513 graph_analysis->any_addr = FALSE;
3515 tap_name = sequence_analysis_get_tap_listener_name(analysis);
3516 tap_flags = sequence_analysis_get_tap_flags(analysis);
3517 tap_func = sequence_analysis_get_packet_func(analysis);
3519 tap_error = register_tap_listener(tap_name, graph_analysis, tap_filter, tap_flags, NULL, tap_func, sharkd_session_process_tap_flow_cb, NULL);
3521 tap_data = graph_analysis;
3522 tap_free = sharkd_session_free_tap_flow_cb;
3524 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
3526 struct register_ct *ct = NULL;
3527 const char *ct_tapname;
3528 struct sharkd_conv_tap_data *ct_data;
3529 tap_packet_cb tap_func = NULL;
3531 if (!strncmp(tok_tap, "conv:", 5))
3533 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
3535 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
3537 sharkd_json_error(
3538 rpcid, -11003, NULL,
3539 "sharkd_session_process_tap() conv %s not found", tok_tap + 5
3541 return;
3544 else if (!strncmp(tok_tap, "endpt:", 6))
3546 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
3548 if (!ct || !(tap_func = get_endpoint_packet_func(ct)))
3550 sharkd_json_error(
3551 rpcid, -11004, NULL,
3552 "sharkd_session_process_tap() endpt %s not found", tok_tap + 6
3554 return;
3557 else
3559 sharkd_json_error(
3560 rpcid, -11005, NULL,
3561 "sharkd_session_process_tap() conv/endpt(?): %s not found", tok_tap
3563 return;
3566 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
3568 ct_data = g_new0(struct sharkd_conv_tap_data, 1);
3569 ct_data->type = tok_tap;
3570 ct_data->hash.user_data = ct_data;
3572 /* XXX: make configurable */
3573 ct_data->resolve_name = TRUE;
3574 ct_data->resolve_port = TRUE;
3576 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb, NULL);
3578 tap_data = &ct_data->hash;
3579 tap_free = sharkd_session_free_tap_conv_cb;
3581 else if (!strncmp(tok_tap, "nstat:", 6))
3583 stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6);
3584 stat_data_t *stat_data;
3586 if (!stat_tap)
3588 sharkd_json_error(
3589 rpcid, -11006, NULL,
3590 "sharkd_session_process_tap() nstat=%s not found", tok_tap + 6
3592 return;
3595 stat_tap->stat_tap_init_cb(stat_tap);
3597 stat_data = g_new0(stat_data_t, 1);
3598 stat_data->stat_tap_data = stat_tap;
3599 stat_data->user_data = NULL;
3601 tap_error = register_tap_listener(stat_tap->tap_name, stat_data, tap_filter, 0, NULL, stat_tap->packet_func, sharkd_session_process_tap_nstat_cb, NULL);
3603 tap_data = stat_data;
3604 tap_free = sharkd_session_free_tap_nstat_cb;
3606 else if (!strncmp(tok_tap, "rtd:", 4))
3608 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
3609 rtd_data_t *rtd_data;
3610 char *err;
3612 if (!rtd)
3614 sharkd_json_error(
3615 rpcid, -11007, NULL,
3616 "sharkd_session_process_tap() rtd=%s not found", tok_tap + 4
3618 return;
3621 rtd_table_get_filter(rtd, "", &tap_filter, &err);
3622 if (err != NULL)
3624 sharkd_json_error(
3625 rpcid, -11008, NULL,
3626 "sharkd_session_process_tap() rtd=%s err=%s", tok_tap + 4, err
3628 g_free(err);
3629 return;
3632 rtd_data = g_new0(rtd_data_t, 1);
3633 rtd_data->user_data = rtd;
3634 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL, NULL);
3636 tap_error = register_tap_listener(get_rtd_tap_listener_name(rtd), rtd_data, tap_filter, 0, NULL, get_rtd_packet_func(rtd), sharkd_session_process_tap_rtd_cb, NULL);
3638 tap_data = rtd_data;
3639 tap_free = sharkd_session_free_tap_rtd_cb;
3641 else if (!strncmp(tok_tap, "srt:", 4))
3643 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
3644 srt_data_t *srt_data;
3645 char *err;
3647 if (!srt)
3649 sharkd_json_error(
3650 rpcid, -11009, NULL,
3651 "sharkd_session_process_tap() srt=%s not found", tok_tap + 4
3653 return;
3656 srt_table_get_filter(srt, "", &tap_filter, &err);
3657 if (err != NULL)
3659 sharkd_json_error(
3660 rpcid, -11010, NULL,
3661 "sharkd_session_process_tap() srt=%s err=%s", tok_tap + 4, err
3663 g_free(err);
3664 return;
3667 srt_data = g_new0(srt_data_t, 1);
3668 srt_data->srt_array = g_array_new(FALSE, TRUE, sizeof(srt_stat_table *));
3669 srt_data->user_data = srt;
3670 srt_table_dissector_init(srt, srt_data->srt_array);
3672 tap_error = register_tap_listener(get_srt_tap_listener_name(srt), srt_data, tap_filter, 0, NULL, get_srt_packet_func(srt), sharkd_session_process_tap_srt_cb, NULL);
3674 tap_data = srt_data;
3675 tap_free = sharkd_session_free_tap_srt_cb;
3677 else if (!strncmp(tok_tap, "eo:", 3))
3679 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
3681 if (!eo)
3683 sharkd_json_error(
3684 rpcid, -11011, NULL,
3685 "sharkd_session_process_tap() eo=%s not found", tok_tap + 3
3687 return;
3690 tap_error = sharkd_session_eo_register_tap_listener(eo, tok_tap, tap_filter, sharkd_session_process_tap_eo_cb, &tap_data, &tap_free);
3692 /* tap_data & tap_free assigned by sharkd_session_eo_register_tap_listener */
3694 else if (!strcmp(tok_tap, "rtp-streams"))
3696 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet_cb, sharkd_session_process_tap_rtp_cb, NULL);
3698 tap_data = &rtp_tapinfo;
3699 tap_free = rtpstream_reset_cb;
3701 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
3703 struct sharkd_analyse_rtp *rtp_req;
3705 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
3706 if (!sharkd_rtp_match_init(&rtp_req->id, tok_tap + 12))
3708 rtpstream_id_free(&rtp_req->id);
3709 g_free(rtp_req);
3710 continue;
3713 rtp_req->tap_name = tok_tap;
3714 rtp_req->statinfo.first_packet = TRUE;
3715 rtp_req->statinfo.reg_pt = PT_UNDEFINED;
3717 tap_error = register_tap_listener("rtp", rtp_req, tap_filter, 0, NULL, sharkd_session_packet_tap_rtp_analyse_cb, sharkd_session_process_tap_rtp_analyse_cb, NULL);
3719 tap_data = rtp_req;
3720 tap_free = sharkd_session_process_tap_rtp_free_cb;
3722 else if (!strcmp(tok_tap, "multicast"))
3724 mcaststream_tapinfo_t *mcaststream_tapinfo;
3725 mcaststream_tapinfo = (mcaststream_tapinfo_t *) g_malloc0(sizeof(*mcaststream_tapinfo));
3727 tap_error = register_tap_listener("udp", mcaststream_tapinfo, tap_filter, 0, NULL, mcaststream_packet, sharkd_session_process_tap_multicast_cb, NULL);
3728 tap_data = mcaststream_tapinfo;
3729 tap_free = sharkd_session_process_free_tap_multicast_cb;
3731 else if (!strcmp(tok_tap, "phs"))
3733 phs_t *rs;
3735 pc_proto_id = proto_registrar_get_id_byname("pkt_comment");
3737 rs = new_phs_t(NULL, tap_filter);
3739 tap_error = register_tap_listener("frame", rs, tap_filter, TL_REQUIRES_PROTO_TREE, NULL,
3740 protohierstat_packet,
3741 sharkd_session_process_tap_phs_cb, NULL);
3743 tap_data = rs;
3744 tap_free = sharkd_session_free_tap_phs_cb;
3746 else if (!strcmp(tok_tap, "voip-calls"))
3748 voip_stat_init_tapinfo();
3750 tap_error = register_tap_listener("frame", &tapinfo_, tap_filter, 0, NULL, NULL, sharkd_session_process_tap_voip_calls_cb, NULL);
3752 tapinfo_.session = cfile.epan;
3753 voip_calls_init_all_taps(&tapinfo_);
3755 tap_data = &tapinfo_;
3756 tap_free = sharkd_session_free_tap_voip_calls_cb;
3758 else if (!strncmp(tok_tap, "voip-convs:", 11))
3760 int len;
3761 unsigned int min, max;
3762 struct sharkd_voip_convs_req *voip_convs_req;
3763 const char *conv_arg = tok_tap + 11;
3765 // parse tok_tap to get which call we are asking for
3766 if (*conv_arg == 0) {
3767 // set all bits of voip_conv_sel (-1 in binary is all 1's)
3768 memset(voip_conv_sel, -1, sizeof(voip_conv_sel));
3769 } else {
3770 memset(voip_conv_sel, 0, sizeof(voip_conv_sel));
3772 while (*conv_arg != 0) {
3773 if (*conv_arg == ',') {
3774 conv_arg++;
3776 if (sscanf(conv_arg, "%u-%u%n", &min, &max, &len) == 2) {
3777 conv_arg += len;
3778 } else if (sscanf(conv_arg, "%u%n", &min, &len) == 1) {
3779 max = min;
3780 conv_arg += len;
3781 } else {
3782 sharkd_json_error(
3783 rpcid, -11014, NULL,
3784 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' parameter", tok_tap
3786 return;
3788 if (min > max || min >= VOIP_CONV_MAX || max >= VOIP_CONV_MAX) {
3789 sharkd_json_error(
3790 rpcid, -11012, NULL,
3791 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' number range", tok_tap
3793 return;
3795 for(; min <= max; min++) {
3796 voip_conv_sel[min / VOIP_CONV_BITS] |= 1 << (min % VOIP_CONV_BITS);
3801 voip_stat_init_tapinfo();
3803 voip_convs_req = (struct sharkd_voip_convs_req *) g_malloc0(sizeof(*voip_convs_req));
3804 voip_convs_req->tapinfo = &tapinfo_;
3805 voip_convs_req->tap_name = tok_tap;
3807 tap_error = register_tap_listener("frame", voip_convs_req, tap_filter, 0, NULL, NULL, sharkd_session_process_tap_voip_convs_cb, NULL);
3809 tapinfo_.session = cfile.epan;
3810 voip_calls_init_all_taps(&tapinfo_);
3812 tap_data = voip_convs_req;
3813 tap_free = sharkd_session_free_tap_voip_convs_cb;
3815 else if (!strncmp(tok_tap, "hosts:", 6))
3817 gboolean dump_v4;
3818 gboolean dump_v6;
3819 struct sharkd_hosts_req *hosts_req;
3820 const char *proto_arg;
3821 gchar **proto_tokens;
3822 gint proto_count;
3824 proto_arg = tok_tap + 6;
3826 if (strlen(proto_arg) == 0) {
3827 dump_v4 = TRUE;
3828 dump_v6 = TRUE;
3829 } else {
3830 dump_v4 = FALSE;
3831 dump_v6 = FALSE;
3833 proto_tokens = g_strsplit(proto_arg, ",", 0);
3834 proto_count = 0;
3835 while (proto_tokens[proto_count]) {
3836 if (!strcmp("ip", proto_tokens[proto_count]) ||
3837 !strcmp("ipv4", proto_tokens[proto_count])) {
3838 dump_v4 = TRUE;
3839 } else if (!strcmp("ipv6", proto_tokens[proto_count])) {
3840 dump_v6 = TRUE;
3841 } else {
3842 g_strfreev(proto_tokens);
3843 sharkd_json_error(
3844 rpcid, -11015, NULL,
3845 "sharkd_session_process_tap() hosts=%s invalid 'protos' parameter", tok_tap
3847 return;
3849 proto_count++;
3851 g_strfreev(proto_tokens);
3854 hosts_req = (struct sharkd_hosts_req *)g_malloc0(sizeof(*hosts_req));
3855 hosts_req->dump_v4 = dump_v4;
3856 hosts_req->dump_v6 = dump_v6;
3857 hosts_req->tap_name = tok_tap;
3859 tap_error = register_tap_listener("frame", hosts_req, tap_filter, TL_REQUIRES_PROTO_TREE, NULL, NULL, sharkd_session_process_tap_hosts_cb, NULL);
3861 tap_data = hosts_req;
3862 tap_free = sharkd_session_free_tap_hosts_cb;
3864 else
3866 sharkd_json_error(
3867 rpcid, -11012, NULL,
3868 "sharkd_session_process_tap() %s not recognized", tok_tap
3870 return;
3873 if (tap_error)
3875 sharkd_json_error(
3876 rpcid, -11013, NULL,
3877 "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str
3879 g_string_free(tap_error, TRUE);
3880 if (tap_free)
3881 tap_free(tap_data);
3882 return;
3885 taps_data[taps_count] = tap_data;
3886 taps_free[taps_count] = tap_free;
3887 taps_count++;
3890 fprintf(stderr, "sharkd_session_process_tap() count=%d\n", taps_count);
3891 if (taps_count == 0)
3893 sharkd_json_result_prologue(rpcid);
3894 sharkd_json_array_open("taps");
3895 sharkd_json_array_close();
3896 sharkd_json_result_epilogue();
3897 return;
3900 sharkd_json_result_prologue(rpcid);
3901 sharkd_json_array_open("taps");
3902 sharkd_retap();
3903 sharkd_json_array_close();
3904 sharkd_json_result_epilogue();
3906 for (i = 0; i < taps_count; i++)
3908 if (taps_data[i])
3909 remove_tap_listener(taps_data[i]);
3911 if (taps_free[i])
3912 taps_free[i](taps_data[i]);
3917 * sharkd_session_process_follow()
3919 * Process follow request
3921 * Input:
3922 * (m) follow - follow protocol request (e.g. HTTP)
3923 * (m) filter - filter request (e.g. tcp.stream == 1)
3924 * (m) stream - stream index number
3925 * (o) sub_stream - follow sub-stream index number (e.g. for HTTP/2 and QUIC streams)
3927 * Output object with attributes:
3929 * (m) err - error code
3930 * (m) shost - server host
3931 * (m) sport - server port
3932 * (m) sbytes - server send bytes count
3933 * (m) chost - client host
3934 * (m) cport - client port
3935 * (m) cbytes - client send bytes count
3936 * (o) payloads - array of object with attributes:
3937 * (o) s - set if server sent, else client
3938 * (m) n - packet number
3939 * (m) d - data base64 encoded
3941 static void
3942 sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
3944 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
3945 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
3946 const char *tok_sub_stream = json_find_attr(buf, tokens, count, "sub_stream");
3948 register_follow_t *follower;
3949 GString *tap_error;
3951 follow_info_t *follow_info;
3952 const char *host;
3953 char *port;
3955 follower = get_follow_by_name(tok_follow);
3956 if (!follower)
3958 sharkd_json_error(
3959 rpcid, -12001, NULL,
3960 "sharkd_session_process_follow() follower=%s not found", tok_follow
3962 return;
3965 guint64 substream_id = SUBSTREAM_UNUSED;
3966 if (tok_sub_stream)
3968 ws_strtou64(tok_sub_stream, NULL, &substream_id);
3971 /* follow_reset_stream ? */
3972 follow_info = g_new0(follow_info_t, 1);
3973 follow_info->substream_id = substream_id;
3974 /* gui_data, filter_out_filter not set, but not used by dissector */
3976 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL, NULL);
3977 if (tap_error)
3979 sharkd_json_error(
3980 rpcid, -12002, NULL,
3981 "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str
3983 g_string_free(tap_error, TRUE);
3984 g_free(follow_info);
3985 return;
3988 sharkd_retap();
3990 sharkd_json_result_prologue(rpcid);
3992 /* Server information: hostname, port, bytes sent */
3993 host = address_to_name(&follow_info->server_ip);
3994 sharkd_json_value_string("shost", host);
3996 port = get_follow_port_to_display(follower)(NULL, follow_info->server_port);
3997 sharkd_json_value_string("sport", port);
3998 wmem_free(NULL, port);
4000 sharkd_json_value_anyf("sbytes", "%u", follow_info->bytes_written[0]);
4002 /* Client information: hostname, port, bytes sent */
4003 host = address_to_name(&follow_info->client_ip);
4004 sharkd_json_value_string("chost", host);
4006 port = get_follow_port_to_display(follower)(NULL, follow_info->client_port);
4007 sharkd_json_value_string("cport", port);
4008 wmem_free(NULL, port);
4010 sharkd_json_value_anyf("cbytes", "%u", follow_info->bytes_written[1]);
4012 if (follow_info->payload)
4014 follow_record_t *follow_record;
4015 GList *cur;
4017 sharkd_json_array_open("payloads");
4018 for (cur = g_list_last(follow_info->payload); cur; cur = g_list_previous(cur))
4020 follow_record = (follow_record_t *) cur->data;
4022 json_dumper_begin_object(&dumper);
4024 sharkd_json_value_anyf("n", "%u", follow_record->packet_num);
4025 sharkd_json_value_base64("d", follow_record->data->data, follow_record->data->len);
4027 if (follow_record->is_server)
4028 sharkd_json_value_anyf("s", "%d", 1);
4030 json_dumper_end_object(&dumper);
4032 sharkd_json_array_close();
4035 sharkd_json_result_epilogue();
4037 remove_tap_listener(follow_info);
4038 follow_info_free(follow_info);
4041 static void
4042 sharkd_session_process_frame_cb_tree(const char *key, epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs, gboolean display_hidden)
4044 proto_node *node;
4046 sharkd_json_array_open(key);
4047 for (node = tree->first_child; node; node = node->next)
4049 field_info *finfo = PNODE_FINFO(node);
4051 if (!finfo)
4052 continue;
4054 if (!display_hidden && FI_GET_FLAG(finfo, FI_HIDDEN))
4055 continue;
4057 json_dumper_begin_object(&dumper);
4059 if (!finfo->rep)
4061 char label_str[ITEM_LABEL_LENGTH];
4063 label_str[0] = '\0';
4064 proto_item_fill_label(finfo, label_str);
4065 sharkd_json_value_string("l", label_str);
4067 else
4069 sharkd_json_value_string("l", finfo->rep->representation);
4072 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
4074 int idx;
4076 for (idx = 1; tvbs[idx]; idx++)
4078 if (tvbs[idx] == finfo->ds_tvb)
4080 sharkd_json_value_anyf("ds", "%d", idx);
4081 break;
4086 if (finfo->start >= 0 && finfo->length > 0)
4087 sharkd_json_value_anyf("h", "[%d,%d]", finfo->start, finfo->length);
4089 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
4090 sharkd_json_value_anyf("i", "[%d,%d]", finfo->appendix_start, finfo->appendix_length);
4093 if (finfo->hfinfo)
4095 char *filter;
4097 if (finfo->hfinfo->type == FT_PROTOCOL)
4099 sharkd_json_value_string("t", "proto");
4101 else if (finfo->hfinfo->type == FT_FRAMENUM)
4103 sharkd_json_value_string("t", "framenum");
4104 sharkd_json_value_anyf("fnum", "%u", fvalue_get_uinteger(finfo->value));
4106 else if (FI_GET_FLAG(finfo, FI_URL) && FT_IS_STRING(finfo->hfinfo->type))
4108 char *url = fvalue_to_string_repr(NULL, finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
4110 sharkd_json_value_string("t", "url");
4111 sharkd_json_value_string("url", url);
4112 wmem_free(NULL, url);
4115 filter = proto_construct_match_selected_string(finfo, edt);
4116 if (filter)
4118 sharkd_json_value_string("f", filter);
4119 wmem_free(NULL, filter);
4122 if (finfo->hfinfo->abbrev)
4123 sharkd_json_value_string("fn", finfo->hfinfo->abbrev);
4126 if (FI_GET_FLAG(finfo, FI_GENERATED))
4127 sharkd_json_value_anyf("g", "true");
4129 if (FI_GET_FLAG(finfo, FI_HIDDEN))
4130 sharkd_json_value_anyf("v", "true");
4132 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
4134 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK), expert_severity_vals);
4136 ws_assert(severity != NULL);
4138 sharkd_json_value_string("s", severity);
4141 if (((proto_tree *) node)->first_child)
4143 if (finfo->tree_type != -1)
4144 sharkd_json_value_anyf("e", "%d", finfo->tree_type);
4146 sharkd_session_process_frame_cb_tree("n", edt, (proto_tree *) node, tvbs, display_hidden);
4149 json_dumper_end_object(&dumper);
4151 sharkd_json_array_close();
4154 static bool
4155 sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_data)
4157 register_follow_t *follower = (register_follow_t *) value;
4158 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4159 packet_info *pi = &edt->pi;
4161 const int proto_id = get_follow_proto_id(follower);
4163 guint32 ignore_stream;
4164 guint32 ignore_sub_stream;
4166 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4168 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4169 char *follow_filter;
4171 follow_filter = get_follow_conv_func(follower)(edt, pi, &ignore_stream, &ignore_sub_stream);
4173 json_dumper_begin_array(&dumper);
4174 json_dumper_value_string(&dumper, layer_proto);
4175 json_dumper_value_string(&dumper, follow_filter);
4176 json_dumper_end_array(&dumper);
4178 g_free(follow_filter);
4181 return FALSE;
4184 static bool
4185 sharkd_followers_visit_layers_cb(const void *key _U_, void *value, void *user_data)
4187 register_follow_t *follower = (register_follow_t *) value;
4188 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4189 packet_info *pi = &edt->pi;
4191 const int proto_id = get_follow_proto_id(follower);
4193 guint stream;
4194 guint sub_stream = (guint) SUBSTREAM_UNUSED;
4196 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4198 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4199 char *follow_filter;
4201 follow_filter = get_follow_conv_func(follower)(edt, pi, (guint *) &stream, (guint *) &sub_stream);
4203 sharkd_json_object_open(NULL);
4204 sharkd_json_value_string("protocol", layer_proto);
4205 sharkd_json_value_string("filter", follow_filter);
4206 sharkd_json_value_anyf("stream", "%d", stream);
4207 if (sub_stream != (guint) SUBSTREAM_UNUSED)
4209 sharkd_json_value_anyf("sub_stream", "%d", sub_stream);
4211 sharkd_json_object_close();
4213 g_free(follow_filter);
4216 return FALSE;
4219 struct sharkd_frame_request_data
4221 gboolean display_hidden;
4224 static void
4225 sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
4227 packet_info *pi = &edt->pi;
4228 frame_data *fdata = pi->fd;
4229 wtap_block_t pkt_block = NULL;
4231 const struct sharkd_frame_request_data * const req_data = (const struct sharkd_frame_request_data * const) data;
4232 const gboolean display_hidden = (req_data) ? req_data->display_hidden : FALSE;
4234 sharkd_json_result_prologue(rpcid);
4236 if (fdata->has_modified_block)
4237 pkt_block = sharkd_get_modified_block(fdata);
4238 else
4239 pkt_block = pi->rec->block;
4241 if (pkt_block)
4243 guint i;
4244 guint n;
4245 gchar *comment;
4247 n = wtap_block_count_option(pkt_block, OPT_COMMENT);
4249 sharkd_json_array_open("comment");
4250 for (i = 0; i < n; i++) {
4251 if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT, i, &comment)) {
4252 sharkd_json_value_string(NULL, comment);
4255 sharkd_json_array_close();
4258 if (tree)
4260 tvbuff_t **tvbs = NULL;
4262 /* arrayize data src, to speedup searching for ds_tvb index */
4263 if (data_src && data_src->next /* only needed if there are more than one data source */)
4265 guint count = g_slist_length((GSList *) data_src);
4266 guint i;
4268 tvbs = (tvbuff_t **) g_malloc0((count + 1) * sizeof(*tvbs));
4270 for (i = 0; i < count; i++)
4272 const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i);
4274 tvbs[i] = get_data_source_tvb(src);
4277 tvbs[count] = NULL;
4280 sharkd_session_process_frame_cb_tree("tree", edt, tree, tvbs, display_hidden);
4282 g_free(tvbs);
4285 if (cinfo)
4287 int col;
4289 sharkd_json_array_open("col");
4290 for (col = 0; col < cinfo->num_cols; ++col)
4292 sharkd_json_value_string(NULL, get_column_text(cinfo, col));
4294 sharkd_json_array_close();
4297 if (fdata->ignored)
4298 sharkd_json_value_anyf("i", "true");
4300 if (fdata->marked)
4301 sharkd_json_value_anyf("m", "true");
4303 if (fdata->color_filter)
4305 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
4306 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
4309 if (data_src)
4311 struct data_source *src = (struct data_source *) data_src->data;
4312 gboolean ds_open = FALSE;
4314 tvbuff_t *tvb;
4315 guint length;
4317 tvb = get_data_source_tvb(src);
4318 length = tvb_captured_length(tvb);
4320 if (length != 0)
4322 const guchar *cp = tvb_get_ptr(tvb, 0, length);
4324 /* XXX pi.fd->encoding */
4325 sharkd_json_value_base64("bytes", cp, length);
4327 else
4329 sharkd_json_value_base64("bytes", "", 0);
4332 data_src = data_src->next;
4333 if (data_src)
4335 sharkd_json_array_open("ds");
4336 ds_open = TRUE;
4339 while (data_src)
4341 src = (struct data_source *) data_src->data;
4343 json_dumper_begin_object(&dumper);
4346 char *src_name = get_data_source_name(src);
4348 sharkd_json_value_string("name", src_name);
4349 wmem_free(NULL, src_name);
4352 tvb = get_data_source_tvb(src);
4353 length = tvb_captured_length(tvb);
4355 if (length != 0)
4357 const guchar *cp = tvb_get_ptr(tvb, 0, length);
4359 /* XXX pi.fd->encoding */
4360 sharkd_json_value_base64("bytes", cp, length);
4362 else
4364 sharkd_json_value_base64("bytes", "", 0);
4367 json_dumper_end_object(&dumper);
4369 data_src = data_src->next;
4372 /* close ds, only if was opened */
4373 if (ds_open)
4374 sharkd_json_array_close();
4377 sharkd_json_array_open("fol");
4378 follow_iterate_followers(sharkd_follower_visit_layers_cb, edt);
4379 sharkd_json_array_close();
4381 sharkd_json_array_open("followers");
4382 follow_iterate_followers(sharkd_followers_visit_layers_cb, edt);
4383 sharkd_json_array_close();
4385 sharkd_json_result_epilogue();
4388 #define SHARKD_IOGRAPH_MAX_ITEMS 1 << 25 /* 33,554,432 limit of items, same as max_io_items_ in ui/qt/io_graph_dialog.h */
4390 struct sharkd_iograph
4392 /* config */
4393 int hf_index;
4394 io_graph_item_unit_t calc_type;
4395 guint32 interval;
4397 /* result */
4398 int space_items;
4399 int num_items;
4400 io_graph_item_t *items;
4401 GString *error;
4404 static tap_packet_status
4405 sharkd_iograph_packet(void *g, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_, tap_flags_t flags _U_)
4407 struct sharkd_iograph *graph = (struct sharkd_iograph *) g;
4408 int idx;
4409 gboolean update_succeeded;
4411 int64_t tmp_idx = get_io_graph_index(pinfo, graph->interval);
4412 if (tmp_idx < 0 || tmp_idx >= SHARKD_IOGRAPH_MAX_ITEMS)
4413 return TAP_PACKET_DONT_REDRAW;
4415 idx = (int)tmp_idx;
4417 if (idx + 1 > graph->num_items)
4419 if (idx + 1 > graph->space_items)
4421 int new_size = idx + 1024;
4423 graph->items = (io_graph_item_t *) g_realloc(graph->items, sizeof(io_graph_item_t) * new_size);
4424 reset_io_graph_items(&graph->items[graph->space_items], new_size - graph->space_items, graph->hf_index);
4426 graph->space_items = new_size;
4428 else if (graph->items == NULL)
4430 graph->items = g_new(io_graph_item_t, graph->space_items);
4431 reset_io_graph_items(graph->items, graph->space_items, graph->hf_index);
4434 graph->num_items = idx + 1;
4437 update_succeeded = update_io_graph_item(graph->items, idx, pinfo, edt, graph->hf_index, graph->calc_type, graph->interval);
4438 /* XXX - TAP_PACKET_FAILED if the item couldn't be updated, with an error message? */
4439 return update_succeeded ? TAP_PACKET_REDRAW : TAP_PACKET_DONT_REDRAW;
4443 * sharkd_session_process_iograph()
4445 * Process iograph request
4447 * Input:
4448 * (o) interval - interval time, if not specified: 1000
4449 * (o) interval_units - units for interval time, must be 's', 'ms' or 'us', if not specified: ms
4450 * (m) graph0 - First graph request
4451 * (o) graph1...graph9 - Other graph requests
4452 * (o) filter0 - First graph filter
4453 * (o) filter1...filter9 - Other graph filters
4455 * Graph requests can be one of: "packets", "bytes", "bits", "sum:<field>", "frames:<field>", "max:<field>", "min:<field>", "avg:<field>", "load:<field>",
4456 * if you use variant with <field>, you need to pass field name in filter request.
4458 * Output object with attributes:
4459 * (m) iograph - array of graph results with attributes:
4460 * errmsg - graph cannot be constructed
4461 * items - graph values, zeros are skipped, if value is not a number it's next index encoded as hex string
4463 static void
4464 sharkd_session_process_iograph(char *buf, const jsmntok_t *tokens, int count)
4466 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4467 const char *tok_interval_units = json_find_attr(buf, tokens, count, "interval_units");
4468 struct sharkd_iograph graphs[10];
4469 gboolean is_any_ok = FALSE;
4470 int graph_count;
4472 int i;
4474 /* default: 1000ms = one per second */
4475 guint32 interval = 1000;
4476 const char *interval_units = "ms";
4478 if (tok_interval)
4479 ws_strtou32(tok_interval, NULL, &interval);
4481 if (tok_interval_units)
4483 if (strcmp(tok_interval_units, "us") != 0 &&
4484 strcmp(tok_interval_units, "ms") != 0 &&
4485 strcmp(tok_interval_units, "s") != 0)
4487 sharkd_json_error(
4488 rpcid, -7003, NULL,
4489 "Invalid interval_units parameter: '%s', must be 's', 'ms' or 'us'", tok_interval_units
4491 return;
4493 interval_units = tok_interval_units;
4496 guint32 interval_us = 0;
4497 if (strcmp(interval_units, "us") == 0)
4499 interval_us = interval;
4501 else if (strcmp(interval_units, "ms") == 0)
4503 interval_us = 1000 * interval;
4505 else if (strcmp(interval_units, "s") == 0)
4507 interval_us = 1000000 * interval;
4510 for (i = graph_count = 0; i < (int) G_N_ELEMENTS(graphs); i++)
4512 struct sharkd_iograph *graph = &graphs[graph_count];
4514 const char *tok_graph;
4515 const char *tok_filter;
4516 char tok_format_buf[32];
4517 const char *field_name;
4519 snprintf(tok_format_buf, sizeof(tok_format_buf), "graph%d", i);
4520 tok_graph = json_find_attr(buf, tokens, count, tok_format_buf);
4521 if (!tok_graph)
4522 break;
4524 snprintf(tok_format_buf, sizeof(tok_format_buf), "filter%d", i);
4525 tok_filter = json_find_attr(buf, tokens, count, tok_format_buf);
4527 if (!strcmp(tok_graph, "packets"))
4528 graph->calc_type = IOG_ITEM_UNIT_PACKETS;
4529 else if (!strcmp(tok_graph, "bytes"))
4530 graph->calc_type = IOG_ITEM_UNIT_BYTES;
4531 else if (!strcmp(tok_graph, "bits"))
4532 graph->calc_type = IOG_ITEM_UNIT_BITS;
4533 else if (g_str_has_prefix(tok_graph, "sum:"))
4534 graph->calc_type = IOG_ITEM_UNIT_CALC_SUM;
4535 else if (g_str_has_prefix(tok_graph, "frames:"))
4536 graph->calc_type = IOG_ITEM_UNIT_CALC_FRAMES;
4537 else if (g_str_has_prefix(tok_graph, "fields:"))
4538 graph->calc_type = IOG_ITEM_UNIT_CALC_FIELDS;
4539 else if (g_str_has_prefix(tok_graph, "max:"))
4540 graph->calc_type = IOG_ITEM_UNIT_CALC_MAX;
4541 else if (g_str_has_prefix(tok_graph, "min:"))
4542 graph->calc_type = IOG_ITEM_UNIT_CALC_MIN;
4543 else if (g_str_has_prefix(tok_graph, "avg:"))
4544 graph->calc_type = IOG_ITEM_UNIT_CALC_AVERAGE;
4545 else if (g_str_has_prefix(tok_graph, "load:"))
4546 graph->calc_type = IOG_ITEM_UNIT_CALC_LOAD;
4547 else
4548 break;
4550 field_name = strchr(tok_graph, ':');
4551 if (field_name)
4552 field_name = field_name + 1;
4554 /* io_graph_item now supports microseconds (and this parameter
4555 * is expected to be in microseconds.) */
4556 graph->interval = interval_us;
4558 graph->hf_index = -1;
4559 graph->error = check_field_unit(field_name, &graph->hf_index, graph->calc_type);
4561 graph->space_items = 0; /* TODO, can avoid realloc()s in sharkd_iograph_packet() by calculating: capture_time / interval */
4562 graph->num_items = 0;
4563 graph->items = NULL;
4565 if (!graph->error)
4566 graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE, NULL, sharkd_iograph_packet, NULL, NULL);
4568 graph_count++;
4570 if (graph->error)
4572 sharkd_json_error(
4573 rpcid, -6001, NULL,
4574 "%s", graph->error->str
4576 g_string_free(graph->error, TRUE);
4577 return;
4580 if (graph->error == NULL)
4581 is_any_ok = TRUE;
4584 /* retap only if we have at least one ok */
4585 if (is_any_ok)
4586 sharkd_retap();
4588 sharkd_json_result_prologue(rpcid);
4590 sharkd_json_array_open("iograph");
4591 for (i = 0; i < graph_count; i++)
4593 struct sharkd_iograph *graph = &graphs[i];
4595 json_dumper_begin_object(&dumper);
4597 if (graph->error)
4599 fprintf(stderr, "SNAP 6002 - we should never get to here.\n");
4600 g_string_free(graph->error, TRUE);
4601 exit(-1);
4603 else
4605 int idx;
4606 int next_idx = 0;
4608 sharkd_json_array_open("items");
4609 for (idx = 0; idx < graph->num_items; idx++)
4611 double val;
4613 val = get_io_graph_item(graph->items, graph->calc_type, idx, graph->hf_index, &cfile, graph->interval, graph->num_items);
4615 /* if it's zero, don't display */
4616 if (val == 0.0)
4617 continue;
4619 /* cause zeros are not printed, need to output index */
4620 if (next_idx != idx)
4621 sharkd_json_value_stringf(NULL, "%x", idx);
4623 sharkd_json_value_anyf(NULL, "%f", val);
4624 next_idx = idx + 1;
4626 sharkd_json_array_close();
4628 json_dumper_end_object(&dumper);
4630 remove_tap_listener(graph);
4631 g_free(graph->items);
4633 sharkd_json_array_close();
4635 sharkd_json_result_epilogue();
4639 * sharkd_session_process_intervals()
4641 * Process intervals request - generate basic capture file statistics per requested interval.
4643 * Input:
4644 * (o) interval - interval time in ms, if not specified: 1000ms
4645 * (o) filter - filter for generating interval request
4647 * Output object with attributes:
4648 * (m) intervals - array of intervals, with indexes:
4649 * [0] - index of interval,
4650 * [1] - number of frames during interval,
4651 * [2] - number of bytes during interval.
4653 * (m) last - last interval number.
4654 * (m) frames - total number of frames
4655 * (m) bytes - total number of bytes
4657 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
4659 static void
4660 sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
4662 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4663 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4665 const guint8 *filter_data = NULL;
4667 struct
4669 unsigned int frames;
4670 guint64 bytes;
4671 } st, st_total;
4673 nstime_t *start_ts;
4675 guint32 interval_ms = 1000; /* default: one per second */
4677 gint64 idx;
4678 gint64 max_idx = 0;
4680 if (tok_interval)
4681 ws_strtou32(tok_interval, NULL, &interval_ms); // already validated
4683 if (tok_filter)
4685 const struct sharkd_filter_item *filter_item;
4687 filter_item = sharkd_session_filter_data(tok_filter);
4688 if (!filter_item)
4690 sharkd_json_error(
4691 rpcid, -7001, NULL,
4692 "Invalid filter parameter: %s", tok_filter
4694 return;
4696 filter_data = filter_item->filtered;
4699 st_total.frames = 0;
4700 st_total.bytes = 0;
4702 st.frames = 0;
4703 st.bytes = 0;
4705 idx = 0;
4707 sharkd_json_result_prologue(rpcid);
4708 sharkd_json_array_open("intervals");
4710 start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL;
4712 for (guint32 framenum = 1; framenum <= cfile.count; framenum++)
4714 frame_data *fdata;
4715 gint64 msec_rel;
4716 gint64 new_idx;
4718 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
4719 continue;
4721 fdata = sharkd_get_frame(framenum);
4723 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (gint64) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
4724 new_idx = msec_rel / interval_ms;
4726 if (idx != new_idx)
4728 if (st.frames != 0)
4730 sharkd_json_value_anyf(NULL, "[%" PRId64 ",%u,%" PRIu64 "]", idx, st.frames, st.bytes);
4733 idx = new_idx;
4734 if (idx > max_idx)
4735 max_idx = idx;
4737 st.frames = 0;
4738 st.bytes = 0;
4741 st.frames += 1;
4742 st.bytes += fdata->pkt_len;
4744 st_total.frames += 1;
4745 st_total.bytes += fdata->pkt_len;
4748 if (st.frames != 0)
4750 sharkd_json_value_anyf(NULL, "[%" PRId64 ",%u,%" PRIu64 "]", idx, st.frames, st.bytes);
4752 sharkd_json_array_close();
4754 sharkd_json_value_anyf("last", "%" PRId64, max_idx);
4755 sharkd_json_value_anyf("frames", "%u", st_total.frames);
4756 sharkd_json_value_anyf("bytes", "%" PRIu64, st_total.bytes);
4758 sharkd_json_result_epilogue();
4762 * sharkd_session_process_frame()
4764 * Process frame request
4766 * Input:
4767 * (m) frame - requested frame number
4768 * (o) ref_frame - time reference frame number
4769 * (o) prev_frame - previously displayed frame number
4770 * (o) proto - set if output frame tree
4771 * (o) columns - set if output frame columns
4772 * (o) color - set if output color-filter bg/fg
4773 * (o) bytes - set if output frame bytes
4774 * (o) hidden - set if output hidden tree fields
4776 * Output object with attributes:
4777 * (m) err - 0 if succeed
4778 * (o) tree - array of frame nodes with attributes:
4779 * l - label
4780 * t: 'proto', 'framenum', 'url' - type of node
4781 * f - filter string
4782 * fn - field name
4783 * s - severity
4784 * e - subtree ett index
4785 * n - array of subtree nodes
4786 * h - two item array: (item start, item length)
4787 * i - two item array: (appendix start, appendix length)
4788 * p - [RESERVED] two item array: (protocol start, protocol length)
4789 * ds- data src index
4790 * url - only for t:'url', url
4791 * fnum - only for t:'framenum', frame number
4792 * g - if field is generated by Wireshark
4793 * v - if field is hidden
4795 * (o) col - array of column data
4796 * (o) bytes - base64 of frame bytes
4797 * (o) ds - array of other data srcs
4798 * (o) comment - frame comment
4799 * (o) fol - array of follow filters:
4800 * [0] - protocol
4801 * [1] - filter string
4802 * (o) followers - array of followers with attributes:
4803 * protocol - protocol string
4804 * filter - filter string
4805 * stream - stream index number
4806 * sub_stream - sub-stream index number (optional, e.g. for HTTP/2 and QUIC streams)
4807 * (o) i - if frame is ignored
4808 * (o) m - if frame is marked
4809 * (o) bg - color filter - background color in hex
4810 * (o) fg - color filter - foreground color in hex
4812 static void
4813 sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
4815 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
4816 const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
4817 const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
4818 column_info *cinfo = NULL;
4820 guint32 framenum, ref_frame_num, prev_dis_num;
4821 guint32 dissect_flags = SHARKD_DISSECT_FLAG_NULL;
4822 struct sharkd_frame_request_data req_data;
4823 wtap_rec rec; /* Record metadata */
4824 Buffer rec_buf; /* Record data */
4825 enum dissect_request_status status;
4826 int err;
4827 gchar *err_info;
4829 ws_strtou32(tok_frame, NULL, &framenum); // we have already validated this
4831 ref_frame_num = (framenum != 1) ? 1 : 0;
4832 if (tok_ref_frame)
4834 ws_strtou32(tok_ref_frame, NULL, &ref_frame_num);
4835 if (ref_frame_num > framenum)
4837 sharkd_json_error(
4838 rpcid, -8001, NULL,
4839 "Invalid ref_frame - The ref_frame occurs after the frame specified"
4841 return;
4845 prev_dis_num = framenum - 1;
4846 if (tok_prev_frame)
4848 ws_strtou32(tok_prev_frame, NULL, &prev_dis_num);
4849 if (prev_dis_num >= framenum)
4851 sharkd_json_error(
4852 rpcid, -8002, NULL,
4853 "Invalid prev_frame - The prev_frame occurs on or after the frame specified"
4855 return;
4859 if (json_find_attr(buf, tokens, count, "proto") != NULL)
4860 dissect_flags |= SHARKD_DISSECT_FLAG_PROTO_TREE;
4861 if (json_find_attr(buf, tokens, count, "bytes") != NULL)
4862 dissect_flags |= SHARKD_DISSECT_FLAG_BYTES;
4863 if (json_find_attr(buf, tokens, count, "columns") != NULL) {
4864 dissect_flags |= SHARKD_DISSECT_FLAG_COLUMNS;
4865 cinfo = &cfile.cinfo;
4867 if (json_find_attr(buf, tokens, count, "color") != NULL)
4868 dissect_flags |= SHARKD_DISSECT_FLAG_COLOR;
4870 req_data.display_hidden = (json_find_attr(buf, tokens, count, "v") != NULL);
4872 wtap_rec_init(&rec);
4873 ws_buffer_init(&rec_buf, 1514);
4875 status = sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num,
4876 &rec, &rec_buf, cinfo, dissect_flags,
4877 &sharkd_session_process_frame_cb, &req_data, &err, &err_info);
4878 switch (status) {
4880 case DISSECT_REQUEST_SUCCESS:
4881 /* success */
4882 break;
4884 case DISSECT_REQUEST_NO_SUCH_FRAME:
4885 sharkd_json_error(
4886 rpcid, -8003, NULL,
4887 "Invalid frame - The frame number requested is out of range"
4889 break;
4891 case DISSECT_REQUEST_READ_ERROR:
4892 sharkd_json_error(
4893 rpcid, -8003, NULL,
4894 /* XXX - show the error details */
4895 "Read error - The frame could not be read from the file"
4897 g_free(err_info);
4898 break;
4901 wtap_rec_cleanup(&rec);
4902 ws_buffer_free(&rec_buf);
4906 * sharkd_session_process_check()
4908 * Process check request.
4910 * Input:
4911 * (o) filter - filter to be checked
4912 * (o) field - field to be checked
4914 * Output object with attributes:
4915 * (m) err - always 0
4916 * (o) filter - 'ok', 'warn' or error message
4917 * (o) field - 'ok', or 'notfound'
4919 static int
4920 sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
4922 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4923 const char *tok_field = json_find_attr(buf, tokens, count, "field");
4925 if (tok_filter != NULL)
4927 dfilter_t *dfp;
4928 df_error_t *df_err = NULL;
4930 if (dfilter_compile(tok_filter, &dfp, &df_err))
4932 if (dfp && dfilter_deprecated_tokens(dfp))
4933 sharkd_json_warning(rpcid, "Filter contains deprecated tokens");
4934 else
4935 sharkd_json_simple_ok(rpcid);
4937 dfilter_free(dfp);
4938 df_error_free(&df_err);
4939 return 0;
4941 else
4943 sharkd_json_error(
4944 rpcid, -5001, NULL,
4945 "Filter invalid - %s", df_err->msg
4947 df_error_free(&df_err);
4948 return -5001;
4952 if (tok_field != NULL)
4954 header_field_info *hfi = proto_registrar_get_byname(tok_field);
4956 if (!hfi)
4958 sharkd_json_error(
4959 rpcid, -5002, NULL,
4960 "Field %s not found", tok_field
4962 return -5002;
4964 else
4966 sharkd_json_simple_ok(rpcid);
4967 return 0;
4971 sharkd_json_simple_ok(rpcid);
4972 return 0;
4975 struct sharkd_session_process_complete_pref_data
4977 const char *module;
4978 const char *pref;
4981 static guint
4982 sharkd_session_process_complete_pref_cb(module_t *module, gpointer d)
4984 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
4986 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
4987 return 0;
4989 json_dumper_begin_object(&dumper);
4990 sharkd_json_value_string("f", module->name);
4991 sharkd_json_value_string("d", module->title);
4992 json_dumper_end_object(&dumper);
4994 return 0;
4997 static guint
4998 sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d)
5000 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5001 const char *pref_name = prefs_get_name(pref);
5002 const char *pref_title = prefs_get_title(pref);
5004 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
5005 return 0;
5007 json_dumper_begin_object(&dumper);
5008 sharkd_json_value_stringf("f", "%s.%s", data->module, pref_name);
5009 sharkd_json_value_string("d", pref_title);
5010 json_dumper_end_object(&dumper);
5012 return 0; /* continue */
5016 * sharkd_session_process_complete()
5018 * Process complete request
5020 * Input:
5021 * (o) field - field to be completed
5022 * (o) pref - preference to be completed
5024 * Output object with attributes:
5025 * (m) err - always 0
5026 * (o) field - array of object with attributes:
5027 * (m) f - field text
5028 * (o) t - field type (FT_ number)
5029 * (o) n - field name
5030 * (o) pref - array of object with attributes:
5031 * (m) f - pref name
5032 * (o) d - pref description
5034 static int
5035 sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
5037 const char *tok_field = json_find_attr(buf, tokens, count, "field");
5038 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5040 sharkd_json_result_prologue(rpcid);
5042 if (tok_field != NULL && tok_field[0])
5044 const size_t filter_length = strlen(tok_field);
5045 const int filter_with_dot = !!strchr(tok_field, '.');
5047 void *proto_cookie;
5048 void *field_cookie;
5049 int proto_id;
5051 sharkd_json_array_open("field");
5053 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
5055 protocol_t *protocol = find_protocol_by_id(proto_id);
5056 const char *protocol_filter;
5057 const char *protocol_name;
5058 header_field_info *hfinfo;
5060 if (!proto_is_protocol_enabled(protocol))
5061 continue;
5063 protocol_name = proto_get_protocol_long_name(protocol);
5064 protocol_filter = proto_get_protocol_filter_name(proto_id);
5066 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
5068 json_dumper_begin_object(&dumper);
5070 sharkd_json_value_string("f", protocol_filter);
5071 sharkd_json_value_anyf("t", "%d", FT_PROTOCOL);
5072 sharkd_json_value_string("n", protocol_name);
5074 json_dumper_end_object(&dumper);
5077 if (!filter_with_dot)
5078 continue;
5080 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
5082 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
5083 continue;
5085 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
5087 json_dumper_begin_object(&dumper);
5089 sharkd_json_value_string("f", hfinfo->abbrev);
5091 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
5092 if (hfinfo->same_name_next == NULL)
5094 sharkd_json_value_anyf("t", "%d", hfinfo->type);
5095 sharkd_json_value_string("n", hfinfo->name);
5098 json_dumper_end_object(&dumper);
5103 sharkd_json_array_close();
5106 if (tok_pref != NULL && tok_pref[0])
5108 struct sharkd_session_process_complete_pref_data data;
5109 char *dot_sepa;
5111 data.module = tok_pref;
5112 data.pref = tok_pref;
5114 sharkd_json_array_open("pref");
5115 if ((dot_sepa = strchr(tok_pref, '.')))
5117 module_t *pref_mod;
5119 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5120 data.pref = dot_sepa + 1;
5122 pref_mod = prefs_find_module(data.module);
5123 if (pref_mod)
5124 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
5126 *dot_sepa = '.';
5128 else
5130 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
5132 sharkd_json_array_close();
5135 sharkd_json_result_epilogue();
5137 return 0;
5141 * sharkd_session_process_setcomment()
5143 * Process setcomment request
5145 * Input:
5146 * (m) frame - frame number
5147 * (o) comment - user comment
5149 * Output object with attributes:
5150 * (m) err - error code: 0 succeed
5152 * Note:
5153 * For now, adds comments, doesn't remove or replace them.
5155 static void
5156 sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count)
5158 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
5159 const char *tok_comment = json_find_attr(buf, tokens, count, "comment");
5161 guint32 framenum;
5162 frame_data *fdata;
5163 wtap_opttype_return_val ret;
5164 wtap_block_t pkt_block = NULL;
5166 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
5168 sharkd_json_error(
5169 rpcid, -3001, NULL,
5170 "Frame number must be a positive integer"
5172 return;
5175 fdata = sharkd_get_frame(framenum); // BUG HERE - If no file loaded you get a crash
5176 if (!fdata)
5178 sharkd_json_error(
5179 rpcid, -3002, NULL,
5180 "Frame number is out of range"
5182 return;
5185 pkt_block = sharkd_get_packet_block(fdata);
5187 ret = wtap_block_add_string_option(pkt_block, OPT_COMMENT, tok_comment, strlen(tok_comment));
5189 if (ret != WTAP_OPTTYPE_SUCCESS)
5191 sharkd_json_error(
5192 rpcid, -3003, NULL,
5193 "Unable to set the comment"
5196 else
5198 sharkd_set_modified_block(fdata, pkt_block);
5199 sharkd_json_simple_ok(rpcid);
5204 * sharkd_session_process_setconf()
5206 * Process setconf request
5208 * Input:
5209 * (m) name - preference name
5210 * (m) value - preference value
5212 * Output object with attributes:
5213 * (m) err - error code: 0 succeed
5215 static void
5216 sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
5218 const char *tok_name = json_find_attr(buf, tokens, count, "name");
5219 const char *tok_value = json_find_attr(buf, tokens, count, "value");
5220 char pref[4096];
5221 char *errmsg = NULL;
5223 prefs_set_pref_e ret;
5225 if (!tok_name || tok_name[0] == '\0')
5227 sharkd_json_error(
5228 rpcid, -4001, NULL,
5229 "Preference name missing"
5231 return;
5234 if (!tok_value)
5236 sharkd_json_error(
5237 rpcid, -4002, NULL,
5238 "Preference value missing"
5240 return;
5243 snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
5245 ret = prefs_set_pref(pref, &errmsg);
5247 switch (ret)
5249 case PREFS_SET_OK:
5250 sharkd_json_simple_ok(rpcid);
5251 break;
5253 case PREFS_SET_OBSOLETE:
5254 sharkd_json_error(
5255 rpcid, -4003, NULL,
5256 "The preference specified is obsolete"
5258 break;
5260 case PREFS_SET_NO_SUCH_PREF:
5261 sharkd_json_error(
5262 rpcid, -4004, NULL,
5263 "No such preference exists"
5265 break;
5267 default:
5268 sharkd_json_error(
5269 rpcid, -4005, NULL,
5270 "Unable to set the preference"
5274 g_free(errmsg);
5277 struct sharkd_session_process_dumpconf_data
5279 module_t *module;
5282 static guint
5283 sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d)
5285 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5286 const char *pref_name = prefs_get_name(pref);
5288 char json_pref_key[512];
5290 snprintf(json_pref_key, sizeof(json_pref_key), "%s.%s", data->module->name, pref_name);
5291 sharkd_json_object_open(json_pref_key);
5293 switch (prefs_get_type(pref))
5295 case PREF_UINT:
5296 sharkd_json_value_anyf("u", "%u", prefs_get_uint_value_real(pref, pref_current));
5297 if (prefs_get_uint_base(pref) != 10)
5298 sharkd_json_value_anyf("ub", "%u", prefs_get_uint_base(pref));
5299 break;
5301 case PREF_BOOL:
5302 sharkd_json_value_anyf("b", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
5303 break;
5305 case PREF_STRING:
5306 case PREF_SAVE_FILENAME:
5307 case PREF_OPEN_FILENAME:
5308 case PREF_DIRNAME:
5309 case PREF_PASSWORD:
5310 case PREF_DISSECTOR:
5311 sharkd_json_value_string("s", prefs_get_string_value(pref, pref_current));
5312 break;
5314 case PREF_ENUM:
5316 const enum_val_t *enums;
5318 sharkd_json_array_open("e");
5319 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
5321 json_dumper_begin_object(&dumper);
5323 sharkd_json_value_anyf("v", "%d", enums->value);
5325 if (enums->value == prefs_get_enum_value(pref, pref_current))
5326 sharkd_json_value_anyf("s", "1");
5328 sharkd_json_value_string("d", enums->description);
5330 json_dumper_end_object(&dumper);
5332 sharkd_json_array_close();
5333 break;
5336 case PREF_RANGE:
5337 case PREF_DECODE_AS_RANGE:
5339 char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
5340 sharkd_json_value_string("r", range_str);
5341 wmem_free(NULL, range_str);
5342 break;
5345 case PREF_UAT:
5347 uat_t *uat = prefs_get_uat_value(pref);
5348 guint idx;
5350 sharkd_json_array_open("t");
5351 for (idx = 0; idx < uat->raw_data->len; idx++)
5353 void *rec = UAT_INDEX_PTR(uat, idx);
5354 guint colnum;
5356 sharkd_json_array_open(NULL);
5357 for (colnum = 0; colnum < uat->ncols; colnum++)
5359 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
5361 sharkd_json_value_string(NULL, str);
5362 g_free(str);
5365 sharkd_json_array_close();
5368 sharkd_json_array_close();
5369 break;
5372 case PREF_COLOR:
5373 case PREF_CUSTOM:
5374 case PREF_STATIC_TEXT:
5375 case PREF_OBSOLETE:
5376 /* TODO */
5377 break;
5380 #if 0
5381 sharkd_json_value_string("t", prefs_get_title(pref));
5382 #endif
5384 sharkd_json_object_close();
5386 return 0; /* continue */
5389 static guint
5390 sharkd_session_process_dumpconf_mod_cb(module_t *module, gpointer d)
5392 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5394 data->module = module;
5395 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
5397 return 0;
5401 * sharkd_session_process_dumpconf()
5403 * Process dumpconf request
5405 * Input:
5406 * (o) pref - module, or preference, NULL for all
5408 * Output object with attributes:
5409 * (o) prefs - object with module preferences
5410 * (m) [KEY] - preference name
5411 * (o) u - preference value (for PREF_UINT)
5412 * (o) ub - preference value suggested base for display (for PREF_UINT) and if different than 10
5413 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
5414 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME, PREF_PASSWORD, PREF_DISSECTOR)
5415 * (o) e - preference possible values (only for PREF_ENUM)
5416 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
5417 * (o) t - preference value (only for PREF_UAT)
5419 static void
5420 sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
5422 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5423 module_t *pref_mod;
5424 char *dot_sepa;
5426 if (!tok_pref)
5428 struct sharkd_session_process_dumpconf_data data;
5430 data.module = NULL;
5432 sharkd_json_result_prologue(rpcid);
5434 sharkd_json_object_open("prefs");
5435 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
5436 sharkd_json_object_close();
5438 sharkd_json_result_epilogue();
5439 return;
5442 if ((dot_sepa = strchr(tok_pref, '.')))
5444 pref_t *pref = NULL;
5446 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5447 pref_mod = prefs_find_module(tok_pref);
5448 if (pref_mod)
5449 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
5450 *dot_sepa = '.';
5452 if (pref)
5454 struct sharkd_session_process_dumpconf_data data;
5456 data.module = pref_mod;
5458 sharkd_json_result_prologue(rpcid);
5460 sharkd_json_object_open("prefs");
5461 sharkd_session_process_dumpconf_cb(pref, &data);
5462 sharkd_json_object_close();
5464 sharkd_json_result_epilogue();
5465 return;
5467 else
5469 sharkd_json_error(
5470 rpcid, -9001, NULL,
5471 "Invalid pref %s.", tok_pref
5473 return;
5478 pref_mod = prefs_find_module(tok_pref);
5479 if (pref_mod)
5481 struct sharkd_session_process_dumpconf_data data;
5483 data.module = pref_mod;
5485 sharkd_json_result_prologue(rpcid);
5487 sharkd_json_object_open("prefs");
5488 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
5489 sharkd_json_object_close();
5491 sharkd_json_result_epilogue();
5493 else
5495 sharkd_json_error(
5496 rpcid, -9002, NULL,
5497 "Invalid pref %s.", tok_pref
5502 struct sharkd_download_rtp
5504 rtpstream_id_t id;
5505 GSList *packets;
5506 double start_time;
5509 static void
5510 sharkd_rtp_download_free_items(void *ptr)
5512 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
5514 g_free(rtp_packet->info);
5515 g_free(rtp_packet->payload_data);
5516 g_free(rtp_packet);
5519 static void
5520 sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
5522 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
5523 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
5525 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
5527 guint32 audio_out_rate_ = 0;
5528 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
5529 struct SpeexResamplerState_ *audio_resampler_ = NULL;
5531 gsize resample_buff_len = 0x1000;
5532 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
5533 spx_uint32_t cur_in_rate = 0;
5534 char *write_buff = NULL;
5535 size_t write_bytes = 0;
5536 unsigned channels = 0;
5537 unsigned sample_rate = 0;
5539 GSList *l;
5541 for (l = req->packets; l; l = l->next)
5543 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
5545 SAMPLE *decode_buff = NULL;
5546 size_t decoded_bytes;
5548 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
5549 if (decoded_bytes == 0 || sample_rate == 0)
5551 /* We didn't decode anything. Clean up and prep for the next packet. */
5552 g_free(decode_buff);
5553 continue;
5556 if (audio_out_rate_ == 0)
5558 guint32 tmp32;
5559 guint16 tmp16;
5560 char wav_hdr[44];
5562 /* First non-zero wins */
5563 audio_out_rate_ = sample_rate;
5565 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
5567 /* write WAVE header */
5568 memset(&wav_hdr, 0, sizeof(wav_hdr));
5569 memcpy(&wav_hdr[0], "RIFF", 4);
5570 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5571 memcpy(&wav_hdr[8], "WAVE", 4);
5573 memcpy(&wav_hdr[12], "fmt ", 4);
5574 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
5575 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
5576 /* # channels */
5577 tmp16 = channels;
5578 memcpy(&wav_hdr[22], &tmp16, 2);
5579 /* sample rate */
5580 tmp32 = sample_rate;
5581 memcpy(&wav_hdr[24], &tmp32, 4);
5582 /* byte rate */
5583 tmp32 = sample_rate * channels * sample_bytes_;
5584 memcpy(&wav_hdr[28], &tmp32, 4);
5585 /* block align */
5586 tmp16 = channels * sample_bytes_;
5587 memcpy(&wav_hdr[32], &tmp16, 2);
5588 /* bits per sample */
5589 tmp16 = 8 * sample_bytes_;
5590 memcpy(&wav_hdr[34], &tmp16, 2);
5592 memcpy(&wav_hdr[36], "data", 4);
5593 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5595 json_dumper_write_base64(&dumper, wav_hdr, sizeof(wav_hdr));
5598 // Write samples to our file.
5599 write_buff = (char *) decode_buff;
5600 write_bytes = decoded_bytes;
5602 if (audio_out_rate_ != sample_rate)
5604 spx_uint32_t in_len, out_len;
5606 /* Resample the audio to match our previous output rate. */
5607 if (!audio_resampler_)
5609 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL);
5610 speex_resampler_skip_zeros(audio_resampler_);
5611 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
5613 else
5615 spx_uint32_t audio_out_rate;
5616 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
5618 if (sample_rate != cur_in_rate)
5620 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
5621 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
5624 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
5625 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
5626 if (out_len * sample_bytes_ > resample_buff_len)
5628 while ((out_len * sample_bytes_ > resample_buff_len))
5629 resample_buff_len *= 2;
5630 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
5633 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
5634 write_buff = (char *) resample_buff;
5635 write_bytes = out_len * sample_bytes_;
5638 /* Write the decoded, possibly-resampled audio */
5639 json_dumper_write_base64(&dumper, write_buff, write_bytes);
5641 g_free(decode_buff);
5644 g_free(resample_buff);
5645 g_hash_table_destroy(decoders_hash_);
5648 static tap_packet_status
5649 sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data, tap_flags_t flags _U_)
5651 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
5652 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
5654 /* do not consider RTP packets without a setup frame */
5655 if (rtp_info->info_setup_frame_num == 0)
5656 return TAP_PACKET_DONT_REDRAW;
5658 if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info))
5660 rtp_packet_t *rtp_packet;
5662 rtp_packet = g_new0(rtp_packet_t, 1);
5663 rtp_packet->info = (struct _rtp_info *) g_memdup2(rtp_info, sizeof(struct _rtp_info));
5665 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
5666 rtp_packet->payload_data = (guint8 *) g_memdup2(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
5668 if (!req_rtp->packets)
5669 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
5671 rtp_packet->frame_num = pinfo->num;
5672 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
5674 /* XXX, O(n) optimize */
5675 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
5678 return TAP_PACKET_DONT_REDRAW;
5681 static gboolean
5682 sharkd_session_eo_retap_listener(const char *tap_type) {
5683 gboolean ok = TRUE;
5684 register_eo_t *eo = NULL;
5685 GString *tap_error = NULL;
5686 void *tap_data = NULL;
5687 GFreeFunc tap_free = NULL;
5689 // get <name> from eo:<name>, get_eo_by_name only needs the name (http etc.)
5690 eo = get_eo_by_name(tap_type + 3);
5691 if (!eo)
5693 ok = FALSE;
5694 sharkd_json_error(
5695 rpcid, -11011, NULL,
5696 "sharkd_session_eo_retap_listener() eo=%s not found", tap_type + 3
5700 if (ok)
5702 tap_error = sharkd_session_eo_register_tap_listener(eo, tap_type, NULL, NULL, &tap_data, &tap_free);
5703 if (tap_error)
5705 ok = FALSE;
5706 sharkd_json_error(
5707 rpcid, -10002, NULL,
5708 "sharkd_session_eo_retap_listener() sharkd_session_eo_register_tap_listener error %s",
5709 tap_error->str);
5710 g_string_free(tap_error, TRUE);
5714 if (ok)
5715 sharkd_retap();
5717 if (!tap_error)
5718 remove_tap_listener(tap_data);
5720 if (tap_free)
5721 tap_free(tap_data);
5723 return ok;
5727 * sharkd_session_process_download()
5729 * Process download request
5731 * Input:
5732 * (m) token - token to download
5734 * Output object with attributes:
5735 * (o) file - suggested name of file
5736 * (o) mime - suggested content type
5737 * (o) data - payload base64 encoded
5739 static void
5740 sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
5742 const char *tok_token = json_find_attr(buf, tokens, count, "token");
5744 if (!tok_token)
5746 sharkd_json_error(
5747 rpcid, -10005, NULL,
5748 "missing token"
5750 return;
5753 if (!strncmp(tok_token, "eo:", 3))
5755 // get eo:<name> from eo:<name>_<row>
5756 char *tap_type = g_strdup(tok_token);
5757 char *tmp = strrchr(tap_type, '_');
5758 if (tmp)
5759 *tmp = '\0';
5761 // if eo:<name> not in sharkd_eo_list, retap
5762 if (!sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type) &&
5763 !sharkd_session_eo_retap_listener(tap_type))
5765 g_free(tap_type);
5766 // sharkd_json_error called in sharkd_session_eo_retap_listener
5767 return;
5770 g_free(tap_type);
5772 struct sharkd_export_object_list *object_list;
5773 const export_object_entry_t *eo_entry = NULL;
5775 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
5777 size_t eo_type_len = strlen(object_list->type);
5779 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
5781 int row;
5783 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
5784 break;
5786 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
5787 break;
5791 if (eo_entry)
5793 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
5794 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
5796 sharkd_json_result_prologue(rpcid);
5797 sharkd_json_value_string("file", filename);
5798 sharkd_json_value_string("mime", mime);
5799 sharkd_json_value_base64("data", eo_entry->payload_data, eo_entry->payload_len);
5800 sharkd_json_result_epilogue();
5802 else
5804 sharkd_json_result_prologue(rpcid);
5805 sharkd_json_result_epilogue();
5808 else if (!strcmp(tok_token, "ssl-secrets"))
5810 gsize str_len;
5811 char *str = ssl_export_sessions(&str_len);
5813 if (str)
5815 const char *mime = "text/plain";
5816 const char *filename = "keylog.txt";
5818 sharkd_json_result_prologue(rpcid);
5819 sharkd_json_value_string("file", filename);
5820 sharkd_json_value_string("mime", mime);
5821 sharkd_json_value_base64("data", str, str_len);
5822 sharkd_json_result_epilogue();
5824 g_free(str);
5826 else if (!strncmp(tok_token, "rtp:", 4))
5828 struct sharkd_download_rtp rtp_req;
5829 GString *tap_error;
5831 memset(&rtp_req, 0, sizeof(rtp_req));
5832 if (!sharkd_rtp_match_init(&rtp_req.id, tok_token + 4))
5834 sharkd_json_error(
5835 rpcid, -10001, NULL,
5836 "sharkd_session_process_download() rtp tokenizing error %s", tok_token
5838 return;
5841 tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL, NULL);
5842 if (tap_error)
5844 sharkd_json_error(
5845 rpcid, -10002, NULL,
5846 "sharkd_session_process_download() rtp error %s", tap_error->str
5848 g_string_free(tap_error, TRUE);
5849 return;
5852 sharkd_retap();
5853 remove_tap_listener(&rtp_req);
5855 if (rtp_req.packets)
5857 const char *mime = "audio/x-wav";
5858 const char *filename = tok_token;
5860 sharkd_json_result_prologue(rpcid);
5861 sharkd_json_value_string("file", filename);
5862 sharkd_json_value_string("mime", mime);
5864 json_dumper_set_member_name(&dumper, "data");
5865 json_dumper_begin_base64(&dumper);
5866 sharkd_rtp_download_decode(&rtp_req);
5867 json_dumper_end_base64(&dumper);
5869 sharkd_json_result_epilogue();
5871 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
5873 else
5875 sharkd_json_error(
5876 rpcid, -10003, NULL,
5877 "no rtp data available"
5881 else
5883 sharkd_json_error(
5884 rpcid, -10004, NULL,
5885 "unrecognized token"
5890 static void
5891 sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
5893 if (json_prep(buf, tokens, count))
5895 /* don't need [0] token */
5896 tokens++;
5897 count--;
5899 const char* tok_method = json_find_attr(buf, tokens, count, "method");
5901 if (!tok_method) {
5902 sharkd_json_error(
5903 rpcid, -32601, NULL,
5904 "No method found");
5905 return;
5907 if (!strcmp(tok_method, "load"))
5908 sharkd_session_process_load(buf, tokens, count);
5909 else if (!strcmp(tok_method, "status"))
5910 sharkd_session_process_status();
5911 else if (!strcmp(tok_method, "analyse"))
5912 sharkd_session_process_analyse();
5913 else if (!strcmp(tok_method, "info"))
5914 sharkd_session_process_info();
5915 else if (!strcmp(tok_method, "check"))
5916 sharkd_session_process_check(buf, tokens, count);
5917 else if (!strcmp(tok_method, "complete"))
5918 sharkd_session_process_complete(buf, tokens, count);
5919 else if (!strcmp(tok_method, "frames"))
5920 sharkd_session_process_frames(buf, tokens, count);
5921 else if (!strcmp(tok_method, "tap"))
5922 sharkd_session_process_tap(buf, tokens, count);
5923 else if (!strcmp(tok_method, "follow"))
5924 sharkd_session_process_follow(buf, tokens, count);
5925 else if (!strcmp(tok_method, "iograph"))
5926 sharkd_session_process_iograph(buf, tokens, count);
5927 else if (!strcmp(tok_method, "intervals"))
5928 sharkd_session_process_intervals(buf, tokens, count);
5929 else if (!strcmp(tok_method, "frame"))
5930 sharkd_session_process_frame(buf, tokens, count);
5931 else if (!strcmp(tok_method, "setcomment"))
5932 sharkd_session_process_setcomment(buf, tokens, count);
5933 else if (!strcmp(tok_method, "setconf"))
5934 sharkd_session_process_setconf(buf, tokens, count);
5935 else if (!strcmp(tok_method, "dumpconf"))
5936 sharkd_session_process_dumpconf(buf, tokens, count);
5937 else if (!strcmp(tok_method, "download"))
5938 sharkd_session_process_download(buf, tokens, count);
5939 else if (!strcmp(tok_method, "bye"))
5941 sharkd_json_simple_ok(rpcid);
5942 exit(0);
5944 else
5946 sharkd_json_error(
5947 rpcid, -32601, NULL,
5948 "The method \"%s\" is unknown", tok_method
5955 sharkd_session_main(int mode_setting)
5957 char buf[8 * 1024];
5958 jsmntok_t *tokens = NULL;
5959 int tokens_max = -1;
5961 mode = mode_setting;
5963 fprintf(stderr, "Hello in child.\n");
5965 dumper.output_file = stdout;
5967 filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free);
5969 #ifdef HAVE_MAXMINDDB
5970 /* mmdbresolve was stopped before fork(), force starting it */
5971 uat_get_table_by_name("MaxMind Database Paths")->post_update_cb();
5972 #endif
5974 set_resolution_synchrony(TRUE);
5976 while (fgets(buf, sizeof(buf), stdin))
5978 /* every command is line separated JSON */
5979 int ret;
5981 ret = json_parse(buf, NULL, 0);
5982 if (ret <= 0)
5984 sharkd_json_error(
5985 rpcid, -32600, NULL,
5986 "Invalid JSON(1)"
5988 continue;
5991 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
5992 ret += 1;
5994 if (tokens == NULL || tokens_max < ret)
5996 tokens_max = ret;
5997 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
6000 memset(tokens, 0, ret * sizeof(jsmntok_t));
6002 ret = json_parse(buf, tokens, ret);
6003 if (ret <= 0)
6005 sharkd_json_error(
6006 rpcid, -32600, NULL,
6007 "Invalid JSON(2)"
6009 continue;
6012 host_name_lookup_process();
6014 sharkd_session_process(buf, tokens, ret);
6017 g_hash_table_destroy(filter_table);
6018 g_free(tokens);
6020 return 0;