annotate pcap-study/rtp-cont-check.c @ 11:8b047c9bfe5f

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