Advertisement
tatarinsys

Kamailio Freeswitch HTABLE

Sep 8th, 2021 (edited)
4,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.63 KB | None | 0 0
  1. ####### Routing Logic ########
  2.  
  3. request_route {
  4.  
  5.  
  6. route(REQINIT);
  7.  
  8. if (is_method("CANCEL")) {
  9.   if (t_check_trans()) {
  10.       route(RELAY);
  11.   }
  12. exit;
  13. }
  14.  
  15. if (is_method("INVITE") || is_method("REGISTER")) {
  16.   route(NAT);
  17. }
  18.  
  19. if (is_method("REGISTER")) {
  20.   route(AUTH);
  21. }
  22.  
  23. route(WITHINDLG);
  24.  
  25. }
  26.  
  27. route[REQINIT] {
  28.     set_reply_no_connect();
  29.     force_rport();
  30.  
  31.     // trusted ip test
  32.     if (sht_match_name("trustip", "eq", "$si")) {
  33.         if ($sht(authban=>$si::$au) > 9) {
  34.           exit;
  35.         }
  36.       } else {
  37.         if ($sht(authban=>$si::$au) > 5) {
  38.           exit;
  39.         }
  40.       }
  41.  
  42.   #!ifdef WITH_ANTIFLOOD
  43.     # flood detection from same IP and traffic ban for a while
  44.     # be sure you exclude checking trusted peers, such as pstn gateways
  45.     # - local host excluded (e.g., loop to self)
  46.     if(src_ip!=myself) {
  47.         if($sht(ipban=>$si)!=$null) {
  48.             # ip is already blocked
  49.             xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  50.             exit;
  51.         }
  52.         if (!pike_check_req()) {
  53.             xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  54.             $sht(ipban=>$si) = 1;
  55.             exit;
  56.         }
  57.     }
  58. #!endif
  59.  
  60.     if($ua =~ "friendly|scanner|sipcli|sipvicious|VaxSIPUserAgent") {
  61.         # silent drop for scanners - uncomment next line if want to reply
  62.         # sl_send_reply("200", "OK");
  63.         exit;
  64.     }
  65.  
  66.     if (!mf_process_maxfwd_header("10")) {
  67.         sl_send_reply("483","Too Many Hops");
  68.         exit;
  69.     }
  70.  
  71.     if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  72.         sl_send_reply("200","Keepalive");
  73.         exit;
  74.     }
  75.  
  76.     if(!sanity_check("17895", "7")) {
  77.         xlog("Malformed SIP request from $si:$sp\n");
  78.         exit;
  79.     }
  80. }
  81.  
  82. route[AUTH] {
  83.  
  84.   if((!is_method("REGISTER")) && allow_source_address(1)) {
  85.       // source IP allowed
  86.     return;
  87.   }
  88.  
  89.   if (sht_match_name("auth", "eq", "$Au")) {
  90.     if (!pv_auth_check("$fd", "$sht(auth=>$Au)", "0", "1")) {
  91.       auth_challenge("$fd", "1");
  92.       if(is_present_hf("Authorization") || is_present_hf("Proxy-Authorization")) {
  93.         $sht(authban=>$si::$au) = $sht(authban=>$si::$au) + 1;
  94.         exit;
  95.       }
  96.       exit;
  97.     }
  98.     consume_credentials();
  99.     $sht(authban=>$si::$au) = $null;
  100.     if (is_method("REGISTER")) {
  101.       save("location", "0x04");
  102.       if (!sht_match_name("trustip", "eq", "$si")) {
  103.         $sht(trustip=>$si) = $au;
  104.       }
  105.       exit;
  106.     }
  107.   } else {
  108.     if (!auth_check("$fd", "subscriber", "1")) {
  109.       auth_challenge("$fd", "1");
  110.       if(is_present_hf("Authorization") || is_present_hf("Proxy-Authorization")) {
  111.         $sht(authban=>$si::$au) = $sht(authban=>$si::$au) + 1;
  112.         exit;
  113.       }
  114.       exit;
  115.     }
  116.     $sht(auth=>$Au) = $avp(pass);
  117.     $sht(authban=>$si::$au) = $null;    
  118.     consume_credentials();
  119.     if (is_method("REGISTER")) {
  120.       save("location", "0x04");
  121.       if (!sht_match_name("trustip", "eq", "$si")) {
  122.         $sht(trustip=>$si) = $au;
  123.       }
  124.       exit;
  125.     }        
  126.   }
  127. }
  128.  
  129. route[NAT] {
  130.   if (nat_uac_test("18") || $Ri == "kam.ext.sock.ip") {
  131.     if (is_method("REGISTER")) {      
  132.       set_contact_alias();
  133.     } else {
  134.       if (is_first_hop()) {
  135.         if (!ds_is_from_list("1", "2")) {  
  136.           set_contact_alias();
  137.         }
  138.       }
  139.     }
  140.   }
  141.   return;
  142. }
  143.  
  144. route[WITHINDLG] {
  145.   if (is_method("INVITE") && !has_totag()) {
  146.     route(DIALOG);
  147.   }
  148.  
  149.   if (has_totag()) {
  150.     if (loose_route()) {
  151.       if (is_method("BYE")) {
  152.           if (sht_match_name("fsdest", "eq", "$fU:$rU")) {
  153.             $sht(fsdest=>$fU:$rU) = $null;
  154.           } else {
  155.             $sht(fsdest=>$rU:$fU) = $null;
  156.           }
  157.       }
  158.       handle_ruri_alias();
  159.       route(RELAY);
  160.     }
  161.   }
  162.  
  163.   if (is_method("ACK")) {
  164.     if (t_check_trans()) {
  165.       route(RELAY);
  166.       exit;
  167.     } else {
  168.       exit;
  169.     }
  170.   }
  171.  
  172. }
  173.  
  174. route[DIALOG] {
  175.   // block any calls from trunk group exept for local subs and services
  176.   if (allow_source_address(1) && dp_translate("1", "$rU")) {
  177.     sl_send_reply("403","Not relaying");
  178.     exit;
  179.   }
  180. #!ifdef RTP_RELAY
  181.     reg_fetch_contacts("location", "$fu", "src");
  182.     $var(callr) = $(ulc(src=>addr){param.value,alias});
  183.     reg_fetch_contacts("location", "$ou", "src");
  184.     $var(callee) = $(ulc(src=>addr){param.value,alias});
  185.     if ($var(callr) != "") {
  186.         setflag(4);
  187.     }
  188.     if ($var(callee) != "") {
  189.         setflag(5);
  190.     }
  191. #!endif
  192.   // Sending TO or setting routing FROM FS
  193.   if (!ds_is_from_list("1", "2")) {
  194.     setflag(toswitch);
  195.     route(AUTH);
  196.     handle_ruri_alias();
  197.     record_route();
  198.     route(RELAY);
  199.   } else {
  200.     setflag(fromswitch);
  201.     // HERE WRITING NEW HTABLE ENTRY FOR BLIND TRANSFER CALLS(THOUGHT MAYBE I NEED TO WRITE IT FOR EVERY INVITE FROM FSs)
  202.     if (is_present_hf("Referred-By")) {
  203.       $sht(fsdest=>$fU:$rU) = $si;
  204.     }
  205.     // prefix testing for PSTN calls
  206.     if (dp_translate("1", "$rU")) {
  207.       route(PSTN);
  208.       record_route();
  209.       route(RELAY);
  210.     } else {
  211.       // Location test and relay to local sub
  212.       if (!lookup("location")) {
  213.         sl_send_reply("403", "Forbidden");
  214.         exit;
  215.       }
  216.       handle_ruri_alias();
  217.       record_route();
  218.       route(RELAY);
  219.     }
  220.   }    
  221. }
  222.  
  223. route[RELAY] {
  224.     if (is_method("INVITE")) {
  225.         if(!t_is_set("failure_route")) t_on_failure("FAILURE");
  226.     }
  227. #!ifdef RTP_RELAY
  228.     if (isflagset(4) || isflagset(5)) {
  229.         setflag(7);
  230.         if (has_body("application/sdp")) {
  231.         rtpengine_manage("replace-session-connection replace-origin ICE=remove direction=internal direction=external");
  232.         }
  233.     }
  234. #!endif
  235.     t_on_reply("REPLY");
  236.     if (isflagset(fromswitch)) {
  237.         t_relay();
  238.     }
  239.  
  240.     if (isflagset(toswitch)) {
  241.         if (is_method("INVITE")) {
  242.           //TEST FOR EXISTING $rU
  243.           if (sht_match_name("fsdest", "re", ":$rU")) {
  244.              //HERE I NEED TO SET NEW ENTRY WITH $du VALUE FROM EXISTING ENTRY, BUT CANT FIND IT
  245.              $sht(fsdest=>$fU:$rU) = MUST BE $sht(fsdest=>existing table with $rU);
  246.              $du = $sht(fsdest=>$fU:$rU);
  247.           } else {        
  248.             ds_select_dst("1", "4");
  249.             $sht(fsdest=>$fU:$rU) = $dd;
  250.             xlog("WRITEN for $fU to $rU = $sht(fsdest=>$fU:$rU)");
  251.           }  
  252.         }
  253.         t_relay();
  254.     }
  255.     t_relay();
  256. }
  257.  
  258. onreply_route[REPLY] {
  259.   route(NAT);
  260. #!ifdef RTP_RELAY
  261.   if (isflagset(7)) {
  262.     if (has_body("application/sdp")) {
  263.       rtpengine_manage("replace-session-connection replace-origin ICE=remove direction=internal direction=external");
  264.     }
  265.   }
  266. #!endif
  267. }
  268.  
  269. route[PSTN] {
  270.     dp_translate("2", "$rU/$rU");
  271.     uac_replace_to("sip:$rU@gw_ip");
  272.     $rd = "gw_ip";
  273.  
  274. }
  275.  
  276. failure_route[FAILURE] {
  277.   route(NAT);
  278.   $sht(fsdest=>$fU:$rU) = $null;
  279. }
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement