forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharkd_session.c
6055 lines (5072 loc) · 197 KB
/
sharkd_session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sharkd_session.c
*
* Copyright (C) 2016 Jakub Zawadzki
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "wtap_opttypes.h"
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <glib.h>
#include <wsutil/wsjson.h>
#include <wsutil/json_dumper.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wsgcrypt.h>
#include <file.h>
#include <epan/epan_dissect.h>
#include <epan/exceptions.h>
#include <epan/color_filters.h>
#include <epan/prefs.h>
#include <epan/prefs-int.h>
#include <epan/uat-int.h>
#include <wiretap/wtap.h>
#include <epan/column.h>
#include <epan/column-info.h>
#include <ui/ssl_key_export.h>
#include <ui/io_graph_item.h>
#include <epan/stats_tree_priv.h>
#include <epan/stat_tap_ui.h>
#include <epan/conversation_table.h>
#include <epan/sequence_analysis.h>
#include <epan/expert.h>
#include <epan/export_object.h>
#include <epan/follow.h>
#include <epan/rtd_table.h>
#include <epan/srt_table.h>
#include <epan/to_str.h>
#include <epan/dissectors/packet-h225.h>
#include <epan/rtp_pt.h>
#include <ui/voip_calls.h>
#include <ui/rtp_stream.h>
#include <ui/tap-rtp-common.h>
#include <ui/tap-rtp-analysis.h>
#include <ui/cli/tap-protohierstat.h>
#include <ui/cli/tap-voip.h>
#include <wsutil/version_info.h>
#include <epan/to_str.h>
#include <epan/addr_resolv.h>
#include <epan/dissectors/packet-rtp.h>
#include <ui/rtp_media.h>
#include <ui/mcast_stream.h>
#include <speex/speex_resampler.h>
#include <epan/maxmind_db.h>
#include <wsutil/pint.h>
#include <wsutil/strnatcmp.h>
#include <wsutil/strtoi.h>
#include "globals.h"
#include "sharkd.h"
struct sharkd_filter_item
{
uint8_t *filtered; /* can be NULL if all frames are matching for given filter. */
};
static GHashTable *filter_table;
static int mode;
static uint32_t rpcid;
static json_dumper dumper;
static const char *
json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
{
int i;
for (i = 0; i < count; i += 2)
{
const char *tok_attr = &buf[tokens[i + 0].start];
const char *tok_value = &buf[tokens[i + 1].start];
if (!strcmp(tok_attr, attr))
return tok_value;
}
return NULL;
}
static void
json_print_base64(const uint8_t *data, size_t len)
{
json_dumper_begin_base64(&dumper);
json_dumper_write_base64(&dumper, data, len);
json_dumper_end_base64(&dumper);
}
static void G_GNUC_PRINTF(2, 3)
sharkd_json_value_anyf(const char *key, const char *format, ...)
{
if (key)
json_dumper_set_member_name(&dumper, key);
va_list ap;
va_start(ap, format);
json_dumper_value_va_list(&dumper, format, ap);
va_end(ap);
}
static void
sharkd_json_value_string(const char *key, const char *str)
{
if (key)
json_dumper_set_member_name(&dumper, key);
json_dumper_value_string(&dumper, str);
}
static void
sharkd_json_value_base64(const char *key, const uint8_t *data, size_t len)
{
if (key)
json_dumper_set_member_name(&dumper, key);
json_print_base64(data, len);
}
static void G_GNUC_PRINTF(2, 3)
sharkd_json_value_stringf(const char *key, const char *format, ...)
{
if (key)
json_dumper_set_member_name(&dumper, key);
va_list ap;
va_start(ap, format);
char* sformat = ws_strdup_printf("\"%s\"", format);
json_dumper_value_va_list(&dumper, sformat, ap);
g_free(sformat);
va_end(ap);
}
static void
sharkd_json_array_open(const char *key)
{
if (key)
json_dumper_set_member_name(&dumper, key);
json_dumper_begin_array(&dumper);
}
static void
sharkd_json_array_close(void)
{
json_dumper_end_array(&dumper);
}
static void
sharkd_json_object_open(const char *key)
{
if (key)
json_dumper_set_member_name(&dumper, key);
json_dumper_begin_object(&dumper);
}
static void
sharkd_json_object_close(void)
{
json_dumper_end_object(&dumper);
}
static void
sharkd_json_response_open(uint32_t id)
{
json_dumper_begin_object(&dumper); // start the message
sharkd_json_value_string("jsonrpc", "2.0");
sharkd_json_value_anyf("id", "%d", id);
}
static void
sharkd_json_response_close(void)
{
json_dumper_end_object(&dumper); // end the message
json_dumper_finish(&dumper);
/*
* We do an explicit fflush after every line, because
* we want output to be written to the socket as soon
* as the line is complete.
*
* The stream is fully-buffered by default, so it's
* only flushed when the buffer fills or the FILE *
* is closed. On UN*X, we could set it to be line
* buffered, but the MSVC standard I/O routines don't
* support line buffering - they only support *byte*
* buffering, doing a write for every byte written,
* which is too inefficient, and full buffering,
* which is what you get if you request line buffering.
*/
fflush(stdout);
}
static void
sharkd_json_result_prologue(uint32_t id)
{
sharkd_json_response_open(id);
sharkd_json_object_open("result"); // start the result object
}
static void
sharkd_json_result_epilogue(void)
{
json_dumper_end_object(&dumper); // end the result object
sharkd_json_response_close();
}
static void
sharkd_json_result_array_prologue(uint32_t id)
{
sharkd_json_response_open(id);
sharkd_json_array_open("result"); // start the result array
}
static void
sharkd_json_result_array_epilogue(void)
{
sharkd_json_array_close(); // end of result array
sharkd_json_response_close();
}
static void
sharkd_json_simple_ok(uint32_t id)
{
sharkd_json_result_prologue(id);
sharkd_json_value_string("status", "OK");
sharkd_json_result_epilogue();
}
static void
sharkd_json_warning(uint32_t id, char *warning)
{
sharkd_json_result_prologue(id);
sharkd_json_value_string("status", "Warning");
sharkd_json_value_string("warning", warning);
sharkd_json_result_epilogue();
}
static void G_GNUC_PRINTF(4, 5)
sharkd_json_error(uint32_t id, int code, char* data, char* format, ...)
{
sharkd_json_response_open(id);
sharkd_json_object_open("error");
sharkd_json_value_anyf("code", "%d", code);
if (format)
{
// format the text message
va_list args;
va_start(args, format);
char *error_msg = ws_strdup_vprintf(format, args);
va_end(args);
sharkd_json_value_string("message", error_msg);
g_free(error_msg);
}
sharkd_json_object_close();
if (data)
sharkd_json_value_string("data", data);
sharkd_json_response_close();
}
static bool
is_param_match(const char *param_in, const char *valid_param)
{
char* ptr;
if ((ptr = g_strrstr(valid_param, "*")))
{
size_t prefix_len = ptr - valid_param;
return !strncmp(param_in, valid_param, prefix_len);
}
else
return !strcmp(param_in, valid_param);
}
/*
* json_prep does four things:
*
* 1. check the syntax of the root and parameter members
* 2. tokenize the names and values by zero terminating them
* 3. unescape the names and values
* 4. extracts and saves the rpcid
* - we have to do it here as it's needed for the error messages
*
* The objective is to minimise the validation work in the functions
* that process each called method.
*
* This gets a little messy as the JSON parser creates a flat list
* of all members rather than create a tree.
*/
static bool
json_prep(char* buf, const jsmntok_t* tokens, int count)
{
int i;
const char* method = NULL;
char* attr_name = NULL;
char* attr_value = NULL;
#define SHARKD_JSON_ANY 0
#define SHARKD_JSON_STRING 1
#define SHARKD_JSON_INTEGER 2
#define SHARKD_JSON_UINTEGER 3
#define SHARKD_JSON_FLOAT 4
#define SHARKD_JSON_OBJECT 5
#define SHARKD_JSON_ARRAY 6
#define SHARKD_JSON_BOOLEAN 7
#define SHARKD_ARRAY_END 99
struct member_attribute {
const char* parent_ctx;
const char* name;
int level;
jsmntype_t type;
int value_type;
bool is_mandatory;
};
#define SHARKD_MANDATORY true
#define SHARKD_OPTIONAL false
/*
* The member attribute structure is key to the syntax checking. The
* array contains all of the root level (1) member names, the data
* types permissible for the value and a boolean that indicates whether
* or not the member is mandatory.
*
* Once we get into the next layer (2) of the json tree, we need to check
* params member names and data types dependent in the context of the method
* (parent_ctx).
*/
struct member_attribute name_array[] = {
// Root members
{NULL, "jsonrpc", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{NULL, "userid", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{NULL, "id", 1, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
{NULL, "method", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{NULL, "params", 1, JSMN_OBJECT, SHARKD_JSON_OBJECT, SHARKD_OPTIONAL},
// Valid methods
{"method", "analyse", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "bye", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "check", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "complete", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "download", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "dumpconf", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "follow", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "frame", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "frames", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "info", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "intervals", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "iograph", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "load", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "setcomment", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "setconf", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "status", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"method", "tap", 1, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
// Parameters and their method context
{"check", "field", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"check", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"complete", "field", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"complete", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"download", "token", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"dumpconf", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"follow", "follow", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"follow", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"follow", "sub_stream", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"frame", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
{"frame", "proto", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"frame", "ref_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"frame", "prev_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"frame", "columns", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"frame", "color", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"frame", "bytes", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"frame", "hidden", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"frames", "column*", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY, SHARKD_OPTIONAL},
{"frames", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"frames", "skip", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"frames", "limit", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"frames", "refs", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"intervals", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"intervals", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_OPTIONAL},
{"iograph", "interval_units", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"iograph", "graph1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "graph9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "filter9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"iograph", "aot0", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot1", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot2", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot3", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot4", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot5", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot6", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot7", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot8", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"iograph", "aot9", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN, SHARKD_OPTIONAL},
{"load", "file", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"setcomment", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER, SHARKD_MANDATORY},
{"setcomment", "comment", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"setconf", "name", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"setconf", "value", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY, SHARKD_MANDATORY},
{"tap", "tap0", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_MANDATORY},
{"tap", "tap1", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap2", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap3", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap4", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap5", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap6", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap7", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap8", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap9", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap10", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap11", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap12", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap13", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap14", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "tap15", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
{"tap", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING, SHARKD_OPTIONAL},
// End of the name_array
{NULL, NULL, 0, JSMN_STRING, SHARKD_ARRAY_END, SHARKD_OPTIONAL},
};
rpcid = 0;
/* sanity check, and split strings */
if (count < 1 || tokens[0].type != JSMN_OBJECT)
{
sharkd_json_error(
rpcid, -32600, NULL,
"The request must an object"
);
return false;
}
/* don't need [0] token */
tokens++;
count--;
if (count & 1)
{
sharkd_json_error(
rpcid, -32600, NULL,
"The request must contain name/value pairs"
);
return false;
}
for (i = 0; i < count; i += 2)
{
buf[tokens[i + 0].end] = '\0';
buf[tokens[i + 1].end] = '\0';
}
// we must get the id as soon as possible so that it's available in all future error messages
attr_value = (char*)json_find_attr(buf, tokens, count, "id");
if (attr_value)
{
if (!ws_strtou32(attr_value, NULL, &rpcid))
{
sharkd_json_error(
rpcid, -32600, NULL,
"The id value must be a positive integer"
);
return false;
}
}
method = json_find_attr(buf, tokens, count, "method");
if (method)
{
bool is_supported = false;
i = 0; // name array index
// check that the request method is good
while (name_array[i].value_type != SHARKD_ARRAY_END)
{
if (name_array[i].parent_ctx)
{
if (!strcmp(method, name_array[i].name) && !strcmp(name_array[i].parent_ctx, "method"))
is_supported = true; // the method is valid
}
i++;
}
if (!is_supported)
{
sharkd_json_error(
rpcid, -32601, NULL,
"The method %s is not supported", method
);
return false;
}
}
for (i = 0; i < count; i += 2)
{
if (tokens[i].type != JSMN_STRING)
{
sharkd_json_error(
rpcid, -32600, NULL,
"Member names must be a string - member %d is not string", (i / 2) + 1
);
return false;
}
attr_name = &buf[tokens[i + 0].start];
attr_value = &buf[tokens[i + 1].start];
if (!strcmp(attr_name, "jsonrpc"))
{
if (strcmp(&buf[tokens[i + 1].start], "2.0"))
{
sharkd_json_error(
rpcid, -32600, NULL,
"Only JSON %s is supported", "2.0"
);
return false;
}
}
/* unescape only value, as keys are simple strings */
if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(attr_value))
{
sharkd_json_error(
rpcid, -32600, NULL,
"Cannot unescape the value string of member %d", (i / 2) + 1
);
return false;
}
/* Confirm that the member is valid */
bool match = false;
// We need to check root members (level 1) and parameters (level 2), hence the for loop.
for (int level = 1; level < 3; level++)
{
size_t j = 0;
while (name_array[j].value_type != SHARKD_ARRAY_END) // iterate through the array until we hit the end
{
if (is_param_match(attr_name, name_array[j].name) && name_array[j].level == level)
{
// We need to be sure the match is in the correct context
// i.e. is this a match for a root member (level 1) or for a parameter (level 2).
if (level == 1)
{
// need to guard against a parameter name matching a method name
if (method)
{
if (name_array[j].parent_ctx)
{
j++;
continue;
}
if (!strcmp(method, &buf[tokens[i + 0].start]))
{
j++;
continue;
}
}
match = true;
}
else if (method)
{
if (level == 2 && !strcmp(name_array[j].parent_ctx, method))
match = true;
else
{
j++;
continue;
}
}
else
{
j++;
continue;
}
// The match looks good, let's now check the data types
if (tokens[i + 1].type != name_array[j].type && name_array[j].type != SHARKD_JSON_ANY)
{
sharkd_json_error(
rpcid, -32600, NULL,
"The data type for member %s is not valid", attr_name
);
return false;
}
else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_UINTEGER)
{
uint32_t temp;
if (!ws_strtou32(attr_value, NULL, &temp) || temp <= 0)
{
sharkd_json_error(
rpcid, -32600, NULL,
"The value for %s must be a positive integer", name_array[j].name
);
return false;
}
}
else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_BOOLEAN)
{
if (strcmp(attr_value, "true") && strcmp(attr_value, "false"))
{
sharkd_json_error(
rpcid, -32600, NULL,
"The value for %s must be a boolean (true or false)", name_array[j].name
);
return false;
}
}
break; // looks like a valid match
}
j++;
}
}
if (!match)
{
sharkd_json_error(
rpcid, -32600, NULL,
"%s is not a valid member name", attr_name
);
return false;
}
}
/* check for mandatory members */
size_t j = 0;
while (name_array[j].value_type != SHARKD_ARRAY_END)
{
if (name_array[j].is_mandatory && name_array[j].level == 1)
{
if (!json_find_attr(buf, tokens, count, name_array[j].name))
{
sharkd_json_error(
rpcid, -32600, NULL,
"Mandatory member %s is missing", name_array[j].name
);
return false;
}
}
j++;
}
// check that the current request contains the mandatory parameters
j = 0;
while (name_array[j].value_type != SHARKD_ARRAY_END)
{
if (name_array[j].is_mandatory && name_array[j].level == 2 && !strcmp(method, name_array[j].parent_ctx))
{
if (!json_find_attr(buf, tokens, count, name_array[j].name))
{
sharkd_json_error(
rpcid, -32600, NULL,
"Mandatory parameter %s is missing", name_array[j].name
);
return false;
}
}
j++;
}
// check that the parameters for the current request are valid for the method and that the data type for the value is valid
return true;
}
static void
sharkd_session_filter_free(void *data)
{
struct sharkd_filter_item *l = (struct sharkd_filter_item *) data;
g_free(l->filtered);
g_free(l);
}
static const struct sharkd_filter_item *
sharkd_session_filter_data(const char *filter)
{
struct sharkd_filter_item *l;
l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter);
if (!l)
{
uint8_t *filtered = NULL;
int ret = sharkd_filter(filter, &filtered);
if (ret == -1)
return NULL;
l = g_new(struct sharkd_filter_item, 1);
l->filtered = filtered;
g_hash_table_insert(filter_table, g_strdup(filter), l);
}
return l;
}
static bool
sharkd_rtp_match_init(rtpstream_id_t *id, const char *init_str)
{
bool ret = false;
char **arr;
uint32_t tmp_addr_src, tmp_addr_dst;
address tmp_src_addr, tmp_dst_addr;
memset(id, 0, sizeof(*id));
arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
if (g_strv_length(arr) != 5)
goto fail;
/* TODO, for now only IPv4 */
if (!get_host_ipaddr(arr[0], &tmp_addr_src))
goto fail;
if (!ws_strtou16(arr[1], NULL, &id->src_port))
goto fail;
if (!get_host_ipaddr(arr[2], &tmp_addr_dst))
goto fail;
if (!ws_strtou16(arr[3], NULL, &id->dst_port))
goto fail;
if (!ws_hexstrtou32(arr[4], NULL, &id->ssrc))
goto fail;
set_address(&tmp_src_addr, AT_IPv4, 4, &tmp_addr_src);
copy_address(&id->src_addr, &tmp_src_addr);
set_address(&tmp_dst_addr, AT_IPv4, 4, &tmp_addr_dst);
copy_address(&id->dst_addr, &tmp_dst_addr);
ret = true;
fail:
g_strfreev(arr);
return ret;
}
static bool
sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata _U_)
{
stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
json_dumper_begin_object(&dumper);
sharkd_json_value_string("name", stat_tap->title);
sharkd_json_value_stringf("tap", "nstat:%s", (const char *) key);
json_dumper_end_object(&dumper);
return false;
}
static bool
sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata _U_)
{
struct register_ct *table = (struct register_ct *) value;
const char *label = (const char *) key;
if (get_conversation_packet_func(table))
{
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Conversation List/%s", label);
sharkd_json_value_stringf("tap", "conv:%s", label);
json_dumper_end_object(&dumper);
}
if (get_endpoint_packet_func(table))
{
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Endpoint/%s", label);
sharkd_json_value_stringf("tap", "endpt:%s", label);
json_dumper_end_object(&dumper);
}
return false;
}
static bool
sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata _U_)
{
register_analysis_t *analysis = (register_analysis_t *) value;
json_dumper_begin_object(&dumper);
sharkd_json_value_string("name", sequence_analysis_get_ui_name(analysis));
sharkd_json_value_stringf("tap", "seqa:%s", (const char *) key);
json_dumper_end_object(&dumper);
return false;
}
static bool
sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data _U_)
{
register_eo_t *eo = (register_eo_t *) value;
const int proto_id = get_eo_proto_id(eo);
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Export Object/%s", label);
sharkd_json_value_stringf("tap", "eo:%s", filter);
json_dumper_end_object(&dumper);
return false;
}
static bool
sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data _U_)
{
register_srt_t *srt = (register_srt_t *) value;
const int proto_id = get_srt_proto_id(srt);
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Service Response Time/%s", label);
sharkd_json_value_stringf("tap", "srt:%s", filter);
json_dumper_end_object(&dumper);
return false;
}
static bool
sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data _U_)
{
register_rtd_t *rtd = (register_rtd_t *) value;
const int proto_id = get_rtd_proto_id(rtd);
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Response Time Delay/%s", label);
sharkd_json_value_stringf("tap", "rtd:%s", filter);
json_dumper_end_object(&dumper);
return false;
}
static bool
sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data _U_)
{
register_follow_t *follower = (register_follow_t *) value;
const int proto_id = get_follow_proto_id(follower);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
json_dumper_begin_object(&dumper);
sharkd_json_value_stringf("name", "Follow/%s", label);
sharkd_json_value_stringf("tap", "follow:%s", filter);
json_dumper_end_object(&dumper);
return false;
}
static void
sharkd_session_print_capture_types(void)
{
unsigned i;
GArray *writable_type_subtypes;
writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
for (i = 0; i < writable_type_subtypes->len; i++) {
int ft = g_array_index(writable_type_subtypes, int, i);
sharkd_json_object_open(NULL);
sharkd_json_value_string("name", wtap_file_type_subtype_name(ft));
sharkd_json_value_string("description", wtap_file_type_subtype_description(ft));
sharkd_json_object_close();
}
g_array_free(writable_type_subtypes, TRUE);
}
struct encap_type_info
{
const char *name;
const char *description;
};
static int
encap_type_info_nat_compare(const void *a, const void *b)
{
return ws_ascii_strnatcmp(((const struct encap_type_info *)a)->name,
((const struct encap_type_info *)b)->name);
}
static void
encap_type_info_visit(void *data, void *user_data _U_)
{
sharkd_json_object_open(NULL);
sharkd_json_value_string("name", ((struct encap_type_info *)data)->name);
sharkd_json_value_string("description", ((struct encap_type_info *)data)->description);
sharkd_json_object_close();
}
static void
sharkd_session_print_encap_types(void)
{
int i;
struct encap_type_info *encaps;
GSList *list = NULL;
encaps = g_new(struct encap_type_info, WTAP_NUM_ENCAP_TYPES);
for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
encaps[i].name = wtap_encap_name(i);
if (encaps[i].name != NULL) {
encaps[i].description = wtap_encap_description(i);
list = g_slist_insert_sorted(list, &encaps[i], encap_type_info_nat_compare);
}
}
g_slist_foreach(list, encap_type_info_visit, NULL);
g_slist_free(list);
g_free(encaps);
}
/**
* sharkd_session_process_info()
*
* Process info request
*
* Output object with attributes:
* (m) version - version number
*
* (m) columns - available column formats, array of object with attributes:
* 'name' - column name
* 'format' - column format-name
*
* (m) stats - available statistics, array of object with attributes:
* 'name' - statistic name
* 'tap' - sharkd tap-name for statistic
*