Bug Summary

File:builds/wireshark/wireshark/ui/io_graph_item.h
Warning:line 214, column 35
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sharkd_session.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-18/lib/clang/18 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-truncation -Wno-format-nonliteral -Wno-pointer-sign -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fwrapv -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -dwarf-debug-flags /usr/lib/llvm-18/bin/clang -### --analyze -x c -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -fvisibility=hidden -fexcess-precision=fast -fstrict-flex-arrays=3 -fstack-clash-protection -fcf-protection=full -D _GLIBCXX_ASSERTIONS -fstack-protector-strong -fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -fexceptions -Wno-format-truncation -Wno-format-nonliteral -fdiagnostics-color=always -Wno-pointer-sign -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -std=gnu11 -fPIE /builds/wireshark/wireshark/sharkd_session.c -o /builds/wireshark/wireshark/sbout/2025-01-02-100258-3934-1 -Xclang -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2025-01-02-100258-3934-1 -x c /builds/wireshark/wireshark/sharkd_session.c

/builds/wireshark/wireshark/sharkd_session.c

1/* sharkd_session.c
2 *
3 * Copyright (C) 2016 Jakub Zawadzki
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <[email protected]>
7 * Copyright 1998 Gerald Combs
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12#include <config.h>
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <stdarg.h>
17#include <string.h>
18#include <errno(*__errno_location ()).h>
19#include <inttypes.h>
20
21#include <glib.h>
22
23#include <wsutil/wsjson.h>
24#include <wsutil/json_dumper.h>
25#include <wsutil/ws_assert.h>
26#include <wsutil/wsgcrypt.h>
27
28#include <file.h>
29#include <epan/epan_dissect.h>
30#include <epan/exceptions.h>
31#include <epan/color_filters.h>
32#include <epan/prefs.h>
33#include <epan/prefs-int.h>
34#include <epan/uat-int.h>
35#include <wiretap/wtap.h>
36
37#include <epan/column.h>
38#include <epan/column-info.h>
39
40#include <ui/ssl_key_export.h>
41
42#include <ui/io_graph_item.h>
43#include <epan/stats_tree_priv.h>
44#include <epan/stat_tap_ui.h>
45#include <epan/conversation_table.h>
46#include <epan/sequence_analysis.h>
47#include <epan/expert.h>
48#include <epan/export_object.h>
49#include <epan/follow.h>
50#include <epan/rtd_table.h>
51#include <epan/srt_table.h>
52#include <epan/to_str.h>
53
54#include <epan/dissectors/packet-h225.h>
55#include <epan/rtp_pt.h>
56#include <ui/voip_calls.h>
57#include <ui/rtp_stream.h>
58#include <ui/tap-rtp-common.h>
59#include <ui/tap-rtp-analysis.h>
60#include <ui/cli/tap-protohierstat.h>
61#include <ui/cli/tap-voip.h>
62#include <wsutil/version_info.h>
63#include <epan/to_str.h>
64
65#include <epan/addr_resolv.h>
66#include <epan/dissectors/packet-rtp.h>
67#include <ui/rtp_media.h>
68#include <ui/mcast_stream.h>
69#include <speex/speex_resampler.h>
70
71#include <epan/maxmind_db.h>
72
73#include <wsutil/pint.h>
74#include <wsutil/strnatcmp.h>
75#include <wsutil/strtoi.h>
76
77#include "globals.h"
78
79#include "sharkd.h"
80
81struct sharkd_filter_item
82{
83 uint8_t *filtered; /* can be NULL if all frames are matching for given filter. */
84};
85
86static GHashTable *filter_table;
87
88static int mode;
89static uint32_t rpcid;
90
91static json_dumper dumper;
92
93
94static const char *
95json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
96{
97 int i;
98
99 for (i = 0; i < count; i += 2)
100 {
101 const char *tok_attr = &buf[tokens[i + 0].start];
102 const char *tok_value = &buf[tokens[i + 1].start];
103
104 if (!strcmp(tok_attr, attr))
105 return tok_value;
106 }
107
108 return NULL((void*)0);
109}
110
111static void
112json_print_base64(const uint8_t *data, size_t len)
113{
114 json_dumper_begin_base64(&dumper);
115 json_dumper_write_base64(&dumper, data, len);
116 json_dumper_end_base64(&dumper);
117}
118
119static void G_GNUC_PRINTF(2, 3)__attribute__((__format__ (__printf__, 2, 3)))
120sharkd_json_value_anyf(const char *key, const char *format, ...)
121{
122 if (key)
123 json_dumper_set_member_name(&dumper, key);
124
125 va_list ap;
126 va_start(ap, format)__builtin_va_start(ap, format);
127 json_dumper_value_va_list(&dumper, format, ap);
128 va_end(ap)__builtin_va_end(ap);
129}
130
131static void
132sharkd_json_value_string(const char *key, const char *str)
133{
134 if (key)
135 json_dumper_set_member_name(&dumper, key);
136 json_dumper_value_string(&dumper, str);
137}
138
139static void
140sharkd_json_value_base64(const char *key, const uint8_t *data, size_t len)
141{
142 if (key)
143 json_dumper_set_member_name(&dumper, key);
144 json_print_base64(data, len);
145}
146
147static void G_GNUC_PRINTF(2, 3)__attribute__((__format__ (__printf__, 2, 3)))
148sharkd_json_value_stringf(const char *key, const char *format, ...)
149{
150 if (key)
151 json_dumper_set_member_name(&dumper, key);
152
153 va_list ap;
154 va_start(ap, format)__builtin_va_start(ap, format);
155 char* sformat = ws_strdup_printf("\"%s\"", format)wmem_strdup_printf(((void*)0), "\"%s\"", format);
156 json_dumper_value_va_list(&dumper, sformat, ap);
157 g_free(sformat);
158 va_end(ap)__builtin_va_end(ap);
159}
160
161static void
162sharkd_json_array_open(const char *key)
163{
164 if (key)
165 json_dumper_set_member_name(&dumper, key);
166 json_dumper_begin_array(&dumper);
167}
168
169static void
170sharkd_json_array_close(void)
171{
172 json_dumper_end_array(&dumper);
173}
174
175static void
176sharkd_json_object_open(const char *key)
177{
178 if (key)
179 json_dumper_set_member_name(&dumper, key);
180 json_dumper_begin_object(&dumper);
181}
182
183static void
184sharkd_json_object_close(void)
185{
186 json_dumper_end_object(&dumper);
187}
188
189static void
190sharkd_json_response_open(uint32_t id)
191{
192 json_dumper_begin_object(&dumper); // start the message
193 sharkd_json_value_string("jsonrpc", "2.0");
194 sharkd_json_value_anyf("id", "%d", id);
195}
196
197static void
198sharkd_json_response_close(void)
199{
200 json_dumper_end_object(&dumper); // end the message
201
202 json_dumper_finish(&dumper);
203
204 /*
205 * We do an explicit fflush after every line, because
206 * we want output to be written to the socket as soon
207 * as the line is complete.
208 *
209 * The stream is fully-buffered by default, so it's
210 * only flushed when the buffer fills or the FILE *
211 * is closed. On UN*X, we could set it to be line
212 * buffered, but the MSVC standard I/O routines don't
213 * support line buffering - they only support *byte*
214 * buffering, doing a write for every byte written,
215 * which is too inefficient, and full buffering,
216 * which is what you get if you request line buffering.
217 */
218 fflush(stdoutstdout);
219}
220
221static void
222sharkd_json_result_prologue(uint32_t id)
223{
224 sharkd_json_response_open(id);
225 sharkd_json_object_open("result"); // start the result object
226}
227
228static void
229sharkd_json_result_epilogue(void)
230{
231 json_dumper_end_object(&dumper); // end the result object
232 sharkd_json_response_close();
233}
234
235static void
236sharkd_json_result_array_prologue(uint32_t id)
237{
238 sharkd_json_response_open(id);
239 sharkd_json_array_open("result"); // start the result array
240}
241
242static void
243sharkd_json_result_array_epilogue(void)
244{
245 sharkd_json_array_close(); // end of result array
246 sharkd_json_response_close();
247}
248
249static void
250sharkd_json_simple_ok(uint32_t id)
251{
252 sharkd_json_result_prologue(id);
253 sharkd_json_value_string("status", "OK");
254 sharkd_json_result_epilogue();
255}
256
257static void
258sharkd_json_warning(uint32_t id, char *warning)
259{
260 sharkd_json_result_prologue(id);
261 sharkd_json_value_string("status", "Warning");
262 sharkd_json_value_string("warning", warning);
263 sharkd_json_result_epilogue();
264}
265
266static void G_GNUC_PRINTF(4, 5)__attribute__((__format__ (__printf__, 4, 5)))
267sharkd_json_error(uint32_t id, int code, char* data, char* format, ...)
268{
269 sharkd_json_response_open(id);
270 sharkd_json_object_open("error");
271 sharkd_json_value_anyf("code", "%d", code);
272
273 if (format)
274 {
275 // format the text message
276 va_list args;
277
278 va_start(args, format)__builtin_va_start(args, format);
279 char *error_msg = ws_strdup_vprintf(format, args)wmem_strdup_vprintf(((void*)0), format, args);
280 va_end(args)__builtin_va_end(args);
281
282 sharkd_json_value_string("message", error_msg);
283
284 g_free(error_msg);
285 }
286
287 sharkd_json_object_close();
288
289 if (data)
290 sharkd_json_value_string("data", data);
291
292 sharkd_json_response_close();
293}
294
295static bool_Bool
296is_param_match(const char *param_in, const char *valid_param)
297{
298 char* ptr;
299
300 if ((ptr = g_strrstr(valid_param, "*")))
301 {
302 size_t prefix_len = ptr - valid_param;
303 return !strncmp(param_in, valid_param, prefix_len);
304 }
305 else
306 return !strcmp(param_in, valid_param);
307}
308
309/*
310 * json_prep does four things:
311 *
312 * 1. check the syntax of the root and parameter members
313 * 2. tokenize the names and values by zero terminating them
314 * 3. unescape the names and values
315 * 4. extracts and saves the rpcid
316 * - we have to do it here as it's needed for the error messages
317 *
318 * The objective is to minimise the validation work in the functions
319 * that process each called method.
320 *
321 * This gets a little messy as the JSON parser creates a flat list
322 * of all members rather than create a tree.
323 */
324static bool_Bool
325json_prep(char* buf, const jsmntok_t* tokens, int count)
326{
327 int i;
328 const char* method = NULL((void*)0);
329 char* attr_name = NULL((void*)0);
330 char* attr_value = NULL((void*)0);
331
332#define SHARKD_JSON_ANY0 0
333#define SHARKD_JSON_STRING1 1
334#define SHARKD_JSON_INTEGER2 2
335#define SHARKD_JSON_UINTEGER3 3
336#define SHARKD_JSON_FLOAT4 4
337#define SHARKD_JSON_OBJECT5 5
338#define SHARKD_JSON_ARRAY6 6
339#define SHARKD_JSON_BOOLEAN7 7
340#define SHARKD_ARRAY_END99 99
341
342 struct member_attribute {
343 const char* parent_ctx;
344 const char* name;
345 int level;
346 jsmntype_t type;
347 int value_type;
348 bool_Bool is_mandatory;
349 };
350
351#define SHARKD_MANDATORY1 true1
352#define SHARKD_OPTIONAL0 false0
353
354 /*
355 * The member attribute structure is key to the syntax checking. The
356 * array contains all of the root level (1) member names, the data
357 * types permissible for the value and a boolean that indicates whether
358 * or not the member is mandatory.
359 *
360 * Once we get into the next layer (2) of the json tree, we need to check
361 * params member names and data types dependent in the context of the method
362 * (parent_ctx).
363 */
364
365 struct member_attribute name_array[] = {
366 // Root members
367 {NULL((void*)0), "jsonrpc", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
368 {NULL((void*)0), "userid", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
369 {NULL((void*)0), "id", 1, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
370 {NULL((void*)0), "method", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
371 {NULL((void*)0), "params", 1, JSMN_OBJECT, SHARKD_JSON_OBJECT5, SHARKD_OPTIONAL0},
372
373 // Valid methods
374 {"method", "analyse", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
375 {"method", "bye", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
376 {"method", "check", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
377 {"method", "complete", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
378 {"method", "download", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
379 {"method", "dumpconf", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
380 {"method", "follow", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
381 {"method", "frame", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
382 {"method", "frames", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
383 {"method", "info", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
384 {"method", "intervals", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
385 {"method", "iograph", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
386 {"method", "load", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
387 {"method", "setcomment", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
388 {"method", "setconf", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
389 {"method", "status", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
390 {"method", "tap", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
391
392 // Parameters and their method context
393 {"check", "field", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
394 {"check", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
395 {"complete", "field", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
396 {"complete", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
397 {"download", "token", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
398 {"dumpconf", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
399 {"follow", "follow", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
400 {"follow", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
401 {"follow", "sub_stream", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
402 {"frame", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
403 {"frame", "proto", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
404 {"frame", "ref_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
405 {"frame", "prev_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
406 {"frame", "columns", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
407 {"frame", "color", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
408 {"frame", "bytes", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
409 {"frame", "hidden", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
410 {"frames", "column*", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY0, SHARKD_OPTIONAL0},
411 {"frames", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
412 {"frames", "skip", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
413 {"frames", "limit", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
414 {"frames", "refs", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
415 {"intervals", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
416 {"intervals", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
417 {"iograph", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
418 {"iograph", "interval_units", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
419 {"iograph", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
420 {"iograph", "graph0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
421 {"iograph", "graph1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
422 {"iograph", "graph2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
423 {"iograph", "graph3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
424 {"iograph", "graph4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
425 {"iograph", "graph5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
426 {"iograph", "graph6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
427 {"iograph", "graph7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
428 {"iograph", "graph8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
429 {"iograph", "graph9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
430 {"iograph", "filter0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
431 {"iograph", "filter1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
432 {"iograph", "filter2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
433 {"iograph", "filter3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
434 {"iograph", "filter4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
435 {"iograph", "filter5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
436 {"iograph", "filter6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
437 {"iograph", "filter7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
438 {"iograph", "filter8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
439 {"iograph", "filter9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
440 {"iograph", "aot0", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
441 {"iograph", "aot1", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
442 {"iograph", "aot2", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
443 {"iograph", "aot3", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
444 {"iograph", "aot4", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
445 {"iograph", "aot5", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
446 {"iograph", "aot6", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
447 {"iograph", "aot7", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
448 {"iograph", "aot8", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
449 {"iograph", "aot9", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
450 {"load", "file", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
451 {"setcomment", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
452 {"setcomment", "comment", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
453 {"setconf", "name", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
454 {"setconf", "value", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY0, SHARKD_MANDATORY1},
455 {"tap", "tap0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
456 {"tap", "tap1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
457 {"tap", "tap2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
458 {"tap", "tap3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
459 {"tap", "tap4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
460 {"tap", "tap5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
461 {"tap", "tap6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
462 {"tap", "tap7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
463 {"tap", "tap8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
464 {"tap", "tap9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
465 {"tap", "tap10", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
466 {"tap", "tap11", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
467 {"tap", "tap12", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
468 {"tap", "tap13", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
469 {"tap", "tap14", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
470 {"tap", "tap15", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
471 {"tap", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
472
473 // End of the name_array
474 {NULL((void*)0), NULL((void*)0), 0, JSMN_STRING, SHARKD_ARRAY_END99, SHARKD_OPTIONAL0},
475 };
476
477 rpcid = 0;
478
479 /* sanity check, and split strings */
480 if (count < 1 || tokens[0].type != JSMN_OBJECT)
481 {
482 sharkd_json_error(
483 rpcid, -32600, NULL((void*)0),
484 "The request must an object"
485 );
486 return false0;
487 }
488
489 /* don't need [0] token */
490 tokens++;
491 count--;
492
493 if (count & 1)
494 {
495 sharkd_json_error(
496 rpcid, -32600, NULL((void*)0),
497 "The request must contain name/value pairs"
498 );
499 return false0;
500 }
501
502 for (i = 0; i < count; i += 2)
503 {
504 buf[tokens[i + 0].end] = '\0';
505 buf[tokens[i + 1].end] = '\0';
506 }
507
508 // we must get the id as soon as possible so that it's available in all future error messages
509 attr_value = (char*)json_find_attr(buf, tokens, count, "id");
510 if (attr_value)
511 {
512 if (!ws_strtou32(attr_value, NULL((void*)0), &rpcid))
513 {
514 sharkd_json_error(
515 rpcid, -32600, NULL((void*)0),
516 "The id value must be a positive integer"
517 );
518 return false0;
519 }
520 }
521
522 method = json_find_attr(buf, tokens, count, "method");
523
524 if (method)
525 {
526 bool_Bool is_supported = false0;
527 i = 0; // name array index
528
529 // check that the request method is good
530 while (name_array[i].value_type != SHARKD_ARRAY_END99)
531 {
532 if (name_array[i].parent_ctx)
533 {
534 if (!strcmp(method, name_array[i].name) && !strcmp(name_array[i].parent_ctx, "method"))
535 is_supported = true1; // the method is valid
536 }
537
538 i++;
539 }
540
541 if (!is_supported)
542 {
543 sharkd_json_error(
544 rpcid, -32601, NULL((void*)0),
545 "The method %s is not supported", method
546 );
547 return false0;
548 }
549 }
550
551 for (i = 0; i < count; i += 2)
552 {
553 if (tokens[i].type != JSMN_STRING)
554 {
555 sharkd_json_error(
556 rpcid, -32600, NULL((void*)0),
557 "Member names must be a string - member %d is not string", (i / 2) + 1
558 );
559 return false0;
560 }
561
562 attr_name = &buf[tokens[i + 0].start];
563 attr_value = &buf[tokens[i + 1].start];
564
565 if (!strcmp(attr_name, "jsonrpc"))
566 {
567 if (strcmp(&buf[tokens[i + 1].start], "2.0"))
568 {
569 sharkd_json_error(
570 rpcid, -32600, NULL((void*)0),
571 "Only JSON %s is supported", "2.0"
572 );
573 return false0;
574 }
575 }
576
577 /* unescape only value, as keys are simple strings */
578 if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(attr_value))
579 {
580 sharkd_json_error(
581 rpcid, -32600, NULL((void*)0),
582 "Cannot unescape the value string of member %d", (i / 2) + 1
583 );
584 return false0;
585 }
586
587 /* Confirm that the member is valid */
588 bool_Bool match = false0;
589
590 // We need to check root members (level 1) and parameters (level 2), hence the for loop.
591
592 for (int level = 1; level < 3; level++)
593 {
594 size_t j = 0;
595
596 while (name_array[j].value_type != SHARKD_ARRAY_END99) // iterate through the array until we hit the end
597 {
598 if (is_param_match(attr_name, name_array[j].name) && name_array[j].level == level)
599 {
600 // We need to be sure the match is in the correct context
601 // i.e. is this a match for a root member (level 1) or for a parameter (level 2).
602
603 if (level == 1)
604 {
605 // need to guard against a parameter name matching a method name
606 if (method)
607 {
608 if (name_array[j].parent_ctx)
609 {
610 j++;
611 continue;
612 }
613
614 if (!strcmp(method, &buf[tokens[i + 0].start]))
615 {
616 j++;
617 continue;
618 }
619 }
620
621 match = true1;
622 }
623 else if (method)
624 {
625 if (level == 2 && !strcmp(name_array[j].parent_ctx, method))
626 match = true1;
627 else
628 {
629 j++;
630 continue;
631 }
632 }
633 else
634 {
635 j++;
636 continue;
637 }
638
639 // The match looks good, let's now check the data types
640
641 if (tokens[i + 1].type != name_array[j].type && name_array[j].type != SHARKD_JSON_ANY0)
642 {
643 sharkd_json_error(
644 rpcid, -32600, NULL((void*)0),
645 "The data type for member %s is not valid", attr_name
646 );
647 return false0;
648 }
649 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_UINTEGER3)
650 {
651 uint32_t temp;
652 if (!ws_strtou32(attr_value, NULL((void*)0), &temp) || temp <= 0)
653 {
654 sharkd_json_error(
655 rpcid, -32600, NULL((void*)0),
656 "The value for %s must be a positive integer", name_array[j].name
657 );
658 return false0;
659 }
660 }
661 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_BOOLEAN7)
662 {
663 if (strcmp(attr_value, "true") && strcmp(attr_value, "false"))
664 {
665 sharkd_json_error(
666 rpcid, -32600, NULL((void*)0),
667 "The value for %s must be a boolean (true or false)", name_array[j].name
668 );
669 return false0;
670 }
671
672 }
673 break; // looks like a valid match
674 }
675 j++;
676 }
677 }
678
679 if (!match)
680 {
681 sharkd_json_error(
682 rpcid, -32600, NULL((void*)0),
683 "%s is not a valid member name", attr_name
684 );
685 return false0;
686 }
687 }
688
689 /* check for mandatory members */
690 size_t j = 0;
691
692 while (name_array[j].value_type != SHARKD_ARRAY_END99)
693 {
694 if (name_array[j].is_mandatory && name_array[j].level == 1)
695 {
696 if (!json_find_attr(buf, tokens, count, name_array[j].name))
697 {
698 sharkd_json_error(
699 rpcid, -32600, NULL((void*)0),
700 "Mandatory member %s is missing", name_array[j].name
701 );
702 return false0;
703 }
704 }
705 j++;
706 }
707
708 // check that the current request contains the mandatory parameters
709 j = 0;
710
711 while (name_array[j].value_type != SHARKD_ARRAY_END99)
712 {
713 if (name_array[j].is_mandatory && name_array[j].level == 2 && !strcmp(method, name_array[j].parent_ctx))
714 {
715 if (!json_find_attr(buf, tokens, count, name_array[j].name))
716 {
717 sharkd_json_error(
718 rpcid, -32600, NULL((void*)0),
719 "Mandatory parameter %s is missing", name_array[j].name
720 );
721 return false0;
722 }
723 }
724 j++;
725 }
726
727
728 // check that the parameters for the current request are valid for the method and that the data type for the value is valid
729
730 return true1;
731}
732
733static void
734sharkd_session_filter_free(void *data)
735{
736 struct sharkd_filter_item *l = (struct sharkd_filter_item *) data;
737
738 g_free(l->filtered);
739 g_free(l);
740}
741
742static const struct sharkd_filter_item *
743sharkd_session_filter_data(const char *filter)
744{
745 struct sharkd_filter_item *l;
746
747 l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter);
748 if (!l)
749 {
750 uint8_t *filtered = NULL((void*)0);
751
752 int ret = sharkd_filter(filter, &filtered);
753
754 if (ret == -1)
755 return NULL((void*)0);
756
757 l = g_new(struct sharkd_filter_item, 1)((struct sharkd_filter_item *) g_malloc_n ((1), sizeof (struct
sharkd_filter_item)))
;
758 l->filtered = filtered;
759
760 g_hash_table_insert(filter_table, g_strdup(filter)g_strdup_inline (filter), l);
761 }
762
763 return l;
764}
765
766static bool_Bool
767sharkd_rtp_match_init(rtpstream_id_t *id, const char *init_str)
768{
769 bool_Bool ret = false0;
770 char **arr;
771 uint32_t tmp_addr_src, tmp_addr_dst;
772 address tmp_src_addr, tmp_dst_addr;
773
774 memset(id, 0, sizeof(*id));
775
776 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
777 if (g_strv_length(arr) != 5)
778 goto fail;
779
780 /* TODO, for now only IPv4 */
781 if (!get_host_ipaddr(arr[0], &tmp_addr_src))
782 goto fail;
783
784 if (!ws_strtou16(arr[1], NULL((void*)0), &id->src_port))
785 goto fail;
786
787 if (!get_host_ipaddr(arr[2], &tmp_addr_dst))
788 goto fail;
789
790 if (!ws_strtou16(arr[3], NULL((void*)0), &id->dst_port))
791 goto fail;
792
793 if (!ws_hexstrtou32(arr[4], NULL((void*)0), &id->ssrc))
794 goto fail;
795
796 set_address(&tmp_src_addr, AT_IPv4, 4, &tmp_addr_src);
797 copy_address(&id->src_addr, &tmp_src_addr);
798 set_address(&tmp_dst_addr, AT_IPv4, 4, &tmp_addr_dst);
799 copy_address(&id->dst_addr, &tmp_dst_addr);
800
801 ret = true1;
802
803fail:
804 g_strfreev(arr);
805 return ret;
806}
807
808static bool_Bool
809sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata _U___attribute__((unused)))
810{
811 stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
812
813 json_dumper_begin_object(&dumper);
814 sharkd_json_value_string("name", stat_tap->title);
815 sharkd_json_value_stringf("tap", "nstat:%s", (const char *) key);
816 json_dumper_end_object(&dumper);
817
818 return false0;
819}
820
821static bool_Bool
822sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata _U___attribute__((unused)))
823{
824 struct register_ct *table = (struct register_ct *) value;
825
826 const char *label = (const char *) key;
827
828 if (get_conversation_packet_func(table))
829 {
830 json_dumper_begin_object(&dumper);
831 sharkd_json_value_stringf("name", "Conversation List/%s", label);
832 sharkd_json_value_stringf("tap", "conv:%s", label);
833 json_dumper_end_object(&dumper);
834 }
835
836 if (get_endpoint_packet_func(table))
837 {
838 json_dumper_begin_object(&dumper);
839 sharkd_json_value_stringf("name", "Endpoint/%s", label);
840 sharkd_json_value_stringf("tap", "endpt:%s", label);
841 json_dumper_end_object(&dumper);
842 }
843 return false0;
844}
845
846static bool_Bool
847sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata _U___attribute__((unused)))
848{
849 register_analysis_t *analysis = (register_analysis_t *) value;
850
851 json_dumper_begin_object(&dumper);
852 sharkd_json_value_string("name", sequence_analysis_get_ui_name(analysis));
853 sharkd_json_value_stringf("tap", "seqa:%s", (const char *) key);
854 json_dumper_end_object(&dumper);
855
856 return false0;
857}
858
859static bool_Bool
860sharkd_export_object_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
861{
862 register_eo_t *eo = (register_eo_t *) value;
863
864 const int proto_id = get_eo_proto_id(eo);
865 const char *filter = proto_get_protocol_filter_name(proto_id);
866 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
867
868 json_dumper_begin_object(&dumper);
869 sharkd_json_value_stringf("name", "Export Object/%s", label);
870 sharkd_json_value_stringf("tap", "eo:%s", filter);
871 json_dumper_end_object(&dumper);
872
873 return false0;
874}
875
876static bool_Bool
877sharkd_srt_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
878{
879 register_srt_t *srt = (register_srt_t *) value;
880
881 const int proto_id = get_srt_proto_id(srt);
882 const char *filter = proto_get_protocol_filter_name(proto_id);
883 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
884
885 json_dumper_begin_object(&dumper);
886 sharkd_json_value_stringf("name", "Service Response Time/%s", label);
887 sharkd_json_value_stringf("tap", "srt:%s", filter);
888 json_dumper_end_object(&dumper);
889
890 return false0;
891}
892
893static bool_Bool
894sharkd_rtd_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
895{
896 register_rtd_t *rtd = (register_rtd_t *) value;
897
898 const int proto_id = get_rtd_proto_id(rtd);
899 const char *filter = proto_get_protocol_filter_name(proto_id);
900 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
901
902 json_dumper_begin_object(&dumper);
903 sharkd_json_value_stringf("name", "Response Time Delay/%s", label);
904 sharkd_json_value_stringf("tap", "rtd:%s", filter);
905 json_dumper_end_object(&dumper);
906
907 return false0;
908}
909
910static bool_Bool
911sharkd_follower_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
912{
913 register_follow_t *follower = (register_follow_t *) value;
914
915 const int proto_id = get_follow_proto_id(follower);
916 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
917 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
918
919 json_dumper_begin_object(&dumper);
920 sharkd_json_value_stringf("name", "Follow/%s", label);
921 sharkd_json_value_stringf("tap", "follow:%s", filter);
922 json_dumper_end_object(&dumper);
923
924 return false0;
925}
926
927static void
928sharkd_session_print_capture_types(void)
929{
930 unsigned i;
931 GArray *writable_type_subtypes;
932 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
933 for (i = 0; i < writable_type_subtypes->len; i++) {
934 int ft = g_array_index(writable_type_subtypes, int, i)(((int*) (void *) (writable_type_subtypes)->data) [(i)]);
935 sharkd_json_object_open(NULL((void*)0));
936 sharkd_json_value_string("name", wtap_file_type_subtype_name(ft));
937 sharkd_json_value_string("description", wtap_file_type_subtype_description(ft));
938 sharkd_json_object_close();
939 }
940 g_array_free(writable_type_subtypes, TRUE(!(0)));
941}
942
943struct encap_type_info
944{
945 const char *name;
946 const char *description;
947};
948
949static int
950encap_type_info_nat_compare(const void *a, const void *b)
951{
952 return ws_ascii_strnatcmp(((const struct encap_type_info *)a)->name,
953 ((const struct encap_type_info *)b)->name);
954}
955
956static void
957encap_type_info_visit(void *data, void *user_data _U___attribute__((unused)))
958{
959 sharkd_json_object_open(NULL((void*)0));
960 sharkd_json_value_string("name", ((struct encap_type_info *)data)->name);
961 sharkd_json_value_string("description", ((struct encap_type_info *)data)->description);
962 sharkd_json_object_close();
963}
964
965static void
966sharkd_session_print_encap_types(void)
967{
968 int i;
969 struct encap_type_info *encaps;
970 GSList *list = NULL((void*)0);
971 encaps = g_new(struct encap_type_info, WTAP_NUM_ENCAP_TYPES)((struct encap_type_info *) g_malloc_n ((wtap_get_num_encap_types
()), sizeof (struct encap_type_info)))
;
972 for (i = 0; i < WTAP_NUM_ENCAP_TYPESwtap_get_num_encap_types(); i++) {
973 encaps[i].name = wtap_encap_name(i);
974 if (encaps[i].name != NULL((void*)0)) {
975 encaps[i].description = wtap_encap_description(i);
976 list = g_slist_insert_sorted(list, &encaps[i], encap_type_info_nat_compare);
977 }
978 }
979 g_slist_foreach(list, encap_type_info_visit, NULL((void*)0));
980 g_slist_free(list);
981 g_free(encaps);
982}
983
984/**
985 * sharkd_session_process_info()
986 *
987 * Process info request
988 *
989 * Output object with attributes:
990 * (m) version - version number
991 *
992 * (m) columns - available column formats, array of object with attributes:
993 * 'name' - column name
994 * 'format' - column format-name
995 *
996 * (m) stats - available statistics, array of object with attributes:
997 * 'name' - statistic name
998 * 'tap' - sharkd tap-name for statistic
999 *
1000 * (m) convs - available conversation list, array of object with attributes:
1001 * 'name' - conversation name
1002 * 'tap' - sharkd tap-name for conversation
1003 *
1004 * (m) eo - available export object list, array of object with attributes:
1005 * 'name' - export object name
1006 * 'tap' - sharkd tap-name for eo
1007 *
1008 * (m) srt - available service response time list, array of object with attributes:
1009 * 'name' - service response time name
1010 * 'tap' - sharkd tap-name for srt
1011 *
1012 * (m) rtd - available response time delay list, array of object with attributes:
1013 * 'name' - response time delay name
1014 * 'tap' - sharkd tap-name for rtd
1015 *
1016 * (m) seqa - available sequence analysis (flow) list, array of object with attributes:
1017 * 'name' - sequence analysis name
1018 * 'tap' - sharkd tap-name
1019 *
1020 * (m) taps - available taps, array of object with attributes:
1021 * 'name' - tap name
1022 * 'tap' - sharkd tap-name
1023 *
1024 * (m) follow - available followers, array of object with attributes:
1025 * 'name' - tap name
1026 * 'tap' - sharkd tap-name
1027 *
1028 * (m) ftypes - conversation table for FT_ number to string, array of FT_xxx strings.
1029 *
1030 * (m) nstat - available table-based taps, array of object with attributes:
1031 * 'name' - tap name
1032 * 'tap' - sharkd tap-name
1033 *
1034 * (m) capture_types - available capture types, array of object with attributes:
1035 * 'name' - capture type name
1036 * 'description' - capture type description
1037 *
1038 * (m) encap_types - available encapsulation types, array of object with attributes:
1039 * 'name' - encapsulation type name
1040 * 'description' - encapsulation type description
1041 */
1042static void
1043sharkd_session_process_info(void)
1044{
1045 int i;
1046
1047 sharkd_json_result_prologue(rpcid);
1048
1049 sharkd_json_array_open("columns");
1050 for (i = 0; i < NUM_COL_FMTS; i++)
1051 {
1052 const char *col_format = col_format_to_string(i);
1053 const char *col_descr = col_format_desc(i);
1054
1055 json_dumper_begin_object(&dumper);
1056 sharkd_json_value_string("name", col_descr);
1057 sharkd_json_value_string("format", col_format);
1058 json_dumper_end_object(&dumper);
1059 }
1060 sharkd_json_array_close();
1061
1062 sharkd_json_array_open("stats");
1063 {
1064 GList *cfg_list = stats_tree_get_cfg_list();
1065 GList *l;
1066
1067 for (l = cfg_list; l; l = l->next)
1068 {
1069 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
1070
1071 json_dumper_begin_object(&dumper);
1072 sharkd_json_value_string("name", cfg->title);
1073 sharkd_json_value_stringf("tap", "stat:%s", cfg->abbr);
1074 json_dumper_end_object(&dumper);
1075 }
1076
1077 g_list_free(cfg_list);
1078 }
1079 sharkd_json_array_close();
1080
1081 sharkd_json_array_open("ftypes");
1082 for (i = 0; i < FT_NUM_TYPES; i++)
1083 sharkd_json_value_string(NULL((void*)0), ftype_name((ftenum_t) i));
1084 sharkd_json_array_close();
1085
1086 sharkd_json_array_open("capture_types");
1087 sharkd_session_print_capture_types();
1088 sharkd_json_array_close();
1089
1090 sharkd_json_array_open("encap_types");
1091 sharkd_session_print_encap_types();
1092 sharkd_json_array_close();
1093
1094 sharkd_json_value_string("version", get_ws_vcs_version_info_short());
1095
1096 sharkd_json_array_open("nstat");
1097 i = 0;
1098 stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
1099 sharkd_json_array_close();
1100
1101 sharkd_json_array_open("convs");
1102 i = 0;
1103 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
1104 sharkd_json_array_close();
1105
1106 sharkd_json_array_open("seqa");
1107 i = 0;
1108 sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
1109 sharkd_json_array_close();
1110
1111 sharkd_json_array_open("taps");
1112 {
1113 json_dumper_begin_object(&dumper);
1114 sharkd_json_value_string("name", "UDP Multicast Streams");
1115 sharkd_json_value_string("tap", "multicast");
1116 json_dumper_end_object(&dumper);
1117
1118 json_dumper_begin_object(&dumper);
1119 sharkd_json_value_string("name", "RTP streams");
1120 sharkd_json_value_string("tap", "rtp-streams");
1121 json_dumper_end_object(&dumper);
1122
1123 json_dumper_begin_object(&dumper);
1124 sharkd_json_value_string("name", "Protocol Hierarchy Statistics");
1125 sharkd_json_value_string("tap", "phs");
1126 json_dumper_end_object(&dumper);
1127
1128 json_dumper_begin_object(&dumper);
1129 sharkd_json_value_string("name", "VoIP Calls");
1130 sharkd_json_value_string("tap", "voip-calls");
1131 json_dumper_end_object(&dumper);
1132
1133 json_dumper_begin_object(&dumper);
1134 sharkd_json_value_string("name", "VoIP Conversations");
1135 sharkd_json_value_string("tap", "voip-convs");
1136 json_dumper_end_object(&dumper);
1137
1138 json_dumper_begin_object(&dumper);
1139 sharkd_json_value_string("name", "Expert Information");
1140 sharkd_json_value_string("tap", "expert");
1141 json_dumper_end_object(&dumper);
1142 }
1143 sharkd_json_array_close();
1144
1145 sharkd_json_array_open("eo");
1146 i = 0;
1147 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
1148 sharkd_json_array_close();
1149
1150 sharkd_json_array_open("srt");
1151 i = 0;
1152 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
1153 sharkd_json_array_close();
1154
1155 sharkd_json_array_open("rtd");
1156 i = 0;
1157 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
1158 sharkd_json_array_close();
1159
1160 sharkd_json_array_open("follow");
1161 i = 0;
1162 follow_iterate_followers(sharkd_follower_visit_cb, &i);
1163 sharkd_json_array_close();
1164
1165 sharkd_json_result_epilogue();
1166}
1167
1168/**
1169 * sharkd_session_process_load()
1170 *
1171 * Process load request
1172 *
1173 * Input:
1174 * (m) file - file to be loaded
1175 *
1176 * Output object with attributes:
1177 * (m) err - error code
1178 */
1179static void
1180sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
1181{
1182 const char *tok_file = json_find_attr(buf, tokens, count, "file");
1183 int err = 0;
1184
1185 if (!tok_file)
1186 return;
1187
1188 fprintf(stderrstderr, "load: filename=%s\n", tok_file);
1189
1190 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO0, false0, &err) != CF_OK)
1191 {
1192 sharkd_json_error(
1193 rpcid, -2001, NULL((void*)0),
1194 "Unable to open the file"
1195 );
1196 return;
1197 }
1198
1199 TRY{ except_t *volatile exc; volatile int except_state = 0; static
const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode
except_sn; struct except_catch except_ch; except_setup_try(&
except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch
.except_jmp)) *(&exc) = &except_ch.except_obj; else *
(&exc) = 0; if(except_state & 1) except_state |= 2; except_state
&= ~1; if (except_state == 0 && exc == 0)
1200 {
1201 err = sharkd_load_cap_file();
1202 }
1203 CATCH(OutOfMemoryError)if (except_state == 0 && exc != 0 && exc->
except_id.except_code == (8) && (except_state |= 1))
1204 {
1205 sharkd_json_error(
1206 rpcid, -32603, NULL((void*)0),
1207 "Load failed, out of memory"
1208 );
1209 fprintf(stderrstderr, "load: OutOfMemoryError\n");
1210 err = ENOMEM12;
1211 }
1212 ENDTRYif(!(except_state&1) && exc != 0) except_rethrow(
exc); except_free(except_ch.except_obj.except_dyndata); except_pop
(); };}
;
1213
1214 if (err == 0)
1215 {
1216 sharkd_json_simple_ok(rpcid);
1217 }
1218 else
1219 {
1220 sharkd_json_result_prologue(rpcid);
1221 sharkd_json_value_string("status", wtap_strerror(err));
1222 sharkd_json_value_anyf("err", "%d", err);
1223 sharkd_json_result_epilogue();
1224 }
1225
1226}
1227
1228/**
1229 * sharkd_session_process_status()
1230 *
1231 * Process status request
1232 *
1233 * Output object with attributes:
1234 * (m) frames - count of currently loaded frames
1235 * (m) duration - time difference between time of first frame, and last loaded frame
1236 * (o) filename - capture filename
1237 * (o) filesize - capture filesize
1238 * (o) columns - array of column titles
1239 * (o) column_info - array of column infos, array of object with attributes:
1240 * 'title' - column title
1241 * 'format' - column format (%x or %Cus:<expr>:<occurrence> if COL_CUSTOM)
1242 * 'visible' - true if column is visible
1243 * 'display' - column display format; 'U', 'R' or 'D'
1244 */
1245static void
1246sharkd_session_process_status(void)
1247{
1248 sharkd_json_result_prologue(rpcid);
1249
1250 sharkd_json_value_anyf("frames", "%u", cfile.count);
1251 sharkd_json_value_anyf("duration", "%.9f", nstime_to_sec(&cfile.elapsed_time));
1252
1253 if (cfile.filename)
1254 {
1255 char *name = g_path_get_basename(cfile.filename);
1256
1257 sharkd_json_value_string("filename", name);
1258 g_free(name);
1259 }
1260
1261 if (cfile.provider.wth)
1262 {
1263 int64_t file_size = wtap_file_size(cfile.provider.wth, NULL((void*)0));
1264
1265 if (file_size > 0)
1266 sharkd_json_value_anyf("filesize", "%" PRId64"l" "d", file_size);
1267 }
1268
1269 if (cfile.cinfo.num_cols > 0)
1270 {
1271 sharkd_json_array_open("columns");
1272 for (int i = 0; i < cfile.cinfo.num_cols; ++i)
1273 {
1274 sharkd_json_value_string(NULL((void*)0), get_column_title(i));
1275 }
1276 sharkd_json_array_close();
1277
1278 sharkd_json_array_open("column_info");
1279 for (int i = 0; i < cfile.cinfo.num_cols; ++i)
1280 {
1281 int fmt = get_column_format(i);
1282 sharkd_json_object_open(NULL((void*)0));
1283 sharkd_json_value_string("title", get_column_title(i));
1284 if (fmt != COL_CUSTOM)
1285 {
1286 sharkd_json_value_string("format", col_format_to_string(fmt));
1287 } else {
1288 sharkd_json_value_stringf("format", "%s:%s:%d", col_format_to_string(fmt), get_column_custom_fields(i), get_column_custom_occurrence(i));
1289 }
1290 sharkd_json_value_anyf("visible", get_column_visible(i) ? "true" : "false");
1291 sharkd_json_value_stringf("display", "%c", get_column_display_format(i));
1292 sharkd_json_object_close();
1293 }
1294 sharkd_json_array_close();
1295 }
1296
1297 sharkd_json_result_epilogue();
1298}
1299
1300struct sharkd_analyse_data
1301{
1302 GHashTable *protocols_set;
1303 nstime_t *first_time;
1304 nstime_t *last_time;
1305};
1306
1307static void
1308sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree _U___attribute__((unused)),
1309 struct epan_column_info *cinfo _U___attribute__((unused)), const GSList *data_src _U___attribute__((unused)), void *data)
1310{
1311 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
1312 packet_info *pi = &edt->pi;
1313 frame_data *fdata = pi->fd;
1314
1315 if (analyser->first_time == NULL((void*)0) || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
1316 analyser->first_time = &fdata->abs_ts;
1317
1318 if (analyser->last_time == NULL((void*)0) || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
1319 analyser->last_time = &fdata->abs_ts;
1320
1321 if (pi->layers)
1322 {
1323 wmem_list_frame_t *frame;
1324
1325 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
1326 {
1327 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame))((guint) (gulong) (wmem_list_frame_data(frame)));
1328
1329 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)), NULL((void*)0), NULL((void*)0)))
1330 {
1331 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)), GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)));
1332 sharkd_json_value_string(NULL((void*)0), proto_get_protocol_filter_name(proto_id));
1333 }
1334 }
1335 }
1336
1337}
1338
1339/**
1340 * sharkd_session_process_status()
1341 *
1342 * Process analyse request
1343 *
1344 * Output object with attributes:
1345 * (m) frames - count of currently loaded frames
1346 * (m) protocols - protocol list
1347 * (m) first - earliest frame time
1348 * (m) last - latest frame time
1349 */
1350static void
1351sharkd_session_process_analyse(void)
1352{
1353 struct sharkd_analyse_data analyser;
1354 wtap_rec rec; /* Record information */
1355
1356 analyser.first_time = NULL((void*)0);
1357 analyser.last_time = NULL((void*)0);
1358 analyser.protocols_set = g_hash_table_new(NULL((void*)0) /* g_direct_hash() */, NULL((void*)0) /* g_direct_equal */);
1359
1360 sharkd_json_result_prologue(rpcid);
1361
1362 sharkd_json_value_anyf("frames", "%u", cfile.count);
1363
1364 sharkd_json_array_open("protocols");
1365
1366 wtap_rec_init(&rec, 1514);
1367
1368 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1369 {
1370 enum dissect_request_status status;
1371 int err;
1372 char *err_info;
1373
1374 status = sharkd_dissect_request(framenum,
1375 (framenum != 1) ? 1 : 0, framenum - 1,
1376 &rec, NULL((void*)0), SHARKD_DISSECT_FLAG_NULL0x00u,
1377 &sharkd_session_process_analyse_cb, &analyser,
1378 &err, &err_info);
1379 switch (status) {
1380
1381 case DISSECT_REQUEST_SUCCESS:
1382 break;
1383
1384 case DISSECT_REQUEST_NO_SUCH_FRAME:
1385 /* XXX - report the error. */
1386 break;
1387
1388 case DISSECT_REQUEST_READ_ERROR:
1389 /*
1390 * Free up the error string.
1391 * XXX - report the error.
1392 */
1393 g_free(err_info);
1394 break;
1395 }
1396 }
1397
1398 sharkd_json_array_close();
1399
1400 if (analyser.first_time)
1401 sharkd_json_value_anyf("first", "%.9f", nstime_to_sec(analyser.first_time));
1402
1403 if (analyser.last_time)
1404 sharkd_json_value_anyf("last", "%.9f", nstime_to_sec(analyser.last_time));
1405
1406 sharkd_json_result_epilogue();
1407
1408 wtap_rec_cleanup(&rec);
1409
1410 g_hash_table_destroy(analyser.protocols_set);
1411}
1412
1413static column_info *
1414sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
1415{
1416 const char *columns_custom[32];
1417 uint16_t columns_fmt[32];
1418 int16_t columns_occur[32];
1419
1420 int i, cols;
1421
1422 for (i = 0; i < 32; i++)
1423 {
1424 const char *tok_column;
1425 char tok_column_name[64];
1426 char *custom_sepa;
1427
1428 snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
1429 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
1430 if (tok_column == NULL((void*)0))
1431 break;
1432
1433 columns_custom[i] = NULL((void*)0);
1434 columns_occur[i] = 0;
1435
1436 if ((custom_sepa = strchr(tok_column, ':')))
1437 {
1438 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
1439
1440 columns_fmt[i] = COL_CUSTOM;
1441 columns_custom[i] = tok_column;
1442
1443 if (!ws_strtoi16(custom_sepa + 1, NULL((void*)0), &columns_occur[i]))
1444 return NULL((void*)0);
1445 }
1446 else
1447 {
1448 if (!ws_strtou16(tok_column, NULL((void*)0), &columns_fmt[i]))
1449 return NULL((void*)0);
1450
1451 if (columns_fmt[i] >= NUM_COL_FMTS)
1452 return NULL((void*)0);
1453
1454 /* if custom, that it shouldn't be just custom number -> error */
1455 if (columns_fmt[i] == COL_CUSTOM)
1456 return NULL((void*)0);
1457 }
1458 }
1459
1460 cols = i;
1461
1462 col_setup(cinfo, cols);
1463
1464 for (i = 0; i < cols; i++)
1465 {
1466 col_item_t *col_item = &cinfo->columns[i];
1467
1468 col_item->col_fmt = columns_fmt[i];
1469 col_item->col_title = NULL((void*)0); /* no need for title */
1470
1471 if (col_item->col_fmt == COL_CUSTOM)
1472 {
1473 col_item->col_custom_fields = g_strdup(columns_custom[i])g_strdup_inline (columns_custom[i]);
1474 col_item->col_custom_occurrence = columns_occur[i];
1475 }
1476
1477 col_item->col_fence = 0;
1478 }
1479
1480 col_finalize(cinfo);
1481
1482 return cinfo;
1483}
1484
1485static void
1486sharkd_session_process_frames_cb(epan_dissect_t *edt, proto_tree *tree _U___attribute__((unused)),
1487 struct epan_column_info *cinfo, const GSList *data_src _U___attribute__((unused)), void *data _U___attribute__((unused)))
1488{
1489 packet_info *pi = &edt->pi;
1490 frame_data *fdata = pi->fd;
1491 wtap_block_t pkt_block = NULL((void*)0);
1492 unsigned int i;
1493 char *comment = NULL((void*)0);
1494
1495 json_dumper_begin_object(&dumper);
1496
1497 sharkd_json_array_open("c");
1498 for (int col = 0; col < cinfo->num_cols; ++col)
1499 {
1500 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
1501 }
1502 sharkd_json_array_close();
1503
1504 sharkd_json_value_anyf("num", "%u", pi->num);
1505
1506 /*
1507 * Get the block for this record, if it has one.
1508 */
1509 pkt_block = sharkd_get_packet_block(fdata);
1510
1511 /*
1512 * Does this record have any comments?
1513 */
1514 if (pkt_block != NULL((void*)0) &&
1515 WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, 0, &comment))
1516 {
1517 sharkd_json_value_anyf("ct", "true");
1518
1519 sharkd_json_array_open("comments");
1520 for (i = 0; wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment) == WTAP_OPTTYPE_SUCCESS; i++) {
1521 sharkd_json_value_string(NULL((void*)0), comment);
1522 }
1523 sharkd_json_array_close();
1524 }
1525
1526 if (fdata->ignored)
1527 sharkd_json_value_anyf("i", "true");
1528
1529 if (fdata->marked)
1530 sharkd_json_value_anyf("m", "true");
1531
1532 if (fdata->color_filter)
1533 {
1534 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
1535 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
1536 }
1537
1538 wtap_block_unref(pkt_block);
1539 json_dumper_end_object(&dumper);
1540}
1541
1542/**
1543 * sharkd_session_process_frames()
1544 *
1545 * Process frames request
1546 *
1547 * Input:
1548 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurrence>).
1549 * If column0 is not specified default column set will be used.
1550 * (o) filter - filter to be used
1551 * (o) skip=N - skip N frames
1552 * (o) limit=N - show only N frames
1553 * (o) refs - list (comma separated) with sorted time reference frame numbers.
1554 *
1555 * Output array of frames with attributes:
1556 * (m) c - array of column data
1557 * (m) num - frame number
1558 * (o) i - if frame is ignored
1559 * (o) m - if frame is marked
1560 * (o) ct - if frame is commented
1561 * (o) comments - array of comment strings
1562 * (o) bg - color filter - background color in hex
1563 * (o) fg - color filter - foreground color in hex
1564 */
1565static void
1566sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
1567{
1568 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1569 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
1570 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
1571 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
1572 const char *tok_refs = json_find_attr(buf, tokens, count, "refs");
1573
1574 const uint8_t *filter_data = NULL((void*)0);
1575
1576 uint32_t prev_dis_num = 0;
1577 uint32_t current_ref_frame = 0, next_ref_frame = UINT32_MAX(4294967295U);
1578 uint32_t skip;
1579 uint32_t limit;
1580
1581 wtap_rec rec; /* Record information */
1582 column_info *cinfo = &cfile.cinfo;
1583 column_info user_cinfo;
1584
1585 if (tok_column)
1586 {
1587 memset(&user_cinfo, 0, sizeof(user_cinfo));
1588 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
1589 if (!cinfo)
1590 {
1591 sharkd_json_error(
1592 rpcid, -13001, NULL((void*)0),
1593 "Column definition invalid - note column 6 requires a custom definition"
1594 );
1595 return;
1596 }
1597 }
1598
1599 if (tok_filter)
1600 {
1601 const struct sharkd_filter_item *filter_item;
1602
1603 filter_item = sharkd_session_filter_data(tok_filter);
1604 if (!filter_item)
1605 {
1606 sharkd_json_error(
1607 rpcid, -13002, NULL((void*)0),
1608 "Filter expression invalid"
1609 );
1610 return;
1611 }
1612
1613 filter_data = filter_item->filtered;
1614 }
1615
1616 skip = 0;
1617 if (tok_skip)
1618 {
1619 if (!ws_strtou32(tok_skip, NULL((void*)0), &skip))
1620 return;
1621 }
1622
1623 limit = 0;
1624 if (tok_limit)
1625 {
1626 if (!ws_strtou32(tok_limit, NULL((void*)0), &limit))
1627 return;
1628 }
1629
1630 if (tok_refs)
1631 {
1632 if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame))
1633 return;
1634 }
1635
1636 sharkd_json_result_array_prologue(rpcid);
1637
1638 wtap_rec_init(&rec, 1514);
1639
1640 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1641 {
1642 frame_data *fdata;
1643 uint32_t ref_frame = (framenum != 1) ? 1 : 0;
1644 enum dissect_request_status status;
1645 int err;
1646 char *err_info;
1647
1648 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
1649 continue;
1650
1651 if (skip)
1652 {
1653 skip--;
1654 prev_dis_num = framenum;
1655 continue;
1656 }
1657
1658 if (tok_refs)
1659 {
1660 if (framenum >= next_ref_frame)
1661 {
1662 current_ref_frame = next_ref_frame;
1663
1664 if (*tok_refs != ',')
1665 next_ref_frame = UINT32_MAX(4294967295U);
1666
1667 while (*tok_refs == ',' && framenum >= next_ref_frame)
1668 {
1669 current_ref_frame = next_ref_frame;
1670
1671 if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame))
1672 {
1673 fprintf(stderrstderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs);
1674 break;
1675 }
1676 }
1677
1678 if (*tok_refs == '\0' && framenum >= next_ref_frame)
1679 {
1680 current_ref_frame = next_ref_frame;
1681 next_ref_frame = UINT32_MAX(4294967295U);
1682 }
1683 }
1684
1685 if (current_ref_frame)
1686 ref_frame = current_ref_frame;
1687 }
1688
1689 fdata = sharkd_get_frame(framenum);
1690 status = sharkd_dissect_request(framenum,
1691 ref_frame, prev_dis_num,
1692 &rec, cinfo,
1693 (fdata->color_filter == NULL((void*)0)) ? SHARKD_DISSECT_FLAG_COLOR0x08u : SHARKD_DISSECT_FLAG_NULL0x00u,
1694 &sharkd_session_process_frames_cb, NULL((void*)0),
1695 &err, &err_info);
1696 switch (status) {
1697
1698 case DISSECT_REQUEST_SUCCESS:
1699 break;
1700
1701 case DISSECT_REQUEST_NO_SUCH_FRAME:
1702 /* XXX - report the error. */
1703 break;
1704
1705 case DISSECT_REQUEST_READ_ERROR:
1706 /*
1707 * Free up the error string.
1708 * XXX - report the error.
1709 */
1710 g_free(err_info);
1711 break;
1712 }
1713
1714 prev_dis_num = framenum;
1715
1716 if (limit && --limit == 0)
1717 break;
1718 }
1719 sharkd_json_result_array_epilogue();
1720
1721 if (cinfo != &cfile.cinfo)
1722 col_cleanup(cinfo);
1723
1724 wtap_rec_cleanup(&rec);
1725}
1726
1727static void
1728sharkd_session_process_tap_stats_node_cb(const char *key, const stat_node *n)
1729{
1730 stat_node *node;
1731
1732 sharkd_json_array_open(key);
1733 for (node = n->children; node; node = node->next)
1734 {
1735 json_dumper_begin_object(&dumper);
1736
1737 /* code based on stats_tree_get_values_from_node() */
1738 sharkd_json_value_string("name", node->name);
1739 sharkd_json_value_anyf("count", "%d", node->counter);
1740 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE0x10000000) || node->rng))
1741 {
1742 switch(node->datatype)
1743 {
1744 case STAT_DT_INT:
1745 sharkd_json_value_anyf("avg", "%.2f", ((float)node->total.int_total) / node->counter);
1746 sharkd_json_value_anyf("min", "%d", node->minvalue.int_min);
1747 sharkd_json_value_anyf("max", "%d", node->maxvalue.int_max);
1748 break;
1749 case STAT_DT_FLOAT:
1750 sharkd_json_value_anyf("avg", "%.2f", node->total.float_total / node->counter);
1751 sharkd_json_value_anyf("min", "%f", node->minvalue.float_min);
1752 sharkd_json_value_anyf("max", "%f", node->maxvalue.float_max);
1753 break;
1754 }
1755 }
1756
1757 if (node->st->elapsed)
1758 sharkd_json_value_anyf("rate", "%.4f", ((float)node->counter) / node->st->elapsed);
1759
1760 if (node->parent && node->parent->counter)
1761 sharkd_json_value_anyf("perc", "%.2f", (node->counter * 100.0) / node->parent->counter);
1762 else if (node->parent == &(node->st->root))
1763 sharkd_json_value_anyf("perc", "100");
1764
1765 if (prefs.st_enable_burstinfo && node->max_burst)
1766 {
1767 if (prefs.st_burst_showcount)
1768 sharkd_json_value_anyf("burstcount", "%d", node->max_burst);
1769 else
1770 sharkd_json_value_anyf("burstrate", "%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
1771
1772 sharkd_json_value_anyf("bursttime", "%.3f", (node->burst_time / 1000.0));
1773 }
1774
1775 if (node->children)
1776 {
1777 sharkd_session_process_tap_stats_node_cb("sub", node);
1778 }
1779 json_dumper_end_object(&dumper);
1780 }
1781 sharkd_json_array_close();
1782}
1783
1784/**
1785 * sharkd_session_process_tap_stats_cb()
1786 *
1787 * Output stats tap:
1788 *
1789 * (m) tap - tap name
1790 * (m) type:stats - tap output type
1791 * (m) name - stat name
1792 * (m) stats - array of object with attributes:
1793 * (m) name - stat item name
1794 * (m) count - stat item counter
1795 * (o) avg - stat item averange value
1796 * (o) min - stat item min value
1797 * (o) max - stat item max value
1798 * (o) rate - stat item rate value (ms)
1799 * (o) perc - stat item percentage
1800 * (o) burstrate - stat item burst rate
1801 * (o) burstcount - stat item burst count
1802 * (o) burstttme - stat item burst start
1803 * (o) sub - array of object with attributes like in stats node.
1804 */
1805static void
1806sharkd_session_process_tap_stats_cb(void *psp)
1807{
1808 stats_tree *st = (stats_tree *) psp;
1809
1810 json_dumper_begin_object(&dumper);
1811
1812 sharkd_json_value_stringf("tap", "stats:%s", st->cfg->abbr);
1813 sharkd_json_value_string("type", "stats");
1814 sharkd_json_value_string("name", st->cfg->path);
1815
1816 sharkd_session_process_tap_stats_node_cb("stats", &st->root);
1817
1818 json_dumper_end_object(&dumper);
1819}
1820
1821static void
1822sharkd_session_free_tap_stats_cb(void *psp)
1823{
1824 stats_tree *st = (stats_tree *) psp;
1825
1826 stats_tree_free(st);
1827}
1828
1829struct sharkd_expert_tap
1830{
1831 GSList *details;
1832 GStringChunk *text;
1833};
1834
1835/**
1836 * sharkd_session_process_tap_expert_cb()
1837 *
1838 * Output expert tap:
1839 *
1840 * (m) tap - tap name
1841 * (m) type:expert - tap output type
1842 * (m) details - array of object with attributes:
1843 * (m) f - frame number, which generated expert information
1844 * (o) s - severity
1845 * (o) g - group
1846 * (m) m - expert message
1847 * (o) p - protocol
1848 */
1849static void
1850sharkd_session_process_tap_expert_cb(void *tapdata)
1851{
1852 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1853 GSList *list;
1854
1855 json_dumper_begin_object(&dumper);
1856
1857 sharkd_json_value_string("tap", "expert");
1858 sharkd_json_value_string("type", "expert");
1859
1860 sharkd_json_array_open("details");
1861 for (list = etd->details; list; list = list->next)
1862 {
1863 expert_info_t *ei = (expert_info_t *) list->data;
1864 const char *tmp;
1865
1866 json_dumper_begin_object(&dumper);
1867
1868 sharkd_json_value_anyf("f", "%u", ei->packet_num);
1869
1870 tmp = try_val_to_str(ei->severity, expert_severity_vals);
1871 if (tmp)
1872 sharkd_json_value_string("s", tmp);
1873
1874 tmp = try_val_to_str(ei->group, expert_group_vals);
1875 if (tmp)
1876 sharkd_json_value_string("g", tmp);
1877
1878 sharkd_json_value_string("m", ei->summary);
1879
1880 if (ei->protocol)
1881 sharkd_json_value_string("p", ei->protocol);
1882
1883 json_dumper_end_object(&dumper);
1884 }
1885 sharkd_json_array_close();
1886
1887 json_dumper_end_object(&dumper);
1888}
1889
1890static tap_packet_status
1891sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U___attribute__((unused)), epan_dissect_t *edt _U___attribute__((unused)), const void *pointer, tap_flags_t flags _U___attribute__((unused)))
1892{
1893 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1894 const expert_info_t *ei = (const expert_info_t *) pointer;
1895 expert_info_t *ei_copy;
1896
1897 if (ei == NULL((void*)0))
1898 return TAP_PACKET_DONT_REDRAW;
1899
1900 ei_copy = g_new(expert_info_t, 1)((expert_info_t *) g_malloc_n ((1), sizeof (expert_info_t)));
1901 /* Note: this is a shallow copy */
1902 *ei_copy = *ei;
1903
1904 /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */
1905 ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
1906 ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary);
1907
1908 etd->details = g_slist_prepend(etd->details, ei_copy);
1909
1910 return TAP_PACKET_REDRAW;
1911}
1912
1913static void
1914sharkd_session_free_tap_expert_cb(void *tapdata)
1915{
1916 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1917
1918 g_slist_free_full(etd->details, g_free);
1919 g_string_chunk_free(etd->text);
1920 g_free(etd);
1921}
1922
1923/**
1924 * sharkd_session_process_tap_flow_cb()
1925 *
1926 * Output flow tap:
1927 * (m) tap - tap name
1928 * (m) type:flow - tap output type
1929 * (m) nodes - array of strings with node address
1930 * (m) flows - array of object with attributes:
1931 * (m) t - frame time string
1932 * (m) n - array of two numbers with source node index and destination node index
1933 * (m) pn - array of two numbers with source and destination port
1934 * (o) c - comment
1935 */
1936static void
1937sharkd_session_process_tap_flow_cb(void *tapdata)
1938{
1939 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1940 GList *flow_list;
1941 unsigned i;
1942
1943 sequence_analysis_get_nodes(graph_analysis);
1944
1945 json_dumper_begin_object(&dumper);
1946 sharkd_json_value_stringf("tap", "seqa:%s", graph_analysis->name);
1947 sharkd_json_value_string("type", "flow");
1948
1949 sharkd_json_array_open("nodes");
1950 for (i = 0; i < graph_analysis->num_nodes; i++)
1951 {
1952 char *addr_str;
1953
1954 addr_str = address_to_display(NULL((void*)0), &(graph_analysis->nodes[i]));
1955 sharkd_json_value_string(NULL((void*)0), addr_str);
1956 wmem_free(NULL((void*)0), addr_str);
1957 }
1958 sharkd_json_array_close();
1959
1960 sharkd_json_array_open("flows");
1961 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
1962 while (flow_list)
1963 {
1964 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
1965
1966 flow_list = g_list_next(flow_list)((flow_list) ? (((GList *)(flow_list))->next) : ((void*)0)
)
;
1967
1968 if (!sai->display)
1969 continue;
1970
1971 json_dumper_begin_object(&dumper);
1972
1973 sharkd_json_value_string("t", sai->time_str);
1974 sharkd_json_value_anyf("n", "[%u,%u]", sai->src_node, sai->dst_node);
1975 sharkd_json_value_anyf("pn", "[%u,%u]", sai->port_src, sai->port_dst);
1976
1977 if (sai->comment)
1978 sharkd_json_value_string("c", sai->comment);
1979
1980 json_dumper_end_object(&dumper);
1981 }
1982 sharkd_json_array_close();
1983
1984 json_dumper_end_object(&dumper);
1985}
1986
1987static void
1988sharkd_session_free_tap_flow_cb(void *tapdata)
1989{
1990 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1991
1992 sequence_analysis_info_free(graph_analysis);
1993}
1994
1995struct sharkd_conv_tap_data
1996{
1997 const char *type;
1998 conv_hash_t hash;
1999 bool_Bool resolve_name;
2000 bool_Bool resolve_port;
2001};
2002
2003static bool_Bool
2004sharkd_session_geoip_addr(address *addr, const char *suffix)
2005{
2006 const mmdb_lookup_t *lookup = NULL((void*)0);
2007 bool_Bool with_geoip = false0;
2008 char json_key[64];
2009
2010 if (addr->type == AT_IPv4)
2011 {
2012 const ws_in4_addr *ip4 = (const ws_in4_addr *) addr->data;
2013
2014 lookup = maxmind_db_lookup_ipv4(ip4);
2015 }
2016 else if (addr->type == AT_IPv6)
2017 {
2018 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
2019
2020 lookup = maxmind_db_lookup_ipv6(ip6);
2021 }
2022
2023 if (!lookup || !lookup->found)
2024 return false0;
2025
2026 if (lookup->country)
2027 {
2028 snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix);
2029 sharkd_json_value_string(json_key, lookup->country);
2030 with_geoip = true1;
2031 }
2032
2033 if (lookup->country_iso)
2034 {
2035 snprintf(json_key, sizeof(json_key), "geoip_country_iso%s", suffix);
2036 sharkd_json_value_string(json_key, lookup->country_iso);
2037 with_geoip = true1;
2038 }
2039
2040 if (lookup->city)
2041 {
2042 snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix);
2043 sharkd_json_value_string(json_key, lookup->city);
2044 with_geoip = true1;
2045 }
2046
2047 if (lookup->as_org)
2048 {
2049 snprintf(json_key, sizeof(json_key), "geoip_as_org%s", suffix);
2050 sharkd_json_value_string(json_key, lookup->as_org);
2051 with_geoip = true1;
2052 }
2053
2054 if (lookup->as_number > 0)
2055 {
2056 snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix);
2057 sharkd_json_value_anyf(json_key, "%u", lookup->as_number);
2058 with_geoip = true1;
2059 }
2060
2061 if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
2062 {
2063 snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix);
2064 sharkd_json_value_anyf(json_key, "%f", lookup->latitude);
2065 with_geoip = true1;
2066 }
2067
2068 if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
2069 {
2070 snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix);
2071 sharkd_json_value_anyf(json_key, "%f", lookup->longitude);
2072 with_geoip = true1;
2073 }
2074
2075 return with_geoip;
2076}
2077
2078struct sharkd_analyse_rtp_items
2079{
2080 uint32_t frame_num;
2081 uint32_t sequence_num;
2082
2083 double delta;
2084 double jitter;
2085 double skew;
2086 double bandwidth;
2087 bool_Bool marker;
2088
2089 double arrive_offset;
2090
2091 /* from tap_rtp_stat_t */
2092 uint32_t flags;
2093 uint16_t pt;
2094};
2095
2096struct sharkd_analyse_rtp
2097{
2098 const char *tap_name;
2099 rtpstream_id_t id;
2100
2101 GSList *packets;
2102 double start_time;
2103 tap_rtp_stat_t statinfo;
2104};
2105
2106static void
2107sharkd_session_process_tap_rtp_free_cb(void *tapdata)
2108{
2109 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2110
2111 g_slist_free_full(rtp_req->packets, g_free);
2112 g_free(rtp_req);
2113}
2114
2115static tap_packet_status
2116sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U___attribute__((unused)), const void *pointer, tap_flags_t flags _U___attribute__((unused)))
2117{
2118 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2119 const struct _rtp_info *rtp_info = (const struct _rtp_info *) pointer;
2120
2121 if (rtpstream_id_equal_pinfo_rtp_info(&rtp_req->id, pinfo, rtp_info))
2122 {
2123 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
2124 struct sharkd_analyse_rtp_items *item;
2125
2126 rtppacket_analyse(statinfo, pinfo, rtp_info);
2127
2128 item = g_new(struct sharkd_analyse_rtp_items, 1)((struct sharkd_analyse_rtp_items *) g_malloc_n ((1), sizeof (
struct sharkd_analyse_rtp_items)))
;
2129
2130 if (!rtp_req->packets)
2131 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
2132
2133 item->frame_num = pinfo->num;
2134 item->sequence_num = rtp_info->info_seq_num;
2135 item->delta = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->delta;
2136 item->jitter = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->jitter;
2137 item->skew = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->skew;
2138 item->bandwidth = statinfo->bandwidth;
2139 item->marker = rtp_info->info_marker_set ? true1 : false0;
2140 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
2141
2142 item->flags = statinfo->flags;
2143 item->pt = statinfo->pt;
2144
2145 /* XXX, O(n) optimize */
2146 rtp_req->packets = g_slist_append(rtp_req->packets, item);
2147 }
2148
2149 return TAP_PACKET_REDRAW;
2150}
2151
2152/**
2153 * sharkd_session_process_tap_rtp_analyse_cb()
2154 *
2155 * Output rtp analyse tap:
2156 * (m) tap - tap name
2157 * (m) type - tap output type
2158 * (m) ssrc - RTP SSRC
2159 * (m) max_delta - Max delta (ms)
2160 * (m) max_delta_nr - Max delta packet #
2161 * (m) max_jitter - Max jitter (ms)
2162 * (m) mean_jitter - Mean jitter (ms)
2163 * (m) max_skew - Max skew (ms)
2164 * (m) total_nr - Total number of RTP packets
2165 * (m) seq_err - Number of sequence errors
2166 * (m) duration - Duration (ms)
2167 * (m) items - array of object with attributes:
2168 * (m) f - frame number
2169 * (m) o - arrive offset
2170 * (m) sn - sequence number
2171 * (m) d - delta
2172 * (m) j - jitter
2173 * (m) sk - skew
2174 * (m) bw - bandwidth
2175 * (o) s - status string
2176 * (o) t - status type
2177 * (o) mark - rtp mark
2178 */
2179static void
2180sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
2181{
2182 const int RTP_TYPE_CN = 1;
2183 const int RTP_TYPE_ERROR = 2;
2184 const int RTP_TYPE_WARN = 3;
2185 const int RTP_TYPE_PT_EVENT = 4;
2186
2187 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2188 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
2189
2190 GSList *l;
2191
2192 json_dumper_begin_object(&dumper);
2193
2194 sharkd_json_value_string("tap", rtp_req->tap_name);
2195 sharkd_json_value_string("type", "rtp-analyse");
2196 sharkd_json_value_stringf("ssrc", "0x%x", rtp_req->id.ssrc);
2197
2198 sharkd_json_value_anyf("max_delta", "%f", statinfo->max_delta);
2199 sharkd_json_value_anyf("max_delta_nr", "%u", statinfo->max_nr);
2200 sharkd_json_value_anyf("max_jitter", "%f", statinfo->max_jitter);
2201 sharkd_json_value_anyf("mean_jitter", "%f", statinfo->mean_jitter);
2202 sharkd_json_value_anyf("max_skew", "%f", statinfo->max_skew);
2203 sharkd_json_value_anyf("total_nr", "%u", statinfo->total_nr);
2204 sharkd_json_value_anyf("seq_err", "%u", statinfo->sequence);
2205 sharkd_json_value_anyf("duration", "%f", statinfo->time - statinfo->start_time);
2206
2207 sharkd_json_array_open("items");
2208 for (l = rtp_req->packets; l; l = l->next)
2209 {
2210 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
2211
2212 json_dumper_begin_object(&dumper);
2213
2214 sharkd_json_value_anyf("f", "%u", item->frame_num);
2215 sharkd_json_value_anyf("o", "%.9f", item->arrive_offset);
2216 sharkd_json_value_anyf("sn", "%u", item->sequence_num);
2217 sharkd_json_value_anyf("d", "%.2f", item->delta);
2218 sharkd_json_value_anyf("j", "%.2f", item->jitter);
2219 sharkd_json_value_anyf("sk", "%.2f", item->skew);
2220 sharkd_json_value_anyf("bw", "%.2f", item->bandwidth);
2221
2222 if (item->pt == PT_CN13)
2223 {
2224 sharkd_json_value_string("s", "Comfort noise (PT=13, RFC 3389)");
2225 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2226 }
2227 else if (item->pt == PT_CN_OLD19)
2228 {
2229 sharkd_json_value_string("s", "Comfort noise (PT=19, reserved)");
2230 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2231 }
2232 else if (item->flags & STAT_FLAG_WRONG_SEQ0x004)
2233 {
2234 sharkd_json_value_string("s", "Wrong sequence number");
2235 sharkd_json_value_anyf("t", "%d", RTP_TYPE_ERROR);
2236 }
2237 else if (item->flags & STAT_FLAG_DUP_PKT0x200)
2238 {
2239 sharkd_json_value_string("s", "Suspected duplicate (MAC address) only delta time calculated");
2240 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2241 }
2242 else if (item->flags & STAT_FLAG_REG_PT_CHANGE0x040)
2243 {
2244 sharkd_json_value_stringf("s", "Payload changed to PT=%u%s",
2245 item->pt,
2246 (item->flags & STAT_FLAG_PT_T_EVENT0x100) ? " telephone/event" : "");
2247 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2248 }
2249 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP0x080)
2250 {
2251 sharkd_json_value_string("s", "Incorrect timestamp");
2252 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2253 }
2254 else if ((item->flags & STAT_FLAG_PT_CHANGE0x008)
2255 && !(item->flags & STAT_FLAG_FIRST0x001)
2256 && !(item->flags & STAT_FLAG_PT_CN0x010)
2257 && (item->flags & STAT_FLAG_FOLLOW_PT_CN0x020)
2258 && !(item->flags & STAT_FLAG_MARKER0x002))
2259 {
2260 sharkd_json_value_string("s", "Marker missing?");
2261 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2262 }
2263 else if (item->flags & STAT_FLAG_PT_T_EVENT0x100)
2264 {
2265 sharkd_json_value_stringf("s", "PT=%u telephone/event", item->pt);
2266 sharkd_json_value_anyf("t", "%d", RTP_TYPE_PT_EVENT);
2267 }
2268 else if (item->flags & STAT_FLAG_MARKER0x002)
2269 {
2270 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2271 }
2272
2273 if (item->marker)
2274 sharkd_json_value_anyf("mark", "1");
2275
2276 json_dumper_end_object(&dumper);
2277 }
2278 sharkd_json_array_close();
2279
2280 json_dumper_end_object(&dumper);
2281}
2282
2283/**
2284 * sharkd_session_process_tap_conv_cb()
2285 *
2286 * Output conv tap:
2287 * (m) tap - tap name
2288 * (m) type - tap output type
2289 * (m) proto - protocol short name
2290 * (o) filter - filter string
2291 * (o) geoip - whether GeoIP information is available, boolean
2292 *
2293 * (o) convs - array of object with attributes:
2294 * (m) saddr - source address
2295 * (m) daddr - destination address
2296 * (o) sport - source port
2297 * (o) dport - destination port
2298 * (m) txf - TX frame count
2299 * (m) txb - TX bytes
2300 * (m) rxf - RX frame count
2301 * (m) rxb - RX bytes
2302 * (m) start - (relative) first packet time
2303 * (m) stop - (relative) last packet time
2304 * (o) filter - conversation filter
2305 *
2306 * (o) hosts - array of object with attributes:
2307 * (m) host - host address
2308 * (o) port - host port
2309 * (m) txf - TX frame count
2310 * (m) txb - TX bytes
2311 * (m) rxf - RX frame count
2312 * (m) rxb - RX bytes
2313 */
2314static void
2315sharkd_session_process_tap_conv_cb(void *arg)
2316{
2317 conv_hash_t *hash = (conv_hash_t *) arg;
2318 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2319 const char *proto;
2320 int proto_with_port;
2321 unsigned i;
2322
2323 int with_geoip = 0;
2324
2325 json_dumper_begin_object(&dumper);
2326 sharkd_json_value_string("tap", iu->type);
2327
2328 if (!strncmp(iu->type, "conv:", 5))
2329 {
2330 sharkd_json_value_string("type", "conv");
2331 sharkd_json_array_open("convs");
2332 proto = iu->type + 5;
2333 }
2334 else if (!strncmp(iu->type, "endpt:", 6))
2335 {
2336 sharkd_json_value_string("type", "host");
2337 sharkd_json_array_open("hosts");
2338 proto = iu->type + 6;
2339 }
2340 else
2341 {
2342 sharkd_json_value_string("type", "err");
2343 proto = "";
2344 }
2345
2346 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
2347
2348 if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "conv:", 5))
2349 {
2350 for (i = 0; i < iu->hash.conv_array->len; i++)
2351 {
2352 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i)(((conv_item_t*) (void *) (iu->hash.conv_array)->data) [
(i)])
;
2353 char *src_addr, *dst_addr;
2354 char *src_port, *dst_port;
2355 char *filter_str;
2356
2357 json_dumper_begin_object(&dumper);
2358
2359 sharkd_json_value_string("saddr", (src_addr = get_conversation_address(NULL((void*)0), &iui->src_address, iu->resolve_name)));
2360 sharkd_json_value_string("daddr", (dst_addr = get_conversation_address(NULL((void*)0), &iui->dst_address, iu->resolve_name)));
2361
2362 if (proto_with_port)
2363 {
2364 sharkd_json_value_string("sport", (src_port = get_conversation_port(NULL((void*)0), iui->src_port, iui->ctype, iu->resolve_port)));
2365 sharkd_json_value_string("dport", (dst_port = get_conversation_port(NULL((void*)0), iui->dst_port, iui->ctype, iu->resolve_port)));
2366
2367 wmem_free(NULL((void*)0), src_port);
2368 wmem_free(NULL((void*)0), dst_port);
2369 }
2370
2371 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", iui->rx_frames);
2372 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", iui->rx_bytes);
2373
2374 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", iui->tx_frames);
2375 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", iui->tx_bytes);
2376
2377 sharkd_json_value_anyf("start", "%.9f", nstime_to_sec(&iui->start_time));
2378 sharkd_json_value_anyf("stop", "%.9f", nstime_to_sec(&iui->stop_time));
2379
2380 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
2381 if (filter_str)
2382 {
2383 sharkd_json_value_string("filter", filter_str);
2384 g_free(filter_str);
2385 }
2386
2387 wmem_free(NULL((void*)0), src_addr);
2388 wmem_free(NULL((void*)0), dst_addr);
2389
2390 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
2391 with_geoip = 1;
2392 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
2393 with_geoip = 1;
2394
2395 json_dumper_end_object(&dumper);
2396 }
2397 }
2398 else if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "endpt:", 6))
2399 {
2400 for (i = 0; i < iu->hash.conv_array->len; i++)
2401 {
2402 endpoint_item_t *endpoint = &g_array_index(iu->hash.conv_array, endpoint_item_t, i)(((endpoint_item_t*) (void *) (iu->hash.conv_array)->data
) [(i)])
;
2403 char *host_str, *port_str;
2404 char *filter_str;
2405
2406 json_dumper_begin_object(&dumper);
2407
2408 sharkd_json_value_string("host", (host_str = get_conversation_address(NULL((void*)0), &endpoint->myaddress, iu->resolve_name)));
2409
2410 if (proto_with_port)
2411 {
2412 sharkd_json_value_string("port", (port_str = get_endpoint_port(NULL((void*)0), endpoint, iu->resolve_port)));
2413
2414 wmem_free(NULL((void*)0), port_str);
2415 }
2416
2417 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", endpoint->rx_frames);
2418 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", endpoint->rx_bytes);
2419
2420 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", endpoint->tx_frames);
2421 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", endpoint->tx_bytes);
2422
2423 filter_str = get_endpoint_filter(endpoint);
2424 if (filter_str)
2425 {
2426 sharkd_json_value_string("filter", filter_str);
2427 g_free(filter_str);
2428 }
2429
2430 wmem_free(NULL((void*)0), host_str);
2431
2432 if (sharkd_session_geoip_addr(&(endpoint->myaddress), ""))
2433 with_geoip = 1;
2434 json_dumper_end_object(&dumper);
2435 }
2436 }
2437 sharkd_json_array_close();
2438
2439 sharkd_json_value_string("proto", proto);
2440 sharkd_json_value_anyf("geoip", with_geoip ? "true" : "false");
2441
2442 json_dumper_end_object(&dumper);
2443}
2444
2445static void
2446sharkd_session_free_tap_conv_cb(void *arg)
2447{
2448 conv_hash_t *hash = (conv_hash_t *) arg;
2449 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2450
2451 if (!strncmp(iu->type, "conv:", 5))
2452 {
2453 reset_conversation_table_data(hash);
2454 }
2455 else if (!strncmp(iu->type, "endpt:", 6))
2456 {
2457 reset_endpoint_table_data(hash);
2458 }
2459
2460 g_free(iu);
2461}
2462
2463/**
2464 * sharkd_session_process_tap_nstat_cb()
2465 *
2466 * Output nstat tap:
2467 * (m) tap - tap name
2468 * (m) type - tap output type
2469 * (m) fields: array of objects with attributes:
2470 * (m) c - name
2471 *
2472 * (m) tables: array of object with attributes:
2473 * (m) t - table title
2474 * (m) i - array of items
2475 */
2476static void
2477sharkd_session_process_tap_nstat_cb(void *arg)
2478{
2479 stat_data_t *stat_data = (stat_data_t *) arg;
2480 unsigned i, j, k;
2481
2482 json_dumper_begin_object(&dumper);
2483 sharkd_json_value_stringf("tap", "nstat:%s", stat_data->stat_tap_data->cli_string);
2484 sharkd_json_value_string("type", "nstat");
2485
2486 sharkd_json_array_open("fields");
2487 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
2488 {
2489 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
2490
2491 json_dumper_begin_object(&dumper);
2492 sharkd_json_value_string("c", field->column_name);
2493 json_dumper_end_object(&dumper);
2494 }
2495 sharkd_json_array_close();
2496
2497 sharkd_json_array_open("tables");
2498 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
2499 {
2500 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i)(((stat_tap_table **) (void *) (stat_data->stat_tap_data->
tables)->data) [(i)])
;
2501
2502 json_dumper_begin_object(&dumper);
2503
2504 sharkd_json_value_string("t", table->title);
2505
2506 sharkd_json_array_open("i");
2507 for (j = 0; j < table->num_elements; j++)
2508 {
2509 stat_tap_table_item_type *field_data;
2510
2511 field_data = stat_tap_get_field_data(table, j, 0);
2512 if (field_data == NULL((void*)0) || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
2513 continue;
2514
2515 sharkd_json_array_open(NULL((void*)0));
2516 for (k = 0; k < table->num_fields; k++)
2517 {
2518 field_data = stat_tap_get_field_data(table, j, k);
2519
2520 switch (field_data->type)
2521 {
2522 case TABLE_ITEM_UINT:
2523 sharkd_json_value_anyf(NULL((void*)0), "%u", field_data->value.uint_value);
2524 break;
2525
2526 case TABLE_ITEM_INT:
2527 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.int_value);
2528 break;
2529
2530 case TABLE_ITEM_STRING:
2531 sharkd_json_value_string(NULL((void*)0), field_data->value.string_value);
2532 break;
2533
2534 case TABLE_ITEM_FLOAT:
2535 sharkd_json_value_anyf(NULL((void*)0), "%f", field_data->value.float_value);
2536 break;
2537
2538 case TABLE_ITEM_ENUM:
2539 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.enum_value);
2540 break;
2541
2542 case TABLE_ITEM_NONE:
2543 sharkd_json_value_anyf(NULL((void*)0), "null");
2544 break;
2545 }
2546 }
2547
2548 sharkd_json_array_close();
2549 }
2550 sharkd_json_array_close();
2551 json_dumper_end_object(&dumper);
2552 }
2553 sharkd_json_array_close();
2554
2555 json_dumper_end_object(&dumper);
2556}
2557
2558static void
2559sharkd_session_free_tap_nstat_cb(void *arg)
2560{
2561 stat_data_t *stat_data = (stat_data_t *) arg;
2562
2563 free_stat_tables(stat_data->stat_tap_data);
2564}
2565
2566/**
2567 * sharkd_session_process_tap_rtd_cb()
2568 *
2569 * Output rtd tap:
2570 * (m) tap - tap name
2571 * (m) type - tap output type
2572 * (m) stats - statistics rows - array object with attributes:
2573 * (m) type - statistic name
2574 * (m) num - number of messages
2575 * (m) min - minimum SRT time
2576 * (m) max - maximum SRT time
2577 * (m) tot - total SRT time
2578 * (m) min_frame - minimal SRT
2579 * (m) max_frame - maximum SRT
2580 * (o) open_req - Open Requests
2581 * (o) disc_rsp - Discarded Responses
2582 * (o) req_dup - Duplicated Requests
2583 * (o) rsp_dup - Duplicated Responses
2584 * (o) open_req - Open Requests
2585 * (o) disc_rsp - Discarded Responses
2586 * (o) req_dup - Duplicated Requests
2587 * (o) rsp_dup - Duplicated Responses
2588 */
2589static void
2590sharkd_session_process_tap_rtd_cb(void *arg)
2591{
2592 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2593 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
2594
2595 unsigned i, j;
2596
2597 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
2598
2599 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
2600 * some multiple table and single timestat (radius, h225)
2601 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
2602 * I wonder how it will gonna work with multiple timestats and multiple tables...
2603 * (for usage grep for: register_rtd_table)
2604 */
2605 const value_string *vs = get_rtd_value_string(rtd);
2606
2607 json_dumper_begin_object(&dumper);
2608 sharkd_json_value_stringf("tap", "rtd:%s", filter);
2609 sharkd_json_value_string("type", "rtd");
2610
2611 if (rtd_data->stat_table.num_rtds == 1)
2612 {
2613 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
2614
2615 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2616 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2617 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2618 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2619 }
2620
2621 sharkd_json_array_open("stats");
2622 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
2623 {
2624 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
2625
2626 for (j = 0; j < ms->num_timestat; j++)
2627 {
2628 const char *type_str;
2629
2630 if (ms->rtd[j].num == 0)
2631 continue;
2632
2633 json_dumper_begin_object(&dumper);
2634
2635 if (rtd_data->stat_table.num_rtds == 1)
2636 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
2637 else
2638 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
2639 sharkd_json_value_string("type", type_str);
2640
2641 sharkd_json_value_anyf("num", "%u", ms->rtd[j].num);
2642 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&(ms->rtd[j].min)));
2643 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&(ms->rtd[j].max)));
2644 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
2645 sharkd_json_value_anyf("min_frame", "%u", ms->rtd[j].min_num);
2646 sharkd_json_value_anyf("max_frame", "%u", ms->rtd[j].max_num);
2647
2648 if (rtd_data->stat_table.num_rtds != 1)
2649 {
2650 /* like in tshark, display it on every row */
2651 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2652 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2653 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2654 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2655 }
2656
2657 json_dumper_end_object(&dumper);
2658 }
2659 }
2660 sharkd_json_array_close();
2661
2662 json_dumper_end_object(&dumper);
2663}
2664
2665static void
2666sharkd_session_free_tap_rtd_cb(void *arg)
2667{
2668 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2669
2670 free_rtd_table(&rtd_data->stat_table);
2671 g_free(rtd_data);
2672}
2673
2674/**
2675 * sharkd_session_process_tap_srt_cb()
2676 *
2677 * Output srt tap:
2678 * (m) tap - tap name
2679 * (m) type - tap output type
2680 *
2681 * (m) tables - array of object with attributes:
2682 * (m) n - table name
2683 * (m) f - table filter
2684 * (o) c - table column name
2685 * (m) r - table rows - array object with attributes:
2686 * (m) n - row name
2687 * (m) idx - procedure index
2688 * (m) num - number of events
2689 * (m) min - minimum SRT time
2690 * (m) max - maximum SRT time
2691 * (m) tot - total SRT time
2692 */
2693static void
2694sharkd_session_process_tap_srt_cb(void *arg)
2695{
2696 srt_data_t *srt_data = (srt_data_t *) arg;
2697 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2698
2699 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
2700
2701 unsigned i;
2702
2703 json_dumper_begin_object(&dumper);
2704 sharkd_json_value_stringf("tap", "srt:%s", filter);
2705 sharkd_json_value_string("type", "srt");
2706
2707 sharkd_json_array_open("tables");
2708 for (i = 0; i < srt_data->srt_array->len; i++)
2709 {
2710 /* SRT table */
2711 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i)(((srt_stat_table **) (void *) (srt_data->srt_array)->data
) [(i)])
;
2712
2713 int j;
2714
2715 json_dumper_begin_object(&dumper);
2716
2717 if (rst->name)
2718 sharkd_json_value_string("n", rst->name);
2719 else if (rst->short_name)
2720 sharkd_json_value_string("n", rst->short_name);
2721 else
2722 sharkd_json_value_stringf("n", "table%u", i);
2723
2724 if (rst->filter_string)
2725 sharkd_json_value_string("f", rst->filter_string);
2726
2727 if (rst->proc_column_name)
2728 sharkd_json_value_string("c", rst->proc_column_name);
2729
2730 sharkd_json_array_open("r");
2731 for (j = 0; j < rst->num_procs; j++)
2732 {
2733 /* SRT row */
2734 srt_procedure_t *proc = &rst->procedures[j];
2735
2736 if (proc->stats.num == 0)
2737 continue;
2738
2739 json_dumper_begin_object(&dumper);
2740
2741 sharkd_json_value_string("n", proc->procedure);
2742
2743 if (rst->filter_string)
2744 sharkd_json_value_anyf("idx", "%d", proc->proc_index);
2745
2746 sharkd_json_value_anyf("num", "%u", proc->stats.num);
2747
2748 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&proc->stats.min));
2749 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&proc->stats.max));
2750 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&proc->stats.tot));
2751
2752 json_dumper_end_object(&dumper);
2753 }
2754 sharkd_json_array_close();
2755
2756 json_dumper_end_object(&dumper);
2757 }
2758 sharkd_json_array_close();
2759
2760 json_dumper_end_object(&dumper);
2761}
2762
2763static void
2764sharkd_session_free_tap_srt_cb(void *arg)
2765{
2766 srt_data_t *srt_data = (srt_data_t *) arg;
2767 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2768
2769 free_srt_table(srt, srt_data->srt_array);
2770 g_array_free(srt_data->srt_array, TRUE(!(0)));
2771 g_free(srt_data);
2772}
2773
2774static void
2775sharkd_session_process_tap_phs_cb_aux(phs_t *rs)
2776{
2777 for (; rs; rs = rs->sibling) {
2778 if (rs->protocol == -1) {
2779 return;
2780 }
2781 sharkd_json_object_open(NULL((void*)0));
2782 sharkd_json_value_string("proto", rs->proto_name);
2783 sharkd_json_value_anyf("frames", "%"PRIu32"u", rs->frames);
2784 sharkd_json_value_anyf("bytes", "%"PRIu64"l" "u", rs->bytes);
2785 if (rs->child != NULL((void*)0) && rs->child->protocol != -1) {
2786 sharkd_json_array_open("protos");
2787 sharkd_session_process_tap_phs_cb_aux(rs->child);
2788 sharkd_json_array_close();
2789 }
2790 sharkd_json_object_close();
2791 }
2792}
2793
2794/**
2795 * sharkd_session_process_tap_phs_cb()
2796 *
2797 * Output phs tap:
2798 * (m) tap - tap name
2799 * (m) type - tap output type
2800 * (m) filter - tap filter argument
2801 * (m) protos - array of proto objects
2802 *
2803 * proto object:
2804 * (m) proto - protocol name
2805 * (m) frames - frame count
2806 * (m) bytes - bytes count
2807 * (o) protos - array of proto objects
2808 */
2809static void
2810sharkd_session_process_tap_phs_cb(void *arg)
2811{
2812 phs_t *rs = (phs_t *)arg;
2813 sharkd_json_object_open(NULL((void*)0));
2814 sharkd_json_value_string("tap", "phs");
2815 sharkd_json_value_string("type", "phs");
2816 sharkd_json_value_string("filter", rs->filter ? rs->filter : "");
2817 sharkd_json_array_open("protos");
2818 sharkd_session_process_tap_phs_cb_aux(rs);
2819 sharkd_json_array_close();
2820 sharkd_json_object_close();
2821}
2822
2823static void
2824sharkd_session_free_tap_phs_cb(void *arg)
2825{
2826 phs_t *rs = (phs_t *)arg;
2827 free_phs(rs);
2828}
2829
2830struct sharkd_export_object_list
2831{
2832 struct sharkd_export_object_list *next;
2833
2834 char *type;
2835 const char *proto;
2836 GSList *entries;
2837};
2838
2839static struct sharkd_export_object_list *sharkd_eo_list;
2840
2841/**
2842 * sharkd_session_process_tap_eo_cb()
2843 *
2844 * Output eo tap:
2845 * (m) tap - tap name
2846 * (m) type - tap output type
2847 * (m) proto - protocol short name
2848 * (m) objects - array of object with attributes:
2849 * (m) pkt - packet number
2850 * (o) hostname - hostname
2851 * (o) type - content type
2852 * (o) filename - filename
2853 * (m) len - object length
2854 * (m) sha1 - object's sha1 sum
2855 */
2856static void
2857sharkd_session_process_tap_eo_cb(void *tapdata)
2858{
2859 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
2860 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data;
2861 GSList *slist;
2862 int i = 0;
2863 char sha1sum_bytes[HASH_SHA1_LENGTH20], *sha1sum_str;
2864
2865 json_dumper_begin_object(&dumper);
2866 sharkd_json_value_string("tap", object_list->type);
2867 sharkd_json_value_string("type", "eo");
2868
2869 sharkd_json_value_string("proto", object_list->proto);
2870
2871 sharkd_json_array_open("objects");
2872 for (slist = object_list->entries; slist; slist = slist->next)
2873 {
2874 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
2875
2876 json_dumper_begin_object(&dumper);
2877
2878 sharkd_json_value_anyf("pkt", "%u", eo_entry->pkt_num);
2879
2880 if (eo_entry->hostname)
2881 sharkd_json_value_string("hostname", eo_entry->hostname);
2882
2883 if (eo_entry->content_type)
2884 sharkd_json_value_string("type", eo_entry->content_type);
2885
2886 if (eo_entry->filename)
2887 sharkd_json_value_string("filename", eo_entry->filename);
2888
2889 sharkd_json_value_stringf("_download", "%s_%d", object_list->type, i);
2890
2891 sharkd_json_value_anyf("len", "%zu", eo_entry->payload_len);
2892
2893 gcry_md_hash_buffer(GCRY_MD_SHA1, sha1sum_bytes, eo_entry->payload_data, eo_entry->payload_len);
2894 sha1sum_str = bytes_to_str(NULL, sha1sum_bytes, HASH_SHA1_LENGTH)bytes_to_str_maxlen(((void*)0), sha1sum_bytes, 20, 36);
2895 sharkd_json_value_string("sha1", sha1sum_str);
2896 g_free(sha1sum_str);
2897
2898 json_dumper_end_object(&dumper);
2899
2900 i++;
2901 }
2902 sharkd_json_array_close();
2903
2904 json_dumper_end_object(&dumper);
2905}
2906
2907static void
2908sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
2909{
2910 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2911
2912 object_list->entries = g_slist_append(object_list->entries, entry);
2913}
2914
2915static export_object_entry_t *
2916sharkd_eo_object_list_get_entry(void *gui_data, int row)
2917{
2918 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2919
2920 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
2921}
2922
2923static struct sharkd_export_object_list *
2924sharkd_eo_object_list_get_entry_by_type(void *gui_data, const char *tap_type)
2925{
2926 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2927 for (; object_list; object_list = object_list->next)
2928 {
2929 if (!strcmp(object_list->type, tap_type))
2930 return object_list;
2931 }
2932 return NULL((void*)0);
2933}
2934
2935
2936/**
2937 * sharkd_session_process_tap_rtp_cb()
2938 *
2939 * Output RTP streams tap:
2940 * (m) tap - tap name
2941 * (m) type - tap output type
2942 * (m) streams - array of object with attributes:
2943 * (m) ssrc - RTP synchronization source identifier
2944 * (m) payload - stream payload
2945 * (m) saddr - source address
2946 * (m) sport - source port
2947 * (m) daddr - destination address
2948 * (m) dport - destination port
2949 * (m) pkts - packets count
2950 * (m) max_delta - max delta (ms)
2951 * (m) max_jitter - max jitter (ms)
2952 * (m) mean_jitter - mean jitter (ms)
2953 * (m) expectednr -
2954 * (m) totalnr -
2955 * (m) problem - if analyser found the problem
2956 * (m) ipver - address IP version (4 or 6)
2957 */
2958static void
2959sharkd_session_process_tap_rtp_cb(void *arg)
2960{
2961 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
2962
2963 GList *listx;
2964
2965 json_dumper_begin_object(&dumper);
2966 sharkd_json_value_string("tap", "rtp-streams");
2967 sharkd_json_value_string("type", "rtp-streams");
2968
2969 sharkd_json_array_open("streams");
2970 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
2971 {
2972 rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data;
2973 rtpstream_info_calc_t calc;
2974
2975 rtpstream_info_calculate(streaminfo, &calc);
2976
2977 json_dumper_begin_object(&dumper);
2978
2979 sharkd_json_value_stringf("ssrc", "0x%x", calc.ssrc);
2980 sharkd_json_value_string("payload", calc.all_payload_type_names);
2981
2982 sharkd_json_value_string("saddr", calc.src_addr_str);
2983 sharkd_json_value_anyf("sport", "%u", calc.src_port);
2984 sharkd_json_value_string("daddr", calc.dst_addr_str);
2985 sharkd_json_value_anyf("dport", "%u", calc.dst_port);
2986
2987 sharkd_json_value_anyf("start_time", "%f", calc.start_time_ms);
2988 sharkd_json_value_anyf("duration", "%f", calc.duration_ms);
2989
2990 sharkd_json_value_anyf("pkts", "%u", calc.packet_count);
2991 sharkd_json_value_anyf("lost", "%u", calc.lost_num);
2992 sharkd_json_value_anyf("lost_percent", "%f", calc.lost_perc);
2993
2994 sharkd_json_value_anyf("max_delta", "%f",calc.max_delta);
2995 sharkd_json_value_anyf("min_delta", "%f",calc.min_delta);
2996 sharkd_json_value_anyf("mean_delta", "%f",calc.mean_delta);
2997 sharkd_json_value_anyf("min_jitter", "%f", calc.min_jitter);
2998 sharkd_json_value_anyf("max_jitter", "%f", calc.max_jitter);
2999 sharkd_json_value_anyf("mean_jitter", "%f", calc.mean_jitter);
3000
3001 sharkd_json_value_anyf("expectednr", "%u", calc.packet_expected);
3002 sharkd_json_value_anyf("totalnr", "%u", calc.total_nr);
3003
3004 sharkd_json_value_anyf("problem", calc.problem ? "true" : "false");
3005
3006 /* for filter */
3007 sharkd_json_value_anyf("ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
3008
3009 rtpstream_info_calc_free(&calc);
3010
3011 json_dumper_end_object(&dumper);
3012 }
3013 sharkd_json_array_close();
3014
3015 json_dumper_end_object(&dumper);
3016}
3017
3018/**
3019* sharkd_session_process_tap_multicast_cb()
3020*
3021* Output UDP Multicast streams tap:
3022* (m) tap - tap name
3023* (m) type - tap output type
3024* (m) bufferThresholdBytes - byte count for a stream where a buffer alarm should be reported
3025* (m) burstIntervalMs - analysis interval in milliseconds
3026* (m) burstThresholdPackets - count of packets in an interval that should trigger an alarm
3027* (m) streams - array of streams with metrics:
3028* (m) saddr - source address
3029* (m) sport - source port
3030* (m) daddr - destination address
3031* (m) dport - destination port
3032* (m) packets - object group for packet metrics with attributes:
3033* (m) number - count of packets in the stream
3034* (m) perSecond - average number of packets per seconds in the stream
3035* (m) bandwidth - object group for bandwidth metrics with attributes:
3036* (m) average - average measured bitrate in the stream
3037* (m) max - max measured bitrate in the stream
3038* (m) buffer - object group for buffer metrics with attributes:
3039* (m) alarms - number of times the stream exceeded the buffer threshold
3040* (m) max - highest stream buffer utilization
3041* (m) burst - object group for burst metrics with attributes:
3042* (m) alarms - number of times the stream exceeded the burst threshold
3043* (m) max - most stream packets measured in a burst interval
3044*/
3045static void
3046sharkd_session_process_tap_multicast_cb(void *arg)
3047{
3048 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)arg;
3049 GList *list_item;
3050 char *addr_str;
3051
3052 json_dumper_begin_object(&dumper);
3053
3054 sharkd_json_value_string("tap", "multicast");
3055 sharkd_json_value_string("type", "multicast");
3056
3057 sharkd_json_value_anyf("bufferThresholdBytes", "%u", mcast_stream_bufferalarm);
3058 sharkd_json_value_anyf("burstIntervalMs", "%u", mcast_stream_burstint);
3059 sharkd_json_value_anyf("burstThresholdPackets", "%u", mcast_stream_trigger);
3060
3061 sharkd_json_array_open("streams");
3062 for (list_item = g_list_first(tapinfo->strinfo_list); list_item; list_item = list_item->next) {
3063 mcast_stream_info_t *stream_info = (mcast_stream_info_t *) list_item->data;
3064 sharkd_json_object_open(NULL((void*)0));
3065 {
3066 addr_str = address_to_display(NULL((void*)0), &stream_info->src_addr);
3067 sharkd_json_value_string("saddr", addr_str);
3068 wmem_free(NULL((void*)0), addr_str);
3069 sharkd_json_value_anyf("sport", "%u", stream_info->src_port);
3070 addr_str = address_to_display(NULL((void*)0), &stream_info->dest_addr);
3071 sharkd_json_value_string("daddr", addr_str);
3072 wmem_free(NULL((void*)0), addr_str);
3073 sharkd_json_value_anyf("dport", "%u", stream_info->dest_port);
3074 sharkd_json_object_open("packets");
3075 {
3076 sharkd_json_value_anyf("number", "%u", stream_info->npackets);
3077 sharkd_json_value_anyf("perSecond", "%f", stream_info->apackets);
3078 }
3079 sharkd_json_object_close();
3080 sharkd_json_object_open("bandwidth");
3081 {
3082 sharkd_json_value_anyf("average", "%f", stream_info->average_bw);
3083 sharkd_json_value_anyf("max", "%f", stream_info->element.maxbw);
3084 }
3085 sharkd_json_object_close();
3086 sharkd_json_object_open("buffer");
3087 {
3088 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbuffalarms);
3089 sharkd_json_value_anyf("max", "%u", stream_info->element.topbuffusage);
3090 }
3091 sharkd_json_object_close();
3092 sharkd_json_object_open("burst");
3093 {
3094 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbursts);
3095 sharkd_json_value_anyf("max", "%u", stream_info->element.topburstsize);
3096 }
3097 sharkd_json_object_close();
3098 }
3099 sharkd_json_object_close();
3100 }
3101 sharkd_json_array_close();
3102
3103 json_dumper_end_object(&dumper);
3104}
3105
3106static void
3107sharkd_session_process_free_tap_multicast_cb(void *tapdata)
3108{
3109 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)tapdata;
3110
3111 mcaststream_reset(tapinfo);
3112
3113 g_free(tapinfo);
3114}
3115
3116/**
3117 * sharkd_session_process_tap_voip_calls_cb()
3118 *
3119 * Output VoIP Calls tap:
3120 * (m) tap - tap name
3121 * (m) type - tap output type
3122 * (m) calls - array of objects with attributes:
3123 * (m) call - call number
3124 * (m) start_time - start timestamp
3125 * (m) stop_time - stop timestamp
3126 * (m) initial_speaker - address of initial speaker
3127 * (m) from - from address
3128 * (m) to - to address
3129 * (m) protocol - protocol name
3130 * (m) packets - packet count
3131 * (m) state - state string
3132 * (m) comment - comment string
3133 */
3134static void
3135sharkd_session_process_tap_voip_calls_cb(void *arg)
3136{
3137 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)arg;
3138 char *addr_str;
3139 GList *cur_call = g_queue_peek_nth_link(tapinfo->callsinfos, 0);
3140 sharkd_json_object_open(NULL((void*)0));
3141 sharkd_json_value_string("tap", "voip-calls");
3142 sharkd_json_value_string("type", "voip-calls");
3143 sharkd_json_array_open("calls");
3144 while (cur_call && cur_call->data) {
3145 voip_calls_info_t *call_info_ = (voip_calls_info_t*) cur_call->data;
3146 sharkd_json_object_open(NULL((void*)0));
3147 sharkd_json_value_anyf("call", "%hu", call_info_->call_num);
3148 sharkd_json_value_anyf("start_time", "%.6f", nstime_to_sec(&(call_info_->start_rel_ts)));
3149 sharkd_json_value_anyf("stop_time", "%.6f", nstime_to_sec(&(call_info_->stop_rel_ts)));
3150 addr_str = address_to_display(NULL((void*)0), &(call_info_->initial_speaker));
3151 sharkd_json_value_string("initial_speaker", addr_str);
3152 wmem_free(NULL((void*)0), addr_str);
3153 sharkd_json_value_string("from", call_info_->from_identity);
3154 sharkd_json_value_string("to", call_info_->to_identity);
3155 sharkd_json_value_string("protocol", ((call_info_->protocol == VOIP_COMMON) && call_info_->protocol_name) ?
3156 call_info_->protocol_name : voip_protocol_name[call_info_->protocol]);
3157 sharkd_json_value_anyf("packets", "%u", call_info_->npackets);
3158 sharkd_json_value_string("state", voip_call_state_name[call_info_->call_state]);
3159 sharkd_json_value_string("comment", call_info_->call_comment);
3160 sharkd_json_object_close();
3161 cur_call = g_list_next(cur_call)((cur_call) ? (((GList *)(cur_call))->next) : ((void*)0));
3162 }
3163 sharkd_json_array_close();
3164 sharkd_json_object_close();
3165}
3166
3167static void
3168sharkd_session_free_tap_voip_calls_cb(void *tapdata)
3169{
3170 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)tapdata;
3171 voip_calls_remove_all_tap_listeners(tapinfo);
3172 if (tapinfo->callsinfos != NULL((void*)0)) {
3173 g_queue_free(tapinfo->callsinfos);
3174 }
3175 if (tapinfo->graph_analysis != NULL((void*)0)) {
3176 sequence_analysis_info_free(tapinfo->graph_analysis);
3177 }
3178 memset(tapinfo, 0, sizeof(*tapinfo));
3179}
3180
3181
3182struct sharkd_voip_convs_req {
3183 voip_calls_tapinfo_t *tapinfo;
3184 const char *tap_name;
3185};
3186
3187/**
3188 * sharkd_session_process_tap_voip_convs_cb()
3189 *
3190 * Output VoIP Conversations tap:
3191 * (m) tap - tap name
3192 * (m) type - tap output type
3193 * (m) convs - array of objects with attributes:
3194 * (m) frame - frame number
3195 * (m) call - call number
3196 * (m) time - timestamp
3197 * (m) dst_addr - destination address
3198 * (m) dst_port - destination port
3199 * (m) src_addr - source address
3200 * (m) src_port - source port
3201 * (m) label - label string
3202 * (m) comment - comment string
3203 */
3204static void
3205sharkd_session_process_tap_voip_convs_cb(void *arg)
3206{
3207 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)arg;
3208 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3209 seq_analysis_info_t *sainfo = tapinfo->graph_analysis;
3210 char *addr_str;
3211 sequence_analysis_list_sort(sainfo);
3212 sharkd_json_object_open(NULL((void*)0));
3213 sharkd_json_value_string("tap", voip_convs_req->tap_name);
3214 sharkd_json_value_string("type", "voip-convs");
3215 sharkd_json_array_open("convs");
3216 for (GList *cur = g_queue_peek_nth_link(sainfo->items, 0); cur; cur = g_list_next(cur)((cur) ? (((GList *)(cur))->next) : ((void*)0))) {
3217 seq_analysis_item_t *sai = (seq_analysis_item_t *) cur->data;
3218 if ((voip_conv_sel[sai->conv_num / VOIP_CONV_BITS(sizeof(int) * 8)] & (1 << (sai->conv_num % VOIP_CONV_BITS(sizeof(int) * 8)))) == 0)
3219 continue;
3220 sharkd_json_object_open(NULL((void*)0));
3221 sharkd_json_value_anyf("frame", "%d", sai->frame_number);
3222 sharkd_json_value_anyf("call", "%d", sai->conv_num);
3223 sharkd_json_value_string("time", sai->time_str);
3224 addr_str = address_to_display(NULL((void*)0), &(sai->dst_addr));
3225 sharkd_json_value_string("dst_addr", addr_str);
3226 wmem_free(NULL((void*)0), addr_str);
3227 sharkd_json_value_anyf("dst_port", "%d", sai->port_dst);
3228 addr_str = address_to_display(NULL((void*)0), &(sai->src_addr));
3229 sharkd_json_value_string("src_addr", addr_str);
3230 wmem_free(NULL((void*)0), addr_str);
3231 sharkd_json_value_anyf("src_port", "%d", sai->port_src);
3232 sharkd_json_value_string("label", sai->frame_label);
3233 sharkd_json_value_string("comment", sai->comment);
3234 sharkd_json_object_close();
3235 }
3236 sharkd_json_array_close();
3237 sharkd_json_object_close();
3238}
3239
3240static void
3241sharkd_session_free_tap_voip_convs_cb(void *tapdata)
3242{
3243 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)tapdata;
3244 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3245 voip_calls_remove_all_tap_listeners(tapinfo);
3246 if (tapinfo->callsinfos != NULL((void*)0)) {
3247 g_queue_free(tapinfo->callsinfos);
3248 }
3249 if (tapinfo->graph_analysis != NULL((void*)0)) {
3250 sequence_analysis_info_free(tapinfo->graph_analysis);
3251 }
3252 memset(tapinfo, 0, sizeof(*tapinfo));
3253 g_free(voip_convs_req);
3254}
3255
3256struct sharkd_hosts_req {
3257 const char *tap_name;
3258 bool_Bool dump_v4;
3259 bool_Bool dump_v6;
3260};
3261
3262static int
3263sharkd_session_tap_ipv4_host_compare(const void *a, const void *b)
3264{
3265 return ws_ascii_strnatcmp(((const hashipv4_t *)a)->name,
3266 ((const hashipv4_t *)b)->name);
3267}
3268
3269static int
3270sharkd_session_tap_ipv6_host_compare(const void *a, const void *b)
3271{
3272 return ws_ascii_strnatcmp(((const hashipv6_t *)a)->name,
3273 ((const hashipv6_t *)b)->name);
3274}
3275
3276static void
3277sharkd_session_tap_ipv4_host_print(void *data, void *user_data _U___attribute__((unused)))
3278{
3279 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)data;
3280 sharkd_json_object_open(NULL((void*)0));
3281 sharkd_json_value_string("name", ipv4_hash_table_entry->name);
3282 sharkd_json_value_string("addr", ipv4_hash_table_entry->ip);
3283 sharkd_json_object_close();
3284}
3285
3286static void
3287sharkd_session_tap_ipv6_host_print(void *data, void *user_data _U___attribute__((unused)))
3288{
3289 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)data;
3290 sharkd_json_object_open(NULL((void*)0));
3291 sharkd_json_value_string("name", ipv6_hash_table_entry->name);
3292 sharkd_json_value_string("addr", ipv6_hash_table_entry->ip6);
3293 sharkd_json_object_close();
3294}
3295
3296static void
3297sharkd_session_tap_ipv4_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3298{
3299 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
3300 GSList **list = (GSList **)user_data;
3301 if ((ipv4_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3302 *list = g_slist_insert_sorted(*list, ipv4_hash_table_entry, sharkd_session_tap_ipv4_host_compare);
3303 }
3304}
3305
3306static void
3307sharkd_session_tap_ipv6_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3308{
3309 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
3310 GSList **list = (GSList **) user_data;
3311 if ((ipv6_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3312 *list = g_slist_insert_sorted(*list, ipv6_hash_table_entry, sharkd_session_tap_ipv6_host_compare);
3313 }
3314}
3315
3316static void
3317sharkd_session_tap_ipv4_hosts_print(void)
3318{
3319 wmem_map_t *ipv4_hash_table = get_ipv4_hash_table();
3320 if (!ipv4_hash_table)
3321 return;
3322 GSList *list = NULL((void*)0);
3323 wmem_map_foreach(ipv4_hash_table, sharkd_session_tap_ipv4_host_insert_sorted, &list);
3324 g_slist_foreach(list, sharkd_session_tap_ipv4_host_print, NULL((void*)0));
3325 g_slist_free(list);
3326}
3327
3328static void
3329sharkd_session_tap_ipv6_hosts_print(void)
3330{
3331 wmem_map_t *ipv6_hash_table = get_ipv6_hash_table();
3332 if (!ipv6_hash_table)
3333 return;
3334 GSList *list = NULL((void*)0);
3335 wmem_map_foreach(ipv6_hash_table, sharkd_session_tap_ipv6_host_insert_sorted, &list);
3336 g_slist_foreach(list, sharkd_session_tap_ipv6_host_print, NULL((void*)0));
3337 g_slist_free(list);
3338}
3339
3340/**
3341 * sharkd_session_process_tap_hosts_cb()
3342 *
3343 * Output Hosts tap:
3344 * (m) tap - tap name
3345 * (m) type - tap output type
3346 * (o) ipv4_hosts - array of objects with attributes:
3347 * (m) addr - ipv4 address
3348 * (m) name - resolved name of address
3349 * (o) ipv6_hosts - array of objects with attributes:
3350 * (m) addr - ipv6 address
3351 * (m) name - resolved name of address
3352 */
3353static void
3354sharkd_session_process_tap_hosts_cb(void *arg)
3355{
3356 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)arg;
3357 sharkd_json_object_open(NULL((void*)0));
3358 sharkd_json_value_string("tap", hosts_req->tap_name);
3359 sharkd_json_value_string("type", "hosts");
3360 if (hosts_req->dump_v4) {
3361 sharkd_json_array_open("ipv4_hosts");
3362 sharkd_session_tap_ipv4_hosts_print();
3363 sharkd_json_array_close();
3364 }
3365 if (hosts_req->dump_v6) {
3366 sharkd_json_array_open("ipv6_hosts");
3367 sharkd_session_tap_ipv6_hosts_print();
3368 sharkd_json_array_close();
3369 }
3370 sharkd_json_object_close();
3371}
3372
3373static void
3374sharkd_session_free_tap_hosts_cb(void *tapdata)
3375{
3376 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)tapdata;
3377 g_free(hosts_req);
3378}
3379
3380static GString*
3381sharkd_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)
3382{
3383 export_object_list_t *eo_object;
3384 struct sharkd_export_object_list *object_list;
3385
3386 object_list = sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type);
3387 if (object_list)
3388 {
3389 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
3390 object_list->entries = NULL((void*)0);
3391 }
3392 else
3393 {
3394 object_list = g_new(struct sharkd_export_object_list, 1)((struct sharkd_export_object_list *) g_malloc_n ((1), sizeof
(struct sharkd_export_object_list)))
;
3395 object_list->type = g_strdup(tap_type)g_strdup_inline (tap_type);
3396 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
3397 object_list->entries = NULL((void*)0);
3398 object_list->next = sharkd_eo_list;
3399 sharkd_eo_list = object_list;
3400 }
3401
3402 eo_object = g_new0(export_object_list_t, 1)((export_object_list_t *) g_malloc0_n ((1), sizeof (export_object_list_t
)))
;
3403 eo_object->add_entry = sharkd_eo_object_list_add_entry;
3404 eo_object->get_entry = sharkd_eo_object_list_get_entry;
3405 eo_object->gui_data = (void *) object_list;
3406
3407 *ptap_data = eo_object;
3408 *ptap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
3409
3410 return register_tap_listener(get_eo_tap_listener_name(eo), eo_object, tap_filter, 0, NULL((void*)0), get_eo_packet_func(eo), tap_draw, NULL((void*)0));
3411}
3412
3413/**
3414 * sharkd_session_process_tap()
3415 *
3416 * Process tap request
3417 *
3418 * Input:
3419 * (m) tap0 - First tap request
3420 * (o) tap1...tap15 - Other tap requests
3421 *
3422 * Output object with attributes:
3423 * (m) taps - array of object with attributes:
3424 * (m) tap - tap name
3425 * (m) type - tap output type
3426 * ...
3427 * for type:stats see sharkd_session_process_tap_stats_cb()
3428 * for type:nstat see sharkd_session_process_tap_nstat_cb()
3429 * for type:conv see sharkd_session_process_tap_conv_cb()
3430 * for type:host see sharkd_session_process_tap_conv_cb()
3431 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
3432 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
3433 * for type:eo see sharkd_session_process_tap_eo_cb()
3434 * for type:expert see sharkd_session_process_tap_expert_cb()
3435 * for type:rtd see sharkd_session_process_tap_rtd_cb()
3436 * for type:srt see sharkd_session_process_tap_srt_cb()
3437 * for type:flow see sharkd_session_process_tap_flow_cb()
3438 *
3439 * (m) err - error code
3440 */
3441static void
3442sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
3443{
3444 void *taps_data[16];
3445 GFreeFunc taps_free[16];
3446 int taps_count = 0;
3447 int i;
3448 const char *tap_filter = json_find_attr(buf, tokens, count, "filter");
3449
3450 rtpstream_tapinfo_t rtp_tapinfo =
3451 { NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), 0, NULL((void*)0), NULL((void*)0), 0, TAP_ANALYSE, NULL((void*)0), NULL((void*)0), NULL((void*)0), false0, false0};
3452
3453 for (i = 0; i < 16; i++)
3454 {
3455 char tapbuf[32];
3456 const char *tok_tap;
3457
3458 void *tap_data = NULL((void*)0);
3459 GFreeFunc tap_free = NULL((void*)0);
3460 GString *tap_error = NULL((void*)0);
3461
3462 snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
3463 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
3464 if (!tok_tap)
3465 break;
3466
3467 if (!strncmp(tok_tap, "stat:", 5))
3468 {
3469 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
3470 stats_tree *st;
3471
3472 if (!cfg)
3473 {
3474 sharkd_json_error(
3475 rpcid, -11001, NULL((void*)0),
3476 "sharkd_session_process_tap() stat %s not found", tok_tap + 5
3477 );
3478 return;
3479 }
3480
3481 st = stats_tree_new(cfg, NULL((void*)0), tap_filter);
3482
3483 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((void*)0));
3484
3485 if (!tap_error && cfg->init)
3486 cfg->init(st);
3487
3488 tap_data = st;
3489 tap_free = sharkd_session_free_tap_stats_cb;
3490 }
3491 else if (!strcmp(tok_tap, "expert"))
3492 {
3493 struct sharkd_expert_tap *expert_tap;
3494
3495 expert_tap = g_new0(struct sharkd_expert_tap, 1)((struct sharkd_expert_tap *) g_malloc0_n ((1), sizeof (struct
sharkd_expert_tap)))
;
3496 expert_tap->text = g_string_chunk_new(100);
3497
3498 tap_error = register_tap_listener("expert", expert_tap, tap_filter, 0, NULL((void*)0), sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb, NULL((void*)0));
3499
3500 tap_data = expert_tap;
3501 tap_free = sharkd_session_free_tap_expert_cb;
3502 }
3503 else if (!strncmp(tok_tap, "seqa:", 5))
3504 {
3505 seq_analysis_info_t *graph_analysis;
3506 register_analysis_t *analysis;
3507 const char *tap_name;
3508 tap_packet_cb tap_func;
3509 unsigned tap_flags;
3510
3511 analysis = sequence_analysis_find_by_name(tok_tap + 5);
3512 if (!analysis)
3513 {
3514 sharkd_json_error(
3515 rpcid, -11002, NULL((void*)0),
3516 "sharkd_session_process_tap() seq analysis %s not found", tok_tap + 5
3517 );
3518 return;
3519 }
3520
3521 graph_analysis = sequence_analysis_info_new();
3522 graph_analysis->name = tok_tap + 5;
3523 /* TODO, make configurable */
3524 graph_analysis->any_addr = false0;
3525
3526 tap_name = sequence_analysis_get_tap_listener_name(analysis);
3527 tap_flags = sequence_analysis_get_tap_flags(analysis);
3528 tap_func = sequence_analysis_get_packet_func(analysis);
3529
3530 tap_error = register_tap_listener(tap_name, graph_analysis, tap_filter, tap_flags, NULL((void*)0), tap_func, sharkd_session_process_tap_flow_cb, NULL((void*)0));
3531
3532 tap_data = graph_analysis;
3533 tap_free = sharkd_session_free_tap_flow_cb;
3534 }
3535 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
3536 {
3537 struct register_ct *ct = NULL((void*)0);
3538 const char *ct_tapname;
3539 struct sharkd_conv_tap_data *ct_data;
3540 tap_packet_cb tap_func = NULL((void*)0);
3541
3542 if (!strncmp(tok_tap, "conv:", 5))
3543 {
3544 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
3545
3546 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
3547 {
3548 sharkd_json_error(
3549 rpcid, -11003, NULL((void*)0),
3550 "sharkd_session_process_tap() conv %s not found", tok_tap + 5
3551 );
3552 return;
3553 }
3554 }
3555 else if (!strncmp(tok_tap, "endpt:", 6))
3556 {
3557 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
3558
3559 if (!ct || !(tap_func = get_endpoint_packet_func(ct)))
3560 {
3561 sharkd_json_error(
3562 rpcid, -11004, NULL((void*)0),
3563 "sharkd_session_process_tap() endpt %s not found", tok_tap + 6
3564 );
3565 return;
3566 }
3567 }
3568 else
3569 {
3570 sharkd_json_error(
3571 rpcid, -11005, NULL((void*)0),
3572 "sharkd_session_process_tap() conv/endpt(?): %s not found", tok_tap
3573 );
3574 return;
3575 }
3576
3577 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
3578
3579 ct_data = g_new0(struct sharkd_conv_tap_data, 1)((struct sharkd_conv_tap_data *) g_malloc0_n ((1), sizeof (struct
sharkd_conv_tap_data)))
;
3580 ct_data->type = tok_tap;
3581 ct_data->hash.user_data = ct_data;
3582
3583 /* XXX: make configurable */
3584 ct_data->resolve_name = true1;
3585 ct_data->resolve_port = true1;
3586
3587 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL((void*)0), tap_func, sharkd_session_process_tap_conv_cb, NULL((void*)0));
3588
3589 tap_data = &ct_data->hash;
3590 tap_free = sharkd_session_free_tap_conv_cb;
3591 }
3592 else if (!strncmp(tok_tap, "nstat:", 6))
3593 {
3594 stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6);
3595 stat_data_t *stat_data;
3596
3597 if (!stat_tap)
3598 {
3599 sharkd_json_error(
3600 rpcid, -11006, NULL((void*)0),
3601 "sharkd_session_process_tap() nstat=%s not found", tok_tap + 6
3602 );
3603 return;
3604 }
3605
3606 stat_tap->stat_tap_init_cb(stat_tap);
3607
3608 stat_data = g_new0(stat_data_t, 1)((stat_data_t *) g_malloc0_n ((1), sizeof (stat_data_t)));
3609 stat_data->stat_tap_data = stat_tap;
3610 stat_data->user_data = NULL((void*)0);
3611
3612 tap_error = register_tap_listener(stat_tap->tap_name, stat_data, tap_filter, 0, NULL((void*)0), stat_tap->packet_func, sharkd_session_process_tap_nstat_cb, NULL((void*)0));
3613
3614 tap_data = stat_data;
3615 tap_free = sharkd_session_free_tap_nstat_cb;
3616 }
3617 else if (!strncmp(tok_tap, "rtd:", 4))
3618 {
3619 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
3620 rtd_data_t *rtd_data;
3621 char *err;
3622
3623 if (!rtd)
3624 {
3625 sharkd_json_error(
3626 rpcid, -11007, NULL((void*)0),
3627 "sharkd_session_process_tap() rtd=%s not found", tok_tap + 4
3628 );
3629 return;
3630 }
3631
3632 rtd_table_get_filter(rtd, "", &tap_filter, &err);
3633 if (err != NULL((void*)0))
3634 {
3635 sharkd_json_error(
3636 rpcid, -11008, NULL((void*)0),
3637 "sharkd_session_process_tap() rtd=%s err=%s", tok_tap + 4, err
3638 );
3639 g_free(err);
3640 return;
3641 }
3642
3643 rtd_data = g_new0(rtd_data_t, 1)((rtd_data_t *) g_malloc0_n ((1), sizeof (rtd_data_t)));
3644 rtd_data->user_data = rtd;
3645 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL((void*)0), NULL((void*)0));
3646
3647 tap_error = register_tap_listener(get_rtd_tap_listener_name(rtd), rtd_data, tap_filter, 0, NULL((void*)0), get_rtd_packet_func(rtd), sharkd_session_process_tap_rtd_cb, NULL((void*)0));
3648
3649 tap_data = rtd_data;
3650 tap_free = sharkd_session_free_tap_rtd_cb;
3651 }
3652 else if (!strncmp(tok_tap, "srt:", 4))
3653 {
3654 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
3655 srt_data_t *srt_data;
3656 char *err;
3657
3658 if (!srt)
3659 {
3660 sharkd_json_error(
3661 rpcid, -11009, NULL((void*)0),
3662 "sharkd_session_process_tap() srt=%s not found", tok_tap + 4
3663 );
3664 return;
3665 }
3666
3667 srt_table_get_filter(srt, "", &tap_filter, &err);
3668 if (err != NULL((void*)0))
3669 {
3670 sharkd_json_error(
3671 rpcid, -11010, NULL((void*)0),
3672 "sharkd_session_process_tap() srt=%s err=%s", tok_tap + 4, err
3673 );
3674 g_free(err);
3675 return;
3676 }
3677
3678 srt_data = g_new0(srt_data_t, 1)((srt_data_t *) g_malloc0_n ((1), sizeof (srt_data_t)));
3679 srt_data->srt_array = g_array_new(FALSE(0), TRUE(!(0)), sizeof(srt_stat_table *));
3680 srt_data->user_data = srt;
3681 srt_table_dissector_init(srt, srt_data->srt_array);
3682
3683 tap_error = register_tap_listener(get_srt_tap_listener_name(srt), srt_data, tap_filter, 0, NULL((void*)0), get_srt_packet_func(srt), sharkd_session_process_tap_srt_cb, NULL((void*)0));
3684
3685 tap_data = srt_data;
3686 tap_free = sharkd_session_free_tap_srt_cb;
3687 }
3688 else if (!strncmp(tok_tap, "eo:", 3))
3689 {
3690 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
3691
3692 if (!eo)
3693 {
3694 sharkd_json_error(
3695 rpcid, -11011, NULL((void*)0),
3696 "sharkd_session_process_tap() eo=%s not found", tok_tap + 3
3697 );
3698 return;
3699 }
3700
3701 tap_error = sharkd_session_eo_register_tap_listener(eo, tok_tap, tap_filter, sharkd_session_process_tap_eo_cb, &tap_data, &tap_free);
3702
3703 /* tap_data & tap_free assigned by sharkd_session_eo_register_tap_listener */
3704 }
3705 else if (!strcmp(tok_tap, "rtp-streams"))
3706 {
3707 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet_cb, sharkd_session_process_tap_rtp_cb, NULL((void*)0));
3708
3709 tap_data = &rtp_tapinfo;
3710 tap_free = rtpstream_reset_cb;
3711 }
3712 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
3713 {
3714 struct sharkd_analyse_rtp *rtp_req;
3715
3716 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
3717 if (!sharkd_rtp_match_init(&rtp_req->id, tok_tap + 12))
3718 {
3719 rtpstream_id_free(&rtp_req->id);
3720 g_free(rtp_req);
3721 continue;
3722 }
3723
3724 rtp_req->tap_name = tok_tap;
3725 rtp_req->statinfo.first_packet = true1;
3726 rtp_req->statinfo.reg_pt = PT_UNDEFINED-1;
3727
3728 tap_error = register_tap_listener("rtp", rtp_req, tap_filter, 0, NULL((void*)0), sharkd_session_packet_tap_rtp_analyse_cb, sharkd_session_process_tap_rtp_analyse_cb, NULL((void*)0));
3729
3730 tap_data = rtp_req;
3731 tap_free = sharkd_session_process_tap_rtp_free_cb;
3732 }
3733 else if (!strcmp(tok_tap, "multicast"))
3734 {
3735 mcaststream_tapinfo_t *mcaststream_tapinfo;
3736 mcaststream_tapinfo = (mcaststream_tapinfo_t *) g_malloc0(sizeof(*mcaststream_tapinfo));
3737
3738 tap_error = register_tap_listener("udp", mcaststream_tapinfo, tap_filter, 0, NULL((void*)0), mcaststream_packet, sharkd_session_process_tap_multicast_cb, NULL((void*)0));
3739 tap_data = mcaststream_tapinfo;
3740 tap_free = sharkd_session_process_free_tap_multicast_cb;
3741 }
3742 else if (!strcmp(tok_tap, "phs"))
3743 {
3744 phs_t *rs;
3745
3746 pc_proto_id = proto_registrar_get_id_byname("pkt_comment");
3747
3748 rs = new_phs_t(NULL((void*)0), tap_filter);
3749
3750 tap_error = register_tap_listener("frame", rs, tap_filter,
3751 TL_REQUIRES_PROTO_TREE0x00000001|TL_REQUIRES_PROTOCOLS0x00000020,
3752 NULL((void*)0), protohierstat_packet,
3753 sharkd_session_process_tap_phs_cb, NULL((void*)0));
3754
3755 tap_data = rs;
3756 tap_free = sharkd_session_free_tap_phs_cb;
3757 }
3758 else if (!strcmp(tok_tap, "voip-calls"))
3759 {
3760 voip_stat_init_tapinfo();
3761
3762 tap_error = register_tap_listener("frame", &tapinfo_, tap_filter, 0, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_voip_calls_cb, NULL((void*)0));
3763
3764 tapinfo_.session = cfile.epan;
3765 voip_calls_init_all_taps(&tapinfo_);
3766
3767 tap_data = &tapinfo_;
3768 tap_free = sharkd_session_free_tap_voip_calls_cb;
3769 }
3770 else if (!strncmp(tok_tap, "voip-convs:", 11))
3771 {
3772 int len;
3773 unsigned int min, max;
3774 struct sharkd_voip_convs_req *voip_convs_req;
3775 const char *conv_arg = tok_tap + 11;
3776
3777 // parse tok_tap to get which call we are asking for
3778 if (*conv_arg == 0) {
3779 // set all bits of voip_conv_sel (-1 in binary is all 1's)
3780 memset(voip_conv_sel, -1, sizeof(voip_conv_sel));
3781 } else {
3782 memset(voip_conv_sel, 0, sizeof(voip_conv_sel));
3783
3784 while (*conv_arg != 0) {
3785 if (*conv_arg == ',') {
3786 conv_arg++;
3787 }
3788 if (sscanf(conv_arg, "%u-%u%n", &min, &max, &len) == 2) {
3789 conv_arg += len;
3790 } else if (sscanf(conv_arg, "%u%n", &min, &len) == 1) {
3791 max = min;
3792 conv_arg += len;
3793 } else {
3794 sharkd_json_error(
3795 rpcid, -11014, NULL((void*)0),
3796 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' parameter", tok_tap
3797 );
3798 return;
3799 }
3800 if (min > max || min >= VOIP_CONV_MAX((sizeof(int) * 8) * ((1<<(sizeof(uint16_t) * 8))/(sizeof
(int) * 8)))
|| max >= VOIP_CONV_MAX((sizeof(int) * 8) * ((1<<(sizeof(uint16_t) * 8))/(sizeof
(int) * 8)))
) {
3801 sharkd_json_error(
3802 rpcid, -11012, NULL((void*)0),
3803 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' number range", tok_tap
3804 );
3805 return;
3806 }
3807 for(; min <= max; min++) {
3808 voip_conv_sel[min / VOIP_CONV_BITS(sizeof(int) * 8)] |= 1 << (min % VOIP_CONV_BITS(sizeof(int) * 8));
3809 }
3810 }
3811 }
3812
3813 voip_stat_init_tapinfo();
3814
3815 voip_convs_req = (struct sharkd_voip_convs_req *) g_malloc0(sizeof(*voip_convs_req));
3816 voip_convs_req->tapinfo = &tapinfo_;
3817 voip_convs_req->tap_name = tok_tap;
3818
3819 tap_error = register_tap_listener("frame", voip_convs_req, tap_filter, 0, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_voip_convs_cb, NULL((void*)0));
3820
3821 tapinfo_.session = cfile.epan;
3822 voip_calls_init_all_taps(&tapinfo_);
3823
3824 tap_data = voip_convs_req;
3825 tap_free = sharkd_session_free_tap_voip_convs_cb;
3826 }
3827 else if (!strncmp(tok_tap, "hosts:", 6))
3828 {
3829 bool_Bool dump_v4;
3830 bool_Bool dump_v6;
3831 struct sharkd_hosts_req *hosts_req;
3832 const char *proto_arg;
3833 char **proto_tokens;
3834 int proto_count;
3835
3836 proto_arg = tok_tap + 6;
3837
3838 if (strlen(proto_arg) == 0) {
3839 dump_v4 = true1;
3840 dump_v6 = true1;
3841 } else {
3842 dump_v4 = false0;
3843 dump_v6 = false0;
3844
3845 proto_tokens = g_strsplit(proto_arg, ",", 0);
3846 proto_count = 0;
3847 while (proto_tokens[proto_count]) {
3848 if (!strcmp("ip", proto_tokens[proto_count]) ||
3849 !strcmp("ipv4", proto_tokens[proto_count])) {
3850 dump_v4 = true1;
3851 } else if (!strcmp("ipv6", proto_tokens[proto_count])) {
3852 dump_v6 = true1;
3853 } else {
3854 g_strfreev(proto_tokens);
3855 sharkd_json_error(
3856 rpcid, -11015, NULL((void*)0),
3857 "sharkd_session_process_tap() hosts=%s invalid 'protos' parameter", tok_tap
3858 );
3859 return;
3860 }
3861 proto_count++;
3862 }
3863 g_strfreev(proto_tokens);
3864 }
3865
3866 hosts_req = (struct sharkd_hosts_req *)g_malloc0(sizeof(*hosts_req));
3867 hosts_req->dump_v4 = dump_v4;
3868 hosts_req->dump_v6 = dump_v6;
3869 hosts_req->tap_name = tok_tap;
3870
3871 tap_error = register_tap_listener("frame", hosts_req, tap_filter, TL_REQUIRES_PROTO_TREE0x00000001, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_hosts_cb, NULL((void*)0));
3872
3873 tap_data = hosts_req;
3874 tap_free = sharkd_session_free_tap_hosts_cb;
3875 }
3876 else
3877 {
3878 sharkd_json_error(
3879 rpcid, -11012, NULL((void*)0),
3880 "sharkd_session_process_tap() %s not recognized", tok_tap
3881 );
3882 return;
3883 }
3884
3885 if (tap_error)
3886 {
3887 sharkd_json_error(
3888 rpcid, -11013, NULL((void*)0),
3889 "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str
3890 );
3891 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
3892 if (tap_free)
3893 tap_free(tap_data);
3894 return;
3895 }
3896
3897 taps_data[taps_count] = tap_data;
3898 taps_free[taps_count] = tap_free;
3899 taps_count++;
3900 }
3901
3902 fprintf(stderrstderr, "sharkd_session_process_tap() count=%d\n", taps_count);
3903 if (taps_count == 0)
3904 {
3905 sharkd_json_result_prologue(rpcid);
3906 sharkd_json_array_open("taps");
3907 sharkd_json_array_close();
3908 sharkd_json_result_epilogue();
3909 return;
3910 }
3911
3912 sharkd_json_result_prologue(rpcid);
3913 sharkd_json_array_open("taps");
3914 sharkd_retap();
3915 sharkd_json_array_close();
3916 sharkd_json_result_epilogue();
3917
3918 for (i = 0; i < taps_count; i++)
3919 {
3920 if (taps_data[i])
3921 remove_tap_listener(taps_data[i]);
3922
3923 if (taps_free[i])
3924 taps_free[i](taps_data[i]);
3925 }
3926}
3927
3928/**
3929 * sharkd_session_process_follow()
3930 *
3931 * Process follow request
3932 *
3933 * Input:
3934 * (m) follow - follow protocol request (e.g. HTTP)
3935 * (m) filter - filter request (e.g. tcp.stream == 1)
3936 * (m) stream - stream index number
3937 * (o) sub_stream - follow sub-stream index number (e.g. for HTTP/2 and QUIC streams)
3938 *
3939 * Output object with attributes:
3940 *
3941 * (m) err - error code
3942 * (m) shost - server host
3943 * (m) sport - server port
3944 * (m) sbytes - server send bytes count
3945 * (m) chost - client host
3946 * (m) cport - client port
3947 * (m) cbytes - client send bytes count
3948 * (o) payloads - array of object with attributes:
3949 * (o) s - set if server sent, else client
3950 * (m) n - packet number
3951 * (m) d - data base64 encoded
3952 */
3953static void
3954sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
3955{
3956 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
3957 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
3958 const char *tok_sub_stream = json_find_attr(buf, tokens, count, "sub_stream");
3959
3960 register_follow_t *follower;
3961 GString *tap_error;
3962
3963 follow_info_t *follow_info;
3964 const char *host;
3965 char *port;
3966
3967 follower = get_follow_by_name(tok_follow);
3968 if (!follower)
3969 {
3970 sharkd_json_error(
3971 rpcid, -12001, NULL((void*)0),
3972 "sharkd_session_process_follow() follower=%s not found", tok_follow
3973 );
3974 return;
3975 }
3976
3977 uint64_t substream_id = SUBSTREAM_UNUSED0xFFFFFFFFFFFFFFFFUL;
3978 if (tok_sub_stream)
3979 {
3980 ws_strtou64(tok_sub_stream, NULL((void*)0), &substream_id);
3981 }
3982
3983 /* follow_reset_stream ? */
3984 follow_info = g_new0(follow_info_t, 1)((follow_info_t *) g_malloc0_n ((1), sizeof (follow_info_t)));
3985 follow_info->substream_id = substream_id;
3986 /* gui_data, filter_out_filter not set, but not used by dissector */
3987
3988 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL((void*)0), get_follow_tap_handler(follower), NULL((void*)0), NULL((void*)0));
3989 if (tap_error)
3990 {
3991 sharkd_json_error(
3992 rpcid, -12002, NULL((void*)0),
3993 "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str
3994 );
3995 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
3996 g_free(follow_info);
3997 return;
3998 }
3999
4000 sharkd_retap();
4001
4002 sharkd_json_result_prologue(rpcid);
4003
4004 /* Server information: hostname, port, bytes sent */
4005 host = address_to_name(&follow_info->server_ip);
4006 sharkd_json_value_string("shost", host);
4007
4008 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->server_port);
4009 sharkd_json_value_string("sport", port);
4010 wmem_free(NULL((void*)0), port);
4011
4012 sharkd_json_value_anyf("sbytes", "%u", follow_info->bytes_written[0]);
4013
4014 /* Client information: hostname, port, bytes sent */
4015 host = address_to_name(&follow_info->client_ip);
4016 sharkd_json_value_string("chost", host);
4017
4018 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->client_port);
4019 sharkd_json_value_string("cport", port);
4020 wmem_free(NULL((void*)0), port);
4021
4022 sharkd_json_value_anyf("cbytes", "%u", follow_info->bytes_written[1]);
4023
4024 if (follow_info->payload)
4025 {
4026 follow_record_t *follow_record;
4027 GList *cur;
4028
4029 sharkd_json_array_open("payloads");
4030 for (cur = g_list_last(follow_info->payload); cur; cur = g_list_previous(cur)((cur) ? (((GList *)(cur))->prev) : ((void*)0)))
4031 {
4032 follow_record = (follow_record_t *) cur->data;
4033
4034 json_dumper_begin_object(&dumper);
4035
4036 sharkd_json_value_anyf("n", "%u", follow_record->packet_num);
4037 sharkd_json_value_base64("d", follow_record->data->data, follow_record->data->len);
4038
4039 if (follow_record->is_server)
4040 sharkd_json_value_anyf("s", "%d", 1);
4041
4042 json_dumper_end_object(&dumper);
4043 }
4044 sharkd_json_array_close();
4045 }
4046
4047 sharkd_json_result_epilogue();
4048
4049 remove_tap_listener(follow_info);
4050 follow_info_free(follow_info);
4051}
4052
4053static void
4054sharkd_session_process_frame_cb_tree(const char *key, epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs, bool_Bool display_hidden)
4055{
4056 proto_node *node;
4057
4058 sharkd_json_array_open(key);
4059 for (node = tree->first_child; node; node = node->next)
4060 {
4061 field_info *finfo = PNODE_FINFO(node)((node)->finfo);
4062
4063 if (!finfo)
4064 continue;
4065
4066 if (!display_hidden && FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4067 continue;
4068
4069 json_dumper_begin_object(&dumper);
4070
4071 if (!finfo->rep)
4072 {
4073 char label_str[ITEM_LABEL_LENGTH240];
4074
4075 label_str[0] = '\0';
4076 proto_item_fill_label(finfo, label_str, NULL((void*)0));
4077 sharkd_json_value_string("l", label_str);
4078 }
4079 else
4080 {
4081 sharkd_json_value_string("l", finfo->rep->representation);
4082 }
4083
4084 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
4085 {
4086 int idx;
4087
4088 for (idx = 1; tvbs[idx]; idx++)
4089 {
4090 if (tvbs[idx] == finfo->ds_tvb)
4091 {
4092 sharkd_json_value_anyf("ds", "%d", idx);
4093 break;
4094 }
4095 }
4096 }
4097
4098 if (finfo->start >= 0 && finfo->length > 0)
4099 sharkd_json_value_anyf("h", "[%d,%d]", finfo->start, finfo->length);
4100
4101 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
4102 sharkd_json_value_anyf("i", "[%d,%d]", finfo->appendix_start, finfo->appendix_length);
4103
4104
4105 if (finfo->hfinfo)
4106 {
4107 char *filter;
4108
4109 if (finfo->hfinfo->type == FT_PROTOCOL)
4110 {
4111 sharkd_json_value_string("t", "proto");
4112 }
4113 else if (finfo->hfinfo->type == FT_FRAMENUM)
4114 {
4115 sharkd_json_value_string("t", "framenum");
4116 sharkd_json_value_anyf("fnum", "%u", fvalue_get_uinteger(finfo->value));
4117 }
4118 else if (FI_GET_FLAG(finfo, FI_URL)((finfo) ? ((finfo)->flags & (0x00000004)) : 0) && FT_IS_STRING(finfo->hfinfo->type)((finfo->hfinfo->type) == FT_STRING || (finfo->hfinfo
->type) == FT_STRINGZ || (finfo->hfinfo->type) == FT_STRINGZPAD
|| (finfo->hfinfo->type) == FT_STRINGZTRUNC || (finfo->
hfinfo->type) == FT_UINT_STRING || (finfo->hfinfo->type
) == FT_AX25)
)
4119 {
4120 char *url = fvalue_to_string_repr(NULL((void*)0), finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
4121
4122 sharkd_json_value_string("t", "url");
4123 sharkd_json_value_string("url", url);
4124 wmem_free(NULL((void*)0), url);
4125 }
4126
4127 filter = proto_construct_match_selected_string(finfo, edt);
4128 if (filter)
4129 {
4130 sharkd_json_value_string("f", filter);
4131 wmem_free(NULL((void*)0), filter);
4132 }
4133
4134 if (finfo->hfinfo->abbrev)
4135 sharkd_json_value_string("fn", finfo->hfinfo->abbrev);
4136 }
4137
4138 if (FI_GET_FLAG(finfo, FI_GENERATED)((finfo) ? ((finfo)->flags & (0x00000002)) : 0))
4139 sharkd_json_value_anyf("g", "true");
4140
4141 if (FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4142 sharkd_json_value_anyf("v", "true");
4143
4144 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0))
4145 {
4146 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0), expert_severity_vals);
4147
4148 ws_assert(severity != NULL)do { if ((1) && !(severity != ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "sharkd_session.c", 4148, __func__, "assertion failed: %s"
, "severity != ((void*)0)"); } while (0)
;
4149
4150 sharkd_json_value_string("s", severity);
4151 }
4152
4153 if (((proto_tree *) node)->first_child)
4154 {
4155 if (finfo->tree_type != -1)
4156 sharkd_json_value_anyf("e", "%d", finfo->tree_type);
4157
4158 sharkd_session_process_frame_cb_tree("n", edt, (proto_tree *) node, tvbs, display_hidden);
4159 }
4160
4161 json_dumper_end_object(&dumper);
4162 }
4163 sharkd_json_array_close();
4164}
4165
4166static bool_Bool
4167sharkd_follower_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4168{
4169 register_follow_t *follower = (register_follow_t *) value;
4170 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4171 packet_info *pi = &edt->pi;
4172
4173 const int proto_id = get_follow_proto_id(follower);
4174
4175 uint32_t ignore_stream;
4176 uint32_t ignore_sub_stream;
4177
4178 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4179 {
4180 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4181 char *follow_filter;
4182
4183 follow_filter = get_follow_conv_func(follower)(edt, pi, &ignore_stream, &ignore_sub_stream);
4184
4185 json_dumper_begin_array(&dumper);
4186 json_dumper_value_string(&dumper, layer_proto);
4187 json_dumper_value_string(&dumper, follow_filter);
4188 json_dumper_end_array(&dumper);
4189
4190 g_free(follow_filter);
4191 }
4192
4193 return false0;
4194}
4195
4196static bool_Bool
4197sharkd_followers_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4198{
4199 register_follow_t *follower = (register_follow_t *) value;
4200 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4201 packet_info *pi = &edt->pi;
4202
4203 const int proto_id = get_follow_proto_id(follower);
4204
4205 unsigned stream;
4206 unsigned sub_stream;
4207
4208 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4209 {
4210 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4211 char *follow_filter;
4212
4213 follow_filter = get_follow_conv_func(follower)(edt, pi, &stream, &sub_stream);
4214
4215 sharkd_json_object_open(NULL((void*)0));
4216 sharkd_json_value_string("protocol", layer_proto);
4217 sharkd_json_value_string("filter", follow_filter);
4218 if (get_follow_stream_count_func(follower) != NULL((void*)0))
4219 {
4220 sharkd_json_value_anyf("stream", "%u", stream);
4221 }
4222 if (get_follow_sub_stream_id_func(follower) != NULL((void*)0))
4223 {
4224 sharkd_json_value_anyf("sub_stream", "%u", sub_stream);
4225 }
4226 sharkd_json_object_close();
4227
4228 g_free(follow_filter);
4229 }
4230
4231 return false0;
4232}
4233
4234struct sharkd_frame_request_data
4235{
4236 bool_Bool display_hidden;
4237};
4238
4239static void
4240sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
4241{
4242 packet_info *pi = &edt->pi;
4243 frame_data *fdata = pi->fd;
4244 wtap_block_t pkt_block = NULL((void*)0);
4245
4246 const struct sharkd_frame_request_data * const req_data = (const struct sharkd_frame_request_data * const) data;
4247 const bool_Bool display_hidden = (req_data) ? req_data->display_hidden : false0;
4248
4249 sharkd_json_result_prologue(rpcid);
4250
4251 if (fdata->has_modified_block)
4252 pkt_block = sharkd_get_modified_block(fdata);
4253 else
4254 pkt_block = pi->rec->block;
4255
4256 if (pkt_block)
4257 {
4258 unsigned i;
4259 unsigned n;
4260 char *comment;
4261
4262 n = wtap_block_count_option(pkt_block, OPT_COMMENT1);
4263
4264 sharkd_json_array_open("comment");
4265 for (i = 0; i < n; i++) {
4266 if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment)) {
4267 sharkd_json_value_string(NULL((void*)0), comment);
4268 }
4269 }
4270 sharkd_json_array_close();
4271 }
4272
4273 if (tree)
4274 {
4275 tvbuff_t **tvbs = NULL((void*)0);
4276
4277 /* arrayize data src, to speedup searching for ds_tvb index */
4278 if (data_src && data_src->next /* only needed if there are more than one data source */)
4279 {
4280 unsigned count = g_slist_length((GSList *) data_src);
4281 unsigned i;
4282
4283 tvbs = (tvbuff_t **) g_malloc0((count + 1) * sizeof(*tvbs));
4284
4285 for (i = 0; i < count; i++)
4286 {
4287 const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i);
4288
4289 tvbs[i] = get_data_source_tvb(src);
4290 }
4291
4292 tvbs[count] = NULL((void*)0);
4293 }
4294
4295 sharkd_session_process_frame_cb_tree("tree", edt, tree, tvbs, display_hidden);
4296
4297 g_free(tvbs);
4298 }
4299
4300 if (cinfo)
4301 {
4302 int col;
4303
4304 sharkd_json_array_open("col");
4305 for (col = 0; col < cinfo->num_cols; ++col)
4306 {
4307 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
4308 }
4309 sharkd_json_array_close();
4310 }
4311
4312 if (fdata->ignored)
4313 sharkd_json_value_anyf("i", "true");
4314
4315 if (fdata->marked)
4316 sharkd_json_value_anyf("m", "true");
4317
4318 if (fdata->color_filter)
4319 {
4320 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
4321 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
4322 }
4323
4324 if (data_src)
4325 {
4326 struct data_source *src = (struct data_source *) data_src->data;
4327 bool_Bool ds_open = false0;
4328
4329 tvbuff_t *tvb;
4330 unsigned length;
4331
4332 tvb = get_data_source_tvb(src);
4333 length = tvb_captured_length(tvb);
4334
4335 if (length != 0)
4336 {
4337 const unsigned char *cp = tvb_get_ptr(tvb, 0, length);
4338
4339 /* XXX pi.fd->encoding */
4340 sharkd_json_value_base64("bytes", cp, length);
4341 }
4342 else
4343 {
4344 sharkd_json_value_base64("bytes", "", 0);
4345 }
4346
4347 data_src = data_src->next;
4348 if (data_src)
4349 {
4350 sharkd_json_array_open("ds");
4351 ds_open = true1;
4352 }
4353
4354 while (data_src)
4355 {
4356 src = (struct data_source *) data_src->data;
4357
4358 json_dumper_begin_object(&dumper);
4359
4360 {
4361 char *src_name = get_data_source_name(src);
4362
4363 sharkd_json_value_string("name", src_name);
4364 wmem_free(NULL((void*)0), src_name);
4365 }
4366
4367 tvb = get_data_source_tvb(src);
4368 length = tvb_captured_length(tvb);
4369
4370 if (length != 0)
4371 {
4372 const unsigned char *cp = tvb_get_ptr(tvb, 0, length);
4373
4374 /* XXX pi.fd->encoding */
4375 sharkd_json_value_base64("bytes", cp, length);
4376 }
4377 else
4378 {
4379 sharkd_json_value_base64("bytes", "", 0);
4380 }
4381
4382 json_dumper_end_object(&dumper);
4383
4384 data_src = data_src->next;
4385 }
4386
4387 /* close ds, only if was opened */
4388 if (ds_open)
4389 sharkd_json_array_close();
4390 }
4391
4392 sharkd_json_array_open("fol");
4393 follow_iterate_followers(sharkd_follower_visit_layers_cb, edt);
4394 sharkd_json_array_close();
4395
4396 sharkd_json_array_open("followers");
4397 follow_iterate_followers(sharkd_followers_visit_layers_cb, edt);
4398 sharkd_json_array_close();
4399
4400 sharkd_json_result_epilogue();
4401}
4402
4403#define SHARKD_IOGRAPH_MAX_ITEMS1 << 25 1 << 25 /* 33,554,432 limit of items, same as max_io_items_ in ui/qt/io_graph_dialog.h */
4404
4405struct sharkd_iograph
4406{
4407 /* config */
4408 int hf_index;
4409 io_graph_item_unit_t calc_type;
4410 uint32_t interval;
4411 bool_Bool aot;
4412
4413 /* result */
4414 int space_items;
4415 int num_items;
4416 io_graph_item_t *items;
4417 GString *error;
4418};
4419
4420static tap_packet_status
4421sharkd_iograph_packet(void *g, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U___attribute__((unused)), tap_flags_t flags _U___attribute__((unused)))
4422{
4423 struct sharkd_iograph *graph = (struct sharkd_iograph *) g;
4424 int idx;
4425 bool_Bool update_succeeded;
4426
4427 int64_t tmp_idx = get_io_graph_index(pinfo, graph->interval);
4428 if (tmp_idx < 0 || tmp_idx >= SHARKD_IOGRAPH_MAX_ITEMS1 << 25)
1
Assuming 'tmp_idx' is >= 0
2
Assuming the condition is false
3
Taking false branch
4429 return TAP_PACKET_DONT_REDRAW;
4430
4431 idx = (int)tmp_idx;
4432
4433 if (idx + 1 > graph->num_items)
4
Assuming the condition is true
5
Taking true branch
4434 {
4435 if (idx + 1 > graph->space_items)
6
Assuming the condition is false
7
Taking false branch
4436 {
4437 int new_size = idx + 1024;
4438
4439 graph->items = (io_graph_item_t *) g_realloc(graph->items, sizeof(io_graph_item_t) * new_size);
4440 reset_io_graph_items(&graph->items[graph->space_items], new_size - graph->space_items, graph->hf_index);
4441
4442 graph->space_items = new_size;
4443 }
4444 else if (graph->items == NULL((void*)0))
8
Assuming field 'items' is equal to NULL
9
Taking true branch
4445 {
4446 graph->items = g_new(io_graph_item_t, graph->space_items)((io_graph_item_t *) g_malloc_n ((graph->space_items), sizeof
(io_graph_item_t)))
;
10
Uninitialized value stored to field 'first_frame_in_invl'
4447 reset_io_graph_items(graph->items, graph->space_items, graph->hf_index);
11
Calling 'reset_io_graph_items'
14
Returning from 'reset_io_graph_items'
4448 }
4449
4450 graph->num_items = idx + 1;
4451 }
4452
4453 update_succeeded = update_io_graph_item(graph->items, idx, pinfo, edt, graph->hf_index, graph->calc_type, graph->interval);
15
Calling 'update_io_graph_item'
4454 /* XXX - TAP_PACKET_FAILED if the item couldn't be updated, with an error message? */
4455 return update_succeeded ? TAP_PACKET_REDRAW : TAP_PACKET_DONT_REDRAW;
4456}
4457
4458/**
4459 * sharkd_session_process_iograph()
4460 *
4461 * Process iograph request
4462 *
4463 * Input:
4464 * (o) interval - interval time, if not specified: 1000
4465 * (o) interval_units - units for interval time, must be 's', 'ms' or 'us', if not specified: ms
4466 * (m) graph0 - First graph request
4467 * (o) graph1...graph9 - Other graph requests
4468 * (o) filter0 - First graph filter
4469 * (o) filter1...filter9 - Other graph filters
4470 *
4471 * Graph requests can be one of: "packets", "bytes", "bits", "sum:<field>", "frames:<field>", "max:<field>", "min:<field>", "avg:<field>", "load:<field>",
4472 * if you use variant with <field>, you need to pass field name in filter request.
4473 *
4474 * Output object with attributes:
4475 * (m) iograph - array of graph results with attributes:
4476 * errmsg - graph cannot be constructed
4477 * items - graph values, zeros are skipped, if value is not a number it's next index encoded as hex string
4478 */
4479static void
4480sharkd_session_process_iograph(char *buf, const jsmntok_t *tokens, int count)
4481{
4482 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4483 const char *tok_interval_units = json_find_attr(buf, tokens, count, "interval_units");
4484 struct sharkd_iograph graphs[10];
4485 bool_Bool is_any_ok = false0;
4486 int graph_count;
4487
4488 int i;
4489
4490 /* default: 1000ms = one per second */
4491 uint32_t interval = 1000;
4492 const char *interval_units = "ms";
4493
4494 if (tok_interval)
4495 ws_strtou32(tok_interval, NULL((void*)0), &interval);
4496
4497 if (tok_interval_units)
4498 {
4499 if (strcmp(tok_interval_units, "us") != 0 &&
4500 strcmp(tok_interval_units, "ms") != 0 &&
4501 strcmp(tok_interval_units, "s") != 0)
4502 {
4503 sharkd_json_error(
4504 rpcid, -7003, NULL((void*)0),
4505 "Invalid interval_units parameter: '%s', must be 's', 'ms' or 'us'", tok_interval_units
4506 );
4507 return;
4508 }
4509 interval_units = tok_interval_units;
4510 }
4511
4512 uint32_t interval_us = 0;
4513 if (strcmp(interval_units, "us") == 0)
4514 {
4515 interval_us = interval;
4516 }
4517 else if (strcmp(interval_units, "ms") == 0)
4518 {
4519 interval_us = 1000 * interval;
4520 }
4521 else if (strcmp(interval_units, "s") == 0)
4522 {
4523 interval_us = 1000000 * interval;
4524 }
4525
4526 for (i = graph_count = 0; i < (int) G_N_ELEMENTS(graphs)(sizeof (graphs) / sizeof ((graphs)[0])); i++)
4527 {
4528 struct sharkd_iograph *graph = &graphs[graph_count];
4529
4530 const char *tok_graph;
4531 const char *tok_filter;
4532 char tok_format_buf[32];
4533 const char *field_name;
4534 const char *tok_aot;
4535
4536 snprintf(tok_format_buf, sizeof(tok_format_buf), "graph%d", i);
4537 tok_graph = json_find_attr(buf, tokens, count, tok_format_buf);
4538 if (!tok_graph)
4539 break;
4540
4541 snprintf(tok_format_buf, sizeof(tok_format_buf), "filter%d", i);
4542 tok_filter = json_find_attr(buf, tokens, count, tok_format_buf);
4543
4544 if (!strcmp(tok_graph, "packets"))
4545 graph->calc_type = IOG_ITEM_UNIT_PACKETS;
4546 else if (!strcmp(tok_graph, "bytes"))
4547 graph->calc_type = IOG_ITEM_UNIT_BYTES;
4548 else if (!strcmp(tok_graph, "bits"))
4549 graph->calc_type = IOG_ITEM_UNIT_BITS;
4550 else if (g_str_has_prefix(tok_graph, "sum:")(__builtin_constant_p ("sum:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("sum:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "sum:") )
)
4551 graph->calc_type = IOG_ITEM_UNIT_CALC_SUM;
4552 else if (g_str_has_prefix(tok_graph, "frames:")(__builtin_constant_p ("frames:")? __extension__ ({ const char
* const __str = (tok_graph); const char * const __prefix = (
"frames:"); gboolean __result = (0); if (__str == ((void*)0) ||
__prefix == ((void*)0)) __result = (g_str_has_prefix) (__str
, __prefix); else { const size_t __str_len = strlen (((__str)
+ !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "frames:"
) )
)
4553 graph->calc_type = IOG_ITEM_UNIT_CALC_FRAMES;
4554 else if (g_str_has_prefix(tok_graph, "fields:")(__builtin_constant_p ("fields:")? __extension__ ({ const char
* const __str = (tok_graph); const char * const __prefix = (
"fields:"); gboolean __result = (0); if (__str == ((void*)0) ||
__prefix == ((void*)0)) __result = (g_str_has_prefix) (__str
, __prefix); else { const size_t __str_len = strlen (((__str)
+ !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "fields:"
) )
)
4555 graph->calc_type = IOG_ITEM_UNIT_CALC_FIELDS;
4556 else if (g_str_has_prefix(tok_graph, "max:")(__builtin_constant_p ("max:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("max:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "max:") )
)
4557 graph->calc_type = IOG_ITEM_UNIT_CALC_MAX;
4558 else if (g_str_has_prefix(tok_graph, "min:")(__builtin_constant_p ("min:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("min:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "min:") )
)
4559 graph->calc_type = IOG_ITEM_UNIT_CALC_MIN;
4560 else if (g_str_has_prefix(tok_graph, "avg:")(__builtin_constant_p ("avg:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("avg:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "avg:") )
)
4561 graph->calc_type = IOG_ITEM_UNIT_CALC_AVERAGE;
4562 else if (g_str_has_prefix(tok_graph, "load:")(__builtin_constant_p ("load:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("load:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "load:") )
)
4563 graph->calc_type = IOG_ITEM_UNIT_CALC_LOAD;
4564 else if (g_str_has_prefix(tok_graph, "throughput:")(__builtin_constant_p ("throughput:")? __extension__ ({ const
char * const __str = (tok_graph); const char * const __prefix
= ("throughput:"); gboolean __result = (0); if (__str == ((void
*)0) || __prefix == ((void*)0)) __result = (g_str_has_prefix)
(__str, __prefix); else { const size_t __str_len = strlen ((
(__str) + !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "throughput:"
) )
)
4565 graph->calc_type = IOG_ITEM_UNIT_CALC_THROUGHPUT;
4566 else
4567 break;
4568
4569 field_name = strchr(tok_graph, ':');
4570 if (field_name)
4571 field_name = field_name + 1;
4572
4573 /* io_graph_item now supports microseconds (and this parameter
4574 * is expected to be in microseconds.) */
4575 graph->interval = interval_us;
4576
4577 graph->hf_index = -1;
4578 graph->error = check_field_unit(field_name, &graph->hf_index, graph->calc_type);
4579
4580 graph->space_items = 0; /* TODO, can avoid realloc()s in sharkd_iograph_packet() by calculating: capture_time / interval */
4581 graph->num_items = 0;
4582 graph->items = NULL((void*)0);
4583
4584 snprintf(tok_format_buf, sizeof(tok_format_buf), "aot%d", i);
4585 tok_aot = json_find_attr(buf, tokens, count, tok_format_buf);
4586 if (tok_aot!=NULL((void*)0)) {
4587 graph->aot = (!strcmp(tok_aot, "true")) ? true1 : false0;
4588 }
4589 else {
4590 graph->aot = false0;
4591 }
4592
4593 if (!graph->error)
4594 graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE0x00000001, NULL((void*)0), sharkd_iograph_packet, NULL((void*)0), NULL((void*)0));
4595
4596 graph_count++;
4597
4598 if (graph->error)
4599 {
4600 sharkd_json_error(
4601 rpcid, -6001, NULL((void*)0),
4602 "%s", graph->error->str
4603 );
4604 g_string_free(graph->error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(graph->error), ((!(0)))) : g_string_free_and_steal (graph
->error)) : (g_string_free) ((graph->error), ((!(0)))))
;
4605 return;
4606 }
4607
4608 if (graph->error == NULL((void*)0))
4609 is_any_ok = true1;
4610 }
4611
4612 /* retap only if we have at least one ok */
4613 if (is_any_ok)
4614 sharkd_retap();
4615
4616 sharkd_json_result_prologue(rpcid);
4617
4618 sharkd_json_array_open("iograph");
4619 for (i = 0; i < graph_count; i++)
4620 {
4621 struct sharkd_iograph *graph = &graphs[i];
4622
4623 json_dumper_begin_object(&dumper);
4624
4625 if (graph->error)
4626 {
4627 fprintf(stderrstderr, "SNAP 6002 - we should never get to here.\n");
4628 g_string_free(graph->error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(graph->error), ((!(0)))) : g_string_free_and_steal (graph
->error)) : (g_string_free) ((graph->error), ((!(0)))))
;
4629 exit(-1);
4630 }
4631 else
4632 {
4633 int idx;
4634 int next_idx = 0;
4635
4636 sharkd_json_array_open("items");
4637 for (idx = 0; idx < graph->num_items; idx++)
4638 {
4639 double val;
4640
4641 val = get_io_graph_item(graph->items, graph->calc_type, idx, graph->hf_index, &cfile, graph->interval, graph->num_items, graph->aot);
4642
4643 /* if it's zero, don't display */
4644 if (val == 0.0)
4645 continue;
4646
4647 /* cause zeros are not printed, need to output index */
4648 if (next_idx != idx)
4649 sharkd_json_value_stringf(NULL((void*)0), "%x", idx);
4650
4651 sharkd_json_value_anyf(NULL((void*)0), "%f", val);
4652 next_idx = idx + 1;
4653 }
4654 sharkd_json_array_close();
4655 }
4656 json_dumper_end_object(&dumper);
4657
4658 remove_tap_listener(graph);
4659 g_free(graph->items);
4660 }
4661 sharkd_json_array_close();
4662
4663 sharkd_json_result_epilogue();
4664}
4665
4666/**
4667 * sharkd_session_process_intervals()
4668 *
4669 * Process intervals request - generate basic capture file statistics per requested interval.
4670 *
4671 * Input:
4672 * (o) interval - interval time in ms, if not specified: 1000ms
4673 * (o) filter - filter for generating interval request
4674 *
4675 * Output object with attributes:
4676 * (m) intervals - array of intervals, with indexes:
4677 * [0] - index of interval,
4678 * [1] - number of frames during interval,
4679 * [2] - number of bytes during interval.
4680 *
4681 * (m) last - last interval number.
4682 * (m) frames - total number of frames
4683 * (m) bytes - total number of bytes
4684 *
4685 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
4686 */
4687static void
4688sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
4689{
4690 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4691 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4692
4693 const uint8_t *filter_data = NULL((void*)0);
4694
4695 struct
4696 {
4697 unsigned int frames;
4698 uint64_t bytes;
4699 } st, st_total;
4700
4701 nstime_t *start_ts;
4702
4703 uint32_t interval_ms = 1000; /* default: one per second */
4704
4705 int64_t idx;
4706 int64_t max_idx = 0;
4707
4708 if (tok_interval)
4709 ws_strtou32(tok_interval, NULL((void*)0), &interval_ms); // already validated
4710
4711 if (tok_filter)
4712 {
4713 const struct sharkd_filter_item *filter_item;
4714
4715 filter_item = sharkd_session_filter_data(tok_filter);
4716 if (!filter_item)
4717 {
4718 sharkd_json_error(
4719 rpcid, -7001, NULL((void*)0),
4720 "Invalid filter parameter: %s", tok_filter
4721 );
4722 return;
4723 }
4724 filter_data = filter_item->filtered;
4725 }
4726
4727 st_total.frames = 0;
4728 st_total.bytes = 0;
4729
4730 st.frames = 0;
4731 st.bytes = 0;
4732
4733 idx = 0;
4734
4735 sharkd_json_result_prologue(rpcid);
4736 sharkd_json_array_open("intervals");
4737
4738 start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL((void*)0);
4739
4740 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
4741 {
4742 frame_data *fdata;
4743 int64_t msec_rel;
4744 int64_t new_idx;
4745
4746 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
4747 continue;
4748
4749 fdata = sharkd_get_frame(framenum);
4750
4751 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (int64_t) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
4752 new_idx = msec_rel / interval_ms;
4753
4754 if (idx != new_idx)
4755 {
4756 if (st.frames != 0)
4757 {
4758 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4759 }
4760
4761 idx = new_idx;
4762 if (idx > max_idx)
4763 max_idx = idx;
4764
4765 st.frames = 0;
4766 st.bytes = 0;
4767 }
4768
4769 st.frames += 1;
4770 st.bytes += fdata->pkt_len;
4771
4772 st_total.frames += 1;
4773 st_total.bytes += fdata->pkt_len;
4774 }
4775
4776 if (st.frames != 0)
4777 {
4778 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4779 }
4780 sharkd_json_array_close();
4781
4782 sharkd_json_value_anyf("last", "%" PRId64"l" "d", max_idx);
4783 sharkd_json_value_anyf("frames", "%u", st_total.frames);
4784 sharkd_json_value_anyf("bytes", "%" PRIu64"l" "u", st_total.bytes);
4785
4786 sharkd_json_result_epilogue();
4787}
4788
4789/**
4790 * sharkd_session_process_frame()
4791 *
4792 * Process frame request
4793 *
4794 * Input:
4795 * (m) frame - requested frame number
4796 * (o) ref_frame - time reference frame number
4797 * (o) prev_frame - previously displayed frame number
4798 * (o) proto - set if output frame tree
4799 * (o) columns - set if output frame columns
4800 * (o) color - set if output color-filter bg/fg
4801 * (o) bytes - set if output frame bytes
4802 * (o) hidden - set if output hidden tree fields
4803 *
4804 * Output object with attributes:
4805 * (m) err - 0 if succeed
4806 * (o) tree - array of frame nodes with attributes:
4807 * l - label
4808 * t: 'proto', 'framenum', 'url' - type of node
4809 * f - filter string
4810 * fn - field name
4811 * s - severity
4812 * e - subtree ett index
4813 * n - array of subtree nodes
4814 * h - two item array: (item start, item length)
4815 * i - two item array: (appendix start, appendix length)
4816 * p - [RESERVED] two item array: (protocol start, protocol length)
4817 * ds- data src index
4818 * url - only for t:'url', url
4819 * fnum - only for t:'framenum', frame number
4820 * g - if field is generated by Wireshark
4821 * v - if field is hidden
4822 *
4823 * (o) col - array of column data
4824 * (o) bytes - base64 of frame bytes
4825 * (o) ds - array of other data srcs
4826 * (o) comment - frame comment
4827 * (o) fol - array of follow filters:
4828 * [0] - protocol
4829 * [1] - filter string
4830 * (o) followers - array of followers with attributes:
4831 * protocol - protocol string
4832 * filter - filter string
4833 * stream - stream index number
4834 * sub_stream - sub-stream index number (optional, e.g. for HTTP/2 and QUIC streams)
4835 * (o) i - if frame is ignored
4836 * (o) m - if frame is marked
4837 * (o) bg - color filter - background color in hex
4838 * (o) fg - color filter - foreground color in hex
4839 */
4840static void
4841sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
4842{
4843 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
4844 const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
4845 const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
4846 column_info *cinfo = NULL((void*)0);
4847
4848 uint32_t framenum, ref_frame_num, prev_dis_num;
4849 uint32_t dissect_flags = SHARKD_DISSECT_FLAG_NULL0x00u;
4850 struct sharkd_frame_request_data req_data;
4851 wtap_rec rec; /* Record information */
4852 enum dissect_request_status status;
4853 int err;
4854 char *err_info;
4855
4856 ws_strtou32(tok_frame, NULL((void*)0), &framenum); // we have already validated this
4857
4858 ref_frame_num = (framenum != 1) ? 1 : 0;
4859 if (tok_ref_frame)
4860 {
4861 ws_strtou32(tok_ref_frame, NULL((void*)0), &ref_frame_num);
4862 if (ref_frame_num > framenum)
4863 {
4864 sharkd_json_error(
4865 rpcid, -8001, NULL((void*)0),
4866 "Invalid ref_frame - The ref_frame occurs after the frame specified"
4867 );
4868 return;
4869 }
4870 }
4871
4872 prev_dis_num = framenum - 1;
4873 if (tok_prev_frame)
4874 {
4875 ws_strtou32(tok_prev_frame, NULL((void*)0), &prev_dis_num);
4876 if (prev_dis_num >= framenum)
4877 {
4878 sharkd_json_error(
4879 rpcid, -8002, NULL((void*)0),
4880 "Invalid prev_frame - The prev_frame occurs on or after the frame specified"
4881 );
4882 return;
4883 }
4884 }
4885
4886 if (json_find_attr(buf, tokens, count, "proto") != NULL((void*)0))
4887 dissect_flags |= SHARKD_DISSECT_FLAG_PROTO_TREE0x04u;
4888 if (json_find_attr(buf, tokens, count, "bytes") != NULL((void*)0))
4889 dissect_flags |= SHARKD_DISSECT_FLAG_BYTES0x01u;
4890 if (json_find_attr(buf, tokens, count, "columns") != NULL((void*)0)) {
4891 dissect_flags |= SHARKD_DISSECT_FLAG_COLUMNS0x02u;
4892 cinfo = &cfile.cinfo;
4893 }
4894 if (json_find_attr(buf, tokens, count, "color") != NULL((void*)0))
4895 dissect_flags |= SHARKD_DISSECT_FLAG_COLOR0x08u;
4896
4897 req_data.display_hidden = (json_find_attr(buf, tokens, count, "v") != NULL((void*)0));
4898
4899 wtap_rec_init(&rec, 1514);
4900
4901 status = sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num,
4902 &rec, cinfo, dissect_flags,
4903 &sharkd_session_process_frame_cb, &req_data, &err, &err_info);
4904 switch (status) {
4905
4906 case DISSECT_REQUEST_SUCCESS:
4907 /* success */
4908 break;
4909
4910 case DISSECT_REQUEST_NO_SUCH_FRAME:
4911 sharkd_json_error(
4912 rpcid, -8003, NULL((void*)0),
4913 "Invalid frame - The frame number requested is out of range"
4914 );
4915 break;
4916
4917 case DISSECT_REQUEST_READ_ERROR:
4918 sharkd_json_error(
4919 rpcid, -8003, NULL((void*)0),
4920 /* XXX - show the error details */
4921 "Read error - The frame could not be read from the file"
4922 );
4923 g_free(err_info);
4924 break;
4925 }
4926
4927 wtap_rec_cleanup(&rec);
4928}
4929
4930/**
4931 * sharkd_session_process_check()
4932 *
4933 * Process check request.
4934 *
4935 * Input:
4936 * (o) filter - filter to be checked
4937 * (o) field - field to be checked
4938 *
4939 * Output object with attributes:
4940 * (m) err - always 0
4941 * (o) filter - 'ok', 'warn' or error message
4942 * (o) field - 'ok', or 'notfound'
4943 */
4944static int
4945sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
4946{
4947 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4948 const char *tok_field = json_find_attr(buf, tokens, count, "field");
4949
4950 if (tok_filter != NULL((void*)0))
4951 {
4952 dfilter_t *dfp;
4953 df_error_t *df_err = NULL((void*)0);
4954
4955 if (dfilter_compile(tok_filter, &dfp, &df_err)dfilter_compile_full(tok_filter, &dfp, &df_err, (1U <<
1)|(1U << 2), __func__)
)
4956 {
4957 if (dfp && dfilter_deprecated_tokens(dfp))
4958 sharkd_json_warning(rpcid, "Filter contains deprecated tokens");
4959 else
4960 sharkd_json_simple_ok(rpcid);
4961
4962 dfilter_free(dfp);
4963 df_error_free(&df_err);
4964 return 0;
4965 }
4966 else
4967 {
4968 sharkd_json_error(
4969 rpcid, -5001, NULL((void*)0),
4970 "Filter invalid - %s", df_err->msg
4971 );
4972 df_error_free(&df_err);
4973 return -5001;
4974 }
4975 }
4976
4977 if (tok_field != NULL((void*)0))
4978 {
4979 header_field_info *hfi = proto_registrar_get_byname(tok_field);
4980
4981 if (!hfi)
4982 {
4983 sharkd_json_error(
4984 rpcid, -5002, NULL((void*)0),
4985 "Field %s not found", tok_field
4986 );
4987 return -5002;
4988 }
4989 else
4990 {
4991 sharkd_json_simple_ok(rpcid);
4992 return 0;
4993 }
4994 }
4995
4996 sharkd_json_simple_ok(rpcid);
4997 return 0;
4998}
4999
5000struct sharkd_session_process_complete_pref_data
5001{
5002 const char *module;
5003 const char *pref;
5004};
5005
5006static unsigned
5007sharkd_session_process_complete_pref_cb(module_t *module, void *d)
5008{
5009 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5010
5011 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
5012 return 0;
5013
5014 json_dumper_begin_object(&dumper);
5015 sharkd_json_value_string("f", module->name);
5016 sharkd_json_value_string("d", module->title);
5017 json_dumper_end_object(&dumper);
5018
5019 return 0;
5020}
5021
5022static unsigned
5023sharkd_session_process_complete_pref_option_cb(pref_t *pref, void *d)
5024{
5025 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5026 const char *pref_name = prefs_get_name(pref);
5027 const char *pref_title = prefs_get_title(pref);
5028
5029 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
5030 return 0;
5031
5032 json_dumper_begin_object(&dumper);
5033 sharkd_json_value_stringf("f", "%s.%s", data->module, pref_name);
5034 sharkd_json_value_string("d", pref_title);
5035 json_dumper_end_object(&dumper);
5036
5037 return 0; /* continue */
5038}
5039
5040/**
5041 * sharkd_session_process_complete()
5042 *
5043 * Process complete request
5044 *
5045 * Input:
5046 * (o) field - field to be completed
5047 * (o) pref - preference to be completed
5048 *
5049 * Output object with attributes:
5050 * (m) err - always 0
5051 * (o) field - array of object with attributes:
5052 * (m) f - field text
5053 * (o) t - field type (FT_ number)
5054 * (o) n - field name
5055 * (o) pref - array of object with attributes:
5056 * (m) f - pref name
5057 * (o) d - pref description
5058 */
5059static int
5060sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
5061{
5062 const char *tok_field = json_find_attr(buf, tokens, count, "field");
5063 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5064
5065 sharkd_json_result_prologue(rpcid);
5066
5067 if (tok_field != NULL((void*)0) && tok_field[0])
5068 {
5069 const size_t filter_length = strlen(tok_field);
5070 const int filter_with_dot = !!strchr(tok_field, '.');
5071
5072 void *proto_cookie;
5073 void *field_cookie;
5074 int proto_id;
5075
5076 sharkd_json_array_open("field");
5077
5078 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
5079 {
5080 protocol_t *protocol = find_protocol_by_id(proto_id);
5081 const char *protocol_filter;
5082 const char *protocol_name;
5083 header_field_info *hfinfo;
5084
5085 if (!proto_is_protocol_enabled(protocol))
5086 continue;
5087
5088 protocol_name = proto_get_protocol_long_name(protocol);
5089 protocol_filter = proto_get_protocol_filter_name(proto_id);
5090
5091 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
5092 {
5093 json_dumper_begin_object(&dumper);
5094 {
5095 sharkd_json_value_string("f", protocol_filter);
5096 sharkd_json_value_anyf("t", "%d", FT_PROTOCOL);
5097 sharkd_json_value_string("n", protocol_name);
5098 }
5099 json_dumper_end_object(&dumper);
5100 }
5101
5102 if (!filter_with_dot)
5103 continue;
5104
5105 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL((void*)0); hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
5106 {
5107 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
5108 continue;
5109
5110 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
5111 {
5112 json_dumper_begin_object(&dumper);
5113 {
5114 sharkd_json_value_string("f", hfinfo->abbrev);
5115
5116 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
5117 if (hfinfo->same_name_next == NULL((void*)0))
5118 {
5119 sharkd_json_value_anyf("t", "%d", hfinfo->type);
5120 sharkd_json_value_string("n", hfinfo->name);
5121 }
5122 }
5123 json_dumper_end_object(&dumper);
5124 }
5125 }
5126 }
5127
5128 sharkd_json_array_close();
5129 }
5130
5131 if (tok_pref != NULL((void*)0) && tok_pref[0])
5132 {
5133 struct sharkd_session_process_complete_pref_data data;
5134 char *dot_sepa;
5135
5136 data.module = tok_pref;
5137 data.pref = tok_pref;
5138
5139 sharkd_json_array_open("pref");
5140 if ((dot_sepa = strchr(tok_pref, '.')))
5141 {
5142 module_t *pref_mod;
5143
5144 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5145 data.pref = dot_sepa + 1;
5146
5147 pref_mod = prefs_find_module(data.module);
5148 if (pref_mod)
5149 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
5150
5151 *dot_sepa = '.';
5152 }
5153 else
5154 {
5155 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
5156 }
5157 sharkd_json_array_close();
5158 }
5159
5160 sharkd_json_result_epilogue();
5161
5162 return 0;
5163}
5164
5165/**
5166 * sharkd_session_process_setcomment()
5167 *
5168 * Process setcomment request
5169 *
5170 * Input:
5171 * (m) frame - frame number
5172 * (o) comment - user comment
5173 *
5174 * Output object with attributes:
5175 * (m) err - error code: 0 succeed
5176 *
5177 * Note:
5178 * For now, adds comments, doesn't remove or replace them.
5179 */
5180static void
5181sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count)
5182{
5183 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
5184 const char *tok_comment = json_find_attr(buf, tokens, count, "comment");
5185
5186 uint32_t framenum;
5187 frame_data *fdata;
5188 wtap_opttype_return_val ret;
5189 wtap_block_t pkt_block = NULL((void*)0);
5190
5191 if (!tok_frame || !ws_strtou32(tok_frame, NULL((void*)0), &framenum) || framenum == 0)
5192 {
5193 sharkd_json_error(
5194 rpcid, -3001, NULL((void*)0),
5195 "Frame number must be a positive integer"
5196 );
5197 return;
5198 }
5199
5200 fdata = sharkd_get_frame(framenum); // BUG HERE - If no file loaded you get a crash
5201 if (!fdata)
5202 {
5203 sharkd_json_error(
5204 rpcid, -3002, NULL((void*)0),
5205 "Frame number is out of range"
5206 );
5207 return;
5208 }
5209
5210 pkt_block = sharkd_get_packet_block(fdata);
5211
5212 ret = wtap_block_add_string_option(pkt_block, OPT_COMMENT1, tok_comment, strlen(tok_comment));
5213
5214 if (ret != WTAP_OPTTYPE_SUCCESS)
5215 {
5216 sharkd_json_error(
5217 rpcid, -3003, NULL((void*)0),
5218 "Unable to set the comment"
5219 );
5220 }
5221 else
5222 {
5223 sharkd_set_modified_block(fdata, pkt_block);
5224 sharkd_json_simple_ok(rpcid);
5225 }
5226}
5227
5228/**
5229 * sharkd_session_process_setconf()
5230 *
5231 * Process setconf request
5232 *
5233 * Input:
5234 * (m) name - preference name
5235 * (m) value - preference value
5236 *
5237 * Output object with attributes:
5238 * (m) err - error code: 0 succeed
5239 */
5240static void
5241sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
5242{
5243 const char *tok_name = json_find_attr(buf, tokens, count, "name");
5244 const char *tok_value = json_find_attr(buf, tokens, count, "value");
5245 char pref[4096];
5246 char *errmsg = NULL((void*)0);
5247
5248 prefs_set_pref_e ret;
5249
5250 if (!tok_name || tok_name[0] == '\0')
5251 {
5252 sharkd_json_error(
5253 rpcid, -4001, NULL((void*)0),
5254 "Preference name missing"
5255 );
5256 return;
5257 }
5258
5259 if (!tok_value)
5260 {
5261 sharkd_json_error(
5262 rpcid, -4002, NULL((void*)0),
5263 "Preference value missing"
5264 );
5265 return;
5266 }
5267
5268 snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
5269
5270 ret = prefs_set_pref(pref, &errmsg);
5271
5272 switch (ret)
5273 {
5274 case PREFS_SET_OK:
5275 sharkd_json_simple_ok(rpcid);
5276 break;
5277
5278 case PREFS_SET_OBSOLETE:
5279 sharkd_json_error(
5280 rpcid, -4003, NULL((void*)0),
5281 "The preference specified is obsolete"
5282 );
5283 break;
5284
5285 case PREFS_SET_NO_SUCH_PREF:
5286 sharkd_json_error(
5287 rpcid, -4004, NULL((void*)0),
5288 "No such preference exists"
5289 );
5290 break;
5291
5292 default:
5293 sharkd_json_error(
5294 rpcid, -4005, NULL((void*)0),
5295 "Unable to set the preference%s%s",
5296 errmsg ? ": " : "", errmsg ? errmsg : ""
5297 );
5298 }
5299
5300 g_free(errmsg);
5301}
5302
5303struct sharkd_session_process_dumpconf_data
5304{
5305 module_t *module;
5306};
5307
5308static unsigned
5309sharkd_session_process_dumpconf_cb(pref_t *pref, void *d)
5310{
5311 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5312 const char *pref_name = prefs_get_name(pref);
5313
5314 char json_pref_key[512];
5315
5316 snprintf(json_pref_key, sizeof(json_pref_key), "%s.%s", data->module->name, pref_name);
5317 sharkd_json_object_open(json_pref_key);
5318
5319 switch (prefs_get_type(pref))
5320 {
5321 case PREF_UINT(1u << 0):
5322 sharkd_json_value_anyf("u", "%u", prefs_get_uint_value(pref, pref_current));
5323 if (prefs_get_uint_base(pref) != 10)
5324 sharkd_json_value_anyf("ub", "%u", prefs_get_uint_base(pref));
5325 break;
5326
5327 case PREF_BOOL(1u << 1):
5328 sharkd_json_value_anyf("b", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
5329 break;
5330
5331 case PREF_STRING(1u << 3):
5332 case PREF_SAVE_FILENAME(1u << 7):
5333 case PREF_OPEN_FILENAME(1u << 14):
5334 case PREF_DIRNAME(1u << 11):
5335 case PREF_PASSWORD(1u << 15):
5336 case PREF_DISSECTOR(1u << 17):
5337 sharkd_json_value_string("s", prefs_get_string_value(pref, pref_current));
5338 break;
5339
5340 case PREF_ENUM(1u << 2):
5341 {
5342 const enum_val_t *enums;
5343
5344 sharkd_json_array_open("e");
5345 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
5346 {
5347 json_dumper_begin_object(&dumper);
5348
5349 sharkd_json_value_anyf("v", "%d", enums->value);
5350
5351 if (enums->value == prefs_get_enum_value(pref, pref_current))
5352 sharkd_json_value_anyf("s", "1");
5353
5354 sharkd_json_value_string("d", enums->description);
5355
5356 json_dumper_end_object(&dumper);
5357 }
5358 sharkd_json_array_close();
5359 break;
5360 }
5361
5362 case PREF_RANGE(1u << 4):
5363 case PREF_DECODE_AS_RANGE(1u << 13):
5364 {
5365 char *range_str = range_convert_range(NULL((void*)0), prefs_get_range_value_real(pref, pref_current));
5366 sharkd_json_value_string("r", range_str);
5367 wmem_free(NULL((void*)0), range_str);
5368 break;
5369 }
5370
5371 case PREF_UAT(1u << 6):
5372 {
5373 uat_t *uat = prefs_get_uat_value(pref);
5374 unsigned idx;
5375
5376 sharkd_json_array_open("t");
5377 for (idx = 0; idx < uat->raw_data->len; idx++)
5378 {
5379 void *rec = UAT_INDEX_PTR(uat, idx)(uat->raw_data->data + (uat->record_size * (idx)));
5380 unsigned colnum;
5381
5382 sharkd_json_array_open(NULL((void*)0));
5383 for (colnum = 0; colnum < uat->ncols; colnum++)
5384 {
5385 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
5386
5387 sharkd_json_value_string(NULL((void*)0), str);
5388 g_free(str);
5389 }
5390
5391 sharkd_json_array_close();
5392 }
5393
5394 sharkd_json_array_close();
5395 break;
5396 }
5397
5398 case PREF_COLOR(1u << 8):
5399 case PREF_CUSTOM(1u << 9):
5400 case PREF_STATIC_TEXT(1u << 5):
5401 case PREF_OBSOLETE(1u << 10):
5402 /* TODO */
5403 break;
5404 }
5405
5406#if 0
5407 sharkd_json_value_string("t", prefs_get_title(pref));
5408#endif
5409
5410 sharkd_json_object_close();
5411
5412 return 0; /* continue */
5413}
5414
5415static unsigned
5416sharkd_session_process_dumpconf_mod_cb(module_t *module, void *d)
5417{
5418 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5419
5420 data->module = module;
5421 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
5422
5423 return 0;
5424}
5425
5426/**
5427 * sharkd_session_process_dumpconf()
5428 *
5429 * Process dumpconf request
5430 *
5431 * Input:
5432 * (o) pref - module, or preference, NULL for all
5433 *
5434 * Output object with attributes:
5435 * (o) prefs - object with module preferences
5436 * (m) [KEY] - preference name
5437 * (o) u - preference value (for PREF_UINT)
5438 * (o) ub - preference value suggested base for display (for PREF_UINT) and if different than 10
5439 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
5440 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME, PREF_PASSWORD, PREF_DISSECTOR)
5441 * (o) e - preference possible values (only for PREF_ENUM)
5442 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
5443 * (o) t - preference value (only for PREF_UAT)
5444 */
5445static void
5446sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
5447{
5448 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5449 module_t *pref_mod;
5450 char *dot_sepa;
5451
5452 if (!tok_pref)
5453 {
5454 struct sharkd_session_process_dumpconf_data data;
5455
5456 data.module = NULL((void*)0);
5457
5458 sharkd_json_result_prologue(rpcid);
5459
5460 sharkd_json_object_open("prefs");
5461 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
5462 sharkd_json_object_close();
5463
5464 sharkd_json_result_epilogue();
5465 return;
5466 }
5467
5468 if ((dot_sepa = strchr(tok_pref, '.')))
5469 {
5470 pref_t *pref = NULL((void*)0);
5471
5472 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5473 pref_mod = prefs_find_module(tok_pref);
5474 if (pref_mod)
5475 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
5476 *dot_sepa = '.';
5477
5478 if (pref)
5479 {
5480 struct sharkd_session_process_dumpconf_data data;
5481
5482 data.module = pref_mod;
5483
5484 sharkd_json_result_prologue(rpcid);
5485
5486 sharkd_json_object_open("prefs");
5487 sharkd_session_process_dumpconf_cb(pref, &data);
5488 sharkd_json_object_close();
5489
5490 sharkd_json_result_epilogue();
5491 return;
5492 }
5493 else
5494 {
5495 sharkd_json_error(
5496 rpcid, -9001, NULL((void*)0),
5497 "Invalid pref %s.", tok_pref
5498 );
5499 return;
5500 }
5501
5502 }
5503
5504 pref_mod = prefs_find_module(tok_pref);
5505 if (pref_mod)
5506 {
5507 struct sharkd_session_process_dumpconf_data data;
5508
5509 data.module = pref_mod;
5510
5511 sharkd_json_result_prologue(rpcid);
5512
5513 sharkd_json_object_open("prefs");
5514 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
5515 sharkd_json_object_close();
5516
5517 sharkd_json_result_epilogue();
5518 }
5519 else
5520 {
5521 sharkd_json_error(
5522 rpcid, -9002, NULL((void*)0),
5523 "Invalid pref %s.", tok_pref
5524 );
5525 }
5526}
5527
5528struct sharkd_download_rtp
5529{
5530 rtpstream_id_t id;
5531 GSList *packets;
5532 double start_time;
5533};
5534
5535static void
5536sharkd_rtp_download_free_items(void *ptr)
5537{
5538 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
5539
5540 g_free(rtp_packet->info);
5541 g_free(rtp_packet->payload_data);
5542 g_free(rtp_packet);
5543}
5544
5545static void
5546sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
5547{
5548 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
5549 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
5550
5551 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
5552
5553 uint32_t audio_out_rate_ = 0;
5554 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
5555 struct SpeexResamplerState_ *audio_resampler_ = NULL((void*)0);
5556
5557 size_t resample_buff_len = 0x1000;
5558 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
5559 spx_uint32_t cur_in_rate = 0;
5560 char *write_buff = NULL((void*)0);
5561 size_t write_bytes = 0;
5562 unsigned channels = 0;
5563 unsigned sample_rate = 0;
5564
5565 GSList *l;
5566
5567 for (l = req->packets; l; l = l->next)
5568 {
5569 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
5570
5571 SAMPLE *decode_buff = NULL((void*)0);
5572 size_t decoded_bytes;
5573
5574 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
5575 if (decoded_bytes == 0 || sample_rate == 0)
5576 {
5577 /* We didn't decode anything. Clean up and prep for the next packet. */
5578 g_free(decode_buff);
5579 continue;
5580 }
5581
5582 if (audio_out_rate_ == 0)
5583 {
5584 uint32_t tmp32;
5585 uint16_t tmp16;
5586 char wav_hdr[44];
5587
5588 /* First non-zero wins */
5589 audio_out_rate_ = sample_rate;
5590
5591 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
5592
5593 /* write WAVE header */
5594 memset(&wav_hdr, 0, sizeof(wav_hdr));
5595 memcpy(&wav_hdr[0], "RIFF", 4);
5596 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5597 memcpy(&wav_hdr[8], "WAVE", 4);
5598
5599 memcpy(&wav_hdr[12], "fmt ", 4);
5600 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
5601 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
5602 /* # channels */
5603 tmp16 = channels;
5604 memcpy(&wav_hdr[22], &tmp16, 2);
5605 /* sample rate */
5606 tmp32 = sample_rate;
5607 memcpy(&wav_hdr[24], &tmp32, 4);
5608 /* byte rate */
5609 tmp32 = sample_rate * channels * sample_bytes_;
5610 memcpy(&wav_hdr[28], &tmp32, 4);
5611 /* block align */
5612 tmp16 = channels * sample_bytes_;
5613 memcpy(&wav_hdr[32], &tmp16, 2);
5614 /* bits per sample */
5615 tmp16 = 8 * sample_bytes_;
5616 memcpy(&wav_hdr[34], &tmp16, 2);
5617
5618 memcpy(&wav_hdr[36], "data", 4);
5619 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5620
5621 json_dumper_write_base64(&dumper, wav_hdr, sizeof(wav_hdr));
5622 }
5623
5624 // Write samples to our file.
5625 write_buff = (char *) decode_buff;
5626 write_bytes = decoded_bytes;
5627
5628 if (audio_out_rate_ != sample_rate)
5629 {
5630 spx_uint32_t in_len, out_len;
5631
5632 /* Resample the audio to match our previous output rate. */
5633 if (!audio_resampler_)
5634 {
5635 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL((void*)0));
5636 speex_resampler_skip_zeros(audio_resampler_);
5637 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
5638 }
5639 else
5640 {
5641 spx_uint32_t audio_out_rate;
5642 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
5643
5644 if (sample_rate != cur_in_rate)
5645 {
5646 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
5647 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
5648 }
5649 }
5650 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
5651 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
5652 if (out_len * sample_bytes_ > resample_buff_len)
5653 {
5654 while ((out_len * sample_bytes_ > resample_buff_len))
5655 resample_buff_len *= 2;
5656 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
5657 }
5658
5659 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
5660 write_buff = (char *) resample_buff;
5661 write_bytes = out_len * sample_bytes_;
5662 }
5663
5664 /* Write the decoded, possibly-resampled audio */
5665 json_dumper_write_base64(&dumper, write_buff, write_bytes);
5666
5667 g_free(decode_buff);
5668 }
5669
5670 g_free(resample_buff);
5671 g_hash_table_destroy(decoders_hash_);
5672}
5673
5674static tap_packet_status
5675sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U___attribute__((unused)), const void *data, tap_flags_t flags _U___attribute__((unused)))
5676{
5677 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
5678 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
5679
5680 /* do not consider RTP packets without a setup frame */
5681 if (rtp_info->info_setup_frame_num == 0)
5682 return TAP_PACKET_DONT_REDRAW;
5683
5684 if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info))
5685 {
5686 rtp_packet_t *rtp_packet;
5687
5688 rtp_packet = g_new0(rtp_packet_t, 1)((rtp_packet_t *) g_malloc0_n ((1), sizeof (rtp_packet_t)));
5689 rtp_packet->info = (struct _rtp_info *) g_memdup2(rtp_info, sizeof(struct _rtp_info));
5690
5691 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
5692 rtp_packet->payload_data = (uint8_t *) g_memdup2(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
5693
5694 if (!req_rtp->packets)
5695 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
5696
5697 rtp_packet->frame_num = pinfo->num;
5698 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
5699
5700 /* XXX, O(n) optimize */
5701 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
5702 }
5703
5704 return TAP_PACKET_DONT_REDRAW;
5705}
5706
5707static bool_Bool
5708sharkd_session_eo_retap_listener(const char *tap_type) {
5709 bool_Bool ok = true1;
5710 register_eo_t *eo = NULL((void*)0);
5711 GString *tap_error = NULL((void*)0);
5712 void *tap_data = NULL((void*)0);
5713 GFreeFunc tap_free = NULL((void*)0);
5714
5715 // get <name> from eo:<name>, get_eo_by_name only needs the name (http etc.)
5716 eo = get_eo_by_name(tap_type + 3);
5717 if (!eo)
5718 {
5719 ok = false0;
5720 sharkd_json_error(
5721 rpcid, -11011, NULL((void*)0),
5722 "sharkd_session_eo_retap_listener() eo=%s not found", tap_type + 3
5723 );
5724 }
5725
5726 if (ok)
5727 {
5728 tap_error = sharkd_session_eo_register_tap_listener(eo, tap_type, NULL((void*)0), NULL((void*)0), &tap_data, &tap_free);
5729 if (tap_error)
5730 {
5731 ok = false0;
5732 sharkd_json_error(
5733 rpcid, -10002, NULL((void*)0),
5734 "sharkd_session_eo_retap_listener() sharkd_session_eo_register_tap_listener error %s",
5735 tap_error->str);
5736 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
5737 }
5738 }
5739
5740 if (ok)
5741 sharkd_retap();
5742
5743 if (!tap_error)
5744 remove_tap_listener(tap_data);
5745
5746 if (tap_free)
5747 tap_free(tap_data);
5748
5749 return ok;
5750}
5751
5752/**
5753 * sharkd_session_process_download()
5754 *
5755 * Process download request
5756 *
5757 * Input:
5758 * (m) token - token to download
5759 *
5760 * Output object with attributes:
5761 * (o) file - suggested name of file
5762 * (o) mime - suggested content type
5763 * (o) data - payload base64 encoded
5764 */
5765static void
5766sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
5767{
5768 const char *tok_token = json_find_attr(buf, tokens, count, "token");
5769
5770 if (!tok_token)
5771 {
5772 sharkd_json_error(
5773 rpcid, -10005, NULL((void*)0),
5774 "missing token"
5775 );
5776 return;
5777 }
5778
5779 if (!strncmp(tok_token, "eo:", 3))
5780 {
5781 // get eo:<name> from eo:<name>_<row>
5782 char *tap_type = g_strdup(tok_token)g_strdup_inline (tok_token);
5783 char *tmp = strrchr(tap_type, '_');
5784 if (tmp)
5785 *tmp = '\0';
5786
5787 // if eo:<name> not in sharkd_eo_list, retap
5788 if (!sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type) &&
5789 !sharkd_session_eo_retap_listener(tap_type))
5790 {
5791 g_free(tap_type);
5792 // sharkd_json_error called in sharkd_session_eo_retap_listener
5793 return;
5794 }
5795
5796 g_free(tap_type);
5797
5798 struct sharkd_export_object_list *object_list;
5799 const export_object_entry_t *eo_entry = NULL((void*)0);
5800
5801 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
5802 {
5803 size_t eo_type_len = strlen(object_list->type);
5804
5805 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
5806 {
5807 int row;
5808
5809 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
5810 break;
5811
5812 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
5813 break;
5814 }
5815 }
5816
5817 if (eo_entry)
5818 {
5819 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
5820 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
5821
5822 sharkd_json_result_prologue(rpcid);
5823 sharkd_json_value_string("file", filename);
5824 sharkd_json_value_string("mime", mime);
5825 sharkd_json_value_base64("data", eo_entry->payload_data, eo_entry->payload_len);
5826 sharkd_json_result_epilogue();
5827 }
5828 else
5829 {
5830 sharkd_json_result_prologue(rpcid);
5831 sharkd_json_result_epilogue();
5832 }
5833 }
5834 else if (!strcmp(tok_token, "ssl-secrets"))
5835 {
5836 size_t str_len;
5837 char *str = ssl_export_sessions(&str_len);
5838
5839 if (str)
5840 {
5841 const char *mime = "text/plain";
5842 const char *filename = "keylog.txt";
5843
5844 sharkd_json_result_prologue(rpcid);
5845 sharkd_json_value_string("file", filename);
5846 sharkd_json_value_string("mime", mime);
5847 sharkd_json_value_base64("data", str, str_len);
5848 sharkd_json_result_epilogue();
5849 }
5850 g_free(str);
5851 }
5852 else if (!strncmp(tok_token, "rtp:", 4))
5853 {
5854 struct sharkd_download_rtp rtp_req;
5855 GString *tap_error;
5856
5857 memset(&rtp_req, 0, sizeof(rtp_req));
5858 if (!sharkd_rtp_match_init(&rtp_req.id, tok_token + 4))
5859 {
5860 sharkd_json_error(
5861 rpcid, -10001, NULL((void*)0),
5862 "sharkd_session_process_download() rtp tokenizing error %s", tok_token
5863 );
5864 return;
5865 }
5866
5867 tap_error = register_tap_listener("rtp", &rtp_req, NULL((void*)0), 0, NULL((void*)0), sharkd_session_packet_download_tap_rtp_cb, NULL((void*)0), NULL((void*)0));
5868 if (tap_error)
5869 {
5870 sharkd_json_error(
5871 rpcid, -10002, NULL((void*)0),
5872 "sharkd_session_process_download() rtp error %s", tap_error->str
5873 );
5874 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
5875 return;
5876 }
5877
5878 sharkd_retap();
5879 remove_tap_listener(&rtp_req);
5880
5881 if (rtp_req.packets)
5882 {
5883 const char *mime = "audio/x-wav";
5884 const char *filename = tok_token;
5885
5886 sharkd_json_result_prologue(rpcid);
5887 sharkd_json_value_string("file", filename);
5888 sharkd_json_value_string("mime", mime);
5889
5890 json_dumper_set_member_name(&dumper, "data");
5891 json_dumper_begin_base64(&dumper);
5892 sharkd_rtp_download_decode(&rtp_req);
5893 json_dumper_end_base64(&dumper);
5894
5895 sharkd_json_result_epilogue();
5896
5897 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
5898 }
5899 else
5900 {
5901 sharkd_json_error(
5902 rpcid, -10003, NULL((void*)0),
5903 "no rtp data available"
5904 );
5905 }
5906 }
5907 else
5908 {
5909 sharkd_json_error(
5910 rpcid, -10004, NULL((void*)0),
5911 "unrecognized token"
5912 );
5913 }
5914}
5915
5916static void
5917sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
5918{
5919 if (json_prep(buf, tokens, count))
5920 {
5921 /* don't need [0] token */
5922 tokens++;
5923 count--;
5924
5925 const char* tok_method = json_find_attr(buf, tokens, count, "method");
5926
5927 if (!tok_method) {
5928 sharkd_json_error(
5929 rpcid, -32601, NULL((void*)0),
5930 "No method found");
5931 return;
5932 }
5933 if (!strcmp(tok_method, "load"))
5934 sharkd_session_process_load(buf, tokens, count);
5935 else if (!strcmp(tok_method, "status"))
5936 sharkd_session_process_status();
5937 else if (!strcmp(tok_method, "analyse"))
5938 sharkd_session_process_analyse();
5939 else if (!strcmp(tok_method, "info"))
5940 sharkd_session_process_info();
5941 else if (!strcmp(tok_method, "check"))
5942 sharkd_session_process_check(buf, tokens, count);
5943 else if (!strcmp(tok_method, "complete"))
5944 sharkd_session_process_complete(buf, tokens, count);
5945 else if (!strcmp(tok_method, "frames"))
5946 sharkd_session_process_frames(buf, tokens, count);
5947 else if (!strcmp(tok_method, "tap"))
5948 sharkd_session_process_tap(buf, tokens, count);
5949 else if (!strcmp(tok_method, "follow"))
5950 sharkd_session_process_follow(buf, tokens, count);
5951 else if (!strcmp(tok_method, "iograph"))
5952 sharkd_session_process_iograph(buf, tokens, count);
5953 else if (!strcmp(tok_method, "intervals"))
5954 sharkd_session_process_intervals(buf, tokens, count);
5955 else if (!strcmp(tok_method, "frame"))
5956 sharkd_session_process_frame(buf, tokens, count);
5957 else if (!strcmp(tok_method, "setcomment"))
5958 sharkd_session_process_setcomment(buf, tokens, count);
5959 else if (!strcmp(tok_method, "setconf"))
5960 sharkd_session_process_setconf(buf, tokens, count);
5961 else if (!strcmp(tok_method, "dumpconf"))
5962 sharkd_session_process_dumpconf(buf, tokens, count);
5963 else if (!strcmp(tok_method, "download"))
5964 sharkd_session_process_download(buf, tokens, count);
5965 else if (!strcmp(tok_method, "bye"))
5966 {
5967 sharkd_json_simple_ok(rpcid);
5968 exit(0);
5969 }
5970 else
5971 {
5972 sharkd_json_error(
5973 rpcid, -32601, NULL((void*)0),
5974 "The method \"%s\" is unknown", tok_method
5975 );
5976 }
5977 }
5978}
5979
5980int
5981sharkd_session_main(int mode_setting)
5982{
5983 char buf[8 * 1024];
5984 jsmntok_t *tokens = NULL((void*)0);
5985 int tokens_max = -1;
5986
5987 mode = mode_setting;
5988
5989 fprintf(stderrstderr, "Hello in child.\n");
5990
5991 dumper.output_file = stdoutstdout;
5992
5993 filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free);
5994
5995#ifdef HAVE_MAXMINDDB1
5996 /* mmdbresolve was stopped before fork(), force starting it */
5997 uat_get_table_by_name("MaxMind Database Paths")->post_update_cb();
5998#endif
5999
6000 set_resolution_synchrony(true1);
6001
6002 while (fgets(buf, sizeof(buf), stdinstdin))
6003 {
6004 /* every command is line separated JSON */
6005 int ret;
6006
6007 ret = json_parse(buf, NULL((void*)0), 0);
6008 if (ret <= 0)
6009 {
6010 sharkd_json_error(
6011 rpcid, -32600, NULL((void*)0),
6012 "Invalid JSON(1)"
6013 );
6014 continue;
6015 }
6016
6017 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
6018 ret += 1;
6019
6020 if (tokens == NULL((void*)0) || tokens_max < ret)
6021 {
6022 tokens_max = ret;
6023 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
6024 }
6025
6026 memset(tokens, 0, ret * sizeof(jsmntok_t));
6027
6028 ret = json_parse(buf, tokens, ret);
6029 if (ret <= 0)
6030 {
6031 sharkd_json_error(
6032 rpcid, -32600, NULL((void*)0),
6033 "Invalid JSON(2)"
6034 );
6035 continue;
6036 }
6037
6038 host_name_lookup_process();
6039
6040 sharkd_session_process(buf, tokens, ret);
6041 }
6042
6043 g_hash_table_destroy(filter_table);
6044 g_free(tokens);
6045
6046 return 0;
6047}

/builds/wireshark/wireshark/ui/io_graph_item.h

1/** @file
2 *
3 * Definitions and functions for I/O graph items
4 *
5 * Copied from gtk/io_stat.c, (c) 2002 Ronnie Sahlberg
6 *
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <[email protected]>
9 * Copyright 1998 Gerald Combs
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 */
13
14#ifndef __IO_GRAPH_ITEM_H__
15#define __IO_GRAPH_ITEM_H__
16
17#include "cfile.h"
18#include <wsutil/ws_assert.h>
19
20#include <epan/epan_dissect.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
25
26typedef enum {
27 IOG_ITEM_UNIT_FIRST,
28 IOG_ITEM_UNIT_PACKETS = IOG_ITEM_UNIT_FIRST,
29 IOG_ITEM_UNIT_BYTES,
30 IOG_ITEM_UNIT_BITS,
31 IOG_ITEM_UNIT_CALC_SUM,
32 IOG_ITEM_UNIT_CALC_FRAMES,
33 IOG_ITEM_UNIT_CALC_FIELDS,
34 IOG_ITEM_UNIT_CALC_MAX,
35 IOG_ITEM_UNIT_CALC_MIN,
36 IOG_ITEM_UNIT_CALC_AVERAGE,
37 IOG_ITEM_UNIT_CALC_THROUGHPUT,
38 IOG_ITEM_UNIT_CALC_LOAD,
39 IOG_ITEM_UNIT_LAST = IOG_ITEM_UNIT_CALC_LOAD,
40 NUM_IOG_ITEM_UNITS
41} io_graph_item_unit_t;
42
43typedef struct _io_graph_item_t {
44 uint32_t frames; /* always calculated, will hold number of frames*/
45 uint64_t bytes; /* always calculated, will hold number of bytes*/
46 uint64_t fields;
47 /* We use a double for totals because of overflow. For min and max,
48 * unsigned 64 bit integers larger than 2^53 cannot all be represented
49 * in a double, and this is useful for determining the frame with the
50 * min or max value, even though for plotting it will be converted to a
51 * double.
52 */
53 union {
54 nstime_t time_max;
55 double double_max;
56 int64_t int_max;
57 uint64_t uint_max;
58 };
59 union {
60 nstime_t time_min;
61 double double_min;
62 int64_t int_min;
63 uint64_t uint_min;
64 };
65 union {
66 nstime_t time_tot;
67 double double_tot;
68 };
69 uint32_t first_frame_in_invl;
70 uint32_t min_frame_in_invl;
71 uint32_t max_frame_in_invl;
72 uint32_t last_frame_in_invl;
73} io_graph_item_t;
74
75/** Reset (zero) an io_graph_item_t.
76 *
77 * @param items [in,out] Array containing the items to reset.
78 * @param count [in] The number of items in the array.
79 */
80static inline void
81reset_io_graph_items(io_graph_item_t *items, size_t count, int hf_index _U___attribute__((unused))) {
82 io_graph_item_t *item;
83 size_t i;
84
85 for (i = 0; i < count; i++) {
12
Assuming 'i' is >= 'count'
13
Loop condition is false. Execution continues on line 85
86 item = &items[i];
87
88 item->frames = 0;
89 item->bytes = 0;
90 item->fields = 0;
91 item->first_frame_in_invl = 0;
92 item->min_frame_in_invl = 0;
93 item->max_frame_in_invl = 0;
94 item->last_frame_in_invl = 0;
95
96 nstime_set_zero(&item->time_max);
97 nstime_set_zero(&item->time_min);
98 nstime_set_zero(&item->time_tot);
99
100#if 0
101 /* XXX - On C, type punning is explicitly allowed since C99 so
102 * setting the nstime_t values to 0 is always sufficient.
103 * On C++ that appears technically to be undefined behavior (though
104 * I don't know of any compilers for which it doesn't work and I
105 * can't get UBSAN to complain about it) and this would be safer.
106 */
107 if (hf_index > 0) {
108
109 switch (proto_registrar_get_ftype(hf_index)) {
110
111 case FT_INT8:
112 case FT_INT16:
113 case FT_INT24:
114 case FT_INT32:
115 case FT_INT40:
116 case FT_INT48:
117 case FT_INT56:
118 case FT_INT64:
119 item->int_max = 0;
120 item->int_min = 0;
121 item->double_tot = 0;
122 break;
123
124 case FT_UINT8:
125 case FT_UINT16:
126 case FT_UINT24:
127 case FT_UINT32:
128 case FT_UINT40:
129 case FT_UINT48:
130 case FT_UINT56:
131 case FT_UINT64:
132 item->uint_max = 0;
133 item->uint_min = 0;
134 item->double_tot = 0;
135 break;
136
137 case FT_DOUBLE:
138 case FT_FLOAT:
139 item->double_max = 0;
140 item->double_min = 0;
141 item->double_tot = 0;
142 break;
143
144 case FT_RELATIVE_TIME:
145 nstime_set_zero(&item->time_max);
146 nstime_set_zero(&item->time_min);
147 nstime_set_zero(&item->time_tot);
148 break;
149
150 default:
151 break;
152 }
153 }
154#endif
155 }
156}
157
158/** Get the interval (array index) for a packet
159 *
160 * It is up to the caller to determine if the return value is valid.
161 *
162 * @param [in] pinfo Packet of interest.
163 * @param [in] interval Time interval in microseconds
164 * @return Array index on success, -1 on failure.
165 *
166 * @note pinfo->rel_ts, and hence the index, is not affected by ignoring
167 * frames.
168 */
169int64_t get_io_graph_index(packet_info *pinfo, int interval);
170
171/** Check field and item unit compatibility
172 *
173 * @param field_name [in] Header field name to check
174 * @param hf_index [out] Assigned the header field index corresponding to field_name if valid.
175 * Can be NULL.
176 * @param item_unit [in] The type of unit to calculate. From IOG_ITEM_UNITS.
177 * @return NULL if compatible, otherwise an error string. The string must
178 * be freed by the caller.
179 */
180GString *check_field_unit(const char *field_name, int *hf_index, io_graph_item_unit_t item_unit);
181
182/** Get the value at the given interval (idx) for the current value unit.
183 *
184 * @param items [in] Array containing the item to get.
185 * @param val_units [in] The type of unit to calculate. From IOG_ITEM_UNITS.
186 * @param idx [in] Index of the item to get.
187 * @param hf_index [in] Header field index for advanced statistics.
188 * @param cap_file [in] Capture file.
189 * @param interval [in] Timing interval in ms.
190 * @param cur_idx [in] Current index.
191 * @param asAOT [in] Interpret when possible the value as an Average Over Time.
192 */
193double get_io_graph_item(const io_graph_item_t *items, io_graph_item_unit_t val_units, int idx, int hf_index, const capture_file *cap_file, int interval, int cur_idx, bool_Bool asAOT);
194
195/** Update the values of an io_graph_item_t.
196 *
197 * Frame and byte counts are always calculated. If edt is non-NULL advanced
198 * statistics are calculated using hfindex.
199 *
200 * @param items [in,out] Array containing the item to update.
201 * @param idx [in] Index of the item to update.
202 * @param pinfo [in] Packet containing update information.
203 * @param edt [in] Dissection information for advanced statistics. May be NULL.
204 * @param hf_index [in] Header field index for advanced statistics.
205 * @param item_unit [in] The type of unit to calculate. From IOG_ITEM_UNITS.
206 * @param interval [in] Timing interval in μs.
207 * @return true if the update was successful, otherwise false.
208 */
209static inline bool_Bool
210update_io_graph_item(io_graph_item_t *items, int idx, packet_info *pinfo, epan_dissect_t *edt, int hf_index, int item_unit, uint32_t interval) {
211 io_graph_item_t *item = &items[idx];
212
213 /* Set the first and last frame num in current interval matching the target field+filter */
214 if (item->first_frame_in_invl == 0) {
16
The left operand of '==' is a garbage value
215 item->first_frame_in_invl = pinfo->num;
216 }
217 item->last_frame_in_invl = pinfo->num;
218
219 if (edt && hf_index >= 0) {
220 GPtrArray *gp;
221 unsigned i;
222
223 gp = proto_get_finfo_ptr_array(edt->tree, hf_index);
224 if (!gp) {
225 return false0;
226 }
227
228 /* Update the appropriate counters. If fields == 0, this is the first seen
229 * value so set any min/max values accordingly. */
230 for (i=0; i < gp->len; i++) {
231 int64_t new_int64;
232 uint64_t new_uint64;
233 float new_float;
234 double new_double;
235 const nstime_t *new_time;
236
237 switch (proto_registrar_get_ftype(hf_index)) {
238 case FT_UINT8:
239 case FT_UINT16:
240 case FT_UINT24:
241 case FT_UINT32:
242 new_uint64 = fvalue_get_uinteger(((field_info *)gp->pdata[i])->value);
243
244 if ((new_uint64 > item->uint_max) || (item->fields == 0)) {
245 item->uint_max = new_uint64;
246 item->max_frame_in_invl = pinfo->num;
247 }
248 if ((new_uint64 < item->uint_min) || (item->fields == 0)) {
249 item->uint_min = new_uint64;
250 item->min_frame_in_invl = pinfo->num;
251 }
252 item->double_tot += (double)new_uint64;
253 item->fields++;
254 break;
255 case FT_INT8:
256 case FT_INT16:
257 case FT_INT24:
258 case FT_INT32:
259 new_int64 = fvalue_get_sinteger(((field_info *)gp->pdata[i])->value);
260 if ((new_int64 > item->int_max) || (item->fields == 0)) {
261 item->int_max = new_int64;
262 item->max_frame_in_invl = pinfo->num;
263 }
264 if ((new_int64 < item->int_min) || (item->fields == 0)) {
265 item->int_min = new_int64;
266 item->min_frame_in_invl = pinfo->num;
267 }
268 item->double_tot += (double)new_int64;
269 item->fields++;
270 break;
271 case FT_UINT40:
272 case FT_UINT48:
273 case FT_UINT56:
274 case FT_UINT64:
275 new_uint64 = fvalue_get_uinteger64(((field_info *)gp->pdata[i])->value);
276 if ((new_uint64 > item->uint_max) || (item->fields == 0)) {
277 item->uint_max = new_uint64;
278 item->max_frame_in_invl = pinfo->num;
279 }
280 if ((new_uint64 < item->uint_min) || (item->fields == 0)) {
281 item->uint_min = new_uint64;
282 item->min_frame_in_invl = pinfo->num;
283 }
284 item->double_tot += (double)new_uint64;
285 item->fields++;
286 break;
287 case FT_INT40:
288 case FT_INT48:
289 case FT_INT56:
290 case FT_INT64:
291 new_int64 = fvalue_get_sinteger64(((field_info *)gp->pdata[i])->value);
292 if ((new_int64 > item->int_max) || (item->fields == 0)) {
293 item->int_max = new_int64;
294 item->max_frame_in_invl = pinfo->num;
295 }
296 if ((new_int64 < item->int_min) || (item->fields == 0)) {
297 item->int_min = new_int64;
298 item->min_frame_in_invl = pinfo->num;
299 }
300 item->double_tot += (double)new_int64;
301 item->fields++;
302 break;
303 case FT_FLOAT:
304 new_float = (float)fvalue_get_floating(((field_info *)gp->pdata[i])->value);
305 if ((new_float > item->double_max) || (item->fields == 0)) {
306 item->double_max = new_float;
307 item->max_frame_in_invl = pinfo->num;
308 }
309 if ((new_float < item->double_min) || (item->fields == 0)) {
310 item->double_min = new_float;
311 item->min_frame_in_invl = pinfo->num;
312 }
313 item->double_tot += new_float;
314 item->fields++;
315 break;
316 case FT_DOUBLE:
317 new_double = fvalue_get_floating(((field_info *)gp->pdata[i])->value);
318 if ((new_double > item->double_max) || (item->fields == 0)) {
319 item->double_max = new_double;
320 item->max_frame_in_invl = pinfo->num;
321 }
322 if ((new_double < item->double_min) || (item->fields == 0)) {
323 item->double_min = new_double;
324 item->min_frame_in_invl = pinfo->num;
325 }
326 item->double_tot += new_double;
327 item->fields++;
328 break;
329 case FT_RELATIVE_TIME:
330 new_time = fvalue_get_time(((field_info *)gp->pdata[i])->value);
331
332 switch (item_unit) {
333 case IOG_ITEM_UNIT_CALC_LOAD:
334 {
335 uint64_t t, pt; /* time in us */
336 int j;
337 /*
338 * Add the time this call spanned each interval according to
339 * its contribution to that interval.
340 * If the call time is negative (unlikely, requires both an
341 * out of order capture file plus retransmission), ignore.
342 */
343 const nstime_t time_zero = NSTIME_INIT_ZERO{0, 0};
344 if (nstime_cmp(new_time, &time_zero) < 0) {
345 break;
346 }
347 t = new_time->secs;
348 t = t * 1000000 + new_time->nsecs / 1000;
349 j = idx;
350 /*
351 * Handle current interval
352 * This cannot be negative, because get_io_graph_index
353 * returns an invalid interval if so.
354 */
355 pt = pinfo->rel_ts.secs * 1000000 + pinfo->rel_ts.nsecs / 1000;
356 pt = pt % interval;
357 if (pt > t) {
358 pt = t;
359 }
360 while (t) {
361 io_graph_item_t *load_item;
362
363 load_item = &items[j];
364 load_item->time_tot.nsecs += (int) (pt * 1000);
365 if (load_item->time_tot.nsecs > 1000000000) {
366 load_item->time_tot.secs++;
367 load_item->time_tot.nsecs -= 1000000000;
368 }
369 load_item->fields++;
370
371 if (j == 0) {
372 break;
373 }
374 j--;
375 t -= pt;
376 if (t > (uint64_t) interval) {
377 pt = (uint64_t) interval;
378 } else {
379 pt = t;
380 }
381 }
382 break;
383 }
384 default:
385 if ( (nstime_cmp(new_time, &item->time_max) > 0)
386 || (item->fields == 0)) {
387 item->time_max = *new_time;
388 item->max_frame_in_invl = pinfo->num;
389 }
390 if ( (nstime_cmp(new_time, &item->time_min) < 0)
391 || (item->fields == 0)) {
392 item->time_min = *new_time;
393 item->min_frame_in_invl = pinfo->num;
394 }
395 nstime_add(&item->time_tot, new_time)nstime_sum(&item->time_tot, &item->time_tot, new_time
)
;
396 item->fields++;
397 }
398 break;
399 default:
400 if ((item_unit == IOG_ITEM_UNIT_CALC_FRAMES) ||
401 (item_unit == IOG_ITEM_UNIT_CALC_FIELDS)) {
402 /*
403 * It's not an integeresque type, but
404 * all we want to do is count it, so
405 * that's all right.
406 */
407 item->fields++;
408 }
409 else {
410 /*
411 * "Can't happen"; see the "check that the
412 * type is compatible" check in
413 * filter_callback().
414 */
415 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "ui/io_graph_item.h", 415
, __func__, "assertion \"not reached\" failed")
;
416 }
417 break;
418 }
419 }
420 }
421
422 item->frames++;
423 item->bytes += pinfo->fd->pkt_len;
424
425 return true1;
426}
427
428
429#ifdef __cplusplus
430}
431#endif /* __cplusplus */
432
433#endif /* __IO_GRAPH_ITEM_H__ */