FreeCalypso > hg > gsm-codec-lib
comparison pcap/rtp-cont-check.c @ 170:b9af126bfddb
rtp-cont-check utility written
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 26 Dec 2022 21:24:00 +0000 |
parents | pcap/rtp-gsmfr-extr.c@851ca64e38e9 |
children | 75607bc26f57 |
comparison
equal
deleted
inserted
replaced
169:0c965c3c5e6e | 170:b9af126bfddb |
---|---|
1 /* | |
2 * This program reads a pcap file, extracts packets belonging to a | |
3 * particular RTP stream as identified by a source or destination | |
4 * IP:port, and checks its continuity: verifies that all sequence | |
5 * numbers and timestamps increase monotonously without breaks, | |
6 * and that the time delta between each consecutive pair of packets | |
7 * is within GSM TCH multiframe jitter bounds of 20 ms. | |
8 * | |
9 * The codec can be anything, and this program can also be used to | |
10 * check continuity of RTP streams coming from PSTN/SIP, but the | |
11 * core assumption is that packets must arrive every 20 ms, with | |
12 * the timestamp field incrementing by 160 units each time. | |
13 */ | |
14 | |
15 #include <sys/types.h> | |
16 #include <sys/socket.h> | |
17 #include <sys/time.h> | |
18 #include <netinet/in.h> | |
19 #include <arpa/inet.h> | |
20 #include <stdio.h> | |
21 #include <stdlib.h> | |
22 #include <string.h> | |
23 #include <strings.h> | |
24 #include <pcap/pcap.h> | |
25 | |
26 static pcap_t *pcap; | |
27 static in_addr_t match_ip_addr; | |
28 static u_short match_udp_port; | |
29 static unsigned iphdr_addr_offset, udphdr_port_offset; | |
30 static unsigned link_hdr_len, ethertype_offset; | |
31 static int stream_init_flag; | |
32 static unsigned last_seq, last_tstamp, stream_ssrc; | |
33 static struct timeval last_pkt_time; | |
34 | |
35 /* usec jitter thresholds around 20 ms ideal */ | |
36 #define DELTAT_MIN 18450 | |
37 #define DELTAT_MAX 23090 | |
38 | |
39 static void | |
40 check_dl_type() | |
41 { | |
42 int dltype; | |
43 | |
44 dltype = pcap_datalink(pcap); | |
45 switch (dltype) { | |
46 case DLT_EN10MB: | |
47 link_hdr_len = 14; | |
48 ethertype_offset = 12; | |
49 break; | |
50 case DLT_RAW: | |
51 link_hdr_len = 0; | |
52 break; | |
53 case DLT_LINUX_SLL: | |
54 link_hdr_len = 16; | |
55 ethertype_offset = 14; | |
56 break; | |
57 default: | |
58 fprintf(stderr, "error: unsupported data link type %d\n", | |
59 dltype); | |
60 exit(1); | |
61 } | |
62 } | |
63 | |
64 static void | |
65 rtp_stream_logic(rtp_hdr, pkt_idx, pkt_time) | |
66 u_char *rtp_hdr; | |
67 unsigned pkt_idx; | |
68 struct timeval *pkt_time; | |
69 { | |
70 unsigned cur_seq, cur_tstamp, cur_ssrc; | |
71 struct timeval deltat; | |
72 | |
73 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3]; | |
74 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) | | |
75 (rtp_hdr[6] << 8) | rtp_hdr[7]; | |
76 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) | | |
77 (rtp_hdr[10] << 8) | rtp_hdr[11]; | |
78 if (stream_init_flag) { | |
79 if (cur_ssrc != stream_ssrc) { | |
80 fprintf(stderr, | |
81 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
82 pkt_idx, stream_ssrc, cur_ssrc); | |
83 exit(1); | |
84 } | |
85 if (cur_seq != last_seq + 1) { | |
86 fprintf(stderr, | |
87 "error in packet #%u: seq break from 0x%04X to 0x%04X\n", | |
88 pkt_idx, last_seq, cur_seq); | |
89 exit(1); | |
90 } | |
91 if (cur_tstamp != last_tstamp + 160) { | |
92 fprintf(stderr, | |
93 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n", | |
94 pkt_idx, last_tstamp, cur_tstamp); | |
95 exit(1); | |
96 } | |
97 if (timercmp(pkt_time, &last_pkt_time, <)) { | |
98 fprintf(stderr, | |
99 "packet #%u timing error: Rx time goes backward\n", | |
100 pkt_idx); | |
101 exit(1); | |
102 } | |
103 timersub(pkt_time, &last_pkt_time, &deltat); | |
104 if (deltat.tv_sec) { | |
105 fprintf(stderr, | |
106 "packet #%u timing error: Rx time gap >= 1 s\n", | |
107 pkt_idx); | |
108 exit(1); | |
109 } | |
110 if (deltat.tv_usec > DELTAT_MAX) { | |
111 fprintf(stderr, | |
112 "packet #%u timing error: Rx time delta of %u us is above max threshold\n", | |
113 pkt_idx, (unsigned) deltat.tv_usec); | |
114 exit(1); | |
115 } | |
116 if (deltat.tv_usec < DELTAT_MIN) { | |
117 fprintf(stderr, | |
118 "packet #%u timing error: Rx time delta of %u us is below min threshold\n", | |
119 pkt_idx, (unsigned) deltat.tv_usec); | |
120 exit(1); | |
121 } | |
122 } else { | |
123 stream_init_flag = 1; | |
124 stream_ssrc = cur_ssrc; | |
125 } | |
126 last_seq = cur_seq; | |
127 last_tstamp = cur_tstamp; | |
128 bcopy(pkt_time, &last_pkt_time, sizeof(struct timeval)); | |
129 } | |
130 | |
131 static void | |
132 process_packet(pkt, caplen, pkt_idx, pkt_time) | |
133 u_char *pkt; | |
134 unsigned caplen, pkt_idx; | |
135 struct timeval *pkt_time; | |
136 { | |
137 unsigned udplen; | |
138 | |
139 if (caplen < link_hdr_len + 28) | |
140 return; | |
141 if (link_hdr_len) { | |
142 if (pkt[ethertype_offset] != 0x08) | |
143 return; | |
144 if (pkt[ethertype_offset+1] != 0x00) | |
145 return; | |
146 pkt += link_hdr_len; | |
147 caplen -= link_hdr_len; | |
148 } | |
149 /* check IP header */ | |
150 if (pkt[0] != 0x45) | |
151 return; | |
152 if (pkt[9] != 17) /* UDP */ | |
153 return; | |
154 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4)) | |
155 return; | |
156 /* check UDP header */ | |
157 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2)) | |
158 return; | |
159 /* it is our target - now scrutinize it */ | |
160 udplen = (pkt[24] << 8) | pkt[25]; | |
161 if (caplen < udplen + 20) { | |
162 fprintf(stderr, | |
163 "error: packet #%u is truncated in the capture\n", | |
164 pkt_idx); | |
165 exit(1); | |
166 } | |
167 if (udplen < 20) { | |
168 fprintf(stderr, | |
169 "error in packet #%u: UDP length is too short for RTP header\n", | |
170 pkt_idx); | |
171 exit(1); | |
172 } | |
173 if (pkt[28] != 0x80) { | |
174 fprintf(stderr, | |
175 "error in packet #%u: unsupported RTP header structure\n", | |
176 pkt_idx); | |
177 exit(1); | |
178 } | |
179 rtp_stream_logic(pkt + 28, pkt_idx, pkt_time); | |
180 } | |
181 | |
182 main(argc, argv) | |
183 char **argv; | |
184 { | |
185 char errbuf[PCAP_ERRBUF_SIZE]; | |
186 u_char *pkt; | |
187 struct pcap_pkthdr pkthdr; | |
188 unsigned pkt_idx; | |
189 | |
190 if (argc != 5) { | |
191 fprintf(stderr, | |
192 "usage: %s pcap-file src|dest ip-addr udp-port\n", | |
193 argv[0]); | |
194 exit(1); | |
195 } | |
196 pcap = pcap_open_offline(argv[1], errbuf); | |
197 if (!pcap) { | |
198 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
199 exit(1); | |
200 } | |
201 check_dl_type(); | |
202 if (!strcmp(argv[2], "src")) { | |
203 iphdr_addr_offset = 12; | |
204 udphdr_port_offset = 0; | |
205 } else if (!strcmp(argv[2], "dest")) { | |
206 iphdr_addr_offset = 16; | |
207 udphdr_port_offset = 2; | |
208 } else { | |
209 fprintf(stderr, | |
210 "error: direction argument must be \"src\" or \"dest\"\n"); | |
211 exit(1); | |
212 } | |
213 match_ip_addr = inet_addr(argv[3]); | |
214 if (match_ip_addr == INADDR_NONE) { | |
215 fprintf(stderr, "error: IP address argument is invalid\n"); | |
216 exit(1); | |
217 } | |
218 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
219 for (pkt_idx = 0; ; pkt_idx++) { | |
220 pkt = pcap_next(pcap, &pkthdr); | |
221 if (!pkt) | |
222 break; | |
223 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx, | |
224 &pkthdr.ts); | |
225 } | |
226 if (!stream_init_flag) { | |
227 fprintf(stderr, "error: specified RTP stream not found\n"); | |
228 exit(1); | |
229 } | |
230 exit(0); | |
231 } |