kamailio-default-websocket.cfg 24 KB

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