SHARE
TWEET

Untitled

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