forked from draios/sysdig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.cpp
2172 lines (1951 loc) · 46.6 KB
/
filter.cpp
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
/*
Copyright (C) 2013-2014 Draios inc.
This file is part of sysdig.
sysdig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
sysdig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sysdig. If not, see <http://www.gnu.org/licenses/>.
*/
//
// Why isn't this parser written using antlr or some other parser generator?
// Essentially, after dealing with that stuff multiple times in the past, and fighting for a day
// to configure everything with crappy documentation and code that doesn't compile,
// I decided that I agree with this http://mortoray.com/2012/07/20/why-i-dont-use-a-parser-generator/
// and that I'm going with a manually written parser. The grammar is simple enough that it's not
// going to take more time. On the other hand I will avoid a crappy dependency that breaks my
// code at every new release, and I will have a cleaner and easier to understand code base.
//
#ifdef _WIN32
#define NOMINMAX
#endif
#include <regex>
#include "sinsp.h"
#include "sinsp_int.h"
#include "utils.h"
#ifdef HAS_FILTERING
#include "filter.h"
#include "filterchecks.h"
#include "value_parser.h"
#ifndef _WIN32
#include "arpa/inet.h"
#endif
#ifndef _GNU_SOURCE
//
// Fallback implementation of memmem
//
void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
#endif
#ifdef _WIN32
#pragma comment(lib, "Ws2_32.lib")
#include <WinSock2.h>
#else
#include <netdb.h>
#endif
extern sinsp_filter_check_list g_filterlist;
///////////////////////////////////////////////////////////////////////////////
// sinsp_filter_check_list implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_filter_check_list::sinsp_filter_check_list()
{
//////////////////////////////////////////////////////////////////////////////
// ADD NEW FILTER CHECK CLASSES HERE
//////////////////////////////////////////////////////////////////////////////
add_filter_check(new sinsp_filter_check_fd());
add_filter_check(new sinsp_filter_check_thread());
add_filter_check(new sinsp_filter_check_event());
add_filter_check(new sinsp_filter_check_user());
add_filter_check(new sinsp_filter_check_group());
add_filter_check(new sinsp_filter_check_syslog());
add_filter_check(new sinsp_filter_check_container());
add_filter_check(new sinsp_filter_check_utils());
add_filter_check(new sinsp_filter_check_fdlist());
#ifndef HAS_ANALYZER
add_filter_check(new sinsp_filter_check_k8s());
#endif // HAS_ANALYZER
add_filter_check(new sinsp_filter_check_mesos());
add_filter_check(new sinsp_filter_check_tracer());
add_filter_check(new sinsp_filter_check_evtin());
}
sinsp_filter_check_list::~sinsp_filter_check_list()
{
uint32_t j;
for(j = 0; j < m_check_list.size(); j++)
{
delete m_check_list[j];
}
}
void sinsp_filter_check_list::add_filter_check(sinsp_filter_check* filter_check)
{
m_check_list.push_back(filter_check);
}
void sinsp_filter_check_list::get_all_fields(OUT vector<const filter_check_info*>* list)
{
uint32_t j;
for(j = 0; j < m_check_list.size(); j++)
{
list->push_back((const filter_check_info*)&(m_check_list[j]->m_info));
}
}
sinsp_filter_check* sinsp_filter_check_list::new_filter_check_from_fldname(const string& name,
sinsp* inspector,
bool do_exact_check)
{
uint32_t j;
for(j = 0; j < m_check_list.size(); j++)
{
m_check_list[j]->m_inspector = inspector;
int32_t fldnamelen = m_check_list[j]->parse_field_name(name.c_str(), false, true);
if(fldnamelen != -1)
{
if(do_exact_check)
{
if((int32_t)name.size() != fldnamelen)
{
goto field_not_found;
}
}
sinsp_filter_check* newchk = m_check_list[j]->allocate_new();
newchk->set_inspector(inspector);
return newchk;
}
}
field_not_found:
//
// If you are implementing a new filter check and this point is reached,
// it's very likely that you've forgotten to add your filter to the list in
// the constructor
//
return NULL;
}
sinsp_filter_check* sinsp_filter_check_list::new_filter_check_from_another(sinsp_filter_check *chk)
{
sinsp_filter_check *newchk = chk->allocate_new();
newchk->m_inspector = chk->m_inspector;
newchk->m_field_id = chk->m_field_id;
newchk->m_field = &chk->m_info.m_fields[chk->m_field_id];
newchk->m_boolop = chk->m_boolop;
newchk->m_cmpop = chk->m_cmpop;
return newchk;
}
///////////////////////////////////////////////////////////////////////////////
// type-based comparison functions
///////////////////////////////////////////////////////////////////////////////
bool flt_compare_uint64(cmpop op, uint64_t operand1, uint64_t operand2)
{
switch(op)
{
case CO_EQ:
return (operand1 == operand2);
case CO_NE:
return (operand1 != operand2);
case CO_LT:
return (operand1 < operand2);
case CO_LE:
return (operand1 <= operand2);
case CO_GT:
return (operand1 > operand2);
case CO_GE:
return (operand1 >= operand2);
case CO_CONTAINS:
throw sinsp_exception("'contains' not supported for numeric filters");
return false;
case CO_ICONTAINS:
throw sinsp_exception("'icontains' not supported for numeric filters");
return false;
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
}
}
bool flt_compare_int64(cmpop op, int64_t operand1, int64_t operand2)
{
switch(op)
{
case CO_EQ:
return (operand1 == operand2);
case CO_NE:
return (operand1 != operand2);
case CO_LT:
return (operand1 < operand2);
case CO_LE:
return (operand1 <= operand2);
case CO_GT:
return (operand1 > operand2);
case CO_GE:
return (operand1 >= operand2);
case CO_CONTAINS:
throw sinsp_exception("'contains' not supported for numeric filters");
return false;
case CO_ICONTAINS:
throw sinsp_exception("'icontains' not supported for numeric filters");
return false;
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
}
}
bool flt_compare_string(cmpop op, char* operand1, char* operand2)
{
switch(op)
{
case CO_EQ:
return (strcmp(operand1, operand2) == 0);
case CO_NE:
return (strcmp(operand1, operand2) != 0);
case CO_CONTAINS:
return (strstr(operand1, operand2) != NULL);
case CO_ICONTAINS:
#ifdef _WIN32
return (_strnicmp(operand1, operand2, strlen(operand1)) != NULL);
#else
return (strcasestr(operand1, operand2) != NULL);
#endif
case CO_STARTSWITH:
return (strncmp(operand1, operand2, strlen(operand2)) == 0);
case CO_GLOB:
return sinsp_utils::glob_match(operand2, operand1);
case CO_LT:
return (strcmp(operand1, operand2) < 0);
case CO_LE:
return (strcmp(operand1, operand2) <= 0);
case CO_GT:
return (strcmp(operand1, operand2) > 0);
case CO_GE:
return (strcmp(operand1, operand2) >= 0);
default:
ASSERT(false);
throw sinsp_exception("invalid filter operator " + std::to_string((long long) op));
return false;
}
}
bool flt_compare_buffer(cmpop op, char* operand1, char* operand2, uint32_t op1_len, uint32_t op2_len)
{
switch(op)
{
case CO_EQ:
return op1_len == op2_len && (memcmp(operand1, operand2, op1_len) == 0);
case CO_NE:
return op1_len != op2_len || (memcmp(operand1, operand2, op1_len) != 0);
case CO_CONTAINS:
return (memmem(operand1, op1_len, operand2, op2_len) != NULL);
case CO_ICONTAINS:
throw sinsp_exception("'icontains' not supported for buffer filters");
case CO_STARTSWITH:
return (memcmp(operand1, operand2, op2_len) == 0);
case CO_GLOB:
throw sinsp_exception("'glob' not supported for buffer filters");
case CO_LT:
throw sinsp_exception("'<' not supported for buffer filters");
case CO_LE:
throw sinsp_exception("'<=' not supported for buffer filters");
case CO_GT:
throw sinsp_exception("'>' not supported for buffer filters");
case CO_GE:
throw sinsp_exception("'>=' not supported for buffer filters");
default:
ASSERT(false);
throw sinsp_exception("invalid filter operator " + std::to_string((long long) op));
return false;
}
}
bool flt_compare_double(cmpop op, double operand1, double operand2)
{
switch(op)
{
case CO_EQ:
return (operand1 == operand2);
case CO_NE:
return (operand1 != operand2);
case CO_LT:
return (operand1 < operand2);
case CO_LE:
return (operand1 <= operand2);
case CO_GT:
return (operand1 > operand2);
case CO_GE:
return (operand1 >= operand2);
case CO_CONTAINS:
throw sinsp_exception("'contains' not supported for numeric filters");
return false;
case CO_ICONTAINS:
throw sinsp_exception("'icontains' not supported for numeric filters");
return false;
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("'unknown' not supported for numeric filters");
return false;
}
}
bool flt_compare_ipv4net(cmpop op, uint64_t operand1, ipv4net* operand2)
{
switch(op)
{
case CO_EQ:
{
return ((operand1 & operand2->m_netmask) == (operand2->m_ip & operand2->m_netmask));
}
case CO_NE:
return ((operand1 & operand2->m_netmask) != (operand2->m_ip && operand2->m_netmask));
case CO_CONTAINS:
throw sinsp_exception("'contains' not supported for numeric filters");
return false;
case CO_ICONTAINS:
throw sinsp_exception("'icontains' not supported for numeric filters");
return false;
case CO_STARTSWITH:
throw sinsp_exception("'startswith' not supported for numeric filters");
return false;
case CO_GLOB:
throw sinsp_exception("'glob' not supported for numeric filters");
return false;
default:
throw sinsp_exception("comparison operator not supported for ipv4 networks");
}
}
bool flt_compare(cmpop op, ppm_param_type type, void* operand1, void* operand2, uint32_t op1_len, uint32_t op2_len)
{
//
// sinsp_filter_check_*::compare
// already discard NULL values
//
if(op == CO_EXISTS)
{
return true;
}
switch(type)
{
case PT_INT8:
return flt_compare_int64(op, (int64_t)*(int8_t*)operand1, (int64_t)*(int8_t*)operand2);
case PT_INT16:
return flt_compare_int64(op, (int64_t)*(int16_t*)operand1, (int64_t)*(int16_t*)operand2);
case PT_INT32:
return flt_compare_int64(op, (int64_t)*(int32_t*)operand1, (int64_t)*(int32_t*)operand2);
case PT_INT64:
case PT_FD:
case PT_PID:
case PT_ERRNO:
return flt_compare_int64(op, *(int64_t*)operand1, *(int64_t*)operand2);
case PT_FLAGS8:
case PT_UINT8:
case PT_SIGTYPE:
return flt_compare_uint64(op, (uint64_t)*(uint8_t*)operand1, (uint64_t)*(uint8_t*)operand2);
case PT_FLAGS16:
case PT_UINT16:
case PT_PORT:
case PT_SYSCALLID:
return flt_compare_uint64(op, (uint64_t)*(uint16_t*)operand1, (uint64_t)*(uint16_t*)operand2);
case PT_UINT32:
case PT_FLAGS32:
case PT_BOOL:
case PT_IPV4ADDR:
return flt_compare_uint64(op, (uint64_t)*(uint32_t*)operand1, (uint64_t)*(uint32_t*)operand2);
case PT_IPV4NET:
return flt_compare_ipv4net(op, (uint64_t)*(uint32_t*)operand1, (ipv4net*)operand2);
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
return flt_compare_uint64(op, *(uint64_t*)operand1, *(uint64_t*)operand2);
case PT_CHARBUF:
return flt_compare_string(op, (char*)operand1, (char*)operand2);
case PT_BYTEBUF:
return flt_compare_buffer(op, (char*)operand1, (char*)operand2, op1_len, op2_len);
case PT_DOUBLE:
return flt_compare_double(op, *(double*)operand1, *(double*)operand2);
case PT_SOCKADDR:
case PT_SOCKTUPLE:
case PT_FDLIST:
case PT_FSPATH:
case PT_SIGSET:
default:
ASSERT(false);
return false;
}
}
bool flt_compare_avg(cmpop op,
ppm_param_type type,
void* operand1,
void* operand2,
uint32_t op1_len,
uint32_t op2_len,
uint32_t cnt1,
uint32_t cnt2)
{
int64_t i641, i642;
uint64_t u641, u642;
double d1, d2;
//
// If count = 0 we assume that the value is zero too (there are assertions to
// check that, and we just divide by 1
//
if(cnt1 == 0)
{
cnt1 = 1;
}
if(cnt2 == 0)
{
cnt2 = 1;
}
switch(type)
{
case PT_INT8:
i641 = ((int64_t)*(int8_t*)operand1) / cnt1;
i642 = ((int64_t)*(int8_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || i641 == 0);
ASSERT(cnt2 != 0 || i642 == 0);
return flt_compare_int64(op, i641, i642);
case PT_INT16:
i641 = ((int64_t)*(int16_t*)operand1) / cnt1;
i642 = ((int64_t)*(int16_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || i641 == 0);
ASSERT(cnt2 != 0 || i642 == 0);
return flt_compare_int64(op, i641, i642);
case PT_INT32:
i641 = ((int64_t)*(int32_t*)operand1) / cnt1;
i642 = ((int64_t)*(int32_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || i641 == 0);
ASSERT(cnt2 != 0 || i642 == 0);
return flt_compare_int64(op, i641, i642);
case PT_INT64:
case PT_FD:
case PT_PID:
case PT_ERRNO:
i641 = ((int64_t)*(int64_t*)operand1) / cnt1;
i642 = ((int64_t)*(int64_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || i641 == 0);
ASSERT(cnt2 != 0 || i642 == 0);
return flt_compare_int64(op, i641, i642);
case PT_FLAGS8:
case PT_UINT8:
case PT_SIGTYPE:
u641 = ((uint64_t)*(uint8_t*)operand1) / cnt1;
u642 = ((uint64_t)*(uint8_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || u641 == 0);
ASSERT(cnt2 != 0 || u642 == 0);
return flt_compare_uint64(op, u641, u642);
case PT_FLAGS16:
case PT_UINT16:
case PT_PORT:
case PT_SYSCALLID:
u641 = ((uint64_t)*(uint16_t*)operand1) / cnt1;
u642 = ((uint64_t)*(uint16_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || u641 == 0);
ASSERT(cnt2 != 0 || u642 == 0);
return flt_compare_uint64(op, u641, u642);
case PT_UINT32:
case PT_FLAGS32:
case PT_BOOL:
case PT_IPV4ADDR:
u641 = ((uint64_t)*(uint32_t*)operand1) / cnt1;
u642 = ((uint64_t)*(uint32_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || u641 == 0);
ASSERT(cnt2 != 0 || u642 == 0);
return flt_compare_uint64(op, u641, u642);
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
u641 = (*(uint64_t*)operand1) / cnt1;
u642 = (*(uint64_t*)operand2) / cnt2;
ASSERT(cnt1 != 0 || u641 == 0);
ASSERT(cnt2 != 0 || u642 == 0);
return flt_compare_uint64(op, u641, u642);
case PT_DOUBLE:
d1 = (*(double*)operand1) / cnt1;
d2 = (*(double*)operand2) / cnt2;
ASSERT(cnt1 != 0 || d1 == 0);
ASSERT(cnt2 != 0 || d2 == 0);
return flt_compare_double(op, d1, d2);
default:
ASSERT(false);
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_filter_check implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_filter_check::sinsp_filter_check()
{
m_boolop = BO_NONE;
m_cmpop = CO_NONE;
m_inspector = NULL;
m_field = NULL;
m_info.m_fields = NULL;
m_info.m_nfields = -1;
m_val_storage_len = 0;
m_aggregation = A_NONE;
m_merge_aggregation = A_NONE;
m_val_storages = vector<vector<uint8_t>> (1, vector<uint8_t>(256));
m_val_storages_min_size = (numeric_limits<uint32_t>::max)();
m_val_storages_max_size = (numeric_limits<uint32_t>::min)();
}
void sinsp_filter_check::set_inspector(sinsp* inspector)
{
m_inspector = inspector;
}
Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval, const filtercheck_field_info* finfo, uint32_t len)
{
ASSERT(rawval != NULL);
ASSERT(finfo != NULL);
switch(finfo->m_type)
{
case PT_INT8:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(int8_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_INT16:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(int16_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_INT32:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(int32_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_INT64:
case PT_PID:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return (Json::Value::Int64)*(int64_t *)rawval;
}
else
{
return rawval_to_string(rawval, finfo, len);
}
case PT_L4PROTO: // This can be resolved in the future
case PT_UINT8:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(uint8_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_PORT: // This can be resolved in the future
case PT_UINT16:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(uint16_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_UINT32:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return *(uint32_t *)rawval;
}
else if(finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
return (Json::Value::UInt64)*(uint64_t *)rawval;
}
else if(
finfo->m_print_format == PF_10_PADDED_DEC ||
finfo->m_print_format == PF_OCT ||
finfo->m_print_format == PF_HEX)
{
return rawval_to_string(rawval, finfo, len);
}
else
{
ASSERT(false);
return Json::nullValue;
}
case PT_SOCKADDR:
case PT_SOCKFAMILY:
ASSERT(false);
return Json::nullValue;
case PT_BOOL:
return Json::Value((bool)(*(uint32_t*)rawval != 0));
case PT_CHARBUF:
case PT_FSPATH:
case PT_BYTEBUF:
case PT_IPV4ADDR:
return rawval_to_string(rawval, finfo, len);
default:
ASSERT(false);
throw sinsp_exception("wrong event type " + to_string((long long) finfo->m_type));
}
}
char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, const filtercheck_field_info* finfo, uint32_t len)
{
char* prfmt;
ASSERT(rawval != NULL);
ASSERT(finfo != NULL);
switch(finfo->m_type)
{
case PT_INT8:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo8;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRId8;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIX8;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(int8_t *)rawval);
return m_getpropertystr_storage;
case PT_INT16:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo16;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRId16;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIX16;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(int16_t *)rawval);
return m_getpropertystr_storage;
case PT_INT32:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo32;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRId32;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIX32;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(int32_t *)rawval);
return m_getpropertystr_storage;
case PT_INT64:
case PT_PID:
case PT_ERRNO:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo64;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRId64;
}
else if(finfo->m_print_format == PF_10_PADDED_DEC)
{
prfmt = (char*)"%09" PRId64;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIX64;
}
else
{
prfmt = (char*)"%" PRId64;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(int64_t *)rawval);
return m_getpropertystr_storage;
case PT_L4PROTO: // This can be resolved in the future
case PT_UINT8:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo8;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRIu8;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIu8;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(uint8_t *)rawval);
return m_getpropertystr_storage;
case PT_PORT: // This can be resolved in the future
case PT_UINT16:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo16;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRIu16;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIu16;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(uint16_t *)rawval);
return m_getpropertystr_storage;
case PT_UINT32:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo32;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRIu32;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIu32;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(uint32_t *)rawval);
return m_getpropertystr_storage;
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
if(finfo->m_print_format == PF_OCT)
{
prfmt = (char*)"%" PRIo64;
}
else if(finfo->m_print_format == PF_DEC ||
finfo->m_print_format == PF_ID)
{
prfmt = (char*)"%" PRIu64;
}
else if(finfo->m_print_format == PF_10_PADDED_DEC)
{
prfmt = (char*)"%09" PRIu64;
}
else if(finfo->m_print_format == PF_HEX)
{
prfmt = (char*)"%" PRIX64;
}
else
{
ASSERT(false);
return NULL;
}
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
prfmt, *(uint64_t *)rawval);
return m_getpropertystr_storage;
case PT_CHARBUF:
case PT_FSPATH:
return (char*)rawval;
case PT_BYTEBUF:
if(rawval[len] == 0)
{
return (char*)rawval;
}
else
{
ASSERT(len < 1024 * 1024);
if(len >= filter_value().size())
{
filter_value().resize(len + 1);
}
memcpy(filter_value_p(), rawval, len);
filter_value_p()[len] = 0;
return (char*)filter_value_p();
}
case PT_SOCKADDR:
ASSERT(false);
return NULL;
case PT_SOCKFAMILY:
ASSERT(false);
return NULL;
case PT_BOOL:
if(*(uint32_t*)rawval != 0)
{
return (char*)"true";
}
else
{
return (char*)"false";
}
case PT_IPV4ADDR:
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
"%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8,
rawval[0],
rawval[1],
rawval[2],
rawval[3]);
return m_getpropertystr_storage;
case PT_DOUBLE:
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
"%.1lf", *(double*)rawval);
return m_getpropertystr_storage;
default:
ASSERT(false);
throw sinsp_exception("wrong event type " + to_string((long long) finfo->m_type));
}
}
char* sinsp_filter_check::tostring(sinsp_evt* evt)
{
uint32_t len;
uint8_t* rawval = extract(evt, &len);
if(rawval == NULL)
{
return NULL;
}
return rawval_to_string(rawval, m_field, len);
}
Json::Value sinsp_filter_check::tojson(sinsp_evt* evt)
{
uint32_t len;