voicemail specific code added (see #define VOICE_MAIL and module vm)
[sip-router] / modules / tm / tm.c
1 /*
2  * $Id$
3  *
4  * TM module
5  *
6  *
7  * ***************************************************
8  * * Jiri's Source Memorial                          *
9  * *                                                 *
10  * * Welcome, pilgrim ! This is the greatest place   *
11  * * where dramatic changes happend. There are not   *
12  * * many places with a history like this, as there  *
13  * * are not so many people like Jiri, one of the    *
14  * * ser's fathers, who brought everywhere the wind  *
15  * * of change, the flood of clean-up. We all felt   *
16  * * his fatherly eye watching over us day and night.*
17  * *                                                 *
18  * * Please, preserve this codework heritage, as     *
19  * * it's unlikly for fresh, juicy pices of code to  *
20  * * arise to give him the again the chance to       *
21  * * demonstrate his clean-up and improvement skills.*
22  * *                                                 *
23  * * Hereby, we solicit you to adopt this historical *
24  * * piece of code. For $100, your name will be      *
25  * * be printed in this banner and we will use       *
26  * * collected funds to create and display an ASCII  *
27  * * statue of Jiri  .                               *
28  * ***************************************************
29  *
30  *
31  * Copyright (C) 2001-2003 Fhg Fokus
32  *
33  * This file is part of ser, a free SIP server.
34  *
35  * ser is free software; you can redistribute it and/or modify
36  * it under the terms of the GNU General Public License as published by
37  * the Free Software Foundation; either version 2 of the License, or
38  * (at your option) any later version
39  *
40  * For a license to use the ser software under conditions
41  * other than those described here, or to purchase support for this
42  * software, please contact iptel.org by e-mail at the following addresses:
43  *    info@iptel.org
44  *
45  * ser is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License 
51  * along with this program; if not, write to the Free Software 
52  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
53  */
54
55
56 #include "defs.h"
57
58
59 #include <stdio.h>
60 #include <string.h>
61 #include <netdb.h>
62
63 #include "../../sr_module.h"
64 #include "../../dprint.h"
65 #include "../../error.h"
66 #include "../../ut.h"
67 #include "../../script_cb.h"
68 #include "../../fifo_server.h"
69
70 #include "sip_msg.h"
71 #include "h_table.h"
72 #include "t_funcs.h"
73 #include "t_hooks.h"
74 #include "tm_load.h"
75 #include "ut.h"
76 #include "t_reply.h"
77 #include "uac.h"
78 #include "t_fwd.h"
79 #include "t_lookup.h"
80 #include "t_stats.h"
81
82
83
84 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2);
85 inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2);
86 #ifdef _OBSOLETED
87 inline static int w_t_reply_unsafe(struct sip_msg* msg, char* str, char* str2);
88 #endif
89 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2);
90 inline static int fixup_t_send_reply(void** param, int param_no);
91 inline static int fixup_str2int( void** param, int param_no);
92 inline static int w_t_retransmit_reply(struct sip_msg* p_msg, char* foo, char* bar );
93 inline static int w_t_newtran(struct sip_msg* p_msg, char* foo, char* bar );
94 inline static int w_t_newdlg( struct sip_msg* p_msg, char* foo, char* bar );
95 inline static int w_t_relay( struct sip_msg  *p_msg , char *_foo, char *_bar);
96 inline static int w_t_relay_to( struct sip_msg  *p_msg , char *proxy, char *);
97 inline static int w_t_replicate( struct sip_msg  *p_msg , 
98         char *proxy, /* struct proxy_l *proxy expected */
99         char *_foo       /* nothing expected */ );
100 inline static int w_t_forward_nonack(struct sip_msg* msg, char* str, char* );
101 inline static int fixup_hostport2proxy(void** param, int param_no);
102 inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo );
103
104
105 static int mod_init(void);
106
107 static int child_init(int rank);
108
109
110 #ifdef STATIC_TM
111 struct module_exports tm_exports = {
112 #else
113 struct module_exports exports= {
114 #endif
115         "tm",
116         /* -------- exported functions ----------- */
117         (char*[]){                      
118                                 "t_newtran",
119                                 "t_lookup_request",
120                                 T_REPLY,
121 #ifdef _OBSO
122                                 T_REPLY_UNSAFE,
123 #endif
124                                 "t_retransmit_reply",
125                                 "t_release",
126                                 T_RELAY_TO,
127                                 "t_replicate",
128                                 T_RELAY,
129                                 T_FORWARD_NONACK,
130                                 "t_on_negative",
131
132                                 /* not applicable from script ... */
133
134                                 "register_tmcb",
135 #ifndef DEPRECATE_OLD_STUFF
136                                 T_UAC,
137 #endif
138                                 T_UAC_DLG,
139                                 "load_tm",
140 #ifdef VOICE_MAIL
141                                 T_REPLY_WB,
142                                 T_IS_LOCAL,
143                                 T_GET_TI,
144                                 T_LOOKUP_IDENT,
145 #endif
146                                 "t_newdlg"
147                         },
148         (cmd_function[]){
149                                         w_t_newtran,
150                                         w_t_check,
151                                         w_t_reply,
152 #ifdef _OBSO
153                                         w_t_reply_unsafe,
154 #endif
155                                         w_t_retransmit_reply,
156                                         w_t_release,
157                                         w_t_relay_to,
158                                         w_t_replicate,
159                                         w_t_relay,
160                                         w_t_forward_nonack,
161                                         w_t_on_negative,
162
163                                         (cmd_function) register_tmcb,
164 #ifndef DEPRECATE_OLD_STUFF
165                                         (cmd_function) t_uac,
166 #endif
167                                         (cmd_function) t_uac_dlg,
168                                         (cmd_function) load_tm,
169 #ifdef VOICE_MAIL
170                                         (cmd_function) t_reply_with_body,
171                                         (cmd_function) t_is_local,
172                                         (cmd_function) t_get_trans_ident,
173                                         (cmd_function) t_lookup_ident,
174 #endif
175                                         w_t_newdlg,
176                                         },
177         (int[]){
178                                 0, /* t_newtran */
179                                 0, /* t_lookup_request */
180                                 2, /* t_reply */
181 #ifdef _OBSO
182                                 2, /* t_reply_unsafe */
183 #endif
184                                 0, /* t_retransmit_reply */
185                                 0, /* t_release */
186                                 2, /* t_relay_to */
187                                 2, /* t_replicate */
188                                 0, /* t_relay */
189                                 2, /* t_forward_nonack */
190                                 1, /* t_on_negative */
191                                 NO_SCRIPT /* register_tmcb */,
192 #ifndef DEPRECATE_OLD_STUFF
193                                 NO_SCRIPT /* t_uac */,
194 #endif
195                                 NO_SCRIPT /* t_uac_dlg */,
196                                 NO_SCRIPT /* load_tm */,
197 #ifdef VOICE_MAIL
198                                 NO_SCRIPT /* t_reply_with_body */,
199                                 NO_SCRIPT /* t_is_local */,
200                                 NO_SCRIPT /* t_get_trans_ident */,
201                                 NO_SCRIPT /* t_lookup_ident */,
202 #endif
203                                 0 /* t_newdlg */
204                         },
205         (fixup_function[]){
206                                 0,                                              /* t_newtran */
207                                 0,                                              /* t_lookup_request */
208                                 fixup_t_send_reply,             /* t_reply */
209 #ifdef _OBSO
210                                 fixup_t_send_reply,             /* t_reply_unsafe */
211 #endif
212                                 0,                                              /* t_retransmit_reply */
213                                 0,                                              /* t_release */
214                                 fixup_hostport2proxy,   /* t_relay_to */
215                                 fixup_hostport2proxy,   /* t_replicate */
216                                 0,                                              /* t_relay */
217                                 fixup_hostport2proxy,   /* t_forward_nonack */
218                                 fixup_str2int,                  /* t_on_negative */
219                                 0,                                              /* register_tmcb */
220 #ifndef DEPRECATE_OLD_STUFF
221                                 0,                                              /* t_uac */
222 #endif
223                                 0,                                              /* t_uac_dlg */
224                                 0,                                              /* load_tm */
225 #ifdef VOICE_MAIL
226                                 0, /* t_reply_with_body */
227                                 0, /* t_is_local */
228                                 0, /* t_get_trans_ident */
229                                 0, /* t_lookup_ident */
230 #endif
231                                 0                                               /* t_newdlg */
232         
233                 },
234 #ifndef DEPRECATE_OLD_STUFF
235         1+
236 #endif
237 #ifdef _OBSO
238         1+
239 #endif
240 #ifdef VOICE_MAIL
241         4+
242 #endif
243         14,
244
245         /* ------------ exported variables ---------- */
246         (char *[]) { /* Module parameter names */
247                 "ruri_matching",
248                 "fr_timer",
249                 "fr_inv_timer",
250                 "wt_timer",
251                 "delete_timer",
252                 "retr_timer1p1",
253                 "retr_timer1p2",
254                 "retr_timer1p3",
255                 "retr_timer2",
256                 "noisy_ctimer"
257 #ifndef DEPRECATE_OLD_STUFF
258                 ,"uac_from"
259 #endif
260         },
261         (modparam_t[]) { /* variable types */
262                 INT_PARAM, /* ruri_matching */
263                 INT_PARAM, /* fr_timer */
264                 INT_PARAM, /* fr_inv_timer */
265                 INT_PARAM, /* wt_timer */
266                 INT_PARAM, /* delete_timer */
267                 INT_PARAM,/* retr_timer1p1 */
268                 INT_PARAM, /* retr_timer1p2 */
269                 INT_PARAM, /* retr_timer1p3 */
270                 INT_PARAM, /* retr_timer2 */
271                 INT_PARAM /* noisy_ctimer */
272 #ifndef DEPRECATE_OLD_STUFF
273                 ,STR_PARAM /* uac_from */
274 #endif
275         },
276         (void *[]) { /* variable pointers */
277                 &ruri_matching,
278                 &(timer_id2timeout[FR_TIMER_LIST]),
279                 &(timer_id2timeout[FR_INV_TIMER_LIST]),
280                 &(timer_id2timeout[WT_TIMER_LIST]),
281                 &(timer_id2timeout[DELETE_LIST]),
282                 &(timer_id2timeout[RT_T1_TO_1]),
283                 &(timer_id2timeout[RT_T1_TO_2]),
284                 &(timer_id2timeout[RT_T1_TO_3]),
285                 &(timer_id2timeout[RT_T2]),
286                 &noisy_ctimer
287 #ifndef DEPRECATE_OLD_STUFF
288                 ,&uac_from
289 #endif
290         },
291 #ifndef DEPRECATE_OLD_STUFF
292         1+
293 #endif
294         10,      /* Number of module paramers */
295
296         mod_init, /* module initialization function */
297         (response_function) t_on_reply,
298         (destroy_function) tm_shutdown,
299         0, /* w_onbreak, */
300         child_init /* per-child init function */
301 };
302
303 inline static int fixup_str2int( void** param, int param_no)
304 {
305         unsigned int go_to;
306         int err;
307
308         if (param_no==1) {
309                 go_to=str2s(*param, strlen(*param), &err );
310                 if (err==0) {
311                         free(*param);
312                         *param=(void *)go_to;
313                         return 0;
314                 } else {
315                         LOG(L_ERR, "ERROR: fixup_str2int: bad number <%s>\n",
316                                 (char *)(*param));
317                         return E_CFG;
318                 }
319         }
320         return 0;
321 }
322
323 static int w_t_unref( struct sip_msg *foo, void *bar)
324 {
325         return t_unref(foo);
326 }
327
328 static int script_init( struct sip_msg *foo, void *bar)
329 {   
330         /* we primarily reset all private memory here to make sure
331            private values left over from previous message will
332            not be used again
333     */
334
335         /* make sure the new message will not inherit previous
336            message's t_on_negative value
337         */
338         t_on_negative( 0 );
339
340         return 1;
341 }
342
343 static int mod_init(void)
344 {
345
346         DBG( "TM - initializing...\n");
347         /* checking if we have sufficient bitmap capacity for given
348            maximum number of  branches */
349         if (MAX_BRANCHES+1>31) {
350                 LOG(L_CRIT, "Too many max UACs for UAC branch_bm_t bitmap: %d\n",
351                         MAX_BRANCHES );
352                 return -1;
353         }
354
355
356 #ifndef DEPRECATE_OLD_STUFF
357         if (register_fifo_cmd(fifo_uac, "t_uac", 0)<0) {
358                 LOG(L_CRIT, "cannot register fifo uac\n");
359                 return -1;
360         }
361         if (register_fifo_cmd(fifo_uac_from, "t_uac_from", 0)<0) {
362                 LOG(L_CRIT, "cannot register fifo uac\n");
363                 return -1;
364         }
365 #endif
366         if (register_fifo_cmd(fifo_uac_dlg, "t_uac_dlg", 0)<0) {
367                 LOG(L_CRIT, "cannot register fifo uac\n");
368                 return -1;
369         }
370         if (register_fifo_cmd(fifo_hash, "t_hash", 0)<0) {
371                 LOG(L_CRIT, "cannot register hash\n");
372                 return -1;
373         }
374
375         if (!init_hash_table()) {
376                 LOG(L_ERR, "ERROR: mod_init: initializing hash_table failed\n");
377                 return -1;
378         }
379
380
381         /* init static hidden values */
382         init_t();
383
384         if (!tm_init_timers()) {
385                 LOG(L_ERR, "ERROR: mod_init: timer init failed\n");
386                 return -1;
387         }
388         /* register the timer function */
389         register_timer( timer_routine , 0 /* empty attr */, 1 );
390
391         /* init_tm_stats calls process_count, which should
392          * NOT be called from mod_init, because one does not
393          * now, if a timer is used and thus how many processes
394          * will be started; however we started already our
395          * timers, so we know and process_count should not
396          * change any more
397          */     
398         if (init_tm_stats()<0) {
399                 LOG(L_CRIT, "ERROR: mod_init: failed to init stats\n");
400                 return -1;
401         }
402
403         /* building the hash table*/
404
405         if (uac_init()==-1) {
406                 LOG(L_ERR, "ERROR: mod_init: uac_init failed\n");
407                 return -1;
408         }
409 #ifdef _OBSO
410         register_tmcb( TMCB_ON_NEGATIVE, on_negative_reply, 
411                         0 /* empty param */);
412 #endif
413         /* register post-script clean-up function */
414         register_script_cb( w_t_unref, POST_SCRIPT_CB, 
415                         0 /* empty param */ );
416         register_script_cb( script_init, PRE_SCRIPT_CB , 
417                         0 /* empty param */ );
418
419         tm_init_tags();
420
421         return 0;
422 }
423
424 static int child_init(int rank) {
425         if (uac_child_init(rank)==-1) {
426                 LOG(L_ERR, "ERROR: child_init: uac_child_init error\n");
427                 return -1;
428         }
429         return 1;
430 }
431
432
433 /* (char *hostname, char *port_nr) ==> (struct proxy_l *, -)  */
434
435 inline static int fixup_hostport2proxy(void** param, int param_no)
436 {
437         unsigned int port;
438         char *host;
439         int err;
440         struct proxy_l *proxy;
441         str s;
442         
443         DBG("TM module: fixup_t_forward(%s, %d)\n", (char*)*param, param_no);
444         if (param_no==1){
445                 DBG("TM module: fixup_t_forward: param 1.. do nothing, wait for #2\n");
446                 return 0;
447         } else if (param_no==2) {
448
449                 host=(char *) (*(param-1)); 
450                 port=str2s(*param, strlen(*param), &err);
451                 if (err!=0) {
452                         LOG(L_ERR, "TM module:fixup_t_forward: bad port number <%s>\n",
453                                 (char*)(*param));
454                          return E_UNSPEC;
455                 }
456                 s.s = host;
457                 s.len = strlen(host);
458                 proxy=mk_proxy(&s, port);
459                 if (proxy==0) {
460                         LOG(L_ERR, "ERROR: fixup_t_forwardv6: bad host name in URI <%s>\n",
461                                 host );
462                         return E_BAD_ADDRESS;
463                 }
464                 /* success -- fix the first parameter to proxy now ! */
465
466                 /* FIXME: janakj, mk_proxy doesn't make copy of host !! */
467                 free( *(param-1));
468                 *(param-1)=proxy;
469                 return 0;
470         } else {
471                 LOG(L_ERR, "ERROR: fixup_t_forwardv6 called with parameter #<>{1,2}\n");
472                 return E_BUG;
473         }
474 }
475
476
477 /* (char *code, char *reason_phrase)==>(int code, r_p as is) */
478 inline static int fixup_t_send_reply(void** param, int param_no)
479 {
480         unsigned int code;
481         int err;
482
483         if (param_no==1){
484                 code=str2s(*param, strlen(*param), &err);
485                 if (err==0){
486                         free(*param);
487                         *param=(void*)code;
488                         return 0;
489                 }else{
490                         LOG(L_ERR, "TM module:fixup_t_send_reply: bad  number <%s>\n",
491                                         (char*)(*param));
492                         return E_UNSPEC;
493                 }
494         }
495         /* second param => no conversion*/
496         return 0;
497 }
498
499
500
501
502 inline static int w_t_check(struct sip_msg* msg, char* str, char* str2)
503 {
504         return t_check( msg , 0  ) ? 1 : -1;
505 }
506
507
508
509 inline static int w_t_forward_nonack(struct sip_msg* msg, char* proxy, char* _foo)
510 {
511         struct cell *t;
512         if (t_check( msg , 0 )==-1) return -1;
513         t=get_t();
514         if ( t && t!=T_UNDEFINED ) {
515                 if (msg->REQ_METHOD==METHOD_ACK) {
516                         LOG(L_WARN,"WARNING: you don't really want to fwd hbh ACK\n");
517                         return -1;
518                 }
519                 return t_forward_nonack(t, msg, ( struct proxy_l *) proxy );
520         } else {
521                 DBG("DEBUG: t_forward_nonack: no transaction found\n");
522                 return -1;
523         }
524 }
525
526
527
528 inline static int w_t_reply(struct sip_msg* msg, char* str, char* str2)
529 {
530         struct cell *t;
531
532         if (msg->REQ_METHOD==METHOD_ACK) {
533                 LOG(L_WARN, "WARNING: t_reply: ACKs are not replied\n");
534                 return -1;
535         }
536         if (t_check( msg , 0 )==-1) return -1;
537         t=get_t();
538         if (!t) {
539                 LOG(L_ERR, "ERROR: t_reply: cannot send a t_reply to a message "
540                         "for which no T-state has been established\n");
541                 return -1;
542         }
543         /* if called from reply_route, make sure that unsafe version
544          * is called; we are already in a mutex and another mutex in
545          * the safe version would lead to a deadlock
546          */
547         if (rmode==MODE_ONREPLY_REQUEST) { 
548                 DBG("DEBUG: t_reply_unsafe called from w_t_reply\n");
549                 return t_reply_unsafe(t, msg, (unsigned int) str, str2);
550         } else {
551                 return t_reply( t, msg, (unsigned int) str, str2);
552         }
553 }
554
555 #ifdef _OBSOLETED
556 inline static int w_t_reply_unsafe(struct sip_msg* msg, char* str, char* str2)
557 {
558         struct cell *t;
559
560         if (msg->REQ_METHOD==METHOD_ACK) {
561                 LOG(L_WARN, "WARNING: t_reply: ACKs are not replied\n");
562                 return -1;
563         }
564         if (t_check( msg , 0 )==-1) return -1;
565         t=get_t();
566         if (!t) {
567                 LOG(L_ERR, "ERROR: t_reply: cannot send a t_reply to a message "
568                         "for which no T-state has been established\n");
569                 return -1;
570         }
571         return t_reply_unsafe(t, msg, (unsigned int) str, str2);
572 }
573 #endif
574
575
576 inline static int w_t_release(struct sip_msg* msg, char* str, char* str2)
577 {
578         struct cell *t;
579         if (t_check( msg  , 0  )==-1) return -1;
580         t=get_t();
581         if ( t && t!=T_UNDEFINED ) 
582                 return t_release_transaction( t );
583         return 1;
584 }
585
586
587
588
589 inline static int w_t_retransmit_reply( struct sip_msg* p_msg, char* foo, char* bar)
590 {
591         struct cell *t;
592
593
594         if (t_check( p_msg  , 0 )==-1) 
595                 return 1;
596         t=get_t();
597         if (t) {
598                 if (p_msg->REQ_METHOD==METHOD_ACK) {
599                         LOG(L_WARN, "WARNING: : ACKs ansmit_replies not replied\n");
600                         return -1;
601                 }
602                 return t_retransmit_reply( t );
603         } else 
604                 return -1;
605         return 1;
606 }
607
608
609
610
611
612 inline static int w_t_newdlg( struct sip_msg* p_msg, char* foo, char* bar ) 
613 {
614         return t_newdlg( p_msg );
615 }
616
617 inline static int w_t_newtran( struct sip_msg* p_msg, char* foo, char* bar ) 
618 {
619         /* t_newtran returns 0 on error (negative value means
620            'transaction exists'
621         */
622         return t_newtran( p_msg );
623 }
624
625
626 inline static int w_t_on_negative( struct sip_msg* msg, char *go_to, char *foo )
627 {
628         return t_on_negative( (unsigned int ) go_to );
629 }
630
631 inline static int w_t_relay_to( struct sip_msg  *p_msg , 
632         char *proxy, /* struct proxy_l *proxy expected */
633         char *_foo       /* nothing expected */ )
634 {
635         return t_relay_to( p_msg, ( struct proxy_l *) proxy,
636         0 /* no replication */ );
637 }
638
639 inline static int w_t_replicate( struct sip_msg  *p_msg , 
640         char *proxy, /* struct proxy_l *proxy expected */
641         char *_foo       /* nothing expected */ )
642 {
643         return t_replicate(p_msg, ( struct proxy_l *) proxy );
644 }
645
646 inline static int w_t_relay( struct sip_msg  *p_msg , 
647                                                 char *_foo, char *_bar)
648 {
649         return t_relay_to( p_msg, 
650                 (struct proxy_l *) 0 /* no proxy */,
651                 0 /* no replication */ );
652 }
653
654