kamailio-basic-kemi-sqlang.sq 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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")!=null || UA.find("scanner")!=null
  117. || UA.find("sipcli")!=null || UA.find("sipvicious")!=null
  118. || UA.find("VaxSIPUserAgent")!= null || UA.find("pplsip")!= null) {
  119. KSR.sl.sl_send_reply(200, "OK");
  120. KSR.x.exit();
  121. }
  122. }
  123. if (KSR.maxfwd.process_maxfwd(10) < 0) {
  124. KSR.sl.sl_send_reply(483, "Too Many Hops");
  125. KSR.x.exit();
  126. }
  127. if (KSR.is_OPTIONS()
  128. && KSR.is_myself_ruri()
  129. && KSR.corex.has_ruri_user() < 0) {
  130. KSR.sl.sl_send_reply(200, "Keepalive");
  131. KSR.x.exit();
  132. }
  133. if (KSR.sanity.sanity_check(1511, 7)<0) {
  134. KSR.err("Malformed SIP message from "
  135. + KSR.pv.get("$si") + ":" + KSR.pv.get("$sp") + "\n");
  136. KSR.x.exit();
  137. }
  138. }
  139. // Handle requests within SIP dialogs
  140. function ksr_route_withindlg()
  141. {
  142. if (KSR.siputils.has_totag()<0) { return; }
  143. // sequential request within a dialog should
  144. // take the path determined by record-routing
  145. if (KSR.rr.loose_route()>0) {
  146. ksr_route_dlguri();
  147. if (KSR.is_BYE()) {
  148. KSR.setflag(FLAGS.FLT_ACC); // do accounting ...
  149. KSR.setflag(FLAGS.FLT_ACCFAILED); // ... even if the transaction fails
  150. } else if (KSR.is_ACK()) {
  151. // ACK is forwarded statelessly
  152. ksr_route_natmanage();
  153. } else if (KSR.is_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 (KSR.is_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.is_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.is_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. if (!KSR.is_REGISTER()) {
  214. if (KSR.permissions.allow_source_address(1)>0) {
  215. // source IP allowed
  216. return;
  217. }
  218. }
  219. if (KSR.is_REGISTER() || KSR.is_myself_furi()) {
  220. // authenticate requests
  221. if (KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0) {
  222. KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0);
  223. KSR.x.exit();
  224. }
  225. // user authenticated - remove auth header
  226. if (!KSR.is_method_in("RP")) {
  227. KSR.auth.consume_credentials();
  228. }
  229. }
  230. // if caller is not local subscriber, then check if it calls
  231. // a local destination, otherwise deny, not an open relay here
  232. if ((!KSR.is_myself_furi())
  233. && (!KSR.is_myself_ruri())) {
  234. KSR.sl.sl_send_reply(403, "Not relaying");
  235. KSR.x.exit();
  236. }
  237. return;
  238. }
  239. // Caller NAT detection
  240. function ksr_route_natdetect()
  241. {
  242. KSR.force_rport();
  243. if (KSR.nathelper.nat_uac_test(19)>0) {
  244. if (KSR.is_REGISTER()) {
  245. KSR.nathelper.fix_nated_register();
  246. } else if (KSR.siputils.is_first_hop()>0) {
  247. KSR.nathelper.set_contact_alias();
  248. }
  249. KSR.setflag(FLAGS.FLT_NATS);
  250. }
  251. return;
  252. }
  253. // RTPProxy control
  254. function ksr_route_natmanage()
  255. {
  256. if (KSR.siputils.is_request()>0) {
  257. if (KSR.siputils.has_totag()>0) {
  258. if (KSR.rr.check_route_param("nat=yes")>0) {
  259. KSR.setbflag(FLAGS.FLB_NATB);
  260. }
  261. }
  262. }
  263. if (! (KSR.isflagset(FLAGS.FLT_NATS) || KSR.isbflagset(FLAGS.FLB_NATB))) {
  264. return;
  265. }
  266. KSR.rtpproxy.rtpproxy_manage("co");
  267. if (KSR.siputils.is_request()>0) {
  268. if (! KSR.siputils.has_totag()) {
  269. if (KSR.tmx.t_is_branch_route()>0) {
  270. KSR.rr.add_rr_param(";nat=yes");
  271. }
  272. }
  273. }
  274. if (KSR.siputils.is_reply()>0) {
  275. if (KSR.isbflagset(FLAGS.FLB_NATB)) {
  276. KSR.nathelper.set_contact_alias();
  277. }
  278. }
  279. return;
  280. }
  281. // URI update for dialog requests
  282. function ksr_route_dlguri()
  283. {
  284. if (! KSR.isdsturiset()) {
  285. KSR.nathelper.handle_ruri_alias();
  286. }
  287. return;
  288. }
  289. // Routing to foreign domains
  290. function ksr_route_sipout()
  291. {
  292. if (KSR.is_myself_ruri()) { return; }
  293. KSR.hdr.append_hf("P-Hint: outbound\r\n");
  294. ksr_route_relay();
  295. KSR.x.exit();
  296. }
  297. // Manage outgoing branches
  298. // equivalent of branch_route[...]{}
  299. function ksr_branch_manage()
  300. {
  301. KSR.dbg("new branch [" + KSR.pv.get("$T_branch_idx")
  302. + "] to " + KSR.pv.get("$ru") + "\n");
  303. ksr_route_natmanage();
  304. return;
  305. }
  306. // Manage incoming replies
  307. // equivalent of onreply_route[...]{}
  308. function ksr_onreply_manage()
  309. {
  310. KSR.dbg("incoming reply\n");
  311. local scode = KSR.pv.get("$rs");
  312. if (scode>100 && scode<=299) {
  313. ksr_route_natmanage();
  314. }
  315. return;
  316. }
  317. // Manage failure routing cases
  318. // equivalent of failure_route[...]{}
  319. function ksr_failure_manage()
  320. {
  321. ksr_route_natmanage();
  322. if (KSR.tm.t_is_canceled()>0) {
  323. return;
  324. }
  325. return;
  326. }
  327. // SIP response handling
  328. // equivalent of reply_route{}
  329. function ksr_reply_route()
  330. {
  331. KSR.info("===== response - from kamailio SQLang script\n");
  332. return;
  333. }