kamailio-basic-kemi-jsdt.js 8.1 KB

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