FreeCalypso > hg > rtp-debug-utils
comparison pcap-study/rtp-gsmfr-dump.c @ 10:e686bc92c7d8
revamp for new subdir structure and configure script
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Wed, 15 May 2024 01:44:46 +0000 |
| parents | rtp-gsmfr-dump.c@7c85a7a913f7 |
| children | 96b37cef5020 |
comparison
equal
deleted
inserted
replaced
| 9:c00510e1ae8b | 10:e686bc92c7d8 |
|---|---|
| 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 printf( | |
| 75 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
| 76 pkt_idx, stream_ssrc, cur_ssrc); | |
| 77 } else if (cur_seq != last_seq + 1 && | |
| 78 (cur_seq != 0 || last_seq != 0xFFFF)) { | |
| 79 printf( | |
| 80 "error in packet #%u: seq break from 0x%04X to 0x%04X\n", | |
| 81 pkt_idx, last_seq, cur_seq); | |
| 82 } else if (cur_tstamp != last_tstamp + 160) { | |
| 83 printf( | |
| 84 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n", | |
| 85 pkt_idx, last_tstamp, cur_tstamp); | |
| 86 } | |
| 87 } else | |
| 88 stream_init_flag = 1; | |
| 89 last_seq = cur_seq; | |
| 90 last_tstamp = cur_tstamp; | |
| 91 stream_ssrc = cur_ssrc; | |
| 92 } | |
| 93 | |
| 94 static void | |
| 95 process_payload(payload, payload_len) | |
| 96 u_char *payload; | |
| 97 unsigned payload_len; | |
| 98 { | |
| 99 gsm_signal params[76]; | |
| 100 int i, j, n; | |
| 101 | |
| 102 printf("seq 0x%04X ts 0x%08X: ", last_seq, last_tstamp); | |
| 103 switch (payload_len) { | |
| 104 case 0: | |
| 105 printf("zero-length payload\n"); | |
| 106 return; | |
| 107 case 1: | |
| 108 if ((payload[0] & 0xF6) != 0xE6) | |
| 109 break; | |
| 110 printf("TEH 0x%02X\n", payload[0]); | |
| 111 return; | |
| 112 case 2: | |
| 113 if (payload[0] != 0xBF) | |
| 114 break; | |
| 115 printf("old BFI TAF=%u\n", payload[1] & 1); | |
| 116 return; | |
| 117 case 31: | |
| 118 if ((payload[0] & 0xF0) != 0xC0) | |
| 119 break; | |
| 120 printf("std EFR SID=%d LPC", EFR_sid_classify(payload)); | |
| 121 EFR_frame2params(payload, params); | |
| 122 print_efr: | |
| 123 n = 0; | |
| 124 for (i = 0; i < 5; i++) | |
| 125 printf(" %d", params[n++]); | |
| 126 putchar('\n'); | |
| 127 for (i = 0; i < 4; i++) { | |
| 128 putchar(' '); | |
| 129 for (j = 0; j < 13; j++) | |
| 130 printf(" %d", params[n++]); | |
| 131 putchar('\n'); | |
| 132 } | |
| 133 return; | |
| 134 case 32: | |
| 135 if ((payload[0] & 0xF4) != 0xE0) | |
| 136 break; | |
| 137 if ((payload[1] & 0xF0) != 0xC0) | |
| 138 break; | |
| 139 printf("TEH 0x%02X EFR SID=%d LPC", payload[0], | |
| 140 EFR_sid_classify(payload+1)); | |
| 141 EFR_frame2params(payload+1, params); | |
| 142 goto print_efr; | |
| 143 case 33: | |
| 144 if ((payload[0] & 0xF0) != 0xD0) | |
| 145 break; | |
| 146 fputs("std FR", stdout); | |
| 147 gsm_explode(dummy_state, payload, params); | |
| 148 print_fr: | |
| 149 n = 0; | |
| 150 for (i = 0; i < 8; i++) | |
| 151 printf(" %d", params[n++]); | |
| 152 putchar('\n'); | |
| 153 for (i = 0; i < 4; i++) { | |
| 154 putchar(' '); | |
| 155 for (j = 0; j < 17; j++) | |
| 156 printf(" %d", params[n++]); | |
| 157 putchar('\n'); | |
| 158 } | |
| 159 return; | |
| 160 case 34: | |
| 161 if ((payload[0] & 0xF4) != 0xE0) | |
| 162 break; | |
| 163 if ((payload[1] & 0xF0) != 0xD0) | |
| 164 break; | |
| 165 printf("TEH 0x%02X FR", payload[0]); | |
| 166 gsm_explode(dummy_state, payload+1, params); | |
| 167 goto print_fr; | |
| 168 } | |
| 169 printf("unknown payload format, length=%u\n", payload_len); | |
| 170 } | |
| 171 | |
| 172 static void | |
| 173 process_packet(pkt, caplen, pkt_idx) | |
| 174 u_char *pkt; | |
| 175 unsigned caplen, pkt_idx; | |
| 176 { | |
| 177 unsigned udplen, payload_len; | |
| 178 | |
| 179 if (caplen < link_hdr_len + 28) | |
| 180 return; | |
| 181 if (link_hdr_len) { | |
| 182 if (pkt[ethertype_offset] != 0x08) | |
| 183 return; | |
| 184 if (pkt[ethertype_offset+1] != 0x00) | |
| 185 return; | |
| 186 pkt += link_hdr_len; | |
| 187 caplen -= link_hdr_len; | |
| 188 } | |
| 189 /* check IP header */ | |
| 190 if (pkt[0] != 0x45) | |
| 191 return; | |
| 192 if (pkt[9] != 17) /* UDP */ | |
| 193 return; | |
| 194 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4)) | |
| 195 return; | |
| 196 /* check UDP header */ | |
| 197 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2)) | |
| 198 return; | |
| 199 /* it is our target - now scrutinize it */ | |
| 200 udplen = (pkt[24] << 8) | pkt[25]; | |
| 201 if (caplen < udplen + 20) { | |
| 202 fprintf(stderr, | |
| 203 "error: packet #%u is truncated in the capture\n", | |
| 204 pkt_idx); | |
| 205 exit(1); | |
| 206 } | |
| 207 if (udplen < 20) { | |
| 208 fprintf(stderr, | |
| 209 "error in packet #%u: UDP length is too short for RTP header\n", | |
| 210 pkt_idx); | |
| 211 exit(1); | |
| 212 } | |
| 213 if (pkt[28] != 0x80) { | |
| 214 fprintf(stderr, | |
| 215 "error in packet #%u: unsupported RTP header structure\n", | |
| 216 pkt_idx); | |
| 217 exit(1); | |
| 218 } | |
| 219 rtp_stream_logic(pkt + 28, pkt_idx); | |
| 220 process_payload(pkt + 40, udplen - 20); | |
| 221 } | |
| 222 | |
| 223 main(argc, argv) | |
| 224 char **argv; | |
| 225 { | |
| 226 char errbuf[PCAP_ERRBUF_SIZE]; | |
| 227 u_char *pkt; | |
| 228 struct pcap_pkthdr pkthdr; | |
| 229 unsigned pkt_idx; | |
| 230 | |
| 231 if (argc != 5) { | |
| 232 fprintf(stderr, | |
| 233 "usage: %s pcap-file src|dest ip-addr udp-port\n", | |
| 234 argv[0]); | |
| 235 exit(1); | |
| 236 } | |
| 237 pcap = pcap_open_offline(argv[1], errbuf); | |
| 238 if (!pcap) { | |
| 239 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
| 240 exit(1); | |
| 241 } | |
| 242 check_dl_type(); | |
| 243 if (!strcmp(argv[2], "src")) { | |
| 244 iphdr_addr_offset = 12; | |
| 245 udphdr_port_offset = 0; | |
| 246 } else if (!strcmp(argv[2], "dest")) { | |
| 247 iphdr_addr_offset = 16; | |
| 248 udphdr_port_offset = 2; | |
| 249 } else { | |
| 250 fprintf(stderr, | |
| 251 "error: direction argument must be \"src\" or \"dest\"\n"); | |
| 252 exit(1); | |
| 253 } | |
| 254 match_ip_addr = inet_addr(argv[3]); | |
| 255 if (match_ip_addr == INADDR_NONE) { | |
| 256 fprintf(stderr, "error: IP address argument is invalid\n"); | |
| 257 exit(1); | |
| 258 } | |
| 259 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
| 260 dummy_state = gsm_create(); | |
| 261 if (!dummy_state) { | |
| 262 fprintf(stderr, "gsm_create() failed!\n"); | |
| 263 exit(1); | |
| 264 } | |
| 265 for (pkt_idx = 0; ; pkt_idx++) { | |
| 266 pkt = pcap_next(pcap, &pkthdr); | |
| 267 if (!pkt) | |
| 268 break; | |
| 269 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx); | |
| 270 } | |
| 271 if (!stream_init_flag) { | |
| 272 fprintf(stderr, "error: specified RTP stream not found\n"); | |
| 273 exit(1); | |
| 274 } | |
| 275 exit(0); | |
| 276 } |
