Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####### Routing Logic ########
- request_route {
- route(REQINIT);
- if (is_method("CANCEL")) {
- if (t_check_trans()) {
- route(RELAY);
- }
- exit;
- }
- if (is_method("INVITE") || is_method("REGISTER")) {
- route(NAT);
- }
- if (is_method("REGISTER")) {
- route(AUTH);
- }
- route(WITHINDLG);
- }
- route[REQINIT] {
- set_reply_no_connect();
- force_rport();
- // trusted ip test
- if (sht_match_name("trustip", "eq", "$si")) {
- if ($sht(authban=>$si::$au) > 9) {
- exit;
- }
- } else {
- if ($sht(authban=>$si::$au) > 5) {
- exit;
- }
- }
- #!ifdef WITH_ANTIFLOOD
- # flood detection from same IP and traffic ban for a while
- # be sure you exclude checking trusted peers, such as pstn gateways
- # - local host excluded (e.g., loop to self)
- if(src_ip!=myself) {
- if($sht(ipban=>$si)!=$null) {
- # ip is already blocked
- xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
- exit;
- }
- if (!pike_check_req()) {
- xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
- $sht(ipban=>$si) = 1;
- exit;
- }
- }
- #!endif
- if($ua =~ "friendly|scanner|sipcli|sipvicious|VaxSIPUserAgent") {
- # silent drop for scanners - uncomment next line if want to reply
- # sl_send_reply("200", "OK");
- exit;
- }
- if (!mf_process_maxfwd_header("10")) {
- sl_send_reply("483","Too Many Hops");
- exit;
- }
- if(is_method("OPTIONS") && uri==myself && $rU==$null) {
- sl_send_reply("200","Keepalive");
- exit;
- }
- if(!sanity_check("17895", "7")) {
- xlog("Malformed SIP request from $si:$sp\n");
- exit;
- }
- }
- route[AUTH] {
- if((!is_method("REGISTER")) && allow_source_address(1)) {
- // source IP allowed
- return;
- }
- if (sht_match_name("auth", "eq", "$Au")) {
- if (!pv_auth_check("$fd", "$sht(auth=>$Au)", "0", "1")) {
- auth_challenge("$fd", "1");
- if(is_present_hf("Authorization") || is_present_hf("Proxy-Authorization")) {
- $sht(authban=>$si::$au) = $sht(authban=>$si::$au) + 1;
- exit;
- }
- exit;
- }
- consume_credentials();
- $sht(authban=>$si::$au) = $null;
- if (is_method("REGISTER")) {
- save("location", "0x04");
- if (!sht_match_name("trustip", "eq", "$si")) {
- $sht(trustip=>$si) = $au;
- }
- exit;
- }
- } else {
- if (!auth_check("$fd", "subscriber", "1")) {
- auth_challenge("$fd", "1");
- if(is_present_hf("Authorization") || is_present_hf("Proxy-Authorization")) {
- $sht(authban=>$si::$au) = $sht(authban=>$si::$au) + 1;
- exit;
- }
- exit;
- }
- $sht(auth=>$Au) = $avp(pass);
- $sht(authban=>$si::$au) = $null;
- consume_credentials();
- if (is_method("REGISTER")) {
- save("location", "0x04");
- if (!sht_match_name("trustip", "eq", "$si")) {
- $sht(trustip=>$si) = $au;
- }
- exit;
- }
- }
- }
- route[NAT] {
- if (nat_uac_test("18") || $Ri == "kam.ext.sock.ip") {
- if (is_method("REGISTER")) {
- set_contact_alias();
- } else {
- if (is_first_hop()) {
- if (!ds_is_from_list("1", "2")) {
- set_contact_alias();
- }
- }
- }
- }
- return;
- }
- route[WITHINDLG] {
- if (is_method("INVITE") && !has_totag()) {
- route(DIALOG);
- }
- if (has_totag()) {
- if (loose_route()) {
- if (is_method("BYE")) {
- if (sht_match_name("fsdest", "eq", "$fU:$rU")) {
- $sht(fsdest=>$fU:$rU) = $null;
- } else {
- $sht(fsdest=>$rU:$fU) = $null;
- }
- }
- handle_ruri_alias();
- route(RELAY);
- }
- }
- if (is_method("ACK")) {
- if (t_check_trans()) {
- route(RELAY);
- exit;
- } else {
- exit;
- }
- }
- }
- route[DIALOG] {
- // block any calls from trunk group exept for local subs and services
- if (allow_source_address(1) && dp_translate("1", "$rU")) {
- sl_send_reply("403","Not relaying");
- exit;
- }
- #!ifdef RTP_RELAY
- reg_fetch_contacts("location", "$fu", "src");
- $var(callr) = $(ulc(src=>addr){param.value,alias});
- reg_fetch_contacts("location", "$ou", "src");
- $var(callee) = $(ulc(src=>addr){param.value,alias});
- if ($var(callr) != "") {
- setflag(4);
- }
- if ($var(callee) != "") {
- setflag(5);
- }
- #!endif
- // Sending TO or setting routing FROM FS
- if (!ds_is_from_list("1", "2")) {
- setflag(toswitch);
- route(AUTH);
- handle_ruri_alias();
- record_route();
- route(RELAY);
- } else {
- setflag(fromswitch);
- // HERE WRITING NEW HTABLE ENTRY FOR BLIND TRANSFER CALLS(THOUGHT MAYBE I NEED TO WRITE IT FOR EVERY INVITE FROM FSs)
- if (is_present_hf("Referred-By")) {
- $sht(fsdest=>$fU:$rU) = $si;
- }
- // prefix testing for PSTN calls
- if (dp_translate("1", "$rU")) {
- route(PSTN);
- record_route();
- route(RELAY);
- } else {
- // Location test and relay to local sub
- if (!lookup("location")) {
- sl_send_reply("403", "Forbidden");
- exit;
- }
- handle_ruri_alias();
- record_route();
- route(RELAY);
- }
- }
- }
- route[RELAY] {
- if (is_method("INVITE")) {
- if(!t_is_set("failure_route")) t_on_failure("FAILURE");
- }
- #!ifdef RTP_RELAY
- if (isflagset(4) || isflagset(5)) {
- setflag(7);
- if (has_body("application/sdp")) {
- rtpengine_manage("replace-session-connection replace-origin ICE=remove direction=internal direction=external");
- }
- }
- #!endif
- t_on_reply("REPLY");
- if (isflagset(fromswitch)) {
- t_relay();
- }
- if (isflagset(toswitch)) {
- if (is_method("INVITE")) {
- //TEST FOR EXISTING $rU
- if (sht_match_name("fsdest", "re", ":$rU")) {
- //HERE I NEED TO SET NEW ENTRY WITH $du VALUE FROM EXISTING ENTRY, BUT CANT FIND IT
- $sht(fsdest=>$fU:$rU) = MUST BE $sht(fsdest=>existing table with $rU);
- $du = $sht(fsdest=>$fU:$rU);
- } else {
- ds_select_dst("1", "4");
- $sht(fsdest=>$fU:$rU) = $dd;
- xlog("WRITEN for $fU to $rU = $sht(fsdest=>$fU:$rU)");
- }
- }
- t_relay();
- }
- t_relay();
- }
- onreply_route[REPLY] {
- route(NAT);
- #!ifdef RTP_RELAY
- if (isflagset(7)) {
- if (has_body("application/sdp")) {
- rtpengine_manage("replace-session-connection replace-origin ICE=remove direction=internal direction=external");
- }
- }
- #!endif
- }
- route[PSTN] {
- dp_translate("2", "$rU/$rU");
- uac_replace_to("sip:$rU@gw_ip");
- $rd = "gw_ip";
- }
- failure_route[FAILURE] {
- route(NAT);
- $sht(fsdest=>$fU:$rU) = $null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement