Advertisement
FaisalKh

kamilio.cfg

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