Advertisement
Guest User

Kamailio Configuration File

a guest
Jun 17th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.17 KB | None | 0 0
  1. #!KAMAILIO
  2.  
  3. #!define WITH_MYSQL
  4. #!define WITH_AUTH
  5. #!define WITH_USRLOCDB
  6. #!define WITH_ASTERISK
  7. #!define WITH_NAT
  8.  
  9.  
  10.  
  11. #
  12. # Kamailio (OpenSER) SIP Server v4.0 - default configuration script
  13. # - web: http://www.kamailio.org
  14. # - git: http://sip-router.org
  15. #
  16. # Direct your questions about this file to: <sr-users@lists.sip-router.org>
  17. #
  18. # Refer to the Core CookBook at http://www.kamailio.org/wiki/
  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. ####### Include Local Config If Exists #########
  112. import_file "kamailio-local.cfg"
  113.  
  114. ####### Defined Values #########
  115.  
  116. # *** Value defines - IDs used later in config
  117. #!ifdef WITH_MYSQL
  118. # - database URL - used to connect to database server by modules such
  119. # as: auth_db, acc, usrloc, a.s.o.
  120. #!ifndef DBURL
  121. #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
  122. #!ifdef WITH_ASTERISK
  123. #!define DBASTURL "mysql://chandra:test123456@10.0.0.253:3336/mydatabase"
  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="sip.mydomain.com"
  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. #listen=udp:10.0.0.10:5060
  176.  
  177. # .... Below line is added.
  178.  
  179. listen=udp:10.0.0.122:5060 advertise 54.85.55.69:5060
  180.  
  181. /* port to listen to
  182. * - can be specified more than once if needed to listen on many ports */
  183. port=5060
  184.  
  185. mhomed=1
  186.  
  187. #!ifdef WITH_TLS
  188. enable_tls=yes
  189. #!endif
  190.  
  191. # life time of TCP connection when there is no traffic
  192. # - a bit higher than registration expires to cope with UA behind NAT
  193. tcp_connection_lifetime=3605
  194.  
  195. ####### Custom Parameters #########
  196.  
  197. # These parameters can be modified runtime via RPC interface
  198. # - see the documentation of 'cfg_rpc' module.
  199. #
  200. # Format: group.id = value 'desc' description
  201. # Access: $sel(cfg_get.group.id) or @cfg_get.group.id
  202. #
  203.  
  204. #!ifdef WITH_PSTN
  205. # PSTN GW Routing
  206. #
  207. # - pstn.gw_ip: valid IP or hostname as string value, example:
  208. # pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
  209. #
  210. # - by default is empty to avoid misrouting
  211. pstn.gw_ip = "" desc "PSTN GW Address"
  212. pstn.gw_port = "" desc "PSTN GW Port"
  213. #!endif
  214.  
  215. #!ifdef WITH_VOICEMAIL
  216. # VoiceMail Routing on offline, busy or no answer
  217. #
  218. # - by default Voicemail server IP is empty to avoid misrouting
  219. voicemail.srv_ip = "" desc "VoiceMail IP Address"
  220. voicemail.srv_port = "5060" desc "VoiceMail Port"
  221. #!endif
  222.  
  223. #!ifdef WITH_ASTERISK
  224. asterisk.bindip = "10.0.0.107" desc "Asterisk IP Address"
  225. asterisk.bindport = "5060" desc "Asterisk Port"
  226. kamailio.bindip = "10.0.0.122" desc "Kamailio IP Address"
  227. kamailio.bindport = "5060" desc "Kamailio Port"
  228. #!endif
  229.  
  230. ####### Modules Section ########
  231.  
  232. # set paths to location of modules (to sources or installation folders)
  233. #!ifdef WITH_SRCPATH
  234. mpath="modules_k:modules"
  235. #!else
  236. mpath="/usr/local/lib/kamailio/modules_k/:/usr/local/lib64/kamailio/modules/"
  237. #!endif
  238.  
  239. #!ifdef WITH_MYSQL
  240. loadmodule "db_mysql.so"
  241. #!endif
  242.  
  243. loadmodule "mi_fifo.so"
  244. loadmodule "kex.so"
  245. loadmodule "corex.so"
  246. loadmodule "tm.so"
  247. loadmodule "tmx.so"
  248. loadmodule "sl.so"
  249. loadmodule "rr.so"
  250. loadmodule "pv.so"
  251. loadmodule "maxfwd.so"
  252. loadmodule "usrloc.so"
  253. loadmodule "registrar.so"
  254. loadmodule "textops.so"
  255. loadmodule "siputils.so"
  256. loadmodule "xlog.so"
  257. loadmodule "sanity.so"
  258. loadmodule "ctl.so"
  259. loadmodule "cfg_rpc.so"
  260. loadmodule "mi_rpc.so"
  261. loadmodule "acc.so"
  262. loadmodule "dispatcher.so"
  263. loadmodule "ipops.so"
  264. loadmodule "sqlops.so"
  265.  
  266.  
  267. #!ifdef WITH_AUTH
  268. loadmodule "auth.so"
  269. loadmodule "auth_db.so"
  270. #!ifdef WITH_IPAUTH
  271. loadmodule "permissions.so"
  272. #!endif
  273. #!endif
  274.  
  275. #!ifdef WITH_ALIASDB
  276. loadmodule "alias_db.so"
  277. #!endif
  278.  
  279. #!ifdef WITH_SPEEDDIAL
  280. loadmodule "speeddial.so"
  281. #!endif
  282.  
  283. #!ifdef WITH_MULTIDOMAIN
  284. loadmodule "domain.so"
  285. #!endif
  286.  
  287. #!ifdef WITH_PRESENCE
  288. loadmodule "presence.so"
  289. loadmodule "presence_xml.so"
  290. #!endif
  291.  
  292. #!ifdef WITH_NAT
  293. loadmodule "nathelper.so"
  294. loadmodule "rtpproxy.so"
  295. #!endif
  296.  
  297. #!ifdef WITH_TLS
  298. loadmodule "tls.so"
  299. #!endif
  300.  
  301. #!ifdef WITH_ANTIFLOOD
  302. loadmodule "htable.so"
  303. loadmodule "pike.so"
  304. #!endif
  305.  
  306. #!ifdef WITH_XMLRPC
  307. loadmodule "xmlrpc.so"
  308. #!endif
  309.  
  310. #!ifdef WITH_DEBUG
  311. loadmodule "debugger.so"
  312. #!endif
  313.  
  314.  
  315. #!ifdef WITH_ASTERISK
  316. loadmodule "uac.so"
  317. #!endif
  318.  
  319.  
  320. # ----------------- setting module-specific parameters ---------------
  321.  
  322. # ------- Load-balancer params ------
  323. modparam("dispatcher", "db_url","mysql://kamailio:kamailiorw@localhost/kamailio")
  324. modparam("dispatcher", "table_name", "dispatcher")
  325. modparam("dispatcher", "setid_col", "setid")
  326. modparam("dispatcher", "destination_col", "destination")
  327. modparam("dispatcher", "force_dst", 1)
  328. modparam("dispatcher", "flags", 3)
  329. modparam("dispatcher", "dst_avp", "$avp(i:271)")
  330. modparam("dispatcher", "grp_avp", "$avp(i:272)")
  331. modparam("dispatcher", "cnt_avp", "$avp(i:273)")
  332. modparam("dispatcher", "ds_ping_from", "sip:proxy@10.0.0.122")
  333. modparam("dispatcher", "ds_ping_interval",15)
  334. modparam("dispatcher", "ds_probing_mode", 1)
  335. modparam("dispatcher", "ds_ping_reply_codes", "class=2;code=403;code=404;code=484;class=3")
  336.  
  337.  
  338. modparam("sqlops","sqlcon","ca=>mysql://kamailio:kamailiorw@localhost/kamailio")
  339.  
  340.  
  341. # ----- mi_fifo params -----
  342. modparam("mi_fifo", "fifo_name", "/tmp/kamailio_fifo")
  343.  
  344.  
  345. # ----- tm params -----
  346. # auto-discard branches from previous serial forking leg
  347. modparam("tm", "failure_reply_mode", 3)
  348. # default retransmission timeout: 30sec
  349. modparam("tm", "fr_timer", 30000)
  350. # default invite retransmission timeout after 1xx: 120sec
  351. modparam("tm", "fr_inv_timer", 120000)
  352.  
  353.  
  354. # ----- rr params -----
  355. # add value to ;lr param to cope with most of the UAs
  356. modparam("rr", "enable_full_lr", 1)
  357. # do not append from tag to the RR (no need for this script)
  358. #!ifdef WITH_ASTERISK
  359. modparam("rr", "append_fromtag", 1)
  360. #!else
  361. modparam("rr", "append_fromtag", 0)
  362. #!endif
  363.  
  364.  
  365. #modparam("rr", "append_fromtag", 0) - Commented out this line and added above WITH_ASTERISK Block.
  366.  
  367.  
  368.  
  369.  
  370.  
  371. # ----- registrar params -----
  372. modparam("registrar", "method_filtering", 1)
  373. /* uncomment the next line to disable parallel forking via location */
  374. # modparam("registrar", "append_branches", 0)
  375. /* uncomment the next line not to allow more than 10 contacts per AOR */
  376. #modparam("registrar", "max_contacts", 10)
  377. # max value for expires of registrations
  378. modparam("registrar", "max_expires", 3600)
  379. # set it to 1 to enable GRUU
  380. modparam("registrar", "gruu_enabled", 0)
  381.  
  382.  
  383. # ----- acc params -----
  384. /* what special events should be accounted ? */
  385. modparam("acc", "early_media", 0)
  386. modparam("acc", "report_ack", 0)
  387. modparam("acc", "report_cancels", 0)
  388. /* by default ww do not adjust the direct of the sequential requests.
  389. if you enable this parameter, be sure the enable "append_fromtag"
  390. in "rr" module */
  391. modparam("acc", "detect_direction", 0)
  392. /* account triggers (flags) */
  393. modparam("acc", "log_flag", FLT_ACC)
  394. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  395. modparam("acc", "log_extra",
  396. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  397. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  398. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  399. /* enhanced DB accounting */
  400. #!ifdef WITH_ACCDB
  401. modparam("acc", "db_flag", FLT_ACC)
  402. modparam("acc", "db_missed_flag", FLT_ACCMISSED)
  403. modparam("acc", "db_url", DBURL)
  404. modparam("acc", "db_extra",
  405. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  406. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  407. #!endif
  408.  
  409.  
  410. # ----- usrloc params -----
  411. /* enable DB persistency for location entries */
  412. #!ifdef WITH_USRLOCDB
  413. modparam("usrloc", "db_url", DBURL)
  414. modparam("usrloc", "db_mode", 2)
  415. modparam("usrloc", "use_domain", MULTIDOMAIN)
  416. #!endif
  417.  
  418.  
  419. # ----- auth_db params -----
  420.  
  421. ##!ifdef WITH_AUTH ----- Default block comes with Config file and added below Asterisk block.
  422. #modparam("auth_db", "db_url", DBURL)
  423. #modparam("auth_db", "calculate_ha1", yes)
  424. #modparam("auth_db", "password_column", "password")
  425. #modparam("auth_db", "load_credentials", "")
  426. #modparam("auth_db", "use_domain", MULTIDOMAIN)
  427.  
  428.  
  429. #!ifdef WITH_AUTH
  430. modparam("auth_db", "calculate_ha1", yes)
  431. modparam("auth_db", "load_credentials", "")
  432.  
  433. #!ifdef WITH_ASTERISK
  434. modparam("auth_db", "user_column", "name")
  435. modparam("auth_db", "password_column", "sippasswd")
  436. modparam("auth_db", "db_url", DBASTURL)
  437. modparam("auth_db", "version_table", 0)
  438. #!else
  439. modparam("auth_db", "db_url", DBURL)
  440. modparam("auth_db", "password_column", "password")
  441. modparam("auth_db", "use_domain", MULTIDOMAIN)
  442. #!endif
  443.  
  444. # ----- permissions params -----
  445. #!ifdef WITH_IPAUTH
  446. modparam("permissions", "db_url", DBURL)
  447. modparam("permissions", "db_mode", 1)
  448. #!endif
  449.  
  450. #!endif
  451.  
  452.  
  453. # ----- alias_db params -----
  454. #!ifdef WITH_ALIASDB
  455. modparam("alias_db", "db_url", DBURL)
  456. modparam("alias_db", "use_domain", MULTIDOMAIN)
  457. #!endif
  458.  
  459.  
  460. # ----- speeddial params -----
  461. #!ifdef WITH_SPEEDDIAL
  462. modparam("speeddial", "db_url", DBURL)
  463. modparam("speeddial", "use_domain", MULTIDOMAIN)
  464. #!endif
  465.  
  466.  
  467. # ----- domain params -----
  468. #!ifdef WITH_MULTIDOMAIN
  469. modparam("domain", "db_url", DBURL)
  470. # register callback to match myself condition with domains list
  471. modparam("domain", "register_myself", 1)
  472. #!endif
  473.  
  474.  
  475. #!ifdef WITH_PRESENCE
  476. # ----- presence params -----
  477. modparam("presence", "db_url", DBURL)
  478.  
  479. # ----- presence_xml params -----
  480. modparam("presence_xml", "db_url", DBURL)
  481. modparam("presence_xml", "force_active", 1)
  482. #!endif
  483.  
  484.  
  485. #!ifdef WITH_NAT
  486. # ----- rtpproxy params -----
  487. modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
  488.  
  489. # ----- nathelper params -----
  490. modparam("nathelper", "natping_interval", 30)
  491. modparam("nathelper", "ping_nated_only", 1)
  492. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  493. modparam("nathelper", "sipping_from", "sip:pinger@kamailio.org")
  494.  
  495. # params needed for NAT traversal in other modules
  496. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  497. modparam("usrloc", "nat_bflag", FLB_NATB)
  498. #!endif
  499.  
  500.  
  501. #!ifdef WITH_TLS
  502. # ----- tls params -----
  503. modparam("tls", "config", "/usr/local/etc/kamailio/tls.cfg")
  504. #!endif
  505.  
  506. #!ifdef WITH_ANTIFLOOD
  507. # ----- pike params -----
  508. modparam("pike", "sampling_time_unit", 2)
  509. modparam("pike", "reqs_density_per_unit", 16)
  510. modparam("pike", "remove_latency", 4)
  511.  
  512. # ----- htable params -----
  513. # ip ban htable with autoexpire after 5 minutes
  514. modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
  515. #!endif
  516.  
  517. #!ifdef WITH_XMLRPC
  518. # ----- xmlrpc params -----
  519. modparam("xmlrpc", "route", "XMLRPC");
  520. modparam("xmlrpc", "url_match", "^/RPC")
  521. #!endif
  522.  
  523. #!ifdef WITH_DEBUG
  524. # ----- debugger params -----
  525. modparam("debugger", "cfgtrace", 1)
  526. #!endif
  527.  
  528.  
  529. ####### Routing Logic ########
  530.  
  531.  
  532. # Main SIP request routing logic
  533. # - processing of any incoming SIP request starts with this route
  534. # - note: this is the same as route { ... }
  535. request_route {
  536.  
  537. # per request initial checks
  538. route(REQINIT);
  539.  
  540. # NAT detection
  541. route(NATDETECT);
  542.  
  543. # CANCEL processing
  544. if (is_method("CANCEL"))
  545. {
  546. if (t_check_trans()) {
  547. route(RELAY);
  548. }
  549. exit;
  550. }
  551.  
  552. # handle requests within SIP dialogs
  553. route(WITHINDLG);
  554.  
  555. ### only initial requests (no To tag)
  556.  
  557. t_check_trans();
  558.  
  559. # authentication
  560. route(AUTH);
  561.  
  562. # record routing for dialog forming requests (in case they are routed)
  563. # - remove preloaded route headers
  564. remove_hf("Route");
  565. if (is_method("INVITE|SUBSCRIBE"))
  566. record_route();
  567.  
  568. # account only INVITEs
  569. if (is_method("INVITE"))
  570. {
  571. setflag(FLT_ACC); # do accounting
  572. }
  573.  
  574. # dispatch requests to foreign domains
  575. route(SIPOUT);
  576.  
  577. ### requests for my local domains
  578.  
  579. # handle presence related requests
  580. route(PRESENCE);
  581.  
  582. # handle registrations
  583. route(REGISTRAR);
  584.  
  585. if ($rU==$null)
  586. {
  587. # request with no Username in RURI
  588. sl_send_reply("484","Address Incomplete");
  589. exit;
  590. }
  591.  
  592. # dispatch destinations to PSTN
  593. route(PSTN);
  594.  
  595. # user location service
  596. route(LOCATION);
  597. }
  598.  
  599.  
  600. route[RELAY] {
  601.  
  602. # enable additional event routes for forwarded requests
  603. # - serial forking, RTP relaying handling, a.s.o.
  604. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  605. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  606. }
  607. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  608. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  609. }
  610. if (is_method("INVITE")) {
  611. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  612. }
  613.  
  614. if (!t_relay()) {
  615. sl_reply_error();
  616. }
  617. exit;
  618. }
  619.  
  620. # Per SIP request initial checks
  621. route[REQINIT] {
  622. #!ifdef WITH_ANTIFLOOD
  623. # flood dection from same IP and traffic ban for a while
  624. # be sure you exclude checking trusted peers, such as pstn gateways
  625. # - local host excluded (e.g., loop to self)
  626. if(src_ip!=myself)
  627. {
  628. if($sht(ipban=>$si)!=$null)
  629. {
  630. # ip is already blocked
  631. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  632. exit;
  633. }
  634. if (!pike_check_req())
  635. {
  636. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  637. $sht(ipban=>$si) = 1;
  638. exit;
  639. }
  640. }
  641. #!endif
  642.  
  643. if (!mf_process_maxfwd_header("10")) {
  644. sl_send_reply("483","Too Many Hops");
  645. exit;
  646. }
  647.  
  648. if(!sanity_check("1511", "7"))
  649. {
  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()) {
  658. # sequential request withing a dialog should
  659. # take the path determined by record-routing
  660. if (loose_route()) {
  661. route(DLGURI);
  662. if (is_method("BYE")) {
  663. setflag(FLT_ACC); # do accounting ...
  664. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  665. }
  666. else if ( is_method("ACK") ) {
  667. # ACK is forwarded statelessy
  668. route(NATMANAGE);
  669. }
  670. else if ( is_method("NOTIFY") ) {
  671. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  672. record_route();
  673. }
  674. route(RELAY);
  675. } else {
  676. if (is_method("SUBSCRIBE") && uri == myself) {
  677. # in-dialog subscribe requests
  678. route(PRESENCE);
  679. exit;
  680. }
  681. if ( is_method("ACK") ) {
  682. if ( t_check_trans() ) {
  683. # no loose-route, but stateful ACK;
  684. # must be an ACK after a 487
  685. # or e.g. 404 from upstream server
  686. route(RELAY);
  687. exit;
  688. } else {
  689. # ACK without matching transaction ... ignore and discard
  690. exit;
  691. }
  692. }
  693. sl_send_reply("404","Not here");
  694. }
  695. exit;
  696. }
  697. }
  698.  
  699. # Handle SIP registrations
  700.  
  701. #route[REGISTRAR] { --- Default block comes with config file and added below block.
  702. # if (is_method("REGISTER"))
  703. # {
  704. # if(isflagset(FLT_NATS))
  705. # {
  706. # setbflag(FLB_NATB);
  707. # # uncomment next line to do SIP NAT pinging
  708. # ## setbflag(FLB_NATSIPPING);
  709. # }
  710. # if (!save("location"))
  711. # sl_reply_error();
  712. #
  713. # exit;
  714. # }
  715. #}
  716.  
  717. route[REGISTRAR] {
  718. if (is_method("REGISTER"))
  719. {
  720. if(isflagset(FLT_NATS))
  721. {
  722. setbflag(FLB_NATB);
  723. # uncomment next line to do SIP NAT pinging
  724. ## setbflag(FLB_NATSIPPING);
  725. }
  726. if (!save("location"))
  727. sl_reply_error();
  728.  
  729. #!ifdef WITH_ASTERISK
  730. route(REGFWD);
  731. #!endif
  732.  
  733. exit;
  734. }
  735. }
  736.  
  737.  
  738. # USER location service
  739. route[LOCATION] {
  740.  
  741. #!ifdef WITH_SPEEDDIAL
  742. # search for short dialing - 2-digit extension
  743. if($rU=~"^[0-9][0-9]$")
  744. if(sd_lookup("speed_dial"))
  745. route(SIPOUT);
  746. #!endif
  747.  
  748. #!ifdef WITH_ALIASDB
  749. # search in DB-based aliases
  750. if(alias_db_lookup("dbaliases"))
  751. route(SIPOUT);
  752. #!endif
  753.  
  754. # ---- Added the below WITH_ASTERISK Block.
  755.  
  756. #!ifdef WITH_ASTERISK
  757. if(is_method("INVITE") && (!route(FROMASTERISK))) {
  758. # if new call from out there - send to Asterisk
  759. # - non-INVITE request are routed directly by Kamailio
  760. # - traffic from Asterisk is routed also directy by Kamailio
  761. route(TOASTERISK);
  762. exit;
  763. }
  764. #!endif
  765.  
  766.  
  767.  
  768. $avp(oexten) = $rU;
  769. if (!lookup("location")) {
  770. $var(rc) = $rc;
  771. route(TOVOICEMAIL);
  772. t_newtran();
  773. switch ($var(rc)) {
  774. case -1:
  775. case -3:
  776. send_reply("404", "Not Found");
  777. exit;
  778. case -2:
  779. send_reply("405", "Method Not Allowed");
  780. exit;
  781. }
  782. }
  783.  
  784. # when routing via usrloc, log the missed calls also
  785. if (is_method("INVITE"))
  786. {
  787. setflag(FLT_ACCMISSED);
  788. }
  789.  
  790. route(RELAY);
  791. exit;
  792. }
  793.  
  794. # Presence server route
  795. route[PRESENCE] {
  796. if(!is_method("PUBLISH|SUBSCRIBE"))
  797. return;
  798.  
  799. #!ifdef WITH_PRESENCE
  800. if (!t_newtran())
  801. {
  802. sl_reply_error();
  803. exit;
  804. };
  805.  
  806. if(is_method("PUBLISH"))
  807. {
  808. handle_publish();
  809. t_release();
  810. }
  811. else
  812. if( is_method("SUBSCRIBE"))
  813. {
  814. handle_subscribe();
  815. t_release();
  816. }
  817. exit;
  818. #!endif
  819.  
  820. # if presence enabled, this part will not be executed
  821. if (is_method("PUBLISH") || $rU==$null)
  822. {
  823. sl_send_reply("404", "Not here");
  824. exit;
  825. }
  826. return;
  827. }
  828.  
  829. # Authentication route
  830. route[AUTH] {
  831. #!ifdef WITH_AUTH
  832.  
  833. # --- Added the below WITH_ASTERISK block.
  834.  
  835. #!ifdef WITH_ASTERISK
  836. # do not auth traffic from Asterisk - trusted!
  837. if(route(FROMASTERISK))
  838. return;
  839. #!endif
  840.  
  841.  
  842.  
  843.  
  844.  
  845. #!ifdef WITH_IPAUTH
  846. if((!is_method("REGISTER")) && allow_source_address())
  847. {
  848. # source IP allowed
  849. return;
  850. }
  851. #!endif
  852.  
  853.  
  854. # ... Commented default block comes with config file and added Below block.
  855.  
  856.  
  857. # if (is_method("REGISTER") || from_uri==myself)
  858. # {
  859. # # authenticate requests
  860. # if (!auth_check("$fd", "subscriber", "1")) {
  861. # auth_challenge("$fd", "0");
  862. # exit;
  863. # }
  864.  
  865. if (is_method("REGISTER") || from_uri==myself)
  866. {
  867. # authenticate requests
  868. #!ifdef WITH_ASTERISK
  869. if (!auth_check("$fd", "mysipusers", "1")) {
  870. #!else
  871. if (!auth_check("$fd", "subscriber", "1")) {
  872. #!endif
  873. auth_challenge("$fd", "0");
  874. exit;
  875. }
  876.  
  877.  
  878. # user authenticated - remove auth header
  879. if(!is_method("REGISTER|PUBLISH"))
  880. consume_credentials();
  881. }
  882. # if caller is not local subscriber, then check if it calls
  883. # a local destination, otherwise deny, not an open relay here
  884. if (from_uri!=myself && uri!=myself)
  885. {
  886. sl_send_reply("403","Not relaying");
  887. exit;
  888. }
  889.  
  890. #!endif
  891. return;
  892. }
  893.  
  894. # Caller NAT detection route
  895. route[NATDETECT] {
  896. #!ifdef WITH_NAT
  897. force_rport();
  898. if (nat_uac_test("19")) {
  899. if (is_method("REGISTER")) {
  900. fix_nated_register();
  901. } else {
  902. add_contact_alias();
  903. }
  904. setflag(FLT_NATS);
  905. }
  906. #!endif
  907. return;
  908. }
  909.  
  910. # RTPProxy control
  911. route[NATMANAGE] {
  912. #!ifdef WITH_NAT
  913. if (is_request()) {
  914. if(has_totag()) {
  915. if(check_route_param("nat=yes")) {
  916. setbflag(FLB_NATB);
  917. }
  918. }
  919. }
  920. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
  921. return;
  922.  
  923. # ... Added below line. - DOUBT HERE
  924. # route(RTPPROXY);
  925.  
  926. # rtpproxy_manage("co"); -- Commented to avoid calling twice
  927.  
  928. if (is_request()) {
  929. if (!has_totag()) {
  930. if(t_is_branch_route()) {
  931. add_rr_param(";nat=yes");
  932. }
  933. }
  934. }
  935. if (is_reply()) {
  936. if(isbflagset(FLB_NATB)) {
  937. add_contact_alias();
  938. #fix_nated_contact();
  939. }
  940. }
  941. #!endif
  942. return;
  943. }
  944.  
  945. # --- Added below block
  946.  
  947. route[RTPPROXY] {
  948. if (is_method("INVITE")){
  949. sql_query("ca", "select destination from dispatcher where destination like '%$dd%'","ra");
  950. if($dbr(ra=>rows)>0){
  951. $avp(duip)=$(du{s.select,-2,:});
  952. if (is_ip_rfc1918("$avp(duip)")) {
  953. xlog("L_INFO", "Call is going to private IPv4 Media Server Engage RTPProxy Now\n");
  954. #rtpproxy_manage("crwie","10.0.0.122");
  955. rtpproxy_manage("rwie");
  956. }
  957.  
  958. }
  959. else if(ds_is_from_list()){
  960. if (is_ip_rfc1918("$si")) {
  961. xlog("L_INFO", " Call is coming from a private IPv4 Media Server Engage RTPProxy Now\n");
  962. #rtpproxy_manage("crwei","54.85.55.69");
  963. rtpproxy_manage("rwei");
  964.  
  965. }
  966. }else if(!ds_is_from_list()){
  967.  
  968. rtpproxy_manage("rwie");
  969.  
  970. }
  971. }
  972. }
  973.  
  974.  
  975.  
  976.  
  977.  
  978. # URI update for dialog requests
  979. route[DLGURI] {
  980. #!ifdef WITH_NAT
  981. if(!isdsturiset()) {
  982. handle_ruri_alias();
  983. }
  984. #!endif
  985. return;
  986. }
  987.  
  988. # Routing to foreign domains
  989. route[SIPOUT] {
  990. if (!uri==myself)
  991. {
  992. append_hf("P-hint: outbound\r\n");
  993. route(RELAY);
  994. }
  995. }
  996.  
  997. # PSTN GW routing
  998. route[PSTN] {
  999. #!ifdef WITH_PSTN
  1000. # check if PSTN GW IP is defined
  1001. if (strempty($sel(cfg_get.pstn.gw_ip))) {
  1002. xlog("SCRIPT: PSTN rotuing enabled but pstn.gw_ip not defined\n");
  1003. return;
  1004. }
  1005.  
  1006. # route to PSTN dialed numbers starting with '+' or '00'
  1007. # (international format)
  1008. # - update the condition to match your dialing rules for PSTN routing
  1009. if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
  1010. return;
  1011.  
  1012. # only local users allowed to call
  1013. if(from_uri!=myself) {
  1014. sl_send_reply("403", "Not Allowed");
  1015. exit;
  1016. }
  1017.  
  1018. if (strempty($sel(cfg_get.pstn.gw_port))) {
  1019. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
  1020. } else {
  1021. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip) + ":"
  1022. + $sel(cfg_get.pstn.gw_port);
  1023. }
  1024.  
  1025. route(RELAY);
  1026. exit;
  1027. #!endif
  1028.  
  1029. return;
  1030. }
  1031.  
  1032. # XMLRPC routing
  1033. #!ifdef WITH_XMLRPC
  1034. route[XMLRPC] {
  1035. # allow XMLRPC from localhost
  1036. if ((method=="POST" || method=="GET")
  1037. && (src_ip==127.0.0.1)) {
  1038. # close connection only for xmlrpclib user agents (there is a bug in
  1039. # xmlrpclib: it waits for EOF before interpreting the response).
  1040. if ($hdr(User-Agent) =~ "xmlrpclib")
  1041. set_reply_close();
  1042. set_reply_no_connect();
  1043. dispatch_rpc();
  1044. exit;
  1045. }
  1046. send_reply("403", "Forbidden");
  1047. exit;
  1048. }
  1049. #!endif
  1050.  
  1051. # route to voicemail server
  1052. route[TOVOICEMAIL] {
  1053. #!ifdef WITH_VOICEMAIL
  1054. if(!is_method("INVITE"))
  1055. return;
  1056.  
  1057. # check if VoiceMail server IP is defined
  1058. if (strempty($sel(cfg_get.voicemail.srv_ip))) {
  1059. xlog("SCRIPT: VoiceMail rotuing enabled but IP not defined\n");
  1060. return;
  1061. }
  1062. if($avp(oexten)==$null)
  1063. return;
  1064.  
  1065. $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
  1066. + ":" + $sel(cfg_get.voicemail.srv_port);
  1067. route(RELAY);
  1068. exit;
  1069. #!endif
  1070.  
  1071. return;
  1072. }
  1073.  
  1074. # manage outgoing branches
  1075. branch_route[MANAGE_BRANCH] {
  1076. xdbg("new branch [$T_branch_idx] to $ru\n");
  1077. route(NATMANAGE);
  1078. }
  1079.  
  1080. # manage incoming replies
  1081. onreply_route[MANAGE_REPLY] {
  1082. xdbg("incoming reply\n");
  1083. if(status=~"[12][0-9][0-9]")
  1084. route(NATMANAGE);
  1085. }
  1086.  
  1087. # manage failure routing cases
  1088. failure_route[MANAGE_FAILURE] {
  1089. route(NATMANAGE);
  1090.  
  1091. if (t_is_canceled()) {
  1092. exit;
  1093. }
  1094.  
  1095. #!ifdef WITH_BLOCK3XX
  1096. # block call redirect based on 3xx replies.
  1097. if (t_check_status("3[0-9][0-9]")) {
  1098. t_reply("404","Not found");
  1099. exit;
  1100. }
  1101. #!endif
  1102.  
  1103. #!ifdef WITH_VOICEMAIL
  1104. # serial forking
  1105. # - route to voicemail on busy or no answer (timeout)
  1106. if (t_check_status("486|408")) {
  1107. $du = $null;
  1108. route(TOVOICEMAIL);
  1109. exit;
  1110. }
  1111. #!endif
  1112. }
  1113.  
  1114. # ... Added below block.
  1115.  
  1116. #!ifdef WITH_ASTERISK
  1117. # Test if coming from Asterisk
  1118.  
  1119. #route[FROMASTERISK] {
  1120. # if($si==$sel(cfg_get.asterisk.bindip)
  1121. # && $sp==$sel(cfg_get.asterisk.bindport))
  1122. # return 1;
  1123. # return -1;
  1124. #}
  1125.  
  1126. # --- Commented about route[FROMASTERISK] block and added below block.
  1127.  
  1128. route[FROMASTERISK] {
  1129. if(ds_is_from_list()){
  1130. rtpproxy_manage("cawie");
  1131. xlog("L_INFO","[$fU@$si:$sp]{$rm} Call from Media-Server Cluster\n");
  1132. return 1;
  1133. }
  1134. return -1;
  1135. }
  1136.  
  1137.  
  1138.  
  1139. # Send to Asterisk
  1140.  
  1141. #route[TOASTERISK] {
  1142. # $du = "sip:" + $sel(cfg_get.asterisk.bindip) + ":"
  1143. # + $sel(cfg_get.asterisk.bindport);
  1144. # route(RELAY);
  1145. # exit;
  1146. #}
  1147.  
  1148. # --- Commented about route[TOASTERISK] block and added below block.
  1149.  
  1150. route[TOASTERISK] {
  1151. ds_mark_dst("P");
  1152. if(!ds_select_dst("1", "4")) {
  1153. sl_send_reply("500", "Service Unavailable");
  1154. xlog("L_INFO","[$fU@$si:$sp]{$rm} No destinations available for $rd \n");
  1155. exit;
  1156. }
  1157.  
  1158. xlog("L_INFO","[$fU@$si:$sp]{$rm} From Outside World to Asterisk Box $du\n");
  1159. rtpproxy_manage("cawei");
  1160.  
  1161. route(RELAY);
  1162. exit;
  1163. }
  1164.  
  1165. # Forward REGISTER to Asterisk
  1166. route[REGFWD] {
  1167. if(!is_method("REGISTER"))
  1168. {
  1169. return;
  1170. }
  1171. $var(rip) = $sel(cfg_get.asterisk.bindip);
  1172. $uac_req(method)="REGISTER";
  1173. $uac_req(ruri)="sip:" + $var(rip) + ":" + $sel(cfg_get.asterisk.bindport);
  1174. $uac_req(furi)="sip:" + $au + "@" + $var(rip);
  1175. $uac_req(turi)="sip:" + $au + "@" + $var(rip);
  1176. $uac_req(hdrs)="Contact: <sip:" + $au + "@"
  1177. + $sel(cfg_get.kamailio.bindip)
  1178. + ":" + $sel(cfg_get.kamailio.bindport) + ">\r\n";
  1179. if($sel(contact.expires) != $null)
  1180. $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $sel(contact.expires) + "\r\n";
  1181. else
  1182. $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $hdr(Expires) + "\r\n";
  1183. uac_req_send();
  1184. }
  1185. #!endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement