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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
|
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <unistd.h>
extern void nDPIsrvd_memprof_log(char const * const format, ...);
extern void nDPIsrvd_memprof_log_alloc(size_t alloc_size);
extern void nDPIsrvd_memprof_log_free(size_t free_size);
// #define VERBOSE_MEMORY_PROFILING 1
#define NO_MAIN 1
#include "utils.c"
#include "nio.c"
#include "nDPIsrvd.c"
#include "nDPId.c"
enum
{
PIPE_nDPId = 1, /* nDPId mock pipefd array index */
PIPE_nDPIsrvd = 0, /* nDPIsrvd mock pipefd array index */
PIPE_TEST_WRITE = 1, /* Distributor (data from nDPIsrvd) write */
PIPE_TEST_READ = 0, /* Distributor (do some validation tests) read */
PIPE_BUFFER_WRITE = 1, /* Distributor (data from nDPIsrvd, buffered json lines) write */
PIPE_BUFFER_READ = 0, /* Distributor (do some validation tests, buffered json lines) read */
PIPE_NULL_WRITE = 1, /* Distributor (data from nDPIsrvd) write */
PIPE_NULL_READ = 0, /* Distributor (print to stdout) read */
PIPE_ARPA_WRITE = 1, /* Distributor (data from nDPIsrvd) write */
PIPE_ARPA_READ = 0, /* Distributor (IP mockup) read */
PIPE_FDS = 2,
MAX_REMOTE_DESCRIPTORS = 5 /* mock pipefd's + 2 * distributor pipefd's */
};
struct thread_return_value
{
int val;
};
struct nDPId_return_value
{
struct thread_return_value thread_return_value;
size_t thread_index;
unsigned long long int packets_captured;
unsigned long long int packets_processed;
unsigned long long int total_skipped_flows;
unsigned long long int total_l4_payload_len;
unsigned long long int not_detected_flow_protocols;
unsigned long long int guessed_flow_protocols;
unsigned long long int detected_flow_protocols;
unsigned long long int flow_detection_updates;
unsigned long long int flow_updates;
unsigned long long int total_active_flows;
unsigned long long int total_idle_flows;
unsigned long long int cur_active_flows;
unsigned long long int cur_idle_flows;
#ifdef ENABLE_ZLIB
unsigned long long int total_compressions;
unsigned long long int total_compression_diff;
unsigned long long int current_compression_diff;
#endif
unsigned long long int total_events_serialized;
};
struct distributor_instance_user_data
{
unsigned long long int flow_cleanup_count;
unsigned long long int daemon_event_count;
};
struct distributor_thread_user_data
{
unsigned long long int flow_new_count;
unsigned long long int flow_end_count;
unsigned long long int flow_idle_count;
unsigned long long int daemon_event_count;
};
struct distributor_global_user_data
{
unsigned long long int total_packets_processed;
unsigned long long int total_l4_payload_len;
unsigned long long int total_events_deserialized;
unsigned long long int total_events_serialized;
unsigned long long int total_flow_timeouts;
unsigned long long int flow_new_count;
unsigned long long int flow_end_count;
unsigned long long int flow_idle_count;
unsigned long long int flow_detected_count;
unsigned long long int flow_guessed_count;
unsigned long long int flow_not_detected_count;
unsigned long long int flow_detection_update_count;
unsigned long long int flow_update_count;
unsigned long long int shutdown_events;
unsigned long long int json_message_len_min;
unsigned long long int json_message_len_max;
double json_message_len_avg;
unsigned long long int cur_active_flows;
unsigned long long int cur_idle_flows;
struct distributor_instance_user_data instance_user_data;
struct distributor_thread_user_data thread_user_data;
int flow_cleanup_error;
// please keep this struct at the end
struct
{
int do_hash_checks;
} options;
} __attribute__((__packed__));
struct distributor_flow_user_data
{
unsigned long long int total_packets_processed;
unsigned long long int flow_total_l4_data_len;
uint8_t is_flow_timedout;
};
struct distributor_return_value
{
struct thread_return_value thread_return_value;
struct distributor_global_user_data stats;
};
#define TC_INIT(initial, wanted) \
{ \
.mutex = PTHREAD_MUTEX_INITIALIZER, .condition = PTHREAD_COND_INITIALIZER, .value = initial, \
.wanted_value = wanted \
}
struct thread_condition
{
pthread_mutex_t mutex;
pthread_cond_t condition;
int value;
int wanted_value;
};
static int mock_pipefds[PIPE_FDS] = {};
static int mock_testfds[PIPE_FDS] = {};
static int mock_bufffds[PIPE_FDS] = {};
static int mock_nullfds[PIPE_FDS] = {};
static int mock_arpafds[PIPE_FDS] = {};
static struct thread_condition start_condition = TC_INIT(3, 0);
#ifdef VERBOSE_MEMORY_PROFILING
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static pthread_mutex_t mem_mutex = PTHREAD_MUTEX_INITIALIZER; // required; memory wrappers are used from two threads
// (distributor and nDPIsrvd)
static unsigned long long int nDPIsrvd_alloc_count = 0;
static unsigned long long int nDPIsrvd_alloc_bytes = 0;
static unsigned long long int nDPIsrvd_free_count = 0;
static unsigned long long int nDPIsrvd_free_bytes = 0;
#define THREAD_ERROR(thread_arg) \
do \
{ \
((struct thread_return_value *)thread_arg)->val = (errno != 0 ? errno : 1); \
} while (0)
#define THREAD_ERROR_GOTO(thread_arg) \
do \
{ \
THREAD_ERROR(thread_arg); \
goto error; \
} while (0)
void nDPIsrvd_memprof_log(char const * const format, ...)
{
#ifdef VERBOSE_MEMORY_PROFILING
int logbuf_used, logbuf_used_tmp;
char logbuf[BUFSIZ];
va_list ap;
va_start(ap, format);
pthread_mutex_lock(&log_mutex);
logbuf_used = snprintf(logbuf, sizeof(logbuf), "%s", "nDPIsrvd MemoryProfiler: ");
if (logbuf_used > 0)
{
logbuf_used_tmp = vsnprintf(logbuf + logbuf_used, sizeof(logbuf) - logbuf_used, format, ap);
if (logbuf_used_tmp > 0)
{
logbuf_used += logbuf_used_tmp;
}
}
fprintf(stderr, "%s\n", logbuf);
pthread_mutex_unlock(&log_mutex);
va_end(ap);
#else
(void)format;
#endif
}
void nDPIsrvd_memprof_log_alloc(size_t alloc_size)
{
unsigned long alloc_count;
// nDPIsrvd.h is used by client applications and nDPIsrvd (two threads!)
pthread_mutex_lock(&mem_mutex);
nDPIsrvd_alloc_count++;
nDPIsrvd_alloc_bytes += alloc_size;
alloc_count = nDPIsrvd_alloc_count;
pthread_mutex_unlock(&mem_mutex);
nDPIsrvd_memprof_log("nDPIsrvd.h: malloc #%llu, %llu bytes", alloc_count, alloc_size);
}
void nDPIsrvd_memprof_log_free(size_t free_size)
{
unsigned long free_count;
// nDPIsrvd.h is used by client applications and nDPIsrvd (two threads!)
pthread_mutex_lock(&mem_mutex);
nDPIsrvd_free_count++;
nDPIsrvd_free_bytes += free_size;
free_count = nDPIsrvd_free_count;
pthread_mutex_unlock(&mem_mutex);
nDPIsrvd_memprof_log("nDPIsrvd.h: free #%llu, %llu bytes", free_count, free_size);
}
static int thread_wait(struct thread_condition * const tc)
{
int ret = 0;
ret |= (pthread_mutex_lock(&tc->mutex) << 16);
while (tc->value > tc->wanted_value)
{
ret |= (pthread_cond_wait(&tc->condition, &tc->mutex) << 8);
}
ret |= (pthread_mutex_unlock(&tc->mutex));
return ret;
}
static int thread_block_signals()
{
sigset_t blocked_signals;
sigfillset(&blocked_signals);
return pthread_sigmask(SIG_BLOCK, &blocked_signals, NULL);
}
static int thread_signal(struct thread_condition * const tc)
{
int ret = 0;
ret |= (pthread_mutex_lock(&tc->mutex) << 16);
if (tc->value > tc->wanted_value)
{
tc->value--;
}
if (tc->value == tc->wanted_value)
{
ret |= (pthread_cond_broadcast(&tc->condition) << 8);
}
ret |= (pthread_mutex_unlock(&tc->mutex));
return ret;
}
static int setup_pipe(int pipefd[PIPE_FDS])
{
if (pipe(pipefd) != 0)
{
return -1;
}
if (fcntl_add_flags(pipefd[0], O_NONBLOCK) != 0)
{
return -1;
}
if (fcntl_add_flags(pipefd[1], O_NONBLOCK) != 0)
{
return -1;
}
return 0;
}
static void * nDPIsrvd_mainloop_thread(void * const arg)
{
int nDPIsrvd_distributor_disconnects = 0;
int const nDPIsrvd_distributor_expected_disconnects = 5;
struct nio io;
struct remote_desc * mock_json_desc = NULL;
struct remote_desc * mock_test_desc = NULL;
struct remote_desc * mock_buff_desc = NULL;
struct remote_desc * mock_null_desc = NULL;
struct remote_desc * mock_arpa_desc = NULL;
logger(0, "nDPIsrvd thread started, init..");
if (thread_block_signals() != 0)
{
logger(1, "nDPIsrvd block signals failed: %s", strerror(errno));
}
nio_init(&io);
#ifdef ENABLE_EPOLL
if (nio_use_epoll(&io, 32) != NIO_SUCCESS)
#else
if (nio_use_poll(&io, nDPIsrvd_MAX_REMOTE_DESCRIPTORS) != NIO_SUCCESS)
#endif
{
logger(1, "%s", "Error creating nDPIsrvd poll/epoll event I/O");
THREAD_ERROR_GOTO(arg);
}
mock_json_desc = get_remote_descriptor(COLLECTOR_UN, mock_pipefds[PIPE_nDPIsrvd], NETWORK_BUFFER_MAX_SIZE);
if (mock_json_desc == NULL)
{
logger(1, "%s", "nDPIsrvd could not acquire remote descriptor (Collector)");
THREAD_ERROR_GOTO(arg);
}
mock_test_desc = get_remote_descriptor(DISTRIBUTOR_UN, mock_testfds[PIPE_TEST_WRITE], NETWORK_BUFFER_MAX_SIZE / 4);
if (mock_test_desc == NULL)
{
logger(1, "%s", "nDPIsrvd could not acquire remote descriptor (TEST Distributor)");
THREAD_ERROR_GOTO(arg);
}
mock_buff_desc = get_remote_descriptor(DISTRIBUTOR_UN, mock_bufffds[PIPE_BUFFER_WRITE], 8);
if (mock_buff_desc == NULL)
{
logger(1, "%s", "nDPIsrvd could not acquire remote descriptor (BUFFER Distributor)");
THREAD_ERROR_GOTO(arg);
}
mock_null_desc = get_remote_descriptor(DISTRIBUTOR_UN, mock_nullfds[PIPE_NULL_WRITE], NETWORK_BUFFER_MAX_SIZE);
if (mock_null_desc == NULL)
{
logger(1, "%s", "nDPIsrvd could not acquire remote descriptor (NULL Distributor)");
THREAD_ERROR_GOTO(arg);
}
mock_arpa_desc = get_remote_descriptor(DISTRIBUTOR_IN, mock_arpafds[PIPE_ARPA_WRITE], NETWORK_BUFFER_MAX_SIZE / 8);
if (mock_arpa_desc == NULL)
{
logger(1, "%s", "nDPIsrvd could not acquire remote descriptor (ARPA Distributor)");
THREAD_ERROR_GOTO(arg);
}
strncpy(mock_arpa_desc->event_distributor_in.peer_addr,
"arpa-mockup",
sizeof(mock_arpa_desc->event_distributor_in.peer_addr));
mock_arpa_desc->event_distributor_in.peer.sin_port = 0;
errno = 0;
if (add_in_event(&io, mock_json_desc) != 0 || add_in_event(&io, mock_test_desc) != 0 ||
add_in_event(&io, mock_buff_desc) != 0 || add_in_event(&io, mock_null_desc) != 0 ||
add_in_event(&io, mock_arpa_desc) != 0)
{
logger(1, "%s", "nDPIsrvd add input event failed");
THREAD_ERROR_GOTO(arg);
}
logger(0, "nDPIsrvd thread init done");
thread_signal(&start_condition);
thread_wait(&start_condition);
while (nDPIsrvd_distributor_disconnects < nDPIsrvd_distributor_expected_disconnects)
{
if (nio_run(&io, -1) != NIO_SUCCESS)
{
logger(1, "nDPIsrvd event I/O returned: %s", strerror(errno));
THREAD_ERROR_GOTO(arg);
}
int nready = nio_get_nready(&io);
for (int i = 0; i < nready; i++)
{
struct remote_desc * remote = (struct remote_desc *)nio_get_ptr(&io, i);
if (remote != mock_json_desc && remote != mock_test_desc && remote != mock_buff_desc &&
remote != mock_null_desc && remote != mock_arpa_desc)
{
logger(1, "nDPIsrvd epoll returned unexpected event data: %p", remote);
THREAD_ERROR_GOTO(arg);
}
if (nio_has_error(&io, i) == NIO_SUCCESS)
{
char const * remote_desc_name;
if (remote == mock_json_desc)
{
remote_desc_name = "Mock JSON";
do
{
if (mock_test_desc->fd >= 0)
drain_write_buffers_blocking(mock_test_desc);
if (mock_buff_desc->fd >= 0)
drain_write_buffers_blocking(mock_buff_desc);
if (mock_null_desc->fd >= 0)
drain_write_buffers_blocking(mock_null_desc);
if (mock_arpa_desc->fd >= 0)
drain_write_buffers_blocking(mock_arpa_desc);
} while (handle_data_event(&io, i) == 0);
}
else if (remote == mock_test_desc)
{
remote_desc_name = "Mock Test";
}
else if (remote == mock_buff_desc)
{
remote_desc_name = "Mock Buffer";
}
else if (remote == mock_null_desc)
{
remote_desc_name = "Mock NULL";
}
else if (remote == mock_arpa_desc)
{
remote_desc_name = "Mock ARPA";
}
else
{
remote_desc_name = "UNKNOWN";
}
nDPIsrvd_distributor_disconnects++;
logger(1,
"nDPIsrvd distributor '%s' connection closed (%d/%d)",
remote_desc_name,
nDPIsrvd_distributor_disconnects,
nDPIsrvd_distributor_expected_disconnects);
free_remote(&io, remote);
}
else
{
if (handle_data_event(&io, i) != 0)
{
if (mock_arpa_desc == remote)
{
// arpa mock does not care about shutdown events
free_remote(&io, mock_arpa_desc);
nDPIsrvd_distributor_disconnects++;
logger(1,
"nDPIsrvd distributor '%s' connection closed (%d/%d)",
"Mock ARPA",
nDPIsrvd_distributor_disconnects,
nDPIsrvd_distributor_expected_disconnects);
continue;
}
logger(1, "%s", "nDPIsrvd data event handler failed");
THREAD_ERROR_GOTO(arg);
}
}
}
}
error:
free_remotes(&io);
nio_free(&io);
logger(0, "%s", "nDPIsrvd worker thread exits..");
return NULL;
}
static enum nDPIsrvd_callback_return update_flow_packets_processed(struct nDPIsrvd_socket * const sock,
struct distributor_flow_user_data * const flow_stats)
{
struct nDPIsrvd_json_token const * const flow_total_packets_processed[FD_COUNT] = {
TOKEN_GET_SZ(sock, "flow_src_packets_processed"), TOKEN_GET_SZ(sock, "flow_dst_packets_processed")};
if (sock == NULL)
{
return CALLBACK_ERROR;
}
if (flow_stats != NULL && sock->flow_user_data_size > 0)
{
flow_stats->total_packets_processed = 0;
}
for (int dir = 0; dir < FD_COUNT; ++dir)
{
if (flow_total_packets_processed[dir] != NULL)
{
nDPIsrvd_ull nmb = 0;
if (TOKEN_VALUE_TO_ULL(sock, flow_total_packets_processed[dir], &nmb) != CONVERSION_OK)
{
return CALLBACK_ERROR;
}
if (flow_stats != NULL)
{
if (sock->flow_user_data_size > 0)
{
flow_stats->total_packets_processed += nmb;
}
}
}
}
return CALLBACK_OK;
}
static enum nDPIsrvd_callback_return update_flow_l4_payload_len(struct nDPIsrvd_socket * const sock,
struct distributor_flow_user_data * const flow_stats)
{
struct nDPIsrvd_json_token const * const flow_total_l4_payload_len[FD_COUNT] = {
TOKEN_GET_SZ(sock, "flow_src_tot_l4_payload_len"), TOKEN_GET_SZ(sock, "flow_dst_tot_l4_payload_len")};
if (flow_stats != NULL && sock->flow_user_data_size > 0)
{
flow_stats->flow_total_l4_data_len = 0;
}
for (int dir = 0; dir < FD_COUNT; ++dir)
{
if (flow_total_l4_payload_len[dir] != NULL)
{
nDPIsrvd_ull nmb = 0;
if (TOKEN_VALUE_TO_ULL(sock, flow_total_l4_payload_len[dir], &nmb) != CONVERSION_OK)
{
return CALLBACK_ERROR;
}
if (sock->flow_user_data_size > 0 && flow_stats != NULL)
{
flow_stats->flow_total_l4_data_len += nmb;
}
}
}
return CALLBACK_OK;
}
static enum nDPIsrvd_callback_return distributor_json_callback(struct nDPIsrvd_socket * const sock,
struct nDPIsrvd_instance * const instance,
struct nDPIsrvd_thread_data * const thread_data,
struct nDPIsrvd_flow * const flow)
{
struct distributor_global_user_data * const global_stats =
(struct distributor_global_user_data *)sock->global_user_data;
struct distributor_instance_user_data * instance_stats =
(struct distributor_instance_user_data *)instance->instance_user_data;
struct distributor_thread_user_data * thread_stats = NULL;
struct distributor_flow_user_data * flow_stats = NULL;
#if 0
printf("Distributor: %.*s\n", (int)sock->buffer.json_message_length, sock->buffer.json_message);
#endif
if (thread_data != NULL)
{
thread_stats = (struct distributor_thread_user_data *)thread_data->thread_user_data;
}
if (flow != NULL)
{
flow_stats = (struct distributor_flow_user_data *)flow->flow_user_data;
}
if (sock->buffer.json_message_length < global_stats->json_message_len_min)
{
global_stats->json_message_len_min = sock->buffer.json_message_length;
}
if (sock->buffer.json_message_length > global_stats->json_message_len_max)
{
global_stats->json_message_len_max = sock->buffer.json_message_length;
}
global_stats->json_message_len_avg =
(global_stats->json_message_len_avg +
(global_stats->json_message_len_max + global_stats->json_message_len_min) / 2) /
2;
global_stats->total_events_deserialized++;
{
struct nDPIsrvd_json_token const * const daemon_event_name = TOKEN_GET_SZ(sock, "daemon_event_name");
if (daemon_event_name != NULL)
{
if (sock->instance_user_data_size > 0)
{
instance_stats->daemon_event_count++;
}
if (thread_stats != NULL && sock->thread_user_data_size > 0)
{
thread_stats->daemon_event_count++;
}
if (TOKEN_VALUE_EQUALS_SZ(sock, daemon_event_name, "shutdown") != 0)
{
struct nDPIsrvd_json_token const * const total_events_serialized =
TOKEN_GET_SZ(sock, "total-events-serialized");
if (total_events_serialized != NULL)
{
nDPIsrvd_ull nmb = 0;
if (TOKEN_VALUE_TO_ULL(sock, total_events_serialized, &nmb) != CONVERSION_OK)
{
goto callback_error;
}
global_stats->total_events_serialized = nmb;
}
logger(0, "%s", "Distributor received shutdown event..");
global_stats->shutdown_events++;
}
}
}
{
struct nDPIsrvd_json_token const * const packet_event_name = TOKEN_GET_SZ(sock, "packet_event_name");
if (packet_event_name != NULL)
{
struct nDPIsrvd_json_token const * const pkt = TOKEN_GET_SZ(sock, "pkt");
struct nDPIsrvd_json_token const * const packet_id = TOKEN_GET_SZ(sock, "packet_id");
if (pkt == NULL || packet_id == NULL)
{
logger(1, "%s", "Missing base64 packet data");
goto callback_error;
}
nDPIsrvd_ull pkt_id = 0ULL;
TOKEN_VALUE_TO_ULL(sock, packet_id, &pkt_id);
if (pkt_id == 0ULL)
{
logger(1, "%s", "Missing packet id");
goto callback_error;
}
nDPIsrvd_ull src_len = nDPIsrvd_get_token_size(sock, pkt);
char const * const encoded_base64_buf = nDPIsrvd_get_token_value(sock, pkt);
if (src_len == 0 || encoded_base64_buf == NULL)
{
logger(1, "Missing base64 packet data for packet id: %llu", pkt_id);
goto callback_error;
}
unsigned char out_buf[8192];
size_t out_len = sizeof(out_buf);
if (nDPIsrvd_base64decode(encoded_base64_buf, src_len, out_buf, &out_len) != 0 || out_len == 0)
{
logger(1, "Decoding base64 packet data failed for packet id: %llu", pkt_id);
logger(1, "Affected base64 packet data (%llu bytes): %.*s", src_len, (int)src_len, encoded_base64_buf);
goto callback_error;
}
char base64_data[nDPId_PACKETS_PLEN_MAX * 4];
size_t base64_data_len = sizeof(base64_data);
if (base64_encode(out_buf, out_len, base64_data, &base64_data_len) != 0)
{
logger(1, "Encoding previously decoded base64 packet data failed for packet id: %llu", pkt_id);
goto callback_error;
}
unsigned char test_buf[8192];
size_t test_len = sizeof(test_buf);
if (nDPIsrvd_base64decode(base64_data, base64_data_len, test_buf, &test_len) != 0 || test_len == 0)
{
logger(1, "Re-decoding base64 packet data failed for packet id: %llu", pkt_id);
goto callback_error;
}
if (out_len != test_len || memcmp(out_buf, test_buf, out_len) != 0)
{
logger(1,
"Re-decoded base64 packet data differs from data decoded from JSON for packet id: %llu",
pkt_id);
goto callback_error;
}
}
}
{
struct nDPIsrvd_json_token const * const flow_event_name = TOKEN_GET_SZ(sock, "flow_event_name");
if (flow_event_name != NULL)
{
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "new") != 0)
{
global_stats->cur_active_flows++;
global_stats->flow_new_count++;
if (thread_stats != NULL && sock->thread_user_data_size > 0)
{
thread_stats->flow_new_count++;
}
unsigned int hash_count = HASH_COUNT(instance->flow_table);
if (global_stats->options.do_hash_checks != 0 && hash_count != global_stats->cur_active_flows)
{
logger(1,
"Amount of flows in the flow table not equal to current active flows counter: %u != %llu",
hash_count,
global_stats->cur_active_flows);
goto callback_error;
}
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "end") != 0)
{
global_stats->cur_active_flows--;
global_stats->cur_idle_flows++;
global_stats->flow_end_count++;
if (thread_stats != NULL && sock->thread_user_data_size > 0)
{
thread_stats->flow_end_count++;
}
if (update_flow_packets_processed(sock, flow_stats) != CALLBACK_OK ||
update_flow_l4_payload_len(sock, flow_stats) != CALLBACK_OK)
{
goto callback_error;
}
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "idle") != 0)
{
global_stats->cur_active_flows--;
global_stats->cur_idle_flows++;
global_stats->flow_idle_count++;
if (thread_stats != NULL && sock->thread_user_data_size > 0)
{
thread_stats->flow_idle_count++;
}
if (update_flow_packets_processed(sock, flow_stats) != CALLBACK_OK ||
update_flow_l4_payload_len(sock, flow_stats) != CALLBACK_OK)
{
goto callback_error;
}
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "detected") != 0)
{
global_stats->flow_detected_count++;
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "guessed") != 0)
{
global_stats->flow_guessed_count++;
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "not-detected") != 0)
{
global_stats->flow_not_detected_count++;
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "detection-update") != 0)
{
global_stats->flow_detection_update_count++;
}
if (TOKEN_VALUE_EQUALS_SZ(sock, flow_event_name, "update") != 0)
{
global_stats->flow_update_count++;
}
struct nDPIsrvd_flow * current_flow;
struct nDPIsrvd_flow * ftmp;
size_t flow_count = 0;
HASH_ITER(hh, instance->flow_table, current_flow, ftmp)
{
flow_count++;
}
if (global_stats->options.do_hash_checks != 0 &&
flow_count != global_stats->cur_active_flows + global_stats->cur_idle_flows)
{
logger(1,
"Amount of flows in flow table not equal current active flows plus current idle flows: %llu != "
"%llu + %llu",
(unsigned long long int)flow_count,
global_stats->cur_active_flows,
global_stats->cur_idle_flows);
goto callback_error;
}
}
}
return CALLBACK_OK;
callback_error:
logger(1, "%s", "Distributor error..");
return CALLBACK_ERROR;
}
static void distributor_instance_cleanup_callback(struct nDPIsrvd_socket * const sock,
struct nDPIsrvd_instance * const instance,
enum nDPIsrvd_cleanup_reason reason)
{
struct distributor_global_user_data * const global_stats =
(struct distributor_global_user_data *)sock->global_user_data;
struct nDPIsrvd_thread_data * current_thread_data;
struct nDPIsrvd_thread_data * ttmp;
(void)reason;
if (sock->global_user_data_size == 0 || sock->thread_user_data_size == 0 || sock->instance_user_data_size == 0)
{
return;
}
HASH_ITER(hh, instance->thread_data_table, current_thread_data, ttmp)
{
struct distributor_thread_user_data * const tud =
(struct distributor_thread_user_data *)current_thread_data->thread_user_data;
global_stats->thread_user_data.daemon_event_count += tud->daemon_event_count;
global_stats->thread_user_data.flow_new_count += tud->flow_new_count;
global_stats->thread_user_data.flow_end_count += tud->flow_end_count;
global_stats->thread_user_data.flow_idle_count += tud->flow_idle_count;
}
global_stats->instance_user_data = *(struct distributor_instance_user_data *)instance->instance_user_data;
}
static void distributor_flow_cleanup_callback(struct nDPIsrvd_socket * const sock,
struct nDPIsrvd_instance * const instance,
struct nDPIsrvd_thread_data * const thread_data,
struct nDPIsrvd_flow * const flow,
enum nDPIsrvd_cleanup_reason reason)
{
struct distributor_global_user_data * const global_stats =
(struct distributor_global_user_data *)sock->global_user_data;
struct distributor_flow_user_data * const flow_stats = (struct distributor_flow_user_data *)flow->flow_user_data;
(void)thread_data;
if (sock->instance_user_data_size > 0)
{
((struct distributor_instance_user_data *)instance->instance_user_data)->flow_cleanup_count++;
}
switch (reason)
{
case CLEANUP_REASON_DAEMON_INIT:
case CLEANUP_REASON_DAEMON_SHUTDOWN:
/* If that happens, it is either a BUG or caused by other applications. */
logger(1, "Invalid flow cleanup reason: %s", nDPIsrvd_enum_to_string(reason));
global_stats->flow_cleanup_error = 1;
break;
case CLEANUP_REASON_FLOW_TIMEOUT:
/*
* Flow timeouts may happen. The cause is libpcap itself.
* Unfortunately, libpcap does not provide retrieving the file descriptor if reading packets from a file.
* Without a file descriptor select(), poll() or epoll() can not work.
* As result all timestamps may have huge gaps depending on the recorded pcap file.
* But those timestamps are necessary to make flow-updates work.
*/
global_stats->total_flow_timeouts++;
flow_stats->is_flow_timedout = 1;
break;
case CLEANUP_REASON_APP_SHUTDOWN:
case CLEANUP_REASON_FLOW_END:
case CLEANUP_REASON_FLOW_IDLE:
break;
case CLEANUP_REASON_LAST_ENUM_VALUE:
break;
}
unsigned hash_count = HASH_COUNT(instance->flow_table);
if (hash_count != global_stats->cur_active_flows + global_stats->cur_idle_flows)
{
logger(1,
"Flow count is not equal to current active flows plus current idle flows plus current timedout flows: "
"%u != %llu + %llu",
hash_count,
global_stats->cur_active_flows,
global_stats->cur_idle_flows);
global_stats->flow_cleanup_error = 1;
}
if (flow_stats->is_flow_timedout == 0)
{
global_stats->total_packets_processed += flow_stats->total_packets_processed;
global_stats->total_l4_payload_len += flow_stats->flow_total_l4_data_len;
global_stats->cur_idle_flows--;
}
}
static enum nDPIsrvd_callback_return distributor_json_mock_buff_callback(
struct nDPIsrvd_socket * const sock,
struct nDPIsrvd_instance * const instance,
struct nDPIsrvd_thread_data * const thread_data,
struct nDPIsrvd_flow * const flow)
{
return distributor_json_callback(sock, instance, thread_data, flow);
}
static enum nDPIsrvd_callback_return distributor_json_printer(struct nDPIsrvd_socket * const sock,
struct nDPIsrvd_instance * const instance,
struct nDPIsrvd_thread_data * const thread_data,
struct nDPIsrvd_flow * const flow)
{
(void)instance;
(void)thread_data;
(void)flow;
{
struct nDPIsrvd_json_token const * const daemon_event_name = TOKEN_GET_SZ(sock, "daemon_event_name");
if (daemon_event_name != NULL)
{
if (TOKEN_VALUE_EQUALS_SZ(sock, daemon_event_name, "shutdown") != 0)
{
logger(0, "%s", "Distributor received shutdown event..");
int * const mock_null_shutdown_events = (int *)sock->global_user_data;
(*mock_null_shutdown_events)++;
}
}
}
printf("%0" NETWORK_BUFFER_LENGTH_DIGITS_STR "llu%.*s",
sock->buffer.json_message_length - NETWORK_BUFFER_LENGTH_DIGITS,
nDPIsrvd_json_buffer_length(sock),
nDPIsrvd_json_buffer_string(sock));
return CALLBACK_OK;
}
static void * distributor_client_mainloop_thread(void * const arg)
{
struct nio io;
#if !defined(__FreeBSD__) && !defined(__APPLE__)
int signalfd = -1;
#endif
struct distributor_return_value * const drv = (struct distributor_return_value *)arg;
struct thread_return_value * const trv = &drv->thread_return_value;
struct nDPIsrvd_socket * mock_sock = nDPIsrvd_socket_init(sizeof(struct distributor_global_user_data),
sizeof(struct distributor_instance_user_data),
sizeof(struct distributor_thread_user_data),
sizeof(struct distributor_flow_user_data),
distributor_json_callback,
distributor_instance_cleanup_callback,
distributor_flow_cleanup_callback);
struct nDPIsrvd_socket * mock_buff = nDPIsrvd_socket_init(sizeof(struct distributor_global_user_data),
sizeof(struct distributor_instance_user_data),
sizeof(struct distributor_thread_user_data),
sizeof(struct distributor_flow_user_data),
distributor_json_mock_buff_callback,
distributor_instance_cleanup_callback,
distributor_flow_cleanup_callback);
struct nDPIsrvd_socket * mock_null =
nDPIsrvd_socket_init(sizeof(int), 0, 0, 0, distributor_json_printer, NULL, NULL);
struct distributor_global_user_data * sock_stats;
struct distributor_global_user_data * buff_stats;
int * mock_null_shutdown_events;
logger(0, "Distributor thread started, init..");
nio_init(&io);
#ifdef ENABLE_EPOLL
if (nio_use_epoll(&io, 32) != NIO_SUCCESS)
#else
if (nio_use_poll(&io, 32) != NIO_SUCCESS)
#endif
{
logger(1, "%s", "Error creating Distributor poll/epoll event I/O");
THREAD_ERROR_GOTO(trv);
}
#if !defined(__FreeBSD__) && !defined(__APPLE__)
signalfd = setup_signalfd(&io);
if (signalfd < 0)
{
logger(1, "Distributor signal fd setup failed: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
}
#endif
if (thread_block_signals() != 0)
{
logger(1, "Distributor block signals failed: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
}
errno = 0;
if (mock_sock == NULL || mock_buff == NULL || mock_null == NULL)
{
THREAD_ERROR_GOTO(trv);
}
mock_sock->fd = mock_testfds[PIPE_TEST_READ];
mock_buff->fd = mock_bufffds[PIPE_BUFFER_READ];
mock_null->fd = mock_nullfds[PIPE_NULL_READ];
errno = 0;
if (add_in_event_fd(&io, mock_testfds[PIPE_TEST_READ]) != 0)
{
THREAD_ERROR_GOTO(trv);
}
errno = 0;
if (add_in_event_fd(&io, mock_bufffds[PIPE_BUFFER_READ]) != 0)
{
THREAD_ERROR_GOTO(trv);
}
errno = 0;
if (add_in_event_fd(&io, mock_nullfds[PIPE_NULL_READ]) != 0)
{
THREAD_ERROR_GOTO(trv);
}
errno = 0;
if (add_in_event_fd(&io, mock_arpafds[PIPE_ARPA_READ]) != 0)
{
THREAD_ERROR_GOTO(trv);
}
sock_stats = (struct distributor_global_user_data *)mock_sock->global_user_data;
sock_stats->json_message_len_min = (unsigned long long int)-1;
sock_stats->options.do_hash_checks = 1;
buff_stats = (struct distributor_global_user_data *)mock_buff->global_user_data;
buff_stats->json_message_len_min = (unsigned long long int)-1;
buff_stats->options.do_hash_checks = 0;
mock_null_shutdown_events = (int *)mock_null->global_user_data;
*mock_null_shutdown_events = 0;
logger(0, "Distributor thread init done");
thread_signal(&start_condition);
thread_wait(&start_condition);
while (sock_stats->shutdown_events == 0 || buff_stats->shutdown_events == 0 || *mock_null_shutdown_events == 0)
{
if (nio_run(&io, -1) != NIO_SUCCESS)
{
logger(1, "nDPIsrvd event I/O returned: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
}
int nready = nio_get_nready(&io);
for (int i = 0; i < nready; i++)
{
int fd = nio_get_fd(&io, i);
if (nio_has_input(&io, i) != NIO_SUCCESS && nio_has_error(&io, i) != NIO_SUCCESS)
{
logger(1, "Invalid epoll event received for fd: %d", fd);
THREAD_ERROR_GOTO(trv);
}
if (nio_has_error(&io, i) == NIO_SUCCESS)
{
logger(1, "Distributor disconnected: %d", fd);
del_event(&io, fd);
}
if (fd == mock_testfds[PIPE_TEST_READ])
{
switch (nDPIsrvd_read(mock_sock))
{
case READ_OK:
break;
case READ_LAST_ENUM_VALUE:
case READ_ERROR:
case READ_TIMEOUT:
logger(1, "Read and verify fd returned an error: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
case READ_PEER_DISCONNECT:
break;
}
enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(mock_sock);
if (parse_ret != PARSE_NEED_MORE_DATA)
{
logger(1, "JSON parsing failed: %s", nDPIsrvd_enum_to_string(parse_ret));
logger(1,
"Problematic JSON message (mock sock, start: %zu, length: %llu, buffer usage: %zu): %.*s",
mock_sock->buffer.json_message_start,
mock_sock->buffer.json_message_length,
mock_sock->buffer.buf.used,
(int)mock_sock->buffer.json_message_length,
mock_sock->buffer.json_message);
THREAD_ERROR_GOTO(trv);
}
if (sock_stats->flow_cleanup_error != 0)
{
logger(1, "%s", "Flow cleanup callback error'd");
THREAD_ERROR_GOTO(trv);
}
}
else if (fd == mock_bufffds[PIPE_BUFFER_READ])
{
switch (nDPIsrvd_read(mock_buff))
{
case READ_OK:
break;
case READ_LAST_ENUM_VALUE:
case READ_ERROR:
case READ_TIMEOUT:
logger(1, "Read and verify fd returned an error: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
case READ_PEER_DISCONNECT:
break;
}
enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(mock_buff);
if (parse_ret != PARSE_NEED_MORE_DATA)
{
logger(1, "JSON parsing failed: %s", nDPIsrvd_enum_to_string(parse_ret));
logger(1,
"Problematic JSON message (buff sock, start: %zu, length: %llu, buffer usage: %zu): %.*s",
mock_buff->buffer.json_message_start,
mock_buff->buffer.json_message_length,
mock_buff->buffer.buf.used,
(int)mock_buff->buffer.json_message_length,
mock_buff->buffer.json_message);
THREAD_ERROR_GOTO(trv);
}
if (buff_stats->flow_cleanup_error != 0)
{
logger(1, "%s", "Flow cleanup callback error'd");
THREAD_ERROR_GOTO(trv);
}
}
else if (fd == mock_nullfds[PIPE_NULL_READ])
{
switch (nDPIsrvd_read(mock_null))
{
case READ_OK:
break;
case READ_LAST_ENUM_VALUE:
case READ_ERROR:
case READ_TIMEOUT:
logger(1, "Read and verify fd returned an error: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
case READ_PEER_DISCONNECT:
break;
}
enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(mock_null);
if (parse_ret != PARSE_NEED_MORE_DATA)
{
logger(1, "JSON parsing failed: %s", nDPIsrvd_enum_to_string(parse_ret));
logger(1,
"Problematic JSON message (buff sock, start: %zu, length: %llu, buffer usage: %zu): %.*s",
mock_null->buffer.json_message_start,
mock_null->buffer.json_message_length,
mock_null->buffer.buf.used,
(int)mock_null->buffer.json_message_length,
mock_null->buffer.json_message);
THREAD_ERROR_GOTO(trv);
}
}
else if (fd == mock_arpafds[PIPE_ARPA_READ])
{
char buf[NETWORK_BUFFER_MAX_SIZE];
ssize_t bytes_read = read(mock_arpafds[PIPE_ARPA_READ], buf, sizeof(buf));
if (bytes_read < 0)
{
logger(1, "Read fd returned an error: %s", strerror(errno));
THREAD_ERROR_GOTO(trv);
}
/*
* Nothing to do .. ?
* I am just here to trigger some IP code paths.
*/
}
#if !defined(__FreeBSD__) && !defined(__APPLE__)
else if (fd == signalfd)
{
struct signalfd_siginfo fdsi;
ssize_t s;
errno = 0;
s = read(signalfd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo))
{
THREAD_ERROR(trv);
}
if (fdsi.ssi_signo == SIGINT || fdsi.ssi_signo == SIGTERM || fdsi.ssi_signo == SIGQUIT)
{
logger(1, "Got signal %d, abort.", fdsi.ssi_signo);
errno = 0;
THREAD_ERROR_GOTO(trv);
}
}
#endif
else
{
logger(1, "Distributor epoll returned unexpected event data: %p", nio_get_ptr(&io, i));
THREAD_ERROR_GOTO(trv);
}
}
}
struct nDPIsrvd_instance * current_instance;
struct nDPIsrvd_instance * itmp;
struct nDPIsrvd_flow * current_flow;
struct nDPIsrvd_flow * ftmp;
HASH_ITER(hh, mock_sock->instance_table, current_instance, itmp)
{
HASH_ITER(hh, current_instance->flow_table, current_flow, ftmp)
{
logger(1,
"[Mock Sock] Active flow found during client distributor shutdown with id: %llu",
current_flow->id_as_ull);
errno = 0;
THREAD_ERROR(trv);
}
nDPIsrvd_cleanup_instance(mock_sock, current_instance, CLEANUP_REASON_APP_SHUTDOWN);
}
HASH_ITER(hh, mock_buff->instance_table, current_instance, itmp)
{
HASH_ITER(hh, current_instance->flow_table, current_flow, ftmp)
{
logger(1,
"[Mock Buff] Active flow found during client distributor shutdown with id: %llu",
current_flow->id_as_ull);
errno = 0;
THREAD_ERROR(trv);
}
nDPIsrvd_cleanup_instance(mock_buff, current_instance, CLEANUP_REASON_APP_SHUTDOWN);
}
if (memcmp(sock_stats, buff_stats, sizeof(*sock_stats) - sizeof(sock_stats->options)) != 0)
{
logger(1,
"Global statistics differ across different sockets! Events: %llu/%llu != %llu/%llu, Total Flows: "
"%llu/%llu + %llu != %llu/%llu + %llu",
buff_stats->total_events_serialized,
buff_stats->total_events_deserialized,
sock_stats->total_events_serialized,
sock_stats->total_events_deserialized,
buff_stats->flow_new_count,
buff_stats->flow_end_count,
buff_stats->flow_idle_count,
sock_stats->flow_new_count,
sock_stats->flow_end_count,
sock_stats->flow_idle_count);
errno = 0;
THREAD_ERROR(trv);
}
drv->stats = *sock_stats;
if (sock_stats->shutdown_events != 1 || buff_stats->shutdown_events != 1 || *mock_null_shutdown_events != 1)
{
logger(1,
"Unexpected amount of shutdown events received, expected 1 per nDPIsrvd socket, got (Sock/Buff/NULL): "
"%llu/%llu/%d",
sock_stats->shutdown_events,
buff_stats->shutdown_events,
*mock_null_shutdown_events);
errno = 0;
THREAD_ERROR(trv);
}
error:
#if !defined(__FreeBSD__) && !defined(__APPLE__)
del_event(&io, signalfd);
#endif
del_event(&io, mock_testfds[PIPE_TEST_READ]);
del_event(&io, mock_bufffds[PIPE_BUFFER_READ]);
del_event(&io, mock_nullfds[PIPE_NULL_READ]);
del_event(&io, mock_arpafds[PIPE_ARPA_READ]);
close(mock_testfds[PIPE_TEST_READ]);
close(mock_bufffds[PIPE_BUFFER_READ]);
close(mock_nullfds[PIPE_NULL_READ]);
close(mock_arpafds[PIPE_ARPA_READ]);
#if !defined(__FreeBSD__) && !defined(__APPLE__)
close(signalfd);
#endif
nio_free(&io);
nDPIsrvd_socket_free(&mock_sock);
nDPIsrvd_socket_free(&mock_buff);
nDPIsrvd_socket_free(&mock_null);
logger(0, "%s", "Distributor worker thread exits..");
return NULL;
}
static void * nDPId_mainloop_thread(void * const arg)
{
struct nDPId_return_value * const nrv = (struct nDPId_return_value *)arg;
struct thread_return_value * const trr = &nrv->thread_return_value;
logger(0, "nDPId thread started, init..");
if (thread_block_signals() != 0)
{
logger(1, "nDPId block signals failed: %s", strerror(errno));
THREAD_ERROR_GOTO(trr);
}
if (setup_reader_threads() != 0)
{
logger(1, "%s", "nDPId setup reader threads failed");
THREAD_ERROR_GOTO(trr);
}
/* Replace nDPId JSON socket fd with the one in our pipe and hope that no socket specific code-path triggered. */
reader_threads[nrv->thread_index].collector_sockfd = mock_pipefds[PIPE_nDPId];
reader_threads[nrv->thread_index].collector_sock_last_errno = 0;
if (set_collector_block(&reader_threads[nrv->thread_index]) != 0)
{
THREAD_ERROR_GOTO(trr);
}
logger(0, "nDPId thread initialize done");
thread_signal(&start_condition);
thread_wait(&start_condition);
jsonize_daemon(&reader_threads[nrv->thread_index], DAEMON_EVENT_INIT);
/* restore SIGPIPE to the default handler (Termination) */
if (signal(SIGPIPE, SIG_DFL) == SIG_ERR)
{
logger(1, "nDPId restore SIGPIPE to the default handler: %s", strerror(errno));
THREAD_ERROR_GOTO(trr);
}
run_capture_loop(&reader_threads[nrv->thread_index]);
process_remaining_flows();
nrv->packets_captured += reader_threads[nrv->thread_index].workflow->packets_captured;
nrv->packets_processed += reader_threads[nrv->thread_index].workflow->packets_processed;
nrv->total_skipped_flows += reader_threads[nrv->thread_index].workflow->total_skipped_flows;
nrv->total_l4_payload_len += reader_threads[nrv->thread_index].workflow->total_l4_payload_len;
nrv->not_detected_flow_protocols += reader_threads[nrv->thread_index].workflow->total_not_detected_flows;
nrv->guessed_flow_protocols += reader_threads[nrv->thread_index].workflow->total_guessed_flows;
nrv->detected_flow_protocols += reader_threads[nrv->thread_index].workflow->total_detected_flows;
nrv->flow_detection_updates += reader_threads[nrv->thread_index].workflow->total_flow_detection_updates;
nrv->flow_updates += reader_threads[nrv->thread_index].workflow->total_flow_updates;
nrv->total_active_flows += reader_threads[nrv->thread_index].workflow->total_active_flows;
nrv->total_idle_flows += reader_threads[nrv->thread_index].workflow->total_idle_flows;
nrv->cur_active_flows += reader_threads[nrv->thread_index].workflow->cur_active_flows;
nrv->cur_idle_flows += reader_threads[nrv->thread_index].workflow->cur_idle_flows;
#ifdef ENABLE_ZLIB
nrv->total_compressions += reader_threads[nrv->thread_index].workflow->total_compressions;
nrv->total_compression_diff += reader_threads[nrv->thread_index].workflow->total_compression_diff;
nrv->current_compression_diff += reader_threads[nrv->thread_index].workflow->current_compression_diff;
#endif
nrv->total_events_serialized += reader_threads[nrv->thread_index].workflow->total_events_serialized;
error:
thread_signal(&start_condition);
close(mock_pipefds[PIPE_nDPId]);
mock_pipefds[PIPE_nDPId] = -1;
logger(0, "%s", "nDPId worker thread exits..");
return NULL;
}
static void usage(char const * const arg0)
{
fprintf(stderr, "usage: %s [path-to-pcap-file] [optional-nDPId-config-file]\n", arg0);
}
static int thread_wait_for_termination(pthread_t thread, time_t wait_time_secs, struct thread_return_value * trv)
{
#if !defined(__FreeBSD__) && !defined(__APPLE__)
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
{
return -1;
}
ts.tv_sec += wait_time_secs;
int err = pthread_timedjoin_np(thread, (void **)&trv, &ts);
switch (err)
{
case EBUSY:
return 0;
case ETIMEDOUT:
return 0;
}
return 1;
#else
(void)wait_time_secs;
return pthread_join(thread, (void **)&trv) != 0;
#endif
}
static int base64_selftest()
{
const char encoded_buf[] =
"YDjgxTWgeJS0JASgCABFABi9tLtAAG0G2Ic0qHBDwKgCZAG7qo4aJ6Tg8cI7hVAYCASApwAAFgMDGJACAABVAwNk4H0AwVcaoxmhfP+LwJ/"
"ozXfcwVjP0gYb2nL2/TIuiiC0IwAA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YQrMhnody2ZSWp4piBCW1cjPfWEHFekL8j8pUylAsDsVTCIztDE2jwemvH5IUlqUPuIuvclWHhBtT2yukU/GnWyLpj/"
"hQNQb8X6BMSHTpbzo/W97x+QPNglYamPJn9hk2ePgjodFMApLNi1JEaGuuNEgMU2aniSkVJXQRmxgBTU0TJ8gITD/"
"gVQmlSCkc613oPSWRe9PeReVV6N2tmPLkPrtDGD420cw8p10XWFLEBuV08p/"
"jECRwlcP9QNcBkKSlwY5OKNTiqi3QESJbh+UrwPMnPiK2j+J8FWxTDAbXVomnvPG+"
"B09j6NML5Q4Hq00uwud8v6ZxPP3N2j4lplSZO0RhLAfwh5XcICEWH1+KetbiZpVoYfITsgWK3T5DgTGE+"
"f1n0x1CF48G59JTXHjfc9saHMEuQi7QelP4MokcI+oXQ1HFxvdJ6a+bg7f91WmQHcna1i2I0xHeahyBs7TT4n+rHxOl+"
"xWcs7sT59q9jZLumRvRDVerSx6rYwUe/1spvo6979lj/"
"oTu1PG6FVqPCU4kelQ7fxxlcIXd6HGqKRLIMjn7rfoqJjrMD6f+MuqOc6zhTtRiXVNt+"
"I7QyDynWob9hXatr7lnV0PrL8khH77gzI2EMAAFpAwAYYQTecdNR1L5rxVVXMe7Djp2zNXzMwoqnJg/"
"0hr4QS1hduMHTwKBXqjb4CGVJbxoc+S/7hjTHGDqWhHCpq9+4SQxcR6MVp65BVNMdGJRLMiHAkUWvivJW4DOanO//"
"36RFuMQEAQEAUMabZOvBbAa1KSTcWbL5ZGc/YPxLofmSTDMxf+KmqSukiR3yVasPbv5J6Hx2zCATM9pR9VRArg==";
size_t encoded_len = strnlen(encoded_buf, sizeof(encoded_buf));
unsigned char decoded_buf[nDPId_PACKETS_PLEN_MAX];
size_t decoded_len = sizeof(decoded_buf);
if (nDPIsrvd_base64decode(encoded_buf, encoded_len, decoded_buf, &decoded_len) != 0)
{
logger(1, "base64_selftest: Could not decode test buffer of size %zu bytes", encoded_len);
return 1;
}
char base64_data[nDPId_PACKETS_PLEN_MAX * 4];
size_t base64_data_len = sizeof(base64_data);
if (base64_encode(decoded_buf, decoded_len, base64_data, &base64_data_len) != 0)
{
logger(1, "base64_selftest: Re-encoding base64 buffer failed");
return 1;
}
if (base64_data_len != encoded_len)
{
logger(1,
"base64_selftest: Re-encoded base64 buffer size differs: %zu bytes != %zu bytes",
base64_data_len,
encoded_len);
return 1;
}
return strncmp(base64_data, encoded_buf, base64_data_len) != 0;
}
static int nio_selftest()
{
struct nio io;
nio_init(&io);
#ifdef ENABLE_EPOLL
logger(0, "%s", "Using epoll for nio");
#else
logger(0, "%s", "Using poll for nio");
#endif
#ifdef ENABLE_EPOLL
if (nio_use_epoll(&io, 32) != NIO_SUCCESS)
#else
if (nio_use_poll(&io, 32) != NIO_SUCCESS)
#endif
{
logger(1, "%s", "Could not use poll/epoll for nio");
goto error;
}
int pipefds[2];
int rv = pipe(pipefds);
if (rv < 0)
{
logger(1, "Could not create a pipe: %s", strerror(errno));
goto error;
}
if (nio_add_fd(&io, pipefds[1], NIO_EVENT_OUTPUT, NULL) != NIO_SUCCESS ||
nio_add_fd(&io, pipefds[0], NIO_EVENT_INPUT, NULL) != NIO_SUCCESS)
{
logger(1, "%s", "Could not add pipe fds to nio");
goto error;
}
if (fcntl_add_flags(pipefds[1], O_NONBLOCK) != 0 || fcntl_add_flags(pipefds[0], O_NONBLOCK) != 0)
{
logger(1, "%s", "Could not set pipe fds to O_NONBLOCK");
goto error;
}
char const wbuf[] = "AAAA";
size_t const wlen = strnlen(wbuf, sizeof(wbuf));
if (write(pipefds[1], wbuf, wlen) < 0)
{
logger(1, "Write '%s' (%zu bytes) to pipe failed with: %s", wbuf, wlen, strerror(errno));
goto error;
}
if (nio_run(&io, 1000) != NIO_SUCCESS)
{
logger(1, "%s", "Event notification failed");
goto error;
}
if (nio_can_output(&io, 0) != NIO_SUCCESS)
{
logger(1, "%s", "Pipe fd (write) can not output");
goto error;
}
if (nio_has_input(&io, 1) != NIO_SUCCESS)
{
logger(1, "%s", "Pipe fd (read) has no input");
goto error;
}
if (nio_is_valid(&io, 0) != NIO_SUCCESS || nio_is_valid(&io, 1) != NIO_SUCCESS ||
nio_has_error(&io, 0) == NIO_SUCCESS || nio_has_error(&io, 1) == NIO_SUCCESS)
{
logger(1, "%s", "Event validation failed");
goto error;
}
char rbuf[4];
if (read(pipefds[0], rbuf, sizeof(rbuf)) != sizeof(rbuf) || strncmp(rbuf, wbuf, wlen) != 0)
{
logger(1, "%s", "Buffer receive failed");
goto error;
}
if (nio_run(&io, 1000) != NIO_SUCCESS)
{
logger(1, "%s", "Event notification failed");
goto error;
}
if (nio_can_output(&io, 0) != NIO_SUCCESS)
{
logger(1, "%s", "Pipe fd (write) can not output");
goto error;
}
if (nio_has_input(&io, 1) == NIO_SUCCESS)
{
logger(1, "%s", "Pipe fd (read) has input");
goto error;
}
if (nio_is_valid(&io, 0) != NIO_SUCCESS || nio_is_valid(&io, 1) == NIO_SUCCESS ||
nio_has_error(&io, 0) == NIO_SUCCESS || nio_has_error(&io, 1) == NIO_SUCCESS)
{
logger(1, "%s", "Event validation failed");
goto error;
}
if (nio_del_fd(&io, pipefds[0]) != NIO_SUCCESS || nio_del_fd(&io, pipefds[1]) != NIO_SUCCESS)
{
logger(1, "%s", "Event delete failed");
goto error;
}
nio_free(&io);
return 0;
error:
nio_free(&io);
return 1;
}
#define THREADS_RETURNED_ERROR() \
(nDPId_return.thread_return_value.val != 0 || nDPIsrvd_return.val != 0 || \
distributor_return.thread_return_value.val != 0)
int main(int argc, char ** argv)
{
if (argc != 1 && argc != 2 && argc != 3)
{
usage(argv[0]);
return 1;
}
set_ndpi_malloc(ndpi_malloc_wrapper);
set_ndpi_free(ndpi_free_wrapper);
set_ndpi_flow_malloc(NULL);
set_ndpi_flow_free(NULL);
init_logging("nDPId-test");
log_app_info();
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
return 1;
}
if (argc == 1)
{
int retval = 0;
usage(argv[0]);
logger(1, "%s", "No pcap file provided. Running selftest mode.");
retval += base64_selftest();
retval += nio_selftest();
logger(1, "Selftest returned: %d", retval);
return retval;
}
if (access(argv[1], R_OK) != 0)
{
logger(1, "%s: pcap file `%s' does not exist or is not readable", argv[0], argv[1]);
return 1;
}
set_config_defaults(&config_map[0], nDPIsrvd_ARRAY_LENGTH(config_map));
set_config_defaults(&general_config_map[0], nDPIsrvd_ARRAY_LENGTH(general_config_map));
set_config_defaults(&tuning_config_map[0], nDPIsrvd_ARRAY_LENGTH(tuning_config_map));
{
int ret;
if (argc == 3)
{
set_cmdarg_string(&nDPId_options.config_file, argv[2]);
}
if (IS_CMDARG_SET(nDPId_options.config_file) != 0 &&
(ret = parse_config_file(GET_CMDARG_STR(nDPId_options.config_file), nDPId_parsed_config_line, NULL)) != 0)
{
if (ret > 0)
{
logger(1, "Config file `%s' is malformed", GET_CMDARG_STR(nDPId_options.config_file));
}
else if (ret == -ENOENT)
{
logger(1, "Path `%s' is not a regular file", GET_CMDARG_STR(nDPId_options.config_file));
}
else
{
logger(1,
"Could not open file `%s' for reading: %s",
GET_CMDARG_STR(nDPId_options.config_file),
strerror(errno));
}
return 1;
}
}
set_cmdarg_ull(&nDPIsrvd_options.max_write_buffers, 32);
set_cmdarg_string(&nDPId_options.pcap_file_or_interface, argv[1]);
set_cmdarg_boolean(&nDPId_options.decode_tunnel, 1);
set_cmdarg_boolean(&nDPId_options.enable_data_analysis, 1);
set_cmdarg_ull(&nDPId_options.max_packets_per_flow_to_send, 5);
#ifdef ENABLE_ZLIB
/*
* zLib compression is forced enabled for testing.
* Remember to compile nDPId with zlib enabled.
* There will be diff's while running `test/run_tests.sh' otherwise.
*/
set_cmdarg_boolean(&nDPId_options.enable_zlib_compression, 1);
#endif
#ifdef ENABLE_MEMORY_PROFILING
set_cmdarg_ull(&nDPId_options.memory_profiling_log_interval, (unsigned long long int)-1);
#endif
set_cmdarg_ull(&nDPId_options.reader_thread_count, 1); /* Please do not change this! Generating meaningful pcap
diff's relies on a single reader thread! */
set_cmdarg_string(&nDPId_options.instance_alias, "nDPId-test");
if (validate_options() != 0)
{
return 1;
}
if (setup_pipe(mock_pipefds) != 0 || setup_pipe(mock_testfds) != 0 || setup_pipe(mock_bufffds) != 0 ||
setup_pipe(mock_nullfds) != 0 || setup_pipe(mock_arpafds) != 0)
{
return 1;
}
/* We do not have any sockets, any socket operation must fail! */
collector_un_sockfd = -1;
distributor_un_sockfd = -1;
distributor_in_sockfd = -1;
global_context = ndpi_global_init();
if (global_context == NULL)
{
logger_early(1, "Could not initialize libnDPI global context.");
}
if (setup_remote_descriptors(MAX_REMOTE_DESCRIPTORS) != 0)
{
return 1;
}
pthread_t nDPId_thread;
struct nDPId_return_value nDPId_return = {.thread_index = 0};
if (pthread_create(&nDPId_thread, NULL, nDPId_mainloop_thread, &nDPId_return) != 0)
{
return 1;
}
pthread_t nDPIsrvd_thread;
struct thread_return_value nDPIsrvd_return = {};
if (pthread_create(&nDPIsrvd_thread, NULL, nDPIsrvd_mainloop_thread, &nDPIsrvd_return) != 0)
{
return 1;
}
pthread_t distributor_thread;
struct distributor_return_value distributor_return = {};
if (pthread_create(&distributor_thread, NULL, distributor_client_mainloop_thread, &distributor_return) != 0)
{
return 1;
}
/* Try to gracefully shutdown all threads. */
while (thread_wait_for_termination(distributor_thread, 1, &distributor_return.thread_return_value) == 0)
{
if (THREADS_RETURNED_ERROR() != 0)
{
break;
}
}
while (thread_wait_for_termination(nDPId_thread, 1, &nDPId_return.thread_return_value) == 0)
{
if (THREADS_RETURNED_ERROR() != 0)
{
break;
}
}
while (thread_wait_for_termination(nDPIsrvd_thread, 1, &nDPIsrvd_return) == 0)
{
if (THREADS_RETURNED_ERROR() != 0)
{
break;
}
}
logger(0, "%s", "All worker threads terminated..");
free_reader_threads();
if (global_context != NULL)
{
ndpi_global_deinit(global_context);
}
global_context = NULL;
if (THREADS_RETURNED_ERROR() != 0)
{
char const * which_thread = "Unknown";
int thread_errno = 0;
if (nDPId_return.thread_return_value.val != 0)
{
which_thread = "nDPId";
thread_errno = nDPId_return.thread_return_value.val;
}
else if (nDPIsrvd_return.val != 0)
{
which_thread = "nDPIsrvd";
thread_errno = nDPIsrvd_return.val;
}
else if (distributor_return.thread_return_value.val != 0)
{
which_thread = "Distributor";
thread_errno = distributor_return.thread_return_value.val;
}
logger(1,
"%s Thread returned a non zero value: %d (%s)",
which_thread,
thread_errno,
(thread_errno < 0 ? strerror(thread_errno) : "Application specific error"));
return 1;
}
{
printf(
"~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~\n"
"~~ packets captured/processed: %llu/%llu\n"
"~~ skipped flows.............: %llu\n"
"~~ total layer4 data length..: %llu bytes\n"
"~~ total detected protocols..: %llu\n"
"~~ total active/idle flows...: %llu/%llu\n"
"~~ total timeout flows.......: %llu\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
nDPId_return.packets_captured,
nDPId_return.packets_processed,
nDPId_return.total_skipped_flows,
nDPId_return.total_l4_payload_len,
nDPId_return.detected_flow_protocols,
nDPId_return.total_active_flows,
nDPId_return.total_idle_flows,
distributor_return.stats.total_flow_timeouts);
unsigned long long int total_alloc_bytes =
#ifdef ENABLE_ZLIB
(unsigned long long int)(MT_GET_AND_ADD(ndpi_memory_alloc_bytes, 0) -
MT_GET_AND_ADD(zlib_compression_bytes, 0) -
(MT_GET_AND_ADD(zlib_compressions, 0) * sizeof(struct nDPId_detection_data)));
#else
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_alloc_bytes, 0);
#endif
unsigned long long int total_free_bytes =
#ifdef ENABLE_ZLIB
(unsigned long long int)(MT_GET_AND_ADD(ndpi_memory_free_bytes, 0) -
MT_GET_AND_ADD(zlib_compression_bytes, 0) -
(MT_GET_AND_ADD(zlib_compressions, 0) * sizeof(struct nDPId_detection_data)));
#else
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_free_bytes, 0);
#endif
unsigned long long int total_alloc_count =
#ifdef ENABLE_ZLIB
(unsigned long long int)(MT_GET_AND_ADD(ndpi_memory_alloc_count, 0) -
MT_GET_AND_ADD(zlib_compressions, 0) * 2);
#else
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_alloc_count, 0);
#endif
unsigned long long int total_free_count =
#ifdef ENABLE_ZLIB
(unsigned long long int)(MT_GET_AND_ADD(ndpi_memory_free_count, 0) -
MT_GET_AND_ADD(zlib_decompressions, 0) * 2);
#else
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_free_count, 0);
#endif
printf(
"~~ total memory allocated....: %llu bytes\n"
"~~ total memory freed........: %llu bytes\n"
"~~ total allocations/frees...: %llu/%llu\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
total_alloc_bytes -
sizeof(struct nDPId_workflow) *
GET_CMDARG_ULL(
nDPId_options.reader_thread_count) /* We do not want to take the workflow into account. */,
total_free_bytes -
sizeof(struct nDPId_workflow) *
GET_CMDARG_ULL(
nDPId_options.reader_thread_count) /* We do not want to take the workflow into account. */,
total_alloc_count,
total_free_count);
printf(
"~~ json message min len.......: %llu chars\n"
"~~ json message max len.......: %llu chars\n"
"~~ json message avg len.......: %llu chars\n",
distributor_return.stats.json_message_len_min,
distributor_return.stats.json_message_len_max,
(unsigned long long int)distributor_return.stats.json_message_len_avg);
}
if (MT_GET_AND_ADD(ndpi_memory_alloc_bytes, 0) != MT_GET_AND_ADD(ndpi_memory_free_bytes, 0) ||
MT_GET_AND_ADD(ndpi_memory_alloc_count, 0) != MT_GET_AND_ADD(ndpi_memory_free_count, 0) ||
nDPId_return.total_active_flows != nDPId_return.total_idle_flows)
{
logger(1, "%s: %s", argv[0], "Memory / Flow leak detected.");
logger(1,
"%s: Allocated / Free'd bytes: %llu / %llu",
argv[0],
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_alloc_bytes, 0),
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_free_bytes, 0));
logger(1,
"%s: Allocated / Free'd count: %llu / %llu",
argv[0],
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_alloc_count, 0),
(unsigned long long int)MT_GET_AND_ADD(ndpi_memory_free_count, 0));
logger(1,
"%s: Total Active / Idle Flows: %llu / %llu",
argv[0],
nDPId_return.total_active_flows,
nDPId_return.total_idle_flows);
return 1;
}
if (nDPIsrvd_alloc_bytes != nDPIsrvd_free_bytes || nDPIsrvd_alloc_count != nDPIsrvd_free_count)
{
logger(1, "%s: %s", argv[0], "nDPIsrvd.h memory leak detected.");
logger(1, "%s: Allocated / Free'd bytes: %llu / %llu", argv[0], nDPIsrvd_alloc_bytes, nDPIsrvd_free_bytes);
logger(1, "%s: Allocated / Free'd count: %llu / %llu", argv[0], nDPIsrvd_alloc_count, nDPIsrvd_free_count);
return 1;
}
if (nDPId_return.cur_active_flows != 0 || nDPId_return.cur_idle_flows != 0)
{
logger(1,
"%s: %s [%llu / %llu]",
argv[0],
"Active / Idle inconsistency detected.",
nDPId_return.cur_active_flows,
nDPId_return.cur_idle_flows);
return 1;
}
if (nDPId_return.total_skipped_flows != 0)
{
logger(1,
"%s: %s [%llu]",
argv[0],
"Skipped flow detected, that should not happen.",
nDPId_return.total_skipped_flows);
return 1;
}
if (nDPId_return.total_events_serialized != distributor_return.stats.total_events_deserialized ||
nDPId_return.total_events_serialized != distributor_return.stats.total_events_serialized)
{
logger(1,
"%s: Event count of nDPId and distributor not equal: %llu != %llu",
argv[0],
nDPId_return.total_events_serialized,
distributor_return.stats.total_events_deserialized);
return 1;
}
if (nDPId_return.packets_processed != distributor_return.stats.total_packets_processed)
{
logger(1,
"%s: Total nDPId and distributor packets processed not equal: %llu != %llu",
argv[0],
nDPId_return.packets_processed,
distributor_return.stats.total_packets_processed);
return 1;
}
if (nDPId_return.total_l4_payload_len != distributor_return.stats.total_l4_payload_len)
{
logger(1,
"%s: Total processed layer4 payload length of nDPId and distributor not equal: %llu != %llu",
argv[0],
nDPId_return.total_l4_payload_len,
distributor_return.stats.total_l4_payload_len);
return 1;
}
if (distributor_return.stats.flow_new_count !=
distributor_return.stats.flow_end_count + distributor_return.stats.flow_idle_count)
{
logger(1,
"%s: Amount of flow 'new' events received is not equal to the amount of 'end' plus 'idle': %llu != "
"%llu + %llu",
argv[0],
distributor_return.stats.flow_new_count,
distributor_return.stats.flow_end_count,
distributor_return.stats.flow_idle_count);
return 1;
}
if (nDPId_return.total_active_flows !=
distributor_return.stats.flow_end_count + distributor_return.stats.flow_idle_count)
{
logger(1,
"%s: Amount of total active flows is not equal to the amount of received 'end' plus 'idle' events: "
"%llu != %llu + %llu",
argv[0],
nDPId_return.total_active_flows,
distributor_return.stats.flow_end_count,
distributor_return.stats.flow_idle_count);
return 1;
}
if (nDPId_return.total_idle_flows !=
distributor_return.stats.flow_idle_count + distributor_return.stats.flow_end_count)
{
logger(1,
"%s: Amount of total idle flows is not equal to the amount of received 'idle' events: %llu != %llu",
argv[0],
nDPId_return.total_idle_flows,
distributor_return.stats.flow_idle_count);
return 1;
}
if (nDPId_return.not_detected_flow_protocols != distributor_return.stats.flow_not_detected_count)
{
logger(1,
"%s: Amount of total undetected flows is not equal to the amount of received 'not-detected' events: "
"%llu != %llu",
argv[0],
nDPId_return.not_detected_flow_protocols,
distributor_return.stats.flow_not_detected_count);
return 1;
}
if (nDPId_return.guessed_flow_protocols != distributor_return.stats.flow_guessed_count)
{
logger(1,
"%s: Amount of total guessed flows is not equal to the amount of received 'guessed' events: %llu != "
"%llu",
argv[0],
nDPId_return.guessed_flow_protocols,
distributor_return.stats.flow_guessed_count);
return 1;
}
if (nDPId_return.detected_flow_protocols != distributor_return.stats.flow_detected_count)
{
logger(1,
"%s: Amount of total detected flows not equal to the amount of received 'detected' events: %llu != "
"%llu",
argv[0],
nDPId_return.detected_flow_protocols,
distributor_return.stats.flow_detected_count);
return 1;
}
if (nDPId_return.flow_detection_updates != distributor_return.stats.flow_detection_update_count)
{
logger(1,
"%s: Amount of total detection updates is not equal to the amount of received 'detection-update' "
"events: %llu != %llu",
argv[0],
nDPId_return.flow_detection_updates,
distributor_return.stats.flow_detection_update_count);
return 1;
}
if (nDPId_return.flow_updates != distributor_return.stats.flow_update_count)
{
logger(1,
"%s: Amount of total flow updates is not equal to the amount of received 'update' events: %llu != "
"%llu",
argv[0],
nDPId_return.flow_updates,
distributor_return.stats.flow_update_count);
return 1;
}
if (nDPId_return.total_active_flows != distributor_return.stats.flow_detected_count +
distributor_return.stats.flow_guessed_count +
distributor_return.stats.flow_not_detected_count)
{
logger(1,
"%s: Amount of total active flows not equal to the amount of received 'detected', 'guessed and "
"'not-detected' flow events: %llu != "
"%llu + %llu + %llu",
argv[0],
nDPId_return.total_active_flows,
distributor_return.stats.flow_detected_count,
distributor_return.stats.flow_guessed_count,
distributor_return.stats.flow_not_detected_count);
return 1;
}
if (distributor_return.stats.instance_user_data.daemon_event_count !=
distributor_return.stats.thread_user_data.daemon_event_count)
{
logger(1,
"%s: Amount of received daemon events differs between instance and thread: %llu != %llu",
argv[0],
distributor_return.stats.instance_user_data.daemon_event_count,
distributor_return.stats.thread_user_data.daemon_event_count);
return 1;
}
if (distributor_return.stats.instance_user_data.flow_cleanup_count - distributor_return.stats.total_flow_timeouts !=
distributor_return.stats.flow_end_count + distributor_return.stats.flow_idle_count)
{
logger(1,
"%s: Amount of flow cleanup callback calls differs between received 'end' and 'idle' flow events: %llu "
"!= %llu + %llu",
argv[0],
distributor_return.stats.instance_user_data.flow_cleanup_count -
distributor_return.stats.total_flow_timeouts,
distributor_return.stats.flow_end_count,
distributor_return.stats.flow_idle_count);
return 1;
}
if (distributor_return.stats.flow_new_count != distributor_return.stats.thread_user_data.flow_new_count ||
distributor_return.stats.flow_end_count != distributor_return.stats.thread_user_data.flow_end_count ||
distributor_return.stats.flow_idle_count != distributor_return.stats.thread_user_data.flow_idle_count)
{
logger(1,
"%s: Thread user data counters not equal to the global user data counters: %llu != %llu or %llu != %llu "
"or %llu != %llu",
argv[0],
distributor_return.stats.flow_new_count,
distributor_return.stats.thread_user_data.flow_new_count,
distributor_return.stats.flow_end_count,
distributor_return.stats.thread_user_data.flow_end_count,
distributor_return.stats.flow_idle_count,
distributor_return.stats.thread_user_data.flow_idle_count);
return 1;
}
#ifdef ENABLE_ZLIB
if (MT_GET_AND_ADD(zlib_compressions, 0) != MT_GET_AND_ADD(zlib_decompressions, 0))
{
logger(1,
"%s: %s (%llu != %llu)",
argv[0],
"ZLib compression / decompression inconsistency detected.",
(unsigned long long int)MT_GET_AND_ADD(zlib_compressions, 0),
(unsigned long long int)MT_GET_AND_ADD(zlib_decompressions, 0));
return 1;
}
if (nDPId_return.current_compression_diff != 0)
{
logger(1,
"%s: %s (%llu bytes)",
argv[0],
"ZLib compression inconsistency detected. It should be 0.",
nDPId_return.current_compression_diff);
return 1;
}
if (nDPId_return.total_compressions != MT_GET_AND_ADD(zlib_compressions, 0))
{
logger(1,
"%s: %s (%llu != %llu)",
argv[0],
"ZLib global<->workflow compression / decompression inconsistency detected.",
(unsigned long long int)MT_GET_AND_ADD(zlib_compressions, 0),
nDPId_return.current_compression_diff);
return 1;
}
if (nDPId_return.total_compression_diff != MT_GET_AND_ADD(zlib_compression_bytes, 0))
{
logger(1,
"%s: %s (%llu bytes != %llu bytes)",
argv[0],
"ZLib global<->workflow compression / decompression inconsistency detected.",
(unsigned long long int)MT_GET_AND_ADD(zlib_compression_bytes, 0),
nDPId_return.total_compression_diff);
return 1;
}
#endif
return 0;
}
|