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