/
rtpproxy.c
5896 lines (5227 loc) · 153 KB
/
rtpproxy.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) 2003-2008 Sippy Software, Inc., http://www.sippysoft.com
*
* 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
*
* History:
* ---------
* 2003-12-01 unforce_rtp_proxy introduced (sobomax)
*
* 2004-01-07 RTP proxy support updated to support new version of the
* RTP proxy (20040107).
*
* force_rtp_proxy() now inserts a special flag
* into the SDP body to indicate that this session already
* proxied and ignores sessions with such flag.
*
* Added run-time check for version of command protocol
* supported by the RTP proxy.
*
* 2004-01-16 Integrated slightly modified patch from Tristan Colgate,
* force_rtp_proxy function with IP as a parameter (janakj)
*
* 2004-02-21 force_rtp_proxy now accepts option argument, which
* consists of string of chars, each of them turns "on"
* some feature, currently supported ones are:
*
* `a' - flags that UA from which message is received
* doesn't support symmetric RTP;
* `l' - force "lookup", that is, only rewrite SDP when
* corresponding session is already exists in the
* RTP proxy. Only makes sense for SIP requests,
* replies are always processed in "lookup" mode;
* `i' - flags that message is received from UA in the
* LAN. Only makes sense when RTP proxy is running
* in the bridge mode.
*
* force_rtp_proxy can now be invoked without any arguments,
* as previously, with one argument - in this case argument
* is treated as option string and with two arguments, in
* which case 1st argument is option string and the 2nd
* one is IP address which have to be inserted into
* SDP (IP address on which RTP proxy listens).
*
* 2004-03-12 Added support for IPv6 addresses in SDPs. Particularly,
* force_rtp_proxy now can work with IPv6-aware RTP proxy,
* replacing IPv4 address in SDP with IPv6 one and vice versa.
* This allows creating full-fledged IPv4<->IPv6 gateway.
* See 4to6.cfg file for example.
*
* Two new options added into force_rtp_proxy:
*
* `f' - instructs rtpproxy to ignore marks inserted
* by another rtpproxy in transit to indicate
* that the session is already goes through another
* proxy. Allows creating chain of proxies.
* `r' - flags that IP address in SDP should be trusted.
* Without this flag, rtpproxy ignores address in the
* SDP and uses source address of the SIP message
* as media address which is passed to the RTP proxy.
*
* Protocol between rtpproxy and RTP proxy in bridge
* mode has been slightly changed. Now RTP proxy expects SER
* to provide 2 flags when creating or updating session
* to indicate direction of this session. Each of those
* flags can be either `e' or `i'. For example `ei' means
* that we received INVITE from UA on the "external" network
* network and will send it to the UA on "internal" one.
* Also possible `ie' (internal->external), `ii'
* (internal->internal) and `ee' (external->external). See
* example file alg.cfg for details.
*
* 2004-03-15 If the rtp proxy test failed (wrong version or not started)
* retry test from time to time, when some *rtpproxy* function
* is invoked. Minimum interval between retries can be
* configured via rtpproxy_disable_tout module parameter (default
* is 60 seconds). Setting it to -1 will disable periodic
* rechecks completely, setting it to 0 will force checks
* for each *rtpproxy* function call. (andrei)
*
* 2004-03-22 Fix assignment of rtpproxy_retr and rtpproxy_tout module
* parameters.
*
* 2004-03-22 Fix get_body position (should be called before get_callid)
* (andrei)
*
* 2004-03-24 Fix newport for null ip address case (e.g onhold re-INVITE)
* (andrei)
*
* 2004-09-30 added received port != via port test (andrei)
*
* 2004-10-10 force_socket option introduced (jiri)
*
* 2005-02-24 Added support for using more than one rtp proxy, in which
* case traffic will be distributed evenly among them. In addition,
* each such proxy can be assigned a weight, which will specify
* which share of the traffic should be placed to this particular
* proxy.
*
* Introduce fail-over mechanism, so that if SER detects that one
* of many proxies is no longer available it temporarily decreases
* its weight to 0, so that no traffic will be assigned to it.
* Such "disabled" proxies are periodically checked to see if they
* are back to normal in which case respective weight is restored
* resulting in traffic being sent to that proxy again.
*
* Those features can be enabled by specifying more than one "URI"
* in the rtpproxy_sock parameter, optionally followed by the weight,
* which if absent is assumed to be 1, for example:
*
* rtpproxy_sock="unix:/foo/bar=4 udp:1.2.3.4:3456=3 udp:5.6.7.8:5432=1"
*
* 2005-03-22 support for multiple media streams added (netch)
* 2005-07-14 SDP origin (o=) IP may be also changed (bogdan)
* 2006-03-28 Support for changing session-level SDP connection (c=) IP when
* media-description also includes connection information (bayan)
* 2007-04-13 Support multiple sets of rtp-proxies and set selection added
* (ancuta)
* 2007-04-26 Added some MI commands:
* nh_enable_rtpp used to enable or disable a specific rtp proxy
* nh_show_rtpp used to display information for all rtp proxies
* (ancuta)
* 2007-05-09 New function start_recording() allowing to start recording RTP
* session in the RTP proxy (Carsten Bock - ported from SER)
* - obsolete by rtpproxy_offer/rtpproxy_answer
* (osas)
*/
#include <sys/types.h>
#include <netinet/in.h>
#ifndef __USE_BSD
#define __USE_BSD
#endif
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif
#include <sys/un.h>
#include <poll.h>
#include <stdlib.h>
#include <sys/uio.h>
#include "../../dprint.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../error.h"
#include "../../forward.h"
#include "../../mem/mem.h"
#include "../../mod_fix.h"
#include "../../timer.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_uri.h"
#include "../../parser/parser_f.h"
#include "../../parser/sdp/sdp_helpr_funcs.h"
#include "../../parser/sdp/sdp.h"
#include "../../db/db.h"
#include "../../parser/parse_content.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_body.h"
#include "../../msg_callbacks.h"
#include "../../evi/evi_modules.h"
#include "../../lib/dassert.h"
#include "../dialog/dlg_load.h"
#include "../tm/tm_load.h"
#include "rtpproxy.h"
#include "rtpproxy_load.h"
#include "nhelpr_funcs.h"
#include "rtpproxy_stream.h"
#include "rtpproxy_callbacks.h"
#include "rtpproxy_vcmd.h"
#include "rtppn_connect.h"
#include "../rtp_relay/rtp_relay.h"
#define NH_TABLE_VERSION 0
#if !defined(AF_LOCAL)
#define AF_LOCAL AF_UNIX
#endif
#if !defined(PF_LOCAL)
#define PF_LOCAL PF_UNIX
#endif
#define DEFAULT_RTPP_SET_ID 0
#define MI_ENABLE_RTP_PROXY "rtpproxy_enable"
#define MI_MIN_RECHECK_TICKS 0
#define MI_MAX_RECHECK_TICKS (unsigned int)-1
#define MI_SHOW_RTP_PROXIES "rtpproxy_show"
#define MI_RELOAD_RTP_PROXIES "rtpproxy_reload"
#define MI_RTP_PROXY_NOT_FOUND "RTP proxy not found"
#define MI_RTP_PROXY_NOT_FOUND_LEN (sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
#define MI_PING_DISABLED "NATping disabled from script"
#define MI_PING_DISABLED_LEN (sizeof(MI_PING_DISABLED)-1)
#define MI_SET "Set"
#define MI_SET_LEN (sizeof(MI_SET)-1)
#define MI_INDEX "index"
#define MI_INDEX_LEN (sizeof(MI_INDEX)-1)
#define MI_DISABLED "disabled"
#define MI_DISABLED_LEN (sizeof(MI_DISABLED)-1)
#define MI_WEIGHT "weight"
#define MI_WEIGHT_LEN (sizeof(MI_WEIGHT)-1)
#define MI_RECHECK_TICKS "recheck_ticks"
#define MI_RECHECK_T_LEN (sizeof(MI_RECHECK_TICKS)-1)
/* param names to be stored in the dialog */
static str param1_name = str_init("rtpproxy_1");
str param1_bavp_name = str_init("$bavp(5589965)");
pv_spec_t param1_spec;
static str param2_name = str_init("rtpproxy_2");
str param2_bavp_name = str_init("$bavp(5589966)");
pv_spec_t param2_spec;
static str param3_name = str_init("rtpproxy_3");
str param3_bavp_name = str_init("$bavp(5589967)");
pv_spec_t param3_spec;
static str late_name = str_init("late_negotiation");
/* parameters name for event signaling */
static str event_name = str_init("E_RTPPROXY_STATUS");
static str socket_name = str_init("socket");
static str status_name = str_init("status");
static str status_connected = str_init("active");
static str status_disconnected = str_init("inactive");
static str rtpproxy_dtmf_event_name = str_init("E_RTPPROXY_DTMF");
struct rtpproxy_event_params {
str name;
evi_param_p param;
};
struct rtpproxy_event_params rtpproxy_event_params[] = {
{ /* 0 */ str_init("digit"), NULL },
{ /* 1 */ str_init("duration"), NULL },
{ /* 2 */str_init("volume"), NULL },
{ /* 3 */str_init("id"), NULL },
{ /* 4 */str_init("is_callid"), NULL },
{ /* 5 */str_init("stream"), NULL },
};
evi_params_p rtpproxy_dtmf_params;
static int extract_mediainfo(str *, str *, str *);
static int alter_mediaip(struct sip_msg *, str *, str *, int, str *, int, int);
static void gencookie(struct iovec *);
static int rtpp_test(struct rtpp_node*, int, int);
static int unforce_rtp_proxy_f(struct sip_msg* msg, nh_set_param_t *pset,
pv_spec_t *var);
static int engage_rtp_proxy5_f(struct sip_msg *msg, str *param1, str *param2,
nh_set_param_t *param3, pv_spec_t *param4, pv_spec_t *param5);
static int force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, nh_set_param_t *setid,
pv_spec_t *var, pv_spec_t *ipvar, str *body, int offer);
static int rtpproxy_recording(struct sip_msg* msg, nh_set_param_t *setid,
pv_spec_t *var, str *flags, str *destination, int *stream_no);
static int rtpproxy_answer6_f(struct sip_msg *msg, str *param1, str *param2,
nh_set_param_t *param3, pv_spec_t *param4, pv_spec_t *param5, pv_spec_t *param6);
static int rtpproxy_offer6_f(struct sip_msg *msg, str *param1, str *param2,
nh_set_param_t *param3, pv_spec_t *param4, pv_spec_t *param5, pv_spec_t *param7);
static inline int rtpproxy_stats_f(struct sip_msg *msg,
pv_spec_t *pup, pv_spec_t *pdown, pv_spec_t *psent, pv_spec_t *pfail,
nh_set_param_t *pset, pv_spec_t *pvar);
static inline int rtpproxy_all_stats_f(struct sip_msg *msg, pv_spec_t *pavp,
nh_set_param_t *pset, pv_spec_t *pvar);
static int rtpp_init_extra_stats(void);
static int add_rtpproxy_socks(struct rtpp_set * rtpp_list, char * rtpproxy);
static int fixup_set_id(void ** param);
static int fixup_free_set_id(void ** param);
static int fixup_all_stats(void ** param);
static struct rtpp_set * select_rtpp_set(int id_set);
static int rtpproxy_set_store(modparam_t type, void * val);
static int rtpproxy_set_notify(modparam_t type, void * val);
static int rtpproxy_add_rtpproxy_set( char * rtp_proxies, int set_id);
static int _add_proxies_from_database();
static int unforce_rtpproxy(struct sip_msg* msg, struct rtpp_args *args, pv_spec_t *var);
static int mod_init(void);
static int mod_preinit(void);
static int child_init(int);
static void mod_destroy(void);
static int mi_child_init(void);
static int engage_force_rtpproxy(struct dlg_cell *dlg, struct sip_msg *msg);
/*mi commands*/
static mi_response_t *mi_enable_rtp_proxy_1(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_enable_rtp_proxy_2(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_show_rtpproxies(const mi_params_t *params,
struct mi_handler *async_hdl);
static mi_response_t *mi_reload_rtpproxies(const mi_params_t *params,
struct mi_handler *async_hdl);
void free_rtpp_nodes(struct rtpp_set *);
void free_rtpp_sets();
struct dlg_binds dlg_api;
/* TM support for saving parameters */
struct tm_binds tm_api;
static int rtpproxy_api_offer(struct rtp_relay_session *sess,
struct rtp_relay_server *server, str *body,
str *ip, str *type, str *in_iface, str *out_iface,
str *global_flags, str *flags, str *extra_flags);
static int rtpproxy_api_answer(struct rtp_relay_session *sess,
struct rtp_relay_server *server, str *body,
str *ip, str *type, str *in_iface, str *out_iface,
str *global_flags, str *flags, str *extra_flags);
static int rtpproxy_api_delete(struct rtp_relay_session *sess, struct rtp_relay_server *server,
str *flags, str *extra);
static int rtpproxy_api_copy_offer(struct rtp_relay_session *sess,
struct rtp_relay_server *server, void **_ctx, str *flags,
unsigned int copy_flags, unsigned int streams, str *body);
static int rtpproxy_api_copy_answer(struct rtp_relay_session *sess,
struct rtp_relay_server *server, void *_ctx, str *flags, str *body);
static int rtpproxy_api_copy_delete(struct rtp_relay_session *sess,
struct rtp_relay_server *server, void *_ctx, str *flags);
static int rtpproxy_api_copy_serialize(void *_ctx, bin_packet_t *packet);
static int rtpproxy_api_copy_deserialize(void **_ctx, bin_packet_t *packet);
int connect_rtpproxies();
int update_rtpp_proxies();
static inline void raise_rtpproxy_event(struct rtpp_node *node, int status);
static struct {
const char *s;
int len;
int is_rtp;
} sup_ptypes[] = {
{.s = "udp", .len = 3, .is_rtp = 0},
{.s = "udptl", .len = 5, .is_rtp = 0},
{.s = "rtp/avp", .len = 7, .is_rtp = 1},
{.s = "rtp/avpf", .len = 8, .is_rtp = 1},
{.s = "rtp/savp", .len = 8, .is_rtp = 1},
{.s = "rtp/savpf", .len = 9, .is_rtp = 1},
{.s = "dccp/tls/rtp/savp", .len = 17, .is_rtp = 1},
{.s = "dccp/tls/rtp/savpf", .len = 18, .is_rtp = 1},
{.s = "udp/tls/rtp/savp", .len = 16, .is_rtp = 1},
{.s = "udp/tls/rtp/savpf", .len = 17, .is_rtp = 1},
{.s = "udp/bfcp", .len = 8, .is_rtp = 0},
{.s = NULL, .len = 0, .is_rtp = 0}
};
static int rtpproxy_disable_tout = 60;
static int rtpproxy_retr = 5;
int rtpproxy_tout = 1000;
static char *rtpproxy_timeout = 0;
static int rtpproxy_autobridge = 0;
static pid_t mypid;
static int myrand = 0;
static unsigned int myseqn = 0;
static str nortpproxy_str = str_init("a=nortpproxy:yes");
str rtpp_notify_socket = {0, 0};
/*
* 0 - Unix socket
* 1 - TCP socket
*/
int rtpp_notify_socket_un = 0;
/* used in rtpproxy_set_store() */
static int rtpp_sets=0;
static char **rtpp_strings=0;
static int rtpp_set_count = 0;
/* RTP proxy balancing list */
struct rtpp_set_head ** rtpp_set_list =0;
struct rtpp_set ** default_rtpp_set=0;
static int default_rtpp_set_no = DEFAULT_RTPP_SET_ID;
/* array with the sockets used by rtpporxy (per process)*/
static int *rtpp_socks = 0;
static unsigned int *rtpp_no = 0;
static unsigned int *list_version;
static unsigned int my_version = 0;
static unsigned int rtpp_number = 0;
/* DB support for loading proxies */
static str db_url = {NULL, 0};
static str table = str_init("rtpproxy_sockets");
static str rtpp_sock_col = str_init("rtpproxy_sock");
static str set_id_col = str_init("set_id");
static db_con_t *db_connection = NULL;
static db_func_t db_functions;
static int *rtpproxy_port;
static gen_lock_t *rtpproxy_port_lock;
static int rtpproxy_port_min = 35000;
static int rtpproxy_port_max = 65000;
static str rtpproxy_media_ip = str_init("127.0.0.1");
static int rtpproxy_gen_port(void)
{
int port;
lock_get(rtpproxy_port_lock);
if ((*rtpproxy_port)++ >= rtpproxy_port_max)
*rtpproxy_port = rtpproxy_port_min;
port = *rtpproxy_port;
lock_release(rtpproxy_port_lock);
return port;
}
static event_id_t ei_id = EVI_ERROR;
static event_id_t rtpproxy_dtmf_event = EVI_ERROR;
rw_lock_t *nh_lock=NULL;
static cmd_export_t cmds[] = {
{"rtpproxy_unforce", (cmd_function)unforce_rtp_proxy_f, {
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"rtpproxy_engage", (cmd_function)engage_rtp_proxy5_f, {
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"rtpproxy_start_recording", (cmd_function)rtpproxy_recording, {
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
{"rtpproxy_offer", (cmd_function)rtpproxy_offer6_f, {
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"rtpproxy_answer", (cmd_function)rtpproxy_answer6_f, {
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0},
{0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"rtpproxy_stream2uac", (cmd_function)rtpproxy_stream2uac4_f, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE | ONREPLY_ROUTE | LOCAL_ROUTE},
{"rtpproxy_stream2uas", (cmd_function)rtpproxy_stream2uas4_f, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE | ONREPLY_ROUTE | LOCAL_ROUTE},
{"rtpproxy_stop_stream2uac", (cmd_function)rtpproxy_stop_stream2uac2_f, {
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE | ONREPLY_ROUTE | LOCAL_ROUTE},
{"rtpproxy_stop_stream2uas", (cmd_function)rtpproxy_stop_stream2uas2_f, {
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE | ONREPLY_ROUTE | LOCAL_ROUTE},
{"rtpproxy_stats", (cmd_function)rtpproxy_stats_f, {
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"rtpproxy_all_stats", (cmd_function)rtpproxy_all_stats_f, {
{CMD_PARAM_VAR, fixup_all_stats, 0},
{CMD_PARAM_INT | CMD_PARAM_OPT, fixup_set_id, fixup_free_set_id},
{CMD_PARAM_VAR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"load_rtpproxy", (cmd_function)load_rtpproxy, {{0,0,0}}, 0},
{0,0,{{0,0,0}},0}
};
static param_export_t params[] = {
{"nortpproxy_str", STR_PARAM, &nortpproxy_str.s },
{"rtpproxy_sock", STR_PARAM|USE_FUNC_PARAM,
(void*)rtpproxy_set_store },
{"rtpproxy_disable_tout", INT_PARAM, &rtpproxy_disable_tout },
{"rtpproxy_retr", INT_PARAM, &rtpproxy_retr },
{"rtpproxy_timeout", STR_PARAM, &rtpproxy_timeout },
{"rtpproxy_autobridge", INT_PARAM, &rtpproxy_autobridge },
{"default_set", INT_PARAM, &default_rtpp_set_no },
{"db_url", STR_PARAM, &db_url.s },
{"db_table", STR_PARAM, &table.s },
{"rtpp_socket_col", STR_PARAM, &rtpp_sock_col.s },
{"set_id_col", STR_PARAM, &set_id_col.s },
{"rtpp_notify_socket", STR_PARAM|USE_FUNC_PARAM,
(void*)rtpproxy_set_notify },
{"generated_sdp_port_min",INT_PARAM, &rtpproxy_port_min },
{"generated_sdp_port_max",INT_PARAM, &rtpproxy_port_max },
{"generated_sdp_media_ip",STR_PARAM, &rtpproxy_media_ip.s },
{0, 0, 0}
};
static mi_export_t mi_cmds[] = {
{ MI_ENABLE_RTP_PROXY, 0, 0, 0, {
{mi_enable_rtp_proxy_1, {"url", "enable", 0}},
{mi_enable_rtp_proxy_2, {"url", "enable", "setid", 0}},
{EMPTY_MI_RECIPE}}
},
{ MI_SHOW_RTP_PROXIES, 0, 0, 0, {
{mi_show_rtpproxies, {0}},
{EMPTY_MI_RECIPE}}
},
{ MI_RELOAD_RTP_PROXIES, 0, 0, mi_child_init, {
{mi_reload_rtpproxies, {0}},
{EMPTY_MI_RECIPE}}
},
{EMPTY_MI_EXPORT}
};
static proc_export_t procs[] = {
{"RTPP notification receiver", 0, 0, notification_listener_process, 1,
PROC_FLAG_INITCHILD|PROC_FLAG_HAS_IPC|PROC_FLAG_NEEDS_SCRIPT},
{0,0,0,0,0,0}
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tm", DEP_ABORT },
{ MOD_TYPE_DEFAULT, "dialog", DEP_SILENT },
{ MOD_TYPE_DEFAULT, "rtp_relay", DEP_SILENT|DEP_REVERSE },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ "db_url", get_deps_sqldb_url },
{ NULL, NULL },
},
};
struct module_exports exports = {
"rtpproxy",
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
0, /* load function */
&deps, /* OpenSIPS module dependencies */
cmds,
NULL,
params,
0, /* exported statistics */
mi_cmds, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
mod_preinit,
mod_init,
0, /* reply processing */
mod_destroy, /* destroy function */
child_init,
0 /* reload confirm function */
};
static int rtpproxy_set_store(modparam_t type, void * val){
char * p;
int len;
p = (char* )val;
if(p==0 || *p=='\0'){
return 0;
}
if(rtpp_sets==0){
rtpp_strings = (char**)pkg_malloc(sizeof(char*));
if(!rtpp_strings){
LM_ERR("no pkg memory left\n");
return -1;
}
} else {/*realloc to make room for the current set*/
rtpp_strings = (char**)pkg_realloc(rtpp_strings,
(rtpp_sets+1)* sizeof(char*));
if(!rtpp_strings){
LM_ERR("no pkg memory left\n");
return -1;
}
}
/*allocate for the current set of urls*/
len = strlen(p);
rtpp_strings[rtpp_sets] = (char*)pkg_malloc((len+1)*sizeof(char));
if(!rtpp_strings[rtpp_sets]){
LM_ERR("no pkg memory left\n");
return -1;
}
memcpy(rtpp_strings[rtpp_sets], p, len);
rtpp_strings[rtpp_sets][len] = '\0';
rtpp_sets++;
return 0;
}
static int rtpproxy_set_notify(modparam_t type, void * val)
{
char * p;
p = (char* )val;
if(p==0 || *p=='\0'){
return 0;
}
rtpp_notify_socket.s = p;
rtpp_notify_socket.len = strlen(p);
exports.procs = procs;
return 0;
}
static int add_rtpproxy_socks(struct rtpp_set * rtpp_list,
char * rtpproxy){
/* Make rtp proxies list. */
char *p, *p1, *p2, *p3, *p4, *plim;
struct rtpp_node *pnode;
int weight;
p = rtpproxy;
plim = p + strlen(p);
for(;;) {
weight = 1;
while (*p && isspace((int)*p))
++p;
if (p >= plim)
break;
p1 = p;
while (*p && !isspace((int)*p))
++p;
if (p <= p1)
break; /* may happen??? */
/* Have weight specified? If yes, scan it */
p2 = memchr(p1, '=', p - p1);
if (p2 != NULL) {
weight = strtoul(p2 + 1, NULL, 10);
} else {
p2 = p;
}
/* check to see if anything was advertised */
p3 = q_memrchr(p1, '|', p2 - p1);
if (p3) {
p4 = p2;
p2 = p3;
p3++;
while (p3 < p2 && isspace((int)*p3))
p3++;
while (p2 > p1 && isspace((int)*p2))
p2--;
}
pnode = shm_malloc(sizeof(struct rtpp_node) +
(p2 - p1 + 1) /* socket */ +
(p3 ? (p4 - p3 + 1): 0));
if (pnode == NULL) {
LM_ERR("no shm memory left\n");
return -1;
}
memset(pnode, 0, sizeof(*pnode));
pnode->rn_url.s = (char *)(pnode + 1);
pnode->idx = *rtpp_no;
*rtpp_no = *rtpp_no + 1;
pnode->rn_recheck_ticks = 0;
pnode->rn_weight = weight;
pnode->rn_umode = CM_UNIX;
pnode->rn_disabled = 0;
memcpy(pnode->rn_url.s, p1, p2 - p1);
pnode->rn_url.s[p2 - p1] = '\0';
pnode->rn_url.len = p2-p1;
if (p3) {
pnode->adv_address = pnode->rn_url.s + pnode->rn_url.len + 1;
memcpy(pnode->adv_address, p3, p4 - p3);
pnode->adv_address[p4 - p3] = 0;
}
LM_DBG("url: %s, len: %i, weight: %d, adv: [%s]\n",
pnode->rn_url.s, pnode->rn_url.len, weight, pnode->adv_address);
/* Leave only address in rn_address */
pnode->rn_address = pnode->rn_url.s;
if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) {
pnode->rn_umode = CM_UDP;
pnode->rn_address += 4;
} else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) {
pnode->rn_umode = CM_UDP6;
pnode->rn_address += 5;
} else if (strncasecmp(pnode->rn_address, "tcp:", 4) == 0) {
pnode->rn_umode = CM_TCP;
pnode->rn_address += 4;
} else if (strncasecmp(pnode->rn_address, "tcp6:", 5) == 0) {
pnode->rn_umode = CM_TCP6;
pnode->rn_address += 5;
} else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
pnode->rn_umode = CM_UNIX;
pnode->rn_address += 5;
}
if (rtpp_list->rn_first == NULL) {
rtpp_list->rn_first = pnode;
} else {
rtpp_list->rn_last->rn_next = pnode;
}
rtpp_list->rn_last = pnode;
rtpp_list->rtpp_node_count++;
}
return 0;
}
/* 0-success
* -1 - error
* */
static int rtpproxy_add_rtpproxy_set( char * rtp_proxies, int set_id)
{
char *p,*p2;
struct rtpp_set * rtpp_list;
unsigned int my_current_id;
str id_set;
int new_list;
/* empty definition? */
p= rtp_proxies;
if(!p || *p=='\0'){
return 0;
}
for(;*p && isspace(*p);p++);
if(*p=='\0'){
return 0;
}
if(set_id < 0)
{
rtp_proxies = strstr(p, "==");
if(rtp_proxies){
if(*(rtp_proxies +2)=='\0'){
LM_ERR("script error -invalid rtp proxy list!\n");
return -1;
}
*rtp_proxies = '\0';
p2 = rtp_proxies-1;
for(;isspace(*p2); *p2 = '\0',p2--);
id_set.s = p; id_set.len = p2 - p+1;
if(id_set.len <= 0 ||str2int(&id_set, &my_current_id)<0 ){
LM_ERR("script error -invalid set_id value!\n");
return -1;
}
rtp_proxies+=2;
}else{
rtp_proxies = p;
my_current_id = default_rtpp_set_no;
}
}
else{
rtp_proxies = p;
my_current_id = set_id;
}
LM_DBG("Adding proxy in set-id %d [%s]\n", my_current_id, rtp_proxies);
for(;*rtp_proxies && isspace(*rtp_proxies);rtp_proxies++);
if(!(*rtp_proxies)){
LM_ERR("script error -empty rtp_proxy list\n");
return -1;
}
/*search for the current_id*/
rtpp_list = (*rtpp_set_list) ? (*rtpp_set_list)->rset_first : 0;
while( rtpp_list != 0 && rtpp_list->id_set!=my_current_id)
rtpp_list = rtpp_list->rset_next;
LM_DBG("List %sfound (%p) for id %d\n", rtpp_list ? "" : "not ",
rtpp_list, my_current_id);
if(rtpp_list==NULL){ /*if a new id_set : add a new set of rtpp*/
rtpp_list = shm_malloc(sizeof(struct rtpp_set));
if(!rtpp_list){
LM_ERR("no shm memory left\n");
return -1;
}
memset(rtpp_list, 0, sizeof(struct rtpp_set));
rtpp_list->id_set = my_current_id;
new_list = 1;
} else {
new_list = 0;
}
if(add_rtpproxy_socks(rtpp_list, rtp_proxies)!= 0){
/*if this list will not be inserted, clean it up*/
goto error;
}
if (new_list) {
if(!(*rtpp_set_list)){/*initialize the list of set -
executed only on the first call*/
*rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head));
if(!(*rtpp_set_list)){
LM_ERR("no shm memory left\n");
return -1;
}
memset(*rtpp_set_list, 0, sizeof(struct rtpp_set_head));
}
/*update the list of set info*/
if(!(*rtpp_set_list)->rset_first){
(*rtpp_set_list)->rset_first = rtpp_list;
}else{
(*rtpp_set_list)->rset_last->rset_next = rtpp_list;
}
(*rtpp_set_list)->rset_last = rtpp_list;
rtpp_set_count++;
}
return 0;
error:
return -1;
}
static int fixup_all_stats(void ** param)
{
if (((pv_spec_t*)*param)->type != PVT_AVP) {
LM_ERR("invalid pvar type - only AVPs are allowed!\n");
return E_SCRIPT;
}
return 0;
}
static int fixup_set_id(void ** param)
{
struct rtpp_set* rtpp_list;
nh_set_param_t * pset;
pset = (nh_set_param_t *) pkg_malloc(sizeof(nh_set_param_t));
if(pset == NULL){
LM_ERR("no more pkg memory to allocate set parameter\n");
return E_OUT_OF_MEM;
}
memset(pset, 0, sizeof(nh_set_param_t));
rtpp_list = select_rtpp_set(*(int*)*param);
if(rtpp_list ==0){
/* simply mark it as undefined and we search it one more time
* at run-time, after the database has been updated */
pset->t = NH_VAL_SET_UNDEF;
pset->v.int_set = *(int*)*param;
} else {
pset->t = NH_VAL_SET_FIXED ;
pset->v.fixed_set = rtpp_list;
}
*param = (void *) pset;
return 0;
}
static int fixup_free_set_id(void ** param)
{
pkg_free(*param);
return 0;
}
static mi_response_t *mi_enable_rtp_proxy(const mi_params_t *params,
unsigned int set_id)
{
str rtpp_url;
int enable;
struct rtpp_set * rtpp_list;
struct rtpp_node * crt_rtpp;
int found;
found = 0;
if(*rtpp_set_list ==NULL)
goto end;
if (get_mi_string_param(params, "url", &rtpp_url.s, &rtpp_url.len) < 0)
return init_mi_param_error();
if(rtpp_url.s == NULL || rtpp_url.len ==0)
return init_mi_error(400, MI_SSTR("Empty url"));
if (get_mi_int_param(params, "enable", &enable) < 0)
return init_mi_param_error();
for(rtpp_list = (*rtpp_set_list)->rset_first; rtpp_list != NULL;
rtpp_list = rtpp_list->rset_next){
/* if set_id given, check only the list with the matching set_id */
if ( (set_id!=(unsigned int)(-1)) && set_id!=rtpp_list->id_set )
continue;
for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
crt_rtpp = crt_rtpp->rn_next){
/*found a matching rtpp*/
if(crt_rtpp->rn_url.len == rtpp_url.len){
if(strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0){
/*set the enabled/disabled status*/
found = 1;
crt_rtpp->rn_recheck_ticks =
enable? MI_MIN_RECHECK_TICKS : MI_MAX_RECHECK_TICKS;
crt_rtpp->rn_disabled = enable?0:1;
raise_rtpproxy_event(crt_rtpp, crt_rtpp->rn_disabled);
}
}
}
}
end:
if(found)
return init_mi_result_ok();
return init_mi_error(404,MI_RTP_PROXY_NOT_FOUND,MI_RTP_PROXY_NOT_FOUND_LEN);
}
static mi_response_t *mi_enable_rtp_proxy_1(const mi_params_t *params,
struct mi_handler *async_hdl)
{
return mi_enable_rtp_proxy(params, (unsigned int)(-1));
}
static mi_response_t *mi_enable_rtp_proxy_2(const mi_params_t *params,
struct mi_handler *async_hdl)
{
int set_id;
if (get_mi_int_param(params, "setid", &set_id) < 0)
return init_mi_param_error();
return mi_enable_rtp_proxy(params, (unsigned int)set_id);
}
static mi_response_t *mi_reload_rtpproxies(const mi_params_t *params,
struct mi_handler *async_hdl)
{
struct rtpp_set *it;
if(db_url.s == NULL) {
LM_ERR("Dynamic loading of rtpproxies not enabled\n");
return init_mi_error(400, MI_SSTR("Dynamic loading not enabled"));
}
lock_start_write( nh_lock );
if(*rtpp_set_list) {
for (it = (*rtpp_set_list)->rset_first; it; it = it->rset_next)
free_rtpp_nodes(it);
}
*rtpp_no = 0;
(*list_version)++;
if(_add_proxies_from_database() < 0)
goto error;
if (update_rtpp_proxies())
goto error;
/* update pointer to default_rtpp_set*/
*default_rtpp_set = select_rtpp_set(default_rtpp_set_no);
if (*default_rtpp_set == NULL)
LM_WARN("there is no rtpproxy engine in the default set %d\n",
default_rtpp_set_no);
/* release the readers */
lock_stop_write( nh_lock );
return init_mi_result_ok();
error:
lock_stop_write( nh_lock );
return init_mi_error(500, MI_SSTR("Internal error"));