-
Notifications
You must be signed in to change notification settings - Fork 3
/
opensips.cfg
1146 lines (848 loc) · 25.9 KB
/
opensips.cfg
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) 2024 SIP Point Consulting SRL
#
# 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 3 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
#
####### Global Parameters #########
log_level = 3
xlog_level=3
stderror_enabled=yes
syslog_enabled=no
syslog_facility=LOG_LOCAL0
udp_workers = 4
tcp_workers = 4
/* uncomment the following lines to enable debugging */
#debug_mode=yes
/* uncomment the next line to enable the auto temporary blacklisting of
not available destinations (default disabled) */
#disable_dns_blacklist=no
/* uncomment the next line to enable IPv6 lookup after IPv4 dns
lookup failures (default disabled) */
#dns_try_ipv6=yes
/* comment the next line to enable the auto discovery of local aliases
based on reverse DNS on IPs */
auto_aliases = no
disable_dns_failover = no
## IDs
server_header="Server: OpenSIPS CE SoftSwitch"
user_agent_header="User-Agent: OpenSIPS CE SoftSwitch"
socket=udp:${OPENSIPS_IP}:5060 as ${HOST_IP}
socket=tcp:${OPENSIPS_IP}:5060 as ${HOST_IP}
ifelse(`${ENABLE_HOMER}',`yes',`
socket=hep_udp:${OPENSIPS_IP}:9060
')
####### Modules Section ########
#set module path
mpath = "/usr/lib/x86_64-linux-gnu/opensips/modules"
#### HTTPD module
loadmodule "httpd.so"
modparam("httpd", "ip", "${OPENSIPS_IP}")
#### MI_HTTP module
loadmodule "mi_http.so"
#### FIFO Management Interface
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
#### SIGNALING module
loadmodule "signaling.so"
#### StateLess module
loadmodule "sl.so"
#### Transaction Module
loadmodule "tm.so"
modparam("tm", "fr_timeout", 5)
modparam("tm", "fr_inv_timeout", 30)
modparam("tm", "wt_timer", 5)
modparam("tm", "restart_fr_on_each_reply", 0)
modparam("tm", "onreply_avp_mode", 1)
modparam("tm", "disable_6xx_block", 1)
#### Record Route Module
loadmodule "rr.so"
modparam("rr", "append_fromtag", 1)
#### MAX ForWarD module
loadmodule "maxfwd.so"
#### SIP MSG OPerationS module
loadmodule "sipmsgops.so"
#### TEXT OPerationS module
loadmodule "textops.so"
#### MYSQL module
loadmodule "db_mysql.so"
#### DIALOG Module
loadmodule "dialog.so"
modparam("dialog", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("dialog", "dlg_match_mode", 1)
modparam("dialog", "db_mode", 2)
modparam("dialog", "default_timeout", 21600) # 6 hours timeout
#### TOPOLOGY HIDING Module
loadmodule "topology_hiding.so"
#### DOMAINS module
loadmodule "domain.so"
modparam("domain", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("domain", "db_mode", 1)
### SQL_OPS module
loadmodule "sqlops.so"
modparam("sqlops", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "working_mode_preset", "single-instance-sql-write-back")
modparam("usrloc", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("usrloc", "use_domain", 1)
modparam("usrloc", "timer_interval", 10)
modparam("usrloc", "nat_bflag", "NAT_DST")
modparam("usrloc", "desc_time_order", 1)
#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
modparam("registrar", "received_avp", "$avp(received)")
modparam("registrar", "max_contacts", 10)
modparam("registrar", "max_expires", 3600)
#### NAT Helper module
loadmodule "nathelper.so"
modparam("nathelper", "natping_interval", 30)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "received_avp", "$avp(received)")
modparam("nathelper", "sipping_bflag", "NAT_DST")
modparam("nathelper", "sipping_from", "sip:pinger@${OPENSIPS_IP}")
#### AUTH module
loadmodule "auth.so"
modparam("auth", "nonce_expire", 30)
modparam("auth", "disable_nonce_check", 1)
##### AUTH_DB module
loadmodule "auth_db.so"
modparam("auth_db", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("auth_db", "use_domain", 1)
modparam("auth_db", "password_column", "ha1")
modparam("auth_db", "calculate_ha1", 0)
modparam("auth_db", "load_credentials","$avp(caller_acls)=acls; $avp(caller_cli)=cli;")
#### ACCounting module
loadmodule "acc.so"
modparam("acc", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("acc", "extra_fields", "db: caller_ip; caller_ua; callee_ip; callee_ua")
modparam("acc", "leg_fields", "db: caller_id;
callee_id;
leg_status;
")
#### DIALPLAN module
loadmodule "dialplan.so"
modparam("dialplan", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### Dynamic ROUTING module
loadmodule "drouting.so"
modparam("drouting", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### Alias-DB module
loadmodule "alias_db.so"
modparam("alias_db", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### UAC module
loadmodule "uac.so"
#### RTP_RELAY module
loadmodule "rtp_relay.so"
#### RTPPROXY module
loadmodule "rtpproxy.so"
modparam("rtpproxy", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### Core Presence modules
loadmodule "presence.so"
loadmodule "presence_xml.so"
modparam("presence", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("presence_xml","force_active",1)
#### XCap parameters
loadmodule "xcap.so"
modparam("xcap", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
#### Dispatcher module
loadmodule "dispatcher.so"
modparam("dispatcher", "db_url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_IP}/${MYSQL_DATABASE}")
modparam("dispatcher", "ds_ping_from", "sip:ds@PRIV_IP")
modparam("dispatcher", "ds_ping_interval", 30)
modparam("dispatcher", "ds_probing_threshhold", 1)
modparam("dispatcher", "options_reply_codes", "404")
modparam("dispatcher", "setid_pvar", "$avp(ds_set)")
#### ConFiG UTILS module
loadmodule "cfgutils.so"
ifelse(`${ENABLE_HOMER}',`yes',`
#### HEP module
loadmodule "proto_hep.so"
modparam("proto_hep", "hep_id", "[hid] ${HOMER_IP}:${HOMER_PORT}; transport=udp; version=3")
#### TRACER module
loadmodule "tracer.so"
modparam("tracer", "trace_on", 1)
modparam("tracer", "trace_id", "[tid] uri=hep:hid")
')
loadmodule "proto_udp.so"
loadmodule "proto_tcp.so"
####### Routing Logic ########
# main request routing logic
route{
ifelse(`${ENABLE_HOMER}',`yes',`trace("tid", "t", "sip|xlog");')
force_rport();
route(handle_pings);
xlog("[$ci] incoming $rm from $si:$sp, From=$fu, RURI=$ru\n");
route(sanity_checks);
route(do_nat_detection);
route(handle_sequentials);
# CANCEL processing
if (is_method("CANCEL")) {
if (t_check_trans())
t_relay();
exit;
}
# absorb retransmissions, but do not create transaction
t_check_trans();
# strip any pre-loaded Route hdrs
remove_hf("Route");
xlog("[$ci] handling initial request\n");
if (is_method("REGISTER"))
route(handle_register);
route(do_auth);
route(do_inter_domain_routing);
### valid initial requests for our local domains
### continue handling requests per method
$avp(start_ts) = $Ts;
if (is_method("INVITE"))
route(handle_invite);
if (is_method("SUBSCRIBE|PUBLISH"))
route(handle_presence);
send_reply(405, "Method Not Allowed");
exit;
}
route[handle_pings]
{
# keepalive notifies replied ok
if ( is_method("NOTIFY|OPTIONS") && !has_totag() && is_myself("$rd") ) {
send_reply(200, "OK");
exit;
}
}
route[sanity_checks]
{
if (!mf_process_maxfwd_header(10)) {
send_reply(483,"Too Many Hops");
exit;
}
}
route[do_nat_detection]
{
# test only received differences (port and IP)
if (nat_uac_test("private-contact,diff-ip-src-via,diff-port-src-via"))
setflag("NAT_SRC");
if (isflagset("NAT_SRC") && !is_method("REGISTER"))
fix_nated_contact(";bnat=yes");
}
route[handle_sequentials]
{
if (has_totag()) {
# handle hop-by-hop ACK (no routing required)
if ( is_method("ACK") && t_check_trans() ) {
t_relay();
exit;
}
# handle sequential presence
if (is_method("SUBSCRIBE|NOTIFY") && is_myself("$rd")) {
# in-dialog subscribe requests for presence
route(handle_presence);
exit;
}
if ( !topology_hiding_match() && !loose_route() ) {
send_reply(404,"Not here");
exit;
}
# RURI pointing to a nated destination
if (ruri_has_param("bnat", "yes")) {
setbflag("NAT_DST");
ruri_del_param("bnat");
}
xlog("[$ci] routing sequential request to $ru/$du\n");
route(relay);
exit;
}
}
route[handle_register]
{
if (!is_domain_local("$td")) {
xlog("[$ci] trying to register non local domain <$td>\n");
send_reply(403, "Forbidden domain" );
exit;
}
t_newtran();
if (!www_authorize("","subscriber")) {
www_challenge("","auth");
exit;
}
if ($au!=$tU) {
xlog("[$ci] user $tu using wrong register credentials $au / $ar\n");
send_reply(403, "Forbidden ID");
exit;
}
if (!is_acl_set(caller_acls, ACL_ENABLED)) {
xlog("[$ci] registration forbidden for disabled user\n");
send_reply(403, "Forbidden");
exit;
}
if (!is_acl_set(caller_acls, ACL_FORCE_NAT))
setflag("NAT_SRC");
# save source NAT flag
if (isflagset("NAT_SRC")) {
fix_nated_register();
setbflag("NAT_DST");
}
if ($socket_in(proto)!="UDP")
setflag("TCP_PERSISTENT");
xlog("[$ci] saving contacts for user $tu\n");
if (!save("location")) {
sl_reply_error();
}
exit;
}
route[do_auth]
{
if (is_from_gw(1)) {
# call from an known GW
xlog("[$ci] call from GW $si/$sp ($avp(___dr_gw_id__)) detected\n");
# mark source leg as GW
setflag("GW_ORIG_SRC");
setflag("GW_SRC");
route(start_accounting);
$acc_leg(caller_id) = "sip:"+$fU+"@pstn";
remove_hf("User-Agent");
append_hf("User-Agent: OpenSIPS CE SoftSwitch\r\n");
} else
if (is_domain_local("$fd")) {
# pretends to be a local subscriber
xlog("[$ci] local caller detected, doing auth\n");
# do digest auth
if (!proxy_authorize("","subscriber")) {
proxy_challenge("", "auth");
exit;
}
# auth succesfully done
if ($au!=$fU) {
xlog("[$ci] user $fu using wrong credentials $au / $ar\n");
send_reply(403, "Forbidden FROM");
exit;
}
if (!is_acl_set(caller_acls, ACL_ENABLED)) {
xlog("[$ci] calling user is not enabled, blocking\n");
send_reply(403, "Forbidden");
exit;
}
# remove the credentials from the request
consume_credentials();
xlog("[$ci] auth successfully done, proceeding\n");
# mark source leg as local subscriber
setflag("LOCAL_SRC");
route(start_accounting);
$acc_leg(caller_id) = "sip:"+$fU+"@"+$fd;
}
else {
# caller from a foreign domain
$acc_leg(caller_id) = "sip:"+$fU+"@"+$fd;
}
}
route[do_inter_domain_routing]
{
if (isflagset("GW_SRC"))
return;
if (!is_domain_local("$rd")) {
# destination domain is not local
if (!isflagset("LOCAL_SRC")) {
xlog("[$ci] declining relaying from user $fu to $rd\n");
send_reply(403,"Forbidden relay");
exit;
}
# route a call from local user to a foreign domain
# allow for now
xlog("[$ci] routing to a foreign request\n");
route(relay_initial);
exit;
}
if (!isflagset("LOCAL_SRC")) {
# call from a foregin domain
# reject for now
xlog("[$ci] declining call from foreign user $fu to $rd\n");
send_reply(403,"Forbidden");
exit;
}
}
route[handle_invite]
{
if (!create_dialog("B")) {
xlog("L_ERR", "[$ci] cannot create dialog!\n");
send_reply(500, "Server Internal Error 1");
exit;
}
if (!topology_hiding(""))
xlog("L_ERR", "[$ci] cannot engage topology hiding!\n");
route(extract_cli);
xlog("[$ci] local $rm from $fu to $ru\n");
route(handle_local_destination);
# this does not return
exit;
}
route[handle_local_destination]
{
# apply dialplan
xlog("[$ci] applying dialplan on <$rU>\n");
while ([1]) {
$var(dp_type) = NULL;
if (!dp_translate(1, $rU, $rU, $var(dp_type))) {
xlog("[$ci] local username <$rU> not recognized by dialplan\n");
send_reply(404,"Not Found");
exit;
}
xlog("[$ci] dialled number translated to <$rU> @ <$rd>, "
"type $var(dp_type)\n");
if ($var(dp_type)==NULL || $var(dp_type)=="") {
xlog("[$ci] provisioning BUG in dialplan, type is not valid\n");
send_reply(404,"Not Found");
exit;
}
switch( $var(dp_type) ) {
case "DP_DID":
if (!alias_db_lookup("dids")) {
## Call to PSTN
route(to_pstn);
exit;
}
# continue translating the new URI
break;
case "DP_USER":
## Call to subscriber/user
route( to_user );
break;
case "DP_DND_ON":
## call to DND
route( handle_starcode, "DND_ON");
break;
case "DP_DND_OFF":
## call to DND
route( handle_starcode, "DND_OFF");
break;
case "DP_VMLISTEN":
## Call to voicemail for listening messages
route( listen_voicemail );
break;
default:
xlog("[$ci] provisioning BUG in dialplan, type $var(dp_type) "
"is not supported\n");
send_reply(404,"Not Found");
exit;
} /* switch */
} /* while */
# end (not ever reached)
}
route[to_user]
{
# routing to user
xlog("[$ci] routing to user <$rU@$rd>, loading profile first\n");
$avp(callee_acls) := NULL;
$avp(callee_cfaw_uri) := NULL;
$avp(callee_cfbs_uri) := NULL;
$avp(callee_cfna_uri) := NULL;
$avp(callee_cfnf_uri) := NULL;
$var(rc) = sql_query_one("select acls, cfaw_uri, cfbs_uri, cfna_uri, cfnf_uri from subscriber where username='$rU' and domain='$rd'",
"$avp(callee_acls), $avp(callee_cfaw_uri), $avp(callee_cfbs_uri), $avp(callee_cfna_uri), $avp(callee_cfnf_uri)");
if ( $rc<0 || $avp(callee_acls)==NULL) {
xlog("[$ci] failed to load profile for user <$rU@$rd>\n");
send_reply( 404, "User Not Found");
exit;
}
xlog("[$ci] profile for <$ru> loaded acls=<$avp(callee_acls)>, cfaw_uri=<$avp(callee_cfaw_uri)>, cfbs_uri=<$avp(callee_cfbs_uri)>, cfna_uri=<$avp(callee_cfna_uri)>, cfnf_uri=<$avp(callee_cfnf_uri)>\n");
setflag("LOCAL_DST");
$ru = "sip:" + $rU + "@" + $rd;
$acc_leg(callee_id) = $ru;
# is permanent Call Forward enabled?
if ($avp(callee_cfaw_uri)!=NULL) {
route( do_call_forward, $avp(callee_cfaw_uri) );
# if CF fails, we will continue
}
if (is_acl_set(callee_acls, ACL_VM_ALWAYS)) {
xlog("[$ci] user had the permanent VM redirect\n");
route( redirect_to_voicemail );
t_reply(480, "Unavailable");
exit;
}
# is DND ACL on?
if (is_acl_set(callee_acls, ACL_DND)) {
xlog("[$ci] user had the DND turned on, rejecting\n");
route( redirect_to_voicemail );
# if no VM, we will continue
t_reply(486, "Busy Here");
exit;
}
xlog("[$ci] routing to registered AOR <$rU@$rd>\n");
if (!lookup("location","method-filtering")) {
# ok, user not registered, check CF and VM options
if ($avp(callee_cfnf_uri)!=NULL) {
route( do_call_forward, $avp(callee_cfnf_uri) );
}
if (is_acl_set(callee_acls, ACL_VM_NOT_FOUND)) {
route( redirect_to_voicemail );
# if no VM, we will continue
}
t_reply(480, "Unavailable");
exit;
}
t_on_failure("missed_call");
route(relay_initial);
}
route[to_pstn]
{
if (!isflagset("LOCAL_SRC") || !is_acl_set(caller_acls, ACL_ALLOW_PSTN)) {
send_reply(403,"Forbidden service");
exit;
}
$acc_leg(callee_id) = "sip:"+$rU+"@pstn";
xlog("[$ci] routing to DID <$rU> from $acc_leg(caller_id)\n");
ruri_del_param("transport");
stream_delete("video");
if ( !do_routing(1,"W") ){
xlog("[$ci] no rule matching destination $rU in group 1\n");
t_reply(404, "Destination not available");
exit;
}
t_on_failure("gw_failover");
xlog("[$ci] routing to GW <$rd>/$avp(___dr_gw_id__), via <$socket_out> as <$ru>\n");
setflag("GW_DST");
route(relay_initial);
}
failure_route[gw_failover]
{
xlog("[$ci] GW call failed to established with $T_reply_code code\n");
if (t_was_cancelled()) {
t_reply(487,"Request cancelled");
exit;
}
# failure indication about GWs?
if ( t_check_status("[56][0-9][0-9]")
|| (t_check_status("408") && t_local_replied("all"))
) {
xlog("[$ci] GW $rd failed with $T_reply_code -> retry\n ");
dr_disable();
if ( use_next_gw() ) {
xlog("[$ci] using next GW <$rd>/$avp(___dr_gw_id__), via <$socket_out> as <$ru>\n");
# send it out again
t_on_failure("gw_failover");
t_relay();
exit;
} else {
xlog("[$ci] no other GW to retry\n ");
t_reply(503,"PSTN not available");
exit;
}
}
# if call failure, allow the reply to propagate to caller
exit;
}
################### Voicemail handling related routes #####################
route[to_mediaserver]
{
$rd = "${FREESWITCH_IP}";
$rp = 5060;
ruri_del_param("transport");
setbflag("MEDIA_DST");
route(relay_initial);
}
route[redirect_to_voicemail]
{
xlog("[$ci] caller $acc_leg(caller_id) leaving message for "
"callee <$acc_leg(callee_id)>\n");
if ( !is_acl_set(callee_acls, ACL_VM_ENABLED) ) {
xlog("[$ci] callee does not have the VM service enabled\n");
return;
}
$ru = $acc_leg(callee_id);
prefix("VM_LV_");
xlog("[$ci] leaving message for VM BOX $ru\n");
$acc_leg(callee_id) = $ru;
route(to_mediaserver);
# does not return
}
route[listen_voicemail]
{
# the caller leg is the party dialling
# to listen/manage the recordings of a vmbox
if (!isflagset("LOCAL_SRC")) {
send_reply(403,"Forbidden service");
exit;
}
xlog("[$ci] caller $acc_leg(caller_id) listening his vmbox\n");
if ( !is_acl_set(caller_acls, ACL_VM_ENABLED) ) {
xlog("[$ci] caller does not have the VM service enabled\n");
send_reply(404,"VM not found");
exit;
}
$ru = $acc_leg(caller_id);
prefix("VM_LT_");
xlog("[$ci] listening message for VM BOX $rU\n");
$acc_leg(callee_id) = $ru;
route(to_mediaserver);
# does not return
}
################### StarCodes related routes #####################
route[handle_starcode]
{
xlog("[$ci] handling star code $param(1) from $acc_leg(caller_id)\n");
# takes one mandatory param which is the name of STAR CODE
if ($param(1) == "DND_ON") {
# set DND
sql_query("update subscriber set acls=concat(REPLACE(acls,'ACL_DND',''),'ACL_DND') where username='$(acc_leg(caller_id){uri.user})' and domain='$(acc_leg(caller_id){uri.host})'");
$rU = "ANN_SERVICE_SET_DND";
} else
if ($param(1) == "DND_OFF") {
# reset DND
sql_query("update subscriber set acls=REPLACE(acls,'ACL_DND','') where username='$(acc_leg(caller_id){uri.user})' and domain='$(acc_leg(caller_id){uri.host})'");
$rU = "ANN_SERVICE_RESET_DND";
} else {
xlog("[$ci] configuration error - unsupported star code\n");
t_reply(404,"Unknown service");
exit;
}
route(to_mediaserver);
exit;
}
################### Relaying related routes #####################
route[extract_cli]
{
$avp(new_cli_username) := NULL;
xlog("[$ci] computing CLI for caller $acc_leg(caller_id) ($fU/$fd, configured-CLI=$avp(caller_cli))\n");
if (isflagset("GW_SRC")) {
# caller is PSTN
if ( is_present_hf("Remote-Party-ID") ) {
$avp(new_cli_username) := $(hdr(Remote-Party-ID){nameaddr.uri}{uri.user}) ;
if ( $(hdr(Remote-Party-ID){nameaddr.param,privacy}) == "full" )
setflag("ANONYMOUS_CALLER");
} else if ( is_present_hf("P-Asserted-Identity") ) {
$avp(new_cli_username) := $(hdr(P-Asserted-Identity){nameaddr.uri}{uri.user}) ;
if (is_present_hf("Privacy") && $hdr(Privacy)=="id")
setflag("ANONYMOUS_CALLER");
} else
$avp(new_cli_username) := $fU;
xlog("[$ci] extracted CLI from PSTN is <$avp(new_cli_username)>\n");
} else if (isflagset("LOCAL_SRC")) {
# caller is a subscriber
$var(cli_username) = NULL;
resetflag("ANONYMOUS_CALLER");
# find and extract any presented CLI
if ( is_present_hf("P-Preferred-Identity") ) {
$var(cli_username) := $(hdr(P-Preferred-Identity){nameaddr.uri}{uri.user}) ;
if (is_present_hf("Privacy") && $hdr(Privacy)=="id")
setflag("ANONYMOUS_CALLER");
} else if ($avp(caller_cli)!=NULL) {
# -> CLI is the "cli" field of the user / PBX
$avp(new_cli_username) = $avp(caller_cli);
if ($fU=="anonymous")
setflag("ANONYMOUS_CALLER");
}
}
xlog("[$ci] newly computed CLI is <$avp(new_cli_username)>\n");
}
route[relay_initial]
{
if (is_method("INVITE")) {
setflag("INITIAL_INVITE");
replace("100rel", "123abc");
}
route(relay);
}
route[relay] {
if (is_method("INVITE")) {
t_on_branch("handle_branch");
}
t_on_reply("reply_handler");
t_relay();
exit;
}
branch_route[handle_branch]
{
xlog("L_INFO", "[$ci] branching to $$ru=$ru, $$du=$du\n");
if (isflagset("INITIAL_INVITE") && (
isflagset("NAT_SRC") ||
isbflagset("NAT_DST") ||
isbflagset("MEDIA_DST"))) {
# take care of media relay
$rtp_relay = "fcos";
$rtp_relay_peer = "fcos";
if (!nat_uac_test("private-sdp"))
$rtp_relay_peer += "r";
if (isbflagset("MEDIA_DST"))
$rtp_relay(ip) = "${RTPPROXY_IP}";
if (rtp_relay_engage("rtpproxy"))
setbflag("INUSE_RTP_RELAY");
}
# inject the new CLI
remove_hf("Remote-Party-ID");
remove_hf("P-Asserted-Identity");
remove_hf("P-Preferred-Identity");
remove_hf("Privacy");
if (isflagset("INITIAL_INVITE") && $avp(new_cli_username)!=NULL) {
if ( isflagset("GW_DST") ) {
# destination is PSTN
$avp(new_cli_domain) := $(acc_leg(caller_id){uri.host});
if ( isflagset("ANONYMOUS_CALLER") ) {
append_hf("P-Asserted-Identity: <sip:$avp(new_cli_username)@$avp(new_cli_domain)>\r\nPrivacy: id\r\n");
uac_replace_from("Anonymous", "sip:[email protected]");
} else {
append_hf("P-Asserted-Identity: <sip:$avp(new_cli_username)@$avp(new_cli_domain)>\r\n");
uac_replace_from("$avp(new_cli_username)", "sip:$avp(new_cli_username)@$avp(new_cli_domain)");
}
} else {
# destination is user
if ( isflagset("ANONYMOUS_CALLER") ) {
uac_replace_from("Anonymous", "sip:[email protected]");
} else {
$avp(new_cli_domain) := $(acc_leg(callee_id){uri.host});
uac_replace_from("$var(new_cli_username)", "sip:$avp(new_cli_username)@$avp(new_cli_domain)");
}
}
}
}
onreply_route[reply_handler]
{
xlog("[$ci] reply received <$rm:$rs:$rr> on branch $T_branch_idx\n");
# take care of NAT traversal
if ((isbflagset("NAT_DST") && $rs!~"^3[0-9][0-9]") || isflagset("GW_DST")) {
fix_nated_contact(";bnat=yes");
}
# take care of media relay
if (isbflagset("INUSE_RTP_RELAY")) {
if (has_body_part("application/sdp")) {
if (!nat_uac_test("private-sdp"))
$rtp_relay += "r";
}
}
if (isflagset("INITIAL_INVITE")) {
# collect accounting data
if ( $rs >= 200 ) {
$acc_extra(callee_ip)= $socket_in(proto)+":"+$si+":"+$sp;
if (is_present_hf("User-Agent"))
$acc_extra(callee_ua)=$(hdr(User-Agent));
else if (is_present_hf("Server"))
$acc_extra(callee_ua)=$(hdr(Server));
else $acc_extra(callee_ua)=NULL;
}
}
}
failure_route[missed_call]
{
xlog("[$ci] call failed to established with $T_reply_code code\n");
if (isbflagset("INUSE_RTP_RELAY")) {
rtpproxy_unforce();