kamailio-basic-kemi-jsdt.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Kamailio - equivalent of routing blocks in JavaScript
  2. //
  3. // KSR - the new dynamic object exporting Kamailio functions
  4. // sr - the old static object exporting Kamailio functions
  5. //
  6. // global variables corresponding to defined values (e.g., flags) in kamailio.cfg
  7. var FLT_ACC=1
  8. var FLT_ACCMISSED=2
  9. var FLT_ACCFAILED=3
  10. var FLT_NATS=5
  11. FLB_NATB=6
  12. FLB_NATSIPPING=7
  13. // SIP request routing
  14. // equivalent of request_route{}
  15. function ksr_request_route()
  16. {
  17. var METHOD = KSR.pv.get("$rm");
  18. // KSR.sl.sl_send_reply(100,"Intelligent trying");
  19. // KSR.info("===== request - from kamailio lua script\n");
  20. // per request initial checks
  21. ksr_route_reqinit();
  22. // NAT detection
  23. ksr_route_natdetect();
  24. // CANCEL processing
  25. if (METHOD == "CANCEL") {
  26. if(KSR.tm.t_check_trans()>0) {
  27. ksr_route_relay();
  28. }
  29. return;
  30. }
  31. // handle requests within SIP dialogs
  32. ksr_route_withindlg();
  33. // -- only initial requests (no To tag)
  34. // handle retransmissions
  35. if (KSR.tmx.t_precheck_trans()>0) {
  36. KSR.tm.t_check_trans();
  37. return;
  38. }
  39. if (KSR.tm.t_check_trans()==0) { return; }
  40. // authentication
  41. ksr_route_auth();
  42. // record routing for dialog forming requests (in case they are routed)
  43. // - remove preloaded route headers
  44. KSR.hdr.remove("Route");
  45. if (METHOD=="INVITE" || METHOD=="SUBSCRIBE") {
  46. KSR.rr.record_route();
  47. }
  48. // account only INVITEs
  49. if (METHOD=="INVITE") {
  50. KSR.setflag(FLT_ACC); // do accounting
  51. }
  52. // dispatch requests to foreign domains
  53. ksr_route_sipout();
  54. // -- requests for my local domains
  55. // handle registrations
  56. ksr_route_registrar();
  57. if (KSR.pv.is_null("$rU")) {
  58. // request with no Username in RURI
  59. KSR.sl.sl_send_reply(484, "Address Incomplete");
  60. return;
  61. }
  62. // user location service
  63. ksr_route_location();
  64. return;
  65. }
  66. // wrapper around tm relay function
  67. function ksr_route_relay()
  68. {
  69. // enable additional event routes for forwarded requests
  70. // - serial forking, RTP relaying handling, a.s.o.
  71. var METHOD = KSR.pv.get("$rm");
  72. if (METHOD=="INVITE" || METHOD=="BYE" || METHOD=="SUBSCRIBE"
  73. || METHOD="UPDATE") {
  74. if (KSR.tm.t_is_set("branch_route")<0) {
  75. KSR.tm.t_on_branch("ksr_branch_manage");
  76. }
  77. }
  78. if (METHOD=="INVITE" || METHOD=="SUBSCRIBE" || METHOD=="UPDATE") {
  79. if (KSR.tm.t_is_set("onreply_route")<0) {
  80. KSR.tm.t_on_reply("ksr_onreply_manage");
  81. }
  82. }
  83. if (METHOD=="INVITE") {
  84. if (KSR.tm.t_is_set("failure_route")<0) {
  85. KSR.tm.t_on_failure("MANAGE_FAILURE");
  86. }
  87. }
  88. if (KSR.tm.t_relay()<0) {
  89. KSR.sl.sl_reply_error();
  90. }
  91. KSR.x.exit();
  92. }
  93. // Per SIP request initial checks
  94. function ksr_route_reqinit()
  95. {
  96. if (!KSR.is_myself(KSR.pv.get("$si"))) {
  97. if (!KSR.pv.is_null("$sht(ipban=>$si)")) {
  98. // ip is already blocked
  99. KSR.dbg("request from blocked IP - " + KSR.pv.get("$rm")
  100. + " from " + KSR.pv.get("$fu") + " (IP:"
  101. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + ")\n");
  102. KSR.x.exit();
  103. }
  104. if (KSR.pike.pike_check_req()<0) {
  105. KSR.err("ALERT: pike blocking " + KSR.pv.get("$rm")
  106. + " from " + KSR.pv.get("$fu") + " (IP:"
  107. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + ")\n");
  108. KSR.pv.seti("$sht(ipban=>$si)", 1);
  109. KSR.x.exit();
  110. }
  111. }
  112. if (!KSR.pv.is_null("$ua")) {
  113. var UA = KSR.pv.get("$ua");
  114. if (UA.indexOf("friendly-scanner")>=0 || UA.indexOf("sipcli")>=0) {
  115. KSR.sl.sl_send_reply(200, "OK");
  116. KSR.x.exit();
  117. }
  118. }
  119. if (KSR.maxfwd.process_maxfwd(10) < 0) {
  120. KSR.sl.sl_send_reply(483,"Too Many Hops");
  121. KSR.x.exit();
  122. }
  123. if (KSR.pv.get("$rm")=="OPTIONS"
  124. && KSR.is_myself(KSR.pv.get("$ru"))
  125. && KSR.pv.is_null("$rU")) {
  126. KSR.sl.sl_send_reply(200,"Keepalive");
  127. KSR.x.exit();
  128. }
  129. if (KSR.sanity.sanity_check(1511, 7)<0) {
  130. KSR.err("Malformed SIP message from "
  131. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + "\n");
  132. KSR.x.exit();
  133. }
  134. }
  135. // Handle requests within SIP dialogs
  136. function ksr_route_withindlg()
  137. {
  138. if (KSR.siputils.has_totag()<0) { return; }
  139. var METHOD = KSR.pv.get("$rm");
  140. // sequential request withing a dialog should
  141. // take the path determined by record-routing
  142. if (KSR.rr.loose_route()>0) {
  143. ksr_route_dlguri();
  144. if (METHOD=="BYE") {
  145. KSR.setflag(FLT_ACC); // do accounting ...
  146. KSR.setflag(FLT_ACCFAILED); // ... even if the transaction fails
  147. } else if (METHOD=="ACK") {
  148. // ACK is forwarded statelessy
  149. ksr_route_natmanage();
  150. } else if (METHOD=="NOTIFY") {
  151. // Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  152. KSR.rr.record_route();
  153. }
  154. ksr_route_relay();
  155. KSR.x.exit();
  156. }
  157. if (METHOD=="ACK") {
  158. if (KSR.tm.t_check_trans() >0) {
  159. // no loose-route, but stateful ACK;
  160. // must be an ACK after a 487
  161. // or e.g. 404 from upstream server
  162. ksr_route_relay();
  163. KSR.x.exit();
  164. } else {
  165. // ACK without matching transaction ... ignore and discard
  166. KSR.x.exit();
  167. }
  168. }
  169. KSR.sl.sl_send_reply(404, "Not here");
  170. KSR.x.exit();
  171. }
  172. // Handle SIP registrations
  173. function ksr_route_registrar()
  174. {
  175. if (KSR.pv.get("$rm")!="REGISTER") { return; }
  176. if (KSR.isflagset(FLT_NATS)) {
  177. KSR.setbflag(FLB_NATB);
  178. // do SIP NAT pinging
  179. KSR.setbflag(FLB_NATSIPPING);
  180. }
  181. if (KSR.registrar.save("location", 0)<0) {
  182. KSR.sl.sl_reply_error();
  183. }
  184. KSR.x.exit();
  185. }
  186. // User location service
  187. function ksr_route_location()
  188. {
  189. var rc = KSR.registrar.lookup("location");
  190. if (rc<0) {
  191. KSR.tm.t_newtran();
  192. if (rc==-1 || rc==-3) {
  193. KSR.sl.send_reply("404", "Not Found");
  194. KSR.x.exit();
  195. } else if (rc==-2) {
  196. KSR.sl.send_reply("405", "Method Not Allowed");
  197. KSR.x.exit();
  198. }
  199. }
  200. // when routing via usrloc, log the missed calls also
  201. if (KSR.pv.get("$rm")=="INVITE") {
  202. KSR.setflag(FLT_ACCMISSED);
  203. }
  204. ksr_route_relay();
  205. KSR.x.exit();
  206. }
  207. // IP authorization and user uthentication
  208. function ksr_route_auth()
  209. {
  210. var METHOD = KSR.pv.get("$rm");
  211. if (METHOD!="REGISTER") {
  212. if (KSR.permissions.allow_source_address(1)>0) {
  213. // source IP allowed
  214. return;
  215. }
  216. }
  217. if (METHOD=="REGISTER" || KSR.is_myself(KSR.pv.get("$fu"))) {
  218. // authenticate requests
  219. if (KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0) {
  220. KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0);
  221. KSR.x.exit();
  222. }
  223. // user authenticated - remove auth header
  224. if (METHOD!="REGISTER" && METHOD!="PUBLISH") {
  225. KSR.auth.consume_credentials();
  226. }
  227. }
  228. // if caller is not local subscriber, then check if it calls
  229. // a local destination, otherwise deny, not an open relay here
  230. if ((! KSR.is_myself(KSR.pv.get("$fu"))
  231. && (! KSR.is_myself(KSR.pv.get("$ru"))))) {
  232. KSR.sl.sl_send_reply(403,"Not relaying");
  233. KSR.x.exit();
  234. }
  235. return;
  236. }
  237. // Caller NAT detection
  238. function ksr_route_natdetect()
  239. {
  240. KSR.force_rport();
  241. if (KSR.nathelper.nat_uac_test(19)>0) {
  242. if (KSR.pv.get("$rm")=="REGISTER") {
  243. KSR.nathelper.fix_nated_register();
  244. } else if (KSR.siputils.is_first_hop()>0) {
  245. KSR.nathelper.set_contact_alias();
  246. }
  247. KSR.setflag(FLT_NATS);
  248. }
  249. return;
  250. }
  251. // RTPProxy control
  252. function ksr_route_natmanage()
  253. {
  254. if (KSR.siputils.is_request()>0) {
  255. if (KSR.siputils.has_totag()>0) {
  256. if (KSR.rr.check_route_param("nat=yes")>0) {
  257. KSR.setbflag(FLB_NATB);
  258. }
  259. }
  260. }
  261. if (! (KSR.isflagset(FLT_NATS) || KSR.isbflagset(FLB_NATB))) {
  262. return;
  263. }
  264. KSR.rtpproxy.rtpproxy_manage("co");
  265. if (KSR.siputils.is_request()>0) {
  266. if (! KSR.siputils.has_totag()) {
  267. if (KSR.tmx.t_is_branch_route()>0) {
  268. KSR.rr.add_rr_param(";nat=yes");
  269. }
  270. }
  271. }
  272. if (KSR.siputils.is_reply()>0) {
  273. if (KSR.isbflagset(FLB_NATB)) {
  274. KSR.nathelper.set_contact_alias();
  275. }
  276. }
  277. return;
  278. }
  279. // URI update for dialog requests
  280. function ksr_route_dlguri()
  281. {
  282. if (! KSR.isdsturiset()) {
  283. KSR.nathelper.handle_ruri_alias();
  284. }
  285. return;
  286. }
  287. // Routing to foreign domains
  288. function ksr_route_sipout()
  289. {
  290. if (KSR.is_myself(KSR.pv.get("$ru"))) { return; }
  291. KSR.hdr.append_hf("P-Hint: outbound\r\n");
  292. ksr_route_relay();
  293. KSR.xexit();;
  294. }
  295. // Manage outgoing branches
  296. // equivalent of branch_route[...]{}
  297. function ksr_branch_manage()
  298. {
  299. KSR.dbg("new branch [" + KSR.pv.get("$T_branch_idx]")
  300. + " to " + KSR.pv.get("$ru") + "\n");
  301. ksr_route_natmanage();
  302. return;
  303. }
  304. // Manage incoming replies
  305. // equivalent of onreply_route[...]{}
  306. function ksr_onreply_manage()
  307. {
  308. KSR.dbg("incoming reply\n");
  309. var scode = KSR.pv.get("$rs");
  310. if (scode>100 && scode<=299) {
  311. ksr_route_natmanage();
  312. }
  313. return;
  314. }
  315. // Manage failure routing cases
  316. // equivalent of failure_route[...]{}
  317. function ksr_failure_manage()
  318. {
  319. ksr_route_natmanage();
  320. if (KSR.tm.t_is_canceled()>0) {
  321. return;
  322. }
  323. return;
  324. }
  325. // SIP response handling
  326. // equivalent of reply_route{}
  327. function ksr_reply_route()
  328. {
  329. KSR.info("===== response - from kamailio JS script\n");
  330. return;
  331. }