Allow CDR's to be linked and work on the linked list
[asterisk/asterisk.git] / cdr.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Call Detail Record API 
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License.
12  *
13  * Includes code and algorithms from the Zapata library.
14  *
15  */
16
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/callerid.h>
22 #include <asterisk/causes.h>
23 #include <asterisk/options.h>
24 #include <asterisk/utils.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29
30 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
31 char ast_default_accountcode[20] = "";
32
33 AST_MUTEX_DEFINE_STATIC(cdrlock);
34
35 static struct ast_cdr_beitem {
36         char name[20];
37         char desc[80];
38         ast_cdrbe be;
39         struct ast_cdr_beitem *next;
40 } *bes = NULL;
41
42 /*
43  * We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
44  * through our fingers somehow.  If someone allocates a CDR, it must be completely handled normally
45  * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
46  * isn't properly generated and posted.
47  */
48
49 int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
50 {
51         struct ast_cdr_beitem *i;
52         if (!name)
53                 return -1;
54         if (!be) {
55                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
56                 return -1;
57         }
58         ast_mutex_lock(&cdrlock);
59         i = bes;
60         while(i) {
61                 if (!strcasecmp(name, i->name))
62                         break;
63                 i = i->next;
64         }
65         ast_mutex_unlock(&cdrlock);
66         if (i) {
67                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
68                 return -1;
69         }
70         i = malloc(sizeof(struct ast_cdr_beitem));
71         if (!i)         
72                 return -1;
73         memset(i, 0, sizeof(struct ast_cdr_beitem));
74         strncpy(i->name, name, sizeof(i->name) - 1);
75         strncpy(i->desc, desc, sizeof(i->desc) - 1);
76         i->be = be;
77         ast_mutex_lock(&cdrlock);
78         i->next = bes;
79         bes = i;
80         ast_mutex_unlock(&cdrlock);
81         return 0;
82 }
83
84 void ast_cdr_unregister(char *name)
85 {
86         struct ast_cdr_beitem *i, *prev = NULL;
87         ast_mutex_lock(&cdrlock);
88         i = bes;
89         while(i) {
90                 if (!strcasecmp(name, i->name)) {
91                         if (prev)
92                                 prev->next = i->next;
93                         else
94                                 bes = i->next;
95                         break;
96                 }
97                 i = i->next;
98         }
99         if (option_verbose > 1)
100                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
101         ast_mutex_unlock(&cdrlock);
102         if (i) 
103                 free(i);
104 }
105
106 void ast_cdr_free(struct ast_cdr *cdr)
107 {
108         char *chan;
109         struct ast_cdr *next; 
110         while (cdr) {
111                 next = cdr->next;
112                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
113                 if (!cdr->posted)
114                         ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
115                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
116                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
117                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
118                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
119                 free(cdr);
120                 cdr = next;
121         }
122 }
123
124 struct ast_cdr *ast_cdr_alloc(void)
125 {
126         struct ast_cdr *cdr;
127         cdr = malloc(sizeof(struct ast_cdr));
128         if (cdr) {
129                 memset(cdr, 0, sizeof(struct ast_cdr));
130         }
131         return cdr;
132 }
133
134 void ast_cdr_start(struct ast_cdr *cdr)
135 {
136         char *chan; 
137         while (cdr) {
138                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
139                 if (cdr->posted)
140                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
141                 if (cdr->start.tv_sec || cdr->start.tv_usec)
142                         ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
143                 gettimeofday(&cdr->start, NULL);
144                 cdr = cdr->next;
145         }
146 }
147
148 void ast_cdr_answer(struct ast_cdr *cdr)
149 {
150         char *chan; 
151         while (cdr) {
152                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
153                 if (cdr->posted)
154                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
155                 if (cdr->disposition < AST_CDR_ANSWERED)
156                         cdr->disposition = AST_CDR_ANSWERED;
157                 if (!cdr->answer.tv_sec && !cdr->answer.tv_usec) {
158                         gettimeofday(&cdr->answer, NULL);
159                 }
160                 cdr = cdr->next;
161         }
162 }
163
164 void ast_cdr_busy(struct ast_cdr *cdr)
165 {
166         char *chan; 
167         while (cdr) {
168                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
169                 if (cdr->posted)
170                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
171                 if (cdr->disposition < AST_CDR_BUSY)
172                         cdr->disposition = AST_CDR_BUSY;
173                 cdr = cdr->next;
174         }
175 }
176
177 void ast_cdr_failed(struct ast_cdr *cdr)
178 {
179         char *chan; 
180         while (cdr) {
181                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
182                 if (cdr->posted)
183                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
184                 cdr->disposition = AST_CDR_FAILED;
185                 cdr = cdr->next;
186         }
187 }
188
189 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
190 {
191         int res = 0;
192         while (cdr) {
193                 switch(cause) {
194                         case AST_CAUSE_BUSY:
195                                 ast_cdr_busy(cdr);
196                                 break;
197                         case AST_CAUSE_FAILURE:
198                                 ast_cdr_failed(cdr);
199                                 break;
200                         case AST_CAUSE_NORMAL:
201                                 break;
202                         case AST_CAUSE_NOTDEFINED:
203                                 res = -1;
204                                 break;
205                         default:
206                                 res = -1;
207                                 ast_log(LOG_WARNING, "We don't handle that cause yet\n");
208                 }
209                 cdr = cdr->next;
210         }
211         return res;
212 }
213
214 void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
215 {
216         char *chan; 
217         while (cdr) {
218                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
219                 if (cdr->posted)
220                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
221                 strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
222                 cdr = cdr->next;
223         }
224 }
225
226 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
227 {
228         char *chan; 
229         while (cdr) {
230                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
231                 if (cdr->posted)
232                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
233                 if (!app)
234                         app = "";
235                 strncpy(cdr->lastapp, app, sizeof(cdr->lastapp) - 1);
236                 if (!data)
237                         data = "";
238                 strncpy(cdr->lastdata, data, sizeof(cdr->lastdata) - 1);
239                 cdr = cdr->next;
240         }
241 }
242
243 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
244 {
245         char tmp[AST_MAX_EXTENSION] = "";
246         char *num, *name;
247         while (cdr) {
248                 /* Grab source from ANI or normal Caller*ID */
249                 if (c->ani)
250                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
251                 else if (c->callerid)
252                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
253                 if (c->callerid)
254                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
255                 name = NULL;
256                 num = NULL;
257                 ast_callerid_parse(tmp, &name, &num);
258                 if (num) {
259                         ast_shrink_phone_number(num);
260                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
261                 }
262                 cdr = cdr->next;
263         }
264         return 0;
265 }
266
267 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
268 {
269         char *chan;
270         char *num, *name;
271         char tmp[AST_MAX_EXTENSION] = "";
272         while (cdr) {
273                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
274                 if (!ast_strlen_zero(cdr->channel)) 
275                         ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
276                 strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
277                 /* Grab source from ANI or normal Caller*ID */
278                 if (c->ani)
279                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
280                 else if (c->callerid)
281                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
282                 if (c->callerid)
283                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
284                 name = NULL;
285                 num = NULL;
286                 ast_callerid_parse(tmp, &name, &num);
287                 if (num) {
288                         ast_shrink_phone_number(num);
289                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
290                 }
291                 
292                 if (c->_state == AST_STATE_UP)
293                         cdr->disposition = AST_CDR_ANSWERED;
294                 else
295                         cdr->disposition = AST_CDR_NOANSWER;
296                 if (c->amaflags)
297                         cdr->amaflags = c->amaflags;
298                 else
299                         cdr->amaflags = ast_default_amaflags;
300                 strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
301                 /* Destination information */
302                 strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
303                 strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
304                 /* Unique call identifier */
305                 strncpy(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid) - 1);
306                 cdr = cdr->next;
307         }
308         return 0;
309 }
310
311 void ast_cdr_end(struct ast_cdr *cdr)
312 {
313         char *chan;
314         while (cdr) {
315                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
316                 if (cdr->posted)
317                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
318                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
319                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
320                 if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
321                         gettimeofday(&cdr->end, NULL);
322                 cdr = cdr->next;
323         }
324 }
325
326 char *ast_cdr_disp2str(int disposition)
327 {
328         switch (disposition) {
329         case AST_CDR_NOANSWER:
330                 return "NO ANSWER";
331         case AST_CDR_FAILED:
332                 return "FAILED";                
333         case AST_CDR_BUSY:
334                 return "BUSY";          
335         case AST_CDR_ANSWERED:
336                 return "ANSWERED";
337         default:
338                 return "UNKNOWN";
339         }
340 }
341
342 char *ast_cdr_flags2str(int flag)
343 {
344         switch(flag) {
345         case AST_CDR_OMIT:
346                 return "OMIT";
347         case AST_CDR_BILLING:
348                 return "BILLING";
349         case AST_CDR_DOCUMENTATION:
350                 return "DOCUMENTATION";
351         }
352         return "Unknown";
353 }
354
355 int ast_cdr_setaccount(struct ast_channel *chan, char *account)
356 {
357         struct ast_cdr *cdr = chan->cdr;
358
359         strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
360         while (cdr) {
361                 strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
362                 cdr = cdr->next;
363         }
364         return 0;
365 }
366
367 int ast_cdr_setuserfield(struct ast_channel *chan, char *userfield)
368 {
369         struct ast_cdr *cdr = chan->cdr;
370
371         while (cdr) {
372                 strncpy(cdr->userfield, userfield, sizeof(cdr->userfield) - 1);
373                 cdr = cdr->next;
374         }
375         return 0;
376 }
377
378 int ast_cdr_appenduserfield(struct ast_channel *chan, char *userfield)
379 {
380         struct ast_cdr *cdr = chan->cdr;
381
382         while (cdr)
383         {
384                 int len = strlen(cdr->userfield);
385                 strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
386                 cdr = cdr->next;
387         }
388         return 0;
389 }
390
391 int ast_cdr_update(struct ast_channel *c)
392 {
393         struct ast_cdr *cdr = c->cdr;
394         char *name, *num;
395         char tmp[AST_MAX_EXTENSION] = "";
396         /* Grab source from ANI or normal Caller*ID */
397         while (cdr) {
398                 if (c->ani)
399                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
400                 else if (c->callerid && !ast_strlen_zero(c->callerid))
401                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
402                 if (c->callerid && !ast_strlen_zero(c->callerid))
403                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
404                 else
405                         strcpy(cdr->clid, "");
406                 name = NULL;
407                 num = NULL;
408                 ast_callerid_parse(tmp, &name, &num);
409                 if (num) {
410                         ast_shrink_phone_number(num);
411                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
412                 }
413                 /* Copy account code et-al */   
414                 strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
415                 /* Destination information */
416                 strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
417                 strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
418                 cdr = cdr->next;
419         }
420         return 0;
421 }
422
423 int ast_cdr_amaflags2int(char *flag)
424 {
425         if (!strcasecmp(flag, "default"))
426                 return 0;
427         if (!strcasecmp(flag, "omit"))
428                 return AST_CDR_OMIT;
429         if (!strcasecmp(flag, "billing"))
430                 return AST_CDR_BILLING;
431         if (!strcasecmp(flag, "documentation"))
432                 return AST_CDR_DOCUMENTATION;
433         return -1;
434 }
435
436 void ast_cdr_post(struct ast_cdr *cdr)
437 {
438         char *chan;
439         struct ast_cdr_beitem *i;
440         while (cdr) {
441                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
442                 if (cdr->posted)
443                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
444                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
445                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
446                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
447                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
448                 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
449                 if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
450                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
451                 } else
452                         cdr->billsec = 0;
453                 cdr->posted = 1;
454                 ast_mutex_lock(&cdrlock);
455                 i = bes;
456                 while(i) {
457                         i->be(cdr);
458                         i = i->next;
459                 }
460                 ast_mutex_unlock(&cdrlock);
461                 cdr = cdr->next;
462         }
463 }
464
465 void ast_cdr_reset(struct ast_cdr *cdr, int post)
466 {
467         while (cdr) {
468                 /* Post if requested */
469                 if (post) {
470                         ast_cdr_end(cdr);
471                         ast_cdr_post(cdr);
472                 }
473                 /* Reset to initial state */
474                 cdr->posted = 0;
475                 memset(&cdr->start, 0, sizeof(cdr->start));
476                 memset(&cdr->end, 0, sizeof(cdr->end));
477                 memset(&cdr->answer, 0, sizeof(cdr->answer));
478                 cdr->billsec = 0;
479                 cdr->duration = 0;
480                 ast_cdr_start(cdr);
481                 cdr->disposition = AST_CDR_NOANSWER;
482                 cdr = cdr->next;
483         }
484 }