-
Notifications
You must be signed in to change notification settings - Fork 4
/
rtpp_session.c
565 lines (498 loc) · 14.9 KB
/
rtpp_session.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
* Copyright (c) 2006-2007 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $Id: rtpp_session.c,v 1.13 2008/12/24 10:46:03 sobomax Exp $
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rtpp_defines.h"
#include "rtpp_log.h"
#include "rtpp_record.h"
#include "rtpp_session.h"
#include "rtpp_util.h"
#include "rtp_transcoder.h"
#include "rtpp_mem.h"
extern struct rtp_data_list* active_resized_sp_list;
extern int g_use_timed_resizer;
void
init_hash_table(struct cfg *cf)
{
int i;
for (i = 0; i < 256; i++)
{
cf->rand_table[i] = random();
}
}
static uint8_t
hash_string(struct cfg *cf, char *bp, char *ep)
{
uint8_t res;
for (res = cf->rand_table[0]; bp[0] != '\0' && bp != ep; bp++)
{
res = cf->rand_table[res ^ bp[0]];
}
return res;
}
void
hash_table_append(struct cfg *cf, struct rtpp_session *sp)
{
uint8_t hash;
struct rtpp_session *tsp;
assert(sp->rtcp != NULL);
hash = hash_string(cf, sp->call_id, NULL);
tsp = cf->hash_table[hash];
if (tsp == NULL)
{
cf->hash_table[hash] = sp;
sp->prev = sp->next = NULL;
return;
}
while (tsp->next != NULL)
{
tsp = tsp->next;
}
tsp->next = sp;
sp->prev = tsp;
sp->next = NULL;
}
static void
hash_table_remove(struct cfg *cf, struct rtpp_session *sp)
{
uint8_t hash;
assert(sp->rtcp != NULL);
if (sp->prev != NULL)
{
sp->prev->next = sp->next;
if (sp->next != NULL)
{
sp->next->prev = sp->prev;
}
return;
}
hash = hash_string(cf, sp->call_id, NULL);
/* Make sure we are removing the right session */
assert(cf->hash_table[hash] == sp);
cf->hash_table[hash] = sp->next;
if (sp->next != NULL)
{
sp->next->prev = NULL;
}
}
struct rtpp_session *
session_findfirst(struct cfg *cf, char *call_id)
{
uint8_t hash;
struct rtpp_session *sp;
hash = hash_string(cf, call_id, NULL);
for (sp = cf->hash_table[hash]; sp != NULL; sp = sp->next)
{
if (strcmp(sp->call_id, call_id) == 0)
{
break;
}
}
return(sp);
}
struct rtpp_session *
session_findnext(struct rtpp_session *psp)
{
struct rtpp_session *sp;
for (sp = psp->next; sp != NULL; sp = sp->next)
{
if (strcmp(sp->call_id, psp->call_id) == 0)
{
break;
}
}
return(sp);
}
void
append_session(struct cfg *cf, struct rtpp_session *sp, int index)
{
if (sp->fds[index] != -1)
{
cf->sessions[cf->nsessions] = sp;
cf->pfds[cf->nsessions].fd = sp->fds[index];
cf->pfds[cf->nsessions].events = POLLIN;
cf->pfds[cf->nsessions].revents = 0;
sp->sidx[index] = cf->nsessions;
rtpp_log_write(RTPP_LOG_INFO, sp->log, "appended-fd:%d session:%d\n", cf->pfds[cf->nsessions].fd, cf->nsessions);
cf->nsessions++;
}
else
{
sp->sidx[index] = -1;
}
}
void
remove_session(struct cfg *cf, struct rtpp_session *sp)
{
int i;
rtpp_log_write(RTPP_LOG_INFO, sp->log, "RTP stats: %lu in from callee, %lu "
"in from caller, %lu relayed, %lu dropped", sp->pcount[0], sp->pcount[1],
sp->pcount[2], sp->pcount[3]);
rtpp_log_write(RTPP_LOG_INFO, sp->log, "RTCP stats: %lu in from callee, %lu "
"in from caller, %lu relayed, %lu dropped", sp->rtcp->pcount[0],
sp->rtcp->pcount[1], sp->rtcp->pcount[2], sp->rtcp->pcount[3]);
rtpp_log_write(RTPP_LOG_INFO, sp->log, "session on ports %d/%d is cleaned up",
sp->ports[0], sp->ports[1]);
for (i = 0; i < 2; i++)
{
if (sp->addr[i] != NULL)
free(sp->addr[i]);
if (sp->prev_addr[i] != NULL)
free(sp->prev_addr[i]);
if (sp->rtcp->addr[i] != NULL)
free(sp->rtcp->addr[i]);
if (sp->rtcp->prev_addr[i] != NULL)
free(sp->rtcp->prev_addr[i]);
//if(sp->stream[i]->bio != NULL) {
// BIO_free(sp->stream[i]->bio);
//}
if (sp->stream[i] != NULL)
{
rtpp_dtls_free_stream(sp->stream[i]);
}
if (sp->rtcp->stream[i] != NULL)
{
rtpp_dtls_free_stream(sp->rtcp->stream[i]);
}
if (sp->fds[i] != -1)
{
close(sp->fds[i]);
assert(cf->sessions[sp->sidx[i]] == sp);
cf->sessions[sp->sidx[i]] = NULL;
assert(cf->pfds[sp->sidx[i]].fd == sp->fds[i]);
cf->pfds[sp->sidx[i]].fd = -1;
cf->pfds[sp->sidx[i]].events = 0;
}
if (sp->rtcp->fds[i] != -1)
{
close(sp->rtcp->fds[i]);
assert(cf->sessions[sp->rtcp->sidx[i]] == sp->rtcp);
cf->sessions[sp->rtcp->sidx[i]] = NULL;
assert(cf->pfds[sp->rtcp->sidx[i]].fd == sp->rtcp->fds[i]);
cf->pfds[sp->rtcp->sidx[i]].fd = -1;
cf->pfds[sp->rtcp->sidx[i]].events = 0;
}
if (sp->rrcs[i] != NULL)
rclose(sp, sp->rrcs[i], 1);
if (sp->rtcp->rrcs[i] != NULL)
rclose(sp, sp->rtcp->rrcs[i], 1);
if (sp->rtps[i] != NULL)
{
cf->rtp_servers[sp->sridx] = NULL;
rtp_server_free(sp->rtps[i]);
}
if (sp->codecs[i] != NULL)
free(sp->codecs[i]);
if (sp->rtcp->codecs[i] != NULL)
free(sp->rtcp->codecs[i]);
if (sp->bridgeBindAddr[i] != NULL) // VLAN Support (FRN4811)
free(sp->bridgeBindAddr[i]);
if (sp->ice_u[i] != NULL)
{
if (sp->ice_u[i]->local_user_name != NULL)
free(sp->ice_u[i]->local_user_name);
if (sp->ice_u[i]->local_password != NULL)
free(sp->ice_u[i]->local_password);
if (sp->ice_u[i]->remote_user_name != NULL)
free(sp->ice_u[i]->remote_user_name);
if (sp->ice_u[i]->remote_password != NULL)
free(sp->ice_u[i]->remote_password);
free(sp->ice_u[i]);
}
if (sp->ice_candidate_list[i] != NULL) // ICE remote candidate support
delete_ice_candidate(sp, i);
if (sp->rtcp->ice_candidate_list[i] != NULL)
delete_ice_candidate(sp->rtcp, i);
if (sp->transcode)
rtp_transcoder_free(&sp->trans[i],cf);
if (sp->secure)
{
rtpp_srtp_free_context(&sp->srtp[i]);
rtpp_srtp_free_context(&sp->rtcp->srtp[i]);
}
rtp_resizer_free(&sp->resizers[i]);
}// for
rtpp_stun_agent_remove(sp); //Remove stun agent Context.
remove_session_frm_active_rsz_lst(sp);
rtpp_log_write(RTPP_LOG_INFO, sp->log, "timed resizer lists cleaned up");
if (sp->timeout_data.notify_tag != NULL)
free(sp->timeout_data.notify_tag);
hash_table_remove(cf, sp);
if (sp->call_id != NULL)
free(sp->call_id);
if (sp->tag != NULL)
free(sp->tag);
rtpp_log_close(sp->log);
free(sp->rtcp);
free(sp);
cf->sessions_active--;
}
int
compare_session_tags(char *tag1, char *tag0, unsigned *medianum_p)
{
size_t len0 = strlen(tag0);
if (!strncmp(tag1, tag0, len0))
{
if (tag1[len0] == ';')
{
if (medianum_p != NULL)
*medianum_p = strtoul(tag1 + len0 + 1, NULL, 10);
return 2;
}
if (tag1[len0] == '\0')
return 1;
return 0;
}
return 0;
}
int
find_stream(struct cfg *cf, char *call_id, char *from_tag, char *to_tag,
struct rtpp_session **spp)
{
char *cp1, *cp2;
for (*spp = session_findfirst(cf, call_id); *spp != NULL; *spp = session_findnext(*spp))
{
if (strcmp((*spp)->tag, from_tag) == 0)
{
return 0;
}
else if (to_tag != NULL)
{
switch (compare_session_tags((*spp)->tag, to_tag, NULL))
{
case 1:
/* Exact tag match */
return 1;
case 2:
/*
* Reverse tag match without medianum. Medianum is always
* applied to the from tag, verify that.
*/
cp1 = strrchr((*spp)->tag, ';');
cp2 = strrchr(from_tag, ';');
if (cp2 != NULL && strcmp(cp1, cp2) == 0)
return 1;
break;
default:
break;
}
}
}
return -1;
}
static void
reconnect_timeout_handler(struct rtpp_session *sp, struct rtpp_timeout_handler *th)
{
struct sockaddr_un remote;
assert(th->socket_name != NULL && th->connected == 0);
if (th->fd == -1)
{
rtpp_log_write(RTPP_LOG_DBUG, sp->log, "connecting timeout socket");
}
else
{
rtpp_log_write(RTPP_LOG_DBUG, sp->log, "reconnecting timeout socket");
close(th->fd);
}
th->fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (th->fd == -1)
{
rtpp_log_ewrite(RTPP_LOG_ERR, sp->log, "can't create timeout socket");
return;
}
remote.sun_family = AF_LOCAL;
strncpy(remote.sun_path, th->socket_name, sizeof(remote.sun_path) - 1);
#if defined(HAVE_SOCKADDR_SUN_LEN)
remote.sun_len = strlen(remote.sun_path);
#endif
if (connect(th->fd, (struct sockaddr *)&remote, sizeof(remote)) == -1)
{
rtpp_log_ewrite(RTPP_LOG_ERR, sp->log, "can't connect to timeout socket");
}
else
{
th->connected = 1;
}
}
void
do_timeout_notification(struct rtpp_session *sp, int retries)
{
int result, len;
struct rtpp_timeout_handler *th = sp->timeout_data.handler;
if (th == NULL)
return;
if (th->connected == 0)
{
reconnect_timeout_handler(sp, th);
/* If connect fails, no notification will be sent */
if (th->connected == 0)
{
rtpp_log_write(RTPP_LOG_ERR, sp->log, "unable to send timeout notification");
return;
}
}
if (sp->timeout_data.notify_tag == NULL)
{
len = snprintf(th->notify_buf, sizeof(th->notify_buf), "%d %d\n",
sp->ports[0], sp->ports[1]);
}
else
{
len = snprintf(th->notify_buf, sizeof(th->notify_buf), "%s\n",
sp->timeout_data.notify_tag);
}
assert(len < (int)sizeof(th->notify_buf));
do
{
result = send(th->fd, th->notify_buf, len, 0);
} while (len == -1 && errno == EINTR);
if (result < 0)
{
th->connected = 0;
rtpp_log_ewrite(RTPP_LOG_ERR, sp->log, "failed to send timeout notification");
if (retries > 0)
do_timeout_notification(sp, retries - 1);
}
}
int
get_ttl(struct rtpp_session *sp)
{
switch (sp->ttl_mode)
{
case TTL_UNIFIED:
return(MAX(sp->ttl[0], sp->ttl[1]));
case TTL_INDEPENDENT:
return(MIN(sp->ttl[0], sp->ttl[1]));
default:
/* Shouldn't happen[tm] */
break;
}
abort();
return 0;
}
void clean_session_rsz_pkt_list(struct rtpp_session *sp, int idx)
{
if (g_use_timed_resizer)
{
rtp_data_list_destroy(&(sp->rsz_pckt_list[idx]));
rtp_data_list_init(&sp->rsz_pckt_list[idx]);
sp->incr_pkt_ts[idx]=0;
sp->marker_status[idx]=0;
}
}
void remove_session_frm_active_rsz_lst(struct rtpp_session *sp)
{
if (g_use_timed_resizer)
{
rtp_data_node* cur = (rtp_data_node*)active_resized_sp_list->head;
int n=0;
for (n=0; n < active_resized_sp_list->node_count; n++)
{
if (cur==NULL) break;
struct active_rsz_sp* sptmp = (active_rsz_sp*)(cur->data);
if (sptmp && sptmp->rsz_sp == sp)
{
rtp_data_list_remove(cur->data, active_resized_sp_list);
break;
}
cur=cur->next;
}
}
clean_session_rsz_pkt_list(sp, 0);
clean_session_rsz_pkt_list(sp, 1);
}
// ICE candidates support
struct remote_ice_candidate*
find_ice_candidate(struct rtpp_session *sp, int ridx, struct sockaddr *raddr, int addr_len)
{
struct remote_ice_candidate *pi = NULL;
struct sockaddr_in *remote = (struct sockaddr_in*)raddr;
for (pi = sp->ice_candidate_list[ridx]; pi != NULL; pi = pi->next)
{
// if ip address and port is the same
struct sockaddr_in *candidate = (struct sockaddr_in*)(pi->addr);
if (candidate->sin_addr.s_addr == remote->sin_addr.s_addr && candidate->sin_port == remote->sin_port)
{
break;
}
}
/* Return the First Candidate as Default for the corresponding sp */
if (pi == NULL)
return sp->ice_candidate_list[ridx];
return pi;
}
void
append_ice_candidate(struct cfg *cf, struct remote_ice_candidate **head, struct remote_ice_candidate **candidate)
{
struct remote_ice_candidate *pi = *head;
if ((*head) == NULL)
{
*head = *candidate;
(*head)->prev = NULL;
return;
}
while (pi->next != NULL)
{
pi = pi->next;
}
pi->next = *candidate;
(*candidate)->prev = *head;
}
void
delete_ice_candidate(struct rtpp_session *sp, int ridx)
{
struct remote_ice_candidate *pi;
pi = sp->ice_candidate_list[ridx];
if (pi == NULL)
{
return;
}
while (pi->next != NULL)
{
pi = pi->next;
}
while (pi->prev != NULL)
{
pi = pi->prev;
free(pi->next->addr);
free(pi->next);
pi->next = NULL;
}
free(pi->addr);
free(pi);
sp->ice_candidate_list[ridx] = NULL;
}