/
rtp_relay_ctx.c
1778 lines (1602 loc) · 46.7 KB
/
rtp_relay_ctx.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2021 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "rtp_relay_ctx.h"
#include "rtp_relay_load.h"
#include "../../mem/shm_mem.h"
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
#include "../../bin_interface.h"
#include "../../lib/cJSON.h"
static struct tm_binds rtp_relay_tmb;
static struct dlg_binds rtp_relay_dlg;
static int rtp_relay_tm_ctx_idx = -1;
static int rtp_relay_dlg_ctx_idx = -1;
static int rtp_relay_ctx_idx = -1;
static rw_lock_t *rtp_relay_contexts_lock;
static struct list_head *rtp_relay_contexts;
#define RTP_RELAY_GET_MSG_CTX() ((struct rtp_relay_ctx *)context_get_ptr(CONTEXT_GLOBAL, \
current_processing_ctx, rtp_relay_ctx_idx))
#define RTP_RELAY_PUT_CTX(_p) context_put_ptr(CONTEXT_GLOBAL, \
current_processing_ctx, rtp_relay_ctx_idx, (_p))
#define RTP_RELAY_GET_TM_CTX(_t) (rtp_relay_tmb.t_ctx_get_ptr(_t, rtp_relay_tm_ctx_idx))
#define RTP_RELAY_PUT_TM_CTX(_t, _p) \
rtp_relay_tmb.t_ctx_put_ptr(_t, rtp_relay_tm_ctx_idx, _p)
#define RTP_RELAY_GET_DLG_CTX(_d) (rtp_relay_dlg.dlg_ctx_get_ptr(_d, rtp_relay_dlg_ctx_idx))
#define RTP_RELAY_PUT_DLG_CTX(_d, _p) \
rtp_relay_dlg.dlg_ctx_put_ptr(_d, rtp_relay_dlg_ctx_idx, _p)
static str rtp_relay_dlg_name = str_init("_rtp_relay_ctx_");
static int rtp_relay_dlg_callbacks(struct dlg_cell *dlg, struct rtp_relay_ctx *ctx);
/* pvar handing */
static struct {
str name;
enum rtp_relay_var_flags flag;
} rtp_relay_var_flags_str[]= {
{ str_init("flags"), RTP_RELAY_FLAGS_SELF },
{ str_init("peer"), RTP_RELAY_FLAGS_PEER },
{ str_init("ip"), RTP_RELAY_FLAGS_IP },
{ str_init("type"), RTP_RELAY_FLAGS_TYPE },
{ str_init("iface"), RTP_RELAY_FLAGS_IFACE },
{ str_init("disabled"), RTP_RELAY_FLAGS_DISABLED },
};
str *rtp_relay_flags_get_str(enum rtp_relay_var_flags flags)
{
static str unknown = str_init("unknown");
int s = sizeof(rtp_relay_var_flags_str) / sizeof (rtp_relay_var_flags_str[0]);
if (flags >= s)
return &unknown;
for (--s; s >=0; s--)
if (rtp_relay_var_flags_str[s].flag == flags)
return &rtp_relay_var_flags_str[s].name;
return &unknown;
}
enum rtp_relay_var_flags rtp_relay_flags_get(const str *name)
{
int s = sizeof(rtp_relay_var_flags_str) / sizeof (rtp_relay_var_flags_str[0]);
for (--s; s >= 0; s--)
if (str_strcasecmp(name, &rtp_relay_var_flags_str[s].name) == 0)
return rtp_relay_var_flags_str[s].flag;
return RTP_RELAY_FLAGS_UNKNOWN;
}
static struct rtp_relay_ctx *rtp_relay_get_dlg_ctx(void)
{
struct dlg_cell *dlg = rtp_relay_dlg.get_dlg();
return dlg?RTP_RELAY_GET_DLG_CTX(dlg):NULL;
}
struct rtp_relay_ctx *rtp_relay_new_ctx(void)
{
struct rtp_relay_ctx *ctx = shm_malloc(sizeof *ctx);
if (!ctx) {
LM_ERR("oom for creating RTP relay context!\n");
return NULL;
}
memset(ctx, 0, sizeof *ctx);
lock_init(&ctx->lock);
INIT_LIST_HEAD(&ctx->sessions);
INIT_LIST_HEAD(&ctx->list);
return ctx;
}
struct rtp_relay_ctx *rtp_relay_get_ctx(void)
{
struct cell* t;
struct rtp_relay_ctx *ctx = rtp_relay_try_get_ctx();
t = rtp_relay_tmb.t_gett();
t = t==T_UNDEFINED ? NULL : t;
if (ctx) {
/* if it is local, and we have transaction, move it in transaction */
if (t && RTP_RELAY_GET_MSG_CTX()) {
RTP_RELAY_PUT_TM_CTX(t, ctx);
RTP_RELAY_PUT_CTX(NULL);
}
return ctx;
}
ctx = rtp_relay_new_ctx();
if (!ctx)
return NULL;
if (t)
RTP_RELAY_PUT_TM_CTX(t, ctx);
else
RTP_RELAY_PUT_CTX(ctx);
return ctx;
}
rtp_ctx rtp_relay_get_context(void)
{
return rtp_relay_try_get_ctx();
}
rtp_ctx rtp_relay_get_context_dlg(struct dlg_cell *dlg)
{
return RTP_RELAY_GET_DLG_CTX(dlg);
}
static void rtp_relay_ctx_free_sess(struct rtp_relay_sess *s)
{
int f;
for (f = 0; f < RTP_RELAY_FLAGS_SIZE; f++) {
if (s->flags[RTP_RELAY_OFFER][f].s)
shm_free(s->flags[RTP_RELAY_OFFER][f].s);
if (s->flags[RTP_RELAY_ANSWER][f].s)
shm_free(s->flags[RTP_RELAY_ANSWER][f].s);
}
if (s->server.node.s)
shm_free(s->server.node.s);
list_del(&s->list);
shm_free(s);
}
void rtp_relay_ctx_free(void *param)
{
struct list_head *it, *safe;
struct rtp_relay_ctx *ctx = (struct rtp_relay_ctx *)param;
if (!ctx)
return;
RTP_RELAY_CTX_LOCK(ctx);
if (rtp_relay_ctx_pending(ctx)) {
rtp_relay_ctx_set_deleted(ctx);
RTP_RELAY_CTX_UNLOCK(ctx);
return;
}
RTP_RELAY_CTX_UNLOCK(ctx);
if (ctx->callid.s)
shm_free(ctx->callid.s);
if (ctx->from_tag.s)
shm_free(ctx->from_tag.s);
if (ctx->to_tag.s)
shm_free(ctx->to_tag.s);
list_for_each_safe(it, safe, &ctx->sessions)
rtp_relay_ctx_free_sess(list_entry(it, struct rtp_relay_sess, list));
lock_destroy(&ctx->lock);
shm_free(ctx);
}
struct rtp_relay_ctx *rtp_relay_try_get_ctx(void)
{
struct cell* t;
struct rtp_relay_ctx* ctx = NULL;
if ((ctx = rtp_relay_get_dlg_ctx()) != NULL)
return ctx;
if ((ctx = RTP_RELAY_GET_MSG_CTX()) != NULL)
return ctx;
/* local one not found - search in transaction */
t = rtp_relay_tmb.t_gett();
t = t==T_UNDEFINED ? NULL : t;
return (t ? RTP_RELAY_GET_TM_CTX(t) : NULL);
}
static void rtp_relay_move_ctx( struct cell* t, int type, struct tmcb_params *ps)
{
struct rtp_relay_ctx *ctx = rtp_relay_try_get_ctx();
if (!ctx || rtp_relay_get_dlg_ctx())
return; /* nothing to move */
t = rtp_relay_tmb.t_gett();
if (!t || t == T_UNDEFINED) {
LM_DBG("no transaction - can't move the context - freeing!\n");
rtp_relay_ctx_free(ctx);
return;
}
RTP_RELAY_PUT_TM_CTX(t, ctx);
RTP_RELAY_PUT_CTX(NULL);
}
#define RTP_RELAY_CTX_VERSION 1
#define RTP_RELAY_BIN_PUSH(_type, _value) \
do { \
if (bin_push_##_type(&packet, _value) < 0) { \
LM_ERR("cannot push '" #_value "' in bin packet!\n"); \
bin_free_packet(&packet); \
return; \
} \
} while (0)
static void rtp_relay_store_callback(struct dlg_cell *dlg, int type,
struct dlg_cb_params *params)
{
str buffer;
str str_empty = str_init("");
bin_packet_t packet;
enum rtp_relay_type rtype;
enum rtp_relay_var_flags flag;
struct rtp_relay_sess *sess;
str name = str_init("rtp_relay_ctx");
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
if (!ctx)
return;
if (bin_init(&packet, &name, 0, RTP_RELAY_CTX_VERSION, 0) < 0) {
LM_ERR("cannot initialize bin packet!\n");
return;
}
if (!ctx->main) {
if (list_empty(&ctx->sessions)) {
LM_WARN("no rtp relay session!\n");
return;
}
LM_WARN("rtp relay session not established - storing last session!\n");
sess = list_last_entry(&ctx->sessions, struct rtp_relay_sess, list);
} else {
sess = ctx->main;
}
RTP_RELAY_BIN_PUSH(str, &sess->relay->name);
RTP_RELAY_BIN_PUSH(int, sess->index);
RTP_RELAY_BIN_PUSH(int, sess->state);
RTP_RELAY_BIN_PUSH(int, sess->server.set);
RTP_RELAY_BIN_PUSH(str, &sess->server.node);
for (rtype = RTP_RELAY_OFFER; rtype < RTP_RELAY_SIZE; rtype++) {
for (flag = RTP_RELAY_FLAGS_SELF; flag < RTP_RELAY_FLAGS_SIZE; flag++) {
if (sess->flags[rtype][flag].s)
RTP_RELAY_BIN_PUSH(str, &sess->flags[rtype][flag]);
else
RTP_RELAY_BIN_PUSH(str, &str_empty);
}
}
bin_get_buffer(&packet, &buffer);
bin_free_packet(&packet);
if (rtp_relay_dlg.store_dlg_value(dlg, &rtp_relay_dlg_name, &buffer) < 0)
LM_WARN("rtp relay ctx was not saved in dialog\n");
}
#undef RTP_RELAY_BIN_PUSH
#define RTP_RELAY_BIN_POP(_type, _value) \
do { \
if (bin_pop_##_type(&packet, _value) < 0) { \
LM_ERR("cannot pop '" #_value "' from bin packet!\n"); \
goto error; \
} \
} while (0)
static void rtp_relay_loaded_callback(struct dlg_cell *dlg, int type,
struct dlg_cb_params *params)
{
str tmp;
int index;
str buffer;
bin_packet_t packet;
struct rtp_relay *relay;
enum rtp_relay_type rtype;
enum rtp_relay_var_flags flag;
struct rtp_relay_sess *sess;
struct rtp_relay_ctx *ctx = NULL;
if (!dlg) {
LM_ERR("null dialog - cannot fetch rtp relay info!\n");
return;
}
if (rtp_relay_dlg.fetch_dlg_value(dlg, &rtp_relay_dlg_name, &buffer, 0) < 0) {
LM_DBG("cannot fetch rtp relay info from the dialog\n");
return;
}
bin_init_buffer(&packet, buffer.s, buffer.len);
if (get_bin_pkg_version(&packet) != RTP_RELAY_CTX_VERSION) {
LM_ERR("invalid serialization version (%d != %d)\n",
get_bin_pkg_version(&packet), RTP_RELAY_CTX_VERSION);
return;
}
RTP_RELAY_BIN_POP(str, &tmp);
relay = rtp_relay_get(&tmp);
if (!relay) {
LM_ERR("no registered '%.*s' relay module\n", tmp.len, tmp.s);
return;
}
ctx = rtp_relay_new_ctx();
if (!ctx)
return;
RTP_RELAY_BIN_POP(int, &index);
sess = rtp_relay_new_sess(ctx, index);
if (!sess)
goto error;
RTP_RELAY_BIN_POP(int, &sess->state);
sess->relay = relay;
RTP_RELAY_BIN_POP(int, &sess->server.set);
RTP_RELAY_BIN_POP(str, &tmp);
shm_str_dup(&sess->server.node, &tmp);
for (rtype = RTP_RELAY_OFFER; rtype < RTP_RELAY_SIZE; rtype++) {
for (flag = RTP_RELAY_FLAGS_SELF; flag < RTP_RELAY_FLAGS_SIZE; flag++) {
RTP_RELAY_BIN_POP(str, &tmp);
if (tmp.len && shm_str_dup(&sess->flags[rtype][flag], &tmp) < 0)
LM_ERR("could not duplicate rtp session flag!\n");
}
}
/* all good now - delete the dialog variable as it is useless */
rtp_relay_dlg.store_dlg_value(dlg, &rtp_relay_dlg_name, NULL);
ctx->main = sess;
if (rtp_relay_dlg_callbacks(dlg, ctx) < 0)
goto error;
return;
error:
rtp_relay_ctx_free(ctx);
}
#undef RTP_RELAY_BIN_POP
int rtp_relay_ctx_preinit(void)
{
/* load the TM API */
if (load_tm_api(&rtp_relay_tmb)!=0) {
LM_ERR("TM not loaded - aborting!\n");
return -1;
}
/* load the DLG API */
if (load_dlg_api(&rtp_relay_dlg)!=0) {
LM_ERR("Dialog not loaded - aborting!\n");
return -1;
}
/* we need to register pointer in pre-init, to make sure the new dialogs
* loaded have the context registered */
rtp_relay_dlg_ctx_idx = rtp_relay_dlg.dlg_ctx_register_ptr(rtp_relay_ctx_free);
return 0;
}
int rtp_relay_ctx_init(void)
{
rtp_relay_contexts_lock = lock_init_rw();
if (!rtp_relay_contexts_lock) {
LM_ERR("cannot create lock for RTP Relay sessions\n");
return -1;
}
rtp_relay_contexts = shm_malloc(sizeof *rtp_relay_contexts);
if (!rtp_relay_contexts) {
LM_ERR("cannot create RTP Relay sessions list\n");
return -1;
}
INIT_LIST_HEAD(rtp_relay_contexts);
rtp_relay_tm_ctx_idx = rtp_relay_tmb.t_ctx_register_ptr(rtp_relay_ctx_free);
/* register a routine to move the pointer in tm when the transaction
* is created! */
if (rtp_relay_tmb.register_tmcb(0, 0, TMCB_REQUEST_IN, rtp_relay_move_ctx, 0, 0)<=0) {
LM_ERR("cannot register tm callbacks\n");
return -2;
}
if (rtp_relay_dlg.register_dlgcb(NULL, DLGCB_LOADED,
rtp_relay_loaded_callback, NULL, NULL) < 0)
LM_WARN("cannot register callback for loaded dialogs - will not be "
"able to restore an ongoing media session after a restart!\n");
rtp_relay_ctx_idx = context_register_ptr(CONTEXT_GLOBAL, rtp_relay_ctx_free);
return 0;
}
int rtp_relay_ctx_branch(void)
{
return rtp_relay_tmb.get_branch_index();
}
int rtp_relay_ctx_upstream(void)
{
return rtp_relay_dlg.get_direction() == DLG_DIR_UPSTREAM;
}
struct rtp_relay_sess *rtp_relay_get_sess(struct rtp_relay_ctx *ctx, int index)
{
struct list_head *it;
struct rtp_relay_sess *sess;
if (index == RTP_RELAY_ALL_BRANCHES)
return ctx->main;
list_for_each(it, &ctx->sessions) {
sess = list_entry(it, struct rtp_relay_sess, list);
if (sess->index == index)
return sess;
}
return NULL;
}
static struct rtp_relay_sess *rtp_relay_sess_empty(void)
{
struct rtp_relay_sess *sess;
sess = shm_malloc(sizeof *sess);
if (!sess) {
LM_ERR("oom for new sess!\n");
return NULL;
}
memset(sess, 0, sizeof *sess);
sess->server.set = -1;
sess->index = RTP_RELAY_ALL_BRANCHES;
INIT_LIST_HEAD(&sess->list);
return sess;
}
struct rtp_relay_sess *rtp_relay_new_sess(struct rtp_relay_ctx *ctx, int index)
{
struct rtp_relay_sess *sess = rtp_relay_sess_empty();
if (!sess)
return NULL;
sess->index = index;
if (index == RTP_RELAY_ALL_BRANCHES)
ctx->main = sess;
list_add(&sess->list, &ctx->sessions);
return sess;
}
#define RTP_RELAY_FLAGS(_t, _f) \
(sess->flags[_t][_f].s?&sess->flags[_t][_f]: \
(main?&main->flags[_t][_f]:NULL))
#define RTP_RELAY_PEER(_t) \
(_t == RTP_RELAY_OFFER?RTP_RELAY_ANSWER:RTP_RELAY_OFFER)
static int rtp_relay_offer(struct rtp_relay_session *info,
struct rtp_relay_sess *sess, struct rtp_relay_sess *main,
enum rtp_relay_type type, str *body)
{
if (!sess->relay) {
LM_BUG("no relay found!\n");
return -1;
}
if (sess->relay->funcs.offer(info, &sess->server,
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_IP),
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_TYPE),
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_IFACE),
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_IFACE),
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_SELF),
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_PEER), body) < 0) {
LM_ERR("could not engage offer!\n");
return -1;
}
rtp_sess_set_pending(sess);
return 1;
}
static int rtp_relay_answer(struct rtp_relay_session *info,
struct rtp_relay_sess *sess, struct rtp_relay_sess *main,
enum rtp_relay_type type, str *body)
{
if (!sess->relay) {
LM_BUG("no relay found!\n");
return -1;
}
return sess->relay->funcs.answer(info, &sess->server,
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_IP),
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_TYPE),
RTP_RELAY_FLAGS(RTP_RELAY_PEER(type), RTP_RELAY_FLAGS_IFACE),
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_IFACE),
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_SELF),
RTP_RELAY_FLAGS(type, RTP_RELAY_FLAGS_PEER), body);
}
#undef RTP_RELAY_PEER
static int rtp_relay_delete(struct rtp_relay_session *info,
struct rtp_relay_sess *sess, struct rtp_relay_sess *main)
{
int ret;
if (!sess->relay) {
LM_BUG("no relay found!\n");
return -1;
}
ret = sess->relay->funcs.delete(info, &sess->server,
RTP_RELAY_FLAGS(RTP_RELAY_OFFER, RTP_RELAY_FLAGS_SELF),
RTP_RELAY_FLAGS(RTP_RELAY_ANSWER, RTP_RELAY_FLAGS_PEER));
if (ret < 0)
return -1;
rtp_sess_reset_pending(sess);
return 1;
}
#undef RTP_RELAY_FLAGS
static inline void rtp_relay_sess_merge(struct rtp_relay_ctx *ctx, struct rtp_relay_sess *sess)
{
int f;
if (ctx->main == sess)
return;
if (ctx->main) {
for (f = 0; f < RTP_RELAY_FLAGS_SIZE; f++) {
if (!sess->flags[RTP_RELAY_OFFER][f].s) {
sess->flags[RTP_RELAY_OFFER][f] =
ctx->main->flags[RTP_RELAY_OFFER][f];
ctx->main->flags[RTP_RELAY_OFFER][f].s = NULL;
}
if (!sess->flags[RTP_RELAY_ANSWER][f].s) {
sess->flags[RTP_RELAY_ANSWER][f] =
ctx->main->flags[RTP_RELAY_ANSWER][f];
ctx->main->flags[RTP_RELAY_ANSWER][f].s = NULL;
}
}
rtp_relay_ctx_free_sess(ctx->main);
}
ctx->main = sess;
}
static inline int rtp_relay_dlg_mi_flags(rtp_relay_flags flags,
mi_item_t *obj)
{
if (flags[RTP_RELAY_FLAGS_SELF].s &&
add_mi_string(obj, MI_SSTR("flags"),
flags[RTP_RELAY_FLAGS_SELF].s,
flags[RTP_RELAY_FLAGS_SELF].len) < 0)
return -1;
if (flags[RTP_RELAY_FLAGS_PEER].s &&
add_mi_string(obj, MI_SSTR("peer"),
flags[RTP_RELAY_FLAGS_PEER].s,
flags[RTP_RELAY_FLAGS_PEER].len) < 0)
return -1;
if (flags[RTP_RELAY_FLAGS_IP].s &&
add_mi_string(obj, MI_SSTR("IP"),
flags[RTP_RELAY_FLAGS_IP].s,
flags[RTP_RELAY_FLAGS_IP].len) < 0)
return -1;
if (flags[RTP_RELAY_FLAGS_TYPE].s &&
add_mi_string(obj, MI_SSTR("type"),
flags[RTP_RELAY_FLAGS_TYPE].s,
flags[RTP_RELAY_FLAGS_TYPE].len) < 0)
return -1;
if (flags[RTP_RELAY_FLAGS_IFACE].s &&
add_mi_string(obj, MI_SSTR("interface"),
flags[RTP_RELAY_FLAGS_IFACE].s,
flags[RTP_RELAY_FLAGS_IFACE].len) < 0)
return -1;
return 0;
}
static int mi_rtp_relay_ctx(struct rtp_relay_ctx *ctx,
mi_item_t *item, int callid)
{
int ret = -1;
struct rtp_relay_sess *sess;
mi_item_t *rtp_item, *caller_item, *callee_item;
rtp_item = add_mi_object(item, MI_SSTR("rtp_relay"));
if (!rtp_item) {
LM_ERR("cold not create rtp_relay!\n");
return ret;
}
sess = ctx->main;
if (!sess)
goto end;
if (callid && add_mi_string(rtp_item, MI_SSTR("callid"),
ctx->callid.s, ctx->callid.len) < 0)
goto end;
caller_item = add_mi_object(rtp_item, MI_SSTR("caller"));
if (!caller_item)
goto end;
if (rtp_relay_dlg_mi_flags(sess->flags[RTP_RELAY_OFFER], caller_item) < 0)
goto end;
callee_item = add_mi_object(rtp_item, MI_SSTR("callee"));
if (!callee_item)
goto end;
if (rtp_relay_dlg_mi_flags(sess->flags[RTP_RELAY_ANSWER], callee_item) < 0)
goto end;
if (add_mi_string(rtp_item, MI_SSTR("relay"),
sess->relay->name.s, sess->relay->name.len) < 0)
goto end;
if (add_mi_string(rtp_item, MI_SSTR("node"),
sess->server.node.s, sess->server.node.len) < 0)
goto end;
if (add_mi_number(rtp_item, MI_SSTR("set"), sess->server.set) < 0)
goto end;
if (sess->index != RTP_RELAY_ALL_BRANCHES &&
add_mi_number(rtp_item, MI_SSTR("branch"), sess->index) < 0)
goto end;
ret = 0;
end:
return ret;
}
static void rtp_relay_dlg_mi(struct dlg_cell* dlg, int type, struct dlg_cb_params * params)
{
mi_item_t *item = (mi_item_t *)(params->dlg_data);
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
if (!ctx || !item)
return;
RTP_RELAY_CTX_LOCK(ctx);
mi_rtp_relay_ctx(ctx, item, 0);
RTP_RELAY_CTX_UNLOCK(ctx);
}
static void rtp_relay_delete_dlg(struct dlg_cell *dlg,
struct rtp_relay_ctx *ctx, struct rtp_relay_sess *sess)
{
struct rtp_relay_session info;
memset(&info, 0, sizeof info);
info.callid = &ctx->callid;
info.from_tag = &ctx->from_tag;
info.to_tag = &ctx->to_tag;
info.branch = sess->index;
rtp_relay_delete(&info, sess, NULL);
}
static void rtp_relay_dlg_end(struct dlg_cell* dlg, int type, struct dlg_cb_params * params)
{
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
if (!ctx->main || !rtp_sess_pending(ctx->main))
return;
RTP_RELAY_CTX_LOCK(ctx);
rtp_relay_delete_dlg(dlg, ctx, ctx->main);
RTP_RELAY_CTX_UNLOCK(ctx);
lock_start_write(rtp_relay_contexts_lock);
list_del(&ctx->list);
lock_stop_write(rtp_relay_contexts_lock);
}
void rtp_relay_indlg_tm_req(struct cell* t, int type, struct tmcb_params *p)
{
enum rtp_relay_type rtype;
struct rtp_relay_session info;
struct dlg_cell *dlg = (struct dlg_cell *)(*p->param);
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
if (!ctx || !ctx->main) {
LM_BUG("could not find a rtp relay context in %p!\n", ctx);
return;
}
memset(&info, 0, sizeof info);
info.branch = ctx->main->index;
info.msg = p->req;
info.body = get_body_part(info.msg, TYPE_APPLICATION, SUBTYPE_SDP);
if (!info.body)
return;
rtype = (rtp_relay_ctx_upstream()?RTP_RELAY_ANSWER:RTP_RELAY_OFFER);
rtp_relay_offer(&info, ctx->main, NULL, rtype, NULL);
}
static void rtp_relay_indlg_tm_rpl(struct sip_msg *msg, struct dlg_cell *dlg, int up)
{
str *body;
enum rtp_relay_type rtype;
struct rtp_relay_session info;
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
if (!ctx || !ctx->main) {
LM_BUG("could not find a rtp relay context in %p!\n", ctx);
return;
}
body = get_body_part(msg, TYPE_APPLICATION, SUBTYPE_SDP);
if (!body)
return;
memset(&info, 0, sizeof info);
info.branch = ctx->main->index;
info.msg = msg;
info.body = body;
rtype = (up?RTP_RELAY_OFFER:RTP_RELAY_ANSWER);
if (rtp_sess_late(ctx->main))
rtp_relay_offer(&info, ctx->main, NULL, rtype, NULL);
else
rtp_relay_answer(&info, ctx->main, NULL, rtype, NULL);
}
static void rtp_relay_indlg_tm_rpl_up(struct cell* t, int type, struct tmcb_params *p)
{
rtp_relay_indlg_tm_rpl(p->rpl, (struct dlg_cell *)(*p->param), 1);
}
static void rtp_relay_indlg_tm_rpl_down(struct cell* t, int type, struct tmcb_params *p)
{
rtp_relay_indlg_tm_rpl(p->rpl, (struct dlg_cell *)(*p->param), 0);
}
static void rtp_relay_indlg(struct dlg_cell* dlg, int type, struct dlg_cb_params * params)
{
struct rtp_relay_session info;
struct sip_msg *msg = params->msg;
struct rtp_relay_ctx *ctx = RTP_RELAY_GET_DLG_CTX(dlg);
str *body;
int ret;
if (!msg) {
LM_DBG("no message available\n");
return;
}
body = get_body_part(msg, TYPE_APPLICATION, SUBTYPE_SDP);
RTP_RELAY_CTX_LOCK(ctx);
ret = (ctx->main && rtp_sess_pending(ctx->main));
if (ret && !body) {
if (msg->REQ_METHOD != METHOD_INVITE) {
if (msg->REQ_METHOD != METHOD_ACK)
LM_DBG("method %d without SDP\n", msg->REQ_METHOD);
else if (rtp_sess_late(ctx->main))
LM_WARN("late negociation without SDP in ACK!\n");
ret = 0;
} else {
rtp_sess_set_late(ctx->main);
}
}
RTP_RELAY_CTX_UNLOCK(ctx);
if (!ret)
return;
if (msg->REQ_METHOD == METHOD_ACK) {
if (ctx->main || !rtp_sess_late(ctx->main))
return;
memset(&info, 0, sizeof info);
info.msg = msg;
info.body = body;
info.branch = ctx->main->index;
rtp_relay_answer(&info, ctx->main, NULL,
(rtp_relay_ctx_upstream()?RTP_RELAY_ANSWER:RTP_RELAY_OFFER), NULL);
return;
}
if (!body && msg->REQ_METHOD != METHOD_INVITE) {
LM_DBG("%d without body! skipping update\n", msg->REQ_METHOD);
return;
}
rtp_sess_reset_pending(ctx->main);
if (rtp_relay_tmb.register_tmcb(msg, 0, TMCB_REQUEST_FWDED,
rtp_relay_indlg_tm_req, dlg, 0)!=1)
LM_ERR("failed to install TM request callback\n");
if (rtp_relay_ctx_upstream()) {
if (rtp_relay_tmb.register_tmcb(msg, 0, TMCB_RESPONSE_FWDED,
rtp_relay_indlg_tm_rpl_up, dlg, 0)!=1)
LM_ERR("failed to install TM upstream reply callback\n");
} else {
if (rtp_relay_tmb.register_tmcb(msg, 0, TMCB_RESPONSE_FWDED,
rtp_relay_indlg_tm_rpl_down, dlg, 0)!=1)
LM_ERR("failed to install TM downstream reply callback\n");
}
}
static int rtp_relay_dlg_callbacks(struct dlg_cell *dlg, struct rtp_relay_ctx *ctx)
{
if (shm_str_sync(&ctx->callid, &dlg->callid) < 0)
LM_ERR("could not store callid in context\n");
if (shm_str_sync(&ctx->from_tag, &dlg->legs[DLG_CALLER_LEG].tag) < 0)
LM_ERR("could not store from tag in context\n");
if (shm_str_sync(&ctx->to_tag, &dlg->legs[callee_idx(dlg)].tag) < 0)
LM_ERR("could not store to tag in context\n");
if (rtp_relay_dlg.register_dlgcb(dlg, DLGCB_MI_CONTEXT,
rtp_relay_dlg_mi, NULL, NULL) < 0)
LM_ERR("could not register MI dlg print!\n");
RTP_RELAY_PUT_DLG_CTX(dlg, ctx);
if (rtp_relay_dlg.register_dlgcb(dlg,
DLGCB_TERMINATED|DLGCB_EXPIRED,
rtp_relay_dlg_end, NULL, NULL) < 0) {
LM_ERR("could not register MI dlg end!\n");
goto error;
}
if (rtp_relay_dlg.register_dlgcb(dlg,
DLGCB_REQ_WITHIN,
rtp_relay_indlg, NULL, NULL) != 0) {
LM_ERR("could not register request within dlg callback!\n");
goto error;
}
if (rtp_relay_dlg.register_dlgcb(dlg, DLGCB_WRITE_VP,
rtp_relay_store_callback, NULL, NULL))
LM_WARN("cannot register callback for rtp relay serialization! "
"Will not be able to engage rtp relay in case of a restart!\n");
lock_start_write(rtp_relay_contexts_lock);
list_add(&ctx->list, rtp_relay_contexts);
lock_stop_write(rtp_relay_contexts_lock);
return 0;
error:
RTP_RELAY_PUT_DLG_CTX(dlg, NULL);
return -1;
}
static int rtp_relay_sess_last(struct rtp_relay_ctx *ctx,
struct rtp_relay_sess *sess)
{
struct list_head *it;
struct rtp_relay_sess *s;
int n = 0;
list_for_each(it, &ctx->sessions) {
s = list_entry(it, struct rtp_relay_sess, list);
if (s->index == RTP_RELAY_ALL_BRANCHES)
continue;
if (sess && sess->index == s->index)
continue;
n++;
}
return n == 0;
}
static int rtp_relay_sess_success(struct rtp_relay_ctx *ctx,
struct rtp_relay_sess *sess, struct cell *t)
{
struct dlg_cell *dlg;
rtp_sess_set_success(sess);
if (rtp_relay_sess_last(ctx, sess))
rtp_relay_sess_merge(ctx, sess);
if (!rtp_relay_ctx_established(ctx)) {
dlg = rtp_relay_dlg.get_dlg();
if (!dlg) {
LM_ERR("could not find dialog!\n");
return -1;
}
/* reset old pointers */
RTP_RELAY_PUT_TM_CTX(t, NULL);
RTP_RELAY_PUT_CTX(NULL);
if (rtp_relay_dlg_callbacks(dlg, ctx) < 0) {
RTP_RELAY_PUT_TM_CTX(t, ctx);
return -1;
}
rtp_relay_ctx_set_established(ctx);
}
return 0;
}
static void rtp_relay_sess_failed(struct rtp_relay_ctx *ctx,
struct rtp_relay_sess *sess)
{
struct rtp_relay_sess *last;
rtp_sess_reset_pending(sess);
list_del(&sess->list);
if (rtp_relay_sess_last(ctx, NULL)) {
last = list_last_entry(&ctx->sessions, struct rtp_relay_sess, list);
if (rtp_sess_success(last))
rtp_relay_sess_merge(ctx, last);
}
}
static int handle_rtp_relay_ctx_leg_reply(struct rtp_relay_ctx *ctx, struct sip_msg *msg,
struct cell *t, struct rtp_relay_sess *sess)
{
int ret;
struct rtp_relay_session info;
memset(&info, 0, sizeof info);
info.msg = msg;
if (msg->REPLY_STATUS >= 300) {
if (!rtp_sess_late(sess)) {
rtp_relay_delete(&info, sess, ctx->main);
} else {
/* nothing to do */
LM_DBG("negative reply on late branch\n");
}
rtp_relay_sess_failed(ctx, sess);
return 1;
}
info.body = get_body_part(msg, TYPE_APPLICATION, SUBTYPE_SDP);
if (!info.body) {
if (msg->REPLY_STATUS < 200) {
LM_DBG("provisioning reply %d without body\n", msg->REPLY_STATUS);
return 1;
} else if (rtp_sess_late(sess)) {
LM_WARN("no SDP in final reply of late negotiation\n");
return -1;
} else {
LM_WARN("final reply without SDP - cannot complete negotiation!\n");
return -1;
}
}
info.branch = sess->index;
if (rtp_sess_late(sess))
ret = rtp_relay_offer(&info, sess, ctx->main, RTP_RELAY_ANSWER, NULL);
else
ret = rtp_relay_answer(&info, sess, ctx->main, RTP_RELAY_ANSWER, NULL);
if (ret > 0 && msg->REPLY_STATUS >= 200)
rtp_relay_sess_success(ctx, sess, t);
return ret;
}
static void rtp_relay_ctx_initial_cb(struct cell* t, int type, struct tmcb_params *p)
{
struct rtp_relay_session info;
struct rtp_relay_sess *sess;
struct rtp_relay_ctx *ctx = *(struct rtp_relay_ctx **)(p->param);
RTP_RELAY_CTX_LOCK(ctx);
switch (type) {
case TMCB_RESPONSE_FWDED:
/* first check if there's anything setup on this branch */
sess = rtp_relay_get_sess(ctx, rtp_relay_ctx_branch());
if (sess) {
if (!rtp_sess_pending(sess)) {
LM_DBG("no pending session on branch %d\n",
rtp_relay_ctx_branch());
sess = ctx->main;
}
} else {
LM_DBG("no session on branch %d\n", rtp_relay_ctx_branch());
sess = ctx->main;
}
if (!sess) {
LM_DBG("no session to respond to\n");
goto end;
}
if (rtp_sess_disabled(sess) || (!rtp_sess_late(sess) && !rtp_sess_pending(sess))) {
LM_DBG("disabled and/or pending session %d/%d\n",
rtp_sess_disabled(sess), rtp_sess_pending(sess));
goto end;
}
handle_rtp_relay_ctx_leg_reply(ctx, p->rpl, t, sess);
break;
case TMCB_REQUEST_FWDED:
if (ctx->main && (rtp_sess_pending(ctx->main) || rtp_sess_late(ctx->main))) {
LM_DBG("RTP relay already engaged in main branch\n");
goto end;
}
sess = rtp_relay_get_sess(ctx, rtp_relay_ctx_branch());
if (!sess) /* not engagned on this branch */ {
LM_DBG("RTP relay not engaged on branch %d!\n", rtp_relay_ctx_branch());
goto end;
}
if (rtp_sess_disabled(sess)) {
LM_DBG("rtp relay on branch %d is disabled\n", rtp_relay_ctx_branch());
goto end;
}
if (rtp_sess_late(sess)) {
LM_DBG("rtp relay on branch %d is late\n", rtp_relay_ctx_branch());
goto end;
}
memset(&info, 0, sizeof info);
info.body = get_body_part(p->req, TYPE_APPLICATION, SUBTYPE_SDP);
info.msg = p->req;
info.branch = sess->index;
if (ctx->main && sess != ctx->main) {
/* inherit props from ctx->main */
if (sess->server.set == -1)
sess->server.set = ctx->main->server.set;
if (!sess->relay)
sess->relay = ctx->main->relay;
}
rtp_relay_offer(&info, sess, ctx->main, RTP_RELAY_OFFER, NULL);
break;
default:
LM_BUG("unhandled callback type %d\n", type);
break;