- avp core replaced with .14 version
[sip-router] / modules / tm / h_table.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser 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  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  * History
28  * -------
29  * 2003-03-06  200/INV to-tag list deallocation added;
30  *             setting "kill_reason" moved in here -- it is moved
31  *             from transaction state to a static var(jiri)
32  * 2003-03-16  removed _TOTAG (jiri)
33  * 2003-03-30  set_kr for requests only (jiri)
34  * 2003-04-04  bug_fix: REQ_IN callback not called for local 
35  *             UAC transactions (jiri)
36  * 2003-09-12  timer_link->tg will be set only if EXTRA_DEBUG (andrei)
37  * 2003-12-04  global callbacks replaceed with callbacks per transaction;
38  *             completion callback merged into them as LOCAL_COMPETED (bogdan)
39  * 2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
40  * 2004-02-13  t->is_invite and t->local replaced with flags;
41  *             timer_link.payload removed (bogdan)
42  * 2004-08-23  avp support added - move and remove avp list to/from
43  *             transactions (bogdan)
44  */
45
46 #include <stdlib.h>
47
48
49 #include "../../mem/shm_mem.h"
50 #include "../../hash_func.h"
51 #include "../../dprint.h"
52 #include "../../md5utils.h"
53 #include "../../ut.h"
54 #include "../../globals.h"
55 #include "../../error.h"
56 #include "../../fifo_server.h"
57 #include "../../unixsock_server.h"
58 #include "defs.h"
59 #include "t_reply.h"
60 #include "t_cancel.h"
61 #include "t_stats.h"
62 #include "h_table.h"
63
64 static enum kill_reason kr;
65
66 /* pointer to the big table where all the transaction data
67    lives */
68 static struct s_table*  tm_table;
69
70
71
72 void set_kr( enum kill_reason _kr )
73 {
74         kr|=_kr;
75 }
76
77
78 enum kill_reason get_kr() {
79         return kr;
80 }
81
82
83 void lock_hash(int i) 
84 {
85         lock(&tm_table->entrys[i].mutex);
86 }
87
88
89 void unlock_hash(int i) 
90 {
91         unlock(&tm_table->entrys[i].mutex);
92 }
93
94
95 struct s_table* get_tm_table()
96 {
97         return tm_table;
98 }
99
100
101 unsigned int transaction_count( void )
102 {
103         unsigned int i;
104         unsigned int count;
105
106         count=0;        
107         for (i=0; i<TABLE_ENTRIES; i++) 
108                 count+=tm_table->entrys[i].cur_entries;
109         return count;
110 }
111
112
113
114 void free_cell( struct cell* dead_cell )
115 {
116         char *b;
117         int i;
118         struct sip_msg *rpl;
119         struct totag_elem *tt, *foo;
120         struct tm_callback *cbs, *cbs_tmp;
121
122         release_cell_lock( dead_cell );
123         shm_lock();
124
125         /* UA Server */
126         if ( dead_cell->uas.request )
127                 sip_msg_free_unsafe( dead_cell->uas.request );
128         if ( dead_cell->uas.response.buffer )
129                 shm_free_unsafe( dead_cell->uas.response.buffer );
130
131         /* callbacks */
132         for( cbs=dead_cell->tmcb_hl.first ; cbs ; ) {
133                 cbs_tmp = cbs;
134                 cbs = cbs->next;
135                 shm_free_unsafe( cbs_tmp );
136         }
137
138         /* UA Clients */
139         for ( i =0 ; i<dead_cell->nr_of_outgoings;  i++ )
140         {
141                 /* retransmission buffer */
142                 if ( (b=dead_cell->uac[i].request.buffer) )
143                         shm_free_unsafe( b );
144                 b=dead_cell->uac[i].local_cancel.buffer;
145                 if (b!=0 && b!=BUSY_BUFFER)
146                         shm_free_unsafe( b );
147                 rpl=dead_cell->uac[i].reply;
148                 if (rpl && rpl!=FAKED_REPLY) {
149                         sip_msg_free_unsafe( rpl );
150                 }
151         }
152
153         /* collected to tags */
154         tt=dead_cell->fwded_totags;
155         while(tt) {
156                 foo=tt->next;
157                 shm_free_unsafe(tt->tag.s);
158                 shm_free_unsafe(tt);
159                 tt=foo;
160         }
161
162         /* free the avp list */
163         if (dead_cell->user_avps)
164                 destroy_avp_list_unsafe( &dead_cell->user_avps );
165
166         /* the cell's body */
167         shm_free_unsafe( dead_cell );
168
169         shm_unlock();
170 }
171
172
173
174 static inline void init_synonym_id( struct cell *t )
175 {
176         struct sip_msg *p_msg;
177         int size;
178         char *c;
179         unsigned int myrand;
180
181         if (!syn_branch) {
182                 p_msg=t->uas.request;
183                 if (p_msg) {
184                         /* char value of a proxied transaction is
185                            calculated out of header-fileds forming
186                            transaction key
187                         */
188                         char_msg_val( p_msg, t->md5 );
189                 } else {
190                         /* char value for a UAC transaction is created
191                            randomly -- UAC is an originating stateful element 
192                            which cannot be refreshed, so the value can be
193                            anything
194                         */
195                         /* HACK : not long enough */
196                         myrand=rand();
197                         c=t->md5;
198                         size=MD5_LEN;
199                         memset(c, '0', size );
200                         int2reverse_hex( &c, &size, myrand );
201                 }
202         }
203 }
204
205 static void inline init_branches(struct cell *t)
206 {
207         unsigned int i;
208         struct ua_client *uac;
209
210         for(i=0;i<MAX_BRANCHES;i++)
211         {
212                 uac=&t->uac[i];
213                 uac->request.my_T = t;
214                 uac->request.branch = i;
215 #ifdef EXTRA_DEBUG
216                 uac->request.fr_timer.tg = TG_FR;
217                 uac->request.retr_timer.tg = TG_RT;
218 #endif
219                 uac->local_cancel=uac->request;
220         }
221 }
222
223
224 struct cell*  build_cell( struct sip_msg* p_msg )
225 {
226         struct cell* new_cell;
227         int          sip_msg_len;
228         struct usr_avp **old;
229
230         /* allocs a new cell */
231         new_cell = (struct cell*)shm_malloc( sizeof( struct cell ) );
232         if  ( !new_cell ) {
233                 ser_error=E_OUT_OF_MEM;
234                 return NULL;
235         }
236
237         /* filling with 0 */
238         memset( new_cell, 0, sizeof( struct cell ) );
239
240         /* UAS */
241 #ifdef EXTRA_DEBUG
242         new_cell->uas.response.retr_timer.tg=TG_RT;
243         new_cell->uas.response.fr_timer.tg=TG_FR;
244 #endif
245         new_cell->uas.response.my_T=new_cell;
246
247         /* move the current avp list to transaction -bogdan */
248         old = set_avp_list( &new_cell->user_avps );
249         new_cell->user_avps = *old;
250         *old = 0;
251
252         /* enter callback, which may potentially want to parse some stuff,
253          * before the request is shmem-ized */
254         if ( p_msg && has_reqin_tmcbs() )
255                 run_reqin_callbacks( new_cell, p_msg, p_msg->REQ_METHOD);
256
257         if (p_msg) {
258                 new_cell->uas.request = sip_msg_cloner(p_msg,&sip_msg_len);
259                 if (!new_cell->uas.request)
260                         goto error;
261                 new_cell->uas.end_request=((char*)new_cell->uas.request)+sip_msg_len;
262         }
263
264         /* UAC */
265         init_branches(new_cell);
266
267         new_cell->relaied_reply_branch   = -1;
268         /* new_cell->T_canceled = T_UNDEFINED; */
269 #ifdef EXTRA_DEBUG
270         new_cell->wait_tl.tg=TG_WT;
271         new_cell->dele_tl.tg=TG_DEL;
272 #endif
273
274         init_synonym_id(new_cell);
275         init_cell_lock(  new_cell );
276         return new_cell;
277
278 error:
279         shm_free(new_cell);
280         /* unlink transaction AVP list and link back the global AVP list (bogdan)*/
281         reset_avps();
282         return NULL;
283 }
284
285
286
287 /* Release all the data contained by the hash table. All the aux. structures
288  *  as sems, lists, etc, are also released */
289 void free_hash_table(  )
290 {
291         struct cell* p_cell;
292         struct cell* tmp_cell;
293         int    i;
294
295         if (tm_table)
296         {
297                 /* remove the data contained by each entry */
298                 for( i = 0 ; i<TABLE_ENTRIES; i++)
299                 {
300                         release_entry_lock( (tm_table->entrys)+i );
301                         /* delete all synonyms at hash-collision-slot i */
302                         p_cell=tm_table->entrys[i].first_cell;
303                         for( ; p_cell; p_cell = tmp_cell )
304                         {
305                                 tmp_cell = p_cell->next_cell;
306                                 free_cell( p_cell );
307                         }
308                 }
309                 shm_free(tm_table);
310         }
311 }
312
313
314
315
316 /*
317  */
318 struct s_table* init_hash_table()
319 {
320         int              i;
321
322         /*allocs the table*/
323         tm_table= (struct s_table*)shm_malloc( sizeof( struct s_table ) );
324         if ( !tm_table) {
325                 LOG(L_ERR, "ERROR: init_hash_table: no shmem for TM table\n");
326                 goto error0;
327         }
328
329         memset( tm_table, 0, sizeof (struct s_table ) );
330
331         /* try first allocating all the structures needed for syncing */
332         if (lock_initialize()==-1)
333                 goto error1;
334
335         /* inits the entrys */
336         for(  i=0 ; i<TABLE_ENTRIES; i++ )
337         {
338                 init_entry_lock( tm_table, (tm_table->entrys)+i );
339                 tm_table->entrys[i].next_label = rand();
340         }
341
342         return  tm_table;
343
344 error1:
345         free_hash_table( );
346 error0:
347         return 0;
348 }
349
350
351
352
353 /*  Takes an already created cell and links it into hash table on the
354  *  appropiate entry. */
355 void insert_into_hash_table_unsafe( struct cell * p_cell, unsigned int _hash )
356 {
357         struct entry* p_entry;
358
359         p_cell->hash_index=_hash;
360
361         /* locates the apropiate entry */
362         p_entry = &tm_table->entrys[ _hash ];
363
364         p_cell->label = p_entry->next_label++;
365         if ( p_entry->last_cell )
366         {
367                 p_entry->last_cell->next_cell = p_cell;
368                 p_cell->prev_cell = p_entry->last_cell;
369         } else p_entry->first_cell = p_cell;
370
371         p_entry->last_cell = p_cell;
372
373         /* update stats */
374         p_entry->cur_entries++;
375         p_entry->acc_entries++;
376         t_stats_new( is_local(p_cell) );
377 }
378
379
380
381 #ifdef _OBSOLETED
382 void insert_into_hash_table( struct cell * p_cell)
383 {
384         LOCK_HASH(p_cell->hash_index);
385         insert_into_hash_table_unsafe(  p_cell );
386         UNLOCK_HASH(p_cell->hash_index);
387 }
388 #endif
389
390
391
392
393 /*  Un-link a  cell from hash_table, but the cell itself is not released */
394 void remove_from_hash_table_unsafe( struct cell * p_cell)
395 {
396         struct entry*  p_entry  = &(tm_table->entrys[p_cell->hash_index]);
397
398         /* unlink the cell from entry list */
399         /* lock( &(p_entry->mutex) ); */
400
401         if ( p_cell->prev_cell )
402                 p_cell->prev_cell->next_cell = p_cell->next_cell;
403         else
404                 p_entry->first_cell = p_cell->next_cell;
405
406         if ( p_cell->next_cell )
407                 p_cell->next_cell->prev_cell = p_cell->prev_cell;
408         else
409                 p_entry->last_cell = p_cell->prev_cell;
410         /* update stats */
411 #       ifdef EXTRA_DEBUG
412         if (p_entry->cur_entries==0) {
413                 LOG(L_CRIT, "BUG: bad things happened: cur_entries=0\n");
414                 abort();
415         }
416 #       endif
417         p_entry->cur_entries--;
418         t_stats_deleted( is_local(p_cell) );
419
420         /* unlock( &(p_entry->mutex) ); */
421 }
422
423 /* print accumulated distribution of the hash table */
424 int fifo_hash( FILE *stream, char *response_file )
425 {
426         FILE *reply_file;
427         unsigned int i;
428
429         reply_file=open_reply_pipe(response_file);
430         if (reply_file==0) {
431                 LOG(L_ERR, "ERROR: fifo_hash: file '%s' not opened\n", 
432                         response_file);
433                 return -1;
434         }
435         fputs( "200 ok\n\tcurrent\ttotal\n", reply_file);
436         for (i=0; i<TABLE_ENTRIES; i++) {
437                 fprintf(reply_file, "%d.\t%lu\t%lu\n", 
438                         i, tm_table->entrys[i].cur_entries ,
439                         tm_table->entrys[i].acc_entries );
440         }
441         fclose(reply_file);
442         return 1;
443 }
444
445
446 int unixsock_hash(str* msg)
447 {
448         unsigned int i, ret;
449
450         ret = 0;
451         unixsock_reply_asciiz( "200 OK\n\tcurrent\ttotal\n");
452
453         for (i = 0; i < TABLE_ENTRIES; i++) {
454                 if (unixsock_reply_printf("%d.\t%lu\t%lu\n", 
455                                           i, tm_table->entrys[i].cur_entries,
456                                           tm_table->entrys[i].acc_entries
457                                           ) < 0) {
458                         unixsock_reply_reset();
459                         unixsock_reply_asciiz("500 Error while creating reply\n");
460                         ret = -1;
461                         break;
462                 }
463         }
464
465         if (unixsock_reply_send() < 0) {
466                 ret = -1;
467         }
468         return ret;
469 }