-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathrtptrans.c
525 lines (479 loc) · 15.1 KB
/
rtptrans.c
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* (c) 1998-2018 by Columbia University; all rights reserved
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "sysdep.h"
#include "rtp.h"
#include "rtpdump.h"
#include "notify.h"
#include "multimer.h"
#include "vat.h"
extern int hpt(char*, struct sockaddr_in*, unsigned char*);
#define PAD(x,n) (((n) - ((x) & (n-1))) & (n-1))
#define MAX_HOST 10
static int debug = 0;
static int hostc = 0;
static int multi_sock[3] = {0,0,0};
static struct {
int sock;
struct sockaddr_in sin;
} side[MAX_HOST][3]; /* [host][proto] */
/*
* create a linked list for traversing the sequence numbers of the different
* streams arriving over a multicast link to a unicast network
*/
typedef struct stream_id{
int seq;
int addr;
int next_ts;
struct stream_id* next;
} STREAM;
typedef struct stream_id stream;
/*
* We need to keep in memory the sequence number of the last sent packet
* for each data stream.
*/
static stream *head; /* start of list */
static stream *middle; /* middle of the list, keeping this pointer reduces the
* avarage searching path to a 1/4 of the list instead of
* 1/2.
*/
static stream *last; /* Last seen stream, as the probability that the next
* packet will belong to the last seen one this reduces
* the searching path even further.
*/
static int list_len;
static int create_stream(int addr, int next)
{
stream *elem;
int i;
stream* new_stream=( stream *) malloc(sizeof( stream));
if(new_stream==NULL) {
perror("can not create a new steam identifier ");
exit (0);
}
/* init created stream */
new_stream->next=NULL;
new_stream->addr=addr;
new_stream->seq=addr;
new_stream->next_ts=next;
if(head==NULL) { /* initialize the list */
head=new_stream;
middle=new_stream;
list_len=1;
new_stream->seq=rand();
last=new_stream;
return new_stream->seq;
}
new_stream->seq=rand(); /* init the first sequence number for this stream */
if((list_len++)%2) {
for(elem=head, i=1; i<(list_len/2)+(list_len%2);i++,elem=elem->next);
middle=elem;
}
/* organize the list in order of the ssrc */
if(head->addr>addr) {
new_stream->next=head;
head=new_stream;
last=new_stream;
return new_stream->seq;
}
for(elem=head;elem->next!=NULL;elem=elem->next) {
if(elem->next->addr>addr) {
new_stream->next=elem->next;
elem->next=new_stream;
last=new_stream;
return new_stream->seq;
}
}
;
if(elem->next==NULL) {
elem->next=new_stream;
last=new_stream;
return new_stream->seq;
}
return new_stream->seq;
}
/*
* Add a stream to the list.
*/
static int find_stream(int addr, int ts, int next, int m)
{
stream *element;
/* packet belongs to the last seen stream */
if ((last!=NULL)&&(addr==last->addr)) {
last->seq+=1;
if (ts != last->next_ts && !m)
last->seq+=1; /* approximate missing some packets */
last->next_ts = next;
return(last->seq);
}
if((middle!=NULL)&&(addr>=middle->addr))
element=middle;
else
element=head;
for(; (element!=NULL)&&(element->addr<=addr); element=element->next) {
if(element->addr==addr) {
element->seq+=1;
if (ts != element->next_ts && !m)
element->seq += 1; /* approximate missing some packets */
element->next_ts = next;
return (element->seq);
}
}
return(create_stream(addr, next));
}
struct sdes_msg {
rtcp_common_t header;
struct rtcp_sdes sdes;
};
/*
* Handle file input events from network sockets.
*/
static Notify_value socket_handler(Notify_client client, int sock)
{
int len;
int proto;
struct sockaddr_in sin_from;
socklen_t addr_len;
char packet[8192];
int i;
const int VAT_LEN=8;
vat_hdr_t *vat_hdr;
rtp_hdr_t *rtp_hdr;
rtp_hdr_t rtp_hdr_send;
proto = ((int)client & 1);
/* Read packet data from socket. */
addr_len = sizeof(sin_from);
len = recvfrom(sock, packet, sizeof(packet), 0,
(struct sockaddr *)&sin_from, &addr_len);
rtp_hdr=(rtp_hdr_t *)packet;
if (debug) {
struct timeval now;
gettimeofday(&now, 0);
printf("%0.3f %s %4d [%s/%d]\n",
now.tv_sec + now.tv_usec/1e6,
rtp_hdr->version==2 ? (proto ? "RTCP" : "RTP ") :
rtp_hdr->version==0 ? (proto ? "vatC" : "vat ") : "UKWN", len,
inet_ntoa(sin_from.sin_addr), ntohs(sin_from.sin_port));
}
/* do not translate packets that already use RTP or arrive over the unicast
link*/
if ((rtp_hdr->version==2)||((sock!=multi_sock[0])&&(sock!=multi_sock[1]))) {
rtp_hdr=(rtp_hdr_t *)packet;
for (i = 0; i < hostc; i++) {
if (side[i][proto].sock != sock
&& side[i][proto].sin.sin_addr.s_addr != INADDR_ANY) {
if (sendto(side[i][2].sock, packet, len, 0,
(struct sockaddr *)&side[i][proto].sin,
sizeof(side[i][proto].sin)) == -1) {
perror("sendto RTPC");
}
}
}
}
else {
struct msghdr msg;
if (!proto) { /* translate VAT packets */
struct iovec iov[2];
char type;
int samples = len-VAT_LEN;
vat_hdr=(vat_hdr_t *)packet;
if(vat_hdr->flags&VATHF_NEWTS)
rtp_hdr_send.m = 1;
else
rtp_hdr_send.m = 0;
type= vat_hdr->flags&VATHF_FMTMASK;
switch (type) {
case VAT_AUDF_GSM:
samples = (samples/33)*160;
case VAT_AUDF_MULAW8:
case VAT_AUDF_G721: /* samples not right for this */
case VAT_AUDF_G723: /* samples not right for this */
rtp_hdr_send.pt=type;
break;
case VAT_AUDF_IDVI:
samples = (samples-4)*2;
rtp_hdr_send.pt=5;
break;
case VAT_AUDF_L16_16:
case VAT_AUDF_L16_44:
case VAT_AUDF_LPC4:
case VAT_AUDF_CELP:
case VAT_AUDF_LPC1:
case VAT_AUDF_UNDEF :
default:
rtp_hdr_send.pt=115; /* hopefully unused */
perror(" unknown codecs ");
break;
}
rtp_hdr_send.ssrc = sin_from.sin_addr.s_addr;
rtp_hdr_send.seq = find_stream(rtp_hdr_send.ssrc, vat_hdr->ts,
vat_hdr->ts + samples, rtp_hdr_send.m);
rtp_hdr_send.version = RTP_VERSION;
rtp_hdr_send.p = 0;
rtp_hdr_send.x = 0;
rtp_hdr_send.cc = 0;
rtp_hdr_send.ts = vat_hdr->ts;
#if WIN32
/* Windows does not support sendmsg(), use copying instead;
* contributed by Lutz Grueneberg <[email protected]>. */
{
unsigned char mbuf[10000];
int mlength = 0;
memcpy (&mbuf[mlength], (char *)&(rtp_hdr_send), sizeof(rtp_hdr_t)-4);
mlength += sizeof(rtp_hdr_t)-4;
memcpy (&mbuf[mlength], packet+VAT_LEN, len-VAT_LEN);
mlength += len-VAT_LEN;
for (i = 0; i < hostc; i++) {
if (side[i][proto].sock != sock) {
if (sendto(side[i][2].sock, mbuf, mlength, 0,
&(side[i][proto].sin), sizeof(side[i][proto].sin))!=
mlength)
perror("sendmsg RTCP");
}
}
}
#else
iov[0].iov_base = (char *)&(rtp_hdr_send);
iov[0].iov_len = sizeof(rtp_hdr_t)-4;
iov[1].iov_base = packet+VAT_LEN;
iov[1].iov_len = len-VAT_LEN;
msg.msg_iov = iov;
msg.msg_iovlen = 2;
for (i = 0; i < hostc; i++) {
if (side[i][proto].sock != sock) {
msg.msg_name = (char*) &side[i][proto].sin;
msg.msg_namelen = sizeof(side[i][proto].sin);
#if HAVE_MSGCONTROL
msg.msg_control = 0;
msg.msg_controllen = 0;
#else
msg.msg_accrights = 0;
msg.msg_accrightslen = 0;
#endif
if ((sendmsg(side[i][2].sock, &msg,0))!=
iov[0].iov_len +iov[1].iov_len)
perror("sendmsg RTCP");
}
}
#endif /* WIN32 */
}
else if (((struct CtrlMsgHdr *)packet)->type == 1) /* vat ID messages */{
rtcp_t *rtcp_msg;
struct sdes_msg *ctl_msg;
rtcp_sdes_item_t *item;
int len=0;
int length;
item=NULL;
/* total length of the packet = IP address+ site entry of vat+ 2 type+ 2
length + 4 common header+ 4 ssrc + 8 empty RR */
length = strlen(inet_ntoa(sin_from.sin_addr)) +
strlen(packet+sizeof(struct CtrlMsgHdr)) + 12 + 8;
length = ((length/4)*4)+4;
rtcp_msg=(rtcp_t *)malloc(length);
memset(rtcp_msg,0,length);
/* init RR */
rtcp_msg->common.version=2;
rtcp_msg->common.p=0;
rtcp_msg->common.count=0;
rtcp_msg->common.pt=RTCP_RR;
rtcp_msg->r.rr.ssrc = rand();
rtcp_msg->common.length=(8 >> 2) - 1;
ctl_msg=(struct sdes_msg *)&rtcp_msg->r.rr.rr[0];
item=( rtcp_sdes_item_t *) ((char *)ctl_msg+8);
/* init CNAME */
item->type=1;
strcpy(item->data,inet_ntoa(sin_from.sin_addr));
item->length=strlen(item->data);
len=item->length+2;
item=(rtcp_sdes_item_t *)((char *)item +len);
/* init NAME */
strcpy(item->data,packet+sizeof(struct CtrlMsgHdr));
item->length=strlen(item->data);
item->type=2;
len+=item->length+2;
ctl_msg->header.version=2;
ctl_msg->header.p=0;
ctl_msg->header.count=1;
ctl_msg->header.pt=202;
ctl_msg->sdes.src=sin_from.sin_addr.s_addr;
ctl_msg->header.length=((length-8) >> 2) - 1;
for (i = 0; i < hostc; i++) {
if (side[i][proto].sock != sock) {
if (sendto(side[i][2].sock,(char *)rtcp_msg,
((rtcp_msg->common.length+1)+(ctl_msg->header.length+1))*4,
0, (struct sockaddr *)&side[i][proto].sin,
sizeof(side[i][proto].sin)) < 0)
perror("sendto RTCP");
}
}
free(rtcp_msg);
}/* control messages */
}
return NOTIFY_DONE;
} /* socket_handler */
static void usage(char *argv0)
{
fprintf(stderr, "usage: %s address/port[/ttl] address/port[/ttl] [...]\n",
argv0);
}
int main(int argc, char *argv[])
{
int c;
struct {
char *name;
unsigned char ttl;
struct sockaddr_in sin;
struct ip_mreq mreq;
} host[MAX_HOST];
struct sockaddr_in sin; /* generic bind */
extern int optind;
char loop = 0; /* multicast loop */
int reuse = 1; /* reuse address */
int i, j;
/* Set up socket. */
startupSocket();
while ((c = getopt(argc, argv, "d?h")) != EOF) {
switch(c) {
case 'd':
debug = 1;
break;
case '?':
case 'h':
usage(argv[0]);
exit(1);
break;
}
}
if (argc - optind < 2) {
usage(argv[0]);
exit(1);
}
/* Parse host descriptions. */
for (i = 0; i < argc - optind; i++) {
if (i >= MAX_HOST)
break;
host[i].ttl = 16;
host[i].name = argv[optind+i];
if (hpt(host[i].name, (struct sockaddr_in*) &host[i].sin.sin_addr,
&host[i].ttl) == -1) {
fprintf(stderr, "Invalid host specification %s\n", host[i].name);
usage(argv[0]);
exit(1);
}
if (IN_CLASSD(ntohl(host[i].sin.sin_addr.s_addr))) {
host[i].mreq.imr_multiaddr = host[i].sin.sin_addr;
host[i].mreq.imr_interface.s_addr = htonl(INADDR_ANY);
}
hostc++;
}
/* Create/bind sockets. */
for (i = 0; i < hostc; i++) { /* hosts (unicast or multicast) */
for (j = 0; j < 3; j++) { /* receive ports (RTP, RTCP), send */
side[i][j].sock = socket(PF_INET, SOCK_DGRAM, 0);
if (side[i][j].sock < 0) {
perror("socket");
exit(1);
}
if (setsockopt(side[i][j].sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse,
sizeof(reuse)) == -1)
perror("setsockopt: reuseaddr");
if (j < 2) {
side[i][j].sin = host[i].sin;
side[i][j].sin.sin_port = htons(ntohs(host[i].sin.sin_port) + j);
}
else {
side[i][j].sin.sin_family = AF_INET;
side[i][j].sin.sin_addr.s_addr = INADDR_ANY;
side[i][j].sin.sin_port = 0;
}
/* Bind to multicast address. */
sin = side[i][j].sin;
if (IN_CLASSD(ntohl(host[i].sin.sin_addr.s_addr))) {
if (!multi_sock[j]) {
multi_sock[j]=side[i][j].sock; /* save number of multicast socket */
}
if (j==2 && setsockopt(side[i][j].sock, IPPROTO_IP, IP_MULTICAST_TTL,
(char *)&host[i].ttl, sizeof(host[i].ttl)) < 0) {
perror("IP_MULTICAST_TTL");
exit(1);
}
again:
if (bind(side[i][j].sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
if (errno == EADDRNOTAVAIL) {
sin.sin_addr.s_addr = INADDR_ANY;
goto again;
}
else {
perror("bind multicast");
exit(1);
}
}
if (j < 2 && setsockopt(side[i][j].sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char *)&host[i].mreq, sizeof(host[i].mreq)) < 0) {
perror("IP_ADD_MEMBERSHIP");
exit(1);
}
if (j==2 && setsockopt(side[i][j].sock, IPPROTO_IP, IP_MULTICAST_LOOP,
(char *)&loop, sizeof(loop)) < 0) {
perror("IP_MULTICAST_LOOP");
}
} /* multicast */
/* unicast */
else {
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(side[i][j].sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind unicast");
exit(1);
}
}
if (j < 2) {
notify_set_input_func((Notify_client)j, socket_handler,
side[i][j].sock);
}
} /* for j (protocols) */
} /* for i (hosts) */
if ((c = notify_start()) != NOTIFY_OK) {
fprintf(stderr, "%s: Notifier error %d.\n", argv[0], c);
perror("select");
};
return 0;
} /* main */