comparison pcap-study/rtp-jitter-view.c @ 10:e686bc92c7d8

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