FreeCalypso > hg > rtp-debug-utils
comparison rtp-gsmfr-dump.c @ 3:50aa973a91ef
new program rtp-gsmfr-dump
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 16 Apr 2023 06:43:18 +0000 |
| parents | rtp-gsmfr-extr.c@210dfa39f573 |
| children | 7c85a7a913f7 |
comparison
equal
deleted
inserted
replaced
| 2:210dfa39f573 | 3:50aa973a91ef |
|---|---|
| 1 /* | |
| 2 * This program is a combination of rtp-gsmfr-extr and gsmrec-dump into | |
| 3 * one step: it reads a pcap file, extracts packets belonging to a | |
| 4 * particular RTP stream as identified by a source or destination | |
| 5 * IP:port, verifies that an unbroken RTP stream is present, which is | |
| 6 * expected to be in GSM FR or EFR codec, but instead of writing this | |
| 7 * FR/EFR frame stream to a gsmx file, decodes it like gsmrec-dump. | |
| 8 * | |
| 9 * The advantage of this program over running rtp-gsmfr-extr followed | |
| 10 * by gsmrec-dump is that TRAUlike Extension Headers (if present) are | |
| 11 * dumped for analysis, rather than discarded. | |
| 12 */ | |
| 13 | |
| 14 #include <sys/types.h> | |
| 15 #include <sys/socket.h> | |
| 16 #include <netinet/in.h> | |
| 17 #include <arpa/inet.h> | |
| 18 #include <stdio.h> | |
| 19 #include <stdlib.h> | |
| 20 #include <string.h> | |
| 21 #include <strings.h> | |
| 22 #include <pcap/pcap.h> | |
| 23 #include <gsm.h> | |
| 24 #include <gsm_efr.h> | |
| 25 | |
| 26 static pcap_t *pcap; | |
| 27 static in_addr_t match_ip_addr; | |
| 28 static u_short match_udp_port; | |
| 29 static unsigned iphdr_addr_offset, udphdr_port_offset; | |
| 30 static unsigned link_hdr_len, ethertype_offset; | |
| 31 static int stream_init_flag; | |
| 32 static unsigned last_seq, last_tstamp, stream_ssrc; | |
| 33 static gsm dummy_state; | |
| 34 | |
| 35 static void | |
| 36 check_dl_type() | |
| 37 { | |
| 38 int dltype; | |
| 39 | |
| 40 dltype = pcap_datalink(pcap); | |
| 41 switch (dltype) { | |
| 42 case DLT_EN10MB: | |
| 43 link_hdr_len = 14; | |
| 44 ethertype_offset = 12; | |
| 45 break; | |
| 46 case DLT_RAW: | |
| 47 link_hdr_len = 0; | |
| 48 break; | |
| 49 case DLT_LINUX_SLL: | |
| 50 link_hdr_len = 16; | |
| 51 ethertype_offset = 14; | |
| 52 break; | |
| 53 default: | |
| 54 fprintf(stderr, "error: unsupported data link type %d\n", | |
| 55 dltype); | |
| 56 exit(1); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 static void | |
| 61 rtp_stream_logic(rtp_hdr, pkt_idx) | |
| 62 u_char *rtp_hdr; | |
| 63 unsigned pkt_idx; | |
| 64 { | |
| 65 unsigned cur_seq, cur_tstamp, cur_ssrc; | |
| 66 | |
| 67 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3]; | |
| 68 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) | | |
| 69 (rtp_hdr[6] << 8) | rtp_hdr[7]; | |
| 70 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) | | |
| 71 (rtp_hdr[10] << 8) | rtp_hdr[11]; | |
| 72 if (stream_init_flag) { | |
| 73 if (cur_ssrc != stream_ssrc) { | |
| 74 fprintf(stderr, | |
| 75 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
| 76 pkt_idx, stream_ssrc, cur_ssrc); | |
| 77 exit(1); | |
| 78 } | |
| 79 if (cur_seq != last_seq + 1 && | |
| 80 (cur_seq != 0 || last_seq != 0xFFFF)) { | |
| 81 fprintf(stderr, | |
| 82 "error in packet #%u: seq break from 0x%04X to 0x%04X\n", | |
| 83 pkt_idx, last_seq, cur_seq); | |
| 84 exit(1); | |
| 85 } | |
| 86 if (cur_tstamp != last_tstamp + 160) { | |
| 87 fprintf(stderr, | |
| 88 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n", | |
| 89 pkt_idx, last_tstamp, cur_tstamp); | |
| 90 exit(1); | |
| 91 } | |
| 92 } else { | |
| 93 stream_init_flag = 1; | |
| 94 stream_ssrc = cur_ssrc; | |
| 95 } | |
| 96 last_seq = cur_seq; | |
| 97 last_tstamp = cur_tstamp; | |
| 98 } | |
| 99 | |
| 100 static void | |
| 101 process_payload(payload, payload_len) | |
| 102 u_char *payload; | |
| 103 unsigned payload_len; | |
| 104 { | |
| 105 gsm_signal params[76]; | |
| 106 int i, j, n; | |
| 107 | |
| 108 printf("seq 0x%04X ts 0x%08X: ", last_seq, last_tstamp); | |
| 109 switch (payload_len) { | |
| 110 case 0: | |
| 111 printf("zero-length payload\n"); | |
| 112 return; | |
| 113 case 1: | |
| 114 if ((payload[0] & 0xF6) != 0xE6) | |
| 115 break; | |
| 116 printf("TEH 0x%02X\n", payload[0]); | |
| 117 return; | |
| 118 case 2: | |
| 119 if (payload[0] != 0xBF) | |
| 120 break; | |
| 121 printf("old BFI TAF=%u\n", payload[1] & 1); | |
| 122 return; | |
| 123 case 31: | |
| 124 if ((payload[0] & 0xF0) != 0xC0) | |
| 125 break; | |
| 126 printf("std EFR SID=%d LPC", EFR_sid_classify(payload)); | |
| 127 EFR_frame2params(payload, params); | |
| 128 print_efr: | |
| 129 n = 0; | |
| 130 for (i = 0; i < 5; i++) | |
| 131 printf(" %d", params[n++]); | |
| 132 putchar('\n'); | |
| 133 for (i = 0; i < 4; i++) { | |
| 134 putchar(' '); | |
| 135 for (j = 0; j < 13; j++) | |
| 136 printf(" %d", params[n++]); | |
| 137 putchar('\n'); | |
| 138 } | |
| 139 return; | |
| 140 case 32: | |
| 141 if ((payload[0] & 0xF4) != 0xE0) | |
| 142 break; | |
| 143 if ((payload[1] & 0xF0) != 0xC0) | |
| 144 break; | |
| 145 printf("TEH 0x%02X EFR SID=%d LPC", payload[0], | |
| 146 EFR_sid_classify(payload+1)); | |
| 147 EFR_frame2params(payload+1, params); | |
| 148 goto print_efr; | |
| 149 case 33: | |
| 150 if ((payload[0] & 0xF0) != 0xD0) | |
| 151 break; | |
| 152 fputs("std FR", stdout); | |
| 153 gsm_explode(dummy_state, payload, params); | |
| 154 print_fr: | |
| 155 n = 0; | |
| 156 for (i = 0; i < 8; i++) | |
| 157 printf(" %d", params[n++]); | |
| 158 putchar('\n'); | |
| 159 for (i = 0; i < 4; i++) { | |
| 160 putchar(' '); | |
| 161 for (j = 0; j < 17; j++) | |
| 162 printf(" %d", params[n++]); | |
| 163 putchar('\n'); | |
| 164 } | |
| 165 return; | |
| 166 case 34: | |
| 167 if ((payload[0] & 0xF4) != 0xE0) | |
| 168 break; | |
| 169 if ((payload[1] & 0xF0) != 0xD0) | |
| 170 break; | |
| 171 printf("TEH 0x%02X FR", payload[0]); | |
| 172 gsm_explode(dummy_state, payload+1, params); | |
| 173 goto print_fr; | |
| 174 } | |
| 175 printf("unknown payload format, length=%u\n", payload_len); | |
| 176 } | |
| 177 | |
| 178 static void | |
| 179 process_packet(pkt, caplen, pkt_idx) | |
| 180 u_char *pkt; | |
| 181 unsigned caplen, pkt_idx; | |
| 182 { | |
| 183 unsigned udplen, payload_len; | |
| 184 | |
| 185 if (caplen < link_hdr_len + 28) | |
| 186 return; | |
| 187 if (link_hdr_len) { | |
| 188 if (pkt[ethertype_offset] != 0x08) | |
| 189 return; | |
| 190 if (pkt[ethertype_offset+1] != 0x00) | |
| 191 return; | |
| 192 pkt += link_hdr_len; | |
| 193 caplen -= link_hdr_len; | |
| 194 } | |
| 195 /* check IP header */ | |
| 196 if (pkt[0] != 0x45) | |
| 197 return; | |
| 198 if (pkt[9] != 17) /* UDP */ | |
| 199 return; | |
| 200 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4)) | |
| 201 return; | |
| 202 /* check UDP header */ | |
| 203 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2)) | |
| 204 return; | |
| 205 /* it is our target - now scrutinize it */ | |
| 206 udplen = (pkt[24] << 8) | pkt[25]; | |
| 207 if (caplen < udplen + 20) { | |
| 208 fprintf(stderr, | |
| 209 "error: packet #%u is truncated in the capture\n", | |
| 210 pkt_idx); | |
| 211 exit(1); | |
| 212 } | |
| 213 if (udplen < 20) { | |
| 214 fprintf(stderr, | |
| 215 "error in packet #%u: UDP length is too short for RTP header\n", | |
| 216 pkt_idx); | |
| 217 exit(1); | |
| 218 } | |
| 219 if (pkt[28] != 0x80) { | |
| 220 fprintf(stderr, | |
| 221 "error in packet #%u: unsupported RTP header structure\n", | |
| 222 pkt_idx); | |
| 223 exit(1); | |
| 224 } | |
| 225 rtp_stream_logic(pkt + 28, pkt_idx); | |
| 226 process_payload(pkt + 40, udplen - 20); | |
| 227 } | |
| 228 | |
| 229 main(argc, argv) | |
| 230 char **argv; | |
| 231 { | |
| 232 char errbuf[PCAP_ERRBUF_SIZE]; | |
| 233 u_char *pkt; | |
| 234 struct pcap_pkthdr pkthdr; | |
| 235 unsigned pkt_idx; | |
| 236 | |
| 237 if (argc != 5) { | |
| 238 fprintf(stderr, | |
| 239 "usage: %s pcap-file src|dest ip-addr udp-port\n", | |
| 240 argv[0]); | |
| 241 exit(1); | |
| 242 } | |
| 243 pcap = pcap_open_offline(argv[1], errbuf); | |
| 244 if (!pcap) { | |
| 245 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
| 246 exit(1); | |
| 247 } | |
| 248 check_dl_type(); | |
| 249 if (!strcmp(argv[2], "src")) { | |
| 250 iphdr_addr_offset = 12; | |
| 251 udphdr_port_offset = 0; | |
| 252 } else if (!strcmp(argv[2], "dest")) { | |
| 253 iphdr_addr_offset = 16; | |
| 254 udphdr_port_offset = 2; | |
| 255 } else { | |
| 256 fprintf(stderr, | |
| 257 "error: direction argument must be \"src\" or \"dest\"\n"); | |
| 258 exit(1); | |
| 259 } | |
| 260 match_ip_addr = inet_addr(argv[3]); | |
| 261 if (match_ip_addr == INADDR_NONE) { | |
| 262 fprintf(stderr, "error: IP address argument is invalid\n"); | |
| 263 exit(1); | |
| 264 } | |
| 265 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
| 266 dummy_state = gsm_create(); | |
| 267 if (!dummy_state) { | |
| 268 fprintf(stderr, "gsm_create() failed!\n"); | |
| 269 exit(1); | |
| 270 } | |
| 271 for (pkt_idx = 0; ; pkt_idx++) { | |
| 272 pkt = pcap_next(pcap, &pkthdr); | |
| 273 if (!pkt) | |
| 274 break; | |
| 275 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx); | |
| 276 } | |
| 277 if (!stream_init_flag) { | |
| 278 fprintf(stderr, "error: specified RTP stream not found\n"); | |
| 279 exit(1); | |
| 280 } | |
| 281 exit(0); | |
| 282 } |
