Advertisement
archit_sinha

Untitled

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