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