kamailio-basic-kemi-sqlang.sq 8.5 KB

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