Advertisement
Guest User

Untitled

a guest
Sep 21st, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.13 KB | None | 0 0
  1. #!KAMAILIO
  2. #
  3. # Kamailio (OpenSER) SIP Server v4.2 - default configuration script
  4. # - web: http://www.kamailio.org
  5. # - git: http://sip-router.org
  6. #
  7. # Direct your questions about this file to: <[email protected]>
  8. #
  9. # Refer to the Core CookBook at http://www.kamailio.org/wiki/
  10. # for an explanation of possible statements, functions and parameters.
  11. #
  12. # Several features can be enabled using '#!define WITH_FEATURE' directives:
  13. #
  14. # *** To run in debug mode:
  15. # - define WITH_DEBUG
  16. #
  17. # *** To enable mysql:
  18. # - define WITH_MYSQL
  19. #
  20. # *** To enable authentication execute:
  21. # - enable mysql
  22. # - define WITH_AUTH
  23. # - add users using 'kamctl'
  24. #
  25. # *** To enable IP authentication execute:
  26. # - enable mysql
  27. # - enable authentication
  28. # - define WITH_IPAUTH
  29. # - add IP addresses with group id '1' to 'address' table
  30. #
  31. # *** To enable persistent user location execute:
  32. # - enable mysql
  33. # - define WITH_USRLOCDB
  34. #
  35. # *** To enable presence server execute:
  36. # - enable mysql
  37. # - define WITH_PRESENCE
  38. #
  39. # *** To enable nat traversal execute:
  40. # - define WITH_NAT
  41. # - install RTPProxy: http://www.rtpproxy.org
  42. # - start RTPProxy:
  43. # rtpproxy -l _your_public_ip_ -s udp:localhost:7722
  44. # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
  45. #
  46. # *** To enable PSTN gateway routing execute:
  47. # - define WITH_PSTN
  48. # - set the value of pstn.gw_ip
  49. # - check route[PSTN] for regexp routing condition
  50. #
  51. # *** To enable database aliases lookup execute:
  52. # - enable mysql
  53. # - define WITH_ALIASDB
  54. #
  55. # *** To enable speed dial lookup execute:
  56. # - enable mysql
  57. # - define WITH_SPEEDDIAL
  58. #
  59. # *** To enable multi-domain support execute:
  60. # - enable mysql
  61. # - define WITH_MULTIDOMAIN
  62. #
  63. # *** To enable TLS support execute:
  64. # - adjust CFGDIR/tls.cfg as needed
  65. # - define WITH_TLS
  66. #
  67. # *** To enable XMLRPC support execute:
  68. # - define WITH_XMLRPC
  69. # - adjust route[XMLRPC] for access policy
  70. #
  71. # *** To enable anti-flood detection execute:
  72. # - adjust pike and htable=>ipban settings as needed (default is
  73. # block if more than 16 requests in 2 seconds and ban for 300 seconds)
  74. # - define WITH_ANTIFLOOD
  75. #
  76. # *** To block 3XX redirect replies execute:
  77. # - define WITH_BLOCK3XX
  78. #
  79. # *** To enable VoiceMail routing execute:
  80. # - define WITH_VOICEMAIL
  81. # - set the value of voicemail.srv_ip
  82. # - adjust the value of voicemail.srv_port
  83. #
  84. # *** To enhance accounting execute:
  85. # - enable mysql
  86. # - define WITH_ACCDB
  87. # - add following columns to database
  88. #!ifdef ACCDB_COMMENT
  89. ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  90. ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  91. ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
  92. ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  93. ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  94. ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  95. ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  96. ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  97. ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
  98. ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  99. ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  100. ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  101. #!endif
  102.  
  103. ####### Include Local Config If Exists #########
  104. import_file "kamailio-local.cfg"
  105.  
  106. ####### Defined Values #########
  107.  
  108. #!define WITH_LOADBALANCE
  109. #!define WITH_MYSQL
  110. #!define WITH_AUTH
  111. #!define WITH_IPAUTH
  112.  
  113. # *** Value defines - IDs used later in config
  114. #!ifdef WITH_MYSQL
  115. # - database URL - used to connect to database server by modules such
  116. # as: auth_db, acc, usrloc, a.s.o.
  117. #!ifndef DBURL
  118. #!define DBURL "mysql://kamailio:kamailiorw@localhost:3306/kamailio"
  119. #!endif
  120. #!endif
  121. #!ifdef WITH_MULTIDOMAIN
  122. # - the value for 'use_domain' parameters
  123. #!define MULTIDOMAIN 1
  124. #!else
  125. #!define MULTIDOMAIN 0
  126. #!endif
  127.  
  128. # - flags
  129. # FLT_ - per transaction (message) flags
  130. # FLB_ - per branch flags
  131. #!define FLT_ACC 1
  132. #!define FLT_ACCMISSED 2
  133. #!define FLT_ACCFAILED 3
  134. #!define FLT_NATS 5
  135.  
  136. #!define FLB_NATB 6
  137. #!define FLB_NATSIPPING 7
  138.  
  139. ####### Global Parameters #########
  140.  
  141. ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
  142. #!ifdef WITH_DEBUG
  143. debug=2
  144. log_stderror=no
  145. #!else
  146. debug=2
  147. log_stderror=no
  148. #!endif
  149.  
  150. memdbg=5
  151. memlog=5
  152.  
  153. log_facility=LOG_LOCAL0
  154.  
  155. fork=yes
  156. children=4
  157.  
  158. /* uncomment the next line to disable TCP (default on) */
  159. #disable_tcp=yes
  160.  
  161. /* uncomment the next line to disable the auto discovery of local aliases
  162. based on reverse DNS on IPs (default on) */
  163. #auto_aliases=no
  164.  
  165. /* add local domain aliases */
  166. #alias="sip.mydomain.com"
  167.  
  168. /* uncomment and configure the following line if you want Kamailio to
  169. bind on a specific interface/port/proto (default bind on all available) */
  170. listen=udp:2.2.2.2:5060
  171.  
  172. /* port to listen to
  173. * - can be specified more than once if needed to listen on many ports */
  174. port=5060
  175.  
  176. #!ifdef WITH_TLS
  177. enable_tls=yes
  178. #!endif
  179.  
  180. # life time of TCP connection when there is no traffic
  181. # - a bit higher than registration expires to cope with UA behind NAT
  182. tcp_connection_lifetime=3605
  183.  
  184. ####### Custom Parameters #########
  185.  
  186. # These parameters can be modified runtime via RPC interface
  187. # - see the documentation of 'cfg_rpc' module.
  188. #
  189. # Format: group.id = value 'desc' description
  190. # Access: $sel(cfg_get.group.id) or @cfg_get.group.id
  191. #
  192.  
  193. #!ifdef WITH_PSTN
  194. # PSTN GW Routing
  195. #
  196. # - pstn.gw_ip: valid IP or hostname as string value, example:
  197. # pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
  198. #
  199. # - by default is empty to avoid misrouting
  200. pstn.gw_ip = "" desc "PSTN GW Address"
  201. pstn.gw_port = "" desc "PSTN GW Port"
  202. #!endif
  203.  
  204. #!ifdef WITH_VOICEMAIL
  205. # VoiceMail Routing on offline, busy or no answer
  206. #
  207. # - by default Voicemail server IP is empty to avoid misrouting
  208. voicemail.srv_ip = "" desc "VoiceMail IP Address"
  209. voicemail.srv_port = "5060" desc "VoiceMail Port"
  210. #!endif
  211.  
  212. ####### Modules Section ########
  213.  
  214. # set paths to location of modules (to sources or installation folders)
  215. #!ifdef WITH_SRCPATH
  216. mpath="modules/"
  217. #!else
  218. mpath="/usr/lib64/kamailio/modules/"
  219. #!endif
  220.  
  221. #!ifdef WITH_MYSQL
  222. loadmodule "db_mysql.so"
  223. #!endif
  224.  
  225. loadmodule "rtpengine.so"
  226.  
  227. loadmodule "mi_fifo.so"
  228. loadmodule "kex.so"
  229. loadmodule "corex.so"
  230. loadmodule "tm.so"
  231. loadmodule "tmx.so"
  232. loadmodule "sl.so"
  233. loadmodule "rr.so"
  234. loadmodule "pv.so"
  235. loadmodule "maxfwd.so"
  236. loadmodule "usrloc.so"
  237. loadmodule "registrar.so"
  238. loadmodule "textops.so"
  239. loadmodule "siputils.so"
  240. loadmodule "xlog.so"
  241. loadmodule "sanity.so"
  242. loadmodule "ctl.so"
  243. loadmodule "cfg_rpc.so"
  244. loadmodule "mi_rpc.so"
  245. loadmodule "acc.so"
  246.  
  247. loadmodule "outbound.so"
  248. loadmodule "stun.so"
  249.  
  250. #!ifdef WITH_AUTH
  251. loadmodule "auth.so"
  252. loadmodule "auth_db.so"
  253. #!ifdef WITH_IPAUTH
  254. loadmodule "permissions.so"
  255. #!endif
  256. #!endif
  257.  
  258. #!ifdef WITH_ALIASDB
  259. loadmodule "alias_db.so"
  260. #!endif
  261.  
  262. #!ifdef WITH_SPEEDDIAL
  263. loadmodule "speeddial.so"
  264. #!endif
  265.  
  266. #!ifdef WITH_MULTIDOMAIN
  267. loadmodule "domain.so"
  268. #!endif
  269.  
  270. #!ifdef WITH_PRESENCE
  271. loadmodule "presence.so"
  272. loadmodule "presence_xml.so"
  273. #!endif
  274.  
  275. #!ifdef WITH_NAT
  276. loadmodule "nathelper.so"
  277. loadmodule "rtpproxy.so"
  278. #!endif
  279.  
  280. #!ifdef WITH_TLS
  281. loadmodule "tls.so"
  282. #!endif
  283.  
  284. #!ifdef WITH_ANTIFLOOD
  285. loadmodule "htable.so"
  286. loadmodule "pike.so"
  287. #!endif
  288.  
  289. #!ifdef WITH_XMLRPC
  290. loadmodule "xmlrpc.so"
  291. #!endif
  292.  
  293. #!ifdef WITH_DEBUG
  294. loadmodule "debugger.so"
  295. #!endif
  296.  
  297.  
  298. #!ifdef WITH_LOADBALANCE
  299. loadmodule "dispatcher.so"
  300. #!endif
  301.  
  302.  
  303. # ----------------- setting module-specific parameters ---------------
  304.  
  305. modparam("rtpengine", "rtpengine_sock", "udp:localhost:2223")
  306.  
  307.  
  308.  
  309. #!ifdef WITH_LOADBALANCE
  310. modparam("dispatcher", "db_url", "mysql://kamailio:kamailiorw@localhost:3306/kamailio")
  311. modparam("dispatcher", "flags", 2)
  312. modparam("dispatcher", "dst_avp", "$avp(AVP_DST)")
  313. modparam("dispatcher", "grp_avp", "$avp(AVP_GRP)")
  314. modparam("dispatcher", "cnt_avp", "$avp(AVP_CNT)")
  315. modparam("dispatcher", "sock_avp", "$avp(AVP_SOCK)")
  316. modparam("dispatcher", "ds_ping_interval", 4)
  317. modparam("dispatcher", "ds_ping_from", "sip:[email protected]")
  318. modparam("dispatcher", "ds_probing_mode", 1)
  319. modparam("dispatcher", "ds_probing_threshold", 5)
  320. modparam("dispatcher", "ds_ping_reply_codes", "class=2;code=480;code=404")
  321. #!endif
  322.  
  323.  
  324. # ----- mi_fifo params -----
  325. modparam("mi_fifo", "fifo_name", "/usr/local/var/run/kamailio/kamailio_fifo")
  326.  
  327.  
  328. # ----- tm params -----
  329. # auto-discard branches from previous serial forking leg
  330. modparam("tm", "failure_reply_mode", 3)
  331. # default retransmission timeout: 30sec
  332. modparam("tm", "fr_timer", 30000)
  333. # default invite retransmission timeout after 1xx: 120sec
  334. modparam("tm", "fr_inv_timer", 120000)
  335.  
  336.  
  337. # ----- rr params -----
  338. # set next param to 1 to add value to ;lr param (helps with some UAs)
  339. modparam("rr", "enable_full_lr", 0)
  340. # do not append from tag to the RR (no need for this script)
  341. modparam("rr", "append_fromtag", 0)
  342.  
  343.  
  344. # ----- registrar params -----
  345. modparam("registrar", "method_filtering", 1)
  346. /* uncomment the next line to disable parallel forking via location */
  347. # modparam("registrar", "append_branches", 0)
  348. /* uncomment the next line not to allow more than 10 contacts per AOR */
  349. #modparam("registrar", "max_contacts", 10)
  350. # max value for expires of registrations
  351. modparam("registrar", "max_expires", 3600)
  352. # set it to 1 to enable GRUU
  353. modparam("registrar", "gruu_enabled", 0)
  354.  
  355.  
  356. # ----- acc params -----
  357. /* what special events should be accounted ? */
  358. modparam("acc", "early_media", 0)
  359. modparam("acc", "report_ack", 0)
  360. modparam("acc", "report_cancels", 0)
  361. /* by default ww do not adjust the direct of the sequential requests.
  362. if you enable this parameter, be sure the enable "append_fromtag"
  363. in "rr" module */
  364. modparam("acc", "detect_direction", 0)
  365. /* account triggers (flags) */
  366. modparam("acc", "log_flag", FLT_ACC)
  367. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  368. modparam("acc", "log_extra",
  369. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  370. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  371. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  372. /* enhanced DB accounting */
  373. #!ifdef WITH_ACCDB
  374. modparam("acc", "db_flag", FLT_ACC)
  375. modparam("acc", "db_missed_flag", FLT_ACCMISSED)
  376. modparam("acc", "db_url", DBURL)
  377. modparam("acc", "db_extra",
  378. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  379. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  380. #!endif
  381.  
  382.  
  383. # ----- usrloc params -----
  384. /* enable DB persistency for location entries */
  385. #!ifdef WITH_USRLOCDB
  386. modparam("usrloc", "db_url", DBURL)
  387. modparam("usrloc", "db_mode", 2)
  388. modparam("usrloc", "use_domain", MULTIDOMAIN)
  389. #!endif
  390.  
  391.  
  392. # ----- auth_db params -----
  393. #!ifdef WITH_AUTH
  394. modparam("auth_db", "db_url", "mysql://kamailio:kamailiorw@localhost:3306/kamailio")
  395. modparam("auth_db", "calculate_ha1", yes)
  396. modparam("auth_db", "password_column", "password")
  397. modparam("auth_db", "load_credentials", "")
  398. modparam("auth_db", "use_domain", MULTIDOMAIN)
  399.  
  400. # ----- permissions params -----
  401. #!ifdef WITH_IPAUTH
  402. modparam("permissions", "db_url", "mysql://kamailio:kamailiorw@localhost:3306/kamailio")
  403. modparam("permissions", "db_mode", 0)
  404. #!endif
  405.  
  406. #!endif
  407.  
  408.  
  409. # ----- alias_db params -----
  410. #!ifdef WITH_ALIASDB
  411. modparam("alias_db", "db_url", DBURL)
  412. modparam("alias_db", "use_domain", MULTIDOMAIN)
  413. #!endif
  414.  
  415.  
  416. # ----- speeddial params -----
  417. #!ifdef WITH_SPEEDDIAL
  418. modparam("speeddial", "db_url", DBURL)
  419. modparam("speeddial", "use_domain", MULTIDOMAIN)
  420. #!endif
  421.  
  422.  
  423. # ----- domain params -----
  424. #!ifdef WITH_MULTIDOMAIN
  425. modparam("domain", "db_url", DBURL)
  426. # register callback to match myself condition with domains list
  427. modparam("domain", "register_myself", 1)
  428. #!endif
  429.  
  430.  
  431. #!ifdef WITH_PRESENCE
  432. # ----- presence params -----
  433. modparam("presence", "db_url", DBURL)
  434.  
  435. # ----- presence_xml params -----
  436. modparam("presence_xml", "db_url", DBURL)
  437. modparam("presence_xml", "force_active", 1)
  438. #!endif
  439.  
  440.  
  441. #!ifdef WITH_NAT
  442. # ----- rtpproxy params -----
  443. modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
  444.  
  445. # ----- nathelper params -----
  446. modparam("nathelper", "natping_interval", 30)
  447. modparam("nathelper", "ping_nated_only", 1)
  448. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  449. modparam("nathelper", "sipping_from", "sip:[email protected]")
  450.  
  451. # params needed for NAT traversal in other modules
  452. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  453. modparam("usrloc", "nat_bflag", FLB_NATB)
  454. #!endif
  455.  
  456.  
  457. #!ifdef WITH_TLS
  458. # ----- tls params -----
  459. modparam("tls", "config", "//etc/kamailio/tls.cfg")
  460. #!endif
  461.  
  462. #!ifdef WITH_ANTIFLOOD
  463. # ----- pike params -----
  464. modparam("pike", "sampling_time_unit", 2)
  465. modparam("pike", "reqs_density_per_unit", 16)
  466. modparam("pike", "remove_latency", 4)
  467.  
  468. # ----- htable params -----
  469. # ip ban htable with autoexpire after 5 minutes
  470. modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
  471. #!endif
  472.  
  473. #!ifdef WITH_XMLRPC
  474. # ----- xmlrpc params -----
  475. modparam("xmlrpc", "route", "XMLRPC");
  476. modparam("xmlrpc", "url_match", "^/RPC")
  477. #!endif
  478.  
  479. #!ifdef WITH_DEBUG
  480. # ----- debugger params -----
  481. modparam("debugger", "cfgtrace", 1)
  482. modparam("debugger", "log_level_name", "exec")
  483. #!endif
  484.  
  485. ####### Routing Logic ########
  486.  
  487.  
  488. # Main SIP request routing logic
  489. # - processing of any incoming SIP request starts with this route
  490. # - note: this is the same as route { ... }
  491. request_route {
  492.  
  493. # per request initial checks
  494. route(REQINIT);
  495.  
  496. # NAT detection
  497. route(NATDETECT);
  498.  
  499. # CANCEL processing
  500. if (is_method("CANCEL")) {
  501. if (t_check_trans()) {
  502. route(RELAY);
  503. }
  504. exit;
  505. }
  506.  
  507. # handle requests within SIP dialogs
  508. route(WITHINDLG);
  509.  
  510. ### only initial requests (no To tag)
  511.  
  512. # handle retransmissions
  513. if(t_precheck_trans()) {
  514. t_check_trans();
  515. exit;
  516. }
  517. t_check_trans();
  518.  
  519. # authentication
  520. route(AUTH);
  521.  
  522. # record routing for dialog forming requests (in case they are routed)
  523. # - remove preloaded route headers
  524. remove_hf("Route");
  525. if (is_method("INVITE|SUBSCRIBE"))
  526. record_route();
  527.  
  528. # account only INVITEs
  529. if (is_method("INVITE")) {
  530. setflag(FLT_ACC); # do accounting
  531. }
  532.  
  533. # dispatch requests to foreign domains
  534. route(SIPOUT);
  535.  
  536. ### requests for my local domains
  537.  
  538. # handle presence related requests
  539. route(PRESENCE);
  540.  
  541. # handle registrations
  542.  
  543. route(REGISTRAR);
  544.  
  545. if ($rU==$null) {
  546. # request with no Username in RURI
  547. sl_send_reply("484","Address Incomplete");
  548. exit;
  549. }
  550.  
  551. # dispatch destinations to PSTN
  552. route(PSTN);
  553.  
  554. #!ifdef WITH_LOADBALANCE
  555. route(LOADBALANCE);
  556. route(RELAY);
  557. #!else
  558. route(LOCATION);
  559. #!endif
  560.  
  561.  
  562. }
  563.  
  564.  
  565.  
  566. #!ifdef WITH_LOADBALANCE
  567. route[LOADBALANCE] {
  568.  
  569. #ds_select_dst(destination_set, algorithm) function chooses the destination for the call. For this it can use a lot of algorithms.
  570. #Alg. 0 is the default one that does the the choosing over the call ID hash
  571. #Alg. 4 is a Round-Robin
  572. #Alg. 10 is the one that chooses the destination based on the minimum load of all destinations
  573. if(!ds_select_dst("1", "4"))
  574. {
  575. #if we are here that means no destination is available. We notify the user by 404 and exit the script.
  576. xlog("L_NOTICE", "No destination available!");
  577. send_reply("404", "No destination");
  578. exit;
  579. }
  580. xlog("L_DEBUG", "Routing call to <$ru> via <$du>\n");
  581. #set the no_reply_recieved timeout to 2 second ... adjust the value to your need
  582. #note: The first value "0" is invite timeout .. we do not need to change it
  583. #This means that is the selected media server fails to respond within 2 seconds the failure_route "MANAGE_FAILURE" is called
  584. #note: this implies that ale the signaling from media servers on the way back to the user goes through the proxy as well
  585. t_set_fr(0,2000);
  586. t_on_failure("MANAGE_FAILURE");
  587. return;
  588. }
  589. #!endif
  590.  
  591.  
  592. # Wrapper for relaying requests
  593. route[RELAY] {
  594.  
  595. rtpengine_manage();
  596.  
  597. # enable additional event routes for forwarded requests
  598. # - serial forking, RTP relaying handling, a.s.o.
  599. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  600. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  601. }
  602. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  603. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  604. }
  605. if (is_method("INVITE")) {
  606. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  607. }
  608.  
  609. if (!t_relay()) {
  610. sl_reply_error();
  611. }
  612. exit;
  613. }
  614.  
  615. # Per SIP request initial checks
  616. route[REQINIT] {
  617. #!ifdef WITH_ANTIFLOOD
  618. # flood dection from same IP and traffic ban for a while
  619. # be sure you exclude checking trusted peers, such as pstn gateways
  620. # - local host excluded (e.g., loop to self)
  621. if(src_ip!=myself) {
  622. if($sht(ipban=>$si)!=$null) {
  623. # ip is already blocked
  624. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  625. exit;
  626. }
  627. if (!pike_check_req()) {
  628. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  629. $sht(ipban=>$si) = 1;
  630. exit;
  631. }
  632. }
  633. if($ua =~ "friendly-scanner") {
  634. sl_send_reply("200", "OK");
  635. exit;
  636. }
  637. #!endif
  638.  
  639. if (!mf_process_maxfwd_header("10")) {
  640. sl_send_reply("483","Too Many Hops");
  641. exit;
  642. }
  643.  
  644. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  645. sl_send_reply("200","Keepalive");
  646. exit;
  647. }
  648.  
  649. if(!sanity_check("1511", "7")) {
  650. xlog("Malformed SIP message from $si:$sp\n");
  651. exit;
  652. }
  653. }
  654.  
  655. # Handle requests within SIP dialogs
  656. route[WITHINDLG] {
  657. if (!has_totag()) return;
  658.  
  659. # sequential request withing a dialog should
  660. # take the path determined by record-routing
  661. if (loose_route()) {
  662. route(DLGURI);
  663. if (is_method("BYE")) {
  664. setflag(FLT_ACC); # do accounting ...
  665. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  666. }
  667. else if ( is_method("ACK") ) {
  668. # ACK is forwarded statelessy
  669. route(NATMANAGE);
  670. }
  671. else if ( is_method("NOTIFY") ) {
  672. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  673. record_route();
  674. }
  675. route(RELAY);
  676. exit;
  677. }
  678.  
  679. if (is_method("SUBSCRIBE") && uri == myself) {
  680. # in-dialog subscribe requests
  681. route(PRESENCE);
  682. exit;
  683. }
  684. if ( is_method("ACK") ) {
  685. if ( t_check_trans() ) {
  686. # no loose-route, but stateful ACK;
  687. # must be an ACK after a 487
  688. # or e.g. 404 from upstream server
  689. route(RELAY);
  690. exit;
  691. } else {
  692. # ACK without matching transaction ... ignore and discard
  693. exit;
  694. }
  695. }
  696. sl_send_reply("404","Not here");
  697. exit;
  698. }
  699.  
  700. # Handle SIP registrations
  701. route[REGISTRAR] {
  702. if (!is_method("REGISTER")) return;
  703.  
  704. if(isflagset(FLT_NATS)) {
  705. setbflag(FLB_NATB);
  706. #!ifdef WITH_NATSIPPING
  707. # do SIP NAT pinging
  708. setbflag(FLB_NATSIPPING);
  709. #!endif
  710. }
  711. if (!save("location"))
  712. sl_reply_error();
  713. exit;
  714. }
  715.  
  716. # User location service
  717. route[LOCATION] {
  718.  
  719. #!ifdef WITH_SPEEDDIAL
  720. # search for short dialing - 2-digit extension
  721. if($rU=~"^[0-9][0-9]$")
  722. if(sd_lookup("speed_dial"))
  723. route(SIPOUT);
  724. #!endif
  725.  
  726. #!ifdef WITH_ALIASDB
  727. # search in DB-based aliases
  728. if(alias_db_lookup("dbaliases"))
  729. route(SIPOUT);
  730. #!endif
  731.  
  732. $avp(oexten) = $rU;
  733. if (!lookup("location")) {
  734. $var(rc) = $rc;
  735. route(TOVOICEMAIL);
  736. t_newtran();
  737. switch ($var(rc)) {
  738. case -1:
  739. case -3:
  740. send_reply("404", "Not Found");
  741. exit;
  742. case -2:
  743. send_reply("405", "Method Not Allowed");
  744. exit;
  745. }
  746. }
  747.  
  748. # when routing via usrloc, log the missed calls also
  749. if (is_method("INVITE")) {
  750. setflag(FLT_ACCMISSED);
  751. }
  752.  
  753. route(RELAY);
  754. exit;
  755. }
  756.  
  757. # Presence server processing
  758. route[PRESENCE] {
  759. if(!is_method("PUBLISH|SUBSCRIBE"))
  760. return;
  761.  
  762. if(is_method("SUBSCRIBE") && $hdr(Event)=="message-summary") {
  763. route(TOVOICEMAIL);
  764. # returns here if no voicemail server is configured
  765. sl_send_reply("404", "No voicemail service");
  766. exit;
  767. }
  768.  
  769. #!ifdef WITH_PRESENCE
  770. if (!t_newtran()) {
  771. sl_reply_error();
  772. exit;
  773. }
  774.  
  775. if(is_method("PUBLISH")) {
  776. handle_publish();
  777. t_release();
  778. } else if(is_method("SUBSCRIBE")) {
  779. handle_subscribe();
  780. t_release();
  781. }
  782. exit;
  783. #!endif
  784.  
  785. # if presence enabled, this part will not be executed
  786. if (is_method("PUBLISH") || $rU==$null) {
  787. sl_send_reply("404", "Not here");
  788. exit;
  789. }
  790. return;
  791. }
  792.  
  793. # IP authorization and user uthentication
  794. route[AUTH] {
  795. #!ifdef WITH_AUTH
  796.  
  797. #!ifdef WITH_IPAUTH
  798.  
  799. // if((!is_method("REGISTER")) && !allow_source_address()) {
  800. // # source IP allowed
  801. // sl_send_reply("403", "Forbidden");
  802. // exit;
  803. // }
  804.  
  805. if (!allow_source_address("1")) {
  806. sl_send_reply("403", "Forbidden");
  807. exit;
  808. };
  809.  
  810. #!endif
  811.  
  812. if (is_method("REGISTER") || from_uri==myself)
  813. {
  814. # authenticate requests
  815. if (!auth_check("$fd", "subscriber", "1")) {
  816. auth_challenge("$fd", "0");
  817. exit;
  818. }
  819. # user authenticated - remove auth header
  820. if(!is_method("REGISTER|PUBLISH"))
  821. consume_credentials();
  822. }
  823. # if caller is not local subscriber, then check if it calls
  824. # a local destination, otherwise deny, not an open relay here
  825. if (from_uri!=myself && uri!=myself) {
  826. sl_send_reply("403","Not relaying");
  827. exit;
  828. }
  829.  
  830. append_hf("X-SIP: $si\r\n");
  831.  
  832. #!endif
  833. return;
  834. }
  835.  
  836. # Caller NAT detection
  837. route[NATDETECT] {
  838. #!ifdef WITH_NAT
  839. force_rport();
  840. if (nat_uac_test("19")) {
  841. if (is_method("REGISTER")) {
  842. fix_nated_register();
  843. } else {
  844. if(is_first_hop())
  845. set_contact_alias();
  846. }
  847. setflag(FLT_NATS);
  848. }
  849. #!endif
  850. return;
  851. }
  852.  
  853. # RTPProxy control and singaling updates for NAT traversal
  854. route[NATMANAGE] {
  855. #!ifdef WITH_NAT
  856. if (is_request()) {
  857. if(has_totag()) {
  858. if(check_route_param("nat=yes")) {
  859. setbflag(FLB_NATB);
  860. }
  861. }
  862. }
  863. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
  864. return;
  865.  
  866. rtpproxy_manage("co");
  867.  
  868. if (is_request()) {
  869. if (!has_totag()) {
  870. if(t_is_branch_route()) {
  871. add_rr_param(";nat=yes");
  872. }
  873. }
  874. }
  875. if (is_reply()) {
  876. if(isbflagset(FLB_NATB)) {
  877. if(is_first_hop())
  878. set_contact_alias();
  879. }
  880. }
  881. #!endif
  882. return;
  883. }
  884.  
  885. # URI update for dialog requests
  886. route[DLGURI] {
  887. #!ifdef WITH_NAT
  888. if(!isdsturiset()) {
  889. handle_ruri_alias();
  890. }
  891. #!endif
  892. return;
  893. }
  894.  
  895. # Routing to foreign domains
  896. route[SIPOUT] {
  897. if (uri==myself) return;
  898.  
  899. append_hf("P-hint: outbound\r\n");
  900. route(RELAY);
  901. exit;
  902. }
  903.  
  904. # PSTN GW routing
  905. route[PSTN] {
  906. #!ifdef WITH_PSTN
  907. # check if PSTN GW IP is defined
  908. if (strempty($sel(cfg_get.pstn.gw_ip))) {
  909. xlog("SCRIPT: PSTN rotuing enabled but pstn.gw_ip not defined\n");
  910. return;
  911. }
  912.  
  913. # route to PSTN dialed numbers starting with '+' or '00'
  914. # (international format)
  915. # - update the condition to match your dialing rules for PSTN routing
  916. if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
  917. return;
  918.  
  919. # only local users allowed to call
  920. if(from_uri!=myself) {
  921. sl_send_reply("403", "Not Allowed");
  922. exit;
  923. }
  924.  
  925. if (strempty($sel(cfg_get.pstn.gw_port))) {
  926. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
  927. } else {
  928. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip) + ":"
  929. + $sel(cfg_get.pstn.gw_port);
  930. }
  931.  
  932. route(RELAY);
  933. exit;
  934. #!endif
  935.  
  936. return;
  937. }
  938.  
  939. # XMLRPC routing
  940. #!ifdef WITH_XMLRPC
  941. route[XMLRPC] {
  942. # allow XMLRPC from localhost
  943. if ((method=="POST" || method=="GET")
  944. && (src_ip==127.0.0.1)) {
  945. # close connection only for xmlrpclib user agents (there is a bug in
  946. # xmlrpclib: it waits for EOF before interpreting the response).
  947. if ($hdr(User-Agent) =~ "xmlrpclib")
  948. set_reply_close();
  949. set_reply_no_connect();
  950. dispatch_rpc();
  951. exit;
  952. }
  953. send_reply("403", "Forbidden");
  954. exit;
  955. }
  956. #!endif
  957.  
  958. # Routing to voicemail server
  959. route[TOVOICEMAIL] {
  960. #!ifdef WITH_VOICEMAIL
  961. if(!is_method("INVITE|SUBSCRIBE"))
  962. return;
  963.  
  964. # check if VoiceMail server IP is defined
  965. if (strempty($sel(cfg_get.voicemail.srv_ip))) {
  966. xlog("SCRIPT: VoiceMail rotuing enabled but IP not defined\n");
  967. return;
  968. }
  969. if(is_method("INVITE")) {
  970. if($avp(oexten)==$null)
  971. return;
  972. $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
  973. + ":" + $sel(cfg_get.voicemail.srv_port);
  974. } else {
  975. if($rU==$null)
  976. return;
  977. $ru = "sip:" + $rU + "@" + $sel(cfg_get.voicemail.srv_ip)
  978. + ":" + $sel(cfg_get.voicemail.srv_port);
  979. }
  980. route(RELAY);
  981. exit;
  982. #!endif
  983.  
  984. return;
  985. }
  986.  
  987.  
  988.  
  989.  
  990. # Manage outgoing branches
  991. branch_route[MANAGE_BRANCH] {
  992. xdbg("new branch [$T_branch_idx] to $ru\n");
  993. route(NATMANAGE);
  994. }
  995.  
  996. # Manage incoming replies
  997. onreply_route[MANAGE_REPLY] {
  998. xdbg("incoming reply\n");
  999. if(status=~"[12][0-9][0-9]") {
  1000. route(NATMANAGE);
  1001. }
  1002. rtpengine_manage();
  1003. }
  1004.  
  1005. # Manage failure routing cases
  1006. failure_route[MANAGE_FAILURE] {
  1007. route(NATMANAGE);
  1008.  
  1009. if (t_is_canceled()) {
  1010. exit;
  1011. }
  1012.  
  1013.  
  1014. #!ifdef WITH_LOADBALANCE
  1015. xlog("L_NOTICE", "Media server $du failed to answer for $ru, selecting other one!");
  1016. # next DST - only for 500 reply or local timeout (set by t_set_fr())
  1017. if (t_check_status("500") || t_branch_timeout() || !t_branch_replied())
  1018. {
  1019. #we mark the destination Inactive and Probing
  1020. # xlog("L_NOTICE", "Marking <$du> as Inactive and Probing\n");
  1021. # ds_mark_dst("ip");
  1022. #select the new destination
  1023. if(ds_next_dst())
  1024. {
  1025. #again set local timeout for reply
  1026. t_set_fr(0,2000);
  1027. t_on_failure("MANAGE_FAILURE");
  1028. route(RELAY);
  1029. exit;
  1030. }
  1031. else
  1032. {
  1033. #last available node failed to reply, no other destinations available
  1034. xlog("L_NOTICE", "No more destinations - either blocked DID or really all gws down: <$du> | <$ru>\n");
  1035. send_reply("404", "No destination");
  1036. exit;
  1037. }
  1038. }
  1039. #!endif
  1040.  
  1041.  
  1042. #!ifdef WITH_BLOCK3XX
  1043. # block call redirect based on 3xx replies.
  1044. if (t_check_status("3[0-9][0-9]")) {
  1045. t_reply("404","Not found");
  1046. exit;
  1047. }
  1048. #!endif
  1049.  
  1050. #!ifdef WITH_VOICEMAIL
  1051. # serial forking
  1052. # - route to voicemail on busy or no answer (timeout)
  1053. if (t_check_status("486|408")) {
  1054. $du = $null;
  1055. route(TOVOICEMAIL);
  1056. exit;
  1057. }
  1058. #!endif
  1059. }
  1060.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement