kamailio-oob.cfg 22 KB

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