/
blox-invite.cfg.m4
695 lines (631 loc) · 37.9 KB
/
blox-invite.cfg.m4
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
# /* Blox is an Opensource Session Border Controller
# * Copyright (c) 2015-2018 "Blox" [http://www.blox.org]
# *
# * This file is part of Blox.
# *
# * Blox 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.
# *
# * This program 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, see <http://www.gnu.org/licenses/>
# */
route[ROUTE_INVITE] {
if(method == "INVITE") {
if($si == $avp(LANIP) || $si == $avp(WANIP)) { #Source IP Matching LAN or WAN IP
xdbg("BLOX_DBG: blox-invite.cfg: Skipping INVITE generated locally by the server\n" );
drop();
exit;
}
if($avp(LAN)) { #/* PBX to SBC */
$avp(TRUNK) = null;
xdbg("BLOX_DBG: blox-invite.cfg: Got from $Ri LAN\n");
$avp(uuid) = "LCR:" + $avp(LAN) ;
if(cache_fetch("local","$avp(uuid)",$avp(LCR))) {
xdbg("BLOX_DBG: blox-invite.cfg: Loaded from cache $avp(uuid): $avp(LCR)\n");
} else if (avp_db_load("$avp(uuid)","$avp(LCR)/blox_config")) {
#NO Cache for LCR Right now
#cache_store("local","$avp(uuid)","$avp(LCR)");
#xdbg("BLOX_DBG: blox-invite.cfg: Stored in cache $avp(uuid): $avp(LCR)\n");
} else {
$avp(LCR) = null;
}
if($avp(LCR)) {
$var(i) = 0;
$var(loop) = 1;
while($var(loop) && $(avp(LCR)[$var(i)])) {
xdbg("BLOX_DBG: blox-invite.cfg: Got from LAN LCR Matching $(avp(LCR)[$var(i)])\n");
$var(LCR) = $(avp(LCR)[$var(i)]);
$var(src) = $si + ":" + $sp ;
xdbg("BLOX_DBG: blox-invite.cfg: Got from LAN LCR Matching $var(src) == $(var(LCR){param.value,PBX})\n");
if($var(src) == $(var(LCR){param.value,PBX})) {
$var(g) = $(var(LCR){param.value,Group});
$avp(g) = $(var(g){s.int});
$var(loop) = null;
}
$var(i) = $var(i) + 1;
}
if($var(loop) != null) {
xlog("L_NOTICE", "BLOX_DBG::: blox-invite.cfg: LCR: SIP Profile access denied for $si:$sp \n");
sl_send_reply("603", "Declined");
exit;
}
$var(oru) = $ru ;
xdbg("BLOX_DBG: blox-invite.cfg: Group: $var(g)\n"); #Dont print directly without substr
if (do_routing("$avp(g)",,,,"$var(gw_attributes)")) { #/* Goes to configured route */
$avp(cac_uuid) = $var(gw_attributes) ;
route(OUTBOUND_CALL_ACCESS_CONTROL);
while (!isflagset(OUTBOUND_CALL_ACCESS_CONTROL)) {
if(use_next_gw(,"$var(gw_attributes)")) {
xlog("L_NOTICE", "BLOX_DBG::: blox-invite.cfg: LCR: Next GW found, PBX $si:$sp $var(gw_attributes)\n");
$avp(cac_uuid) = $var(gw_attributes) ;
route(OUTBOUND_CALL_ACCESS_CONTROL);
} else {
xlog("L_WARN", "BLOX_DBG::: blox-invite.cfg: No Next GW found for LCR, PBX $si:$sp $var(gw_attributes)\n");
send_reply("503", "No Rules matching the URI");
exit;
}
}
append_to_reply("Diversion: <$ru>;reason=deflection\r\n");
$avp(LAN) = $(var(gw_attributes){uri.param,LAN});
xdbg("BLOX_DBG: blox-invite.cfg: Group: $avp(LAN)\n"); #Dont print directly without substr
route(READ_LAN_PROFILE);
$var(RDIP) = $(avp(LANProfile){uri.host});
$var(RDPORT) = $(avp(LANProfile){uri.port});
#Manipulated Adding, Striping Prefix, Suffix
$var(ru) = "sip:" + $rU + "@" + $var(RDIP) + ":" + $var(RDPORT) ; #/* 5062 should be Unique port for the Gateway to set in Diversion IP:PORT */
xlog("L_NOTICE","{ \"LCR-REDIRECT\" : { \"FURI\": \"$fu;tag=$ft\", \"RURI-ORG\": \"$var(oru)\", \"RURI\": \"$ru\", \"REDIRECT\": \"$var(ru)\", \"SRCIP\": \"$si:$sp\", \"DSTIP\": \"$Ri:$Rp\", \"TS\": $TS } }"); /* NOTICE USED FOR LCR AND LOGGED INTO lcr.log */
$ru = $var(ru) ;
sl_send_reply("302","LCR Redirect");
exit;
} else {
xlog("L_NOTICE", "BLOX_DBG::: blox-invite.cfg: LCR: No Next GW found for LCR, PBX $si:$sp $var(gw_attributes)\n");
send_reply("503", "No Rules matching the URI");
exit;
}
}
if(!$avp(TRUNK)) { #If not already set by LCR
$avp(uuid) = "TRUNK:" + $avp(LAN) ;
if(cache_fetch("local","$avp(uuid)",$avp(TRUNK))) {
xdbg("BLOX_DBG: blox-invite.cfg: Loaded from cache $avp(uuid): $avp(TRUNK)\n");
} else if (avp_db_load("$avp(uuid)","$avp(TRUNK)/blox_config")) {
cache_store("local","$avp(uuid)","$avp(TRUNK)");
xdbg("BLOX_DBG: blox-invite.cfg: Stored in cache $avp(uuid): $avp(TRUNK)\n");
} else {
$avp(TRUNK) = null;
}
}
if($avp(TRUNK)) {
xdbg("BLOX_DBG: blox-invite.cfg: Routing Forwarded PBX MESSAGE $avp(TRUNK)\n");
$var(cfgparam) = "cfgparam" ;
$avp($var(cfgparam)) = $avp(TRUNK);
avp_db_store("$hdr(call-id)","$avp($var(cfgparam))");
$var(TRUNKUSER) = $(avp(TRUNK){uri.user});
$var(TRUNKIP) = $(avp(TRUNK){uri.host});
$var(TRUNKPORT) = $(avp(TRUNK){uri.port});
$var(TRUNKDOMAIN) = $(avp(TRUNK){uri.domain});
$avp(WAN) = $(avp(TRUNK){uri.param,WAN});
$avp(T38Param) = $(avp(TRUNK){uri.param,T38Param});
$avp(MEDIA) = $(avp(TRUNK){uri.param,MEDIA});
$avp(GWID) = $(avp(TRUNK){uri.param,GWID});
$avp(SrcSRTP) = $(avp(TRUNK){uri.param,LANSRTP});
$avp(DstSRTP) = $(avp(TRUNK){uri.param,WANSRTP});
route(READ_ENUM,$avp(uuid));
if($avp(ENUM)) {
$var(ENUMSX) = $(avp(ENUM){uri.param,ENUMSX}); #SUFFIX, default: e164.arpa.
$var(ENUMSE) = $(avp(ENUM){uri.param,ENUMSE}); #SERVICE, default: e2u+sip
$var(ENUMTYPE) = $(avp(ENUM){uri.param,ENUMTYPE}); #SERVICE, default: e2u+sip
if($var(ENUMTYPE)==""){$var(ENUMTYPE)=null;}
if($var(ENUMSE)==""){$var(ENUMSE)=null;}
if($var(ENUMSX)==""){$var(ENUMSX)=null;}
}
if($var(TRUNKUSER)==""){$var(TRUNKUSER)=null;}
if($var(TRUNKIP)==""){$var(TRUNKIP)=null;}
if($var(TRUNKPORT)==""){$var(TRUNKPORT)=null;}
if($var(TRUNKDOMAIN)==""){$var(TRUNKDOMAIN)=null;}
if($avp(WAN)==""){$avp(WAN)=null;}
if($avp(T38Param)==""){$avp(T38Param)=null;}
if($avp(MEDIA)==""){$avp(MEDIA)=null;}
if($avp(GWID)==""){$avp(GWID)=null;}
if($avp(SrcSRTP)==""){$avp(SrcSRTP)=null;}
if($avp(DstSRTP)==""){$avp(DstSRTP)=null;}
if($avp(WAN)) {
if(!has_totag()) {
xdbg("BLOX_DBG: blox-invite.cfg: $avp(TRUNK)/$var(TRUNKUSER)/ $var(TRUNKIP)/$var(TRUNKPORT)/$avp(SIPProfile)\n");
$avp(cac_uuid) = $avp(TRUNK) ;
setflag(487); /* Send response 487 if GW not available */
route(OUTBOUND_CALL_ACCESS_CONTROL);
if(!isflagset(OUTBOUND_CALL_ACCESS_CONTROL)) {
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Dropping SIP Method $rm received from $fu $si $sp to $ru ($avp(rcv))\n");
drop();
exit;
}
$dlg_val(MediaProfileID) = $avp(MEDIA);
$dlg_val(from) = $fu ;
$dlg_val(request) = $ru ;
$dlg_val(channel) = "sip:" + $si + ":" + $sp;
$dlg_val(direction) = "outbound";
if(pcre_match("$ci","^BLOX_CALLID_PREFIX")) { /* Already tophide applied */
topology_hiding();
} else {
topology_hiding("C");
}
xdbg("BLOX_DBG: blox-invite.cfg: Storing the cseq offset for $ft\n") ;
if($(hdr(Diversion))) {
$dlg_val(dchannel) = $avp(TRUNK) + ";Diversion=" + $(hdr(Diversion)) ;
} else {
$dlg_val(dchannel) = $avp(TRUNK) ;
}
setflag(ACC_FLAG_CDR_FLAG);
setflag(ACC_FLAG_LOG_FLAG);
setflag(ACC_FLAG_DB_FLAG);
setflag(ACC_FLAG_FAILED_TRANSACTION);
append_hf("P-hint: TopHide-Applied\r\n");
};
if( route_to_gw("$avp(GWID)") ) {
if(!has_totag()) { #Set From/To Execute inital time
if($avp(WANProfile)) {
$avp(WANIP) = $(avp(WANProfile){uri.host});
$avp(WANPORT) = $(avp(WANProfile){uri.port});
$avp(WANPROTO) = $(avp(WANProfile){uri.param,transport});
$avp(WANADVIP) = $(avp(WANProfile){uri.param,advip});
$avp(WANADVPORT) = $(avp(WANProfile){uri.param,advport});
if($avp(WANPROTO)==""){$avp(WANPROTO)=null;}
if($avp(WANADVIP)==""){$avp(WANADVIP)=null;}
if($avp(WANADVPORT)==""){$avp(WANADVPORT)=null;}
$var(to) = "sip:" + $rU + "@" + $var(TRUNKIP) + ":" + $var(TRUNKPORT) ;
$var(from) = "sip:" + $var(TRUNKUSER) + "@" + $var(TRUNKIP) + ":" + $var(TRUNKPORT) ;
uac_replace_to("$var(to)");
uac_replace_from("$var(from)");
} else {
xlog("L_ERROR", "BLOX_DBG::: blox-invite.cfg: No WAN Profile, Leaking through To/From Header\n");
}
set_dlg_flag("DLG_FLAG_LAN2WAN") ;
}
remove_hf("Diversion");
$du = $ru ;
if($var(ENUMSE) != null && $var(ENUMSX) != null) {
route(ENUM,$var(ENUMTYPE),$var(ENUMSX),$var(ENUMSE)) ;
}
if($var(TRUNKDOMAIN)) {
$ru = "sip:" + $tU + "@" + $var(TRUNKDOMAIN) ;
}
t_on_failure("LAN2WAN");
xdbg("BLOX_DBG: blox-invite.cfg: Routing $var(from) $var(to) $ru to $du from $si : $sp via $fs\n" );
route(LAN2WAN);
} else {
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Failed to route to $avp(GWID) $avp(TRUNK) from $si : $sp\n" );
}
exit;
}
xdbg("BLOX_DBG: blox-invite.cfg: SIP Profile for $si:$sp access denied\n");
sl_send_reply("603", "Declined");
exit;
} else {
$avp(uuid) = "PBX:" + $avp(LAN) ;
if(cache_fetch("local","$avp(uuid)",$avp(PBX))) {
xdbg("BLOX_DBG: blox-invite.cfg: Loaded from cache $avp(uuid): $avp(PBX)\n");
} else if (avp_db_load("$avp(uuid)","$avp(PBX)/blox_config")) {
cache_store("local","$avp(uuid)","$avp(PBX)");
xdbg("BLOX_DBG: blox-invite.cfg: Stored in cache $avp(uuid): $avp(PBX)\n");
} else {
xlog("L_WARN", "BLOX_DBG::: blox-invite.cfg: SIP Profile for $si:$sp access denied\n");
sl_send_reply("603", "Declined");
exit;
}
if($avp(PBX)) {
xdbg("BLOX_DBG: blox-invite.cfg: Got route $Ri RE\n");
$var(cfgparam) = "cfgparam" ;
$avp($var(cfgparam)) = $avp(PBX);
avp_db_store("$hdr(call-id)","$avp($var(cfgparam))");
#/* Check Roaming Extension routing */
$var(PBXIP) = $(avp(PBX){uri.host}) ;
$var(PBXPORT) = $(avp(PBX){uri.port}) ;
$avp(T38Param) = $(avp(PBX){uri.param,T38Param});
$avp(MEDIA) = $(avp(PBX){uri.param,MEDIA});
$avp(SrcSRTP) = $(avp(PBX){uri.param,LANSRTP});
$avp(DstSRTP) = $(avp(PBX){uri.param,WANSRTP});
route(READ_ENUM,$avp(uuid));
if($avp(ENUM)) {
$var(ENUMSX) = $(avp(ENUM){uri.param,ENUMSX}); #SUFFIX, default: e164.arpa.
$var(ENUMSE) = $(avp(ENUM){uri.param,ENUMSE}); #SERVICE, default: e2u+sip
$var(ENUMTYPE) = $(avp(ENUM){uri.param,ENUMTYPE}); #SERVICE, default: e2u+sip
if($var(ENUMSX)==""){$var(ENUMSX)=null;}
if($var(ENUMSE)==""){$var(ENUMSE)=null;}
if($var(ENUMTYPE)==""){$var(ENUMTYPE)=null;}
}
if($var(PBXIP)==""){$var(PBXIP)=null;}
if($var(PBXPORT)==""){$var(PBXPORT)=null;}
if($avp(T38Param)==""){$avp(T38Param)=null;}
if($avp(MEDIA)==""){$avp(MEDIA)=null;}
if($avp(SrcSRTP)==""){$avp(SrcSRTP)=null;}
if($avp(DstSRTP)==""){$avp(DstSRTP)=null;}
$avp(cac_uuid) = $avp(PBX) ;
setflag(487);
route(OUTBOUND_CALL_ACCESS_CONTROL);
$avp(WAN) = $(avp(PBX){uri.param,WAN});
if($avp(WAN)==""){$avp(WAN)=null;}
route(READ_WAN_PROFILE);
if($avp(WANProfile)) {
$avp(WANIP) = $(avp(WANProfile){uri.host});
$avp(WANPORT) = $(avp(WANProfile){uri.port});
$avp(WANPROTO) = $(avp(WANProfile){uri.param,transport});
$avp(WANADVIP) = $(avp(WANProfile){uri.param,advip});
$avp(WANADVPORT) = $(avp(WANProfile){uri.param,advport});
$fs = $avp(WANPROTO) + ":" + $avp(WANIP) + ":" + $avp(WANPORT);
}
#search for aor mapped to pbx wan profile
$var(aor) = "sip:" + $tU + "@" + $avp(WANIP) + ":" + $avp(WANPORT) ;
xdbg("BLOX_DBG: blox-invite.cfg: Looking for $var(aor) in locationpbx\n");
# /* Last Check for Roaming Extension */
if (!lookup("locationpbx","m", "$var(aor)")) { ; #/* Find RE Registered to US */
switch ($retcode) {
case -1:
case -3:
t_newtran();
t_on_failure("WAN2LAN");
t_reply("404", "Not Found");
exit;
case -2:
append_hf("Allow: INVITE, ACK, REFER, NOTIFY, CANCEL, BYE, REGISTER" );
sl_send_reply("405", "Method Not Allowed");
exit;
}
};
if(!has_totag()) {
create_dialog("PpB");
$dlg_val(MediaProfileID) = $avp(MEDIA);
$dlg_val(from) = $fu ;
$dlg_val(request) = $ru ;
$dlg_val(channel) = "sip:" + $si + ":" + $sp;
$dlg_val(dchannel) = $du ;
$dlg_val(direction) = "outbound";
if(pcre_match("$ci","^BLOX_CALLID_PREFIX")) { /* Already tophide applied */
topology_hiding();
} else {
topology_hiding("C");
}
setflag(ACC_FLAG_CDR_FLAG);
setflag(ACC_FLAG_LOG_FLAG);
setflag(ACC_FLAG_DB_FLAG);
setflag(ACC_FLAG_FAILED_TRANSACTION);
append_hf("P-hint: TopHide-Applied\r\n");
set_dlg_flag("DLG_FLAG_LAN2WAN") ;
};
if($var(nat96)) { # /* If Contact not same as source IP Address */
if(!is_ip_rfc1918("$si")) { # /* Set Source IP, Source is Priviate IP */
if(!has_totag()) {
$dlg_val(ucontact) = "sip:" + $si + ":" + $sp + ";transport=" + $proto ;
} else if($DLG_dir == "downstream") {
$dlg_val(dcontact) = "sip:" + $si + ":" + $sp + ";transport=" + $proto ;
} else {
$dlg_val(ucontact) = "sip:" + $si + ":" + $sp + ";transport=" + $proto ;
}
} else { # /* Set INVITE Contact */
$var(cthost) = $(var(cturi){uri.host}) ;
$dlg_val(rcv) = "sip:" + $si + ":" + $sp + ";transport=" + $proto ;
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: $var(ct) ==> $var(cthost) <==> $Ri : $dlg_val(loop)\n");
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: $DLG_dir | Set Source IP, Source is Priviate IP and received!=via $si:$sp;$var(ctparams)\n");
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Set INVITE Contact $var(cturi)\n");
if(!has_totag()) {
$dlg_val(ucontact) = $var(cturi);
} else if($DLG_dir == "downstream") {
$dlg_val(dcontact) = $var(cturi);
} else {
$dlg_val(ucontact) = $var(cturi);
}
}
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: $var(cturi) != $si Request dir: $DLG_dir -> up: $dlg_val(ucontact) -> down: $dlg_val(dcontact) <-\n");
}
if($var(ENUMSE) != null && $var(ENUMSX) != null) {
route(ENUM,$var(ENUMTYPE),$var(ENUMSX),$var(ENUMSE));
}
if($avp(WANADVIP)) {
$var(to) = "sip:" + $rU + "@" + $avp(WANADVIP) + ":" + $avp(WANADVPORT) ;
$var(from) = "sip:" + $fU + "@" + $avp(WANADVIP) + ":" + $avp(WANADVPORT) ;
} else {
$var(to) = "sip:" + $rU + "@" + $avp(WANIP) + ":" + $avp(WANPORT) ;
$var(from) = "sip:" + $fU + "@" + $avp(WANIP) + ":" + $avp(WANPORT) ;
}
uac_replace_to("$var(to)");
uac_replace_from("$var(from)");
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Found PBX Requesting $ru -> $var(to)/$du -> $var(from)" );
route(LAN2WAN);
exit;
}
}
} else if ($avp(WAN)) { #WAN
xdbg("BLOX_DBG: blox-invite.cfg: Got from $Ri WAN\n");
$avp(uuid) = "TRUNK:" + $avp(WAN) ;
if(cache_fetch("local","$avp(uuid)",$avp(TRUNK))) {
xdbg("BLOX_DBG: blox-invite.cfg: Loaded from cache $avp(uuid): $avp(TRUNK)\n");
} else if (avp_db_load("$avp(uuid)","$avp(TRUNK)/blox_config")) {
cache_store("local","$avp(uuid)","$avp(TRUNK)");
xdbg("BLOX_DBG: blox-invite.cfg: Stored in cache $avp(uuid): $avp(TRUNK)\n");
} else {
$avp(TRUNK) = null;
}
if($avp(TRUNK)) {
xdbg("BLOX_DBG: blox-invite.cfg: Got from $Ri TRUNK $avp(TRUNK)\n");
$var(cfgparam) = "cfgparam" ;
$avp($var(cfgparam)) = $avp(TRUNK);
avp_db_store("$hdr(call-id)","$avp($var(cfgparam))");
#/* INBOUND Trunk Call */
$var(TRUNKUSER) = $(avp(TRUNK){uri.user});
$var(TRUNKIP) = $(avp(TRUNK){uri.host});
$var(TRUNKPORT) = $(avp(TRUNK){uri.port});
$var(TRUNKDOMAIN) = $(avp(TRUNK){uri.domain});
$avp(T38Param) = $(avp(TRUNK){uri.param,T38Param});
$avp(MEDIA) = $(avp(TRUNK){uri.param,MEDIA});
$avp(SrcSRTP) = $(avp(TRUNK){uri.param,WANSRTP});
$avp(DstSRTP) = $(avp(TRUNK){uri.param,LANSRTP});
$avp(WAN) = $(avp(TRUNK){uri.param,WAN});
$avp(INBNDURI) = $(avp(TRUNK){uri.param,INBNDURI});
if($var(TRUNKUSER)==""){$var(TRUNKUSER)=null;}
if($var(TRUNKIP)==""){$var(TRUNKIP)=null;}
if($var(TRUNKPORT)==""){$var(TRUNKPORT)=null;}
if($var(TRUNKDOMAIN)==""){$var(TRUNKDOMAIN)=null;}
if($avp(T38Param)==""){$avp(T38Param)=null;}
if($avp(MEDIA)==""){$avp(MEDIA)=null;}
if($avp(SrcSRTP)==""){$avp(SrcSRTP)=null;}
if($avp(DstSRTP)==""){$avp(DstSRTP)=null;}
if($avp(WAN)==""){$avp(WAN)=null;}
if($avp(INBNDURI)==""){$avp(INBNDURI)=null;}
if($avp(INBNDURI)){
$avp(INBNDURI) = 'sip:' + $tU + '@' + $(avp(INBNDURI){s.decode.hexa}) ;
}
if($avp(WANProfile)) { # /* Passed to WAN2LAN */
$avp(WANIP) = $(avp(WANProfile){uri.host});
$avp(WANPORT) = $(avp(WANProfile){uri.port});
$avp(WANPROTO) = $(avp(WANProfile){uri.param,transport});
$avp(WANADVIP) = $(avp(WANProfile){uri.param,advip});
$avp(WANADVPORT) = $(avp(WANProfile){uri.param,advport});
if($avp(WANPROTO)==""){$avp(WANPROTO)=null;}
if($avp(WANADVIP)==""){$avp(WANADVIP)=null;}
if($avp(WANADVPORT)==""){$avp(WANADVPORT)=null;}
}
$var(lbret) = 0;
route(BLOX_LOADBALANCE,$avp(uuid));
if($var(lbret) == 1 ) { #Check LoadBalance Route
xdbg("BLOX_DBG: blox-invite.cfg: TRUNK Load Balance is Successful\n");
} else if($avp(INBNDURI)) {
$avp(LAN) = $(avp(TRUNK){uri.param,LAN});
if($avp(LAN)==""){$avp(LAN)=null;}
route(READ_LAN_PROFILE);
$avp(LANIP) = $(avp(LANProfile){uri.host});
$avp(LANPORT) = $(avp(LANProfile){uri.port});
$avp(LANPROTO) = $(avp(LANProfile){uri.param,transport});
$fs = $avp(LANPROTO) + ":" + $avp(LANIP) + ":" + $avp(LANPORT) ;
$ru = $avp(INBNDURI) + ';transport=' + $avp(LANPROTO) ;
$du = $avp(INBNDURI) + ';transport=' + $avp(LANPROTO) ;
} else {
if (!lookup("locationtrunk","m")) { ; /* Find PBX Registered to US */
xdbg("BLOX_DBG: blox-invite.cfg: Error no registration to SBC for TRUNK $avp(TRUNK)\n");
switch ($retcode) {
case -1:
case -3:
t_newtran();
t_on_failure("LAN2WAN");
t_reply("404", "Not Found");
exit;
case -2:
append_hf("Allow: INVITE, ACK, REFER, NOTIFY, CANCEL, BYE, REGISTER" );
sl_send_reply("405", "Method Not Allowed");
exit;
}
}
}
xdbg("BLOX_DBG: blox-invite.cfg: Found to route $fs $ru $du TRUNK\n");
$avp(cac_uuid) = $avp(TRUNK) ;
setflag(487); /* Send 487 reply with route INBOUND_CALL_ACCESS_CONTROL, if failed */
route(INBOUND_CALL_ACCESS_CONTROL); /* Check for call limitation */
if(!has_totag()) {
$dlg_val(MediaProfileID) = $(avp(TRUNK){uri.param,MEDIA});
$dlg_val(from) = $fu ;
$dlg_val(request) = $ru ;
$dlg_val(channel) = "sip:" + $si + ":" + $sp;
$dlg_val(dchannel) = $du;
$dlg_val(direction) = "inbound";
/* Call-ID encoding not required for WAN to LAN */
topology_hiding();
setflag(ACC_FLAG_CDR_FLAG);
setflag(ACC_FLAG_LOG_FLAG);
setflag(ACC_FLAG_DB_FLAG);
setflag(ACC_FLAG_FAILED_TRANSACTION);
set_dlg_flag("DLG_FLAG_WAN2LAN") ;
append_hf("P-hint: TopHide-Applied\r\n");
};
if($var(TRUNKDOMAIN)) {
$rd = $var(TRUNKDOMAIN);
} else {
$rd = $var(TRUNKIP);
}
t_on_failure("WAN2LAN");
route(WAN2LAN);
exit;
}
$avp(uuid) = "PBX:" + $avp(WAN) ;
if(cache_fetch("local","$avp(uuid)",$avp(PBX))) {
xdbg("BLOX_DBG: blox-invite.cfg: Loaded from cache $avp(uuid): $avp(PBX)\n");
} else if (avp_db_load("$avp(uuid)","$avp(PBX)/blox_config")) {
cache_store("local","$avp(uuid)","$avp(PBX)");
xdbg("BLOX_DBG: blox-invite.cfg: Stored in cache $avp(uuid): $avp(PBX)\n");
} else {
$avp(PBX) = null;
}
if($avp(PBX)) {
xdbg("BLOX_DBG: blox-invite.cfg: Got from $Ri RE $avp(PBX)\n");
$var(cfgparam) = "cfgparam" ;
$avp($var(cfgparam)) = $avp(PBX);
avp_db_store("$hdr(call-id)","$avp($var(cfgparam))");
#/* Check Roaming Extension routing */
$var(PBXIP) = $(avp(PBX){uri.host}) ;
$var(PBXPORT) = $(avp(PBX){uri.port}) ;
$avp(LAN) = $(avp(PBX){uri.param,LAN});
$avp(T38Param) = $(avp(PBX){uri.param,T38Param});
$avp(MEDIA) = $(avp(PBX){uri.param,MEDIA});
$avp(SrcSRTP) = $(avp(PBX){uri.param,WANSRTP});
$avp(DstSRTP) = $(avp(PBX){uri.param,LANSRTP});
$var(PBXIPAUTH) = $(avp(PBX){uri.param,IPAuth}) ;
if($var(PBXIP)==""){$var(PBXIP)=null;}
if($var(PBXPORT)==""){$var(PBXPORT)=null;}
if($avp(LAN)==""){$avp(LAN)=null;}
if($avp(T38Param)==""){$avp(T38Param)=null;}
if($avp(MEDIA)==""){$avp(MEDIA)=null;}
if($avp(SrcSRTP)==""){$avp(SrcSRTP)=null;}
if($avp(DstSRTP)==""){$avp(DstSRTP)=null;}
if($var(PBXIPAUTH)==""){$var(PBXIPAUTH)=null;}
$avp(cac_uuid) = $avp(PBX) ;
setflag(487);
route(INBOUND_CALL_ACCESS_CONTROL); /* Check for call limitation */
if($avp(LAN)) {
route(READ_WAN_PROFILE);
$avp(WANADVIP) = $(avp(WANProfile){uri.param,advip});
if($avp(WANADVIP)==""){$avp(WANADVIP)=null;}
if($avp(WANADVIP)) {
$avp(WANSOCKET) = $pr + ":" + $avp(WANADVIP) + ":" + $Rp ;
} else {
$avp(WANSOCKET) = $pr + ":" + $Ri + ":" + $Rp ;
}
$avp(RESOCKET) = "sip:" + $si + ":" + $sp ;
##$avp(RECHKONLYIP) = 1 ;
#if($avp(RECHKONLYIP)) { # /* Match only IP address in registrar not IP:PORT or PROTO */
# $avp(RESOCKET) = $si ;
#}
if($var(PBXIPAUTH) && pcre_match_group("$si","$var(PBXIPAUTH)")) {
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: $si:$sp ($ua) Autheticated Via IPAuth $avp(PBX): Group:$var(PBXIPAUTH)\n");
} else {
if(cache_fetch("local","locationpbx:$fU:$avp(WANSOCKET):contact", $avp(contact)) \
&& cache_fetch("local","locationpbx:$fU:$avp(WANSOCKET):received", $avp(received))) {
xdbg("BLOX_DBG: blox-invite.cfg: locationpbx:$fU:$avp(WANSOCKET):contact => locationpbx:$fU:$avp(WANSOCKET):received => $avp(contact);$avp(received)") ;
} else if(avp_db_query("SELECT contact, received, TIMESTAMP(expires) FROM locationpbx WHERE username = '$fU' AND socket = '$avp(WANSOCKET)' ORDER BY last_modified DESC LIMIT 1", "$avp(contact);$avp(received);$avp(expires)")) {
xdbg("BLOX_DBG: blox-invite.cfg: SELECT contact, received, TIMESTAMP(expires)-NOW() FROM locationpbx WHERE username = '$fU' AND socket = '$avp(WANSOCKET)' ORDER BY last_modified LIMIT 1, $avp(contact);$avp(received);$avp(expires)") ;
$var(expires) = ($avp(expires) - $Ts) * 1000;
#if($avp(RECHKONLYIP)) { # /* Match only IP address in registrar not IP:PORT or PROTO */
# $avp(received) = $(avp(received){s.select,1,:}) ;
#}
cache_store("local","locationpbx:$fU:$avp(WANSOCKET):contact","$avp(contact)", $var(expires));
cache_store("local","locationpbx:$fU:$avp(WANSOCKET):received","$avp(received)", $var(expires));
} else {
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: No Registration found try Re-Registering\n");
t_newtran();
t_on_failure("LAN2WAN");
t_reply("404", "Not Found");
exit;
}
if(!($avp(RESOCKET) == $avp(received))) { #We might be checking old cache, fix it now
xdbg("BLOX_DBG: blox-invite.cfg: Not maching re-check DB $avp(RESOCKET) != $avp(received)\n");
if(avp_db_query("SELECT contact, received, TIMESTAMP(expires) FROM locationpbx WHERE username = '$fU' AND socket = '$avp(WANSOCKET)' ORDER BY last_modified LIMIT 1", "$avp(contact);$avp(received);$avp(expires)")) {
xdbg("BLOX_DBG: blox-invite.cfg: SELECT contact, received, TIMESTAMP(expires)-NOW() FROM locationpbx WHERE username = '$fU' AND socket = '$avp(WANSOCKET)' ORDER BY last_modified LIMIT 1, $avp(contact);$avp(received);$avp(expires)") ;
$var(expires) = ($avp(expires) - $Ts) * 1000;
#if($avp(RECHKONLYIP)) { # /* Match only IP address in registrar not IP:PORT or PROTO */
# $avp(received) = $(avp(received){s.select,1,:}) ;
#}
cache_store("local","locationpbx:$fU:$avp(WANSOCKET):contact","$avp(contact)", $var(expires));
cache_store("local","locationpbx:$fU:$avp(WANSOCKET):received","$avp(received)", $var(expires));
} else {
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: No Registration found try Re-Registering\n");
t_newtran();
t_on_failure("LAN2WAN");
t_reply("404", "Not Found");
exit;
}
}
if($(avp(received){uri.param,transport})) {
if(!$(avp(RESOCKET){uri.param,transport})) { #/* If RESOCKET transport is empty */
$avp(RESOCKET) = $avp(RESOCKET) + ";transport=" + $(avp(received){uri.param,transport}) ;
}
}
if(!($avp(RESOCKET) == $avp(received))) {
xdbg("BLOX_DBG: blox-invite.cfg: Not maching $avp(RESOCKET) != $avp(received)\n");
t_newtran();
t_on_failure("LAN2WAN");
t_reply("404", "Not Found");
exit;
}
}
$avp(LAN) = $(avp(PBX){uri.param,LAN});
if($avp(LAN)==""){$avp(LAN)=null;}
route(READ_LAN_PROFILE);
$avp(LANIP) = $(avp(LANProfile){uri.host});
$avp(LANPORT) = $(avp(LANProfile){uri.port});
$avp(LANPROTO) = $(avp(LANProfile){uri.param,transport});
$avp(LANDOMAIN) = $(avp(LANProfile){uri.param,domain});
if($avp(LANDOMAIN)==""){$avp(LANDOMAIN)=null;}
$fs = $avp(LANPROTO) + ":" + $avp(LANIP) + ":" + $avp(LANPORT) ;
$var(lbret) = 0;
route(BLOX_LOADBALANCE,$avp(uuid));
if($var(lbret) == 1 ) { #Check LoadBalance Route
xlog("BLOX_DBG: blox-invite.cfg: PBX Load Balance is Successful\n");
} else {
route(BLOX_DOMAIN,$avp(uuid));
$var(PBXIP) = $(avp(DEFURI){uri.host}) ;
$var(PBXPORT) = $(avp(DEFURI){uri.port}) ;
xlog("BLOX_DBG: blox-invite.cfg: Load Balance Failed so choosen destination uri $du\n");
}
xlog("BLOX_DBG: blox-invite.cfg: -- choosen destination uri $du\n");
if($avp(LANDOMAIN)) {
$ru = "sip:" + $rU + "@" + $avp(LANDOMAIN) + ":" + $var(PBXPORT) ;
$var(Tto) = "sip:" + $tU + "@" + $avp(LANDOMAIN) ;
$var(Tfrom) = "sip:" + $fU + "@" + $avp(LANDOMAIN) ;
} else {
$ru = "sip:" + $rU + "@" + $var(PBXIP) + ":" + $var(PBXPORT) ;
$var(Tto) = "sip:" + $tU + "@" + $var(PBXIP) + ":" + $var(PBXPORT);
$var(Tfrom) = "sip:" + $fU + "@" + $var(PBXIP) + ":" + $var(PBXPORT);
}
$var(to) = $tu ;
$var(from) = $fu ;
xdbg("BLOX_DBG:: blox-invite.cfg: Update from $var(Tto) << $var(to) : $var(Tfrom) << $var(from)\n");
if(!has_totag()) {
create_dialog("PpB");
$dlg_val(MediaProfileID) = $(avp(PBX){uri.param,MEDIA});
$dlg_val(from) = $fu ;
$dlg_val(request) = $ru ;
$dlg_val(channel) = "sip:" + $si + ":" + $sp;
$dlg_val(dchannel) = $du ;
$dlg_val(direction) = "inbound";
/* Call-ID encoding not required for WAN to LAN */
topology_hiding(); #flags remove: CR: C - CallID, R - Refer-To
add_rcv_param();
setflag(ACC_FLAG_CDR_FLAG);
setflag(ACC_FLAG_LOG_FLAG);
setflag(ACC_FLAG_DB_FLAG);
setflag(ACC_FLAG_FAILED_TRANSACTION);
set_dlg_flag("DLG_FLAG_WAN2LAN") ;
append_hf("P-hint: TopHide-Applied\r\n");
$var(humbug) = HUMBUG_ENABLED ;
if($var(humbug) == "yes") {
route(HUMBUG_FRAUD_DETECTION);
}
}
$var(fproto) = $(var(from){uri.param,transport}) ;
$var(tproto) = $(var(to){uri.param,transport}) ;
if($var(tproto)==""){$var(tproto)=null;}
if($var(fproto)==""){$var(fproto)=null;}
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Update from $var(Tto) << $var(to) : $var(Tfrom) << $var(from)\n");
if($var(tproto) == null) {
$var(to) = $var(to) + ";transport=" + $avp(LANPROTO) ;
uac_replace_to("$var(to)");
}
if($var(fproto) == null) {
$var(from) = $var(from) + ";transport=" + $avp(LANPROTO) ;
uac_replace_from("$var(from)");
}
xlog("BLOX_DBG::: Checking Load Balance Configuration $avp(LBRuleID) : $avp(LBID)\n");
t_on_failure("WAN2LAN");
route(WAN2LAN);
exit;
}
}
xlog("L_INFO", "BLOX_DBG::: blox-invite.cfg: Dropping SIP Method $rm received from $fu $si $sp to $ru ($avp(rcv))\n"); /* Dont know what to do */
drop();
exit;
}
}
}