File: acc_cdr.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (810 lines) | stat: -rw-r--r-- 21,465 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
 * Accounting module
 *
 * Copyright (C) 2011 - Sven Knoblich 1&1 Internet AG
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*! \file
 * \ingroup acc
 * \brief Acc:: File to handle CDR generation with the help of the dialog module
 *
 * - Module: \ref acc
 */

/*! \defgroup acc ACC :: The Kamailio accounting Module
 *
 * The ACC module is used to account transactions information to
 *  different backends like syslog, SQL, RADIUS and DIAMETER (beta
 *  version).
 *
 */
#include "../../modules/tm/tm_load.h"
#include "../../str.h"
#include "../dialog/dlg_load.h"

#include "acc_api.h"
#include "acc_cdr.h"
#include "acc_mod.h"
#include "acc_extra.h"
#include "acc.h"

#ifdef SQL_ACC
#include "../../lib/srdb1/db.h"
#endif

#include <sys/time.h>

/* Solaris does not provide timersub macro in <sys/time.h> */
#ifdef __OS_solaris
#define timersub(tvp, uvp, vvp)                     \
    do {                                \
        (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;      \
        (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;   \
        if ((vvp)->tv_usec < 0) {               \
            (vvp)->tv_sec--;                \
            (vvp)->tv_usec += 1000000;          \
        }                           \
    } while (0)
#endif // __OS_solaris

#define TIME_STR_BUFFER_SIZE 20
#define TIME_BUFFER_LENGTH 256

struct dlg_binds dlgb;
struct acc_extra* cdr_extra = NULL;
int cdr_facility = LOG_DAEMON;

static const str zero_duration = { "0", 1};
static const char time_separator = {'.'};
static char time_buffer[ TIME_BUFFER_LENGTH];
static const str empty_string = { "", 0};

// buffers which are used to collect the crd data for writing
static str cdr_attrs[ MAX_CDR_CORE + MAX_CDR_EXTRA];
static str cdr_value_array[ MAX_CDR_CORE + MAX_CDR_EXTRA];
static int cdr_int_array[ MAX_CDR_CORE + MAX_CDR_EXTRA];
static char cdr_type_array[ MAX_CDR_CORE + MAX_CDR_EXTRA];

extern struct tm_binds tmb;
extern str cdr_start_str;
extern str cdr_end_str;
extern str cdr_duration_str;
extern str acc_cdrs_table;
extern int cdr_log_enable;

/* write all basic information to buffers(e.g. start-time ...) */
static int cdr_core2strar( struct dlg_cell* dlg,
                           str* values,
                           int* unused,
                           char* types)
{
    str* start = NULL;
    str* end = NULL;
    str* duration = NULL;

    if( !dlg || !values || !types)
    {
        LM_ERR( "invalid input parameter!\n");
        return 0;
    }

    start = dlgb.get_dlg_var( dlg, (str*)&cdr_start_str);
    end = dlgb.get_dlg_var( dlg, (str*)&cdr_end_str);
    duration = dlgb.get_dlg_var( dlg, (str*)&cdr_duration_str);

    values[0] = ( start != NULL ? *start : empty_string);
    types[0] = ( start != NULL ? TYPE_STR : TYPE_NULL);

    values[1] = ( end != NULL ? *end : empty_string);
    types[1] = ( end != NULL ? TYPE_STR : TYPE_NULL);

    values[2] = ( duration != NULL ? *duration : empty_string);
    types[2] = ( duration != NULL ? TYPE_STR : TYPE_NULL);

    return MAX_CDR_CORE;
}

#ifdef SQL_ACC
/* caution: keys need to be aligned to core format */
static db_key_t db_cdr_keys[ MAX_CDR_CORE + MAX_CDR_EXTRA];
static db_val_t db_cdr_vals[ MAX_CDR_CORE + MAX_CDR_EXTRA];

/* collect all crd data and write it to a syslog */
static int db_write_cdr( struct dlg_cell* dialog,
                      struct sip_msg* message)
{
	int m = 0;
	int n = 0;
	int i;
	db_func_t *df=NULL;
	db1_con_t *dh=NULL;
	void *vf=NULL;
	void *vh=NULL;

	if(acc_cdrs_table.len<=0)
		return 0;

	if(acc_get_db_handlers(&vf, &vh)<0) {
		LM_ERR("cannot get db handlers\n");
		return -1;
	}
	df = (db_func_t*)vf;
	dh = (db1_con_t*)vh;

	/* get default values */
	m = cdr_core2strar( dialog,
						cdr_value_array,
						cdr_int_array,
						cdr_type_array);

	for(i=0; i<m; i++) {
		db_cdr_keys[i] = &cdr_attrs[i];
		VAL_TYPE(db_cdr_vals+i)=DB1_STR;
		VAL_NULL(db_cdr_vals+i)=0;
		VAL_STR(db_cdr_vals+i) = cdr_value_array[i];
	}

    /* get extra values */
    if (message)
    {
		n += extra2strar( cdr_extra,
							message,
							cdr_value_array + m,
							cdr_int_array + m,
							cdr_type_array + m);
		m += n;
    } else if (cdr_expired_dlg_enable){
        LM_WARN( "fallback to dlg_only search because of message doesn't exist.\n");
        m += extra2strar_dlg_only( cdr_extra,
                dialog,
                cdr_value_array + m,
                cdr_int_array + m,
                cdr_type_array +m,
                &dlgb);
    }

	for( ; i<m; i++) {
		db_cdr_keys[i] = &cdr_attrs[i];
		VAL_TYPE(db_cdr_vals+i)=DB1_STR;
		VAL_NULL(db_cdr_vals+i)=0;
		VAL_STR(db_cdr_vals+i) = cdr_value_array[i];
	}

	if (df->use_table(dh, &acc_cdrs_table /*table*/) < 0) {
		LM_ERR("error in use_table\n");
		goto error;
	}

	if(acc_db_insert_mode==1 && df->insert_delayed!=NULL) {
		if (df->insert_delayed(dh, db_cdr_keys, db_cdr_vals, m) < 0) {
			LM_ERR("failed to insert delayed into database\n");
			goto error;
		}
	} else if(acc_db_insert_mode==2 && df->insert_async!=NULL) {
		if (df->insert_async(dh, db_cdr_keys, db_cdr_vals, m) < 0) {
			LM_ERR("failed to insert async into database\n");
			goto error;
		}
	} else {
		if (df->insert(dh, db_cdr_keys, db_cdr_vals, m) < 0) {
			LM_ERR("failed to insert into database\n");
			goto error;
		}
	}

	/* Free memory allocated by acc_extra.c/extra2strar */
	free_strar_mem( &(cdr_type_array[m-n]), &(cdr_value_array[m-n]), n, m);
	return 0;

error:
    /* Free memory allocated by acc_extra.c/extra2strar */
	free_strar_mem( &(cdr_type_array[m-n]), &(cdr_value_array[m-n]), n, m);
    return -1;
}
#endif

/* collect all crd data and write it to a syslog */
static int log_write_cdr( struct dlg_cell* dialog,
                      struct sip_msg* message)
{
    static char cdr_message[ MAX_SYSLOG_SIZE];
    static char* const cdr_message_end = cdr_message +
                                         MAX_SYSLOG_SIZE -
                                         2;// -2 because of the string ending '\n\0'
    char* message_position = NULL;
    int message_index = 0;
	int extra_index = 0;
    int counter = 0;

	if(cdr_log_enable==0)
		return 0;

    /* get default values */
    message_index = cdr_core2strar( dialog,
                                    cdr_value_array,
                                    cdr_int_array,
                                    cdr_type_array);

    /* get extra values */
    if (message)
    {
        extra_index += extra2strar( cdr_extra,
                                      message,
                                      cdr_value_array + message_index,
                                      cdr_int_array + message_index,
                                      cdr_type_array + message_index);
    } else if (cdr_expired_dlg_enable){
        LM_WARN( "fallback to dlg_only search because of message does not exist.\n");
        message_index += extra2strar_dlg_only( cdr_extra,
                                               dialog,
                                               cdr_value_array + message_index,
                                               cdr_int_array + message_index,
                                               cdr_type_array + message_index,
                                               &dlgb);
    }
	message_index += extra_index;

    for( counter = 0, message_position = cdr_message;
         counter < message_index ;
         counter++ )
    {
        const char* const next_message_end = message_position +
                                             2 + // ', ' -> two letters
                                             cdr_attrs[ counter].len +
                                             1 + // '=' -> one letter
                                             cdr_value_array[ counter].len;

        if( next_message_end >= cdr_message_end ||
            next_message_end < message_position)
        {
            LM_WARN("cdr message too long, truncating..\n");
            message_position = cdr_message_end;
            break;
        }

        if( counter > 0)
        {
            *(message_position++) = A_SEPARATOR_CHR;
            *(message_position++) = A_SEPARATOR_CHR_2;
        }

        memcpy( message_position,
                cdr_attrs[ counter].s,
                cdr_attrs[ counter].len);

        message_position += cdr_attrs[ counter].len;

        *( message_position++) = A_EQ_CHR;

        memcpy( message_position,
                cdr_value_array[ counter].s,
                cdr_value_array[ counter].len);

        message_position += cdr_value_array[ counter].len;
    }

    /* terminating line */
    *(message_position++) = '\n';
    *(message_position++) = '\0';

    LM_GEN2( cdr_facility, log_level, "%s", cdr_message);

	/* free memory allocated by extra2strar, nothing is done in case no extra strings were found by extra2strar */
    free_strar_mem( &(cdr_type_array[message_index-extra_index]), &(cdr_value_array[message_index-extra_index]),
				   extra_index, message_index);
    return 0;
}

/* collect all crd data and write it to a syslog */
static int write_cdr( struct dlg_cell* dialog,
                      struct sip_msg* message)
{
	int ret = 0;

	if( !dialog)
	{
		LM_ERR( "dialog is empty!");
		return -1;
	}
	/* message can be null when logging expired dialogs  */
	if ( !cdr_expired_dlg_enable && !message ){
		LM_ERR( "message is empty!");
		return -1;
	}

	ret = log_write_cdr(dialog, message);
#ifdef SQL_ACC
	ret |= db_write_cdr(dialog, message);
#endif
	return ret;
}

/* convert a string into a timeval struct */
static int string2time( str* time_str, struct timeval* time_value)
{    
    char* dot_address = NULL;
    int dot_position = -1;
    char zero_terminated_value[TIME_STR_BUFFER_SIZE];

    if( !time_str)
    {
        LM_ERR( "time_str is empty!");
        return -1;
    }
    
    if( time_str->len >= TIME_STR_BUFFER_SIZE)
    {
        LM_ERR( "time_str is too long %d >= %d!",
		time_str->len,
		TIME_STR_BUFFER_SIZE);
        return -1;
    }
    
    memcpy( zero_terminated_value, time_str->s, time_str->len);
    zero_terminated_value[time_str->len] = '\0';
    
    dot_address = strchr( zero_terminated_value, time_separator);
    
    if( !dot_address)
    {
        LM_ERR( "failed to find separator('%c') in '%s'!\n",
                time_separator,
                zero_terminated_value);
        return -1;
    }
    
    dot_position = dot_address-zero_terminated_value + 1;
    
    if( dot_position >= strlen(zero_terminated_value) ||
        strchr(dot_address + 1, time_separator))
    {
        LM_ERR( "invalid time-string '%s'\n", zero_terminated_value);
        return -1;
    }
    
    time_value->tv_sec = strtol( zero_terminated_value, (char **)NULL, 10);
    time_value->tv_usec = strtol( dot_address + 1, (char **)NULL, 10) * 1000; // restore usec precision
    return 0;
}

/* convert a timeval struct into a string */
static int time2string( struct timeval* time_value, str* time_str)
{
    int buffer_length;

    if( !time_value)
    {
        LM_ERR( "time_value or any of its fields is empty!\n");
        return -1;
    }

    buffer_length = snprintf( time_buffer,
                              TIME_BUFFER_LENGTH,
                              "%ld%c%03d",
                              (long int)time_value->tv_sec,
                              time_separator,
                              (int)(time_value->tv_usec/1000));

    if( buffer_length < 0)
    {
        LM_ERR( "failed to write to buffer.\n");
        return -1;
    }

    time_str->s = time_buffer;
    time_str->len = buffer_length;
    return 0;
}

/* set the duration in the dialog struct */
static int set_duration( struct dlg_cell* dialog)
{
    struct timeval start_time;
    struct timeval end_time;
    struct timeval duration_time;
    str duration_str;

    if( !dialog)
    {
        LM_ERR("dialog is empty!\n");
        return -1;
    }

    if ( string2time( dlgb.get_dlg_var( dialog, (str*)&cdr_start_str), &start_time) < 0) {
        LM_ERR( "failed to extract start time\n");
        return -1;
    }
    if ( string2time( dlgb.get_dlg_var( dialog, (str*)&cdr_end_str), &end_time) < 0) {
        LM_ERR( "failed to extract end time\n");
        return -1;
    }

    timersub(&end_time, &start_time, &duration_time);

    if( time2string(&duration_time, &duration_str) < 0) {
        LM_ERR( "failed to convert current time to string\n");
        return -1;
    }

    if( dlgb.set_dlg_var( dialog,
                          (str*)&cdr_duration_str,
                          (str*)&duration_str) != 0)
    {
        LM_ERR( "failed to set duration time");
        return -1;
    }

    return 0;
}

/* set the current time as start-time in the dialog struct */
static int set_start_time( struct dlg_cell* dialog)
{
    struct timeval current_time;
    str start_time;

    if( !dialog)
    {
        LM_ERR("dialog is empty!\n");
        return -1;
    }

    if( gettimeofday( &current_time, NULL) < 0)
    {
        LM_ERR( "failed to get current time!\n");
        return -1;
    }

    if( time2string(&current_time, &start_time) < 0) {
        LM_ERR( "failed to convert current time to string\n");
        return -1;
    }

    if( dlgb.set_dlg_var( dialog,
                          (str*)&cdr_start_str,
                          (str*)&start_time) != 0)
    {
        LM_ERR( "failed to set start time\n");
        return -1;
    }

    if( dlgb.set_dlg_var( dialog,
                          (str*)&cdr_end_str,
                          (str*)&start_time) != 0)
    {
        LM_ERR( "failed to set initiation end time\n");
        return -1;
    }

    if( dlgb.set_dlg_var( dialog,
                          (str*)&cdr_duration_str,
                          (str*)&zero_duration) != 0)
    {
        LM_ERR( "failed to set initiation duration time\n");
        return -1;
    }

    return 0;
}

/* set the current time as end-time in the dialog struct */
static int set_end_time( struct dlg_cell* dialog)
{
    struct timeval current_time;
    str end_time;

    if( !dialog)
    {
        LM_ERR("dialog is empty!\n");
        return -1;
    }

    if( gettimeofday( &current_time, NULL) < 0)
    {
        LM_ERR( "failed to set time!\n");
        return -1;
    }

    if( time2string(&current_time, &end_time) < 0) {
        LM_ERR( "failed to convert current time to string\n");
        return -1;
    }

    if( dlgb.set_dlg_var( dialog,
                          (str*)&cdr_end_str,
                          (str*)&end_time) != 0)
    {
        LM_ERR( "failed to set start time");
        return -1;
    }

    return 0;
}

/* callback for a confirmed (INVITE) dialog. */
static void cdr_on_start( struct dlg_cell* dialog,
                          int type,
                          struct dlg_cb_params* params)
{
    if( !dialog || !params)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    if( cdr_start_on_confirmed == 0)
    {
        return;
    }

    if( set_start_time( dialog) != 0)
    {
        LM_ERR( "failed to set start time!\n");
        return;
    }
}

/* callback for a failure during a dialog. */
static void cdr_on_failed( struct dlg_cell* dialog,
                           int type,
                           struct dlg_cb_params* params)
{
    struct sip_msg* msg = 0;

    if( !dialog || !params)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    if( params->rpl && params->rpl != FAKED_REPLY)
    {
        msg = params->rpl;
    }
    else if( params->req)
    {
        msg = params->req;
    }
    else
    {
        LM_ERR( "request and response are invalid!");
        return;
    }

    if( write_cdr( dialog, msg) != 0)
    {
        LM_ERR( "failed to write cdr!\n");
        return;
    }
}

/* callback for the finish of a dialog (reply to BYE). */
void cdr_on_end_confirmed( struct dlg_cell* dialog,
                        int type,
                        struct dlg_cb_params* params)
{
    if( !dialog || !params || !params->req)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    if( write_cdr( dialog, params->req) != 0)
    {
        LM_ERR( "failed to write cdr!\n");
        return;
    }
}

/* callback for the end of a dialog (BYE). */
static void cdr_on_end( struct dlg_cell* dialog,
                        int type,
                        struct dlg_cb_params* params)
{
    if( !dialog || !params)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    if( set_end_time( dialog) != 0)
    {
        LM_ERR( "failed to set end time!\n");
        return;
    }

    if( set_duration( dialog) != 0)
    {
        LM_ERR( "failed to set duration!\n");
        return;
    }
}

/* callback for an expired dialog. */
static void cdr_on_expired( struct dlg_cell* dialog,
                            int type,
                            struct dlg_cb_params* params)
{
    if( !dialog || !params)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    LM_DBG("dialog '%p' expired!\n", dialog);

    if( cdr_expired_dlg_enable  && (write_cdr( dialog, 0) != 0))
    {
        LM_ERR( "failed to write cdr!\n");
        return;
    }
}

/* callback for the cleanup of a dialog. */
static void cdr_on_destroy( struct dlg_cell* dialog,
                            int type,
                            struct dlg_cb_params* params)
{
    if( !dialog || !params)
    {
        LM_ERR("invalid values\n!");
        return;
    }

    LM_DBG("dialog '%p' destroyed!\n", dialog);
}

/* callback for the creation of a dialog. */
static void cdr_on_create( struct dlg_cell* dialog,
                           int type,
                           struct dlg_cb_params* params)
{
    if( !dialog || !params || !params->req)
    {
        LM_ERR( "invalid values\n!");
        return;
    }

    if( cdr_enable == 0)
    {
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_CONFIRMED, cdr_on_start, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog CONFIRM callback\n");
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_FAILED, cdr_on_failed, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog FAILED callback\n");
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_TERMINATED, cdr_on_end, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog TERMINATED callback\n");
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_TERMINATED_CONFIRMED, cdr_on_end_confirmed, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog TERMINATED CONFIRMED callback\n");
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_EXPIRED, cdr_on_expired, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog EXPIRED callback\n");
        return;
    }

    if( dlgb.register_dlgcb( dialog, DLGCB_DESTROY, cdr_on_destroy, 0, 0) != 0)
    {
        LM_ERR("can't register create dialog DESTROY callback\n");
        return;
    }

    LM_DBG("dialog '%p' created!", dialog);

    if( set_start_time( dialog) != 0)
    {
        LM_ERR( "failed to set start time");
        return;
    }
}
/* convert the extra-data string into a list and store it */
int set_cdr_extra( char* cdr_extra_value)
{
    struct acc_extra* extra = 0;
    int counter = 0;

    if( cdr_extra_value && ( cdr_extra = parse_acc_extra( cdr_extra_value))==0)
    {
        LM_ERR("failed to parse crd_extra param\n");
        return -1;
    }

    /* fixed core attributes */
    cdr_attrs[ counter++] = cdr_start_str;
    cdr_attrs[ counter++] = cdr_end_str;
    cdr_attrs[ counter++] = cdr_duration_str;

    for(extra=cdr_extra; extra ; extra=extra->next)
    {
        cdr_attrs[ counter++] = extra->name;
    }

    return 0;
}

/* convert the facility-name string into a id and store it */
int set_cdr_facility( char* cdr_facility_str)
{
    int facility_id = -1;

    if( !cdr_facility_str)
    {
        LM_ERR( "facility is empty\n");
        return -1;
    }

    facility_id = str2facility( cdr_facility_str);

    if( facility_id == -1)
    {
        LM_ERR("invalid cdr facility configured\n");
        return -1;
    }

    cdr_facility = facility_id;

    return 0;
}

/* initialization of all necessary callbacks to track a dialog */
int init_cdr_generation( void)
{
    if( load_dlg_api( &dlgb) != 0)
    {
        LM_ERR("can't load dialog API\n");
        return -1;
    }

    if( dlgb.register_dlgcb( 0, DLGCB_CREATED, cdr_on_create, 0, 0) != 0)
    {
        LM_ERR("can't register create callback\n");
        return -1;
    }

    return 0;
}

/* convert the facility-name string into a id and store it */
void destroy_cdr_generation( void)
{
    if( !cdr_extra)
    {
        return;
    }

    destroy_extras( cdr_extra);
}