~didier-barvaux/rohc/rohc-1.7.x

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
/*
 * Copyright 2012,2013,2014 Didier Barvaux
 * Copyright 2012,2013 Viveris Technologies
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file   sniffer.c
 * @brief  ROHC sniffer program
 * @author Didier Barvaux <didier@barvaux.org>
 *
 * Objectives:
 *   Easily test the library on a network without affecting it.
 *   Gather compression statistics on a network without affecting it.
 *
 * How it works:
 *   The program sniffs IP packets from a given network interface, and tests
 *   the ROHC library with them. The packets are compressed, then decompressed,
 *   and finally compared with the original IP packets.
 *
 * Statistics:
 *   Some statistics are gathered during the tests. There are printed on the
 *   console. More stats should be added. A better way to export them remains to
 *   be added too.
 *
 * Post-mortem bug analysis:
 *   The program stops (assertion) if compression/decompression/comparison
 *   fails. The last library traces are recorded and printed in case of error.
 *   The last packets are recorded in PCAP files, one per context. This is
 *   also a good idea to run the program with core enabled. Many elements are
 *   thus available to reproduce and fix the discovered problems.
 */

#include "config.h" /* for HAVE_*_H and PACKAGE_BUGREPORT */

/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <math.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>

/* include for the PCAP library */
#if HAVE_PCAP_PCAP_H == 1
#  include <pcap/pcap.h>
#elif HAVE_PCAP_H == 1
#  include <pcap.h>
#else
#  error "pcap.h header not found, did you specified --enable-rohc-tests \
for ./configure ? If yes, check configure output and config.log"
#endif

/* ROHC includes */
#include <rohc/rohc.h>
#include <rohc/rohc_comp.h>
#include <rohc/rohc_decomp.h>



/** Return the smaller value from the two */
#define min(x, y)  (((x) < (y)) ? (x) : (y))
/** Return the greater value from the two */
#define max(x, y)  (((x) > (y)) ? (x) : (y))

/** The device MTU (TODO: should not be hardcoded) */
#define DEV_MTU 1518U

/** The maximal size for the ROHC packets */
#define MAX_ROHC_SIZE  (5 * 1024)

/** The length of the Linux Cooked Sockets header */
#define LINUX_COOKED_HDR_LEN  16U

/** The length (in bytes) of the Ethernet header */
#define ETHER_HDR_LEN  14U

/** The minimum Ethernet length (in bytes) */
#define ETHER_FRAME_MIN_LEN  60U


/** Some statistics collected by the sniffer */
struct sniffer_stats_t
{
	/** The size of one unit */
	unsigned long comp_unit_size;

	/** Cumulative number of units before ROHC compression */
	unsigned long comp_pre_nr_units;
	/** Cumulative number of bytes before ROHC compression */
	unsigned long comp_pre_nr_bytes;

	/** Cumulative number of units after ROHC compression */
	unsigned long comp_post_nr_units;
	/** Cumulative number of bytes after ROHC compression */
	unsigned long comp_post_nr_bytes;

	/** Cumulative number of packets per ROHC profile */
	unsigned long comp_nr_pkts_per_profile[ROHC_PROFILE_UDPLITE + 1];
	/** Cumulative number of packets per ROHC mode */
	unsigned long comp_nr_pkts_per_mode[ROHC_R_MODE + 1];
	/** Cumulative number of packets per state */
	unsigned long comp_nr_pkts_per_state[ROHC_COMP_STATE_SO + 1];
	/** Cumulative number of packets per packet type */
	unsigned long comp_nr_pkts_per_pkt_type[ROHC_PACKET_TCP_SEQ_8 + 1];
	/** Cumulative number of times a context is reused (first time included) */
	unsigned long comp_nr_reused_cid;

	/** Cumulative number of packets */
	unsigned long total_packets;
	/** Cumulative number of bad packets */
	unsigned long bad_packets;

	/** Cumulative number of (possible) lost packets */
	unsigned long nr_lost_packets;
	/** Cumulative number of (possible) loss bursts */
	unsigned long nr_loss_bursts;
	/** Maximum length of (possible) loss bursts */
	unsigned long max_loss_burst_len;
	/** Minimum length of (possible) loss bursts */
	unsigned long min_loss_burst_len;

	/** Cumulative number of (possible) mis-ordered packets */
	unsigned long nr_misordered_packets;
	/** Cumulative number of (possible) duplicated packets */
	unsigned long nr_duplicated_packets;
};


/* prototypes of private functions */

static void usage(void);
static void sniffer_interrupt(int signum);
static void sniffer_print_stats(int signum);

static bool sniff(const rohc_cid_type_t cid_type,
                  const size_t max_contexts,
                  const int enabled_profiles[],
                  const char *const device_name)
	__attribute__((warn_unused_result, nonnull(4)));
static int compress_decompress(struct rohc_comp *comp,
                               struct rohc_decomp *decomp,
                               struct pcap_pkthdr header,
                               unsigned char *packet,
                               size_t link_len_src,
                               pcap_t *handle,
                               pcap_dumper_t *dumpers[],
                               struct rohc_buf *const feedback_send,
                               unsigned int *const cid,
                               struct sniffer_stats_t *stats);

static int compare_packets(const struct rohc_buf pkt1,
                           const struct rohc_buf pkt2)
	__attribute__((warn_unused_result));
static size_t get_tcp_opt_padding(const struct rohc_buf packet)
	__attribute__((warn_unused_result));

static void print_rohc_traces(void *const priv_ctxt,
                              const rohc_trace_level_t level,
                              const rohc_trace_entity_t entity,
                              const int profile,
                              const char *const format,
                              ...)
	__attribute__((format(printf, 5, 6), nonnull(5)));
static int gen_false_random_num(const struct rohc_comp *const comp,
                                void *const user_context)
	__attribute__((nonnull(1)));
static bool rtp_detect_cb(const unsigned char *const ip,
                          const unsigned char *const udp,
                          const unsigned char *const payload,
                          const unsigned int payload_size,
                          void *const rtp_private)
	__attribute__((nonnull(1, 2, 3), warn_unused_result));


/** Whether the application shall stop or not */
static bool stop_program;

/** Some statistics collected by the sniffer */
static struct sniffer_stats_t sniffer_stats;

/** Whether the application runs in daemon mode or not */
static bool is_daemon;

/** Whether the application runs in verbose mode or not */
static bool is_verbose;

/** The PCAP dumpers */
static pcap_dumper_t *sniffer_dumpers[ROHC_LARGE_CID_MAX + 1] = { 0 };

/** The maximum number of traces to keep */
#define MAX_LAST_TRACES  5000
/** The maximum length of a trace */
#define MAX_TRACE_LEN  300

/** The ring buffer for the last traces */
static char last_traces[MAX_LAST_TRACES][MAX_LAST_TRACES + 1];
/** The index of the first trace */
static int last_traces_first;
/** The index of the last trace */
static int last_traces_last;

/** Whether to print traces on stderr or not */
static bool do_print_stderr;

/** Print a trace in syslog and eventually on stderr */
#define SNIFFER_LOG(prio, format, ...) \
	do \
	{ \
		if(do_print_stderr) \
		{ \
			fprintf(stderr, format "\n", ##__VA_ARGS__); \
			fflush(stderr); \
		} \
		syslog(prio, format, ##__VA_ARGS__); \
	} while(0)


/**
 * @brief Main function for the ROHC sniffer application
 *
 * @param argc The number of program arguments
 * @param argv The program arguments
 * @return     The unix return code:
 *              \li 0 in case of success,
 *              \li 1 in case of failure
 */
int main(int argc, char *argv[])
{
	const int do_change_dir = 1;
	const int do_close_fds = 1;
	int enabled_profiles[ROHC_PROFILE_UDPLITE + 1];
	char *pidfilename = NULL;
	char *cid_type_name = NULL;
	char *device_name = NULL;
	int max_contexts = ROHC_SMALL_CID_MAX + 1;
	rohc_cid_type_t cid_type;
	int args_used;
	int ret;
	int i;

	/* by default, we don't stop */
	stop_program = false;

	/* reset stats */
	memset(&sniffer_stats, 0, sizeof(struct sniffer_stats_t));
	sniffer_stats.comp_unit_size = 1;

	/* set to quiet mode by default */
	is_verbose = false;
	/* disable daemon mode by default */
	is_daemon = false;

	/* enable all ROHC profiles by default */
	enabled_profiles[ROHC_PROFILE_UNCOMPRESSED] = 1;
	enabled_profiles[ROHC_PROFILE_RTP] = 1;
	enabled_profiles[ROHC_PROFILE_UDP] = 1;
	enabled_profiles[ROHC_PROFILE_ESP] = 1;
	enabled_profiles[ROHC_PROFILE_IP] = 1;
	enabled_profiles[0x0005] = -1;
	enabled_profiles[ROHC_PROFILE_TCP] = 1;
	enabled_profiles[0x0007] = -1;
	enabled_profiles[ROHC_PROFILE_UDPLITE] = 1;

	/* no traces at the moment */
	for(i = 0; i < MAX_LAST_TRACES; i++)
	{
		last_traces[i][0] = '\0';
	}
	last_traces_first = -1;
	last_traces_last = -1;

	/* traces go to syslog */
	openlog("rohc_sniffer", LOG_PID, LOG_USER);

	/* parse program arguments, print the help message in case of failure */
	if(argc <= 1)
	{
		usage();
		goto error;
	}

	for(argc--, argv++; argc > 0; argc -= args_used, argv += args_used)
	{
		args_used = 1;

		if(!strcmp(*argv, "-v") || !strcmp(*argv, "--version"))
		{
			/* print version */
			printf("rohc_sniffer version %s\n", rohc_version());
			goto error;
		}
		else if(!strcmp(*argv, "-h") || !strcmp(*argv, "--help"))
		{
			/* print help */
			usage();
			goto error;
		}
		else if(!strcmp(*argv, "--verbose"))
		{
			/* enable verbose mode */
			is_verbose = true;
		}
		else if(!strcmp(*argv, "--daemon") || !strcmp(*argv, "-d"))
		{
			/* enable daemon mode */
			is_daemon = true;
		}
		else if(!strcmp(*argv, "-p") || !strcmp(*argv, "--pidfile"))
		{
			/* get the name of the pidfile */
			pidfilename = argv[1];
			args_used++;
		}
		else if(!strcmp(*argv, "-m") || !strcmp(*argv, "--max-contexts"))
		{
			/* get the maximum number of contexts the test should use */
			max_contexts = atoi(argv[1]);
			args_used++;
		}
		else if(!strcmp(*argv, "--disable"))
		{
			/* disable the given ROHC profile */
			const int rohc_profile = atoi(argv[1]);
			if(rohc_profile >= ROHC_PROFILE_UNCOMPRESSED &&
			   rohc_profile <= ROHC_PROFILE_UDPLITE)
			{
				enabled_profiles[rohc_profile] = 0;
				SNIFFER_LOG(LOG_INFO, "disable ROHC profile 0x%04x", rohc_profile);
			}
			args_used++;
		}
		else if(cid_type_name == NULL)
		{
			/* get the type of CID to use within the ROHC library */
			cid_type_name = argv[0];
		}
		else if(device_name == NULL)
		{
			/* get the device on which we will capture packets to compress,
			 * then decompress */
			device_name = argv[0];
		}
		else
		{
			/* do not accept more than one filename without option name */
			usage();
			goto error;
		}
	}

	/* check CID type */
	if(!strcmp(cid_type_name, "smallcid"))
	{
		cid_type = ROHC_SMALL_CID;

		/* the maximum number of ROHC contexts should be valid */
		if(max_contexts < 1 || max_contexts > (ROHC_SMALL_CID_MAX + 1))
		{
			SNIFFER_LOG(LOG_WARNING, "the maximum number of ROHC contexts "
			            "should be between 1 and %u", ROHC_SMALL_CID_MAX + 1);
			usage();
			goto error;
		}
	}
	else if(!strcmp(cid_type_name, "largecid"))
	{
		cid_type = ROHC_LARGE_CID;

		/* the maximum number of ROHC contexts should be valid */
		if(max_contexts < 1 || max_contexts > (ROHC_LARGE_CID_MAX + 1))
		{
			SNIFFER_LOG(LOG_WARNING, "the maximum number of ROHC contexts "
			            "should be between 1 and %u", ROHC_LARGE_CID_MAX + 1);
			usage();
			goto error;
		}
	}
	else
	{
		SNIFFER_LOG(LOG_WARNING, "invalid CID type '%s', only 'smallcid' and "
		            "'largecid' expected", cid_type_name);
		goto error;
	}

	/* the source filename is mandatory */
	if(device_name == NULL)
	{
		SNIFFER_LOG(LOG_WARNING, "device name is mandatory");
		usage();
		goto error;
	}

	/* --pidfile cannot be used in foreground mode */
	if(pidfilename != NULL && !is_daemon)
	{
		SNIFFER_LOG(LOG_WARNING, "option --pidfile cannot be used without "
		            "option --daemon");
		usage();
		goto error;
	}

	/* run in daemon mode if asked */
	if(is_daemon)
	{
		const char dirname[] = "/var/tmp/rohc_sniffer/";

		ret = daemon(do_change_dir, do_close_fds);
		if(ret != 0)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to run in background: %s (%d)",
			            strerror(errno), errno);
			goto error;
		}

		/* in daemon mode, do not write on stderr anymore, only syslog */
		do_print_stderr = false;

		/* create temporary directory for captures */
		ret = mkdir(dirname, 0700);
		if(ret != 0 && errno != EEXIST)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to create directory '%s': %s (%d)",
			            dirname, strerror(errno), errno);
			goto error;
		}

		/* enter the temporary directory */
		ret = chdir(dirname);
		if(ret != 0)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to enter directory '%s': %s (%d)",
			            dirname, strerror(errno), errno);
			goto error;
		}

		if(pidfilename != NULL)
		{
			FILE *pidfile;

			pidfile = fopen(pidfilename, "w");
			if(pidfile == NULL)
			{
				SNIFFER_LOG(LOG_WARNING, "failed to open PID file '%s': %s (%d)",
				            pidfilename, strerror(errno), errno);
				goto error;
			}
			ret = fprintf(pidfile, "%d\n", getpid());
			if(ret <= 0)
			{
				SNIFFER_LOG(LOG_WARNING, "failed to write PID in file '%s': "
				            "%s (%d)", pidfilename, strerror(errno), errno);
				ret = fclose(pidfile);
				if(ret != 0)
				{
					SNIFFER_LOG(LOG_WARNING, "failed to close PID file '%s': "
					            "%s (%d)", pidfilename, strerror(errno), errno);
				}
				goto error;
			}
			ret = fclose(pidfile);
			if(ret != 0)
			{
				SNIFFER_LOG(LOG_WARNING, "failed to close PID file '%s': %s (%d)",
				            pidfilename, strerror(errno), errno);
				goto error;
			}
		}
	}

	SNIFFER_LOG(LOG_INFO, "starting ROHC sniffer");

	/* set signal handlers */
	signal(SIGINT, sniffer_interrupt);
	signal(SIGTERM, sniffer_interrupt);
	signal(SIGSEGV, sniffer_interrupt);
	signal(SIGABRT, sniffer_interrupt);
	signal(SIGUSR1, sniffer_print_stats);
	{
		struct sigaction action;
		memset(&action, 0, sizeof(struct sigaction));
		action.sa_handler = SIG_IGN;
		sigaction(SIGHUP, &action, NULL);
	}

	/* test ROHC compression/decompression with the packets from the file */
	if(!sniff(cid_type, max_contexts, enabled_profiles, device_name))
	{
		goto error;
	}

	if(pidfilename != NULL)
	{
		ret = unlink(pidfilename);
		if(ret != 0)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to remove PID file '%s': %s (%d)",
			            pidfilename, strerror(errno), errno);
			goto error;
		}
	}
	closelog();
	return 0;

error:
	if(pidfilename != NULL)
	{
		ret = unlink(pidfilename);
		if(ret != 0)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to remove PID file '%s': %s (%d)",
			            pidfilename, strerror(errno), errno);
		}
	}
	closelog();
	return 1;
}


/**
 * @brief Print usage of the sniffer test application
 */
static void usage(void)
{
	printf("The ROHC sniffer tests the ROHC library with sniffed traffic\n"
	       "\n"
	       "You need to be root (or to have POSIX capability CAP_NET_ADMIN)\n"
	       "to run the ROHC sniffer.\n"
	       "\n"
	       "Usage: rohc_sniffer [OPTIONS] CID_TYPE DEVICE\n"
	       "\n"
	       "Options:\n"
	       "  CID_TYPE                The type of CID to use among 'smallcid'\n"
	       "                          and 'largecid'\n"
	       "  DEVICE                  The name of the network device to use\n"
	       "  -v, --version           Print version information and exit\n"
	       "  -h, --help              Print this usage and exit\n"
	       "  -d, --daemon            Run in background, trace in syslog\n"
	       "  -p, --pidfile FILE      Write daemon PID in the given file\n"
	       "  -m, --max-contexts NUM  The maximum number of ROHC contexts to\n"
	       "                          simultaneously use during the test\n"
	       "      --disable PROFILE   A ROHC profile to disable\n"
	       "                          (may be specified several times)\n"
	       "      --verbose           Make the test more verbose\n"
	       "\n"
	       "Examples:\n"
	       "  rohc_sniffer smallcid eth0          compress traffic from eth0\n"
	       "                                      with small CIDs\n"
	       "  rohc_sniffer -m 450 largecid wlan0  compress traffic from\n"
	       "                                      wlan0 with large CIDs, no\n"
	       "                                      more than 450 streams\n"
	       "\n"
	       "Report bugs to <" PACKAGE_BUGREPORT ">.\n");
}


/**
 * @brief Handle UNIX signals that interrupt the program
 *
 * @param signum  The received signal
 */
static void sniffer_interrupt(int signum)
{
	/* end the program with next captured packet */
	SNIFFER_LOG(LOG_NOTICE, "signal %d catched", signum);
	stop_program = true;

	/* for SIGSEGV/SIGABRT, close the PCAP dumps, print the last debug traces,
	 * then kill the program */
	if(signum == SIGSEGV || signum == SIGABRT)
	{
		int i;

		if(signum == SIGSEGV)
		{
			SNIFFER_LOG(LOG_WARNING, "a segfault occurred at packet #%lu",
			            sniffer_stats.total_packets);
		}
		else
		{
			SNIFFER_LOG(LOG_WARNING, "an assertion failed at packet #%lu",
			            sniffer_stats.total_packets);
		}

		/* close PCAP dumpers */
		for(i = 0; i <= ROHC_LARGE_CID_MAX; i++)
		{
			if(sniffer_dumpers[i] != NULL)
			{
				SNIFFER_LOG(LOG_INFO, "close dump file for context with ID %u", i);
				pcap_dump_close(sniffer_dumpers[i]);
			}
		}

		/* print last debug traces */
		if(last_traces_first == -1 || last_traces_last == -1)
		{
			SNIFFER_LOG(LOG_NOTICE, "no trace to print");
			raise(SIGKILL);
		}

		if(last_traces_first <= last_traces_last)
		{
			SNIFFER_LOG(LOG_NOTICE, "print the last %d traces...",
			            last_traces_last - last_traces_first);
			for(i = last_traces_first; i <= last_traces_last; i++)
			{
				SNIFFER_LOG(LOG_WARNING, "%s", last_traces[i]);
			}
		}
		else
		{
			SNIFFER_LOG(LOG_NOTICE, "print the last %d traces...",
			            MAX_LAST_TRACES - last_traces_first + last_traces_last);
			for(i = last_traces_first;
			    i <= MAX_LAST_TRACES + last_traces_last;
			    i++)
			{
				SNIFFER_LOG(LOG_WARNING, "%s", last_traces[i % MAX_LAST_TRACES]);
			}
		}
		SNIFFER_LOG(LOG_NOTICE, "all last traces printed, you can analyze "
		            "the problem, have a nice day!");

		if(signum == SIGSEGV)
		{
			struct sigaction action;
			memset(&action, 0, sizeof(struct sigaction));
			action.sa_handler = SIG_DFL;
			sigaction(SIGSEGV, &action, NULL);
			raise(signum);
		}
	}
}


/**
 * @brief Compute a percentage
 *
 * @param value  The value to compute percentage from
 * @param total  The total to compute percentage from
 * @return       The percentage
 */
static unsigned long long compute_percent(const unsigned long value,
                                          const unsigned long total)
{
	unsigned long long percent;

	if(total == 0)
	{
		percent = 0;
	}
	else
	{
		percent = value;
		percent *= 100;
		percent /= total;
	}

	return percent;
}


/**
 * @brief Handle UNIX signals that print statistics
 *
 * @param signum  The received signal
 */
static void sniffer_print_stats(int signum __attribute__((unused)))
{
	unsigned long total;
	int i;

	SNIFFER_LOG(LOG_INFO, "dump ROHC sniffer statistics...");

	/* general */
	SNIFFER_LOG(LOG_INFO, "general:");
	SNIFFER_LOG(LOG_INFO, "  total packets: %lu packets",
	            sniffer_stats.total_packets);
	SNIFFER_LOG(LOG_INFO, "  bad packets: %lu packets (%llu%%)",
	            sniffer_stats.bad_packets,
	            compute_percent(sniffer_stats.bad_packets, sniffer_stats.total_packets));
	SNIFFER_LOG(LOG_INFO, "  loss (estim.):");
	SNIFFER_LOG(LOG_INFO, "    %lu packets among %lu bursts (%llu%%)",
	            sniffer_stats.nr_lost_packets, sniffer_stats.nr_loss_bursts,
	            compute_percent(sniffer_stats.nr_lost_packets, sniffer_stats.total_packets));
	SNIFFER_LOG(LOG_INFO, "    packets per burst: max %lu, avg %lu, min %lu",
	            sniffer_stats.max_loss_burst_len, (sniffer_stats.nr_loss_bursts != 0 ?
	            sniffer_stats.nr_lost_packets / sniffer_stats.nr_loss_bursts : 0),
	            sniffer_stats.min_loss_burst_len);
	SNIFFER_LOG(LOG_INFO, "  mis-ordered packets (estim.): %lu packets "
	            "(%llu%%)", sniffer_stats.nr_misordered_packets,
	            compute_percent(sniffer_stats.nr_misordered_packets, sniffer_stats.total_packets));
	SNIFFER_LOG(LOG_INFO, "  duplicated packets (estim.): %lu packets "
	            "(%llu%%)", sniffer_stats.nr_duplicated_packets,
	            compute_percent(sniffer_stats.nr_duplicated_packets, sniffer_stats.total_packets));

	/* compression gain */
	SNIFFER_LOG(LOG_INFO, "compression gain:");
	if(sniffer_stats.comp_unit_size == 1)
	{
		SNIFFER_LOG(LOG_INFO, "  pre-compress: %lu bytes",
		            sniffer_stats.comp_pre_nr_bytes);
	}
	else
	{
		SNIFFER_LOG(LOG_INFO, "  pre-compress: %lu %s",
		            sniffer_stats.comp_pre_nr_units,
		            sniffer_stats.comp_unit_size == 1000 ? "KB" :
		            (sniffer_stats.comp_unit_size == 1000*1000 ? "MB" :
		             (sniffer_stats.comp_unit_size == 1000*1000*1000 ? "GB" : "?")));
	}
	if(sniffer_stats.comp_unit_size == 1)
	{
		SNIFFER_LOG(LOG_INFO, "  post-compress: %lu bytes",
		            sniffer_stats.comp_post_nr_bytes);
	}
	else
	{
		SNIFFER_LOG(LOG_INFO, "  post-compress: %lu %s",
		            sniffer_stats.comp_post_nr_units,
		            sniffer_stats.comp_unit_size == 1000 ? "KB" :
		            (sniffer_stats.comp_unit_size == 1000*1000 ? "MB" :
		             (sniffer_stats.comp_unit_size == 1000*1000*1000 ? "GB" : "?")));
	}
	if(sniffer_stats.comp_unit_size == 1)
	{
		SNIFFER_LOG(LOG_INFO, "  compress ratio: %llu%% of total, ie. %llu%% "
		            "of gain",
		            compute_percent(sniffer_stats.comp_post_nr_bytes, sniffer_stats.comp_pre_nr_bytes),
		            100 - compute_percent(sniffer_stats.comp_post_nr_bytes, sniffer_stats.comp_pre_nr_bytes));
	}
	else
	{
		SNIFFER_LOG(LOG_INFO, "  compress ratio: %llu%% of total, ie. %llu%% "
		            "of gain",
		            compute_percent(sniffer_stats.comp_post_nr_units, sniffer_stats.comp_pre_nr_units),
		            100 - compute_percent(sniffer_stats.comp_post_nr_units, sniffer_stats.comp_pre_nr_units));
	}
	SNIFFER_LOG(LOG_INFO, "  used and re-used contexts: %lu",
	            sniffer_stats.comp_nr_reused_cid);

	/* packets per profile */
	total = sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UNCOMPRESSED] +
	        sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_RTP] +
	        sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDP] +
	        sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_IP] +
	        sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_TCP] +
	        sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDPLITE];
	SNIFFER_LOG(LOG_INFO, "packets per profile:");
	SNIFFER_LOG(LOG_INFO, "  Uncompressed profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UNCOMPRESSED],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UNCOMPRESSED],
	                    total));
	SNIFFER_LOG(LOG_INFO, "  IP/UDP/RTP profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_RTP],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_RTP], total));
	SNIFFER_LOG(LOG_INFO, "  IP/UDP profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDP],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDP], total));
	SNIFFER_LOG(LOG_INFO, "  IP-only profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_IP],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_IP], total));
	SNIFFER_LOG(LOG_INFO, "  IP/TCP profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_TCP],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_TCP], total));
	SNIFFER_LOG(LOG_INFO, "  IP/UDP-Lite profile: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDPLITE],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_profile[ROHC_PROFILE_UDPLITE], total));

	/* packets per mode */
	total = sniffer_stats.comp_nr_pkts_per_mode[ROHC_U_MODE] +
	        sniffer_stats.comp_nr_pkts_per_mode[ROHC_O_MODE] +
	        sniffer_stats.comp_nr_pkts_per_mode[ROHC_R_MODE];
	SNIFFER_LOG(LOG_INFO, "packets per mode:");
	SNIFFER_LOG(LOG_INFO, "  U-mode: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_mode[ROHC_U_MODE],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_mode[ROHC_U_MODE], total));
	SNIFFER_LOG(LOG_INFO, "  O-mode: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_mode[ROHC_O_MODE],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_mode[ROHC_O_MODE], total));
	SNIFFER_LOG(LOG_INFO, "  R-mode: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_mode[ROHC_R_MODE],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_mode[ROHC_R_MODE], total));

	/* packets per state */
	total = sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_IR] +
	        sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_FO] +
	        sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_SO];
	SNIFFER_LOG(LOG_INFO, "packets per state:");
	SNIFFER_LOG(LOG_INFO, "  IR state: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_IR],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_IR], total));
	SNIFFER_LOG(LOG_INFO, "  FO state: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_FO],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_FO], total));
	SNIFFER_LOG(LOG_INFO, "  SO state: %lu packets (%llu%%)",
	            sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_SO],
	            compute_percent(sniffer_stats.comp_nr_pkts_per_state[ROHC_COMP_STATE_SO], total));

	/* packets per packet type */
	SNIFFER_LOG(LOG_INFO, "packets per packet type:");
	total = 0;
	for(i = ROHC_PACKET_IR; i <= ROHC_PACKET_TCP_SEQ_8; i++)
	{
		total += sniffer_stats.comp_nr_pkts_per_pkt_type[i];
	}
	for(i = ROHC_PACKET_IR; i <= ROHC_PACKET_TCP_SEQ_8; i++)
	{
		if(i != ROHC_PACKET_UNKNOWN &&
		   strcmp(rohc_get_packet_descr(i), "no description") != 0)
		{
			SNIFFER_LOG(LOG_INFO, "  packet type %s: %lu packets (%llu%%)",
			            rohc_get_packet_descr(i),
			            sniffer_stats.comp_nr_pkts_per_pkt_type[i],
			            compute_percent(sniffer_stats.comp_nr_pkts_per_pkt_type[i], total));
		}
	}

	SNIFFER_LOG(LOG_INFO, "all ROHC sniffer statistics dumped");
}


/**
 * @brief Test the ROHC library with a sniffed flow of IP packets going
 *        through one compressor/decompressor pair
 *
 * @param cid_type          The type of CIDs that the compressor shall use
 * @param max_contexts      The maximum number of ROHC contexts to use
 * @param enabled_profiles  The ROHC profiles to enable
 * @param device_name       The name of the network device
 * @return                  Whether the sniffer setup was OK
 */
static bool sniff(const rohc_cid_type_t cid_type,
                  const size_t max_contexts,
                  const int enabled_profiles[],
                  const char *const device_name)
{
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *handle;
	int link_layer_type_src;
	int link_len_src;
	struct pcap_pkthdr header;
	unsigned char *packet;

	struct rohc_comp *comp;
	struct rohc_decomp *decomp;

	uint8_t feedback_send_buffer[MAX_ROHC_SIZE];
	struct rohc_buf feedback_send =
		rohc_buf_init_empty(feedback_send_buffer, MAX_ROHC_SIZE);

	int ret;
	unsigned int i;

	/* statistics */
	unsigned int nb_ok = 0;
	unsigned int nb_bad = 0;
	unsigned int nb_internal_err = 0;
	unsigned int err_comp = 0;
	unsigned int err_decomp = 0;
	unsigned int nb_ref = 0;

	/* init status */
	bool status = false;

	assert(device_name != NULL);

	/* open the network device */
	handle = pcap_open_live(device_name, DEV_MTU, 0, 0, errbuf);
	if(handle == NULL)
	{
		SNIFFER_LOG(LOG_WARNING, "failed to open network device '%s': %s",
		            device_name, errbuf);
		goto error;
	}

	/* link layer in the source dump must be Ethernet */
	link_layer_type_src = pcap_datalink(handle);
	if(link_layer_type_src != DLT_EN10MB &&
	   link_layer_type_src != DLT_LINUX_SLL &&
	   link_layer_type_src != DLT_RAW)
	{
		SNIFFER_LOG(LOG_WARNING, "link layer type %d not supported in source "
		            "dump (supported = %d, %d, %d)", link_layer_type_src,
		            DLT_EN10MB, DLT_LINUX_SLL, DLT_RAW);
		goto close_input;
	}

	if(link_layer_type_src == DLT_EN10MB)
	{
		link_len_src = ETHER_HDR_LEN;
	}
	else if(link_layer_type_src == DLT_LINUX_SLL)
	{
		link_len_src = LINUX_COOKED_HDR_LEN;
	}
	else /* DLT_RAW */
	{
		link_len_src = 0;
	}

	/* create the ROHC compressor */
	comp = rohc_comp_new2(cid_type, max_contexts - 1, gen_false_random_num, NULL);
	if(comp == NULL)
	{
		SNIFFER_LOG(LOG_WARNING, "failed to create the ROHC compressor");
		goto close_input;
	}

	/* set the callback for traces on compressor */
	if(!rohc_comp_set_traces_cb2(comp, print_rohc_traces, NULL))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to set the trace callback for the "
		            "compressor");
		goto destroy_comp;
	}

	/* enable the compression profiles */
	for(i = ROHC_PROFILE_UNCOMPRESSED; i <= ROHC_PROFILE_UDPLITE; i++)
	{
		if(enabled_profiles[i] == 1 && !rohc_comp_enable_profile(comp, i))
		{
			SNIFFER_LOG(LOG_WARNING, "failed to enable compression profile "
			            "0x%04x", i);
			goto destroy_comp;
		}
		else if(enabled_profiles[i] == 0 && !rohc_comp_disable_profile(comp, i))
		{
			SNIFFER_LOG(LOG_WARNING, "failed to disable compression profile "
			            "0x%04x", i);
			goto destroy_comp;
		}
	}

	/* set the callback for RTP stream detection */
	if(!rohc_comp_set_rtp_detection_cb(comp, rtp_detect_cb, NULL))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to set the RTP stream detection "
		            "callback for compressor");
		goto destroy_comp;
	}

	/* create the decompressor (bi-directional mode) */
	decomp = rohc_decomp_new2(cid_type, max_contexts - 1, ROHC_O_MODE);
	if(decomp == NULL)
	{
		SNIFFER_LOG(LOG_WARNING, "failed to create the decompressor");
		goto destroy_comp;
	}

	/* set the callback for traces on decompressor */
	if(!rohc_decomp_set_traces_cb2(decomp, print_rohc_traces, NULL))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to set trace callback for "
		            "decompressor");
		goto destroy_decomp;
	}

	/* enable the decompression profiles */
	for(i = ROHC_PROFILE_UNCOMPRESSED; i <= ROHC_PROFILE_UDPLITE; i++)
	{
		if(enabled_profiles[i] == 1 && !rohc_decomp_enable_profile(decomp, i))
		{
			SNIFFER_LOG(LOG_WARNING, "failed to enable decompression profile "
			            "0x%04x", i);
			goto destroy_decomp;
		}
		else if(enabled_profiles[i] == 0 &&
		        !rohc_decomp_disable_profile(decomp, i))
		{
			SNIFFER_LOG(LOG_WARNING, "failed to disable decompression profile "
			            "0x%04x", i);
			goto destroy_decomp;
		}
	}

	/* reset the PCAP dumpers (used to save sniffed packets in several PCAP
	 * files, one per Context ID) */
	bzero(sniffer_dumpers, sizeof(pcap_dumper_t *) * max_contexts);

	SNIFFER_LOG(LOG_INFO, "ROHC sniffer successfully started");
	SNIFFER_LOG(LOG_INFO, "start processing captured packets");

	/* for each sniffed packet */
	sniffer_stats.total_packets = 0;
	while(!stop_program)
	{
		unsigned int cid = 0;

		/* try to capture a packet */
		packet = (unsigned char *) pcap_next(handle, &header);
		if(packet == NULL)
		{
			/* no packet captured, re-try */
			continue;
		}

		sniffer_stats.total_packets++;

		if(!is_daemon &&
		   (sniffer_stats.total_packets == 1 || (sniffer_stats.total_packets % 100) == 0))
		{
			if(sniffer_stats.total_packets > 1)
			{
				printf("\r");
			}
			printf("packet #%lu", sniffer_stats.total_packets);
			fflush(stdout);
		}

		/* compress & decompress from compressor to decompressor */
		ret = compress_decompress(comp, decomp, header, packet,
		                          link_len_src, handle, sniffer_dumpers,
		                          &feedback_send, &cid, &sniffer_stats);
		if(ret == -1)
		{
			err_comp++;
		}
		else if(ret == -2)
		{
			err_decomp++;
		}
		else if(ret == 0)
		{
			nb_ref++;
		}
		else if(ret == 1)
		{
			nb_ok++;
		}
		else if(ret == -3)
		{
			nb_bad++;
			sniffer_stats.bad_packets++;
		}
		else
		{
			nb_internal_err++;
		}

		/* in case of problem (ignore bad packets), just die! */
		if(ret != 1 && ret != -3)
		{
			SNIFFER_LOG(LOG_WARNING, "packet #%lu, CID %u: stats OK, ERR(COMP), "
			            "ERR(DECOMP), ERR(REF), ERR(BAD), ERR(INTERNAL)  =  "
			            "%u  %u  %u  %u  %u  %u", sniffer_stats.total_packets,
			            cid, nb_ok, err_comp, err_decomp, nb_ref, nb_bad,
			            nb_internal_err);

			/* last debug traces are recorded in SIGABRT handler */
			assert(0);
		}
	}

	if(stop_program)
	{
		SNIFFER_LOG(LOG_INFO, "program stopped by signal");
	}

	status = true;

	/* close PCAP dumpers */
	for(i = 0; i < max_contexts; i++)
	{
		if(sniffer_dumpers[i] != NULL)
		{
			SNIFFER_LOG(LOG_INFO, "close dump file for context with ID %u", i);
			pcap_dump_close(sniffer_dumpers[i]);
		}
	}

destroy_decomp:
	rohc_decomp_free(decomp);
destroy_comp:
	rohc_comp_free(comp);
close_input:
	pcap_close(handle);
error:
	return status;
}


/**
 * @brief Compress and decompress one uncompressed IP packet with the given
 *        compressor and decompressor
 *
 * @param comp           The compressor to use to compress the IP packet
 * @param decomp         The decompressor to use to decompress the IP packet
 * @param header         The PCAP header for the packet
 * @param packet         The packet to compress/decompress (link layer included)
 * @param link_len_src   The length of the link layer header before IP data
 * @param handle         The PCAP handler that sniffed the packet
 * @param dumpers        The PCAP dumpers, one per context
 * @param cid            OUT: the CID used for the last packet
 * @param stats          IN/OUT: The sniffer stats
 * @return               1 if the process is successful
 *                       0 if the decompressed packet doesn't match the
 *                         original one
 *                       -1 if an error occurs while compressing
 *                       -2 if an error occurs while decompressing
 *                       -3 if the link layer is not Ethernet
 *                       -4 if (de)compression info cannot be retrieved
 */
static int compress_decompress(struct rohc_comp *comp,
                               struct rohc_decomp *decomp,
                               struct pcap_pkthdr header,
                               unsigned char *packet,
                               size_t link_len_src,
                               pcap_t *handle,
                               pcap_dumper_t *dumpers[],
                               struct rohc_buf *const feedback_send,
                               unsigned int *const cid,
                               struct sniffer_stats_t *stats)
{
	const struct rohc_ts arrival_time = { .sec = 0, .nsec = 0 };
	struct rohc_buf ip_packet =
		rohc_buf_init_full(packet, header.caplen, arrival_time);

	const size_t output_packet_max_len =
		max(ETHER_HDR_LEN, LINUX_COOKED_HDR_LEN) + MAX_ROHC_SIZE;
	uint8_t output_packet[output_packet_max_len];
	struct rohc_buf rohc_packet =
		rohc_buf_init_empty(output_packet, output_packet_max_len);

	uint8_t decomp_buffer[MAX_ROHC_SIZE];
	struct rohc_buf decomp_packet =
		rohc_buf_init_empty(decomp_buffer, MAX_ROHC_SIZE);

	uint8_t rcvd_feedback_buffer[MAX_ROHC_SIZE];
	struct rohc_buf rcvd_feedback =
		rohc_buf_init_empty(rcvd_feedback_buffer, MAX_ROHC_SIZE);

	rohc_comp_last_packet_info2_t comp_last_packet_info;
	rohc_decomp_last_packet_info_t decomp_last_packet_info;
	unsigned long possible_unit;
	rohc_status_t status;
	int ret;

	/* check Ethernet frame length */
	if(header.len <= link_len_src || header.len != header.caplen)
	{
		if(is_verbose)
		{
			SNIFFER_LOG(LOG_WARNING, "bad PCAP packet (full len = %d, capture "
			            "len = %d)", header.len, header.caplen);
		}
		ret = -3;
		goto error;
	}

	/* skip the link layer header */
	rohc_buf_pull(&ip_packet, link_len_src);
	rohc_packet.len += link_len_src;
	rohc_buf_pull(&rohc_packet, link_len_src);

	/* check for padding after the IP packet in the Ethernet payload */
	if(link_len_src == ETHER_HDR_LEN && header.len == ETHER_FRAME_MIN_LEN)
	{
		int version;
		uint16_t tot_len;

		version = (rohc_buf_byte(ip_packet) >> 4) & 0x0f;
		if(version == 4)
		{
			memcpy(&tot_len, rohc_buf_data_at(ip_packet, 2), sizeof(uint16_t));
			tot_len = ntohs(tot_len);
		}
		else
		{
			const size_t ipv6_header_len = 40;
			memcpy(&tot_len, rohc_buf_data_at(ip_packet, 4), sizeof(uint16_t));
			tot_len = ipv6_header_len + ntohs(tot_len);
		}

		if(tot_len < ip_packet.len)
		{
			if(is_verbose)
			{
				SNIFFER_LOG(LOG_INFO, "the Ethernet frame has %zd bytes of "
				            "padding after the %u byte IP packet!",
				            ip_packet.len - tot_len, tot_len);
			}
			ip_packet.len = tot_len;
		}
	}

	/* fix IPv4 packets with non-standard-compliant 0xffff checksums instead
	 * of 0x0000 (Windows Vista seems to be faulty for the latter), to avoid
	 * false comparison failures after decompression */
	if(((rohc_buf_byte_at(ip_packet, 0) >> 4) & 0x0f) == 4 &&
	   ip_packet.len >= 20 &&
	   rohc_buf_byte_at(ip_packet, 10) == 0xff &&
	   rohc_buf_byte_at(ip_packet, 11) == 0xff)
	{
		rohc_buf_byte_at(ip_packet, 10) = 0x00;
		rohc_buf_byte_at(ip_packet, 11) = 0x00;
	}

	/* piggyback the feedback */
	rohc_buf_append_buf(&rohc_packet, *feedback_send);
	rohc_buf_pull(&rohc_packet, feedback_send->len);

	/* compress the IP packet */
	status = rohc_compress4(comp, ip_packet, &rohc_packet);
	if(status != ROHC_STATUS_OK)
	{
		pcap_dumper_t *dumper;

		SNIFFER_LOG(LOG_WARNING, "compression failed");
		ret = -1;
		rohc_buf_push(&ip_packet, link_len_src);

		/* open the new dumper */
		dumper = pcap_dump_open(handle, "./dump_stream_default.pcap");
		if(dumper == NULL)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to open new dump file '%s'",
			            "./dump_stream_default.pcap");
			assert(0);
			goto error;
		}

		/* dump the IP packet */
		SNIFFER_LOG(LOG_INFO, "dump packet in file '%s'",
		            "./dump_stream_default.pcap");
		pcap_dump((u_char *) dumper, &header, packet);

		SNIFFER_LOG(LOG_INFO, "close dump file");
		pcap_dump_close(dumper);

		goto error;
	}

	/* get some statistics about the last compressed packet */
	comp_last_packet_info.version_major = 0;
	comp_last_packet_info.version_minor = 0;
	if(!rohc_comp_get_last_packet_info2(comp, &comp_last_packet_info))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to get compression info");
		ret = -4;
		goto error;
	}
	stats->comp_pre_nr_bytes += ip_packet.len;
	stats->comp_post_nr_bytes += rohc_packet.len;
	possible_unit = stats->comp_unit_size;
	if(stats->comp_unit_size == 1)
	{
		if(stats->comp_pre_nr_bytes >= (100 * 1000) &&
		   stats->comp_post_nr_bytes >= (100 * 1000))
		{
			possible_unit = 1000;
		}
	}
	else
	{
		if(stats->comp_pre_nr_units >= (100 * 1000) &&
		   stats->comp_post_nr_units >= (100 * 1000))
		{
			possible_unit = stats->comp_unit_size * 1000;
		}
	}
	if(possible_unit != stats->comp_unit_size)
	{
		if(stats->comp_unit_size == 1)
		{
			stats->comp_pre_nr_units =
				stats->comp_pre_nr_bytes / 1000;
			stats->comp_pre_nr_bytes %= 1000;
			stats->comp_post_nr_units =
				stats->comp_post_nr_bytes / 1000;
			stats->comp_post_nr_bytes %= 1000;
		}
		else
		{
			unsigned long rest;
			rest = stats->comp_pre_nr_units % 1000;
			stats->comp_pre_nr_units /= 1000;
			stats->comp_pre_nr_bytes += rest * possible_unit;
			rest = stats->comp_post_nr_units % 1000;
			stats->comp_post_nr_units /= 1000;
			stats->comp_post_nr_bytes += rest * possible_unit;
		}
		stats->comp_unit_size = possible_unit;
	}
	else
	{
		if(stats->comp_unit_size > 1)
		{
			stats->comp_pre_nr_units +=
				stats->comp_pre_nr_bytes / stats->comp_unit_size;
			stats->comp_pre_nr_bytes %= stats->comp_unit_size;
			stats->comp_post_nr_units +=
				stats->comp_post_nr_bytes / stats->comp_unit_size;
			stats->comp_post_nr_bytes %= stats->comp_unit_size;
		}
	}
	stats->comp_nr_pkts_per_profile[comp_last_packet_info.profile_id]++;
	stats->comp_nr_pkts_per_mode[comp_last_packet_info.context_mode]++;
	stats->comp_nr_pkts_per_state[comp_last_packet_info.context_state]++;
	stats->comp_nr_pkts_per_pkt_type[comp_last_packet_info.packet_type]++;
	if(comp_last_packet_info.is_context_init)
	{
		stats->comp_nr_reused_cid++;
	}

	/* open a new dumper if none exists or the stream changed */
	if(comp_last_packet_info.is_context_init)
	{
		char dump_filename[1024];

		snprintf(dump_filename, 1024, "./dump_stream_cid_%u.pcap",
		         comp_last_packet_info.context_id);
		/* TODO: check result */

		/* close the previous dumper and remove its file if one was opened */
		if(dumpers[comp_last_packet_info.context_id] != NULL)
		{
			if(is_verbose)
			{
				SNIFFER_LOG(LOG_INFO, "replace dump file '%s' for context with "
				            "ID %u", dump_filename,
				            comp_last_packet_info.context_id);
			}
			pcap_dump_close(dumpers[comp_last_packet_info.context_id]);
			unlink(dump_filename);
			/* TODO: check result */
		}

		/* open the new dumper */
		dumpers[comp_last_packet_info.context_id] =
			pcap_dump_open(handle, dump_filename);
		if(dumpers[comp_last_packet_info.context_id] == NULL)
		{
			SNIFFER_LOG(LOG_WARNING, "failed to open new dump file '%s' for "
			            "context with ID %u", dump_filename,
			            comp_last_packet_info.context_id);
			assert(0);
			goto error;
		}
	}

	/* dump the IP packet */
	rohc_buf_push(&ip_packet, link_len_src);
	pcap_dump((u_char *) dumpers[comp_last_packet_info.context_id],
	          &header, packet);
	rohc_buf_pull(&ip_packet, link_len_src);

	/* record the CID */
	*cid = comp_last_packet_info.context_id;

	/* reset the feedback buffer */
	feedback_send->data -= feedback_send->offset;
	feedback_send->len = 0;

	/* decompress the ROHC packet */
	status = rohc_decompress3(decomp, rohc_packet, &decomp_packet,
	                          &rcvd_feedback, feedback_send);
	if(status != ROHC_STATUS_OK)
	{
		SNIFFER_LOG(LOG_WARNING, "decompression failed");
		ret = -2;
		goto error;
	}

	/* get some statistics about the last decompressed packet */
	decomp_last_packet_info.version_major = 0;
	decomp_last_packet_info.version_minor = 0;
	if(!rohc_decomp_get_last_packet_info(decomp, &decomp_last_packet_info))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to get decompression info");
		ret = -4;
		goto error;
	}
	stats->nr_lost_packets += decomp_last_packet_info.nr_lost_packets;
	if(decomp_last_packet_info.nr_lost_packets > 0)
	{
		stats->nr_loss_bursts++;
		if(decomp_last_packet_info.nr_lost_packets > stats->max_loss_burst_len)
		{
			stats->max_loss_burst_len = decomp_last_packet_info.nr_lost_packets;
		}
		if(decomp_last_packet_info.nr_lost_packets < stats->min_loss_burst_len ||
		   stats->min_loss_burst_len == 0)
		{
			stats->min_loss_burst_len = decomp_last_packet_info.nr_lost_packets;
		}
	}
	stats->nr_misordered_packets += decomp_last_packet_info.nr_misordered_packets;
	if(decomp_last_packet_info.is_duplicated)
	{
		stats->nr_duplicated_packets++;
	}

	/* deliver any received feedback data to the associated compressor */
	if(!rohc_comp_deliver_feedback2(comp, rcvd_feedback))
	{
		SNIFFER_LOG(LOG_WARNING, "failed to deliver feedback");
		ret = -4;
		goto error;
	}

	/* compare the decompressed packet with the original one */
	if(!compare_packets(ip_packet, decomp_packet))
	{
		SNIFFER_LOG(LOG_WARNING, "comparison with original packet failed");
		ret = 0;
	}
	else
	{
		/* comparison is OK */
		ret = 1;
	}

error:
	return ret;
}


/**
 * @brief Compare two network packets and print differences if any
 *
 * @param pkt1      The first packet
 * @param pkt2      The second packet
 * @return          Whether the packets are equal or not
 */
static int compare_packets(const struct rohc_buf pkt1,
                           const struct rohc_buf pkt2)
{
	int valid = 1;
	size_t min_size;
	size_t i, j, k;
	char str1[4][7], str2[4][7];
	char sep1, sep2;
	size_t tcp_padding_bytes;

	/* do not compare more than the shortest of the 2 packets */
	min_size = min(pkt1.len, pkt2.len);

	/* do not compare more than 180 bytes to avoid huge output */
	min_size = min(180, min_size);

	/* if packets are equal, do not print the packets */
	if(pkt1.len == pkt2.len &&
	   memcmp(rohc_buf_data(pkt1), rohc_buf_data(pkt2), pkt1.len) == 0)
	{
		goto skip;
	}
	/* packets seem different, double check for extra padding of TCP options:
	 * packets with extra padding at the end of TCP options cannot be compared */
	tcp_padding_bytes = get_tcp_opt_padding(pkt1);
	if(tcp_padding_bytes >= 4)
	{
		fprintf(stderr, "unexpected padding at the end of TCP options: "
		        "%zu EOL bytes\n", tcp_padding_bytes);
		goto skip;
	}

	/* packets are different */
	valid = 0;

	SNIFFER_LOG(LOG_WARNING, "------------------------------ Compare ------------------------------");
	SNIFFER_LOG(LOG_WARNING, "--------- reference ----------         ----------- new --------------");

	if(pkt1.len != pkt2.len)
	{
		SNIFFER_LOG(LOG_WARNING, "packets have different sizes (%zd != %zd), "
		            "compare only the %zd first bytes",
		            pkt1.len, pkt2.len, min_size);
	}

	j = 0;
	for(i = 0; i < min_size; i++)
	{
		if(rohc_buf_byte_at(pkt1, i) != rohc_buf_byte_at(pkt2, i))
		{
			sep1 = '#';
			sep2 = '#';
		}
		else
		{
			sep1 = '[';
			sep2 = ']';
		}

		sprintf(str1[j], "%c0x%.2x%c", sep1, rohc_buf_byte_at(pkt1, i), sep2);
		sprintf(str2[j], "%c0x%.2x%c", sep1, rohc_buf_byte_at(pkt2, i), sep2);

		/* make the output human readable */
		if(j >= 3 || (i + 1) >= min_size)
		{
			for(k = 0; k < 4; k++)
			{
				if(k < (j + 1))
				{
					SNIFFER_LOG(LOG_WARNING, "%s  ", str1[k]);
				}
				else /* fill the line with blanks if nothing to print */
				{
					SNIFFER_LOG(LOG_WARNING, "        ");
				}
			}

			SNIFFER_LOG(LOG_WARNING, "      ");

			for(k = 0; k < (j + 1); k++)
			{
				SNIFFER_LOG(LOG_WARNING, "%s  ", str2[k]);
			}

			j = 0;
		}
		else
		{
			j++;
		}
	}

	SNIFFER_LOG(LOG_WARNING, "----------------------- packets are different -----------------------");

skip:
	return valid;
}


/**
 * @brief How many bytes of padding the packet got after TCP options?
 *
 * TCP options shall be padded to be aligned on 32-bit boundaries, ie. add
 * 0 to 3 bytes of padding 0x00. Some TCP stacks however adds extra padding
 * (4 bytes of example). This stops us to compare original and decompressed
 * packet, since the ROHC decompressor will only add the minimal number of
 * padding bytes.
 *
 * @param packet  The packet to check for padding
 * @return        The number of padding bytes found after TCP options
 */
static size_t get_tcp_opt_padding(const struct rohc_buf packet)
{
	struct tcphdr *tcp;
	size_t opt_len;
	size_t nr_eol_found = 0;
	size_t ip_len;
	size_t i;

	/* check IP version */
	if(packet.len < 1)
	{
		goto too_short;
	}
	if((rohc_buf_byte(packet) & 0xf0) == 0x40)
	{
		/* IPv4 */
		struct iphdr *ip = (struct iphdr *) rohc_buf_data(packet);
		ip_len = ip->ihl * 4;
		if(packet.len <= ip_len || ip->protocol != 6)
		{
			goto not_tcp;
		}
	}
	else if((rohc_buf_byte(packet) & 0xf0) == 0x60)
	{
		/* IPv6 */
		struct ip6_hdr *ip = (struct ip6_hdr *) rohc_buf_data(packet);
		ip_len = sizeof(struct ip6_hdr);
		if(packet.len <= ip_len || ip->ip6_nxt != 6)
		{
			goto not_tcp;
		}
	}
	else
	{
		goto not_ip;
	}

	/* enough room for IP and TCP base header? */
	if(packet.len < (ip_len + sizeof(struct tcphdr)))
	{
		goto malformed;
	}

	/* enough room for TCP options? */
	tcp = (struct tcphdr *) rohc_buf_data_at(packet, ip_len);
	if(packet.len < (ip_len + tcp->doff * 4))
	{
		goto malformed;
	}

	/* parse TCP options, count padding bytes */
	nr_eol_found = 0;
	for(i = ip_len + sizeof(struct tcphdr);
	    i < (ip_len + tcp->doff * 4);
	    i += opt_len)
	{
		switch(rohc_buf_byte_at(packet, i))
		{
			case 0x01: /* NOP */
				opt_len = 1;
				break;
			case 0x00: /* EOL */
				opt_len = 1;
				nr_eol_found++;
				break;
			default: /* long option TLV */
				if(packet.len <= (i + 1))
				{
					goto malformed;
				}
				opt_len = rohc_buf_byte_at(packet, i + 1);
				if(packet.len <= (i + opt_len))
				{
					goto malformed;
				}
				break;
		}
	}

	return nr_eol_found;

not_ip:
not_tcp:
malformed:
too_short:
	return 0;
}


/**
 * @brief Callback to print traces of the ROHC library
 *
 * @param priv_ctxt  An optional private context, may be NULL
 * @param level      The priority level of the trace
 * @param entity     The entity that emitted the trace among:
 *                    \li ROHC_TRACE_COMP
 *                    \li ROHC_TRACE_DECOMP
 * @param profile    The ID of the ROHC compression/decompression profile
 *                   the trace is related to
 * @param format     The format string of the trace
 */
static void print_rohc_traces(void *const priv_ctxt __attribute__((unused)),
                              const rohc_trace_level_t level,
                              const rohc_trace_entity_t entity __attribute__((unused)),
                              const int profile __attribute__((unused)),
                              const char *format, ...)
{
	const char *level_descrs[] =
	{
		[ROHC_TRACE_DEBUG]   = "DEBUG",
		[ROHC_TRACE_INFO]    = "INFO",
		[ROHC_TRACE_WARNING] = "WARNING",
		[ROHC_TRACE_ERROR]   = "ERROR"
	};

	if(level >= ROHC_TRACE_WARNING || is_verbose)
	{
		va_list args;
		if(do_print_stderr)
		{
			fprintf(stdout, "[%s] ", level_descrs[level]);
			va_start(args, format);
			vfprintf(stdout, format, args);
			va_end(args);
		}
		if(is_daemon)
		{
			va_start(args, format);
			vsyslog(LOG_DEBUG, format, args);
			va_end(args);
		}
	}

	if(last_traces_last == -1)
	{
		last_traces_last = 0;
	}
	else
	{
		last_traces_last = (last_traces_last + 1) % MAX_LAST_TRACES;
	}
	{
		va_list args;
		va_start(args, format);
		vsnprintf(last_traces[last_traces_last], MAX_TRACE_LEN + 1, format, args);
		/* TODO: check return code */
		last_traces[last_traces_last][MAX_TRACE_LEN] = '\0';
		va_end(args);
	}
	if(last_traces_first == -1)
	{
		last_traces_first = 0;
	}
	else if(last_traces_first == last_traces_last)
	{
		last_traces_first = (last_traces_first + 1) % MAX_LAST_TRACES;
	}
}


/**
 * @brief Generate a false random number for testing the ROHC library
 *
 * @param comp          The ROHC compressor
 * @param user_context  Should always be NULL
 * @return              Always 0
 */
static int gen_false_random_num(const struct rohc_comp *const comp,
                                void *const user_context)
{
	assert(comp != NULL);
	assert(user_context == NULL);
	return 0;
}


/**
 * @brief The detection callback which do detect RTP stream
 *
 * @param ip           The inner ip packet
 * @param udp          The udp header of the packet
 * @param payload      The payload of the packet
 * @param payload_size The size of the payload (in bytes)
 * @return             1 if the packet is an RTP packet, 0 otherwise
 */
static bool rtp_detect_cb(const unsigned char *const ip,
                          const unsigned char *const udp,
                          const unsigned char *const payload,
                          const unsigned int payload_size,
                          void *const rtp_private)
{
	const uint16_t max_well_known_port = 1024;
	const uint16_t sip_port = 5060;
	uint16_t udp_sport;
	uint16_t udp_dport;
	uint16_t udp_len;
	uint8_t rtp_pt;
	bool is_rtp = false;

	assert(ip != NULL);
	assert(udp != NULL);
	assert(payload != NULL);
	assert(rtp_private == NULL);

	/* retrieve UDP source and destination ports and UDP length */
	memcpy(&udp_sport, udp, sizeof(uint16_t));
	memcpy(&udp_dport, udp + 2, sizeof(uint16_t));
	memcpy(&udp_len, udp + 4, sizeof(uint16_t));

	/* RTP streams do not use well known ports */
	if(ntohs(udp_sport) <= max_well_known_port ||
	   ntohs(udp_dport) <= max_well_known_port)
	{
		goto not_rtp;
	}

	/* SIP (UDP/5060) is not RTP */
	if(ntohs(udp_sport) == sip_port && ntohs(udp_dport) == sip_port)
	{
		goto not_rtp;
	}

	/* the UDP destination port of RTP packet is even (the RTCP destination
	 * port are RTP destination port + 1, so it is odd) */
	if((ntohs(udp_dport) % 2) != 0)
	{
		goto not_rtp;
	}

	/* UDP Length shall not be too large */
	if(ntohs(udp_len) > 200)
	{
		goto not_rtp;
	}

	/* UDP payload shall at least contain the smallest RTP header */
	if(payload_size < 12)
	{
		goto not_rtp;
	}

	/* RTP version bits shall be 2 */
	if(((payload[0] >> 6) & 0x3) != 0x2)
	{
		goto not_rtp;
	}

	/* RTP payload type shall be GSM (0x03), ITU-T G.723 (0x04),
	 * ITU-T G.729 (0x12), dynamic RTP type 97 (0x61),
	 * telephony-event (0x65), Speex (0x72),
	 * or dynamic RTP type 125 (0x7d) */
	rtp_pt = payload[1] & 0x7f;
	if (rtp_pt != 0x03 && rtp_pt != 0x04 && rtp_pt != 0x12 &&
	    rtp_pt != 0x61 && rtp_pt != 0x65 && rtp_pt != 0x72 &&
	    rtp_pt != 0x7d)
	{
		goto not_rtp;
	}

	/* we think that the UDP packet is a RTP packet */
	is_rtp = true;

not_rtp:
	return is_rtp;
}