Guest User

Untitled

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