c5b597add29293d31df19ac9afba042c085293b9
[sip-router] / src / modules / sl / sl.c
1 /*
2  * sl module - stateless reply
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 /**
24  * @file
25  * @brief SL :: module definitions
26  *
27  * @ingroup sl
28  * Module: @ref sl
29  */
30
31 /*!
32  * @defgroup sl SL :: The SER SL Module
33  *
34  * The SL module allows SER to act as a stateless UA server and
35  * generate replies to SIP requests without keeping state. That is beneficial
36  * in many scenarios, in which you wish not to burden server's memory and scale
37  * well.
38  */
39
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44
45 #include "../../core/sr_module.h"
46 #include "../../core/dprint.h"
47 #include "../../core/error.h"
48 #include "../../core/ut.h"
49 #include "../../core/data_lump.h"
50 #include "../../core/mod_fix.h"
51 #include "../../core/script_cb.h"
52 #include "../../core/mem/mem.h"
53 #include "../../core/kemi.h"
54
55 #include "../../modules/tm/tm_load.h"
56
57 #include "sl_stats.h"
58 #include "sl_funcs.h"
59 #include "sl.h"
60
61 MODULE_VERSION
62
63 static int default_code = 500;
64 static str default_reason = STR_STATIC_INIT("Internal Server Error");
65
66 static int sl_bind_tm = 1;
67 static struct tm_binds tmb;
68
69 static int w_sl_send_reply(struct sip_msg* msg, char* str1, char* str2);
70 static int w_send_reply(struct sip_msg* msg, char* str1, char* str2);
71 static int w_sl_reply_error(struct sip_msg* msg, char* str1, char* str2);
72 static int w_sl_forward_reply0(sip_msg_t* msg, char* str1, char* str2);
73 static int w_sl_forward_reply1(sip_msg_t* msg, char* str1, char* str2);
74 static int w_sl_forward_reply2(sip_msg_t* msg, char* str1, char* str2);
75 static int bind_sl(sl_api_t* api);
76 static int mod_init(void);
77 static int child_init(int rank);
78 static void mod_destroy();
79 static int fixup_sl_reply(void** param, int param_no);
80
81 static cmd_export_t cmds[]={
82         {"sl_send_reply",  w_sl_send_reply,             2, fixup_sl_reply,
83                 REQUEST_ROUTE},
84         {"sl_reply",       w_sl_send_reply,             2, fixup_sl_reply,
85                 REQUEST_ROUTE},
86         {"send_reply",     w_send_reply,                2, fixup_sl_reply,
87                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE},
88         {"sl_reply_error", w_sl_reply_error,            0, 0,
89                 REQUEST_ROUTE},
90         {"sl_forward_reply",  w_sl_forward_reply0,      0, 0,
91                 ONREPLY_ROUTE},
92         {"sl_forward_reply",  w_sl_forward_reply1,      1, fixup_spve_all,
93                 ONREPLY_ROUTE},
94         {"sl_forward_reply",  w_sl_forward_reply2,      2, fixup_spve_all,
95                 ONREPLY_ROUTE},
96         {"bind_sl",        (cmd_function)bind_sl,       0, 0,              0},
97         {0,0,0,0,0}
98 };
99
100
101 /*
102  * Exported parameters
103  */
104 static param_export_t params[] = {
105         {"default_code",   PARAM_INT, &default_code},
106         {"default_reason", PARAM_STR, &default_reason},
107         {"bind_tm",        PARAM_INT, &sl_bind_tm},
108
109         {0, 0, 0}
110 };
111
112
113 #ifdef STATIC_SL
114 struct module_exports sl_exports = {
115 #else
116 struct module_exports exports= {
117 #endif
118         "sl",
119         cmds,
120         sl_rpc,     /* RPC methods */
121         params,     /* param exports */
122         mod_init,   /* module initialization function */
123         (response_function) 0,
124         mod_destroy,
125         0,
126         child_init  /* per-child init function */
127 };
128
129
130 static int mod_init(void)
131 {
132         if (init_sl_stats() < 0) {
133                 ERR("init_sl_stats failed\n");
134                 return -1;
135         }
136         if (sl_register_kstats()<0) {
137                 ERR("init k stats failed\n");
138                 return -1;
139         }
140
141         /* if SL loaded, filter ACKs on beginning */
142         if (register_script_cb( sl_filter_ACK, PRE_SCRIPT_CB|REQUEST_CB, 0 )<0) {
143                 ERR("Failed to install SCRIPT callback\n");
144                 return -1;
145         }
146         if(sl_startup()<0)
147         {
148                 ERR("Failed to do startup tasks\n");
149                 return -1;
150         }
151
152         memset(&tmb, 0, sizeof(struct tm_binds));
153         if(sl_bind_tm!=0)
154         {
155                 if(load_tm_api(&tmb)==-1)
156                 {
157                         LM_INFO("could not bind tm module - only stateless mode"
158                                         " available during modules initialization\n");
159                 }
160         }
161
162         sl_lookup_event_routes();
163
164         return 0;
165 }
166
167 static int child_init(int rank)
168 {
169         if (rank == PROC_INIT) {
170                 if (init_sl_stats_child() < 0) {
171                         ERR("init_sl_stats_child failed\n");
172                         return -1;
173                 }
174                 if(sl_bind_tm!=0 && tmb.register_tmcb==0) {
175                         if(load_tm_api(&tmb)==-1) {
176                                 LM_INFO("could not bind tm module - only stateless mode"
177                                         " available during runtime\n");
178                                 sl_bind_tm=0;
179                         }
180                 }
181
182         }
183         return 0;
184 }
185
186
187 static void mod_destroy()
188 {
189         sl_stats_destroy();
190         sl_shutdown();
191 }
192
193
194 /**
195  * @brief Small wrapper around sl_send_reply
196  *
197  * Warapper around sl_send_rply() which accepts parameters that include
198  * config variables
199  *
200  */
201 static int w_sl_send_reply(struct sip_msg* msg, char* p1, char* p2)
202 {
203         int code, ret;
204         str reason;
205         char* r;
206
207         if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
208                 code = default_code;
209         }
210
211         if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
212                 reason = default_reason;
213         }
214
215         if(reason.s[reason.len-1]=='\0') {
216                 r = reason.s;
217         } else {
218                 r = as_asciiz(&reason);
219                 if (r == NULL) r = default_reason.s;
220         }
221         ret = sl_send_reply(msg, code, r);
222         if ((r!=reason.s) && (r!=default_reason.s)) pkg_free(r);
223
224         return ret;
225 }
226
227
228 /**
229  * @brief Small wrapper around sl_reply_error
230  */
231 static int w_sl_reply_error( struct sip_msg* msg, char* str, char* str2)
232 {
233         return sl_reply_error( msg );
234 }
235
236 /**
237  * @brief send stateful reply if transaction was created
238  *
239  * Check if transation was created for respective SIP request and reply
240  * in stateful mode, otherwise send stateless reply
241  *
242  * @param msg - SIP message structure
243  * @param code - reply status code
244  * @param reason - reply reason phrase
245  * @return 1 for success and -1 for failure
246  */
247 int send_reply(struct sip_msg *msg, int code, str *reason)
248 {
249         char *r = NULL;
250         struct cell *t;
251         int ret = 1;
252
253         if(reason->s[reason->len-1]=='\0') {
254                 r = reason->s;
255         } else {
256                 r = as_asciiz(reason);
257                 if (r == NULL)
258                 {
259                         LM_ERR("no pkg for reason phrase\n");
260                         return -1;
261                 }
262         }
263
264         if(sl_bind_tm!=0 && tmb.t_gett!=0)
265         {
266                 t = tmb.t_gett();
267                 if(t!= NULL && t!=T_UNDEFINED)
268                 {
269                         if(tmb.t_reply(msg, code, r)< 0)
270                         {
271                                 LM_ERR("failed to reply stateful (tm)\n");
272                                 goto error;
273                         }
274                         LM_DBG("reply in stateful mode (tm)\n");
275                         goto done;
276                 }
277         }
278
279         if(msg->first_line.type==SIP_REPLY)
280                 goto error;
281
282         LM_DBG("reply in stateless mode (sl)\n");
283         ret = sl_send_reply(msg, code, r);
284
285 done:
286         if(r!=reason->s) pkg_free(r);
287         return ret;
288
289 error:
290         if(r!=reason->s) pkg_free(r);
291         return -1;
292 }
293
294 /**
295  * @brief Small wrapper around send_reply
296  */
297 static int w_send_reply(struct sip_msg* msg, char* p1, char* p2)
298 {
299         int code;
300         str reason;
301
302         if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
303                 code = default_code;
304         }
305
306         if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
307                 reason = default_reason;
308         }
309
310         return send_reply(msg, code, &reason);
311 }
312
313 /**
314  * @brief store To-tag value in totag parameter
315  */
316 int get_reply_totag(struct sip_msg *msg, str *totag)
317 {
318         struct cell * t;
319         if(msg==NULL || totag==NULL)
320                 return -1;
321         if(sl_bind_tm!=0 && tmb.t_gett!=0)
322         {
323                 t = tmb.t_gett();
324                 if(t!= NULL && t!=T_UNDEFINED)
325                 {
326                         if(tmb.t_get_reply_totag(msg, totag)< 0)
327                         {
328                                 LM_ERR("failed to get totag (tm)\n");
329                                 return -1;
330                         }
331                         LM_DBG("totag stateful mode (tm)\n");
332                         return 1;
333                 }
334         }
335
336         LM_DBG("totag stateless mode (sl)\n");
337         return sl_get_reply_totag(msg, totag);
338 }
339
340
341 /**
342  * @brief fixup for SL reply config file functions
343  */
344 static int fixup_sl_reply(void** param, int param_no)
345 {
346         if (param_no == 1) {
347                 return fixup_var_int_12(param, 1);
348         } else if (param_no == 2) {
349                 return fixup_var_pve_str_12(param, 2);
350         }
351         return 0;
352 }
353
354 /**
355  * @brief forward SIP reply statelessy with different code and reason text
356  */
357 static int w_sl_forward_reply(sip_msg_t* msg, str* code, str* reason)
358 {
359         char oldscode[3];
360         int oldncode;
361         int ret;
362         struct lump     *ldel = NULL;
363         struct lump     *ladd = NULL;
364         char *rbuf;
365
366         if(msg->first_line.type!=SIP_REPLY) {
367                 LM_ERR("invalid SIP message type\n");
368                 return -1;
369         }
370         if(code!=NULL) {
371                 if(code->len!=3) {
372                         LM_ERR("invalid reply code value %.*s\n", code->len, code->s);
373                         return -1;
374                 }
375                 if(msg->first_line.u.reply.status.s[0]!=code->s[0]) {
376                         LM_ERR("reply code class cannot be changed\n");
377                         return -1;
378                 }
379                 if(code->s[1]<'0' || code->s[1]>'9'
380                                 || code->s[2]<'0' || code->s[2]>'9') {
381                         LM_ERR("invalid reply code value %.*s!\n", code->len, code->s);
382                         return -1;
383                 }
384         }
385         if(reason!=NULL && reason->len<=0) {
386                 LM_ERR("invalid reply reason value\n");
387                 return -1;
388         }
389         /* backup old values */
390         oldscode[0] = msg->first_line.u.reply.status.s[0];
391         oldscode[1] = msg->first_line.u.reply.status.s[1];
392         oldscode[2] = msg->first_line.u.reply.status.s[2];
393         oldncode = msg->first_line.u.reply.statuscode;
394         if(code!=NULL) {
395                 /* update status code directly in msg buffer */
396                 msg->first_line.u.reply.statuscode = (code->s[0]-'0')*100
397                         + (code->s[1]-'0')*10 + code->s[2]-'0';
398                 msg->first_line.u.reply.status.s[0] = code->s[0];
399                 msg->first_line.u.reply.status.s[1] = code->s[1];
400                 msg->first_line.u.reply.status.s[2] = code->s[2];
401
402         }
403         if(reason!=NULL) {
404                 ldel = del_lump(msg,
405                                         msg->first_line.u.reply.reason.s - msg->buf,
406                                         msg->first_line.u.reply.reason.len,
407                                         0);
408                 if (ldel==NULL) {
409                         LM_ERR("failed to add del lump\n");
410                         ret = -1;
411                         goto restore;
412                 }
413                 rbuf = (char *)pkg_malloc(reason->len);
414                 if (rbuf==NULL) {
415                         LM_ERR("not enough memory\n");
416                         ret = -1;
417                         goto restore;
418                 }
419                 memcpy(rbuf, reason->s, reason->len);
420                 ladd = insert_new_lump_after(ldel, rbuf, reason->len, 0);
421                 if (ladd==0) {
422                         LOG(L_ERR, "failed to add reason lump: %.*s\n",
423                                 reason->len, reason->s);
424                         pkg_free(rbuf);
425                         ret = -1;
426                         goto restore;
427                 }
428         }
429         ret = forward_reply_nocb(msg);
430 restore:
431         if(reason!=NULL) {
432                 if(ldel!=NULL) {
433                         remove_lump(msg, ldel);
434                         /* ladd is liked in the 'after' list inside ldel,
435                          * destroyed together, no need for its own remove operation */
436                 }
437         }
438         if(code!=NULL) {
439                 msg->first_line.u.reply.statuscode = oldncode;
440                 msg->first_line.u.reply.status.s[0] = oldscode[0];
441                 msg->first_line.u.reply.status.s[1] = oldscode[1];
442                 msg->first_line.u.reply.status.s[2] = oldscode[2];
443         }
444         return (ret==0)?1:ret;
445 }
446
447 /**
448  * @brief forward SIP reply statelessy
449  */
450 static int w_sl_forward_reply0(sip_msg_t* msg, char* str1, char* str2)
451 {
452         return w_sl_forward_reply(msg, NULL, NULL);
453 }
454
455 /**
456  * @brief forward SIP reply statelessy with a new code
457  */
458 static int w_sl_forward_reply1(sip_msg_t* msg, char* str1, char* str2)
459 {
460         str code;
461         if(fixup_get_svalue(msg, (gparam_t*)str1, &code)<0) {
462                 LM_ERR("cannot get the reply code parameter value\n");
463                 return -1;
464         }
465         return w_sl_forward_reply(msg, &code, NULL);
466 }
467
468 /**
469  * @brief forward SIP reply statelessy with new code and reason text
470  */
471 static int w_sl_forward_reply2(sip_msg_t* msg, char* str1, char* str2)
472 {
473         str code;
474         str reason;
475         if(fixup_get_svalue(msg, (gparam_t*)str1, &code)<0) {
476                 LM_ERR("cannot get the reply code parameter value\n");
477                 return -1;
478         }
479         if(fixup_get_svalue(msg, (gparam_t*)str2, &reason)<0) {
480                 LM_ERR("cannot get the reply reason parameter value\n");
481                 return -1;
482         }
483         return w_sl_forward_reply(msg, &code, &reason);
484 }
485
486 /**
487  * @brief bind functions to SL API structure
488  */
489 static int bind_sl(sl_api_t* api)
490 {
491         if (!api) {
492                 ERR("Invalid parameter value\n");
493                 return -1;
494         }
495         api->zreply = sl_send_reply;
496         api->sreply = sl_send_reply_str;
497         api->dreply = sl_send_reply_dlg;
498         api->freply = send_reply;
499         api->get_reply_totag = get_reply_totag;
500         api->register_cb = sl_register_callback;
501
502         return 0;
503 }
504
505 /**
506  *
507  */
508 static sr_kemi_t sl_kemi_exports[] = {
509         { str_init("sl"), str_init("sl_send_reply"),
510                 SR_KEMIP_INT, sl_send_reply_str,
511                 { SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
512                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
513         },
514         { str_init("sl"), str_init("send_reply"),
515                 SR_KEMIP_INT, send_reply,
516                 { SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,
517                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
518         },
519         { str_init("sl"), str_init("sl_reply_error"),
520                 SR_KEMIP_INT, sl_reply_error,
521                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
522                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
523         },
524
525         { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
526 };
527
528 int mod_register(char *path, int *dlflags, void *p1, void *p2)
529 {
530         sr_kemi_modules_add(sl_kemi_exports);
531         return 0;
532 }