comparison pcap/rtp-g711-extr.c @ 175:9bd01faadaed

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