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/2024-11-18-100252-3912-1 -Xclang -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2024-11-18-100252-3912-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 metadata */
1355 Buffer rec_buf; /* Record data */
1356
1357 analyser.first_time = NULL((void*)0);
1358 analyser.last_time = NULL((void*)0);
1359 analyser.protocols_set = g_hash_table_new(NULL((void*)0) /* g_direct_hash() */, NULL((void*)0) /* g_direct_equal */);
1360
1361 sharkd_json_result_prologue(rpcid);
1362
1363 sharkd_json_value_anyf("frames", "%u", cfile.count);
1364
1365 sharkd_json_array_open("protocols");
1366
1367 wtap_rec_init(&rec);
1368 ws_buffer_init(&rec_buf, 1514);
1369
1370 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1371 {
1372 enum dissect_request_status status;
1373 int err;
1374 char *err_info;
1375
1376 status = sharkd_dissect_request(framenum,
1377 (framenum != 1) ? 1 : 0, framenum - 1,
1378 &rec, &rec_buf, NULL((void*)0), SHARKD_DISSECT_FLAG_NULL0x00u,
1379 &sharkd_session_process_analyse_cb, &analyser,
1380 &err, &err_info);
1381 switch (status) {
1382
1383 case DISSECT_REQUEST_SUCCESS:
1384 break;
1385
1386 case DISSECT_REQUEST_NO_SUCH_FRAME:
1387 /* XXX - report the error. */
1388 break;
1389
1390 case DISSECT_REQUEST_READ_ERROR:
1391 /*
1392 * Free up the error string.
1393 * XXX - report the error.
1394 */
1395 g_free(err_info);
1396 break;
1397 }
1398 }
1399
1400 sharkd_json_array_close();
1401
1402 if (analyser.first_time)
1403 sharkd_json_value_anyf("first", "%.9f", nstime_to_sec(analyser.first_time));
1404
1405 if (analyser.last_time)
1406 sharkd_json_value_anyf("last", "%.9f", nstime_to_sec(analyser.last_time));
1407
1408 sharkd_json_result_epilogue();
1409
1410 wtap_rec_cleanup(&rec);
1411 ws_buffer_free(&rec_buf);
1412
1413 g_hash_table_destroy(analyser.protocols_set);
1414}
1415
1416static column_info *
1417sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
1418{
1419 const char *columns_custom[32];
1420 uint16_t columns_fmt[32];
1421 int16_t columns_occur[32];
1422
1423 int i, cols;
1424
1425 for (i = 0; i < 32; i++)
1426 {
1427 const char *tok_column;
1428 char tok_column_name[64];
1429 char *custom_sepa;
1430
1431 snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
1432 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
1433 if (tok_column == NULL((void*)0))
1434 break;
1435
1436 columns_custom[i] = NULL((void*)0);
1437 columns_occur[i] = 0;
1438
1439 if ((custom_sepa = strchr(tok_column, ':')))
1440 {
1441 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
1442
1443 columns_fmt[i] = COL_CUSTOM;
1444 columns_custom[i] = tok_column;
1445
1446 if (!ws_strtoi16(custom_sepa + 1, NULL((void*)0), &columns_occur[i]))
1447 return NULL((void*)0);
1448 }
1449 else
1450 {
1451 if (!ws_strtou16(tok_column, NULL((void*)0), &columns_fmt[i]))
1452 return NULL((void*)0);
1453
1454 if (columns_fmt[i] >= NUM_COL_FMTS)
1455 return NULL((void*)0);
1456
1457 /* if custom, that it shouldn't be just custom number -> error */
1458 if (columns_fmt[i] == COL_CUSTOM)
1459 return NULL((void*)0);
1460 }
1461 }
1462
1463 cols = i;
1464
1465 col_setup(cinfo, cols);
1466
1467 for (i = 0; i < cols; i++)
1468 {
1469 col_item_t *col_item = &cinfo->columns[i];
1470
1471 col_item->col_fmt = columns_fmt[i];
1472 col_item->col_title = NULL((void*)0); /* no need for title */
1473
1474 if (col_item->col_fmt == COL_CUSTOM)
1475 {
1476 col_item->col_custom_fields = g_strdup(columns_custom[i])g_strdup_inline (columns_custom[i]);
1477 col_item->col_custom_occurrence = columns_occur[i];
1478 }
1479
1480 col_item->col_fence = 0;
1481 }
1482
1483 col_finalize(cinfo);
1484
1485 return cinfo;
1486}
1487
1488static void
1489sharkd_session_process_frames_cb(epan_dissect_t *edt, proto_tree *tree _U___attribute__((unused)),
1490 struct epan_column_info *cinfo, const GSList *data_src _U___attribute__((unused)), void *data _U___attribute__((unused)))
1491{
1492 packet_info *pi = &edt->pi;
1493 frame_data *fdata = pi->fd;
1494 wtap_block_t pkt_block = NULL((void*)0);
1495 unsigned int i;
1496 char *comment = NULL((void*)0);
1497
1498 json_dumper_begin_object(&dumper);
1499
1500 sharkd_json_array_open("c");
1501 for (int col = 0; col < cinfo->num_cols; ++col)
1502 {
1503 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
1504 }
1505 sharkd_json_array_close();
1506
1507 sharkd_json_value_anyf("num", "%u", pi->num);
1508
1509 /*
1510 * Get the block for this record, if it has one.
1511 */
1512 pkt_block = sharkd_get_packet_block(fdata);
1513
1514 /*
1515 * Does this record have any comments?
1516 */
1517 if (pkt_block != NULL((void*)0) &&
1518 WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, 0, &comment))
1519 {
1520 sharkd_json_value_anyf("ct", "true");
1521
1522 sharkd_json_array_open("comments");
1523 for (i = 0; wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment) == WTAP_OPTTYPE_SUCCESS; i++) {
1524 sharkd_json_value_string(NULL((void*)0), comment);
1525 }
1526 sharkd_json_array_close();
1527 }
1528
1529 if (fdata->ignored)
1530 sharkd_json_value_anyf("i", "true");
1531
1532 if (fdata->marked)
1533 sharkd_json_value_anyf("m", "true");
1534
1535 if (fdata->color_filter)
1536 {
1537 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
1538 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
1539 }
1540
1541 wtap_block_unref(pkt_block);
1542 json_dumper_end_object(&dumper);
1543}
1544
1545/**
1546 * sharkd_session_process_frames()
1547 *
1548 * Process frames request
1549 *
1550 * Input:
1551 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurrence>).
1552 * If column0 is not specified default column set will be used.
1553 * (o) filter - filter to be used
1554 * (o) skip=N - skip N frames
1555 * (o) limit=N - show only N frames
1556 * (o) refs - list (comma separated) with sorted time reference frame numbers.
1557 *
1558 * Output array of frames with attributes:
1559 * (m) c - array of column data
1560 * (m) num - frame number
1561 * (o) i - if frame is ignored
1562 * (o) m - if frame is marked
1563 * (o) ct - if frame is commented
1564 * (o) comments - array of comment strings
1565 * (o) bg - color filter - background color in hex
1566 * (o) fg - color filter - foreground color in hex
1567 */
1568static void
1569sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
1570{
1571 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1572 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
1573 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
1574 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
1575 const char *tok_refs = json_find_attr(buf, tokens, count, "refs");
1576
1577 const uint8_t *filter_data = NULL((void*)0);
1578
1579 uint32_t prev_dis_num = 0;
1580 uint32_t current_ref_frame = 0, next_ref_frame = UINT32_MAX(4294967295U);
1581 uint32_t skip;
1582 uint32_t limit;
1583
1584 wtap_rec rec; /* Record metadata */
1585 Buffer rec_buf; /* Record data */
1586 column_info *cinfo = &cfile.cinfo;
1587 column_info user_cinfo;
1588
1589 if (tok_column)
1590 {
1591 memset(&user_cinfo, 0, sizeof(user_cinfo));
1592 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
1593 if (!cinfo)
1594 {
1595 sharkd_json_error(
1596 rpcid, -13001, NULL((void*)0),
1597 "Column definition invalid - note column 6 requires a custom definition"
1598 );
1599 return;
1600 }
1601 }
1602
1603 if (tok_filter)
1604 {
1605 const struct sharkd_filter_item *filter_item;
1606
1607 filter_item = sharkd_session_filter_data(tok_filter);
1608 if (!filter_item)
1609 {
1610 sharkd_json_error(
1611 rpcid, -13002, NULL((void*)0),
1612 "Filter expression invalid"
1613 );
1614 return;
1615 }
1616
1617 filter_data = filter_item->filtered;
1618 }
1619
1620 skip = 0;
1621 if (tok_skip)
1622 {
1623 if (!ws_strtou32(tok_skip, NULL((void*)0), &skip))
1624 return;
1625 }
1626
1627 limit = 0;
1628 if (tok_limit)
1629 {
1630 if (!ws_strtou32(tok_limit, NULL((void*)0), &limit))
1631 return;
1632 }
1633
1634 if (tok_refs)
1635 {
1636 if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame))
1637 return;
1638 }
1639
1640 sharkd_json_result_array_prologue(rpcid);
1641
1642 wtap_rec_init(&rec);
1643 ws_buffer_init(&rec_buf, 1514);
1644
1645 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1646 {
1647 frame_data *fdata;
1648 uint32_t ref_frame = (framenum != 1) ? 1 : 0;
1649 enum dissect_request_status status;
1650 int err;
1651 char *err_info;
1652
1653 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
1654 continue;
1655
1656 if (skip)
1657 {
1658 skip--;
1659 prev_dis_num = framenum;
1660 continue;
1661 }
1662
1663 if (tok_refs)
1664 {
1665 if (framenum >= next_ref_frame)
1666 {
1667 current_ref_frame = next_ref_frame;
1668
1669 if (*tok_refs != ',')
1670 next_ref_frame = UINT32_MAX(4294967295U);
1671
1672 while (*tok_refs == ',' && framenum >= next_ref_frame)
1673 {
1674 current_ref_frame = next_ref_frame;
1675
1676 if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame))
1677 {
1678 fprintf(stderrstderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs);
1679 break;
1680 }
1681 }
1682
1683 if (*tok_refs == '\0' && framenum >= next_ref_frame)
1684 {
1685 current_ref_frame = next_ref_frame;
1686 next_ref_frame = UINT32_MAX(4294967295U);
1687 }
1688 }
1689
1690 if (current_ref_frame)
1691 ref_frame = current_ref_frame;
1692 }
1693
1694 fdata = sharkd_get_frame(framenum);
1695 status = sharkd_dissect_request(framenum,
1696 ref_frame, prev_dis_num,
1697 &rec, &rec_buf, cinfo,
1698 (fdata->color_filter == NULL((void*)0)) ? SHARKD_DISSECT_FLAG_COLOR0x08u : SHARKD_DISSECT_FLAG_NULL0x00u,
1699 &sharkd_session_process_frames_cb, NULL((void*)0),
1700 &err, &err_info);
1701 switch (status) {
1702
1703 case DISSECT_REQUEST_SUCCESS:
1704 break;
1705
1706 case DISSECT_REQUEST_NO_SUCH_FRAME:
1707 /* XXX - report the error. */
1708 break;
1709
1710 case DISSECT_REQUEST_READ_ERROR:
1711 /*
1712 * Free up the error string.
1713 * XXX - report the error.
1714 */
1715 g_free(err_info);
1716 break;
1717 }
1718
1719 prev_dis_num = framenum;
1720
1721 if (limit && --limit == 0)
1722 break;
1723 }
1724 sharkd_json_result_array_epilogue();
1725
1726 if (cinfo != &cfile.cinfo)
1727 col_cleanup(cinfo);
1728
1729 wtap_rec_cleanup(&rec);
1730 ws_buffer_free(&rec_buf);
1731}
1732
1733static void
1734sharkd_session_process_tap_stats_node_cb(const char *key, const stat_node *n)
1735{
1736 stat_node *node;
1737
1738 sharkd_json_array_open(key);
1739 for (node = n->children; node; node = node->next)
1740 {
1741 json_dumper_begin_object(&dumper);
1742
1743 /* code based on stats_tree_get_values_from_node() */
1744 sharkd_json_value_string("name", node->name);
1745 sharkd_json_value_anyf("count", "%d", node->counter);
1746 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE0x10000000) || node->rng))
1747 {
1748 switch(node->datatype)
1749 {
1750 case STAT_DT_INT:
1751 sharkd_json_value_anyf("avg", "%.2f", ((float)node->total.int_total) / node->counter);
1752 sharkd_json_value_anyf("min", "%d", node->minvalue.int_min);
1753 sharkd_json_value_anyf("max", "%d", node->maxvalue.int_max);
1754 break;
1755 case STAT_DT_FLOAT:
1756 sharkd_json_value_anyf("avg", "%.2f", node->total.float_total / node->counter);
1757 sharkd_json_value_anyf("min", "%f", node->minvalue.float_min);
1758 sharkd_json_value_anyf("max", "%f", node->maxvalue.float_max);
1759 break;
1760 }
1761 }
1762
1763 if (node->st->elapsed)
1764 sharkd_json_value_anyf("rate", "%.4f", ((float)node->counter) / node->st->elapsed);
1765
1766 if (node->parent && node->parent->counter)
1767 sharkd_json_value_anyf("perc", "%.2f", (node->counter * 100.0) / node->parent->counter);
1768 else if (node->parent == &(node->st->root))
1769 sharkd_json_value_anyf("perc", "100");
1770
1771 if (prefs.st_enable_burstinfo && node->max_burst)
1772 {
1773 if (prefs.st_burst_showcount)
1774 sharkd_json_value_anyf("burstcount", "%d", node->max_burst);
1775 else
1776 sharkd_json_value_anyf("burstrate", "%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
1777
1778 sharkd_json_value_anyf("bursttime", "%.3f", (node->burst_time / 1000.0));
1779 }
1780
1781 if (node->children)
1782 {
1783 sharkd_session_process_tap_stats_node_cb("sub", node);
1784 }
1785 json_dumper_end_object(&dumper);
1786 }
1787 sharkd_json_array_close();
1788}
1789
1790/**
1791 * sharkd_session_process_tap_stats_cb()
1792 *
1793 * Output stats tap:
1794 *
1795 * (m) tap - tap name
1796 * (m) type:stats - tap output type
1797 * (m) name - stat name
1798 * (m) stats - array of object with attributes:
1799 * (m) name - stat item name
1800 * (m) count - stat item counter
1801 * (o) avg - stat item averange value
1802 * (o) min - stat item min value
1803 * (o) max - stat item max value
1804 * (o) rate - stat item rate value (ms)
1805 * (o) perc - stat item percentage
1806 * (o) burstrate - stat item burst rate
1807 * (o) burstcount - stat item burst count
1808 * (o) burstttme - stat item burst start
1809 * (o) sub - array of object with attributes like in stats node.
1810 */
1811static void
1812sharkd_session_process_tap_stats_cb(void *psp)
1813{
1814 stats_tree *st = (stats_tree *) psp;
1815
1816 json_dumper_begin_object(&dumper);
1817
1818 sharkd_json_value_stringf("tap", "stats:%s", st->cfg->abbr);
1819 sharkd_json_value_string("type", "stats");
1820 sharkd_json_value_string("name", st->cfg->path);
1821
1822 sharkd_session_process_tap_stats_node_cb("stats", &st->root);
1823
1824 json_dumper_end_object(&dumper);
1825}
1826
1827static void
1828sharkd_session_free_tap_stats_cb(void *psp)
1829{
1830 stats_tree *st = (stats_tree *) psp;
1831
1832 stats_tree_free(st);
1833}
1834
1835struct sharkd_expert_tap
1836{
1837 GSList *details;
1838 GStringChunk *text;
1839};
1840
1841/**
1842 * sharkd_session_process_tap_expert_cb()
1843 *
1844 * Output expert tap:
1845 *
1846 * (m) tap - tap name
1847 * (m) type:expert - tap output type
1848 * (m) details - array of object with attributes:
1849 * (m) f - frame number, which generated expert information
1850 * (o) s - severity
1851 * (o) g - group
1852 * (m) m - expert message
1853 * (o) p - protocol
1854 */
1855static void
1856sharkd_session_process_tap_expert_cb(void *tapdata)
1857{
1858 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1859 GSList *list;
1860
1861 json_dumper_begin_object(&dumper);
1862
1863 sharkd_json_value_string("tap", "expert");
1864 sharkd_json_value_string("type", "expert");
1865
1866 sharkd_json_array_open("details");
1867 for (list = etd->details; list; list = list->next)
1868 {
1869 expert_info_t *ei = (expert_info_t *) list->data;
1870 const char *tmp;
1871
1872 json_dumper_begin_object(&dumper);
1873
1874 sharkd_json_value_anyf("f", "%u", ei->packet_num);
1875
1876 tmp = try_val_to_str(ei->severity, expert_severity_vals);
1877 if (tmp)
1878 sharkd_json_value_string("s", tmp);
1879
1880 tmp = try_val_to_str(ei->group, expert_group_vals);
1881 if (tmp)
1882 sharkd_json_value_string("g", tmp);
1883
1884 sharkd_json_value_string("m", ei->summary);
1885
1886 if (ei->protocol)
1887 sharkd_json_value_string("p", ei->protocol);
1888
1889 json_dumper_end_object(&dumper);
1890 }
1891 sharkd_json_array_close();
1892
1893 json_dumper_end_object(&dumper);
1894}
1895
1896static tap_packet_status
1897sharkd_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)))
1898{
1899 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1900 const expert_info_t *ei = (const expert_info_t *) pointer;
1901 expert_info_t *ei_copy;
1902
1903 if (ei == NULL((void*)0))
1904 return TAP_PACKET_DONT_REDRAW;
1905
1906 ei_copy = g_new(expert_info_t, 1)((expert_info_t *) g_malloc_n ((1), sizeof (expert_info_t)));
1907 /* Note: this is a shallow copy */
1908 *ei_copy = *ei;
1909
1910 /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */
1911 ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
1912 ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary);
1913
1914 etd->details = g_slist_prepend(etd->details, ei_copy);
1915
1916 return TAP_PACKET_REDRAW;
1917}
1918
1919static void
1920sharkd_session_free_tap_expert_cb(void *tapdata)
1921{
1922 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1923
1924 g_slist_free_full(etd->details, g_free);
1925 g_string_chunk_free(etd->text);
1926 g_free(etd);
1927}
1928
1929/**
1930 * sharkd_session_process_tap_flow_cb()
1931 *
1932 * Output flow tap:
1933 * (m) tap - tap name
1934 * (m) type:flow - tap output type
1935 * (m) nodes - array of strings with node address
1936 * (m) flows - array of object with attributes:
1937 * (m) t - frame time string
1938 * (m) n - array of two numbers with source node index and destination node index
1939 * (m) pn - array of two numbers with source and destination port
1940 * (o) c - comment
1941 */
1942static void
1943sharkd_session_process_tap_flow_cb(void *tapdata)
1944{
1945 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1946 GList *flow_list;
1947 unsigned i;
1948
1949 sequence_analysis_get_nodes(graph_analysis);
1950
1951 json_dumper_begin_object(&dumper);
1952 sharkd_json_value_stringf("tap", "seqa:%s", graph_analysis->name);
1953 sharkd_json_value_string("type", "flow");
1954
1955 sharkd_json_array_open("nodes");
1956 for (i = 0; i < graph_analysis->num_nodes; i++)
1957 {
1958 char *addr_str;
1959
1960 addr_str = address_to_display(NULL((void*)0), &(graph_analysis->nodes[i]));
1961 sharkd_json_value_string(NULL((void*)0), addr_str);
1962 wmem_free(NULL((void*)0), addr_str);
1963 }
1964 sharkd_json_array_close();
1965
1966 sharkd_json_array_open("flows");
1967 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
1968 while (flow_list)
1969 {
1970 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
1971
1972 flow_list = g_list_next(flow_list)((flow_list) ? (((GList *)(flow_list))->next) : ((void*)0)
)
;
1973
1974 if (!sai->display)
1975 continue;
1976
1977 json_dumper_begin_object(&dumper);
1978
1979 sharkd_json_value_string("t", sai->time_str);
1980 sharkd_json_value_anyf("n", "[%u,%u]", sai->src_node, sai->dst_node);
1981 sharkd_json_value_anyf("pn", "[%u,%u]", sai->port_src, sai->port_dst);
1982
1983 if (sai->comment)
1984 sharkd_json_value_string("c", sai->comment);
1985
1986 json_dumper_end_object(&dumper);
1987 }
1988 sharkd_json_array_close();
1989
1990 json_dumper_end_object(&dumper);
1991}
1992
1993static void
1994sharkd_session_free_tap_flow_cb(void *tapdata)
1995{
1996 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1997
1998 sequence_analysis_info_free(graph_analysis);
1999}
2000
2001struct sharkd_conv_tap_data
2002{
2003 const char *type;
2004 conv_hash_t hash;
2005 bool_Bool resolve_name;
2006 bool_Bool resolve_port;
2007};
2008
2009static bool_Bool
2010sharkd_session_geoip_addr(address *addr, const char *suffix)
2011{
2012 const mmdb_lookup_t *lookup = NULL((void*)0);
2013 bool_Bool with_geoip = false0;
2014 char json_key[64];
2015
2016 if (addr->type == AT_IPv4)
2017 {
2018 const ws_in4_addr *ip4 = (const ws_in4_addr *) addr->data;
2019
2020 lookup = maxmind_db_lookup_ipv4(ip4);
2021 }
2022 else if (addr->type == AT_IPv6)
2023 {
2024 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
2025
2026 lookup = maxmind_db_lookup_ipv6(ip6);
2027 }
2028
2029 if (!lookup || !lookup->found)
2030 return false0;
2031
2032 if (lookup->country)
2033 {
2034 snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix);
2035 sharkd_json_value_string(json_key, lookup->country);
2036 with_geoip = true1;
2037 }
2038
2039 if (lookup->country_iso)
2040 {
2041 snprintf(json_key, sizeof(json_key), "geoip_country_iso%s", suffix);
2042 sharkd_json_value_string(json_key, lookup->country_iso);
2043 with_geoip = true1;
2044 }
2045
2046 if (lookup->city)
2047 {
2048 snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix);
2049 sharkd_json_value_string(json_key, lookup->city);
2050 with_geoip = true1;
2051 }
2052
2053 if (lookup->as_org)
2054 {
2055 snprintf(json_key, sizeof(json_key), "geoip_as_org%s", suffix);
2056 sharkd_json_value_string(json_key, lookup->as_org);
2057 with_geoip = true1;
2058 }
2059
2060 if (lookup->as_number > 0)
2061 {
2062 snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix);
2063 sharkd_json_value_anyf(json_key, "%u", lookup->as_number);
2064 with_geoip = true1;
2065 }
2066
2067 if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
2068 {
2069 snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix);
2070 sharkd_json_value_anyf(json_key, "%f", lookup->latitude);
2071 with_geoip = true1;
2072 }
2073
2074 if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
2075 {
2076 snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix);
2077 sharkd_json_value_anyf(json_key, "%f", lookup->longitude);
2078 with_geoip = true1;
2079 }
2080
2081 return with_geoip;
2082}
2083
2084struct sharkd_analyse_rtp_items
2085{
2086 uint32_t frame_num;
2087 uint32_t sequence_num;
2088
2089 double delta;
2090 double jitter;
2091 double skew;
2092 double bandwidth;
2093 bool_Bool marker;
2094
2095 double arrive_offset;
2096
2097 /* from tap_rtp_stat_t */
2098 uint32_t flags;
2099 uint16_t pt;
2100};
2101
2102struct sharkd_analyse_rtp
2103{
2104 const char *tap_name;
2105 rtpstream_id_t id;
2106
2107 GSList *packets;
2108 double start_time;
2109 tap_rtp_stat_t statinfo;
2110};
2111
2112static void
2113sharkd_session_process_tap_rtp_free_cb(void *tapdata)
2114{
2115 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2116
2117 g_slist_free_full(rtp_req->packets, g_free);
2118 g_free(rtp_req);
2119}
2120
2121static tap_packet_status
2122sharkd_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)))
2123{
2124 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2125 const struct _rtp_info *rtp_info = (const struct _rtp_info *) pointer;
2126
2127 if (rtpstream_id_equal_pinfo_rtp_info(&rtp_req->id, pinfo, rtp_info))
2128 {
2129 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
2130 struct sharkd_analyse_rtp_items *item;
2131
2132 rtppacket_analyse(statinfo, pinfo, rtp_info);
2133
2134 item = g_new(struct sharkd_analyse_rtp_items, 1)((struct sharkd_analyse_rtp_items *) g_malloc_n ((1), sizeof (
struct sharkd_analyse_rtp_items)))
;
2135
2136 if (!rtp_req->packets)
2137 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
2138
2139 item->frame_num = pinfo->num;
2140 item->sequence_num = rtp_info->info_seq_num;
2141 item->delta = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->delta;
2142 item->jitter = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->jitter;
2143 item->skew = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->skew;
2144 item->bandwidth = statinfo->bandwidth;
2145 item->marker = rtp_info->info_marker_set ? true1 : false0;
2146 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
2147
2148 item->flags = statinfo->flags;
2149 item->pt = statinfo->pt;
2150
2151 /* XXX, O(n) optimize */
2152 rtp_req->packets = g_slist_append(rtp_req->packets, item);
2153 }
2154
2155 return TAP_PACKET_REDRAW;
2156}
2157
2158/**
2159 * sharkd_session_process_tap_rtp_analyse_cb()
2160 *
2161 * Output rtp analyse tap:
2162 * (m) tap - tap name
2163 * (m) type - tap output type
2164 * (m) ssrc - RTP SSRC
2165 * (m) max_delta - Max delta (ms)
2166 * (m) max_delta_nr - Max delta packet #
2167 * (m) max_jitter - Max jitter (ms)
2168 * (m) mean_jitter - Mean jitter (ms)
2169 * (m) max_skew - Max skew (ms)
2170 * (m) total_nr - Total number of RTP packets
2171 * (m) seq_err - Number of sequence errors
2172 * (m) duration - Duration (ms)
2173 * (m) items - array of object with attributes:
2174 * (m) f - frame number
2175 * (m) o - arrive offset
2176 * (m) sn - sequence number
2177 * (m) d - delta
2178 * (m) j - jitter
2179 * (m) sk - skew
2180 * (m) bw - bandwidth
2181 * (o) s - status string
2182 * (o) t - status type
2183 * (o) mark - rtp mark
2184 */
2185static void
2186sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
2187{
2188 const int RTP_TYPE_CN = 1;
2189 const int RTP_TYPE_ERROR = 2;
2190 const int RTP_TYPE_WARN = 3;
2191 const int RTP_TYPE_PT_EVENT = 4;
2192
2193 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2194 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
2195
2196 GSList *l;
2197
2198 json_dumper_begin_object(&dumper);
2199
2200 sharkd_json_value_string("tap", rtp_req->tap_name);
2201 sharkd_json_value_string("type", "rtp-analyse");
2202 sharkd_json_value_stringf("ssrc", "0x%x", rtp_req->id.ssrc);
2203
2204 sharkd_json_value_anyf("max_delta", "%f", statinfo->max_delta);
2205 sharkd_json_value_anyf("max_delta_nr", "%u", statinfo->max_nr);
2206 sharkd_json_value_anyf("max_jitter", "%f", statinfo->max_jitter);
2207 sharkd_json_value_anyf("mean_jitter", "%f", statinfo->mean_jitter);
2208 sharkd_json_value_anyf("max_skew", "%f", statinfo->max_skew);
2209 sharkd_json_value_anyf("total_nr", "%u", statinfo->total_nr);
2210 sharkd_json_value_anyf("seq_err", "%u", statinfo->sequence);
2211 sharkd_json_value_anyf("duration", "%f", statinfo->time - statinfo->start_time);
2212
2213 sharkd_json_array_open("items");
2214 for (l = rtp_req->packets; l; l = l->next)
2215 {
2216 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
2217
2218 json_dumper_begin_object(&dumper);
2219
2220 sharkd_json_value_anyf("f", "%u", item->frame_num);
2221 sharkd_json_value_anyf("o", "%.9f", item->arrive_offset);
2222 sharkd_json_value_anyf("sn", "%u", item->sequence_num);
2223 sharkd_json_value_anyf("d", "%.2f", item->delta);
2224 sharkd_json_value_anyf("j", "%.2f", item->jitter);
2225 sharkd_json_value_anyf("sk", "%.2f", item->skew);
2226 sharkd_json_value_anyf("bw", "%.2f", item->bandwidth);
2227
2228 if (item->pt == PT_CN13)
2229 {
2230 sharkd_json_value_string("s", "Comfort noise (PT=13, RFC 3389)");
2231 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2232 }
2233 else if (item->pt == PT_CN_OLD19)
2234 {
2235 sharkd_json_value_string("s", "Comfort noise (PT=19, reserved)");
2236 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2237 }
2238 else if (item->flags & STAT_FLAG_WRONG_SEQ0x004)
2239 {
2240 sharkd_json_value_string("s", "Wrong sequence number");
2241 sharkd_json_value_anyf("t", "%d", RTP_TYPE_ERROR);
2242 }
2243 else if (item->flags & STAT_FLAG_DUP_PKT0x200)
2244 {
2245 sharkd_json_value_string("s", "Suspected duplicate (MAC address) only delta time calculated");
2246 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2247 }
2248 else if (item->flags & STAT_FLAG_REG_PT_CHANGE0x040)
2249 {
2250 sharkd_json_value_stringf("s", "Payload changed to PT=%u%s",
2251 item->pt,
2252 (item->flags & STAT_FLAG_PT_T_EVENT0x100) ? " telephone/event" : "");
2253 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2254 }
2255 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP0x080)
2256 {
2257 sharkd_json_value_string("s", "Incorrect timestamp");
2258 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2259 }
2260 else if ((item->flags & STAT_FLAG_PT_CHANGE0x008)
2261 && !(item->flags & STAT_FLAG_FIRST0x001)
2262 && !(item->flags & STAT_FLAG_PT_CN0x010)
2263 && (item->flags & STAT_FLAG_FOLLOW_PT_CN0x020)
2264 && !(item->flags & STAT_FLAG_MARKER0x002))
2265 {
2266 sharkd_json_value_string("s", "Marker missing?");
2267 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2268 }
2269 else if (item->flags & STAT_FLAG_PT_T_EVENT0x100)
2270 {
2271 sharkd_json_value_stringf("s", "PT=%u telephone/event", item->pt);
2272 sharkd_json_value_anyf("t", "%d", RTP_TYPE_PT_EVENT);
2273 }
2274 else if (item->flags & STAT_FLAG_MARKER0x002)
2275 {
2276 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2277 }
2278
2279 if (item->marker)
2280 sharkd_json_value_anyf("mark", "1");
2281
2282 json_dumper_end_object(&dumper);
2283 }
2284 sharkd_json_array_close();
2285
2286 json_dumper_end_object(&dumper);
2287}
2288
2289/**
2290 * sharkd_session_process_tap_conv_cb()
2291 *
2292 * Output conv tap:
2293 * (m) tap - tap name
2294 * (m) type - tap output type
2295 * (m) proto - protocol short name
2296 * (o) filter - filter string
2297 * (o) geoip - whether GeoIP information is available, boolean
2298 *
2299 * (o) convs - array of object with attributes:
2300 * (m) saddr - source address
2301 * (m) daddr - destination address
2302 * (o) sport - source port
2303 * (o) dport - destination port
2304 * (m) txf - TX frame count
2305 * (m) txb - TX bytes
2306 * (m) rxf - RX frame count
2307 * (m) rxb - RX bytes
2308 * (m) start - (relative) first packet time
2309 * (m) stop - (relative) last packet time
2310 * (o) filter - conversation filter
2311 *
2312 * (o) hosts - array of object with attributes:
2313 * (m) host - host address
2314 * (o) port - host port
2315 * (m) txf - TX frame count
2316 * (m) txb - TX bytes
2317 * (m) rxf - RX frame count
2318 * (m) rxb - RX bytes
2319 */
2320static void
2321sharkd_session_process_tap_conv_cb(void *arg)
2322{
2323 conv_hash_t *hash = (conv_hash_t *) arg;
2324 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2325 const char *proto;
2326 int proto_with_port;
2327 unsigned i;
2328
2329 int with_geoip = 0;
2330
2331 json_dumper_begin_object(&dumper);
2332 sharkd_json_value_string("tap", iu->type);
2333
2334 if (!strncmp(iu->type, "conv:", 5))
2335 {
2336 sharkd_json_value_string("type", "conv");
2337 sharkd_json_array_open("convs");
2338 proto = iu->type + 5;
2339 }
2340 else if (!strncmp(iu->type, "endpt:", 6))
2341 {
2342 sharkd_json_value_string("type", "host");
2343 sharkd_json_array_open("hosts");
2344 proto = iu->type + 6;
2345 }
2346 else
2347 {
2348 sharkd_json_value_string("type", "err");
2349 proto = "";
2350 }
2351
2352 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
2353
2354 if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "conv:", 5))
2355 {
2356 for (i = 0; i < iu->hash.conv_array->len; i++)
2357 {
2358 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)])
;
2359 char *src_addr, *dst_addr;
2360 char *src_port, *dst_port;
2361 char *filter_str;
2362
2363 json_dumper_begin_object(&dumper);
2364
2365 sharkd_json_value_string("saddr", (src_addr = get_conversation_address(NULL((void*)0), &iui->src_address, iu->resolve_name)));
2366 sharkd_json_value_string("daddr", (dst_addr = get_conversation_address(NULL((void*)0), &iui->dst_address, iu->resolve_name)));
2367
2368 if (proto_with_port)
2369 {
2370 sharkd_json_value_string("sport", (src_port = get_conversation_port(NULL((void*)0), iui->src_port, iui->ctype, iu->resolve_port)));
2371 sharkd_json_value_string("dport", (dst_port = get_conversation_port(NULL((void*)0), iui->dst_port, iui->ctype, iu->resolve_port)));
2372
2373 wmem_free(NULL((void*)0), src_port);
2374 wmem_free(NULL((void*)0), dst_port);
2375 }
2376
2377 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", iui->rx_frames);
2378 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", iui->rx_bytes);
2379
2380 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", iui->tx_frames);
2381 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", iui->tx_bytes);
2382
2383 sharkd_json_value_anyf("start", "%.9f", nstime_to_sec(&iui->start_time));
2384 sharkd_json_value_anyf("stop", "%.9f", nstime_to_sec(&iui->stop_time));
2385
2386 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
2387 if (filter_str)
2388 {
2389 sharkd_json_value_string("filter", filter_str);
2390 g_free(filter_str);
2391 }
2392
2393 wmem_free(NULL((void*)0), src_addr);
2394 wmem_free(NULL((void*)0), dst_addr);
2395
2396 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
2397 with_geoip = 1;
2398 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
2399 with_geoip = 1;
2400
2401 json_dumper_end_object(&dumper);
2402 }
2403 }
2404 else if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "endpt:", 6))
2405 {
2406 for (i = 0; i < iu->hash.conv_array->len; i++)
2407 {
2408 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)])
;
2409 char *host_str, *port_str;
2410 char *filter_str;
2411
2412 json_dumper_begin_object(&dumper);
2413
2414 sharkd_json_value_string("host", (host_str = get_conversation_address(NULL((void*)0), &endpoint->myaddress, iu->resolve_name)));
2415
2416 if (proto_with_port)
2417 {
2418 sharkd_json_value_string("port", (port_str = get_endpoint_port(NULL((void*)0), endpoint, iu->resolve_port)));
2419
2420 wmem_free(NULL((void*)0), port_str);
2421 }
2422
2423 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", endpoint->rx_frames);
2424 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", endpoint->rx_bytes);
2425
2426 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", endpoint->tx_frames);
2427 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", endpoint->tx_bytes);
2428
2429 filter_str = get_endpoint_filter(endpoint);
2430 if (filter_str)
2431 {
2432 sharkd_json_value_string("filter", filter_str);
2433 g_free(filter_str);
2434 }
2435
2436 wmem_free(NULL((void*)0), host_str);
2437
2438 if (sharkd_session_geoip_addr(&(endpoint->myaddress), ""))
2439 with_geoip = 1;
2440 json_dumper_end_object(&dumper);
2441 }
2442 }
2443 sharkd_json_array_close();
2444
2445 sharkd_json_value_string("proto", proto);
2446 sharkd_json_value_anyf("geoip", with_geoip ? "true" : "false");
2447
2448 json_dumper_end_object(&dumper);
2449}
2450
2451static void
2452sharkd_session_free_tap_conv_cb(void *arg)
2453{
2454 conv_hash_t *hash = (conv_hash_t *) arg;
2455 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2456
2457 if (!strncmp(iu->type, "conv:", 5))
2458 {
2459 reset_conversation_table_data(hash);
2460 }
2461 else if (!strncmp(iu->type, "endpt:", 6))
2462 {
2463 reset_endpoint_table_data(hash);
2464 }
2465
2466 g_free(iu);
2467}
2468
2469/**
2470 * sharkd_session_process_tap_nstat_cb()
2471 *
2472 * Output nstat tap:
2473 * (m) tap - tap name
2474 * (m) type - tap output type
2475 * (m) fields: array of objects with attributes:
2476 * (m) c - name
2477 *
2478 * (m) tables: array of object with attributes:
2479 * (m) t - table title
2480 * (m) i - array of items
2481 */
2482static void
2483sharkd_session_process_tap_nstat_cb(void *arg)
2484{
2485 stat_data_t *stat_data = (stat_data_t *) arg;
2486 unsigned i, j, k;
2487
2488 json_dumper_begin_object(&dumper);
2489 sharkd_json_value_stringf("tap", "nstat:%s", stat_data->stat_tap_data->cli_string);
2490 sharkd_json_value_string("type", "nstat");
2491
2492 sharkd_json_array_open("fields");
2493 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
2494 {
2495 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
2496
2497 json_dumper_begin_object(&dumper);
2498 sharkd_json_value_string("c", field->column_name);
2499 json_dumper_end_object(&dumper);
2500 }
2501 sharkd_json_array_close();
2502
2503 sharkd_json_array_open("tables");
2504 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
2505 {
2506 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)])
;
2507
2508 json_dumper_begin_object(&dumper);
2509
2510 sharkd_json_value_string("t", table->title);
2511
2512 sharkd_json_array_open("i");
2513 for (j = 0; j < table->num_elements; j++)
2514 {
2515 stat_tap_table_item_type *field_data;
2516
2517 field_data = stat_tap_get_field_data(table, j, 0);
2518 if (field_data == NULL((void*)0) || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
2519 continue;
2520
2521 sharkd_json_array_open(NULL((void*)0));
2522 for (k = 0; k < table->num_fields; k++)
2523 {
2524 field_data = stat_tap_get_field_data(table, j, k);
2525
2526 switch (field_data->type)
2527 {
2528 case TABLE_ITEM_UINT:
2529 sharkd_json_value_anyf(NULL((void*)0), "%u", field_data->value.uint_value);
2530 break;
2531
2532 case TABLE_ITEM_INT:
2533 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.int_value);
2534 break;
2535
2536 case TABLE_ITEM_STRING:
2537 sharkd_json_value_string(NULL((void*)0), field_data->value.string_value);
2538 break;
2539
2540 case TABLE_ITEM_FLOAT:
2541 sharkd_json_value_anyf(NULL((void*)0), "%f", field_data->value.float_value);
2542 break;
2543
2544 case TABLE_ITEM_ENUM:
2545 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.enum_value);
2546 break;
2547
2548 case TABLE_ITEM_NONE:
2549 sharkd_json_value_anyf(NULL((void*)0), "null");
2550 break;
2551 }
2552 }
2553
2554 sharkd_json_array_close();
2555 }
2556 sharkd_json_array_close();
2557 json_dumper_end_object(&dumper);
2558 }
2559 sharkd_json_array_close();
2560
2561 json_dumper_end_object(&dumper);
2562}
2563
2564static void
2565sharkd_session_free_tap_nstat_cb(void *arg)
2566{
2567 stat_data_t *stat_data = (stat_data_t *) arg;
2568
2569 free_stat_tables(stat_data->stat_tap_data);
2570}
2571
2572/**
2573 * sharkd_session_process_tap_rtd_cb()
2574 *
2575 * Output rtd tap:
2576 * (m) tap - tap name
2577 * (m) type - tap output type
2578 * (m) stats - statistics rows - array object with attributes:
2579 * (m) type - statistic name
2580 * (m) num - number of messages
2581 * (m) min - minimum SRT time
2582 * (m) max - maximum SRT time
2583 * (m) tot - total SRT time
2584 * (m) min_frame - minimal SRT
2585 * (m) max_frame - maximum SRT
2586 * (o) open_req - Open Requests
2587 * (o) disc_rsp - Discarded Responses
2588 * (o) req_dup - Duplicated Requests
2589 * (o) rsp_dup - Duplicated Responses
2590 * (o) open_req - Open Requests
2591 * (o) disc_rsp - Discarded Responses
2592 * (o) req_dup - Duplicated Requests
2593 * (o) rsp_dup - Duplicated Responses
2594 */
2595static void
2596sharkd_session_process_tap_rtd_cb(void *arg)
2597{
2598 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2599 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
2600
2601 unsigned i, j;
2602
2603 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
2604
2605 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
2606 * some multiple table and single timestat (radius, h225)
2607 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
2608 * I wonder how it will gonna work with multiple timestats and multiple tables...
2609 * (for usage grep for: register_rtd_table)
2610 */
2611 const value_string *vs = get_rtd_value_string(rtd);
2612
2613 json_dumper_begin_object(&dumper);
2614 sharkd_json_value_stringf("tap", "rtd:%s", filter);
2615 sharkd_json_value_string("type", "rtd");
2616
2617 if (rtd_data->stat_table.num_rtds == 1)
2618 {
2619 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
2620
2621 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2622 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2623 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2624 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2625 }
2626
2627 sharkd_json_array_open("stats");
2628 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
2629 {
2630 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
2631
2632 for (j = 0; j < ms->num_timestat; j++)
2633 {
2634 const char *type_str;
2635
2636 if (ms->rtd[j].num == 0)
2637 continue;
2638
2639 json_dumper_begin_object(&dumper);
2640
2641 if (rtd_data->stat_table.num_rtds == 1)
2642 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
2643 else
2644 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
2645 sharkd_json_value_string("type", type_str);
2646
2647 sharkd_json_value_anyf("num", "%u", ms->rtd[j].num);
2648 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&(ms->rtd[j].min)));
2649 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&(ms->rtd[j].max)));
2650 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
2651 sharkd_json_value_anyf("min_frame", "%u", ms->rtd[j].min_num);
2652 sharkd_json_value_anyf("max_frame", "%u", ms->rtd[j].max_num);
2653
2654 if (rtd_data->stat_table.num_rtds != 1)
2655 {
2656 /* like in tshark, display it on every row */
2657 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2658 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2659 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2660 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2661 }
2662
2663 json_dumper_end_object(&dumper);
2664 }
2665 }
2666 sharkd_json_array_close();
2667
2668 json_dumper_end_object(&dumper);
2669}
2670
2671static void
2672sharkd_session_free_tap_rtd_cb(void *arg)
2673{
2674 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2675
2676 free_rtd_table(&rtd_data->stat_table);
2677 g_free(rtd_data);
2678}
2679
2680/**
2681 * sharkd_session_process_tap_srt_cb()
2682 *
2683 * Output srt tap:
2684 * (m) tap - tap name
2685 * (m) type - tap output type
2686 *
2687 * (m) tables - array of object with attributes:
2688 * (m) n - table name
2689 * (m) f - table filter
2690 * (o) c - table column name
2691 * (m) r - table rows - array object with attributes:
2692 * (m) n - row name
2693 * (m) idx - procedure index
2694 * (m) num - number of events
2695 * (m) min - minimum SRT time
2696 * (m) max - maximum SRT time
2697 * (m) tot - total SRT time
2698 */
2699static void
2700sharkd_session_process_tap_srt_cb(void *arg)
2701{
2702 srt_data_t *srt_data = (srt_data_t *) arg;
2703 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2704
2705 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
2706
2707 unsigned i;
2708
2709 json_dumper_begin_object(&dumper);
2710 sharkd_json_value_stringf("tap", "srt:%s", filter);
2711 sharkd_json_value_string("type", "srt");
2712
2713 sharkd_json_array_open("tables");
2714 for (i = 0; i < srt_data->srt_array->len; i++)
2715 {
2716 /* SRT table */
2717 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)])
;
2718
2719 int j;
2720
2721 json_dumper_begin_object(&dumper);
2722
2723 if (rst->name)
2724 sharkd_json_value_string("n", rst->name);
2725 else if (rst->short_name)
2726 sharkd_json_value_string("n", rst->short_name);
2727 else
2728 sharkd_json_value_stringf("n", "table%u", i);
2729
2730 if (rst->filter_string)
2731 sharkd_json_value_string("f", rst->filter_string);
2732
2733 if (rst->proc_column_name)
2734 sharkd_json_value_string("c", rst->proc_column_name);
2735
2736 sharkd_json_array_open("r");
2737 for (j = 0; j < rst->num_procs; j++)
2738 {
2739 /* SRT row */
2740 srt_procedure_t *proc = &rst->procedures[j];
2741
2742 if (proc->stats.num == 0)
2743 continue;
2744
2745 json_dumper_begin_object(&dumper);
2746
2747 sharkd_json_value_string("n", proc->procedure);
2748
2749 if (rst->filter_string)
2750 sharkd_json_value_anyf("idx", "%d", proc->proc_index);
2751
2752 sharkd_json_value_anyf("num", "%u", proc->stats.num);
2753
2754 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&proc->stats.min));
2755 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&proc->stats.max));
2756 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&proc->stats.tot));
2757
2758 json_dumper_end_object(&dumper);
2759 }
2760 sharkd_json_array_close();
2761
2762 json_dumper_end_object(&dumper);
2763 }
2764 sharkd_json_array_close();
2765
2766 json_dumper_end_object(&dumper);
2767}
2768
2769static void
2770sharkd_session_free_tap_srt_cb(void *arg)
2771{
2772 srt_data_t *srt_data = (srt_data_t *) arg;
2773 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2774
2775 free_srt_table(srt, srt_data->srt_array);
2776 g_array_free(srt_data->srt_array, TRUE(!(0)));
2777 g_free(srt_data);
2778}
2779
2780static void
2781sharkd_session_process_tap_phs_cb_aux(phs_t *rs)
2782{
2783 for (; rs; rs = rs->sibling) {
2784 if (rs->protocol == -1) {
2785 return;
2786 }
2787 sharkd_json_object_open(NULL((void*)0));
2788 sharkd_json_value_string("proto", rs->proto_name);
2789 sharkd_json_value_anyf("frames", "%"PRIu32"u", rs->frames);
2790 sharkd_json_value_anyf("bytes", "%"PRIu64"l" "u", rs->bytes);
2791 if (rs->child != NULL((void*)0) && rs->child->protocol != -1) {
2792 sharkd_json_array_open("protos");
2793 sharkd_session_process_tap_phs_cb_aux(rs->child);
2794 sharkd_json_array_close();
2795 }
2796 sharkd_json_object_close();
2797 }
2798}
2799
2800/**
2801 * sharkd_session_process_tap_phs_cb()
2802 *
2803 * Output phs tap:
2804 * (m) tap - tap name
2805 * (m) type - tap output type
2806 * (m) filter - tap filter argument
2807 * (m) protos - array of proto objects
2808 *
2809 * proto object:
2810 * (m) proto - protocol name
2811 * (m) frames - frame count
2812 * (m) bytes - bytes count
2813 * (o) protos - array of proto objects
2814 */
2815static void
2816sharkd_session_process_tap_phs_cb(void *arg)
2817{
2818 phs_t *rs = (phs_t *)arg;
2819 sharkd_json_object_open(NULL((void*)0));
2820 sharkd_json_value_string("tap", "phs");
2821 sharkd_json_value_string("type", "phs");
2822 sharkd_json_value_string("filter", rs->filter ? rs->filter : "");
2823 sharkd_json_array_open("protos");
2824 sharkd_session_process_tap_phs_cb_aux(rs);
2825 sharkd_json_array_close();
2826 sharkd_json_object_close();
2827}
2828
2829static void
2830sharkd_session_free_tap_phs_cb(void *arg)
2831{
2832 phs_t *rs = (phs_t *)arg;
2833 free_phs(rs);
2834}
2835
2836struct sharkd_export_object_list
2837{
2838 struct sharkd_export_object_list *next;
2839
2840 char *type;
2841 const char *proto;
2842 GSList *entries;
2843};
2844
2845static struct sharkd_export_object_list *sharkd_eo_list;
2846
2847/**
2848 * sharkd_session_process_tap_eo_cb()
2849 *
2850 * Output eo tap:
2851 * (m) tap - tap name
2852 * (m) type - tap output type
2853 * (m) proto - protocol short name
2854 * (m) objects - array of object with attributes:
2855 * (m) pkt - packet number
2856 * (o) hostname - hostname
2857 * (o) type - content type
2858 * (o) filename - filename
2859 * (m) len - object length
2860 * (m) sha1 - object's sha1 sum
2861 */
2862static void
2863sharkd_session_process_tap_eo_cb(void *tapdata)
2864{
2865 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
2866 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data;
2867 GSList *slist;
2868 int i = 0;
2869 char sha1sum_bytes[HASH_SHA1_LENGTH20], *sha1sum_str;
2870
2871 json_dumper_begin_object(&dumper);
2872 sharkd_json_value_string("tap", object_list->type);
2873 sharkd_json_value_string("type", "eo");
2874
2875 sharkd_json_value_string("proto", object_list->proto);
2876
2877 sharkd_json_array_open("objects");
2878 for (slist = object_list->entries; slist; slist = slist->next)
2879 {
2880 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
2881
2882 json_dumper_begin_object(&dumper);
2883
2884 sharkd_json_value_anyf("pkt", "%u", eo_entry->pkt_num);
2885
2886 if (eo_entry->hostname)
2887 sharkd_json_value_string("hostname", eo_entry->hostname);
2888
2889 if (eo_entry->content_type)
2890 sharkd_json_value_string("type", eo_entry->content_type);
2891
2892 if (eo_entry->filename)
2893 sharkd_json_value_string("filename", eo_entry->filename);
2894
2895 sharkd_json_value_stringf("_download", "%s_%d", object_list->type, i);
2896
2897 sharkd_json_value_anyf("len", "%zu", eo_entry->payload_len);
2898
2899 gcry_md_hash_buffer(GCRY_MD_SHA1, sha1sum_bytes, eo_entry->payload_data, eo_entry->payload_len);
2900 sha1sum_str = bytes_to_str(NULL, sha1sum_bytes, HASH_SHA1_LENGTH)bytes_to_str_maxlen(((void*)0), sha1sum_bytes, 20, 36);
2901 sharkd_json_value_string("sha1", sha1sum_str);
2902 g_free(sha1sum_str);
2903
2904 json_dumper_end_object(&dumper);
2905
2906 i++;
2907 }
2908 sharkd_json_array_close();
2909
2910 json_dumper_end_object(&dumper);
2911}
2912
2913static void
2914sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
2915{
2916 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2917
2918 object_list->entries = g_slist_append(object_list->entries, entry);
2919}
2920
2921static export_object_entry_t *
2922sharkd_eo_object_list_get_entry(void *gui_data, int row)
2923{
2924 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2925
2926 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
2927}
2928
2929static struct sharkd_export_object_list *
2930sharkd_eo_object_list_get_entry_by_type(void *gui_data, const char *tap_type)
2931{
2932 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2933 for (; object_list; object_list = object_list->next)
2934 {
2935 if (!strcmp(object_list->type, tap_type))
2936 return object_list;
2937 }
2938 return NULL((void*)0);
2939}
2940
2941
2942/**
2943 * sharkd_session_process_tap_rtp_cb()
2944 *
2945 * Output RTP streams tap:
2946 * (m) tap - tap name
2947 * (m) type - tap output type
2948 * (m) streams - array of object with attributes:
2949 * (m) ssrc - RTP synchronization source identifier
2950 * (m) payload - stream payload
2951 * (m) saddr - source address
2952 * (m) sport - source port
2953 * (m) daddr - destination address
2954 * (m) dport - destination port
2955 * (m) pkts - packets count
2956 * (m) max_delta - max delta (ms)
2957 * (m) max_jitter - max jitter (ms)
2958 * (m) mean_jitter - mean jitter (ms)
2959 * (m) expectednr -
2960 * (m) totalnr -
2961 * (m) problem - if analyser found the problem
2962 * (m) ipver - address IP version (4 or 6)
2963 */
2964static void
2965sharkd_session_process_tap_rtp_cb(void *arg)
2966{
2967 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
2968
2969 GList *listx;
2970
2971 json_dumper_begin_object(&dumper);
2972 sharkd_json_value_string("tap", "rtp-streams");
2973 sharkd_json_value_string("type", "rtp-streams");
2974
2975 sharkd_json_array_open("streams");
2976 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
2977 {
2978 rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data;
2979 rtpstream_info_calc_t calc;
2980
2981 rtpstream_info_calculate(streaminfo, &calc);
2982
2983 json_dumper_begin_object(&dumper);
2984
2985 sharkd_json_value_stringf("ssrc", "0x%x", calc.ssrc);
2986 sharkd_json_value_string("payload", calc.all_payload_type_names);
2987
2988 sharkd_json_value_string("saddr", calc.src_addr_str);
2989 sharkd_json_value_anyf("sport", "%u", calc.src_port);
2990 sharkd_json_value_string("daddr", calc.dst_addr_str);
2991 sharkd_json_value_anyf("dport", "%u", calc.dst_port);
2992
2993 sharkd_json_value_anyf("start_time", "%f", calc.start_time_ms);
2994 sharkd_json_value_anyf("duration", "%f", calc.duration_ms);
2995
2996 sharkd_json_value_anyf("pkts", "%u", calc.packet_count);
2997 sharkd_json_value_anyf("lost", "%u", calc.lost_num);
2998 sharkd_json_value_anyf("lost_percent", "%f", calc.lost_perc);
2999
3000 sharkd_json_value_anyf("max_delta", "%f",calc.max_delta);
3001 sharkd_json_value_anyf("min_delta", "%f",calc.min_delta);
3002 sharkd_json_value_anyf("mean_delta", "%f",calc.mean_delta);
3003 sharkd_json_value_anyf("min_jitter", "%f", calc.min_jitter);
3004 sharkd_json_value_anyf("max_jitter", "%f", calc.max_jitter);
3005 sharkd_json_value_anyf("mean_jitter", "%f", calc.mean_jitter);
3006
3007 sharkd_json_value_anyf("expectednr", "%u", calc.packet_expected);
3008 sharkd_json_value_anyf("totalnr", "%u", calc.total_nr);
3009
3010 sharkd_json_value_anyf("problem", calc.problem ? "true" : "false");
3011
3012 /* for filter */
3013 sharkd_json_value_anyf("ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
3014
3015 rtpstream_info_calc_free(&calc);
3016
3017 json_dumper_end_object(&dumper);
3018 }
3019 sharkd_json_array_close();
3020
3021 json_dumper_end_object(&dumper);
3022}
3023
3024/**
3025* sharkd_session_process_tap_multicast_cb()
3026*
3027* Output UDP Multicast streams tap:
3028* (m) tap - tap name
3029* (m) type - tap output type
3030* (m) bufferThresholdBytes - byte count for a stream where a buffer alarm should be reported
3031* (m) burstIntervalMs - analysis interval in milliseconds
3032* (m) burstThresholdPackets - count of packets in an interval that should trigger an alarm
3033* (m) streams - array of streams with metrics:
3034* (m) saddr - source address
3035* (m) sport - source port
3036* (m) daddr - destination address
3037* (m) dport - destination port
3038* (m) packets - object group for packet metrics with attributes:
3039* (m) number - count of packets in the stream
3040* (m) perSecond - average number of packets per seconds in the stream
3041* (m) bandwidth - object group for bandwidth metrics with attributes:
3042* (m) average - average measured bitrate in the stream
3043* (m) max - max measured bitrate in the stream
3044* (m) buffer - object group for buffer metrics with attributes:
3045* (m) alarms - number of times the stream exceeded the buffer threshold
3046* (m) max - highest stream buffer utilization
3047* (m) burst - object group for burst metrics with attributes:
3048* (m) alarms - number of times the stream exceeded the burst threshold
3049* (m) max - most stream packets measured in a burst interval
3050*/
3051static void
3052sharkd_session_process_tap_multicast_cb(void *arg)
3053{
3054 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)arg;
3055 GList *list_item;
3056 char *addr_str;
3057
3058 json_dumper_begin_object(&dumper);
3059
3060 sharkd_json_value_string("tap", "multicast");
3061 sharkd_json_value_string("type", "multicast");
3062
3063 sharkd_json_value_anyf("bufferThresholdBytes", "%u", mcast_stream_bufferalarm);
3064 sharkd_json_value_anyf("burstIntervalMs", "%u", mcast_stream_burstint);
3065 sharkd_json_value_anyf("burstThresholdPackets", "%u", mcast_stream_trigger);
3066
3067 sharkd_json_array_open("streams");
3068 for (list_item = g_list_first(tapinfo->strinfo_list); list_item; list_item = list_item->next) {
3069 mcast_stream_info_t *stream_info = (mcast_stream_info_t *) list_item->data;
3070 sharkd_json_object_open(NULL((void*)0));
3071 {
3072 addr_str = address_to_display(NULL((void*)0), &stream_info->src_addr);
3073 sharkd_json_value_string("saddr", addr_str);
3074 wmem_free(NULL((void*)0), addr_str);
3075 sharkd_json_value_anyf("sport", "%u", stream_info->src_port);
3076 addr_str = address_to_display(NULL((void*)0), &stream_info->dest_addr);
3077 sharkd_json_value_string("daddr", addr_str);
3078 wmem_free(NULL((void*)0), addr_str);
3079 sharkd_json_value_anyf("dport", "%u", stream_info->dest_port);
3080 sharkd_json_object_open("packets");
3081 {
3082 sharkd_json_value_anyf("number", "%u", stream_info->npackets);
3083 sharkd_json_value_anyf("perSecond", "%f", stream_info->apackets);
3084 }
3085 sharkd_json_object_close();
3086 sharkd_json_object_open("bandwidth");
3087 {
3088 sharkd_json_value_anyf("average", "%f", stream_info->average_bw);
3089 sharkd_json_value_anyf("max", "%f", stream_info->element.maxbw);
3090 }
3091 sharkd_json_object_close();
3092 sharkd_json_object_open("buffer");
3093 {
3094 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbuffalarms);
3095 sharkd_json_value_anyf("max", "%u", stream_info->element.topbuffusage);
3096 }
3097 sharkd_json_object_close();
3098 sharkd_json_object_open("burst");
3099 {
3100 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbursts);
3101 sharkd_json_value_anyf("max", "%u", stream_info->element.topburstsize);
3102 }
3103 sharkd_json_object_close();
3104 }
3105 sharkd_json_object_close();
3106 }
3107 sharkd_json_array_close();
3108
3109 json_dumper_end_object(&dumper);
3110}
3111
3112static void
3113sharkd_session_process_free_tap_multicast_cb(void *tapdata)
3114{
3115 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)tapdata;
3116
3117 mcaststream_reset(tapinfo);
3118
3119 g_free(tapinfo);
3120}
3121
3122/**
3123 * sharkd_session_process_tap_voip_calls_cb()
3124 *
3125 * Output VoIP Calls tap:
3126 * (m) tap - tap name
3127 * (m) type - tap output type
3128 * (m) calls - array of objects with attributes:
3129 * (m) call - call number
3130 * (m) start_time - start timestamp
3131 * (m) stop_time - stop timestamp
3132 * (m) initial_speaker - address of initial speaker
3133 * (m) from - from address
3134 * (m) to - to address
3135 * (m) protocol - protocol name
3136 * (m) packets - packet count
3137 * (m) state - state string
3138 * (m) comment - comment string
3139 */
3140static void
3141sharkd_session_process_tap_voip_calls_cb(void *arg)
3142{
3143 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)arg;
3144 char *addr_str;
3145 GList *cur_call = g_queue_peek_nth_link(tapinfo->callsinfos, 0);
3146 sharkd_json_object_open(NULL((void*)0));
3147 sharkd_json_value_string("tap", "voip-calls");
3148 sharkd_json_value_string("type", "voip-calls");
3149 sharkd_json_array_open("calls");
3150 while (cur_call && cur_call->data) {
3151 voip_calls_info_t *call_info_ = (voip_calls_info_t*) cur_call->data;
3152 sharkd_json_object_open(NULL((void*)0));
3153 sharkd_json_value_anyf("call", "%hu", call_info_->call_num);
3154 sharkd_json_value_anyf("start_time", "%.6f", nstime_to_sec(&(call_info_->start_rel_ts)));
3155 sharkd_json_value_anyf("stop_time", "%.6f", nstime_to_sec(&(call_info_->stop_rel_ts)));
3156 addr_str = address_to_display(NULL((void*)0), &(call_info_->initial_speaker));
3157 sharkd_json_value_string("initial_speaker", addr_str);
3158 wmem_free(NULL((void*)0), addr_str);
3159 sharkd_json_value_string("from", call_info_->from_identity);
3160 sharkd_json_value_string("to", call_info_->to_identity);
3161 sharkd_json_value_string("protocol", ((call_info_->protocol == VOIP_COMMON) && call_info_->protocol_name) ?
3162 call_info_->protocol_name : voip_protocol_name[call_info_->protocol]);
3163 sharkd_json_value_anyf("packets", "%u", call_info_->npackets);
3164 sharkd_json_value_string("state", voip_call_state_name[call_info_->call_state]);
3165 sharkd_json_value_string("comment", call_info_->call_comment);
3166 sharkd_json_object_close();
3167 cur_call = g_list_next(cur_call)((cur_call) ? (((GList *)(cur_call))->next) : ((void*)0));
3168 }
3169 sharkd_json_array_close();
3170 sharkd_json_object_close();
3171}
3172
3173static void
3174sharkd_session_free_tap_voip_calls_cb(void *tapdata)
3175{
3176 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)tapdata;
3177 voip_calls_remove_all_tap_listeners(tapinfo);
3178 if (tapinfo->callsinfos != NULL((void*)0)) {
3179 g_queue_free(tapinfo->callsinfos);
3180 }
3181 if (tapinfo->graph_analysis != NULL((void*)0)) {
3182 sequence_analysis_info_free(tapinfo->graph_analysis);
3183 }
3184 memset(tapinfo, 0, sizeof(*tapinfo));
3185}
3186
3187
3188struct sharkd_voip_convs_req {
3189 voip_calls_tapinfo_t *tapinfo;
3190 const char *tap_name;
3191};
3192
3193/**
3194 * sharkd_session_process_tap_voip_convs_cb()
3195 *
3196 * Output VoIP Conversations tap:
3197 * (m) tap - tap name
3198 * (m) type - tap output type
3199 * (m) convs - array of objects with attributes:
3200 * (m) frame - frame number
3201 * (m) call - call number
3202 * (m) time - timestamp
3203 * (m) dst_addr - destination address
3204 * (m) dst_port - destination port
3205 * (m) src_addr - source address
3206 * (m) src_port - source port
3207 * (m) label - label string
3208 * (m) comment - comment string
3209 */
3210static void
3211sharkd_session_process_tap_voip_convs_cb(void *arg)
3212{
3213 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)arg;
3214 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3215 seq_analysis_info_t *sainfo = tapinfo->graph_analysis;
3216 char *addr_str;
3217 sequence_analysis_list_sort(sainfo);
3218 sharkd_json_object_open(NULL((void*)0));
3219 sharkd_json_value_string("tap", voip_convs_req->tap_name);
3220 sharkd_json_value_string("type", "voip-convs");
3221 sharkd_json_array_open("convs");
3222 for (GList *cur = g_queue_peek_nth_link(sainfo->items, 0); cur; cur = g_list_next(cur)((cur) ? (((GList *)(cur))->next) : ((void*)0))) {
3223 seq_analysis_item_t *sai = (seq_analysis_item_t *) cur->data;
3224 if ((voip_conv_sel[sai->conv_num / VOIP_CONV_BITS(sizeof(int) * 8)] & (1 << (sai->conv_num % VOIP_CONV_BITS(sizeof(int) * 8)))) == 0)
3225 continue;
3226 sharkd_json_object_open(NULL((void*)0));
3227 sharkd_json_value_anyf("frame", "%d", sai->frame_number);
3228 sharkd_json_value_anyf("call", "%d", sai->conv_num);
3229 sharkd_json_value_string("time", sai->time_str);
3230 addr_str = address_to_display(NULL((void*)0), &(sai->dst_addr));
3231 sharkd_json_value_string("dst_addr", addr_str);
3232 wmem_free(NULL((void*)0), addr_str);
3233 sharkd_json_value_anyf("dst_port", "%d", sai->port_dst);
3234 addr_str = address_to_display(NULL((void*)0), &(sai->src_addr));
3235 sharkd_json_value_string("src_addr", addr_str);
3236 wmem_free(NULL((void*)0), addr_str);
3237 sharkd_json_value_anyf("src_port", "%d", sai->port_src);
3238 sharkd_json_value_string("label", sai->frame_label);
3239 sharkd_json_value_string("comment", sai->comment);
3240 sharkd_json_object_close();
3241 }
3242 sharkd_json_array_close();
3243 sharkd_json_object_close();
3244}
3245
3246static void
3247sharkd_session_free_tap_voip_convs_cb(void *tapdata)
3248{
3249 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)tapdata;
3250 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3251 voip_calls_remove_all_tap_listeners(tapinfo);
3252 if (tapinfo->callsinfos != NULL((void*)0)) {
3253 g_queue_free(tapinfo->callsinfos);
3254 }
3255 if (tapinfo->graph_analysis != NULL((void*)0)) {
3256 sequence_analysis_info_free(tapinfo->graph_analysis);
3257 }
3258 memset(tapinfo, 0, sizeof(*tapinfo));
3259 g_free(voip_convs_req);
3260}
3261
3262struct sharkd_hosts_req {
3263 const char *tap_name;
3264 bool_Bool dump_v4;
3265 bool_Bool dump_v6;
3266};
3267
3268static int
3269sharkd_session_tap_ipv4_host_compare(const void *a, const void *b)
3270{
3271 return ws_ascii_strnatcmp(((const hashipv4_t *)a)->name,
3272 ((const hashipv4_t *)b)->name);
3273}
3274
3275static int
3276sharkd_session_tap_ipv6_host_compare(const void *a, const void *b)
3277{
3278 return ws_ascii_strnatcmp(((const hashipv6_t *)a)->name,
3279 ((const hashipv6_t *)b)->name);
3280}
3281
3282static void
3283sharkd_session_tap_ipv4_host_print(void *data, void *user_data _U___attribute__((unused)))
3284{
3285 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)data;
3286 sharkd_json_object_open(NULL((void*)0));
3287 sharkd_json_value_string("name", ipv4_hash_table_entry->name);
3288 sharkd_json_value_string("addr", ipv4_hash_table_entry->ip);
3289 sharkd_json_object_close();
3290}
3291
3292static void
3293sharkd_session_tap_ipv6_host_print(void *data, void *user_data _U___attribute__((unused)))
3294{
3295 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)data;
3296 sharkd_json_object_open(NULL((void*)0));
3297 sharkd_json_value_string("name", ipv6_hash_table_entry->name);
3298 sharkd_json_value_string("addr", ipv6_hash_table_entry->ip6);
3299 sharkd_json_object_close();
3300}
3301
3302static void
3303sharkd_session_tap_ipv4_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3304{
3305 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
3306 GSList **list = (GSList **)user_data;
3307 if ((ipv4_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3308 *list = g_slist_insert_sorted(*list, ipv4_hash_table_entry, sharkd_session_tap_ipv4_host_compare);
3309 }
3310}
3311
3312static void
3313sharkd_session_tap_ipv6_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3314{
3315 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
3316 GSList **list = (GSList **) user_data;
3317 if ((ipv6_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3318 *list = g_slist_insert_sorted(*list, ipv6_hash_table_entry, sharkd_session_tap_ipv6_host_compare);
3319 }
3320}
3321
3322static void
3323sharkd_session_tap_ipv4_hosts_print(void)
3324{
3325 wmem_map_t *ipv4_hash_table = get_ipv4_hash_table();
3326 if (!ipv4_hash_table)
3327 return;
3328 GSList *list = NULL((void*)0);
3329 wmem_map_foreach(ipv4_hash_table, sharkd_session_tap_ipv4_host_insert_sorted, &list);
3330 g_slist_foreach(list, sharkd_session_tap_ipv4_host_print, NULL((void*)0));
3331 g_slist_free(list);
3332}
3333
3334static void
3335sharkd_session_tap_ipv6_hosts_print(void)
3336{
3337 wmem_map_t *ipv6_hash_table = get_ipv6_hash_table();
3338 if (!ipv6_hash_table)
3339 return;
3340 GSList *list = NULL((void*)0);
3341 wmem_map_foreach(ipv6_hash_table, sharkd_session_tap_ipv6_host_insert_sorted, &list);
3342 g_slist_foreach(list, sharkd_session_tap_ipv6_host_print, NULL((void*)0));
3343 g_slist_free(list);
3344}
3345
3346/**
3347 * sharkd_session_process_tap_hosts_cb()
3348 *
3349 * Output Hosts tap:
3350 * (m) tap - tap name
3351 * (m) type - tap output type
3352 * (o) ipv4_hosts - array of objects with attributes:
3353 * (m) addr - ipv4 address
3354 * (m) name - resolved name of address
3355 * (o) ipv6_hosts - array of objects with attributes:
3356 * (m) addr - ipv6 address
3357 * (m) name - resolved name of address
3358 */
3359static void
3360sharkd_session_process_tap_hosts_cb(void *arg)
3361{
3362 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)arg;
3363 sharkd_json_object_open(NULL((void*)0));
3364 sharkd_json_value_string("tap", hosts_req->tap_name);
3365 sharkd_json_value_string("type", "hosts");
3366 if (hosts_req->dump_v4) {
3367 sharkd_json_array_open("ipv4_hosts");
3368 sharkd_session_tap_ipv4_hosts_print();
3369 sharkd_json_array_close();
3370 }
3371 if (hosts_req->dump_v6) {
3372 sharkd_json_array_open("ipv6_hosts");
3373 sharkd_session_tap_ipv6_hosts_print();
3374 sharkd_json_array_close();
3375 }
3376 sharkd_json_object_close();
3377}
3378
3379static void
3380sharkd_session_free_tap_hosts_cb(void *tapdata)
3381{
3382 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)tapdata;
3383 g_free(hosts_req);
3384}
3385
3386static GString*
3387sharkd_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)
3388{
3389 export_object_list_t *eo_object;
3390 struct sharkd_export_object_list *object_list;
3391
3392 object_list = sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type);
3393 if (object_list)
3394 {
3395 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
3396 object_list->entries = NULL((void*)0);
3397 }
3398 else
3399 {
3400 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)))
;
3401 object_list->type = g_strdup(tap_type)g_strdup_inline (tap_type);
3402 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
3403 object_list->entries = NULL((void*)0);
3404 object_list->next = sharkd_eo_list;
3405 sharkd_eo_list = object_list;
3406 }
3407
3408 eo_object = g_new0(export_object_list_t, 1)((export_object_list_t *) g_malloc0_n ((1), sizeof (export_object_list_t
)))
;
3409 eo_object->add_entry = sharkd_eo_object_list_add_entry;
3410 eo_object->get_entry = sharkd_eo_object_list_get_entry;
3411 eo_object->gui_data = (void *) object_list;
3412
3413 *ptap_data = eo_object;
3414 *ptap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
3415
3416 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));
3417}
3418
3419/**
3420 * sharkd_session_process_tap()
3421 *
3422 * Process tap request
3423 *
3424 * Input:
3425 * (m) tap0 - First tap request
3426 * (o) tap1...tap15 - Other tap requests
3427 *
3428 * Output object with attributes:
3429 * (m) taps - array of object with attributes:
3430 * (m) tap - tap name
3431 * (m) type - tap output type
3432 * ...
3433 * for type:stats see sharkd_session_process_tap_stats_cb()
3434 * for type:nstat see sharkd_session_process_tap_nstat_cb()
3435 * for type:conv see sharkd_session_process_tap_conv_cb()
3436 * for type:host see sharkd_session_process_tap_conv_cb()
3437 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
3438 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
3439 * for type:eo see sharkd_session_process_tap_eo_cb()
3440 * for type:expert see sharkd_session_process_tap_expert_cb()
3441 * for type:rtd see sharkd_session_process_tap_rtd_cb()
3442 * for type:srt see sharkd_session_process_tap_srt_cb()
3443 * for type:flow see sharkd_session_process_tap_flow_cb()
3444 *
3445 * (m) err - error code
3446 */
3447static void
3448sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
3449{
3450 void *taps_data[16];
3451 GFreeFunc taps_free[16];
3452 int taps_count = 0;
3453 int i;
3454 const char *tap_filter = json_find_attr(buf, tokens, count, "filter");
3455
3456 rtpstream_tapinfo_t rtp_tapinfo =
3457 { 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};
3458
3459 for (i = 0; i < 16; i++)
3460 {
3461 char tapbuf[32];
3462 const char *tok_tap;
3463
3464 void *tap_data = NULL((void*)0);
3465 GFreeFunc tap_free = NULL((void*)0);
3466 GString *tap_error = NULL((void*)0);
3467
3468 snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
3469 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
3470 if (!tok_tap)
3471 break;
3472
3473 if (!strncmp(tok_tap, "stat:", 5))
3474 {
3475 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
3476 stats_tree *st;
3477
3478 if (!cfg)
3479 {
3480 sharkd_json_error(
3481 rpcid, -11001, NULL((void*)0),
3482 "sharkd_session_process_tap() stat %s not found", tok_tap + 5
3483 );
3484 return;
3485 }
3486
3487 st = stats_tree_new(cfg, NULL((void*)0), tap_filter);
3488
3489 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));
3490
3491 if (!tap_error && cfg->init)
3492 cfg->init(st);
3493
3494 tap_data = st;
3495 tap_free = sharkd_session_free_tap_stats_cb;
3496 }
3497 else if (!strcmp(tok_tap, "expert"))
3498 {
3499 struct sharkd_expert_tap *expert_tap;
3500
3501 expert_tap = g_new0(struct sharkd_expert_tap, 1)((struct sharkd_expert_tap *) g_malloc0_n ((1), sizeof (struct
sharkd_expert_tap)))
;
3502 expert_tap->text = g_string_chunk_new(100);
3503
3504 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));
3505
3506 tap_data = expert_tap;
3507 tap_free = sharkd_session_free_tap_expert_cb;
3508 }
3509 else if (!strncmp(tok_tap, "seqa:", 5))
3510 {
3511 seq_analysis_info_t *graph_analysis;
3512 register_analysis_t *analysis;
3513 const char *tap_name;
3514 tap_packet_cb tap_func;
3515 unsigned tap_flags;
3516
3517 analysis = sequence_analysis_find_by_name(tok_tap + 5);
3518 if (!analysis)
3519 {
3520 sharkd_json_error(
3521 rpcid, -11002, NULL((void*)0),
3522 "sharkd_session_process_tap() seq analysis %s not found", tok_tap + 5
3523 );
3524 return;
3525 }
3526
3527 graph_analysis = sequence_analysis_info_new();
3528 graph_analysis->name = tok_tap + 5;
3529 /* TODO, make configurable */
3530 graph_analysis->any_addr = false0;
3531
3532 tap_name = sequence_analysis_get_tap_listener_name(analysis);
3533 tap_flags = sequence_analysis_get_tap_flags(analysis);
3534 tap_func = sequence_analysis_get_packet_func(analysis);
3535
3536 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));
3537
3538 tap_data = graph_analysis;
3539 tap_free = sharkd_session_free_tap_flow_cb;
3540 }
3541 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
3542 {
3543 struct register_ct *ct = NULL((void*)0);
3544 const char *ct_tapname;
3545 struct sharkd_conv_tap_data *ct_data;
3546 tap_packet_cb tap_func = NULL((void*)0);
3547
3548 if (!strncmp(tok_tap, "conv:", 5))
3549 {
3550 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
3551
3552 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
3553 {
3554 sharkd_json_error(
3555 rpcid, -11003, NULL((void*)0),
3556 "sharkd_session_process_tap() conv %s not found", tok_tap + 5
3557 );
3558 return;
3559 }
3560 }
3561 else if (!strncmp(tok_tap, "endpt:", 6))
3562 {
3563 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
3564
3565 if (!ct || !(tap_func = get_endpoint_packet_func(ct)))
3566 {
3567 sharkd_json_error(
3568 rpcid, -11004, NULL((void*)0),
3569 "sharkd_session_process_tap() endpt %s not found", tok_tap + 6
3570 );
3571 return;
3572 }
3573 }
3574 else
3575 {
3576 sharkd_json_error(
3577 rpcid, -11005, NULL((void*)0),
3578 "sharkd_session_process_tap() conv/endpt(?): %s not found", tok_tap
3579 );
3580 return;
3581 }
3582
3583 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
3584
3585 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)))
;
3586 ct_data->type = tok_tap;
3587 ct_data->hash.user_data = ct_data;
3588
3589 /* XXX: make configurable */
3590 ct_data->resolve_name = true1;
3591 ct_data->resolve_port = true1;
3592
3593 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));
3594
3595 tap_data = &ct_data->hash;
3596 tap_free = sharkd_session_free_tap_conv_cb;
3597 }
3598 else if (!strncmp(tok_tap, "nstat:", 6))
3599 {
3600 stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6);
3601 stat_data_t *stat_data;
3602
3603 if (!stat_tap)
3604 {
3605 sharkd_json_error(
3606 rpcid, -11006, NULL((void*)0),
3607 "sharkd_session_process_tap() nstat=%s not found", tok_tap + 6
3608 );
3609 return;
3610 }
3611
3612 stat_tap->stat_tap_init_cb(stat_tap);
3613
3614 stat_data = g_new0(stat_data_t, 1)((stat_data_t *) g_malloc0_n ((1), sizeof (stat_data_t)));
3615 stat_data->stat_tap_data = stat_tap;
3616 stat_data->user_data = NULL((void*)0);
3617
3618 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));
3619
3620 tap_data = stat_data;
3621 tap_free = sharkd_session_free_tap_nstat_cb;
3622 }
3623 else if (!strncmp(tok_tap, "rtd:", 4))
3624 {
3625 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
3626 rtd_data_t *rtd_data;
3627 char *err;
3628
3629 if (!rtd)
3630 {
3631 sharkd_json_error(
3632 rpcid, -11007, NULL((void*)0),
3633 "sharkd_session_process_tap() rtd=%s not found", tok_tap + 4
3634 );
3635 return;
3636 }
3637
3638 rtd_table_get_filter(rtd, "", &tap_filter, &err);
3639 if (err != NULL((void*)0))
3640 {
3641 sharkd_json_error(
3642 rpcid, -11008, NULL((void*)0),
3643 "sharkd_session_process_tap() rtd=%s err=%s", tok_tap + 4, err
3644 );
3645 g_free(err);
3646 return;
3647 }
3648
3649 rtd_data = g_new0(rtd_data_t, 1)((rtd_data_t *) g_malloc0_n ((1), sizeof (rtd_data_t)));
3650 rtd_data->user_data = rtd;
3651 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL((void*)0), NULL((void*)0));
3652
3653 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));
3654
3655 tap_data = rtd_data;
3656 tap_free = sharkd_session_free_tap_rtd_cb;
3657 }
3658 else if (!strncmp(tok_tap, "srt:", 4))
3659 {
3660 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
3661 srt_data_t *srt_data;
3662 char *err;
3663
3664 if (!srt)
3665 {
3666 sharkd_json_error(
3667 rpcid, -11009, NULL((void*)0),
3668 "sharkd_session_process_tap() srt=%s not found", tok_tap + 4
3669 );
3670 return;
3671 }
3672
3673 srt_table_get_filter(srt, "", &tap_filter, &err);
3674 if (err != NULL((void*)0))
3675 {
3676 sharkd_json_error(
3677 rpcid, -11010, NULL((void*)0),
3678 "sharkd_session_process_tap() srt=%s err=%s", tok_tap + 4, err
3679 );
3680 g_free(err);
3681 return;
3682 }
3683
3684 srt_data = g_new0(srt_data_t, 1)((srt_data_t *) g_malloc0_n ((1), sizeof (srt_data_t)));
3685 srt_data->srt_array = g_array_new(FALSE(0), TRUE(!(0)), sizeof(srt_stat_table *));
3686 srt_data->user_data = srt;
3687 srt_table_dissector_init(srt, srt_data->srt_array);
3688
3689 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));
3690
3691 tap_data = srt_data;
3692 tap_free = sharkd_session_free_tap_srt_cb;
3693 }
3694 else if (!strncmp(tok_tap, "eo:", 3))
3695 {
3696 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
3697
3698 if (!eo)
3699 {
3700 sharkd_json_error(
3701 rpcid, -11011, NULL((void*)0),
3702 "sharkd_session_process_tap() eo=%s not found", tok_tap + 3
3703 );
3704 return;
3705 }
3706
3707 tap_error = sharkd_session_eo_register_tap_listener(eo, tok_tap, tap_filter, sharkd_session_process_tap_eo_cb, &tap_data, &tap_free);
3708
3709 /* tap_data & tap_free assigned by sharkd_session_eo_register_tap_listener */
3710 }
3711 else if (!strcmp(tok_tap, "rtp-streams"))
3712 {
3713 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));
3714
3715 tap_data = &rtp_tapinfo;
3716 tap_free = rtpstream_reset_cb;
3717 }
3718 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
3719 {
3720 struct sharkd_analyse_rtp *rtp_req;
3721
3722 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
3723 if (!sharkd_rtp_match_init(&rtp_req->id, tok_tap + 12))
3724 {
3725 rtpstream_id_free(&rtp_req->id);
3726 g_free(rtp_req);
3727 continue;
3728 }
3729
3730 rtp_req->tap_name = tok_tap;
3731 rtp_req->statinfo.first_packet = true1;
3732 rtp_req->statinfo.reg_pt = PT_UNDEFINED-1;
3733
3734 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));
3735
3736 tap_data = rtp_req;
3737 tap_free = sharkd_session_process_tap_rtp_free_cb;
3738 }
3739 else if (!strcmp(tok_tap, "multicast"))
3740 {
3741 mcaststream_tapinfo_t *mcaststream_tapinfo;
3742 mcaststream_tapinfo = (mcaststream_tapinfo_t *) g_malloc0(sizeof(*mcaststream_tapinfo));
3743
3744 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));
3745 tap_data = mcaststream_tapinfo;
3746 tap_free = sharkd_session_process_free_tap_multicast_cb;
3747 }
3748 else if (!strcmp(tok_tap, "phs"))
3749 {
3750 phs_t *rs;
3751
3752 pc_proto_id = proto_registrar_get_id_byname("pkt_comment");
3753
3754 rs = new_phs_t(NULL((void*)0), tap_filter);
3755
3756 tap_error = register_tap_listener("frame", rs, tap_filter,
3757 TL_REQUIRES_PROTO_TREE0x00000001|TL_REQUIRES_PROTOCOLS0x00000020,
3758 NULL((void*)0), protohierstat_packet,
3759 sharkd_session_process_tap_phs_cb, NULL((void*)0));
3760
3761 tap_data = rs;
3762 tap_free = sharkd_session_free_tap_phs_cb;
3763 }
3764 else if (!strcmp(tok_tap, "voip-calls"))
3765 {
3766 voip_stat_init_tapinfo();
3767
3768 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));
3769
3770 tapinfo_.session = cfile.epan;
3771 voip_calls_init_all_taps(&tapinfo_);
3772
3773 tap_data = &tapinfo_;
3774 tap_free = sharkd_session_free_tap_voip_calls_cb;
3775 }
3776 else if (!strncmp(tok_tap, "voip-convs:", 11))
3777 {
3778 int len;
3779 unsigned int min, max;
3780 struct sharkd_voip_convs_req *voip_convs_req;
3781 const char *conv_arg = tok_tap + 11;
3782
3783 // parse tok_tap to get which call we are asking for
3784 if (*conv_arg == 0) {
3785 // set all bits of voip_conv_sel (-1 in binary is all 1's)
3786 memset(voip_conv_sel, -1, sizeof(voip_conv_sel));
3787 } else {
3788 memset(voip_conv_sel, 0, sizeof(voip_conv_sel));
3789
3790 while (*conv_arg != 0) {
3791 if (*conv_arg == ',') {
3792 conv_arg++;
3793 }
3794 if (sscanf(conv_arg, "%u-%u%n", &min, &max, &len) == 2) {
3795 conv_arg += len;
3796 } else if (sscanf(conv_arg, "%u%n", &min, &len) == 1) {
3797 max = min;
3798 conv_arg += len;
3799 } else {
3800 sharkd_json_error(
3801 rpcid, -11014, NULL((void*)0),
3802 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' parameter", tok_tap
3803 );
3804 return;
3805 }
3806 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)))
) {
3807 sharkd_json_error(
3808 rpcid, -11012, NULL((void*)0),
3809 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' number range", tok_tap
3810 );
3811 return;
3812 }
3813 for(; min <= max; min++) {
3814 voip_conv_sel[min / VOIP_CONV_BITS(sizeof(int) * 8)] |= 1 << (min % VOIP_CONV_BITS(sizeof(int) * 8));
3815 }
3816 }
3817 }
3818
3819 voip_stat_init_tapinfo();
3820
3821 voip_convs_req = (struct sharkd_voip_convs_req *) g_malloc0(sizeof(*voip_convs_req));
3822 voip_convs_req->tapinfo = &tapinfo_;
3823 voip_convs_req->tap_name = tok_tap;
3824
3825 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));
3826
3827 tapinfo_.session = cfile.epan;
3828 voip_calls_init_all_taps(&tapinfo_);
3829
3830 tap_data = voip_convs_req;
3831 tap_free = sharkd_session_free_tap_voip_convs_cb;
3832 }
3833 else if (!strncmp(tok_tap, "hosts:", 6))
3834 {
3835 bool_Bool dump_v4;
3836 bool_Bool dump_v6;
3837 struct sharkd_hosts_req *hosts_req;
3838 const char *proto_arg;
3839 char **proto_tokens;
3840 int proto_count;
3841
3842 proto_arg = tok_tap + 6;
3843
3844 if (strlen(proto_arg) == 0) {
3845 dump_v4 = true1;
3846 dump_v6 = true1;
3847 } else {
3848 dump_v4 = false0;
3849 dump_v6 = false0;
3850
3851 proto_tokens = g_strsplit(proto_arg, ",", 0);
3852 proto_count = 0;
3853 while (proto_tokens[proto_count]) {
3854 if (!strcmp("ip", proto_tokens[proto_count]) ||
3855 !strcmp("ipv4", proto_tokens[proto_count])) {
3856 dump_v4 = true1;
3857 } else if (!strcmp("ipv6", proto_tokens[proto_count])) {
3858 dump_v6 = true1;
3859 } else {
3860 g_strfreev(proto_tokens);
3861 sharkd_json_error(
3862 rpcid, -11015, NULL((void*)0),
3863 "sharkd_session_process_tap() hosts=%s invalid 'protos' parameter", tok_tap
3864 );
3865 return;
3866 }
3867 proto_count++;
3868 }
3869 g_strfreev(proto_tokens);
3870 }
3871
3872 hosts_req = (struct sharkd_hosts_req *)g_malloc0(sizeof(*hosts_req));
3873 hosts_req->dump_v4 = dump_v4;
3874 hosts_req->dump_v6 = dump_v6;
3875 hosts_req->tap_name = tok_tap;
3876
3877 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));
3878
3879 tap_data = hosts_req;
3880 tap_free = sharkd_session_free_tap_hosts_cb;
3881 }
3882 else
3883 {
3884 sharkd_json_error(
3885 rpcid, -11012, NULL((void*)0),
3886 "sharkd_session_process_tap() %s not recognized", tok_tap
3887 );
3888 return;
3889 }
3890
3891 if (tap_error)
3892 {
3893 sharkd_json_error(
3894 rpcid, -11013, NULL((void*)0),
3895 "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str
3896 );
3897 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)))))
;
3898 if (tap_free)
3899 tap_free(tap_data);
3900 return;
3901 }
3902
3903 taps_data[taps_count] = tap_data;
3904 taps_free[taps_count] = tap_free;
3905 taps_count++;
3906 }
3907
3908 fprintf(stderrstderr, "sharkd_session_process_tap() count=%d\n", taps_count);
3909 if (taps_count == 0)
3910 {
3911 sharkd_json_result_prologue(rpcid);
3912 sharkd_json_array_open("taps");
3913 sharkd_json_array_close();
3914 sharkd_json_result_epilogue();
3915 return;
3916 }
3917
3918 sharkd_json_result_prologue(rpcid);
3919 sharkd_json_array_open("taps");
3920 sharkd_retap();
3921 sharkd_json_array_close();
3922 sharkd_json_result_epilogue();
3923
3924 for (i = 0; i < taps_count; i++)
3925 {
3926 if (taps_data[i])
3927 remove_tap_listener(taps_data[i]);
3928
3929 if (taps_free[i])
3930 taps_free[i](taps_data[i]);
3931 }
3932}
3933
3934/**
3935 * sharkd_session_process_follow()
3936 *
3937 * Process follow request
3938 *
3939 * Input:
3940 * (m) follow - follow protocol request (e.g. HTTP)
3941 * (m) filter - filter request (e.g. tcp.stream == 1)
3942 * (m) stream - stream index number
3943 * (o) sub_stream - follow sub-stream index number (e.g. for HTTP/2 and QUIC streams)
3944 *
3945 * Output object with attributes:
3946 *
3947 * (m) err - error code
3948 * (m) shost - server host
3949 * (m) sport - server port
3950 * (m) sbytes - server send bytes count
3951 * (m) chost - client host
3952 * (m) cport - client port
3953 * (m) cbytes - client send bytes count
3954 * (o) payloads - array of object with attributes:
3955 * (o) s - set if server sent, else client
3956 * (m) n - packet number
3957 * (m) d - data base64 encoded
3958 */
3959static void
3960sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
3961{
3962 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
3963 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
3964 const char *tok_sub_stream = json_find_attr(buf, tokens, count, "sub_stream");
3965
3966 register_follow_t *follower;
3967 GString *tap_error;
3968
3969 follow_info_t *follow_info;
3970 const char *host;
3971 char *port;
3972
3973 follower = get_follow_by_name(tok_follow);
3974 if (!follower)
3975 {
3976 sharkd_json_error(
3977 rpcid, -12001, NULL((void*)0),
3978 "sharkd_session_process_follow() follower=%s not found", tok_follow
3979 );
3980 return;
3981 }
3982
3983 uint64_t substream_id = SUBSTREAM_UNUSED0xFFFFFFFFFFFFFFFFUL;
3984 if (tok_sub_stream)
3985 {
3986 ws_strtou64(tok_sub_stream, NULL((void*)0), &substream_id);
3987 }
3988
3989 /* follow_reset_stream ? */
3990 follow_info = g_new0(follow_info_t, 1)((follow_info_t *) g_malloc0_n ((1), sizeof (follow_info_t)));
3991 follow_info->substream_id = substream_id;
3992 /* gui_data, filter_out_filter not set, but not used by dissector */
3993
3994 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));
3995 if (tap_error)
3996 {
3997 sharkd_json_error(
3998 rpcid, -12002, NULL((void*)0),
3999 "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str
4000 );
4001 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)))))
;
4002 g_free(follow_info);
4003 return;
4004 }
4005
4006 sharkd_retap();
4007
4008 sharkd_json_result_prologue(rpcid);
4009
4010 /* Server information: hostname, port, bytes sent */
4011 host = address_to_name(&follow_info->server_ip);
4012 sharkd_json_value_string("shost", host);
4013
4014 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->server_port);
4015 sharkd_json_value_string("sport", port);
4016 wmem_free(NULL((void*)0), port);
4017
4018 sharkd_json_value_anyf("sbytes", "%u", follow_info->bytes_written[0]);
4019
4020 /* Client information: hostname, port, bytes sent */
4021 host = address_to_name(&follow_info->client_ip);
4022 sharkd_json_value_string("chost", host);
4023
4024 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->client_port);
4025 sharkd_json_value_string("cport", port);
4026 wmem_free(NULL((void*)0), port);
4027
4028 sharkd_json_value_anyf("cbytes", "%u", follow_info->bytes_written[1]);
4029
4030 if (follow_info->payload)
4031 {
4032 follow_record_t *follow_record;
4033 GList *cur;
4034
4035 sharkd_json_array_open("payloads");
4036 for (cur = g_list_last(follow_info->payload); cur; cur = g_list_previous(cur)((cur) ? (((GList *)(cur))->prev) : ((void*)0)))
4037 {
4038 follow_record = (follow_record_t *) cur->data;
4039
4040 json_dumper_begin_object(&dumper);
4041
4042 sharkd_json_value_anyf("n", "%u", follow_record->packet_num);
4043 sharkd_json_value_base64("d", follow_record->data->data, follow_record->data->len);
4044
4045 if (follow_record->is_server)
4046 sharkd_json_value_anyf("s", "%d", 1);
4047
4048 json_dumper_end_object(&dumper);
4049 }
4050 sharkd_json_array_close();
4051 }
4052
4053 sharkd_json_result_epilogue();
4054
4055 remove_tap_listener(follow_info);
4056 follow_info_free(follow_info);
4057}
4058
4059static void
4060sharkd_session_process_frame_cb_tree(const char *key, epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs, bool_Bool display_hidden)
4061{
4062 proto_node *node;
4063
4064 sharkd_json_array_open(key);
4065 for (node = tree->first_child; node; node = node->next)
4066 {
4067 field_info *finfo = PNODE_FINFO(node)((node)->finfo);
4068
4069 if (!finfo)
4070 continue;
4071
4072 if (!display_hidden && FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4073 continue;
4074
4075 json_dumper_begin_object(&dumper);
4076
4077 if (!finfo->rep)
4078 {
4079 char label_str[ITEM_LABEL_LENGTH240];
4080
4081 label_str[0] = '\0';
4082 proto_item_fill_label(finfo, label_str, NULL((void*)0));
4083 sharkd_json_value_string("l", label_str);
4084 }
4085 else
4086 {
4087 sharkd_json_value_string("l", finfo->rep->representation);
4088 }
4089
4090 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
4091 {
4092 int idx;
4093
4094 for (idx = 1; tvbs[idx]; idx++)
4095 {
4096 if (tvbs[idx] == finfo->ds_tvb)
4097 {
4098 sharkd_json_value_anyf("ds", "%d", idx);
4099 break;
4100 }
4101 }
4102 }
4103
4104 if (finfo->start >= 0 && finfo->length > 0)
4105 sharkd_json_value_anyf("h", "[%d,%d]", finfo->start, finfo->length);
4106
4107 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
4108 sharkd_json_value_anyf("i", "[%d,%d]", finfo->appendix_start, finfo->appendix_length);
4109
4110
4111 if (finfo->hfinfo)
4112 {
4113 char *filter;
4114
4115 if (finfo->hfinfo->type == FT_PROTOCOL)
4116 {
4117 sharkd_json_value_string("t", "proto");
4118 }
4119 else if (finfo->hfinfo->type == FT_FRAMENUM)
4120 {
4121 sharkd_json_value_string("t", "framenum");
4122 sharkd_json_value_anyf("fnum", "%u", fvalue_get_uinteger(finfo->value));
4123 }
4124 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)
)
4125 {
4126 char *url = fvalue_to_string_repr(NULL((void*)0), finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
4127
4128 sharkd_json_value_string("t", "url");
4129 sharkd_json_value_string("url", url);
4130 wmem_free(NULL((void*)0), url);
4131 }
4132
4133 filter = proto_construct_match_selected_string(finfo, edt);
4134 if (filter)
4135 {
4136 sharkd_json_value_string("f", filter);
4137 wmem_free(NULL((void*)0), filter);
4138 }
4139
4140 if (finfo->hfinfo->abbrev)
4141 sharkd_json_value_string("fn", finfo->hfinfo->abbrev);
4142 }
4143
4144 if (FI_GET_FLAG(finfo, FI_GENERATED)((finfo) ? ((finfo)->flags & (0x00000002)) : 0))
4145 sharkd_json_value_anyf("g", "true");
4146
4147 if (FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4148 sharkd_json_value_anyf("v", "true");
4149
4150 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0))
4151 {
4152 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0), expert_severity_vals);
4153
4154 ws_assert(severity != NULL)do { if ((1) && !(severity != ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "sharkd_session.c", 4154, __func__, "assertion failed: %s"
, "severity != ((void*)0)"); } while (0)
;
4155
4156 sharkd_json_value_string("s", severity);
4157 }
4158
4159 if (((proto_tree *) node)->first_child)
4160 {
4161 if (finfo->tree_type != -1)
4162 sharkd_json_value_anyf("e", "%d", finfo->tree_type);
4163
4164 sharkd_session_process_frame_cb_tree("n", edt, (proto_tree *) node, tvbs, display_hidden);
4165 }
4166
4167 json_dumper_end_object(&dumper);
4168 }
4169 sharkd_json_array_close();
4170}
4171
4172static bool_Bool
4173sharkd_follower_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4174{
4175 register_follow_t *follower = (register_follow_t *) value;
4176 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4177 packet_info *pi = &edt->pi;
4178
4179 const int proto_id = get_follow_proto_id(follower);
4180
4181 uint32_t ignore_stream;
4182 uint32_t ignore_sub_stream;
4183
4184 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4185 {
4186 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4187 char *follow_filter;
4188
4189 follow_filter = get_follow_conv_func(follower)(edt, pi, &ignore_stream, &ignore_sub_stream);
4190
4191 json_dumper_begin_array(&dumper);
4192 json_dumper_value_string(&dumper, layer_proto);
4193 json_dumper_value_string(&dumper, follow_filter);
4194 json_dumper_end_array(&dumper);
4195
4196 g_free(follow_filter);
4197 }
4198
4199 return false0;
4200}
4201
4202static bool_Bool
4203sharkd_followers_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4204{
4205 register_follow_t *follower = (register_follow_t *) value;
4206 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4207 packet_info *pi = &edt->pi;
4208
4209 const int proto_id = get_follow_proto_id(follower);
4210
4211 unsigned stream;
4212 unsigned sub_stream;
4213
4214 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4215 {
4216 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4217 char *follow_filter;
4218
4219 follow_filter = get_follow_conv_func(follower)(edt, pi, &stream, &sub_stream);
4220
4221 sharkd_json_object_open(NULL((void*)0));
4222 sharkd_json_value_string("protocol", layer_proto);
4223 sharkd_json_value_string("filter", follow_filter);
4224 if (get_follow_stream_count_func(follower) != NULL((void*)0))
4225 {
4226 sharkd_json_value_anyf("stream", "%u", stream);
4227 }
4228 if (get_follow_sub_stream_id_func(follower) != NULL((void*)0))
4229 {
4230 sharkd_json_value_anyf("sub_stream", "%u", sub_stream);
4231 }
4232 sharkd_json_object_close();
4233
4234 g_free(follow_filter);
4235 }
4236
4237 return false0;
4238}
4239
4240struct sharkd_frame_request_data
4241{
4242 bool_Bool display_hidden;
4243};
4244
4245static void
4246sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
4247{
4248 packet_info *pi = &edt->pi;
4249 frame_data *fdata = pi->fd;
4250 wtap_block_t pkt_block = NULL((void*)0);
4251
4252 const struct sharkd_frame_request_data * const req_data = (const struct sharkd_frame_request_data * const) data;
4253 const bool_Bool display_hidden = (req_data) ? req_data->display_hidden : false0;
4254
4255 sharkd_json_result_prologue(rpcid);
4256
4257 if (fdata->has_modified_block)
4258 pkt_block = sharkd_get_modified_block(fdata);
4259 else
4260 pkt_block = pi->rec->block;
4261
4262 if (pkt_block)
4263 {
4264 unsigned i;
4265 unsigned n;
4266 char *comment;
4267
4268 n = wtap_block_count_option(pkt_block, OPT_COMMENT1);
4269
4270 sharkd_json_array_open("comment");
4271 for (i = 0; i < n; i++) {
4272 if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment)) {
4273 sharkd_json_value_string(NULL((void*)0), comment);
4274 }
4275 }
4276 sharkd_json_array_close();
4277 }
4278
4279 if (tree)
4280 {
4281 tvbuff_t **tvbs = NULL((void*)0);
4282
4283 /* arrayize data src, to speedup searching for ds_tvb index */
4284 if (data_src && data_src->next /* only needed if there are more than one data source */)
4285 {
4286 unsigned count = g_slist_length((GSList *) data_src);
4287 unsigned i;
4288
4289 tvbs = (tvbuff_t **) g_malloc0((count + 1) * sizeof(*tvbs));
4290
4291 for (i = 0; i < count; i++)
4292 {
4293 const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i);
4294
4295 tvbs[i] = get_data_source_tvb(src);
4296 }
4297
4298 tvbs[count] = NULL((void*)0);
4299 }
4300
4301 sharkd_session_process_frame_cb_tree("tree", edt, tree, tvbs, display_hidden);
4302
4303 g_free(tvbs);
4304 }
4305
4306 if (cinfo)
4307 {
4308 int col;
4309
4310 sharkd_json_array_open("col");
4311 for (col = 0; col < cinfo->num_cols; ++col)
4312 {
4313 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
4314 }
4315 sharkd_json_array_close();
4316 }
4317
4318 if (fdata->ignored)
4319 sharkd_json_value_anyf("i", "true");
4320
4321 if (fdata->marked)
4322 sharkd_json_value_anyf("m", "true");
4323
4324 if (fdata->color_filter)
4325 {
4326 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
4327 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
4328 }
4329
4330 if (data_src)
4331 {
4332 struct data_source *src = (struct data_source *) data_src->data;
4333 bool_Bool ds_open = false0;
4334
4335 tvbuff_t *tvb;
4336 unsigned length;
4337
4338 tvb = get_data_source_tvb(src);
4339 length = tvb_captured_length(tvb);
4340
4341 if (length != 0)
4342 {
4343 const unsigned char *cp = tvb_get_ptr(tvb, 0, length);
4344
4345 /* XXX pi.fd->encoding */
4346 sharkd_json_value_base64("bytes", cp, length);
4347 }
4348 else
4349 {
4350 sharkd_json_value_base64("bytes", "", 0);
4351 }
4352
4353 data_src = data_src->next;
4354 if (data_src)
4355 {
4356 sharkd_json_array_open("ds");
4357 ds_open = true1;
4358 }
4359
4360 while (data_src)
4361 {
4362 src = (struct data_source *) data_src->data;
4363
4364 json_dumper_begin_object(&dumper);
4365
4366 {
4367 char *src_name = get_data_source_name(src);
4368
4369 sharkd_json_value_string("name", src_name);
4370 wmem_free(NULL((void*)0), src_name);
4371 }
4372
4373 tvb = get_data_source_tvb(src);
4374 length = tvb_captured_length(tvb);
4375
4376 if (length != 0)
4377 {
4378 const unsigned char *cp = tvb_get_ptr(tvb, 0, length);
4379
4380 /* XXX pi.fd->encoding */
4381 sharkd_json_value_base64("bytes", cp, length);
4382 }
4383 else
4384 {
4385 sharkd_json_value_base64("bytes", "", 0);
4386 }
4387
4388 json_dumper_end_object(&dumper);
4389
4390 data_src = data_src->next;
4391 }
4392
4393 /* close ds, only if was opened */
4394 if (ds_open)
4395 sharkd_json_array_close();
4396 }
4397
4398 sharkd_json_array_open("fol");
4399 follow_iterate_followers(sharkd_follower_visit_layers_cb, edt);
4400 sharkd_json_array_close();
4401
4402 sharkd_json_array_open("followers");
4403 follow_iterate_followers(sharkd_followers_visit_layers_cb, edt);
4404 sharkd_json_array_close();
4405
4406 sharkd_json_result_epilogue();
4407}
4408
4409#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 */
4410
4411struct sharkd_iograph
4412{
4413 /* config */
4414 int hf_index;
4415 io_graph_item_unit_t calc_type;
4416 uint32_t interval;
4417 bool_Bool aot;
4418
4419 /* result */
4420 int space_items;
4421 int num_items;
4422 io_graph_item_t *items;
4423 GString *error;
4424};
4425
4426static tap_packet_status
4427sharkd_iograph_packet(void *g, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U___attribute__((unused)), tap_flags_t flags _U___attribute__((unused)))
4428{
4429 struct sharkd_iograph *graph = (struct sharkd_iograph *) g;
4430 int idx;
4431 bool_Bool update_succeeded;
4432
4433 int64_t tmp_idx = get_io_graph_index(pinfo, graph->interval);
4434 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
4435 return TAP_PACKET_DONT_REDRAW;
4436
4437 idx = (int)tmp_idx;
4438
4439 if (idx + 1 > graph->num_items)
4
Assuming the condition is true
5
Taking true branch
4440 {
4441 if (idx + 1 > graph->space_items)
6
Assuming the condition is false
7
Taking false branch
4442 {
4443 int new_size = idx + 1024;
4444
4445 graph->items = (io_graph_item_t *) g_realloc(graph->items, sizeof(io_graph_item_t) * new_size);
4446 reset_io_graph_items(&graph->items[graph->space_items], new_size - graph->space_items, graph->hf_index);
4447
4448 graph->space_items = new_size;
4449 }
4450 else if (graph->items == NULL((void*)0))
8
Assuming field 'items' is equal to NULL
9
Taking true branch
4451 {
4452 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'
4453 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'
4454 }
4455
4456 graph->num_items = idx + 1;
4457 }
4458
4459 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'
4460 /* XXX - TAP_PACKET_FAILED if the item couldn't be updated, with an error message? */
4461 return update_succeeded ? TAP_PACKET_REDRAW : TAP_PACKET_DONT_REDRAW;
4462}
4463
4464/**
4465 * sharkd_session_process_iograph()
4466 *
4467 * Process iograph request
4468 *
4469 * Input:
4470 * (o) interval - interval time, if not specified: 1000
4471 * (o) interval_units - units for interval time, must be 's', 'ms' or 'us', if not specified: ms
4472 * (m) graph0 - First graph request
4473 * (o) graph1...graph9 - Other graph requests
4474 * (o) filter0 - First graph filter
4475 * (o) filter1...filter9 - Other graph filters
4476 *
4477 * Graph requests can be one of: "packets", "bytes", "bits", "sum:<field>", "frames:<field>", "max:<field>", "min:<field>", "avg:<field>", "load:<field>",
4478 * if you use variant with <field>, you need to pass field name in filter request.
4479 *
4480 * Output object with attributes:
4481 * (m) iograph - array of graph results with attributes:
4482 * errmsg - graph cannot be constructed
4483 * items - graph values, zeros are skipped, if value is not a number it's next index encoded as hex string
4484 */
4485static void
4486sharkd_session_process_iograph(char *buf, const jsmntok_t *tokens, int count)
4487{
4488 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4489 const char *tok_interval_units = json_find_attr(buf, tokens, count, "interval_units");
4490 struct sharkd_iograph graphs[10];
4491 bool_Bool is_any_ok = false0;
4492 int graph_count;
4493
4494 int i;
4495
4496 /* default: 1000ms = one per second */
4497 uint32_t interval = 1000;
4498 const char *interval_units = "ms";
4499
4500 if (tok_interval)
4501 ws_strtou32(tok_interval, NULL((void*)0), &interval);
4502
4503 if (tok_interval_units)
4504 {
4505 if (strcmp(tok_interval_units, "us") != 0 &&
4506 strcmp(tok_interval_units, "ms") != 0 &&
4507 strcmp(tok_interval_units, "s") != 0)
4508 {
4509 sharkd_json_error(
4510 rpcid, -7003, NULL((void*)0),
4511 "Invalid interval_units parameter: '%s', must be 's', 'ms' or 'us'", tok_interval_units
4512 );
4513 return;
4514 }
4515 interval_units = tok_interval_units;
4516 }
4517
4518 uint32_t interval_us = 0;
4519 if (strcmp(interval_units, "us") == 0)
4520 {
4521 interval_us = interval;
4522 }
4523 else if (strcmp(interval_units, "ms") == 0)
4524 {
4525 interval_us = 1000 * interval;
4526 }
4527 else if (strcmp(interval_units, "s") == 0)
4528 {
4529 interval_us = 1000000 * interval;
4530 }
4531
4532 for (i = graph_count = 0; i < (int) G_N_ELEMENTS(graphs)(sizeof (graphs) / sizeof ((graphs)[0])); i++)
4533 {
4534 struct sharkd_iograph *graph = &graphs[graph_count];
4535
4536 const char *tok_graph;
4537 const char *tok_filter;
4538 char tok_format_buf[32];
4539 const char *field_name;
4540 const char *tok_aot;
4541
4542 snprintf(tok_format_buf, sizeof(tok_format_buf), "graph%d", i);
4543 tok_graph = json_find_attr(buf, tokens, count, tok_format_buf);
4544 if (!tok_graph)
4545 break;
4546
4547 snprintf(tok_format_buf, sizeof(tok_format_buf), "filter%d", i);
4548 tok_filter = json_find_attr(buf, tokens, count, tok_format_buf);
4549
4550 if (!strcmp(tok_graph, "packets"))
4551 graph->calc_type = IOG_ITEM_UNIT_PACKETS;
4552 else if (!strcmp(tok_graph, "bytes"))
4553 graph->calc_type = IOG_ITEM_UNIT_BYTES;
4554 else if (!strcmp(tok_graph, "bits"))
4555 graph->calc_type = IOG_ITEM_UNIT_BITS;
4556 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:") )
)
4557 graph->calc_type = IOG_ITEM_UNIT_CALC_SUM;
4558 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:"
) )
)
4559 graph->calc_type = IOG_ITEM_UNIT_CALC_FRAMES;
4560 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:"
) )
)
4561 graph->calc_type = IOG_ITEM_UNIT_CALC_FIELDS;
4562 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:") )
)
4563 graph->calc_type = IOG_ITEM_UNIT_CALC_MAX;
4564 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:") )
)
4565 graph->calc_type = IOG_ITEM_UNIT_CALC_MIN;
4566 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:") )
)
4567 graph->calc_type = IOG_ITEM_UNIT_CALC_AVERAGE;
4568 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:") )
)
4569 graph->calc_type = IOG_ITEM_UNIT_CALC_LOAD;
4570 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:"
) )
)
4571 graph->calc_type = IOG_ITEM_UNIT_CALC_THROUGHPUT;
4572 else
4573 break;
4574
4575 field_name = strchr(tok_graph, ':');
4576 if (field_name)
4577 field_name = field_name + 1;
4578
4579 /* io_graph_item now supports microseconds (and this parameter
4580 * is expected to be in microseconds.) */
4581 graph->interval = interval_us;
4582
4583 graph->hf_index = -1;
4584 graph->error = check_field_unit(field_name, &graph->hf_index, graph->calc_type);
4585
4586 graph->space_items = 0; /* TODO, can avoid realloc()s in sharkd_iograph_packet() by calculating: capture_time / interval */
4587 graph->num_items = 0;
4588 graph->items = NULL((void*)0);
4589
4590 snprintf(tok_format_buf, sizeof(tok_format_buf), "aot%d", i);
4591 tok_aot = json_find_attr(buf, tokens, count, tok_format_buf);
4592 if (tok_aot!=NULL((void*)0)) {
4593 graph->aot = (!strcmp(tok_aot, "true")) ? true1 : false0;
4594 }
4595 else {
4596 graph->aot = false0;
4597 }
4598
4599 if (!graph->error)
4600 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));
4601
4602 graph_count++;
4603
4604 if (graph->error)
4605 {
4606 sharkd_json_error(
4607 rpcid, -6001, NULL((void*)0),
4608 "%s", graph->error->str
4609 );
4610 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)))))
;
4611 return;
4612 }
4613
4614 if (graph->error == NULL((void*)0))
4615 is_any_ok = true1;
4616 }
4617
4618 /* retap only if we have at least one ok */
4619 if (is_any_ok)
4620 sharkd_retap();
4621
4622 sharkd_json_result_prologue(rpcid);
4623
4624 sharkd_json_array_open("iograph");
4625 for (i = 0; i < graph_count; i++)
4626 {
4627 struct sharkd_iograph *graph = &graphs[i];
4628
4629 json_dumper_begin_object(&dumper);
4630
4631 if (graph->error)
4632 {
4633 fprintf(stderrstderr, "SNAP 6002 - we should never get to here.\n");
4634 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)))))
;
4635 exit(-1);
4636 }
4637 else
4638 {
4639 int idx;
4640 int next_idx = 0;
4641
4642 sharkd_json_array_open("items");
4643 for (idx = 0; idx < graph->num_items; idx++)
4644 {
4645 double val;
4646
4647 val = get_io_graph_item(graph->items, graph->calc_type, idx, graph->hf_index, &cfile, graph->interval, graph->num_items, graph->aot);
4648
4649 /* if it's zero, don't display */
4650 if (val == 0.0)
4651 continue;
4652
4653 /* cause zeros are not printed, need to output index */
4654 if (next_idx != idx)
4655 sharkd_json_value_stringf(NULL((void*)0), "%x", idx);
4656
4657 sharkd_json_value_anyf(NULL((void*)0), "%f", val);
4658 next_idx = idx + 1;
4659 }
4660 sharkd_json_array_close();
4661 }
4662 json_dumper_end_object(&dumper);
4663
4664 remove_tap_listener(graph);
4665 g_free(graph->items);
4666 }
4667 sharkd_json_array_close();
4668
4669 sharkd_json_result_epilogue();
4670}
4671
4672/**
4673 * sharkd_session_process_intervals()
4674 *
4675 * Process intervals request - generate basic capture file statistics per requested interval.
4676 *
4677 * Input:
4678 * (o) interval - interval time in ms, if not specified: 1000ms
4679 * (o) filter - filter for generating interval request
4680 *
4681 * Output object with attributes:
4682 * (m) intervals - array of intervals, with indexes:
4683 * [0] - index of interval,
4684 * [1] - number of frames during interval,
4685 * [2] - number of bytes during interval.
4686 *
4687 * (m) last - last interval number.
4688 * (m) frames - total number of frames
4689 * (m) bytes - total number of bytes
4690 *
4691 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
4692 */
4693static void
4694sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
4695{
4696 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4697 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4698
4699 const uint8_t *filter_data = NULL((void*)0);
4700
4701 struct
4702 {
4703 unsigned int frames;
4704 uint64_t bytes;
4705 } st, st_total;
4706
4707 nstime_t *start_ts;
4708
4709 uint32_t interval_ms = 1000; /* default: one per second */
4710
4711 int64_t idx;
4712 int64_t max_idx = 0;
4713
4714 if (tok_interval)
4715 ws_strtou32(tok_interval, NULL((void*)0), &interval_ms); // already validated
4716
4717 if (tok_filter)
4718 {
4719 const struct sharkd_filter_item *filter_item;
4720
4721 filter_item = sharkd_session_filter_data(tok_filter);
4722 if (!filter_item)
4723 {
4724 sharkd_json_error(
4725 rpcid, -7001, NULL((void*)0),
4726 "Invalid filter parameter: %s", tok_filter
4727 );
4728 return;
4729 }
4730 filter_data = filter_item->filtered;
4731 }
4732
4733 st_total.frames = 0;
4734 st_total.bytes = 0;
4735
4736 st.frames = 0;
4737 st.bytes = 0;
4738
4739 idx = 0;
4740
4741 sharkd_json_result_prologue(rpcid);
4742 sharkd_json_array_open("intervals");
4743
4744 start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL((void*)0);
4745
4746 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
4747 {
4748 frame_data *fdata;
4749 int64_t msec_rel;
4750 int64_t new_idx;
4751
4752 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
4753 continue;
4754
4755 fdata = sharkd_get_frame(framenum);
4756
4757 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (int64_t) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
4758 new_idx = msec_rel / interval_ms;
4759
4760 if (idx != new_idx)
4761 {
4762 if (st.frames != 0)
4763 {
4764 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4765 }
4766
4767 idx = new_idx;
4768 if (idx > max_idx)
4769 max_idx = idx;
4770
4771 st.frames = 0;
4772 st.bytes = 0;
4773 }
4774
4775 st.frames += 1;
4776 st.bytes += fdata->pkt_len;
4777
4778 st_total.frames += 1;
4779 st_total.bytes += fdata->pkt_len;
4780 }
4781
4782 if (st.frames != 0)
4783 {
4784 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4785 }
4786 sharkd_json_array_close();
4787
4788 sharkd_json_value_anyf("last", "%" PRId64"l" "d", max_idx);
4789 sharkd_json_value_anyf("frames", "%u", st_total.frames);
4790 sharkd_json_value_anyf("bytes", "%" PRIu64"l" "u", st_total.bytes);
4791
4792 sharkd_json_result_epilogue();
4793}
4794
4795/**
4796 * sharkd_session_process_frame()
4797 *
4798 * Process frame request
4799 *
4800 * Input:
4801 * (m) frame - requested frame number
4802 * (o) ref_frame - time reference frame number
4803 * (o) prev_frame - previously displayed frame number
4804 * (o) proto - set if output frame tree
4805 * (o) columns - set if output frame columns
4806 * (o) color - set if output color-filter bg/fg
4807 * (o) bytes - set if output frame bytes
4808 * (o) hidden - set if output hidden tree fields
4809 *
4810 * Output object with attributes:
4811 * (m) err - 0 if succeed
4812 * (o) tree - array of frame nodes with attributes:
4813 * l - label
4814 * t: 'proto', 'framenum', 'url' - type of node
4815 * f - filter string
4816 * fn - field name
4817 * s - severity
4818 * e - subtree ett index
4819 * n - array of subtree nodes
4820 * h - two item array: (item start, item length)
4821 * i - two item array: (appendix start, appendix length)
4822 * p - [RESERVED] two item array: (protocol start, protocol length)
4823 * ds- data src index
4824 * url - only for t:'url', url
4825 * fnum - only for t:'framenum', frame number
4826 * g - if field is generated by Wireshark
4827 * v - if field is hidden
4828 *
4829 * (o) col - array of column data
4830 * (o) bytes - base64 of frame bytes
4831 * (o) ds - array of other data srcs
4832 * (o) comment - frame comment
4833 * (o) fol - array of follow filters:
4834 * [0] - protocol
4835 * [1] - filter string
4836 * (o) followers - array of followers with attributes:
4837 * protocol - protocol string
4838 * filter - filter string
4839 * stream - stream index number
4840 * sub_stream - sub-stream index number (optional, e.g. for HTTP/2 and QUIC streams)
4841 * (o) i - if frame is ignored
4842 * (o) m - if frame is marked
4843 * (o) bg - color filter - background color in hex
4844 * (o) fg - color filter - foreground color in hex
4845 */
4846static void
4847sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
4848{
4849 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
4850 const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
4851 const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
4852 column_info *cinfo = NULL((void*)0);
4853
4854 uint32_t framenum, ref_frame_num, prev_dis_num;
4855 uint32_t dissect_flags = SHARKD_DISSECT_FLAG_NULL0x00u;
4856 struct sharkd_frame_request_data req_data;
4857 wtap_rec rec; /* Record metadata */
4858 Buffer rec_buf; /* Record data */
4859 enum dissect_request_status status;
4860 int err;
4861 char *err_info;
4862
4863 ws_strtou32(tok_frame, NULL((void*)0), &framenum); // we have already validated this
4864
4865 ref_frame_num = (framenum != 1) ? 1 : 0;
4866 if (tok_ref_frame)
4867 {
4868 ws_strtou32(tok_ref_frame, NULL((void*)0), &ref_frame_num);
4869 if (ref_frame_num > framenum)
4870 {
4871 sharkd_json_error(
4872 rpcid, -8001, NULL((void*)0),
4873 "Invalid ref_frame - The ref_frame occurs after the frame specified"
4874 );
4875 return;
4876 }
4877 }
4878
4879 prev_dis_num = framenum - 1;
4880 if (tok_prev_frame)
4881 {
4882 ws_strtou32(tok_prev_frame, NULL((void*)0), &prev_dis_num);
4883 if (prev_dis_num >= framenum)
4884 {
4885 sharkd_json_error(
4886 rpcid, -8002, NULL((void*)0),
4887 "Invalid prev_frame - The prev_frame occurs on or after the frame specified"
4888 );
4889 return;
4890 }
4891 }
4892
4893 if (json_find_attr(buf, tokens, count, "proto") != NULL((void*)0))
4894 dissect_flags |= SHARKD_DISSECT_FLAG_PROTO_TREE0x04u;
4895 if (json_find_attr(buf, tokens, count, "bytes") != NULL((void*)0))
4896 dissect_flags |= SHARKD_DISSECT_FLAG_BYTES0x01u;
4897 if (json_find_attr(buf, tokens, count, "columns") != NULL((void*)0)) {
4898 dissect_flags |= SHARKD_DISSECT_FLAG_COLUMNS0x02u;
4899 cinfo = &cfile.cinfo;
4900 }
4901 if (json_find_attr(buf, tokens, count, "color") != NULL((void*)0))
4902 dissect_flags |= SHARKD_DISSECT_FLAG_COLOR0x08u;
4903
4904 req_data.display_hidden = (json_find_attr(buf, tokens, count, "v") != NULL((void*)0));
4905
4906 wtap_rec_init(&rec);
4907 ws_buffer_init(&rec_buf, 1514);
4908
4909 status = sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num,
4910 &rec, &rec_buf, cinfo, dissect_flags,
4911 &sharkd_session_process_frame_cb, &req_data, &err, &err_info);
4912 switch (status) {
4913
4914 case DISSECT_REQUEST_SUCCESS:
4915 /* success */
4916 break;
4917
4918 case DISSECT_REQUEST_NO_SUCH_FRAME:
4919 sharkd_json_error(
4920 rpcid, -8003, NULL((void*)0),
4921 "Invalid frame - The frame number requested is out of range"
4922 );
4923 break;
4924
4925 case DISSECT_REQUEST_READ_ERROR:
4926 sharkd_json_error(
4927 rpcid, -8003, NULL((void*)0),
4928 /* XXX - show the error details */
4929 "Read error - The frame could not be read from the file"
4930 );
4931 g_free(err_info);
4932 break;
4933 }
4934
4935 wtap_rec_cleanup(&rec);
4936 ws_buffer_free(&rec_buf);
4937}
4938
4939/**
4940 * sharkd_session_process_check()
4941 *
4942 * Process check request.
4943 *
4944 * Input:
4945 * (o) filter - filter to be checked
4946 * (o) field - field to be checked
4947 *
4948 * Output object with attributes:
4949 * (m) err - always 0
4950 * (o) filter - 'ok', 'warn' or error message
4951 * (o) field - 'ok', or 'notfound'
4952 */
4953static int
4954sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
4955{
4956 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4957 const char *tok_field = json_find_attr(buf, tokens, count, "field");
4958
4959 if (tok_filter != NULL((void*)0))
4960 {
4961 dfilter_t *dfp;
4962 df_error_t *df_err = NULL((void*)0);
4963
4964 if (dfilter_compile(tok_filter, &dfp, &df_err)dfilter_compile_full(tok_filter, &dfp, &df_err, (1U <<
1)|(1U << 2), __func__)
)
4965 {
4966 if (dfp && dfilter_deprecated_tokens(dfp))
4967 sharkd_json_warning(rpcid, "Filter contains deprecated tokens");
4968 else
4969 sharkd_json_simple_ok(rpcid);
4970
4971 dfilter_free(dfp);
4972 df_error_free(&df_err);
4973 return 0;
4974 }
4975 else
4976 {
4977 sharkd_json_error(
4978 rpcid, -5001, NULL((void*)0),
4979 "Filter invalid - %s", df_err->msg
4980 );
4981 df_error_free(&df_err);
4982 return -5001;
4983 }
4984 }
4985
4986 if (tok_field != NULL((void*)0))
4987 {
4988 header_field_info *hfi = proto_registrar_get_byname(tok_field);
4989
4990 if (!hfi)
4991 {
4992 sharkd_json_error(
4993 rpcid, -5002, NULL((void*)0),
4994 "Field %s not found", tok_field
4995 );
4996 return -5002;
4997 }
4998 else
4999 {
5000 sharkd_json_simple_ok(rpcid);
5001 return 0;
5002 }
5003 }
5004
5005 sharkd_json_simple_ok(rpcid);
5006 return 0;
5007}
5008
5009struct sharkd_session_process_complete_pref_data
5010{
5011 const char *module;
5012 const char *pref;
5013};
5014
5015static unsigned
5016sharkd_session_process_complete_pref_cb(module_t *module, void *d)
5017{
5018 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5019
5020 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
5021 return 0;
5022
5023 json_dumper_begin_object(&dumper);
5024 sharkd_json_value_string("f", module->name);
5025 sharkd_json_value_string("d", module->title);
5026 json_dumper_end_object(&dumper);
5027
5028 return 0;
5029}
5030
5031static unsigned
5032sharkd_session_process_complete_pref_option_cb(pref_t *pref, void *d)
5033{
5034 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5035 const char *pref_name = prefs_get_name(pref);
5036 const char *pref_title = prefs_get_title(pref);
5037
5038 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
5039 return 0;
5040
5041 json_dumper_begin_object(&dumper);
5042 sharkd_json_value_stringf("f", "%s.%s", data->module, pref_name);
5043 sharkd_json_value_string("d", pref_title);
5044 json_dumper_end_object(&dumper);
5045
5046 return 0; /* continue */
5047}
5048
5049/**
5050 * sharkd_session_process_complete()
5051 *
5052 * Process complete request
5053 *
5054 * Input:
5055 * (o) field - field to be completed
5056 * (o) pref - preference to be completed
5057 *
5058 * Output object with attributes:
5059 * (m) err - always 0
5060 * (o) field - array of object with attributes:
5061 * (m) f - field text
5062 * (o) t - field type (FT_ number)
5063 * (o) n - field name
5064 * (o) pref - array of object with attributes:
5065 * (m) f - pref name
5066 * (o) d - pref description
5067 */
5068static int
5069sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
5070{
5071 const char *tok_field = json_find_attr(buf, tokens, count, "field");
5072 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5073
5074 sharkd_json_result_prologue(rpcid);
5075
5076 if (tok_field != NULL((void*)0) && tok_field[0])
5077 {
5078 const size_t filter_length = strlen(tok_field);
5079 const int filter_with_dot = !!strchr(tok_field, '.');
5080
5081 void *proto_cookie;
5082 void *field_cookie;
5083 int proto_id;
5084
5085 sharkd_json_array_open("field");
5086
5087 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
5088 {
5089 protocol_t *protocol = find_protocol_by_id(proto_id);
5090 const char *protocol_filter;
5091 const char *protocol_name;
5092 header_field_info *hfinfo;
5093
5094 if (!proto_is_protocol_enabled(protocol))
5095 continue;
5096
5097 protocol_name = proto_get_protocol_long_name(protocol);
5098 protocol_filter = proto_get_protocol_filter_name(proto_id);
5099
5100 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
5101 {
5102 json_dumper_begin_object(&dumper);
5103 {
5104 sharkd_json_value_string("f", protocol_filter);
5105 sharkd_json_value_anyf("t", "%d", FT_PROTOCOL);
5106 sharkd_json_value_string("n", protocol_name);
5107 }
5108 json_dumper_end_object(&dumper);
5109 }
5110
5111 if (!filter_with_dot)
5112 continue;
5113
5114 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))
5115 {
5116 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
5117 continue;
5118
5119 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
5120 {
5121 json_dumper_begin_object(&dumper);
5122 {
5123 sharkd_json_value_string("f", hfinfo->abbrev);
5124
5125 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
5126 if (hfinfo->same_name_next == NULL((void*)0))
5127 {
5128 sharkd_json_value_anyf("t", "%d", hfinfo->type);
5129 sharkd_json_value_string("n", hfinfo->name);
5130 }
5131 }
5132 json_dumper_end_object(&dumper);
5133 }
5134 }
5135 }
5136
5137 sharkd_json_array_close();
5138 }
5139
5140 if (tok_pref != NULL((void*)0) && tok_pref[0])
5141 {
5142 struct sharkd_session_process_complete_pref_data data;
5143 char *dot_sepa;
5144
5145 data.module = tok_pref;
5146 data.pref = tok_pref;
5147
5148 sharkd_json_array_open("pref");
5149 if ((dot_sepa = strchr(tok_pref, '.')))
5150 {
5151 module_t *pref_mod;
5152
5153 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5154 data.pref = dot_sepa + 1;
5155
5156 pref_mod = prefs_find_module(data.module);
5157 if (pref_mod)
5158 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
5159
5160 *dot_sepa = '.';
5161 }
5162 else
5163 {
5164 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
5165 }
5166 sharkd_json_array_close();
5167 }
5168
5169 sharkd_json_result_epilogue();
5170
5171 return 0;
5172}
5173
5174/**
5175 * sharkd_session_process_setcomment()
5176 *
5177 * Process setcomment request
5178 *
5179 * Input:
5180 * (m) frame - frame number
5181 * (o) comment - user comment
5182 *
5183 * Output object with attributes:
5184 * (m) err - error code: 0 succeed
5185 *
5186 * Note:
5187 * For now, adds comments, doesn't remove or replace them.
5188 */
5189static void
5190sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count)
5191{
5192 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
5193 const char *tok_comment = json_find_attr(buf, tokens, count, "comment");
5194
5195 uint32_t framenum;
5196 frame_data *fdata;
5197 wtap_opttype_return_val ret;
5198 wtap_block_t pkt_block = NULL((void*)0);
5199
5200 if (!tok_frame || !ws_strtou32(tok_frame, NULL((void*)0), &framenum) || framenum == 0)
5201 {
5202 sharkd_json_error(
5203 rpcid, -3001, NULL((void*)0),
5204 "Frame number must be a positive integer"
5205 );
5206 return;
5207 }
5208
5209 fdata = sharkd_get_frame(framenum); // BUG HERE - If no file loaded you get a crash
5210 if (!fdata)
5211 {
5212 sharkd_json_error(
5213 rpcid, -3002, NULL((void*)0),
5214 "Frame number is out of range"
5215 );
5216 return;
5217 }
5218
5219 pkt_block = sharkd_get_packet_block(fdata);
5220
5221 ret = wtap_block_add_string_option(pkt_block, OPT_COMMENT1, tok_comment, strlen(tok_comment));
5222
5223 if (ret != WTAP_OPTTYPE_SUCCESS)
5224 {
5225 sharkd_json_error(
5226 rpcid, -3003, NULL((void*)0),
5227 "Unable to set the comment"
5228 );
5229 }
5230 else
5231 {
5232 sharkd_set_modified_block(fdata, pkt_block);
5233 sharkd_json_simple_ok(rpcid);
5234 }
5235}
5236
5237/**
5238 * sharkd_session_process_setconf()
5239 *
5240 * Process setconf request
5241 *
5242 * Input:
5243 * (m) name - preference name
5244 * (m) value - preference value
5245 *
5246 * Output object with attributes:
5247 * (m) err - error code: 0 succeed
5248 */
5249static void
5250sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
5251{
5252 const char *tok_name = json_find_attr(buf, tokens, count, "name");
5253 const char *tok_value = json_find_attr(buf, tokens, count, "value");
5254 char pref[4096];
5255 char *errmsg = NULL((void*)0);
5256
5257 prefs_set_pref_e ret;
5258
5259 if (!tok_name || tok_name[0] == '\0')
5260 {
5261 sharkd_json_error(
5262 rpcid, -4001, NULL((void*)0),
5263 "Preference name missing"
5264 );
5265 return;
5266 }
5267
5268 if (!tok_value)
5269 {
5270 sharkd_json_error(
5271 rpcid, -4002, NULL((void*)0),
5272 "Preference value missing"
5273 );
5274 return;
5275 }
5276
5277 snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
5278
5279 ret = prefs_set_pref(pref, &errmsg);
5280
5281 switch (ret)
5282 {
5283 case PREFS_SET_OK:
5284 sharkd_json_simple_ok(rpcid);
5285 break;
5286
5287 case PREFS_SET_OBSOLETE:
5288 sharkd_json_error(
5289 rpcid, -4003, NULL((void*)0),
5290 "The preference specified is obsolete"
5291 );
5292 break;
5293
5294 case PREFS_SET_NO_SUCH_PREF:
5295 sharkd_json_error(
5296 rpcid, -4004, NULL((void*)0),
5297 "No such preference exists"
5298 );
5299 break;
5300
5301 default:
5302 sharkd_json_error(
5303 rpcid, -4005, NULL((void*)0),
5304 "Unable to set the preference"
5305 );
5306 }
5307
5308 g_free(errmsg);
5309}
5310
5311struct sharkd_session_process_dumpconf_data
5312{
5313 module_t *module;
5314};
5315
5316static unsigned
5317sharkd_session_process_dumpconf_cb(pref_t *pref, void *d)
5318{
5319 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5320 const char *pref_name = prefs_get_name(pref);
5321
5322 char json_pref_key[512];
5323
5324 snprintf(json_pref_key, sizeof(json_pref_key), "%s.%s", data->module->name, pref_name);
5325 sharkd_json_object_open(json_pref_key);
5326
5327 switch (prefs_get_type(pref))
5328 {
5329 case PREF_UINT(1u << 0):
5330 sharkd_json_value_anyf("u", "%u", prefs_get_uint_value_real(pref, pref_current));
5331 if (prefs_get_uint_base(pref) != 10)
5332 sharkd_json_value_anyf("ub", "%u", prefs_get_uint_base(pref));
5333 break;
5334
5335 case PREF_BOOL(1u << 1):
5336 sharkd_json_value_anyf("b", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
5337 break;
5338
5339 case PREF_STRING(1u << 3):
5340 case PREF_SAVE_FILENAME(1u << 7):
5341 case PREF_OPEN_FILENAME(1u << 14):
5342 case PREF_DIRNAME(1u << 11):
5343 case PREF_PASSWORD(1u << 15):
5344 case PREF_DISSECTOR(1u << 17):
5345 sharkd_json_value_string("s", prefs_get_string_value(pref, pref_current));
5346 break;
5347
5348 case PREF_ENUM(1u << 2):
5349 {
5350 const enum_val_t *enums;
5351
5352 sharkd_json_array_open("e");
5353 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
5354 {
5355 json_dumper_begin_object(&dumper);
5356
5357 sharkd_json_value_anyf("v", "%d", enums->value);
5358
5359 if (enums->value == prefs_get_enum_value(pref, pref_current))
5360 sharkd_json_value_anyf("s", "1");
5361
5362 sharkd_json_value_string("d", enums->description);
5363
5364 json_dumper_end_object(&dumper);
5365 }
5366 sharkd_json_array_close();
5367 break;
5368 }
5369
5370 case PREF_RANGE(1u << 4):
5371 case PREF_DECODE_AS_RANGE(1u << 13):
5372 {
5373 char *range_str = range_convert_range(NULL((void*)0), prefs_get_range_value_real(pref, pref_current));
5374 sharkd_json_value_string("r", range_str);
5375 wmem_free(NULL((void*)0), range_str);
5376 break;
5377 }
5378
5379 case PREF_UAT(1u << 6):
5380 {
5381 uat_t *uat = prefs_get_uat_value(pref);
5382 unsigned idx;
5383
5384 sharkd_json_array_open("t");
5385 for (idx = 0; idx < uat->raw_data->len; idx++)
5386 {
5387 void *rec = UAT_INDEX_PTR(uat, idx)(uat->raw_data->data + (uat->record_size * (idx)));
5388 unsigned colnum;
5389
5390 sharkd_json_array_open(NULL((void*)0));
5391 for (colnum = 0; colnum < uat->ncols; colnum++)
5392 {
5393 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
5394
5395 sharkd_json_value_string(NULL((void*)0), str);
5396 g_free(str);
5397 }
5398
5399 sharkd_json_array_close();
5400 }
5401
5402 sharkd_json_array_close();
5403 break;
5404 }
5405
5406 case PREF_COLOR(1u << 8):
5407 case PREF_CUSTOM(1u << 9):
5408 case PREF_STATIC_TEXT(1u << 5):
5409 case PREF_OBSOLETE(1u << 10):
5410 /* TODO */
5411 break;
5412 }
5413
5414#if 0
5415 sharkd_json_value_string("t", prefs_get_title(pref));
5416#endif
5417
5418 sharkd_json_object_close();
5419
5420 return 0; /* continue */
5421}
5422
5423static unsigned
5424sharkd_session_process_dumpconf_mod_cb(module_t *module, void *d)
5425{
5426 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5427
5428 data->module = module;
5429 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
5430
5431 return 0;
5432}
5433
5434/**
5435 * sharkd_session_process_dumpconf()
5436 *
5437 * Process dumpconf request
5438 *
5439 * Input:
5440 * (o) pref - module, or preference, NULL for all
5441 *
5442 * Output object with attributes:
5443 * (o) prefs - object with module preferences
5444 * (m) [KEY] - preference name
5445 * (o) u - preference value (for PREF_UINT)
5446 * (o) ub - preference value suggested base for display (for PREF_UINT) and if different than 10
5447 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
5448 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME, PREF_PASSWORD, PREF_DISSECTOR)
5449 * (o) e - preference possible values (only for PREF_ENUM)
5450 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
5451 * (o) t - preference value (only for PREF_UAT)
5452 */
5453static void
5454sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
5455{
5456 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5457 module_t *pref_mod;
5458 char *dot_sepa;
5459
5460 if (!tok_pref)
5461 {
5462 struct sharkd_session_process_dumpconf_data data;
5463
5464 data.module = NULL((void*)0);
5465
5466 sharkd_json_result_prologue(rpcid);
5467
5468 sharkd_json_object_open("prefs");
5469 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
5470 sharkd_json_object_close();
5471
5472 sharkd_json_result_epilogue();
5473 return;
5474 }
5475
5476 if ((dot_sepa = strchr(tok_pref, '.')))
5477 {
5478 pref_t *pref = NULL((void*)0);
5479
5480 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5481 pref_mod = prefs_find_module(tok_pref);
5482 if (pref_mod)
5483 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
5484 *dot_sepa = '.';
5485
5486 if (pref)
5487 {
5488 struct sharkd_session_process_dumpconf_data data;
5489
5490 data.module = pref_mod;
5491
5492 sharkd_json_result_prologue(rpcid);
5493
5494 sharkd_json_object_open("prefs");
5495 sharkd_session_process_dumpconf_cb(pref, &data);
5496 sharkd_json_object_close();
5497
5498 sharkd_json_result_epilogue();
5499 return;
5500 }
5501 else
5502 {
5503 sharkd_json_error(
5504 rpcid, -9001, NULL((void*)0),
5505 "Invalid pref %s.", tok_pref
5506 );
5507 return;
5508 }
5509
5510 }
5511
5512 pref_mod = prefs_find_module(tok_pref);
5513 if (pref_mod)
5514 {
5515 struct sharkd_session_process_dumpconf_data data;
5516
5517 data.module = pref_mod;
5518
5519 sharkd_json_result_prologue(rpcid);
5520
5521 sharkd_json_object_open("prefs");
5522 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
5523 sharkd_json_object_close();
5524
5525 sharkd_json_result_epilogue();
5526 }
5527 else
5528 {
5529 sharkd_json_error(
5530 rpcid, -9002, NULL((void*)0),
5531 "Invalid pref %s.", tok_pref
5532 );
5533 }
5534}
5535
5536struct sharkd_download_rtp
5537{
5538 rtpstream_id_t id;
5539 GSList *packets;
5540 double start_time;
5541};
5542
5543static void
5544sharkd_rtp_download_free_items(void *ptr)
5545{
5546 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
5547
5548 g_free(rtp_packet->info);
5549 g_free(rtp_packet->payload_data);
5550 g_free(rtp_packet);
5551}
5552
5553static void
5554sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
5555{
5556 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
5557 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
5558
5559 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
5560
5561 uint32_t audio_out_rate_ = 0;
5562 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
5563 struct SpeexResamplerState_ *audio_resampler_ = NULL((void*)0);
5564
5565 size_t resample_buff_len = 0x1000;
5566 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
5567 spx_uint32_t cur_in_rate = 0;
5568 char *write_buff = NULL((void*)0);
5569 size_t write_bytes = 0;
5570 unsigned channels = 0;
5571 unsigned sample_rate = 0;
5572
5573 GSList *l;
5574
5575 for (l = req->packets; l; l = l->next)
5576 {
5577 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
5578
5579 SAMPLE *decode_buff = NULL((void*)0);
5580 size_t decoded_bytes;
5581
5582 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
5583 if (decoded_bytes == 0 || sample_rate == 0)
5584 {
5585 /* We didn't decode anything. Clean up and prep for the next packet. */
5586 g_free(decode_buff);
5587 continue;
5588 }
5589
5590 if (audio_out_rate_ == 0)
5591 {
5592 uint32_t tmp32;
5593 uint16_t tmp16;
5594 char wav_hdr[44];
5595
5596 /* First non-zero wins */
5597 audio_out_rate_ = sample_rate;
5598
5599 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
5600
5601 /* write WAVE header */
5602 memset(&wav_hdr, 0, sizeof(wav_hdr));
5603 memcpy(&wav_hdr[0], "RIFF", 4);
5604 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5605 memcpy(&wav_hdr[8], "WAVE", 4);
5606
5607 memcpy(&wav_hdr[12], "fmt ", 4);
5608 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
5609 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
5610 /* # channels */
5611 tmp16 = channels;
5612 memcpy(&wav_hdr[22], &tmp16, 2);
5613 /* sample rate */
5614 tmp32 = sample_rate;
5615 memcpy(&wav_hdr[24], &tmp32, 4);
5616 /* byte rate */
5617 tmp32 = sample_rate * channels * sample_bytes_;
5618 memcpy(&wav_hdr[28], &tmp32, 4);
5619 /* block align */
5620 tmp16 = channels * sample_bytes_;
5621 memcpy(&wav_hdr[32], &tmp16, 2);
5622 /* bits per sample */
5623 tmp16 = 8 * sample_bytes_;
5624 memcpy(&wav_hdr[34], &tmp16, 2);
5625
5626 memcpy(&wav_hdr[36], "data", 4);
5627 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5628
5629 json_dumper_write_base64(&dumper, wav_hdr, sizeof(wav_hdr));
5630 }
5631
5632 // Write samples to our file.
5633 write_buff = (char *) decode_buff;
5634 write_bytes = decoded_bytes;
5635
5636 if (audio_out_rate_ != sample_rate)
5637 {
5638 spx_uint32_t in_len, out_len;
5639
5640 /* Resample the audio to match our previous output rate. */
5641 if (!audio_resampler_)
5642 {
5643 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL((void*)0));
5644 speex_resampler_skip_zeros(audio_resampler_);
5645 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
5646 }
5647 else
5648 {
5649 spx_uint32_t audio_out_rate;
5650 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
5651
5652 if (sample_rate != cur_in_rate)
5653 {
5654 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
5655 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
5656 }
5657 }
5658 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
5659 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
5660 if (out_len * sample_bytes_ > resample_buff_len)
5661 {
5662 while ((out_len * sample_bytes_ > resample_buff_len))
5663 resample_buff_len *= 2;
5664 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
5665 }
5666
5667 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
5668 write_buff = (char *) resample_buff;
5669 write_bytes = out_len * sample_bytes_;
5670 }
5671
5672 /* Write the decoded, possibly-resampled audio */
5673 json_dumper_write_base64(&dumper, write_buff, write_bytes);
5674
5675 g_free(decode_buff);
5676 }
5677
5678 g_free(resample_buff);
5679 g_hash_table_destroy(decoders_hash_);
5680}
5681
5682static tap_packet_status
5683sharkd_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)))
5684{
5685 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
5686 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
5687
5688 /* do not consider RTP packets without a setup frame */
5689 if (rtp_info->info_setup_frame_num == 0)
5690 return TAP_PACKET_DONT_REDRAW;
5691
5692 if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info))
5693 {
5694 rtp_packet_t *rtp_packet;
5695
5696 rtp_packet = g_new0(rtp_packet_t, 1)((rtp_packet_t *) g_malloc0_n ((1), sizeof (rtp_packet_t)));
5697 rtp_packet->info = (struct _rtp_info *) g_memdup2(rtp_info, sizeof(struct _rtp_info));
5698
5699 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
5700 rtp_packet->payload_data = (uint8_t *) g_memdup2(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
5701
5702 if (!req_rtp->packets)
5703 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
5704
5705 rtp_packet->frame_num = pinfo->num;
5706 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
5707
5708 /* XXX, O(n) optimize */
5709 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
5710 }
5711
5712 return TAP_PACKET_DONT_REDRAW;
5713}
5714
5715static bool_Bool
5716sharkd_session_eo_retap_listener(const char *tap_type) {
5717 bool_Bool ok = true1;
5718 register_eo_t *eo = NULL((void*)0);
5719 GString *tap_error = NULL((void*)0);
5720 void *tap_data = NULL((void*)0);
5721 GFreeFunc tap_free = NULL((void*)0);
5722
5723 // get <name> from eo:<name>, get_eo_by_name only needs the name (http etc.)
5724 eo = get_eo_by_name(tap_type + 3);
5725 if (!eo)
5726 {
5727 ok = false0;
5728 sharkd_json_error(
5729 rpcid, -11011, NULL((void*)0),
5730 "sharkd_session_eo_retap_listener() eo=%s not found", tap_type + 3
5731 );
5732 }
5733
5734 if (ok)
5735 {
5736 tap_error = sharkd_session_eo_register_tap_listener(eo, tap_type, NULL((void*)0), NULL((void*)0), &tap_data, &tap_free);
5737 if (tap_error)
5738 {
5739 ok = false0;
5740 sharkd_json_error(
5741 rpcid, -10002, NULL((void*)0),
5742 "sharkd_session_eo_retap_listener() sharkd_session_eo_register_tap_listener error %s",
5743 tap_error->str);
5744 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)))))
;
5745 }
5746 }
5747
5748 if (ok)
5749 sharkd_retap();
5750
5751 if (!tap_error)
5752 remove_tap_listener(tap_data);
5753
5754 if (tap_free)
5755 tap_free(tap_data);
5756
5757 return ok;
5758}
5759
5760/**
5761 * sharkd_session_process_download()
5762 *
5763 * Process download request
5764 *
5765 * Input:
5766 * (m) token - token to download
5767 *
5768 * Output object with attributes:
5769 * (o) file - suggested name of file
5770 * (o) mime - suggested content type
5771 * (o) data - payload base64 encoded
5772 */
5773static void
5774sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
5775{
5776 const char *tok_token = json_find_attr(buf, tokens, count, "token");
5777
5778 if (!tok_token)
5779 {
5780 sharkd_json_error(
5781 rpcid, -10005, NULL((void*)0),
5782 "missing token"
5783 );
5784 return;
5785 }
5786
5787 if (!strncmp(tok_token, "eo:", 3))
5788 {
5789 // get eo:<name> from eo:<name>_<row>
5790 char *tap_type = g_strdup(tok_token)g_strdup_inline (tok_token);
5791 char *tmp = strrchr(tap_type, '_');
5792 if (tmp)
5793 *tmp = '\0';
5794
5795 // if eo:<name> not in sharkd_eo_list, retap
5796 if (!sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type) &&
5797 !sharkd_session_eo_retap_listener(tap_type))
5798 {
5799 g_free(tap_type);
5800 // sharkd_json_error called in sharkd_session_eo_retap_listener
5801 return;
5802 }
5803
5804 g_free(tap_type);
5805
5806 struct sharkd_export_object_list *object_list;
5807 const export_object_entry_t *eo_entry = NULL((void*)0);
5808
5809 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
5810 {
5811 size_t eo_type_len = strlen(object_list->type);
5812
5813 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
5814 {
5815 int row;
5816
5817 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
5818 break;
5819
5820 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
5821 break;
5822 }
5823 }
5824
5825 if (eo_entry)
5826 {
5827 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
5828 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
5829
5830 sharkd_json_result_prologue(rpcid);
5831 sharkd_json_value_string("file", filename);
5832 sharkd_json_value_string("mime", mime);
5833 sharkd_json_value_base64("data", eo_entry->payload_data, eo_entry->payload_len);
5834 sharkd_json_result_epilogue();
5835 }
5836 else
5837 {
5838 sharkd_json_result_prologue(rpcid);
5839 sharkd_json_result_epilogue();
5840 }
5841 }
5842 else if (!strcmp(tok_token, "ssl-secrets"))
5843 {
5844 size_t str_len;
5845 char *str = ssl_export_sessions(&str_len);
5846
5847 if (str)
5848 {
5849 const char *mime = "text/plain";
5850 const char *filename = "keylog.txt";
5851
5852 sharkd_json_result_prologue(rpcid);
5853 sharkd_json_value_string("file", filename);
5854 sharkd_json_value_string("mime", mime);
5855 sharkd_json_value_base64("data", str, str_len);
5856 sharkd_json_result_epilogue();
5857 }
5858 g_free(str);
5859 }
5860 else if (!strncmp(tok_token, "rtp:", 4))
5861 {
5862 struct sharkd_download_rtp rtp_req;
5863 GString *tap_error;
5864
5865 memset(&rtp_req, 0, sizeof(rtp_req));
5866 if (!sharkd_rtp_match_init(&rtp_req.id, tok_token + 4))
5867 {
5868 sharkd_json_error(
5869 rpcid, -10001, NULL((void*)0),
5870 "sharkd_session_process_download() rtp tokenizing error %s", tok_token
5871 );
5872 return;
5873 }
5874
5875 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));
5876 if (tap_error)
5877 {
5878 sharkd_json_error(
5879 rpcid, -10002, NULL((void*)0),
5880 "sharkd_session_process_download() rtp error %s", tap_error->str
5881 );
5882 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)))))
;
5883 return;
5884 }
5885
5886 sharkd_retap();
5887 remove_tap_listener(&rtp_req);
5888
5889 if (rtp_req.packets)
5890 {
5891 const char *mime = "audio/x-wav";
5892 const char *filename = tok_token;
5893
5894 sharkd_json_result_prologue(rpcid);
5895 sharkd_json_value_string("file", filename);
5896 sharkd_json_value_string("mime", mime);
5897
5898 json_dumper_set_member_name(&dumper, "data");
5899 json_dumper_begin_base64(&dumper);
5900 sharkd_rtp_download_decode(&rtp_req);
5901 json_dumper_end_base64(&dumper);
5902
5903 sharkd_json_result_epilogue();
5904
5905 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
5906 }
5907 else
5908 {
5909 sharkd_json_error(
5910 rpcid, -10003, NULL((void*)0),
5911 "no rtp data available"
5912 );
5913 }
5914 }
5915 else
5916 {
5917 sharkd_json_error(
5918 rpcid, -10004, NULL((void*)0),
5919 "unrecognized token"
5920 );
5921 }
5922}
5923
5924static void
5925sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
5926{
5927 if (json_prep(buf, tokens, count))
5928 {
5929 /* don't need [0] token */
5930 tokens++;
5931 count--;
5932
5933 const char* tok_method = json_find_attr(buf, tokens, count, "method");
5934
5935 if (!tok_method) {
5936 sharkd_json_error(
5937 rpcid, -32601, NULL((void*)0),
5938 "No method found");
5939 return;
5940 }
5941 if (!strcmp(tok_method, "load"))
5942 sharkd_session_process_load(buf, tokens, count);
5943 else if (!strcmp(tok_method, "status"))
5944 sharkd_session_process_status();
5945 else if (!strcmp(tok_method, "analyse"))
5946 sharkd_session_process_analyse();
5947 else if (!strcmp(tok_method, "info"))
5948 sharkd_session_process_info();
5949 else if (!strcmp(tok_method, "check"))
5950 sharkd_session_process_check(buf, tokens, count);
5951 else if (!strcmp(tok_method, "complete"))
5952 sharkd_session_process_complete(buf, tokens, count);
5953 else if (!strcmp(tok_method, "frames"))
5954 sharkd_session_process_frames(buf, tokens, count);
5955 else if (!strcmp(tok_method, "tap"))
5956 sharkd_session_process_tap(buf, tokens, count);
5957 else if (!strcmp(tok_method, "follow"))
5958 sharkd_session_process_follow(buf, tokens, count);
5959 else if (!strcmp(tok_method, "iograph"))
5960 sharkd_session_process_iograph(buf, tokens, count);
5961 else if (!strcmp(tok_method, "intervals"))
5962 sharkd_session_process_intervals(buf, tokens, count);
5963 else if (!strcmp(tok_method, "frame"))
5964 sharkd_session_process_frame(buf, tokens, count);
5965 else if (!strcmp(tok_method, "setcomment"))
5966 sharkd_session_process_setcomment(buf, tokens, count);
5967 else if (!strcmp(tok_method, "setconf"))
5968 sharkd_session_process_setconf(buf, tokens, count);
5969 else if (!strcmp(tok_method, "dumpconf"))
5970 sharkd_session_process_dumpconf(buf, tokens, count);
5971 else if (!strcmp(tok_method, "download"))
5972 sharkd_session_process_download(buf, tokens, count);
5973 else if (!strcmp(tok_method, "bye"))
5974 {
5975 sharkd_json_simple_ok(rpcid);
5976 exit(0);
5977 }
5978 else
5979 {
5980 sharkd_json_error(
5981 rpcid, -32601, NULL((void*)0),
5982 "The method \"%s\" is unknown", tok_method
5983 );
5984 }
5985 }
5986}
5987
5988int
5989sharkd_session_main(int mode_setting)
5990{
5991 char buf[8 * 1024];
5992 jsmntok_t *tokens = NULL((void*)0);
5993 int tokens_max = -1;
5994
5995 mode = mode_setting;
5996
5997 fprintf(stderrstderr, "Hello in child.\n");
5998
5999 dumper.output_file = stdoutstdout;
6000
6001 filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free);
6002
6003#ifdef HAVE_MAXMINDDB1
6004 /* mmdbresolve was stopped before fork(), force starting it */
6005 uat_get_table_by_name("MaxMind Database Paths")->post_update_cb();
6006#endif
6007
6008 set_resolution_synchrony(true1);
6009
6010 while (fgets(buf, sizeof(buf), stdinstdin))
6011 {
6012 /* every command is line separated JSON */
6013 int ret;
6014
6015 ret = json_parse(buf, NULL((void*)0), 0);
6016 if (ret <= 0)
6017 {
6018 sharkd_json_error(
6019 rpcid, -32600, NULL((void*)0),
6020 "Invalid JSON(1)"
6021 );
6022 continue;
6023 }
6024
6025 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
6026 ret += 1;
6027
6028 if (tokens == NULL((void*)0) || tokens_max < ret)
6029 {
6030 tokens_max = ret;
6031 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
6032 }
6033
6034 memset(tokens, 0, ret * sizeof(jsmntok_t));
6035
6036 ret = json_parse(buf, tokens, ret);
6037 if (ret <= 0)
6038 {
6039 sharkd_json_error(
6040 rpcid, -32600, NULL((void*)0),
6041 "Invalid JSON(2)"
6042 );
6043 continue;
6044 }
6045
6046 host_name_lookup_process();
6047
6048 sharkd_session_process(buf, tokens, ret);
6049 }
6050
6051 g_hash_table_destroy(filter_table);
6052 g_free(tokens);
6053
6054 return 0;
6055}

/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__ */