Bug Summary

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

←

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