Merge branch 'master' into treimann/acc-cdr
[sip-router] / modules_k / dialog / dlg_hash.c
1 /*
2  * Copyright (C) 2006 Voice System SRL
3  * Copyright (C) 2011 Carsten Bock, carsten@ng-voice.com
4  *
5  * This file is part of Kamailio, a free SIP server.
6  *
7  * Kamailio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version
11  *
12  * Kamailio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License 
18  * along with this program; if not, write to the Free Software 
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * History:
22  * --------
23  * 2006-04-14  initial version (bogdan)
24  * 2007-03-06  syncronized state machine added for dialog state. New tranzition
25  *             design based on events; removed num_1xx and num_2xx (bogdan)
26  * 2007-04-30  added dialog matching without DID (dialog ID), but based only
27  *             on RFC3261 elements - based on an original patch submitted 
28  *             by Michel Bensoussan <michel@extricom.com> (bogdan)
29  * 2007-07-06  additional information stored in order to save it in the db:
30  *             cseq, route_set, contact and sock_info for both caller and 
31  *             callee (ancuta)
32  * 2007-07-10  Optimized dlg_match_mode 2 (DID_NONE), it now employs a proper
33  *             hash table lookup and isn't dependant on the is_direction 
34  *             function (which requires an RR param like dlg_match_mode 0 
35  *             anyways.. ;) ; based on a patch from 
36  *             Tavis Paquette <tavis@galaxytelecom.net> 
37  *             and Peter Baer <pbaer@galaxytelecom.net>  (bogdan)
38  * 2008-04-17  added new type of callback to be triggered right before the
39  *              dialog is destroyed (deleted from memory) (bogdan)
40  * 2008-04-17  added new dialog flag to avoid state tranzitions from DELETED to
41  *             CONFIRMED_NA due delayed "200 OK" (bogdan)
42  */
43
44
45 /*!
46  * \file
47  * \brief Functions related to dialog creation and searching
48  * \ingroup dialog
49  * Module: \ref dialog
50  */
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "../../dprint.h"
56 #include "../../ut.h"
57 #include "../../lib/kcore/hash_func.h"
58 #include "../../lib/kmi/mi.h"
59 #include "dlg_timer.h"
60 #include "dlg_var.h"
61 #include "dlg_hash.h"
62 #include "dlg_profile.h"
63 #include "dlg_req_within.h"
64 #include "dlg_db_handler.h"
65
66 #define MAX_LDG_LOCKS  2048
67 #define MIN_LDG_LOCKS  2
68
69
70 /*! global dialog table */
71 struct dlg_table *d_table = 0;
72
73
74 /*!
75  * \brief Initialize the global dialog table
76  * \param size size of the table
77  * \return 0 on success, -1 on failure
78  */
79 int init_dlg_table(unsigned int size)
80 {
81         unsigned int n;
82         unsigned int i;
83
84         d_table = (struct dlg_table*)shm_malloc
85                 ( sizeof(struct dlg_table) + size*sizeof(struct dlg_entry));
86         if (d_table==0) {
87                 LM_ERR("no more shm mem (1)\n");
88                 goto error0;
89         }
90
91         memset( d_table, 0, sizeof(struct dlg_table) );
92         d_table->size = size;
93         d_table->entries = (struct dlg_entry*)(d_table+1);
94
95         n = (size<MAX_LDG_LOCKS)?size:MAX_LDG_LOCKS;
96         for(  ; n>=MIN_LDG_LOCKS ; n-- ) {
97                 d_table->locks = lock_set_alloc(n);
98                 if (d_table->locks==0)
99                         continue;
100                 if (lock_set_init(d_table->locks)==0) {
101                         lock_set_dealloc(d_table->locks);
102                         d_table->locks = 0;
103                         continue;
104                 }
105                 d_table->locks_no = n;
106                 break;
107         }
108
109         if (d_table->locks==0) {
110                 LM_ERR("unable to allocted at least %d locks for the hash table\n",
111                         MIN_LDG_LOCKS);
112                 goto error1;
113         }
114
115         for( i=0 ; i<size; i++ ) {
116                 memset( &(d_table->entries[i]), 0, sizeof(struct dlg_entry) );
117                 d_table->entries[i].next_id = rand();
118                 d_table->entries[i].lock_idx = i % d_table->locks_no;
119         }
120
121         return 0;
122 error1:
123         shm_free( d_table );
124 error0:
125         return -1;
126 }
127
128
129 /*!
130  * \brief Destroy a dialog, run callbacks and free memory
131  * \param dlg destroyed dialog
132  */
133 inline void destroy_dlg(struct dlg_cell *dlg)
134 {
135         int ret = 0;
136         struct dlg_var *var;
137
138         LM_DBG("destroying dialog %p\n",dlg);
139
140         ret = remove_dialog_timer(&dlg->tl);
141         if (ret < 0) {
142                 LM_CRIT("unable to unlink the timer on dlg %p [%u:%u] "
143                         "with clid '%.*s' and tags '%.*s' '%.*s'\n",
144                         dlg, dlg->h_entry, dlg->h_id,
145                         dlg->callid.len, dlg->callid.s,
146                         dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
147                         dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
148         } else if (ret > 0) {
149                 LM_DBG("removed timer for dlg %p [%u:%u] "
150                         "with clid '%.*s' and tags '%.*s' '%.*s'\n",
151                         dlg, dlg->h_entry, dlg->h_id,
152                         dlg->callid.len, dlg->callid.s,
153                         dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
154                         dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
155         }
156
157         run_dlg_callbacks( DLGCB_DESTROY , dlg, NULL, NULL, DLG_DIR_NONE, 0);
158
159
160         /* delete the dialog from DB*/
161         if (dlg_db_mode)
162                 remove_dialog_from_db(dlg);
163
164         if(dlg==get_current_dlg_pointer())
165                 reset_current_dlg_pointer();
166
167         if (dlg->cbs.first)
168                 destroy_dlg_callbacks_list(dlg->cbs.first);
169
170         if (dlg->profile_links)
171                 destroy_linkers(dlg->profile_links);
172
173         if (dlg->tag[DLG_CALLER_LEG].s)
174                 shm_free(dlg->tag[DLG_CALLER_LEG].s);
175
176         if (dlg->tag[DLG_CALLEE_LEG].s)
177                 shm_free(dlg->tag[DLG_CALLEE_LEG].s);
178
179         if (dlg->cseq[DLG_CALLER_LEG].s)
180                 shm_free(dlg->cseq[DLG_CALLER_LEG].s);
181
182         if (dlg->cseq[DLG_CALLEE_LEG].s)
183                 shm_free(dlg->cseq[DLG_CALLEE_LEG].s);
184
185         if (dlg->toroute_name.s)
186                 shm_free(dlg->toroute_name.s);
187
188         
189         while (dlg->vars) {
190                 var = dlg->vars;
191                 dlg->vars = dlg->vars->next;
192                 shm_free(var->key.s);
193                 shm_free(var->value.s);
194                 shm_free(var);
195         }
196
197
198         shm_free(dlg);
199         dlg = 0;
200 }
201
202
203 /*!
204  * \brief Destroy the global dialog table
205  */
206 void destroy_dlg_table(void)
207 {
208         struct dlg_cell *dlg, *l_dlg;
209         unsigned int i;
210
211         if (d_table==0)
212                 return;
213
214         if (d_table->locks) {
215                 lock_set_destroy(d_table->locks);
216                 lock_set_dealloc(d_table->locks);
217         }
218
219         for( i=0 ; i<d_table->size; i++ ) {
220                 dlg = d_table->entries[i].first;
221                 while (dlg) {
222                         l_dlg = dlg;
223                         dlg = dlg->next;
224                         destroy_dlg(l_dlg);
225                 }
226
227         }
228
229         shm_free(d_table);
230         d_table = 0;
231
232         return;
233 }
234
235
236 /*!
237  * \brief Create a new dialog structure for a SIP dialog
238  * \param callid dialog callid
239  * \param from_uri dialog from uri
240  * \param to_uri dialog to uri
241  * \param from_tag dialog from tag
242  * \param req_uri dialog r-uri
243  * \return created dialog structure on success, NULL otherwise
244  */
245 struct dlg_cell* build_new_dlg( str *callid, str *from_uri, str *to_uri,
246                 str *from_tag, str *req_uri)
247 {
248         struct dlg_cell *dlg;
249         int len;
250         char *p;
251
252         len = sizeof(struct dlg_cell) + callid->len + from_uri->len +
253                 to_uri->len + req_uri->len;
254         dlg = (struct dlg_cell*)shm_malloc( len );
255         if (dlg==0) {
256                 LM_ERR("no more shm mem (%d)\n",len);
257                 return 0;
258         }
259
260         memset( dlg, 0, len);
261         dlg->state = DLG_STATE_UNCONFIRMED;
262
263         dlg->h_entry = core_hash( callid, 0, d_table->size);
264         LM_DBG("new dialog on hash %u\n",dlg->h_entry);
265
266         p = (char*)(dlg+1);
267
268         dlg->callid.s = p;
269         dlg->callid.len = callid->len;
270         memcpy( p, callid->s, callid->len);
271         p += callid->len;
272
273         dlg->from_uri.s = p;
274         dlg->from_uri.len = from_uri->len;
275         memcpy( p, from_uri->s, from_uri->len);
276         p += from_uri->len;
277
278         dlg->to_uri.s = p;
279         dlg->to_uri.len = to_uri->len;
280         memcpy( p, to_uri->s, to_uri->len);
281         p += to_uri->len; 
282
283         dlg->req_uri.s = p;
284         dlg->req_uri.len = req_uri->len;
285         memcpy( p, req_uri->s, req_uri->len);
286         p += req_uri->len;
287
288         if ( p!=(((char*)dlg)+len) ) {
289                 LM_CRIT("buffer overflow\n");
290                 shm_free(dlg);
291                 return 0;
292         }
293
294         return dlg;
295 }
296
297
298 /*!
299  * \brief Set the leg information for an existing dialog
300  * \param dlg dialog
301  * \param tag from tag or to tag
302  * \param rr record-routing information
303  * \param contact caller or callee contact
304  * \param cseq CSEQ of caller or callee
305  * \param leg must be either DLG_CALLER_LEG, or DLG_CALLEE_LEG
306  * \return 0 on success, -1 on failure
307  */
308 int dlg_set_leg_info(struct dlg_cell *dlg, str* tag, str *rr, str *contact,
309                                         str *cseq, unsigned int leg)
310 {
311         char *p;
312
313         dlg->tag[leg].s = (char*)shm_malloc( tag->len + rr->len + contact->len );
314         dlg->cseq[leg].s = (char*)shm_malloc( cseq->len );
315         if ( dlg->tag[leg].s==NULL || dlg->cseq[leg].s==NULL) {
316                 LM_ERR("no more shm mem\n");
317                 if (dlg->tag[leg].s)
318                 {
319                         shm_free(dlg->tag[leg].s);
320                         dlg->tag[leg].s = NULL;
321                 }
322                 if (dlg->cseq[leg].s)
323                 {
324                         shm_free(dlg->cseq[leg].s);
325                         dlg->cseq[leg].s = NULL;
326                 }
327                 return -1;
328         }
329         p = dlg->tag[leg].s;
330
331         /* tag */
332         dlg->tag[leg].len = tag->len;
333         memcpy( p, tag->s, tag->len);
334         p += tag->len;
335         /* contact */
336         dlg->contact[leg].s = p;
337         dlg->contact[leg].len = contact->len;
338         memcpy( p, contact->s, contact->len);
339         p += contact->len;
340         /* rr */
341         if (rr->len) {
342                 dlg->route_set[leg].s = p;
343                 dlg->route_set[leg].len = rr->len;
344                 memcpy( p, rr->s, rr->len);
345         }
346
347         /* cseq */
348         dlg->cseq[leg].len = cseq->len;
349         memcpy( dlg->cseq[leg].s, cseq->s, cseq->len);
350
351         return 0;
352 }
353
354
355 /*!
356  * \brief Update or set the CSEQ for an existing dialog
357  * \param dlg dialog
358  * \param leg must be either DLG_CALLER_LEG, or DLG_CALLEE_LEG
359  * \param cseq CSEQ of caller or callee
360  * \return 0 on success, -1 on failure
361  */
362 int dlg_update_cseq(struct dlg_cell * dlg, unsigned int leg, str *cseq)
363 {
364         if ( dlg->cseq[leg].s ) {
365                 if (dlg->cseq[leg].len < cseq->len) {
366                         shm_free(dlg->cseq[leg].s);
367                         dlg->cseq[leg].s = (char*)shm_malloc(cseq->len);
368                         if (dlg->cseq[leg].s==NULL)
369                                 goto error;
370                 }
371         } else {
372                 dlg->cseq[leg].s = (char*)shm_malloc(cseq->len);
373                 if (dlg->cseq[leg].s==NULL)
374                         goto error;
375         }
376
377         memcpy( dlg->cseq[leg].s, cseq->s, cseq->len );
378         dlg->cseq[leg].len = cseq->len;
379
380         LM_DBG("cseq is %.*s\n", dlg->cseq[leg].len, dlg->cseq[leg].s);
381         return 0;
382 error:
383         LM_ERR("not more shm mem\n");
384         return -1;
385 }
386
387
388 /*!
389  * \brief Lookup a dialog in the global list
390  * \param h_entry number of the hash table entry
391  * \param h_id id of the hash table entry
392  * \param del will set to 1 if dialog is deleted
393  * \return dialog structure on success, NULL on failure or if not found
394  */
395 struct dlg_cell* lookup_dlg( unsigned int h_entry, unsigned int h_id, unsigned int *del)
396 {
397         struct dlg_cell *dlg;
398         struct dlg_entry *d_entry;
399
400         if (del != NULL)
401                 *del = 0;
402
403         if (h_entry>=d_table->size)
404                 goto not_found;
405
406         d_entry = &(d_table->entries[h_entry]);
407
408         dlg_lock( d_table, d_entry);
409
410         for( dlg=d_entry->first ; dlg ; dlg=dlg->next ) {
411                 if (dlg->h_id == h_id) {
412                         if (dlg->state==DLG_STATE_DELETED) {
413                                 if (del != NULL)
414                                         *del = 1;
415                                 dlg_unlock( d_table, d_entry);
416                                 goto not_found;
417                         }
418                         dlg->ref++;
419                         LM_DBG("ref dlg %p with 1 -> %d\n", dlg, dlg->ref);
420                         dlg_unlock( d_table, d_entry);
421                         LM_DBG("dialog id=%u found on entry %u\n", h_id, h_entry);
422                         return dlg;
423                 }
424         }
425
426         dlg_unlock( d_table, d_entry);
427 not_found:
428         LM_DBG("no dialog id=%u found on entry %u\n", h_id, h_entry);
429         return 0;
430 }
431
432
433 /*!
434  * \brief Helper function to get a dialog corresponding to a SIP message
435  * \see get_dlg
436  * \param callid callid
437  * \param ftag from tag
438  * \param ttag to tag
439  * \param dir direction
440  * \param del will set to 1 if dialog is deleted
441  * \return dialog structure on success, NULL on failure or if not found
442  */
443 static inline struct dlg_cell* internal_get_dlg(unsigned int h_entry,
444                                                 str *callid, str *ftag, str *ttag, unsigned int *dir,
445                                                 unsigned int *del)
446 {
447         struct dlg_cell *dlg;
448         struct dlg_entry *d_entry;
449
450         if (del != NULL)
451                 *del = 0;
452
453         d_entry = &(d_table->entries[h_entry]);
454
455         dlg_lock( d_table, d_entry);
456
457         for( dlg = d_entry->first ; dlg ; dlg = dlg->next ) {
458                 /* Check callid / fromtag / totag */
459                 if (match_dialog( dlg, callid, ftag, ttag, dir)==1) {
460                         if (dlg->state==DLG_STATE_DELETED) {
461                                 if (del != NULL)
462                                         *del = 1;
463                                 dlg_unlock( d_table, d_entry);
464                                 goto not_found;
465                         }
466                         dlg->ref++;
467                         LM_DBG("ref dlg %p with 1 -> %d\n", dlg, dlg->ref);
468                         dlg_unlock( d_table, d_entry);
469                         LM_DBG("dialog callid='%.*s' found\n on entry %u, dir=%d\n",
470                                 callid->len, callid->s,h_entry,*dir);
471                         return dlg;
472                 }
473         }
474
475         dlg_unlock( d_table, d_entry);
476
477 not_found:
478         LM_DBG("no dialog callid='%.*s' found\n", callid->len, callid->s);
479         return 0;
480 }
481
482
483
484 /*!
485  * \brief Get dialog that correspond to CallId, From Tag and To Tag
486  *
487  * Get dialog that correspond to CallId, From Tag and To Tag.
488  * See RFC 3261, paragraph 4. Overview of Operation:                 
489  * "The combination of the To tag, From tag, and Call-ID completely  
490  * defines a peer-to-peer SIP relationship between [two UAs] and is 
491  * referred to as a dialog."
492  * \param callid callid
493  * \param ftag from tag
494  * \param ttag to tag
495  * \param dir direction
496  * \param del deleted dialog information
497  * \return dialog structure on success, NULL on failure
498  */
499 struct dlg_cell* get_dlg( str *callid, str *ftag, str *ttag, unsigned int *dir,
500                 unsigned int *del)
501 {
502         struct dlg_cell *dlg;
503
504         if ((dlg = internal_get_dlg(core_hash(callid, 0,
505                         d_table->size), callid, ftag, ttag, dir, del)) == 0 &&
506                         (dlg = internal_get_dlg(core_hash(callid, ttag->len
507                         ?ttag:0, d_table->size), callid, ftag, ttag, dir, del)) == 0) {
508                 LM_DBG("no dialog callid='%.*s' found\n", callid->len, callid->s);
509                 return 0;
510         }
511         return dlg;
512 }
513
514
515 /*!
516  * \brief Link a dialog structure
517  * \param dlg dialog
518  * \param n extra increments for the reference counter
519  */
520 void link_dlg(struct dlg_cell *dlg, int n)
521 {
522         struct dlg_entry *d_entry;
523
524         d_entry = &(d_table->entries[dlg->h_entry]);
525
526         dlg_lock( d_table, d_entry);
527
528         dlg->h_id = d_entry->next_id++;
529         if (d_entry->first==0) {
530                 d_entry->first = d_entry->last = dlg;
531         } else {
532                 d_entry->last->next = dlg;
533                 dlg->prev = d_entry->last;
534                 d_entry->last = dlg;
535         }
536
537         dlg->ref += 1 + n;
538
539         LM_DBG("ref dlg %p with %d -> %d\n", dlg, n+1, dlg->ref);
540
541         dlg_unlock( d_table, d_entry);
542         return;
543 }
544
545
546 /*!
547  * \brief Reference a dialog without locking
548  * \param _dlg dialog
549  * \param _cnt increment for the reference counter
550  */
551 #define ref_dlg_unsafe(_dlg,_cnt)     \
552         do { \
553                 (_dlg)->ref += (_cnt); \
554                 LM_DBG("ref dlg %p with %d -> %d\n", \
555                         (_dlg),(_cnt),(_dlg)->ref); \
556         }while(0)
557
558
559 /*!
560  * \brief Unreference a dialog without locking
561  * \param _dlg dialog
562  * \param _cnt decrement for the reference counter
563  * \param _d_entry dialog entry
564  */
565 #define unref_dlg_unsafe(_dlg,_cnt,_d_entry)   \
566         do { \
567                 (_dlg)->ref -= (_cnt); \
568                 LM_DBG("unref dlg %p with %d -> %d\n",\
569                         (_dlg),(_cnt),(_dlg)->ref);\
570                 if ((_dlg)->ref<0) {\
571                         LM_CRIT("bogus ref %d with cnt %d for dlg %p [%u:%u] "\
572                                 "with clid '%.*s' and tags '%.*s' '%.*s'\n",\
573                                 (_dlg)->ref, _cnt, _dlg,\
574                                 (_dlg)->h_entry, (_dlg)->h_id,\
575                                 (_dlg)->callid.len, (_dlg)->callid.s,\
576                                 (_dlg)->tag[DLG_CALLER_LEG].len,\
577                                 (_dlg)->tag[DLG_CALLER_LEG].s,\
578                                 (_dlg)->tag[DLG_CALLEE_LEG].len,\
579                                 (_dlg)->tag[DLG_CALLEE_LEG].s); \
580                 }\
581                 if ((_dlg)->ref<=0) { \
582                         unlink_unsafe_dlg( _d_entry, _dlg);\
583                         LM_DBG("ref <=0 for dialog %p\n",_dlg);\
584                         destroy_dlg(_dlg);\
585                 }\
586         }while(0)
587
588
589 /*!
590  * \brief Refefence a dialog with locking
591  * \see ref_dlg_unsafe
592  * \param dlg dialog
593  * \param cnt increment for the reference counter
594  */
595 void ref_dlg(struct dlg_cell *dlg, unsigned int cnt)
596 {
597         struct dlg_entry *d_entry;
598
599         d_entry = &(d_table->entries[dlg->h_entry]);
600
601         dlg_lock( d_table, d_entry);
602         ref_dlg_unsafe( dlg, cnt);
603         dlg_unlock( d_table, d_entry);
604 }
605
606
607 /*!
608  * \brief Unreference a dialog with locking
609  * \see unref_dlg_unsafe
610  * \param dlg dialog
611  * \param cnt decrement for the reference counter
612  */
613 void unref_dlg(struct dlg_cell *dlg, unsigned int cnt)
614 {
615         struct dlg_entry *d_entry;
616
617         d_entry = &(d_table->entries[dlg->h_entry]);
618
619         dlg_lock( d_table, d_entry);
620         unref_dlg_unsafe( dlg, cnt, d_entry);
621         dlg_unlock( d_table, d_entry);
622 }
623
624
625 /*!
626  * Small logging helper functions for next_state_dlg.
627  * \param event logged event
628  * \param dlg dialog data
629  * \see next_state_dlg
630  */
631 static inline void log_next_state_dlg(const int event, const struct dlg_cell *dlg) {
632         LM_CRIT("bogus event %d in state %d for dlg %p [%u:%u] with clid '%.*s' and tags "
633                 "'%.*s' '%.*s'\n", event, dlg->state, dlg, dlg->h_entry, dlg->h_id,
634                 dlg->callid.len, dlg->callid.s,
635                 dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
636                 dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
637 }
638
639
640 /*!
641  * \brief Update a dialog state according a event and the old state
642  *
643  * This functions implement the main state machine that update a dialog
644  * state according a processed event and the current state. If necessary
645  * it will delete the processed dialog. The old and new state are also
646  * saved for reference.
647  * \param dlg updated dialog
648  * \param event current event
649  * \param old_state old dialog state
650  * \param new_state new dialog state
651  * \param unref set to 1 when the dialog was deleted, 0 otherwise
652  */
653 void next_state_dlg(struct dlg_cell *dlg, int event,
654                 int *old_state, int *new_state, int *unref)
655 {
656         struct dlg_entry *d_entry;
657
658         d_entry = &(d_table->entries[dlg->h_entry]);
659
660         *unref = 0;
661
662         dlg_lock( d_table, d_entry);
663
664         *old_state = dlg->state;
665
666         switch (event) {
667                 case DLG_EVENT_TDEL:
668                         switch (dlg->state) {
669                                 case DLG_STATE_UNCONFIRMED:
670                                 case DLG_STATE_EARLY:
671                                         dlg->state = DLG_STATE_DELETED;
672                                         unref_dlg_unsafe(dlg,1,d_entry);
673                                         *unref = 1;
674                                         break;
675                                 case DLG_STATE_CONFIRMED_NA:
676                                 case DLG_STATE_CONFIRMED:
677                                         unref_dlg_unsafe(dlg,1,d_entry);
678                                         break;
679                                 case DLG_STATE_DELETED:
680                                         *unref = 1;
681                                         break;
682                                 default:
683                                         log_next_state_dlg(event, dlg);
684                         }
685                         break;
686                 case DLG_EVENT_RPL1xx:
687                         switch (dlg->state) {
688                                 case DLG_STATE_UNCONFIRMED:
689                                 case DLG_STATE_EARLY:
690                                         dlg->state = DLG_STATE_EARLY;
691                                         break;
692                                 default:
693                                         log_next_state_dlg(event, dlg);
694                         }
695                         break;
696                 case DLG_EVENT_RPL3xx:
697                         switch (dlg->state) {
698                                 case DLG_STATE_UNCONFIRMED:
699                                 case DLG_STATE_EARLY:
700                                         dlg->state = DLG_STATE_DELETED;
701                                         *unref = 1;
702                                         break;
703                                 default:
704                                         log_next_state_dlg(event, dlg);
705                         }
706                         break;
707                 case DLG_EVENT_RPL2xx:
708                         switch (dlg->state) {
709                                 case DLG_STATE_DELETED:
710                                         if (dlg->dflags&DLG_FLAG_HASBYE) {
711                                                 LM_CRIT("bogus event %d in state %d (with BYE) "
712                                                         "for dlg %p [%u:%u] with clid '%.*s' and tags '%.*s' '%.*s'\n",
713                                                         event, dlg->state, dlg, dlg->h_entry, dlg->h_id,
714                                                         dlg->callid.len, dlg->callid.s,
715                                                         dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
716                                                         dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
717                                                 break;
718                                         }
719                                         ref_dlg_unsafe(dlg,1);
720                                 case DLG_STATE_UNCONFIRMED:
721                                 case DLG_STATE_EARLY:
722                                         dlg->state = DLG_STATE_CONFIRMED_NA;
723                                         break;
724                                 case DLG_STATE_CONFIRMED_NA:
725                                 case DLG_STATE_CONFIRMED:
726                                         break;
727                                 default:
728                                         log_next_state_dlg(event, dlg);
729                         }
730                         break;
731                 case DLG_EVENT_REQACK:
732                         switch (dlg->state) {
733                                 case DLG_STATE_CONFIRMED_NA:
734                                         dlg->state = DLG_STATE_CONFIRMED;
735                                         break;
736                                 case DLG_STATE_CONFIRMED:
737                                         break;
738                                 case DLG_STATE_DELETED:
739                                         break;
740                                 default:
741                                         log_next_state_dlg(event, dlg);
742                         }
743                         break;
744                 case DLG_EVENT_REQBYE:
745                         switch (dlg->state) {
746                                 case DLG_STATE_CONFIRMED_NA:
747                                 case DLG_STATE_CONFIRMED:
748                                         dlg->dflags |= DLG_FLAG_HASBYE;
749                                         dlg->state = DLG_STATE_DELETED;
750                                         *unref = 1;
751                                         break;
752                                 case DLG_STATE_EARLY:
753                                 case DLG_STATE_DELETED:
754                                         break;
755                                 default:
756                                         log_next_state_dlg(event, dlg);
757                         }
758                         break;
759                 case DLG_EVENT_REQPRACK:
760                         switch (dlg->state) {
761                                 case DLG_STATE_EARLY:
762                                 case DLG_STATE_CONFIRMED_NA:
763                                         break;
764                                 default:
765                                         log_next_state_dlg(event, dlg);
766                         }
767                         break;
768                 case DLG_EVENT_REQ:
769                         switch (dlg->state) {
770                                 case DLG_STATE_EARLY:
771                                 case DLG_STATE_CONFIRMED_NA:
772                                 case DLG_STATE_CONFIRMED:
773                                         break;
774                                 default:
775                                         log_next_state_dlg(event, dlg);
776                         }
777                         break;
778                 default:
779                         LM_CRIT("unknown event %d in state %d "
780                                 "for dlg %p [%u:%u] with clid '%.*s' and tags '%.*s' '%.*s'\n",
781                                 event, dlg->state, dlg, dlg->h_entry, dlg->h_id,
782                                 dlg->callid.len, dlg->callid.s,
783                                 dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
784                                 dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
785         }
786         *new_state = dlg->state;
787
788         dlg_unlock( d_table, d_entry);
789
790         LM_DBG("dialog %p changed from state %d to "
791                 "state %d, due event %d\n",dlg,*old_state,*new_state,event);
792 }
793
794 /**
795  *
796  */
797 int dlg_set_toroute(struct dlg_cell *dlg, str *route)
798 {
799         if(dlg==NULL || route==NULL || route->len<=0)
800                 return 0;
801         if(dlg->toroute_name.s!=NULL) {
802                 shm_free(dlg->toroute_name.s);
803                 dlg->toroute_name.s = NULL;
804                 dlg->toroute_name.len = 0;
805         }
806         dlg->toroute_name.s = (char*)shm_malloc((route->len+1)*sizeof(char));
807         if(dlg->toroute_name.s==NULL) {
808                 LM_ERR("no more shared memory\n");
809                 return -1;
810         }
811         memcpy(dlg->toroute_name.s, route->s, route->len);
812         dlg->toroute_name.len = route->len;
813         dlg->toroute_name.s[dlg->toroute_name.len] = '\0';
814         dlg->toroute = route_lookup(&main_rt, dlg->toroute_name.s);
815         return 0;
816 }
817
818 /**************************** MI functions ******************************/
819 /*!
820  * \brief Helper method that output a dialog via the MI interface
821  * \see mi_print_dlg
822  * \param rpl MI node that should be filled
823  * \param dlg printed dialog
824  * \param with_context if 1 then the dialog context will be also printed
825  * \return 0 on success, -1 on failure
826  */
827 static inline int internal_mi_print_dlg(struct mi_node *rpl,
828                                                                         struct dlg_cell *dlg, int with_context)
829 {
830         struct mi_node* node= NULL;
831         struct mi_node* node1 = NULL;
832         struct mi_attr* attr= NULL;
833         int len;
834         char* p;
835
836         node = add_mi_node_child(rpl, 0, "dialog",6 , 0, 0 );
837         if (node==0)
838                 goto error;
839
840         attr = addf_mi_attr( node, 0, "hash", 4, "%u:%u",
841                         dlg->h_entry, dlg->h_id );
842         if (attr==0)
843                 goto error;
844
845         p= int2str((unsigned long)dlg->state, &len);
846         node1 = add_mi_node_child( node, MI_DUP_VALUE, "state", 5, p, len);
847         if (node1==0)
848                 goto error;
849
850         p= int2str((unsigned long)dlg->ref, &len);
851         node1 = add_mi_node_child( node, MI_DUP_VALUE, "ref_count", 9, p, len);
852         if (node1==0)
853                 goto error;
854
855         p= int2str((unsigned long)dlg->start_ts, &len);
856         node1 = add_mi_node_child(node,MI_DUP_VALUE,"timestart",9, p, len);
857         if (node1==0)
858                 goto error;
859
860         p= int2str((unsigned long)dlg->tl.timeout, &len);
861         node1 = add_mi_node_child(node,MI_DUP_VALUE, "timeout", 7, p, len);
862         if (node1==0)
863                 goto error;
864
865         node1 = add_mi_node_child(node, MI_DUP_VALUE, "callid", 6,
866                         dlg->callid.s, dlg->callid.len);
867         if(node1 == 0)
868                 goto error;
869
870         node1 = add_mi_node_child(node, MI_DUP_VALUE, "from_uri", 8,
871                         dlg->from_uri.s, dlg->from_uri.len);
872         if(node1 == 0)
873                 goto error;
874
875         node1 = add_mi_node_child(node, MI_DUP_VALUE, "from_tag", 8,
876                         dlg->tag[DLG_CALLER_LEG].s, dlg->tag[DLG_CALLER_LEG].len);
877         if(node1 == 0)
878                 goto error;
879
880         node1 = add_mi_node_child(node, MI_DUP_VALUE, "caller_contact", 14,
881                         dlg->contact[DLG_CALLER_LEG].s,
882                         dlg->contact[DLG_CALLER_LEG].len);
883         if(node1 == 0)
884                 goto error;
885
886         node1 = add_mi_node_child(node, MI_DUP_VALUE, "caller_cseq", 11,
887                         dlg->cseq[DLG_CALLER_LEG].s,
888                         dlg->cseq[DLG_CALLER_LEG].len);
889         if(node1 == 0)
890                 goto error;
891
892         node1 = add_mi_node_child(node, MI_DUP_VALUE,"caller_route_set",16,
893                         dlg->route_set[DLG_CALLER_LEG].s,
894                         dlg->route_set[DLG_CALLER_LEG].len);
895         if(node1 == 0)
896                 goto error;
897
898         if (dlg->bind_addr[DLG_CALLER_LEG]) {
899                 node1 = add_mi_node_child(node, 0,
900                         "caller_bind_addr",16,
901                         dlg->bind_addr[DLG_CALLER_LEG]->sock_str.s,
902                         dlg->bind_addr[DLG_CALLER_LEG]->sock_str.len);
903         } else {
904                 node1 = add_mi_node_child(node, 0,
905                         "caller_bind_addr",16,0,0);
906         }
907
908         if (dlg->bind_addr[DLG_CALLEE_LEG]) {
909                 node1 = add_mi_node_child(node, 0,
910                         "callee_bind_addr",16,
911                         dlg->bind_addr[DLG_CALLEE_LEG]->sock_str.s,
912                         dlg->bind_addr[DLG_CALLEE_LEG]->sock_str.len);
913         } else {
914                 node1 = add_mi_node_child(node, 0,
915                         "callee_bind_addr",16,0,0);
916         }
917
918         node1 = add_mi_node_child(node, MI_DUP_VALUE, "to_uri", 6,
919                         dlg->to_uri.s, dlg->to_uri.len);
920         if(node1 == 0)
921                 goto error;
922
923         node1 = add_mi_node_child(node, MI_DUP_VALUE, "to_tag", 6,
924                         dlg->tag[DLG_CALLEE_LEG].s, dlg->tag[DLG_CALLEE_LEG].len);
925         if(node1 == 0)
926                 goto error;
927
928         node1 = add_mi_node_child(node, MI_DUP_VALUE, "callee_contact", 14,
929                         dlg->contact[DLG_CALLEE_LEG].s,
930                         dlg->contact[DLG_CALLEE_LEG].len);
931         if(node1 == 0)
932                 goto error;
933
934         node1 = add_mi_node_child(node, MI_DUP_VALUE, "callee_cseq", 11,
935                         dlg->cseq[DLG_CALLEE_LEG].s,
936                         dlg->cseq[DLG_CALLEE_LEG].len);
937         if(node1 == 0)
938                 goto error;
939
940         node1 = add_mi_node_child(node, MI_DUP_VALUE,"callee_route_set",16,
941                         dlg->route_set[DLG_CALLEE_LEG].s,
942                         dlg->route_set[DLG_CALLEE_LEG].len);
943         if(node1 == 0)
944                 goto error;
945
946         if(node1 == 0)
947                 goto error;
948
949         if (with_context) {
950                 node1 = add_mi_node_child(node, 0, "context", 7, 0, 0);
951                 if(node1 == 0)
952                         goto error;
953                 run_dlg_callbacks( DLGCB_MI_CONTEXT,
954                                    dlg,
955                                    NULL,
956                                    NULL,
957                                    DLG_DIR_NONE,
958                                    (void *)node1);
959         }
960         return 0;
961
962 error:
963         LM_ERR("failed to add node\n");
964         return -1;
965 }
966
967
968 /*!
969  * \brief Output a dialog via the MI interface
970  * \param rpl MI node that should be filled
971  * \param dlg printed dialog
972  * \param with_context if 1 then the dialog context will be also printed
973  * \return 0 on success, -1 on failure
974  */
975 int mi_print_dlg(struct mi_node *rpl, struct dlg_cell *dlg, int with_context)
976 {
977         return internal_mi_print_dlg( rpl, dlg, with_context);
978 }
979
980 /*!
981  * \brief Helper function that output all dialogs via the MI interface
982  * \see mi_print_dlgs
983  * \param rpl MI node that should be filled
984  * \param with_context if 1 then the dialog context will be also printed
985  * \return 0 on success, -1 on failure
986  */
987 static int internal_mi_print_dlgs(struct mi_node *rpl, int with_context)
988 {
989         struct dlg_cell *dlg;
990         unsigned int i;
991
992         LM_DBG("printing %i dialogs\n", d_table->size);
993
994         for( i=0 ; i<d_table->size ; i++ ) {
995                 dlg_lock( d_table, &(d_table->entries[i]) );
996
997                 for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next ) {
998                         if (internal_mi_print_dlg(rpl, dlg, with_context)!=0)
999                                 goto error;
1000                 }
1001                 dlg_unlock( d_table, &(d_table->entries[i]) );
1002         }
1003         return 0;
1004
1005 error:
1006         dlg_unlock( d_table, &(d_table->entries[i]) );
1007         LM_ERR("failed to print dialog\n");
1008         return -1;
1009 }
1010
1011
1012 static inline struct mi_root* process_mi_params(struct mi_root *cmd_tree,
1013                                                                                                         struct dlg_cell **dlg_p)
1014 {
1015         struct mi_node* node;
1016         struct dlg_entry *d_entry;
1017         struct dlg_cell *dlg;
1018         str *callid;
1019         str *from_tag;
1020         unsigned int h_entry;
1021
1022         node = cmd_tree->node.kids;
1023         if (node == NULL) {
1024                 /* no parameters at all */
1025                 *dlg_p = NULL;
1026                 return NULL;
1027         }
1028
1029         /* we have params -> get callid and fromtag */
1030         callid = &node->value;
1031         LM_DBG("callid='%.*s'\n", callid->len, callid->s);
1032
1033         node = node->next;
1034         if ( !node || !node->value.s || !node->value.len) {
1035                 from_tag = NULL;
1036         } else {
1037                 from_tag = &node->value;
1038                 LM_DBG("from_tag='%.*s'\n", from_tag->len, from_tag->s);
1039                 if ( node->next!=NULL )
1040                         return init_mi_tree( 400, MI_SSTR(MI_MISSING_PARM));
1041         }
1042
1043         h_entry = core_hash( callid, 0, d_table->size);
1044
1045         d_entry = &(d_table->entries[h_entry]);
1046         dlg_lock( d_table, d_entry);
1047
1048         for( dlg = d_entry->first ; dlg ; dlg = dlg->next ) {
1049                 if (match_downstream_dialog( dlg, callid, from_tag)==1) {
1050                         if (dlg->state==DLG_STATE_DELETED) {
1051                                 *dlg_p = NULL;
1052                                 break;
1053                         } else {
1054                                 *dlg_p = dlg;
1055                                 dlg_unlock( d_table, d_entry);
1056                                 return 0;
1057                         }
1058                 }
1059         }
1060         dlg_unlock( d_table, d_entry);
1061
1062         return init_mi_tree( 404, MI_SSTR("Nu such dialog"));
1063 }
1064
1065
1066 /*!
1067  * \brief Output all dialogs via the MI interface
1068  * \param cmd_tree MI command tree
1069  * \param param unused
1070  * \return mi node with the dialog information, or NULL on failure
1071  */
1072 struct mi_root * mi_print_dlgs(struct mi_root *cmd_tree, void *param )
1073 {
1074         struct mi_root* rpl_tree= NULL;
1075         struct mi_node* rpl = NULL;
1076         struct dlg_cell* dlg = NULL;
1077
1078         rpl_tree = process_mi_params( cmd_tree, &dlg);
1079         if (rpl_tree)
1080                 /* param error */
1081                 return rpl_tree;
1082
1083         rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
1084         if (rpl_tree==0)
1085                 return 0;
1086         rpl = &rpl_tree->node;
1087
1088         if (dlg==NULL) {
1089                 if ( internal_mi_print_dlgs(rpl,0)!=0 )
1090                         goto error;
1091         } else {
1092                 if ( internal_mi_print_dlg(rpl,dlg,0)!=0 )
1093                         goto error;
1094         }
1095
1096         return rpl_tree;
1097 error:
1098         free_mi_tree(rpl_tree);
1099         return NULL;
1100 }
1101
1102
1103 /*!
1104  * \brief Print a dialog context via the MI interface
1105  * \param cmd_tree MI command tree
1106  * \param param unused
1107  * \return mi node with the dialog information, or NULL on failure
1108  */
1109 struct mi_root * mi_print_dlgs_ctx(struct mi_root *cmd_tree, void *param )
1110 {
1111         struct mi_root* rpl_tree= NULL;
1112         struct mi_node* rpl = NULL;
1113         struct dlg_cell* dlg = NULL;
1114
1115         rpl_tree = process_mi_params( cmd_tree, &dlg);
1116         if (rpl_tree)
1117                 /* param error */
1118                 return rpl_tree;
1119
1120         rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
1121         if (rpl_tree==0)
1122                 return 0;
1123         rpl = &rpl_tree->node;
1124
1125         if (dlg==NULL) {
1126                 if ( internal_mi_print_dlgs(rpl,1)!=0 )
1127                         goto error;
1128         } else {
1129                 if ( internal_mi_print_dlg(rpl,dlg,1)!=0 )
1130                         goto error;
1131         }
1132
1133         return rpl_tree;
1134 error:
1135         free_mi_tree(rpl_tree);
1136         return NULL;
1137 }
1138
1139 /*!
1140  * \brief Terminate all or selected dialogs via the MI interface
1141  * \param cmd_tree MI command tree
1142  * \param param unused
1143  * \return mi node with the dialog information, or NULL on failure
1144  */
1145 struct mi_root * mi_terminate_dlgs(struct mi_root *cmd_tree, void *param )
1146 {
1147         struct mi_root* rpl_tree= NULL;
1148         struct dlg_cell* dlg = NULL;
1149         str headers = {0, 0};
1150
1151         rpl_tree = process_mi_params( cmd_tree, &dlg);
1152         if (rpl_tree)
1153                 /* param error */
1154                 return rpl_tree;
1155         if (dlg==NULL)
1156                 return init_mi_tree( 400, MI_SSTR(MI_MISSING_PARM));
1157
1158         rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
1159         if (rpl_tree==0)
1160                 return 0;
1161         if (dlg_bye_all(dlg, &headers)!=0)
1162                 goto error;
1163         return rpl_tree;
1164 error:
1165         free_mi_tree(rpl_tree);
1166         return NULL;
1167 }
1168
1169