comparison rtp-g711-extr.c @ 0:05ff0f7ac977

initial commit: split from gsm-codec-lib
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 02 Apr 2023 00:28:28 +0000
parents
children 9b0613775cf6
comparison
equal deleted inserted replaced
-1:000000000000 0:05ff0f7ac977
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 (cur_seq != 0 || last_seq != 0xFFFF)) {
76 fprintf(stderr,
77 "error in packet #%u: seq break from 0x%04X to 0x%04X\n",
78 pkt_idx, last_seq, cur_seq);
79 exit(1);
80 }
81 if (cur_tstamp != last_tstamp + 160) {
82 fprintf(stderr,
83 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n",
84 pkt_idx, last_tstamp, cur_tstamp);
85 exit(1);
86 }
87 } else {
88 stream_init_flag = 1;
89 stream_ssrc = cur_ssrc;
90 }
91 last_seq = cur_seq;
92 last_tstamp = cur_tstamp;
93 }
94
95 static void
96 process_packet(pkt, caplen, pkt_idx)
97 u_char *pkt;
98 unsigned caplen, pkt_idx;
99 {
100 unsigned udplen, payload_len;
101
102 if (caplen < link_hdr_len + 28)
103 return;
104 if (link_hdr_len) {
105 if (pkt[ethertype_offset] != 0x08)
106 return;
107 if (pkt[ethertype_offset+1] != 0x00)
108 return;
109 pkt += link_hdr_len;
110 caplen -= link_hdr_len;
111 }
112 /* check IP header */
113 if (pkt[0] != 0x45)
114 return;
115 if (pkt[9] != 17) /* UDP */
116 return;
117 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4))
118 return;
119 /* check UDP header */
120 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2))
121 return;
122 /* it is our target - now scrutinize it */
123 udplen = (pkt[24] << 8) | pkt[25];
124 if (caplen < udplen + 20) {
125 fprintf(stderr,
126 "error: packet #%u is truncated in the capture\n",
127 pkt_idx);
128 exit(1);
129 }
130 if (udplen < 20) {
131 fprintf(stderr,
132 "error in packet #%u: UDP length is too short for RTP header\n",
133 pkt_idx);
134 exit(1);
135 }
136 if (pkt[28] != 0x80) {
137 fprintf(stderr,
138 "error in packet #%u: unsupported RTP header structure\n",
139 pkt_idx);
140 exit(1);
141 }
142 rtp_stream_logic(pkt + 28, pkt_idx);
143 payload_len = udplen - 20;
144 if (payload_len != 160) {
145 fprintf(stderr, "error in packet #%u: unsupported payload\n",
146 pkt_idx);
147 exit(1);
148 }
149 fwrite(pkt + 40, 1, payload_len, outfile);
150 }
151
152 main(argc, argv)
153 char **argv;
154 {
155 char errbuf[PCAP_ERRBUF_SIZE];
156 u_char *pkt;
157 struct pcap_pkthdr pkthdr;
158 unsigned pkt_idx;
159
160 if (argc != 6) {
161 fprintf(stderr,
162 "usage: %s pcap-file src|dest ip-addr udp-port outfile\n",
163 argv[0]);
164 exit(1);
165 }
166 pcap = pcap_open_offline(argv[1], errbuf);
167 if (!pcap) {
168 fprintf(stderr, "%s: %s\n", argv[1], errbuf);
169 exit(1);
170 }
171 check_dl_type();
172 if (!strcmp(argv[2], "src")) {
173 iphdr_addr_offset = 12;
174 udphdr_port_offset = 0;
175 } else if (!strcmp(argv[2], "dest")) {
176 iphdr_addr_offset = 16;
177 udphdr_port_offset = 2;
178 } else {
179 fprintf(stderr,
180 "error: direction argument must be \"src\" or \"dest\"\n");
181 exit(1);
182 }
183 match_ip_addr = inet_addr(argv[3]);
184 if (match_ip_addr == INADDR_NONE) {
185 fprintf(stderr, "error: IP address argument is invalid\n");
186 exit(1);
187 }
188 match_udp_port = htons(strtoul(argv[4], 0, 0));
189 outfile = fopen(argv[5], "w");
190 if (!outfile) {
191 perror(argv[5]);
192 exit(1);
193 }
194 for (pkt_idx = 0; ; pkt_idx++) {
195 pkt = pcap_next(pcap, &pkthdr);
196 if (!pkt)
197 break;
198 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx);
199 }
200 if (!stream_init_flag) {
201 fprintf(stderr, "error: specified RTP stream not found\n");
202 exit(1);
203 }
204 fclose(outfile);
205 exit(0);
206 }