FreeCalypso > hg > rtp-debug-utils
comparison rtp-gsmfr-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 | 210dfa39f573 |
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, verifies that an unbroken RTP stream is present, in | |
5 * GSM FR or EFR codec, and writes the FR/EFR frame stream to our | |
6 * extended-libgsm format binary file, to be fed to decoding test | |
7 * programs. | |
8 */ | |
9 | |
10 #include <sys/types.h> | |
11 #include <sys/socket.h> | |
12 #include <netinet/in.h> | |
13 #include <arpa/inet.h> | |
14 #include <stdio.h> | |
15 #include <stdlib.h> | |
16 #include <string.h> | |
17 #include <strings.h> | |
18 #include <pcap/pcap.h> | |
19 | |
20 static pcap_t *pcap; | |
21 static in_addr_t match_ip_addr; | |
22 static u_short match_udp_port; | |
23 static unsigned iphdr_addr_offset, udphdr_port_offset; | |
24 static unsigned link_hdr_len, ethertype_offset; | |
25 static FILE *outfile; | |
26 static int stream_init_flag; | |
27 static unsigned last_seq, last_tstamp, stream_ssrc; | |
28 | |
29 static void | |
30 check_dl_type() | |
31 { | |
32 int dltype; | |
33 | |
34 dltype = pcap_datalink(pcap); | |
35 switch (dltype) { | |
36 case DLT_EN10MB: | |
37 link_hdr_len = 14; | |
38 ethertype_offset = 12; | |
39 break; | |
40 case DLT_RAW: | |
41 link_hdr_len = 0; | |
42 break; | |
43 case DLT_LINUX_SLL: | |
44 link_hdr_len = 16; | |
45 ethertype_offset = 14; | |
46 break; | |
47 default: | |
48 fprintf(stderr, "error: unsupported data link type %d\n", | |
49 dltype); | |
50 exit(1); | |
51 } | |
52 } | |
53 | |
54 static void | |
55 rtp_stream_logic(rtp_hdr, pkt_idx) | |
56 u_char *rtp_hdr; | |
57 unsigned pkt_idx; | |
58 { | |
59 unsigned cur_seq, cur_tstamp, cur_ssrc; | |
60 | |
61 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3]; | |
62 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) | | |
63 (rtp_hdr[6] << 8) | rtp_hdr[7]; | |
64 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) | | |
65 (rtp_hdr[10] << 8) | rtp_hdr[11]; | |
66 if (stream_init_flag) { | |
67 if (cur_ssrc != stream_ssrc) { | |
68 fprintf(stderr, | |
69 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
70 pkt_idx, stream_ssrc, cur_ssrc); | |
71 exit(1); | |
72 } | |
73 if (cur_seq != last_seq + 1 && | |
74 (cur_seq != 0 || last_seq != 0xFFFF)) { | |
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 switch (payload_len) { | |
144 case 2: | |
145 if (pkt[40] == 0xBF) | |
146 break; | |
147 goto bad_payload; | |
148 case 31: | |
149 if ((pkt[40] & 0xF0) == 0xC0) | |
150 break; | |
151 goto bad_payload; | |
152 case 33: | |
153 if ((pkt[40] & 0xF0) == 0xD0) | |
154 break; | |
155 /* FALL THRU */ | |
156 default: | |
157 bad_payload: | |
158 fprintf(stderr, "error in packet #%u: unsupported payload\n", | |
159 pkt_idx); | |
160 exit(1); | |
161 } | |
162 fwrite(pkt + 40, 1, payload_len, outfile); | |
163 } | |
164 | |
165 main(argc, argv) | |
166 char **argv; | |
167 { | |
168 char errbuf[PCAP_ERRBUF_SIZE]; | |
169 u_char *pkt; | |
170 struct pcap_pkthdr pkthdr; | |
171 unsigned pkt_idx; | |
172 | |
173 if (argc != 6) { | |
174 fprintf(stderr, | |
175 "usage: %s pcap-file src|dest ip-addr udp-port outfile\n", | |
176 argv[0]); | |
177 exit(1); | |
178 } | |
179 pcap = pcap_open_offline(argv[1], errbuf); | |
180 if (!pcap) { | |
181 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
182 exit(1); | |
183 } | |
184 check_dl_type(); | |
185 if (!strcmp(argv[2], "src")) { | |
186 iphdr_addr_offset = 12; | |
187 udphdr_port_offset = 0; | |
188 } else if (!strcmp(argv[2], "dest")) { | |
189 iphdr_addr_offset = 16; | |
190 udphdr_port_offset = 2; | |
191 } else { | |
192 fprintf(stderr, | |
193 "error: direction argument must be \"src\" or \"dest\"\n"); | |
194 exit(1); | |
195 } | |
196 match_ip_addr = inet_addr(argv[3]); | |
197 if (match_ip_addr == INADDR_NONE) { | |
198 fprintf(stderr, "error: IP address argument is invalid\n"); | |
199 exit(1); | |
200 } | |
201 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
202 outfile = fopen(argv[5], "w"); | |
203 if (!outfile) { | |
204 perror(argv[5]); | |
205 exit(1); | |
206 } | |
207 for (pkt_idx = 0; ; pkt_idx++) { | |
208 pkt = pcap_next(pcap, &pkthdr); | |
209 if (!pkt) | |
210 break; | |
211 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx); | |
212 } | |
213 if (!stream_init_flag) { | |
214 fprintf(stderr, "error: specified RTP stream not found\n"); | |
215 exit(1); | |
216 } | |
217 fclose(outfile); | |
218 exit(0); | |
219 } |