-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtp-pcap-replay.cpp
414 lines (357 loc) · 12.4 KB
/
rtp-pcap-replay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* MIT License
*
* Copyright (c) 2020 vzvca
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Groupsock.hh>
#include <GroupsockHelper.hh>
#include <BasicUsageEnvironment.hh>
#include <iostream>
#include <cstdarg>
#include <stdio.h>
#include <pcap.h>
#include <endian.h>
#define G_LITTLE_ENDIAN 1234
#define G_BIG_ENDIAN 4321
#define G_BYTE_ORDER 1234
typedef struct _RTPHeader
{
//first byte
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
unsigned int CC:4;// CC field
unsigned int X:1;// /* X field */
unsigned int P:1;// /* padding flag */
unsigned int version:2;
#elif G_BYTE_ORDER == G_BIG_ENDIAN
unsigned int version:2;
unsigned int P:1;// /* padding flag */
unsigned int X:1;// /* X field */
unsigned int CC:4;// /* CC field*/
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
//second byte
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
unsigned int PT:7; /* PT field */
unsigned int M:1; /* M field */
#elif G_BYTE_ORDER == G_BIG_ENDIAN
unsigned int M:1; /* M field */
unsigned int PT:7; /* PT field */
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
uint16_t seq_num; /* length of the recovery */
uint32_t TS; /* Timestamp */
uint32_t ssrc;
} RTPHeader; //12 bytes
// global variables
char errbuf[PCAP_ERRBUF_SIZE];
const char* filter = NULL;
const char* pcapfile = "capture.pcap";
const char* dstAddressString = "232.0.1.1";
uint16_t rtpPortNum = 1500;
uint8_t verbose = 0;
uint8_t loop = 0;
// linux cooked frame offset
//uint32_t offset = 3*16-4;
// ethernet frame
//uint32_t offset = 3*16-6;
uint32_t offset = 0;
// global constants
const uint8_t ttl = 4;
// main live555 object
UsageEnvironment* env = NULL;
TaskScheduler* scheduler = NULL;
Groupsock* rtpGroupsock = NULL;
struct bpf_program pcapfilter;
/* --------------------------------------------------------------------------
* Data structure used
* --------------------------------------------------------------------------*/
struct todo {
pcap_t *fp;
struct pcap_pkthdr *header;
const u_char *pkt_data;
};
/* --------------------------------------------------------------------------
* Print an help message and leave
* --------------------------------------------------------------------------*/
void usage( const char *progname, const char *fmt, ...)
{
if ( fmt != NULL ) {
char tmp[1024];
va_list va;
va_start(va, fmt);
vsprintf(tmp, fmt, va);
va_end(va);
std::cerr << progname << " error: " << tmp << std::endl;
}
std::cout << "Usage: ";
std::cout << progname << " [-v] [-a mcastaddr] [-p mcastport] [-c pcapfile]" << std::endl;
std::cout << "\t -v : verbose " << std::endl;
std::cout << "\t -a mcastaddr : multicast address (default "<< dstAddressString << ")" << std::endl;
std::cout << "\t -p mcastport : multicast port (default "<< rtpPortNum << ")" << std::endl;
std::cout << "\t -c file : pcap file (default "<< pcapfile << ")" << std::endl;
std::cout << "\t -f filter : pcap filter (default none)" << std::endl;
std::cout << "\t -l num : how many times to play the file, 0 means infinite looping (default " << (loop+1) << ")" << std::endl;
std::cout << "\t -o offset : offset of payload in pcap packet (44 is good for linux cooked tcpdump captures)" << std::endl;
exit( (fmt != NULL) ? 1 : 0);
}
/* --------------------------------------------------------------------------
* Parse command line switches
* --------------------------------------------------------------------------*/
void parse( int argc, char **argv)
{
// decode parameters
int c = 0;
while ((c = getopt (argc, argv, "hva:p:c:f:l:o:")) != -1)
{
switch (c)
{
case 'a': dstAddressString = optarg; break;
case 'c': pcapfile = optarg; break;
case 'f': filter = optarg; break;
case 'p': rtpPortNum = atoi(optarg); break;
case 'v': verbose = 1; break;
case 'l': loop = atoi(optarg)-1; break;
case 'o': offset = atoi(optarg); break;
case 'h': usage(argv[0], NULL); break;
default: usage(argv[0], "syntax error");
}
}
if ( optind < argc )
{
usage(argv[0], "unable to process command line.");
}
}
/* --------------------------------------------------------------------------
* program initialisation
* --------------------------------------------------------------------------*/
void live( int argc, char **argv)
{
parse(argc, argv);
scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
struct in_addr dstAddress;
dstAddress.s_addr = ::our_inet_addr(dstAddressString);
rtpGroupsock = new Groupsock(*env,dstAddress,::Port(rtpPortNum),ttl);
if ( ::IsMulticastAddress(::our_inet_addr(dstAddressString)) ) {
rtpGroupsock->multicastSendOnly();
}
}
/* --------------------------------------------------------------------------
* Compute offset from DLT
* --------------------------------------------------------------------------*/
int get_offset(pcap_t *pcap)
{
if (offset > 0) {
return offset;
}
/* compute offset */
switch(pcap_datalink(pcap)) {
case DLT_EN10MB:
return 3*16-6;
case DLT_LINUX_SLL:
default:
return 3*16-4;
}
}
/* --------------------------------------------------------------------------
* Open capture and compile filter
* --------------------------------------------------------------------------*/
void open_capture(pcap_t **pfp)
{
int reopen = (*pfp != NULL);
// open capture
if ((*pfp = pcap_open_offline(pcapfile, errbuf)) == NULL) {
fprintf(stderr,"\nUnable to open the file '%s'.\n", pcapfile);
exit(1);
}
if ( verbose ) {
*env << "Capture file reopened.\n";
}
/* compile pcap filter if given */
if ( filter != NULL && strlen(filter) > 0 ) {
if (reopen) {
pcap_freecode( &pcapfilter);
}
if ( pcap_compile(*pfp, &pcapfilter, filter, 0, PCAP_NETMASK_UNKNOWN) == -1 ) {
fprintf(stderr,"\nUnable to compile filter '%s'.\n", filter);
exit(1);
}
if ( pcap_setfilter(*pfp, &pcapfilter) == -1) {
fprintf(stderr,"\nUnable to install filter '%s'.\n", filter);
exit(1);
}
}
}
/* --------------------------------------------------------------------------
* Get next packet - reopen file if needed
* --------------------------------------------------------------------------*/
void next_packet( pcap_t **pfp, struct pcap_pkthdr **phdr, const u_char ** pdata)
{
int res;
res = pcap_next_ex(*pfp, phdr, pdata);
if (res <= 0) {
// compute remaining loop number
// -1 means infinite loop
if ( loop > 0 ) loop--;
// if looping over file content and eof reached reopen
if ( (loop != 0) && (res == -2)) {
// close capture
if ( filter ) {
pcap_freecode( &pcapfilter);
}
pcap_close(*pfp);
// open capture
open_capture(pfp);
next_packet(pfp, phdr, pdata);
return;
}
else {
// it was an error
error:
printf("Error reading the packets: %s\n", pcap_geterr(*pfp));
exit(1);
}
}
}
/* --------------------------------------------------------------------------
* Single processing step
* --------------------------------------------------------------------------*/
void step( void *clientData )
{
static int npkts = 0; // tracks number of played packets
static uint32_t rtpts = 0; // computed RTP timestamp - used while looping
static int rtpseq = 0; // computed RTP sequence number - used for looping
static uint32_t lastrtpts = 0; // remember last RTP timestamp read from file
static int rtptsinc = 0; // remember RTP timestamp increment
static int64_t usecs = 0; // remember last number of usec to wait
struct todo *task= (struct todo*) clientData;
struct pcap_pkthdr *header = task->header;
const u_char *pkt_data = task->pkt_data;
struct timeval ts, next_send_time, before_send, after_send;
if ( header == NULL ) {
next_packet( &task->fp, &header, &pkt_data);
}
// if looping over file content
// we need to recompute the RTP timestamp and sequence number
if ( loop != 0 ) {
RTPHeader *rtph = (RTPHeader*) (pkt_data + get_offset(task->fp));
// compute RTP timestamp
// RTP header used big endian integers
uint32_t hts = rtph->TS;
hts = be32toh(hts);
if ( hts != lastrtpts ) {
if ( rtpts == 0 ) {
rtpts = hts;
}
rtpts += rtptsinc;
if ( rtptsinc == 0 && lastrtpts > 0) {
rtptsinc = hts - lastrtpts;
}
lastrtpts = hts;
}
hts = rtpts;
rtph->TS = htobe32(hts);
// compute RTP sequence number
// RTP header used big endian integers
if ( rtpseq == 0 ) {
rtpseq = be16toh(rtph->seq_num);
}
else {
rtpseq++;
}
rtph->seq_num = htobe16(rtpseq);
}
// increase number of processed packets
// and print i
npkts = npkts + 1;
if ( verbose ) {
if ( (npkts % 100) == 0 ) {
*env << "Processing packets # " << npkts << " TS: " << rtpts << " rtptsinc : " << rtptsinc << " seq: " << rtpseq << "\n";
}
}
// remember timestamp of packet to send
ts = header->ts;
// Send the packet
// Note we have to move forward !!
int len = header->caplen-get_offset(task->fp);
gettimeofday(&before_send, NULL);
rtpGroupsock->output(*env, (unsigned char*) (pkt_data + get_offset(task->fp)), len);
// schedule next call
// get next packet from capture
next_packet(&task->fp, &task->header, &task->pkt_data);
// compute usec delta to apply
gettimeofday(&after_send, NULL);
after_send.tv_usec -= before_send.tv_usec;
after_send.tv_sec -= before_send.tv_sec;
if ( after_send.tv_usec < 0 ) {
after_send.tv_sec --;
after_send.tv_usec += 1000000;
}
// Figure out the time at which the next packet should be sent, based
// on the duration of the payload that we just read:
next_send_time.tv_usec = task->header->ts.tv_usec - ts.tv_usec - after_send.tv_usec;
next_send_time.tv_sec = task->header->ts.tv_sec - ts.tv_sec - after_send.tv_sec;
if ( next_send_time.tv_usec < 0 ) {
next_send_time.tv_sec --;
next_send_time.tv_usec += 1000000;
}
// if next_send_time.tv_sec < 0 and looping over file
// reuse last usecs. Note that we test the opposite
if ( next_send_time.tv_sec == 0 || loop == 0 ) {
usecs = next_send_time.tv_sec*1000000 + next_send_time.tv_usec;
if ( usecs < 0 ) {
if ( verbose ) {
std::cerr << "being late (" << -usecs << " usec)\n";
}
usecs = 0;
}
}
// Delay this amount of time:
scheduler->scheduleDelayedTask(usecs, (TaskFunc*)step, clientData);
}
/* --------------------------------------------------------------------------
* Main program
* --------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
pcap_t *fp = NULL;
struct todo task;
/* init */
live(argc, argv);
/* Open the capture file */
open_capture(&fp);
/* fill task data */
task.fp = fp;
task.header = NULL;
task.pkt_data = NULL;
/* schedule next task */
scheduler->scheduleDelayedTask(0, (TaskFunc*)step, &task);
/* enter live555 event loop */
scheduler->doEventLoop();
/* cleanup and exit */
if ( filter ) {
pcap_freecode( &pcapfilter);
}
pcap_close(fp);
return 0;
}