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