FreeCalypso > hg > rtp-debug-utils
comparison rtp-cont-check.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 |
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 checks its continuity: verifies that the sequence | |
| 5 * number always increments by 1 and that the timestamp always | |
| 6 * increments by 160 units. Finally, this program prints out | |
| 7 * the minimum and maximum observed capture time deltas between | |
| 8 * successive packets of the stream. | |
| 9 * | |
| 10 * The codec can be anything, and this program can also be used to | |
| 11 * check continuity of RTP streams coming from PSTN/SIP, but the | |
| 12 * core assumption is that packets must arrive every 20 ms, with | |
| 13 * the timestamp field incrementing by 160 units each time. | |
| 14 */ | |
| 15 | |
| 16 #include <sys/types.h> | |
| 17 #include <sys/socket.h> | |
| 18 #include <sys/time.h> | |
| 19 #include <netinet/in.h> | |
| 20 #include <arpa/inet.h> | |
| 21 #include <stdio.h> | |
| 22 #include <stdlib.h> | |
| 23 #include <string.h> | |
| 24 #include <strings.h> | |
| 25 #include <pcap/pcap.h> | |
| 26 | |
| 27 static pcap_t *pcap; | |
| 28 static in_addr_t match_ip_addr; | |
| 29 static u_short match_udp_port; | |
| 30 static unsigned iphdr_addr_offset, udphdr_port_offset; | |
| 31 static unsigned link_hdr_len, ethertype_offset; | |
| 32 static int stream_init_flag, deltat_init_flag; | |
| 33 static unsigned last_seq, last_tstamp, stream_ssrc; | |
| 34 static struct timeval last_pkt_time; | |
| 35 static unsigned deltat_min, deltat_max; | |
| 36 | |
| 37 static void | |
| 38 check_dl_type() | |
| 39 { | |
| 40 int dltype; | |
| 41 | |
| 42 dltype = pcap_datalink(pcap); | |
| 43 switch (dltype) { | |
| 44 case DLT_EN10MB: | |
| 45 link_hdr_len = 14; | |
| 46 ethertype_offset = 12; | |
| 47 break; | |
| 48 case DLT_RAW: | |
| 49 link_hdr_len = 0; | |
| 50 break; | |
| 51 case DLT_LINUX_SLL: | |
| 52 link_hdr_len = 16; | |
| 53 ethertype_offset = 14; | |
| 54 break; | |
| 55 default: | |
| 56 fprintf(stderr, "error: unsupported data link type %d\n", | |
| 57 dltype); | |
| 58 exit(1); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 static void | |
| 63 rtp_stream_logic(rtp_hdr, pkt_idx, pkt_time) | |
| 64 u_char *rtp_hdr; | |
| 65 unsigned pkt_idx; | |
| 66 struct timeval *pkt_time; | |
| 67 { | |
| 68 unsigned cur_seq, cur_tstamp, cur_ssrc; | |
| 69 struct timeval deltat; | |
| 70 | |
| 71 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3]; | |
| 72 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) | | |
| 73 (rtp_hdr[6] << 8) | rtp_hdr[7]; | |
| 74 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) | | |
| 75 (rtp_hdr[10] << 8) | rtp_hdr[11]; | |
| 76 if (stream_init_flag) { | |
| 77 if (cur_ssrc != stream_ssrc) { | |
| 78 fprintf(stderr, | |
| 79 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
| 80 pkt_idx, stream_ssrc, cur_ssrc); | |
| 81 exit(1); | |
| 82 } | |
| 83 if (cur_seq != last_seq + 1 && | |
| 84 (cur_seq != 0 || last_seq != 0xFFFF)) { | |
| 85 fprintf(stderr, | |
| 86 "error in packet #%u: seq break from 0x%04X to 0x%04X\n", | |
| 87 pkt_idx, last_seq, cur_seq); | |
| 88 exit(1); | |
| 89 } | |
| 90 if (cur_tstamp != last_tstamp + 160) { | |
| 91 fprintf(stderr, | |
| 92 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n", | |
| 93 pkt_idx, last_tstamp, cur_tstamp); | |
| 94 exit(1); | |
| 95 } | |
| 96 if (timercmp(pkt_time, &last_pkt_time, <)) { | |
| 97 fprintf(stderr, | |
| 98 "packet #%u timing error: Rx time goes backward\n", | |
| 99 pkt_idx); | |
| 100 exit(1); | |
| 101 } | |
| 102 timersub(pkt_time, &last_pkt_time, &deltat); | |
| 103 if (deltat.tv_sec) { | |
| 104 fprintf(stderr, | |
| 105 "packet #%u timing error: Rx time gap >= 1 s\n", | |
| 106 pkt_idx); | |
| 107 exit(1); | |
| 108 } | |
| 109 if (deltat_init_flag) { | |
| 110 if (deltat.tv_usec < deltat_min) | |
| 111 deltat_min = deltat.tv_usec; | |
| 112 if (deltat.tv_usec > deltat_max) | |
| 113 deltat_max = deltat.tv_usec; | |
| 114 } else { | |
| 115 deltat_min = deltat.tv_usec; | |
| 116 deltat_max = deltat.tv_usec; | |
| 117 deltat_init_flag = 1; | |
| 118 } | |
| 119 } else { | |
| 120 stream_init_flag = 1; | |
| 121 stream_ssrc = cur_ssrc; | |
| 122 } | |
| 123 last_seq = cur_seq; | |
| 124 last_tstamp = cur_tstamp; | |
| 125 bcopy(pkt_time, &last_pkt_time, sizeof(struct timeval)); | |
| 126 } | |
| 127 | |
| 128 static void | |
| 129 process_packet(pkt, caplen, pkt_idx, pkt_time) | |
| 130 u_char *pkt; | |
| 131 unsigned caplen, pkt_idx; | |
| 132 struct timeval *pkt_time; | |
| 133 { | |
| 134 unsigned udplen; | |
| 135 | |
| 136 if (caplen < link_hdr_len + 28) | |
| 137 return; | |
| 138 if (link_hdr_len) { | |
| 139 if (pkt[ethertype_offset] != 0x08) | |
| 140 return; | |
| 141 if (pkt[ethertype_offset+1] != 0x00) | |
| 142 return; | |
| 143 pkt += link_hdr_len; | |
| 144 caplen -= link_hdr_len; | |
| 145 } | |
| 146 /* check IP header */ | |
| 147 if (pkt[0] != 0x45) | |
| 148 return; | |
| 149 if (pkt[9] != 17) /* UDP */ | |
| 150 return; | |
| 151 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4)) | |
| 152 return; | |
| 153 /* check UDP header */ | |
| 154 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2)) | |
| 155 return; | |
| 156 /* it is our target - now scrutinize it */ | |
| 157 udplen = (pkt[24] << 8) | pkt[25]; | |
| 158 if (caplen < udplen + 20) { | |
| 159 fprintf(stderr, | |
| 160 "error: packet #%u is truncated in the capture\n", | |
| 161 pkt_idx); | |
| 162 exit(1); | |
| 163 } | |
| 164 if (udplen < 20) { | |
| 165 fprintf(stderr, | |
| 166 "error in packet #%u: UDP length is too short for RTP header\n", | |
| 167 pkt_idx); | |
| 168 exit(1); | |
| 169 } | |
| 170 if (pkt[28] != 0x80) { | |
| 171 fprintf(stderr, | |
| 172 "error in packet #%u: unsupported RTP header structure\n", | |
| 173 pkt_idx); | |
| 174 exit(1); | |
| 175 } | |
| 176 rtp_stream_logic(pkt + 28, pkt_idx, pkt_time); | |
| 177 } | |
| 178 | |
| 179 main(argc, argv) | |
| 180 char **argv; | |
| 181 { | |
| 182 char errbuf[PCAP_ERRBUF_SIZE]; | |
| 183 u_char *pkt; | |
| 184 struct pcap_pkthdr pkthdr; | |
| 185 unsigned pkt_idx; | |
| 186 | |
| 187 if (argc != 5) { | |
| 188 fprintf(stderr, | |
| 189 "usage: %s pcap-file src|dest ip-addr udp-port\n", | |
| 190 argv[0]); | |
| 191 exit(1); | |
| 192 } | |
| 193 pcap = pcap_open_offline(argv[1], errbuf); | |
| 194 if (!pcap) { | |
| 195 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
| 196 exit(1); | |
| 197 } | |
| 198 check_dl_type(); | |
| 199 if (!strcmp(argv[2], "src")) { | |
| 200 iphdr_addr_offset = 12; | |
| 201 udphdr_port_offset = 0; | |
| 202 } else if (!strcmp(argv[2], "dest")) { | |
| 203 iphdr_addr_offset = 16; | |
| 204 udphdr_port_offset = 2; | |
| 205 } else { | |
| 206 fprintf(stderr, | |
| 207 "error: direction argument must be \"src\" or \"dest\"\n"); | |
| 208 exit(1); | |
| 209 } | |
| 210 match_ip_addr = inet_addr(argv[3]); | |
| 211 if (match_ip_addr == INADDR_NONE) { | |
| 212 fprintf(stderr, "error: IP address argument is invalid\n"); | |
| 213 exit(1); | |
| 214 } | |
| 215 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
| 216 for (pkt_idx = 0; ; pkt_idx++) { | |
| 217 pkt = pcap_next(pcap, &pkthdr); | |
| 218 if (!pkt) | |
| 219 break; | |
| 220 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx, | |
| 221 &pkthdr.ts); | |
| 222 } | |
| 223 if (!stream_init_flag) { | |
| 224 fprintf(stderr, "error: specified RTP stream not found\n"); | |
| 225 exit(1); | |
| 226 } | |
| 227 if (!deltat_init_flag) { | |
| 228 fprintf(stderr, "error: found only one matching packet\n"); | |
| 229 exit(1); | |
| 230 } | |
| 231 printf("Minimum time delta: %u us\n", deltat_min); | |
| 232 printf("Maximum time delta: %u us\n", deltat_max); | |
| 233 exit(0); | |
| 234 } |
