Add peercontext parameter for default outbound context (bug #1929)
[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
29 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
30 char ast_default_accountcode[20] = "";
31
32 AST_MUTEX_DEFINE_STATIC(cdrlock);
33
34 static struct ast_cdr_beitem {
35         char name[20];
36         char desc[80];
37         ast_cdrbe be;
38         struct ast_cdr_beitem *next;
39 } *bes = NULL;
40
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 (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_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                 if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
139                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
140                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
141                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
142                         if (cdr->start.tv_sec || cdr->start.tv_usec)
143                                 ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
144                         gettimeofday(&cdr->start, NULL);
145                 }
146                         cdr = cdr->next;
147         }
148 }
149
150 void ast_cdr_answer(struct ast_cdr *cdr)
151 {
152         char *chan; 
153         while (cdr) {
154                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
155                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
156                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
157                 if (cdr->disposition < AST_CDR_ANSWERED)
158                         cdr->disposition = AST_CDR_ANSWERED;
159                 if (!cdr->answer.tv_sec && !cdr->answer.tv_usec) {
160                         gettimeofday(&cdr->answer, NULL);
161                 }
162                 cdr = cdr->next;
163         }
164 }
165
166 void ast_cdr_busy(struct ast_cdr *cdr)
167 {
168         char *chan; 
169         while (cdr) {
170                 if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
171                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
172                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
173                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
174                         if (cdr->disposition < AST_CDR_BUSY)
175                                 cdr->disposition = AST_CDR_BUSY;
176                 }
177                 cdr = cdr->next;
178         }
179 }
180
181 void ast_cdr_failed(struct ast_cdr *cdr)
182 {
183         char *chan; 
184         while (cdr) {
185                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
186                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
187                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
188                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
189                         cdr->disposition = AST_CDR_FAILED;
190                 cdr = cdr->next;
191         }
192 }
193
194 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
195 {
196         int res = 0;
197         while (cdr) {
198                 switch(cause) {
199                         case AST_CAUSE_BUSY:
200                                 ast_cdr_busy(cdr);
201                                 break;
202                         case AST_CAUSE_FAILURE:
203                                 ast_cdr_failed(cdr);
204                                 break;
205                         case AST_CAUSE_NORMAL:
206                                 break;
207                         case AST_CAUSE_NOTDEFINED:
208                                 res = -1;
209                                 break;
210                         default:
211                                 res = -1;
212                                 ast_log(LOG_WARNING, "We don't handle that cause yet\n");
213                 }
214                 cdr = cdr->next;
215         }
216         return res;
217 }
218
219 void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
220 {
221         char *chan; 
222         while (cdr) {
223                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
224                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
225                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
226                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
227                         strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
228                 cdr = cdr->next;
229         }
230 }
231
232 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
233 {
234         char *chan; 
235         while (cdr) {
236                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
237                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
238                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
239                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
240                         if (!app)
241                                 app = "";
242                         strncpy(cdr->lastapp, app, sizeof(cdr->lastapp) - 1);
243                         if (!data)
244                                 data = "";
245                         strncpy(cdr->lastdata, data, sizeof(cdr->lastdata) - 1);
246                 }
247                 cdr = cdr->next;
248         }
249 }
250
251 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
252 {
253         char tmp[AST_MAX_EXTENSION] = "";
254         char *num, *name;
255         while (cdr) {
256                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
257                         /* Grab source from ANI or normal Caller*ID */
258                         if (c->ani)
259                                 strncpy(tmp, c->ani, sizeof(tmp) - 1);
260                         else if (c->callerid)
261                                 strncpy(tmp, c->callerid, sizeof(tmp) - 1);
262                         if (c->callerid)
263                                 strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
264                         name = NULL;
265                         num = NULL;
266                         ast_callerid_parse(tmp, &name, &num);
267                         if (num) {
268                                 ast_shrink_phone_number(num);
269                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
270                         }
271                 }
272                 cdr = cdr->next;
273         }
274         return 0;
275 }
276
277 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
278 {
279         char *chan;
280         char *num, *name;
281         char tmp[AST_MAX_EXTENSION] = "";
282         while (cdr) {
283                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
284                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
285                         if (!ast_strlen_zero(cdr->channel)) 
286                                 ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
287                         strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
288                         /* Grab source from ANI or normal Caller*ID */
289                         if (c->ani)
290                                 strncpy(tmp, c->ani, sizeof(tmp) - 1);
291                         else if (c->callerid)
292                                 strncpy(tmp, c->callerid, sizeof(tmp) - 1);
293                         if (c->callerid)
294                                 strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
295                         name = NULL;
296                         num = NULL;
297                         ast_callerid_parse(tmp, &name, &num);
298                         if (num) {
299                                 ast_shrink_phone_number(num);
300                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
301                         }
302                         
303                         if (c->_state == AST_STATE_UP)
304                                 cdr->disposition = AST_CDR_ANSWERED;
305                         else
306                                 cdr->disposition = AST_CDR_NOANSWER;
307                         if (c->amaflags)
308                                 cdr->amaflags = c->amaflags;
309                         else
310                                 cdr->amaflags = ast_default_amaflags;
311                         strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
312                         /* Destination information */
313                         strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
314                         strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
315                         /* Unique call identifier */
316                         strncpy(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid) - 1);
317                 }
318                 cdr = cdr->next;
319         }
320         return 0;
321 }
322
323 void ast_cdr_end(struct ast_cdr *cdr)
324 {
325         char *chan;
326         while (cdr) {
327                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
328                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
329                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
330                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
331                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
332                 if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
333                         gettimeofday(&cdr->end, NULL);
334                 cdr = cdr->next;
335         }
336 }
337
338 char *ast_cdr_disp2str(int disposition)
339 {
340         switch (disposition) {
341         case AST_CDR_NOANSWER:
342                 return "NO ANSWER";
343         case AST_CDR_FAILED:
344                 return "FAILED";                
345         case AST_CDR_BUSY:
346                 return "BUSY";          
347         case AST_CDR_ANSWERED:
348                 return "ANSWERED";
349         default:
350                 return "UNKNOWN";
351         }
352 }
353
354 char *ast_cdr_flags2str(int flag)
355 {
356         switch(flag) {
357         case AST_CDR_OMIT:
358                 return "OMIT";
359         case AST_CDR_BILLING:
360                 return "BILLING";
361         case AST_CDR_DOCUMENTATION:
362                 return "DOCUMENTATION";
363         }
364         return "Unknown";
365 }
366
367 int ast_cdr_setaccount(struct ast_channel *chan, char *account)
368 {
369         struct ast_cdr *cdr = chan->cdr;
370
371         strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
372         while (cdr) {
373                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
374                         strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
375                 cdr = cdr->next;
376         }
377         return 0;
378 }
379
380 int ast_cdr_setuserfield(struct ast_channel *chan, char *userfield)
381 {
382         struct ast_cdr *cdr = chan->cdr;
383
384         while (cdr) {
385                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) 
386                         strncpy(cdr->userfield, userfield, sizeof(cdr->userfield) - 1);
387                 cdr = cdr->next;
388         }
389         return 0;
390 }
391
392 int ast_cdr_appenduserfield(struct ast_channel *chan, char *userfield)
393 {
394         struct ast_cdr *cdr = chan->cdr;
395
396         while (cdr)
397         {
398
399                 int len = strlen(cdr->userfield);
400                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
401                         strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
402                 cdr = cdr->next;
403         }
404         return 0;
405 }
406
407 int ast_cdr_update(struct ast_channel *c)
408 {
409         struct ast_cdr *cdr = c->cdr;
410         char *name, *num;
411         char tmp[AST_MAX_EXTENSION] = "";
412         /* Grab source from ANI or normal Caller*ID */
413         while (cdr) {
414                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
415                         if (c->ani)
416                                 strncpy(tmp, c->ani, sizeof(tmp) - 1);
417                         else if (c->callerid && !ast_strlen_zero(c->callerid))
418                                 strncpy(tmp, c->callerid, sizeof(tmp) - 1);
419                         if (c->callerid && !ast_strlen_zero(c->callerid))
420                                 strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
421                         else
422                                 strcpy(cdr->clid, "");
423                         name = NULL;
424                         num = NULL;
425                         ast_callerid_parse(tmp, &name, &num);
426                         if (num) {
427                                 ast_shrink_phone_number(num);
428                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
429                         }
430                         /* Copy account code et-al */   
431                         strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
432                         /* Destination information */
433                         strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
434                         strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
435                 }
436                 cdr = cdr->next;
437         }
438
439         return 0;
440 }
441
442 int ast_cdr_amaflags2int(char *flag)
443 {
444         if (!strcasecmp(flag, "default"))
445                 return 0;
446         if (!strcasecmp(flag, "omit"))
447                 return AST_CDR_OMIT;
448         if (!strcasecmp(flag, "billing"))
449                 return AST_CDR_BILLING;
450         if (!strcasecmp(flag, "documentation"))
451                 return AST_CDR_DOCUMENTATION;
452         return -1;
453 }
454
455 void ast_cdr_post(struct ast_cdr *cdr)
456 {
457         char *chan;
458         struct ast_cdr_beitem *i;
459         while (cdr) {
460                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
461                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
462                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
463                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
464                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
465                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
466                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
467                 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
468                 if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
469                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
470                 } else
471                         cdr->billsec = 0;
472                 ast_cdr_add_flag(cdr,AST_CDR_FLAG_POSTED);
473                 ast_mutex_lock(&cdrlock);
474                 i = bes;
475                 while(i) {
476                         i->be(cdr);
477                         i = i->next;
478                 }
479                 ast_mutex_unlock(&cdrlock);
480                 cdr = cdr->next;
481         }
482 }
483
484 void ast_cdr_reset(struct ast_cdr *cdr, int post)
485 {
486         while (cdr) {
487                 /* Post if requested */
488                 if (post) {
489                         ast_cdr_end(cdr);
490                         ast_cdr_post(cdr);
491                 }
492                 /* Reset to initial state */
493                 cdr->flags=0;
494                 memset(&cdr->start, 0, sizeof(cdr->start));
495                 memset(&cdr->end, 0, sizeof(cdr->end));
496                 memset(&cdr->answer, 0, sizeof(cdr->answer));
497                 cdr->billsec = 0;
498                 cdr->duration = 0;
499                 ast_cdr_start(cdr);
500                 cdr->disposition = AST_CDR_NOANSWER;
501                 cdr = cdr->next;
502         }
503 }
504
505 void ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr) {
506         if (cdr) {
507                 while(cdr->next)
508                         cdr = cdr->next;
509                 cdr->next = newcdr;
510         } else
511                 ast_log(LOG_ERROR, "Can't append a CDR to NULL!\n");
512 }