comparison pcap-study/rtp-tw5-extr.c @ 16:1bc144545563

pcap-study: new program rtp-tw5-extr
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 10 Oct 2024 17:40:16 +0000
parents pcap-study/rtp-g711-extr.c@e686bc92c7d8
children
comparison
equal deleted inserted replaced
15:96b37cef5020 16:1bc144545563
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 verifies that an unbroken RTP stream is present,
5 * with timestamp increments of 160 units per packet. The selected
6 * RTP stream is saved in a TW-TS-005 hexadecimal file.
7 */
8
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <pcap/pcap.h>
18
19 static pcap_t *pcap;
20 static in_addr_t match_ip_addr;
21 static u_short match_udp_port;
22 static unsigned iphdr_addr_offset, udphdr_port_offset;
23 static unsigned link_hdr_len, ethertype_offset;
24 static FILE *outfile;
25 static int stream_init_flag;
26 static unsigned last_seq, last_tstamp, stream_ssrc;
27
28 static void
29 check_dl_type()
30 {
31 int dltype;
32
33 dltype = pcap_datalink(pcap);
34 switch (dltype) {
35 case DLT_EN10MB:
36 link_hdr_len = 14;
37 ethertype_offset = 12;
38 break;
39 case DLT_RAW:
40 link_hdr_len = 0;
41 break;
42 case DLT_LINUX_SLL:
43 link_hdr_len = 16;
44 ethertype_offset = 14;
45 break;
46 default:
47 fprintf(stderr, "error: unsupported data link type %d\n",
48 dltype);
49 exit(1);
50 }
51 }
52
53 static void
54 rtp_stream_logic(rtp_hdr, pkt_idx)
55 u_char *rtp_hdr;
56 unsigned pkt_idx;
57 {
58 unsigned cur_seq, cur_tstamp, cur_ssrc;
59
60 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3];
61 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) |
62 (rtp_hdr[6] << 8) | rtp_hdr[7];
63 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) |
64 (rtp_hdr[10] << 8) | rtp_hdr[11];
65 if (stream_init_flag) {
66 if (cur_ssrc != stream_ssrc) {
67 fprintf(stderr,
68 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n",
69 pkt_idx, stream_ssrc, cur_ssrc);
70 exit(1);
71 }
72 if (cur_seq != last_seq + 1 &&
73 (cur_seq != 0 || last_seq != 0xFFFF)) {
74 fprintf(stderr,
75 "error in packet #%u: seq break from 0x%04X to 0x%04X\n",
76 pkt_idx, last_seq, cur_seq);
77 exit(1);
78 }
79 if (cur_tstamp != last_tstamp + 160) {
80 fprintf(stderr,
81 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n",
82 pkt_idx, last_tstamp, cur_tstamp);
83 exit(1);
84 }
85 } else {
86 stream_init_flag = 1;
87 stream_ssrc = cur_ssrc;
88 }
89 last_seq = cur_seq;
90 last_tstamp = cur_tstamp;
91 }
92
93 static void
94 emit_hex_frame(frame, nbytes)
95 u_char *frame;
96 unsigned nbytes;
97 {
98 unsigned n;
99
100 for (n = 0; n < nbytes; n++)
101 fprintf(outfile, "%02X", frame[n]);
102 putc('\n', outfile);
103 }
104
105 static void
106 process_packet(pkt, caplen, pkt_idx)
107 u_char *pkt;
108 unsigned caplen, pkt_idx;
109 {
110 unsigned udplen, payload_len;
111
112 if (caplen < link_hdr_len + 28)
113 return;
114 if (link_hdr_len) {
115 if (pkt[ethertype_offset] != 0x08)
116 return;
117 if (pkt[ethertype_offset+1] != 0x00)
118 return;
119 pkt += link_hdr_len;
120 caplen -= link_hdr_len;
121 }
122 /* check IP header */
123 if (pkt[0] != 0x45)
124 return;
125 if (pkt[9] != 17) /* UDP */
126 return;
127 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4))
128 return;
129 /* check UDP header */
130 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2))
131 return;
132 /* it is our target - now scrutinize it */
133 udplen = (pkt[24] << 8) | pkt[25];
134 if (caplen < udplen + 20) {
135 fprintf(stderr,
136 "error: packet #%u is truncated in the capture\n",
137 pkt_idx);
138 exit(1);
139 }
140 if (udplen < 20) {
141 fprintf(stderr,
142 "error in packet #%u: UDP length is too short for RTP header\n",
143 pkt_idx);
144 exit(1);
145 }
146 if (pkt[28] != 0x80) {
147 fprintf(stderr,
148 "error in packet #%u: unsupported RTP header structure\n",
149 pkt_idx);
150 exit(1);
151 }
152 rtp_stream_logic(pkt + 28, pkt_idx);
153 payload_len = udplen - 20;
154 if (payload_len > 40) {
155 fprintf(stderr,
156 "error in packet #%u: payload exceeds TW-TS-005 limit\n",
157 pkt_idx);
158 exit(1);
159 }
160 if (payload_len)
161 emit_hex_frame(pkt + 40, payload_len);
162 else
163 fputs("NULL\n", outfile);
164 }
165
166 main(argc, argv)
167 char **argv;
168 {
169 char errbuf[PCAP_ERRBUF_SIZE];
170 u_char *pkt;
171 struct pcap_pkthdr pkthdr;
172 unsigned pkt_idx, skip_num;
173
174 if (argc < 6 || argc > 7) {
175 fprintf(stderr,
176 "usage: %s pcap-file src|dest ip-addr udp-port outfile [skip-count]\n",
177 argv[0]);
178 exit(1);
179 }
180 pcap = pcap_open_offline(argv[1], errbuf);
181 if (!pcap) {
182 fprintf(stderr, "%s: %s\n", argv[1], errbuf);
183 exit(1);
184 }
185 check_dl_type();
186 if (!strcmp(argv[2], "src")) {
187 iphdr_addr_offset = 12;
188 udphdr_port_offset = 0;
189 } else if (!strcmp(argv[2], "dest")) {
190 iphdr_addr_offset = 16;
191 udphdr_port_offset = 2;
192 } else {
193 fprintf(stderr,
194 "error: direction argument must be \"src\" or \"dest\"\n");
195 exit(1);
196 }
197 match_ip_addr = inet_addr(argv[3]);
198 if (match_ip_addr == INADDR_NONE) {
199 fprintf(stderr, "error: IP address argument is invalid\n");
200 exit(1);
201 }
202 match_udp_port = htons(strtoul(argv[4], 0, 0));
203 outfile = fopen(argv[5], "w");
204 if (!outfile) {
205 perror(argv[5]);
206 exit(1);
207 }
208 if (argv[6])
209 skip_num = strtoul(argv[6], 0, 0);
210 else
211 skip_num = 0;
212 for (pkt_idx = 0; ; pkt_idx++) {
213 pkt = pcap_next(pcap, &pkthdr);
214 if (!pkt)
215 break;
216 if (pkt_idx < skip_num)
217 continue;
218 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx);
219 }
220 if (!stream_init_flag) {
221 fprintf(stderr, "error: specified RTP stream not found\n");
222 exit(1);
223 }
224 fclose(outfile);
225 exit(0);
226 }