Advertisement
Guest User

Untitled

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