Advertisement
Guest User

Untitled

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