load-net

kamailio-asterisk-dipatcher

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