kamailio-basic-kemi-sqlang.sq 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // KSR.sl.sl_send_reply(100,"Intelligent trying");
  20. // KSR.info("===== request - from kamailio SQLang script\n");
  21. // per request initial checks
  22. ksr_route_reqinit();
  23. // NAT detection
  24. ksr_route_natdetect();
  25. // CANCEL processing
  26. if (KSR.is_CANCEL()) {
  27. if(KSR.tm.t_check_trans()>0) {
  28. ksr_route_relay();
  29. }
  30. return;
  31. }
  32. // handle requests within SIP dialogs
  33. ksr_route_withindlg();
  34. // -- only initial requests (no To tag)
  35. // handle retransmissions
  36. if (!KSR.is_ACK()) {
  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. }
  43. // authentication
  44. ksr_route_auth();
  45. // record routing for dialog forming requests (in case they are routed)
  46. // - remove preloaded route headers
  47. KSR.hdr.remove("Route");
  48. if (KSR.is_method_in("IS")) {
  49. KSR.rr.record_route();
  50. }
  51. // account only INVITEs
  52. if (KSR.is_INVITE()) {
  53. KSR.setflag(FLAGS.FLT_ACC); // do accounting
  54. }
  55. // dispatch requests to foreign domains
  56. ksr_route_sipout();
  57. // -- requests for my local domains
  58. // handle registrations
  59. ksr_route_registrar();
  60. if (KSR.corex.has_ruri_user() < 0) {
  61. // request with no Username in RURI
  62. KSR.sl.sl_send_reply(484, "Address Incomplete");
  63. return;
  64. }
  65. // user location service
  66. ksr_route_location();
  67. return;
  68. }
  69. // wrapper around tm relay function
  70. function ksr_route_relay()
  71. {
  72. // enable additional event routes for forwarded requests
  73. // - serial forking, RTP relaying handling, a.s.o.
  74. if (KSR.is_method_in("IBSU")) {
  75. if (KSR.tm.t_is_set("branch_route")<0) {
  76. KSR.tm.t_on_branch("ksr_branch_manage");
  77. }
  78. }
  79. if (KSR.is_method_in("ISU")) {
  80. if (KSR.tm.t_is_set("onreply_route")<0) {
  81. KSR.tm.t_on_reply("ksr_onreply_manage");
  82. }
  83. }
  84. if (KSR.is_INVITE()) {
  85. if (KSR.tm.t_is_set("failure_route")<0) {
  86. KSR.tm.t_on_failure("ksr_failure_manage");
  87. }
  88. }
  89. if (KSR.tm.t_relay()<0) {
  90. KSR.sl.sl_reply_error();
  91. }
  92. KSR.x.exit();
  93. }
  94. // Per SIP request initial checks
  95. function ksr_route_reqinit()
  96. {
  97. if (!KSR.is_myself_srcip()) {
  98. if (!KSR.pv.is_null("$sht(ipban=>$si)")) {
  99. // ip is already blocked
  100. KSR.dbg("request from blocked IP - " + KSR.pv.get("$rm")
  101. + " from " + KSR.pv.get("$fu") + " (IP:"
  102. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + ")\n");
  103. KSR.x.exit();
  104. }
  105. if (KSR.pike.pike_check_req()<0) {
  106. KSR.err("ALERT: pike blocking " + KSR.pv.get("$rm")
  107. + " from " + KSR.pv.get("$fu") + " (IP:"
  108. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + ")\n");
  109. KSR.pv.seti("$sht(ipban=>$si)", 1);
  110. KSR.x.exit();
  111. }
  112. }
  113. if (KSR.corex.has_user_agent()>0) {
  114. local UA = KSR.pv.get("$ua");
  115. // if (sipscanregex.match(UA)) {
  116. if (UA.find("friendly-scanner")!=null || UA.find("sipcli")!=null) {
  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(1511, 7)<0) {
  132. KSR.err("Malformed SIP message from "
  133. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + "\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 withing 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(FLAGS.FLT_ACC); // do accounting ...
  147. KSR.setflag(FLAGS.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(FLAGS.FLT_NATS)) {
  178. KSR.setbflag(FLAGS.FLB_NATB);
  179. // do SIP NAT pinging
  180. KSR.setbflag(FLAGS.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. local 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(FLAGS.FLT_ACCMISSED);
  204. }
  205. ksr_route_relay();
  206. KSR.x.exit();
  207. }
  208. // IP authorization and user uthentication
  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.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 (!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(FLAGS.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(FLAGS.FLB_NATB);
  258. }
  259. }
  260. }
  261. if (! (KSR.isflagset(FLAGS.FLT_NATS) || KSR.isbflagset(FLAGS.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(FLAGS.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.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. local 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 SQLang script\n");
  330. return;
  331. }