kamailio-basic-kemi-jsdt.js 8.2 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. 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 retransmissions
  31. if (!KSR.is_ACK()) {
  32. if (KSR.tmx.t_precheck_trans()>0) {
  33. KSR.tm.t_check_trans();
  34. return;
  35. }
  36. if (KSR.tm.t_check_trans()==0) { return; }
  37. }
  38. // handle requests within SIP dialogs
  39. ksr_route_withindlg();
  40. // -- only initial requests (no To tag)
  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. var srcip = KSR.kx.get_srcip();
  97. if (KSR.htable.sht_match_name("ipban", "eq", srcip) > 0) {
  98. // ip is already blocked
  99. KSR.dbg("request from blocked IP - " + KSR.kx.get_method()
  100. + " from " + KSR.kx.get_furi() + " (IP:"
  101. + srcip + ":" + KSR.kx.get_srcport() + ")\n");
  102. KSR.x.exit();
  103. }
  104. if (KSR.pike.pike_check_req()<0) {
  105. KSR.err("ALERT: pike blocking " + KSR.kx.get_method()
  106. + " from " + KSR.kx.get_furi() + " (IP:"
  107. + srcip + ":" + KSR.kx.get_srcport() + ")\n");
  108. KSR.htable.sht_seti("ipban", srcip, 1);
  109. KSR.x.exit();
  110. }
  111. }
  112. if (KSR.corex.has_user_agent()>0) {
  113. var UA = KSR.kx.gete_ua();
  114. if (UA.indexOf("friendly")>=0 || UA.indexOf("scanner")>=0
  115. || UA.indexOf("sipcli")>=0 || UA.indexOf("sipvicious")>=0
  116. || UA.indexOf("VaxSIPUserAgent")>=0 || UA.indexOf("pplsip")>= 0) {
  117. KSR.sl.sl_send_reply(200, "OK");
  118. KSR.x.exit();
  119. }
  120. }
  121. if (KSR.maxfwd.process_maxfwd(10) < 0) {
  122. KSR.sl.sl_send_reply(483, "Too Many Hops");
  123. KSR.x.exit();
  124. }
  125. if (KSR.is_OPTIONS()
  126. && KSR.is_myself_ruri()
  127. && KSR.corex.has_ruri_user() < 0) {
  128. KSR.sl.sl_send_reply(200, "Keepalive");
  129. KSR.x.exit();
  130. }
  131. if (KSR.sanity.sanity_check(17895, 7)<0) {
  132. KSR.err("Malformed SIP message from "
  133. + KSR.kx.get_srcip() + ":" + KSR.kx.get_srcport() + "\n");
  134. KSR.x.exit();
  135. }
  136. }
  137. // Handle requests within SIP dialogs
  138. function ksr_route_withindlg()
  139. {
  140. if (KSR.siputils.has_totag()<0) { return; }
  141. // sequential request within a dialog should
  142. // take the path determined by record-routing
  143. if (KSR.rr.loose_route()>0) {
  144. ksr_route_dlguri();
  145. if (KSR.is_BYE()) {
  146. KSR.setflag(FLT_ACC); // do accounting ...
  147. KSR.setflag(FLT_ACCFAILED); // ... even if the transaction fails
  148. } else if (KSR.is_ACK()) {
  149. // ACK is forwarded statelessly
  150. ksr_route_natmanage();
  151. } else if (KSR.is_NOTIFY()) {
  152. // Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  153. KSR.rr.record_route();
  154. }
  155. ksr_route_relay();
  156. KSR.x.exit();
  157. }
  158. if (KSR.is_ACK()) {
  159. if (KSR.tm.t_check_trans() >0) {
  160. // no loose-route, but stateful ACK;
  161. // must be an ACK after a 487
  162. // or e.g. 404 from upstream server
  163. ksr_route_relay();
  164. KSR.x.exit();
  165. } else {
  166. // ACK without matching transaction ... ignore and discard
  167. KSR.x.exit();
  168. }
  169. }
  170. KSR.sl.sl_send_reply(404, "Not here");
  171. KSR.x.exit();
  172. }
  173. // Handle SIP registrations
  174. function ksr_route_registrar()
  175. {
  176. if (!KSR.is_REGISTER()) { return; }
  177. if (KSR.isflagset(FLT_NATS)) {
  178. KSR.setbflag(FLB_NATB);
  179. // do SIP NAT pinging
  180. KSR.setbflag(FLB_NATSIPPING);
  181. }
  182. if (KSR.registrar.save("location", 0)<0) {
  183. KSR.sl.sl_reply_error();
  184. }
  185. KSR.x.exit();
  186. }
  187. // User location service
  188. function ksr_route_location()
  189. {
  190. var rc = KSR.registrar.lookup("location");
  191. if (rc<0) {
  192. KSR.tm.t_newtran();
  193. if (rc==-1 || rc==-3) {
  194. KSR.sl.send_reply(404, "Not Found");
  195. KSR.x.exit();
  196. } else if (rc==-2) {
  197. KSR.sl.send_reply(405, "Method Not Allowed");
  198. KSR.x.exit();
  199. }
  200. }
  201. // when routing via usrloc, log the missed calls also
  202. if (KSR.is_INVITE()) {
  203. KSR.setflag(FLT_ACCMISSED);
  204. }
  205. ksr_route_relay();
  206. KSR.x.exit();
  207. }
  208. // IP authorization and user authentication
  209. function ksr_route_auth()
  210. {
  211. if (!KSR.is_REGISTER()) {
  212. if (KSR.permissions.allow_source_address(1)>0) {
  213. // source IP allowed
  214. return;
  215. }
  216. }
  217. if (KSR.is_REGISTER() || KSR.is_myself_furi()) {
  218. // authenticate requests
  219. if (KSR.auth_db.auth_check(KSR.kx.gete_fhost(), "subscriber", 1)<0) {
  220. KSR.auth.auth_challenge(KSR.kx.gete_fhost(), 0);
  221. KSR.x.exit();
  222. }
  223. // user authenticated - remove auth header
  224. if (!KSR.is_method_in("RP")) {
  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_furi())
  231. && (!KSR.is_myself_ruri())) {
  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.is_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_ruri()) { return; }
  291. KSR.hdr.append_hf("P-Hint: outbound\r\n");
  292. ksr_route_relay();
  293. KSR.x.exit();
  294. }
  295. // Manage outgoing branches
  296. // equivalent of branch_route[...]{}
  297. function ksr_branch_manage()
  298. {
  299. KSR.dbg("new branch [" + KSR.tm.t_get_branch_index()
  300. + "] to " + KSR.kx.get_ruri() + "\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.kx.gets_status();
  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. }