Source

asterisk / cdr.c

  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
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Call Detail Record API 
 * 
 * Copyright (C) 1999, Mark Spencer
 *
 * Mark Spencer <markster@linux-support.net>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 *
 * Includes code and algorithms from the Zapata library.
 *
 */

#include <asterisk/lock.h>
#include <asterisk/channel.h>
#include <asterisk/cdr.h>
#include <asterisk/logger.h>
#include <asterisk/callerid.h>
#include <asterisk/causes.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

int ast_default_amaflags = AST_CDR_DOCUMENTATION;
char ast_default_accountcode[20] = "";

static ast_mutex_t cdrlock = AST_MUTEX_INITIALIZER;

static struct ast_cdr_beitem {
	char name[20];
	char desc[80];
	ast_cdrbe be;
	struct ast_cdr_beitem *next;
} *bes = NULL;

/*
 * We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
 * through our fingers somehow.  If someone allocates a CDR, it must be completely handled normally
 * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
 * isn't properly generated and posted.
 */

int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
{
	struct ast_cdr_beitem *i;
	if (!name)
		return -1;
	if (!be) {
		ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
		return -1;
	}
	ast_mutex_lock(&cdrlock);
	i = bes;
	while(i) {
		if (!strcasecmp(name, i->name))
			break;
		i = i->next;
	}
	ast_mutex_unlock(&cdrlock);
	if (i) {
		ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
		return -1;
	}
	i = malloc(sizeof(struct ast_cdr_beitem));
	if (!i) 	
		return -1;
	memset(i, 0, sizeof(struct ast_cdr_beitem));
	strncpy(i->name, name, sizeof(i->name) - 1);
	strncpy(i->desc, desc, sizeof(i->desc) - 1);
	i->be = be;
	ast_mutex_lock(&cdrlock);
	i->next = bes;
	bes = i;
	ast_mutex_unlock(&cdrlock);
	return 0;
}

void ast_cdr_unregister(char *name)
{
	struct ast_cdr_beitem *i, *prev = NULL;
	ast_mutex_lock(&cdrlock);
	i = bes;
	while(i) {
		if (!strcasecmp(name, i->name)) {
			if (prev)
				prev->next = i->next;
			else
				bes = i->next;
			break;
		}
		i = i->next;
	}
	ast_mutex_unlock(&cdrlock);
	if (i) 
		free(i);
}

void ast_cdr_free(struct ast_cdr *cdr)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (!cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
		if (!cdr->end.tv_sec && !cdr->end.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
		if (!cdr->start.tv_sec && !cdr->start.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
		free(cdr);
	}
}

struct ast_cdr *ast_cdr_alloc(void)
{
	struct ast_cdr *cdr;
	cdr = malloc(sizeof(struct ast_cdr));
	if (cdr) {
		memset(cdr, 0, sizeof(struct ast_cdr));
	}
	return cdr;
}

void ast_cdr_start(struct ast_cdr *cdr)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (cdr->start.tv_sec || cdr->start.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
		gettimeofday(&cdr->start, NULL);
	}
}

void ast_cdr_answer(struct ast_cdr *cdr)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (cdr->disposition < AST_CDR_ANSWERED)
			cdr->disposition = AST_CDR_ANSWERED;
		if (!cdr->answer.tv_sec && !cdr->answer.tv_usec) {
			gettimeofday(&cdr->answer, NULL);
		}
	}
}

void ast_cdr_busy(struct ast_cdr *cdr)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (cdr->disposition < AST_CDR_BUSY)
			cdr->disposition = AST_CDR_BUSY;
	}
}

void ast_cdr_failed(struct ast_cdr *cdr)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
			cdr->disposition = AST_CDR_FAILED;
	}
}

int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
{
	int res = 0;
	if (cdr) {
		switch(cause) {
			case AST_CAUSE_BUSY:
				ast_cdr_busy(cdr);
				break;
			case AST_CAUSE_FAILURE:
				ast_cdr_failed(cdr);
				break;
			case AST_CAUSE_NORMAL:
				break;
			case AST_CAUSE_NOTDEFINED:
				res = -1;
				break;
			default:
				res = -1;
				ast_log(LOG_WARNING, "We don't handle that cause yet\n");
		}
	}
	return res;
}

void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
	}
}

void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
{
	char *chan; 
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (!app)
			app = "";
		strncpy(cdr->lastapp, app, sizeof(cdr->lastapp) - 1);
		if (!data)
			data = "";
		strncpy(cdr->lastdata, data, sizeof(cdr->lastdata) - 1);
	}
}

int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
{
	char tmp[AST_MAX_EXTENSION] = "";
	char *num, *name;
	if (cdr) {
		/* Grab source from ANI or normal Caller*ID */
		if (c->ani)
			strncpy(tmp, c->ani, sizeof(tmp) - 1);
		else if (c->callerid)
			strncpy(tmp, c->callerid, sizeof(tmp) - 1);
		if (c->callerid)
			strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
		name = NULL;
		num = NULL;
		ast_callerid_parse(tmp, &name, &num);
		if (num) {
			ast_shrink_phone_number(num);
			strncpy(cdr->src, num, sizeof(cdr->src) - 1);
		}
	}
	return 0;
}

int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
{
	char *chan;
	char *num, *name;
	char tmp[AST_MAX_EXTENSION] = "";
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (strlen(cdr->channel)) 
			ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
		strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
		/* Grab source from ANI or normal Caller*ID */
		if (c->ani)
			strncpy(tmp, c->ani, sizeof(tmp) - 1);
		else if (c->callerid)
			strncpy(tmp, c->callerid, sizeof(tmp) - 1);
		if (c->callerid)
			strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
		name = NULL;
		num = NULL;
		ast_callerid_parse(tmp, &name, &num);
		if (num) {
			ast_shrink_phone_number(num);
			strncpy(cdr->src, num, sizeof(cdr->src) - 1);
		}
		
		if (c->_state == AST_STATE_UP)
			cdr->disposition = AST_CDR_ANSWERED;
		else
			cdr->disposition = AST_CDR_NOANSWER;
		if (c->amaflags)
			cdr->amaflags = c->amaflags;
		else
			cdr->amaflags = ast_default_amaflags;
		strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
		/* Destination information */
		strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
		strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
		/* Unique call identifier */
		strncpy(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid) - 1);
	}
	return 0;
}

void ast_cdr_end(struct ast_cdr *cdr)
{
	char *chan;
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (!cdr->start.tv_sec && !cdr->start.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
		if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
			gettimeofday(&cdr->end, NULL);
	}
}

char *ast_cdr_disp2str(int disposition)
{
	switch (disposition) {
	case AST_CDR_NOANSWER:
		return "NO ANSWER";
	case AST_CDR_FAILED:
		return "FAILED";		
	case AST_CDR_BUSY:
		return "BUSY";		
	case AST_CDR_ANSWERED:
		return "ANSWERED";
	default:
		return "UNKNOWN";
	}
}

char *ast_cdr_flags2str(int flag)
{
	switch(flag) {
	case AST_CDR_OMIT:
		return "OMIT";
	case AST_CDR_BILLING:
		return "BILLING";
	case AST_CDR_DOCUMENTATION:
		return "DOCUMENTATION";
	}
	return "Unknown";
}

int ast_cdr_setaccount(struct ast_channel *chan, char *account)
{
	struct ast_cdr *cdr = chan->cdr;

	strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
	if (cdr)
		strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
	return 0;
}

int ast_cdr_setuserfield(struct ast_channel *chan, char *userfield)
{
	struct ast_cdr *cdr = chan->cdr;

	if (cdr)
		strncpy(cdr->userfield, userfield, sizeof(cdr->userfield) - 1);
	return 0;
}

int ast_cdr_appenduserfield(struct ast_channel *chan, char *userfield)
{
	struct ast_cdr *cdr = chan->cdr;

	if (cdr)
	{
		int len = strlen(cdr->userfield);
		strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
	}
	return 0;
}

int ast_cdr_update(struct ast_channel *c)
{
	struct ast_cdr *cdr = c->cdr;
	char *name, *num;
	char tmp[AST_MAX_EXTENSION] = "";
	/* Grab source from ANI or normal Caller*ID */
	if (cdr) {
		if (c->ani)
			strncpy(tmp, c->ani, sizeof(tmp) - 1);
		else if (c->callerid && strlen(c->callerid))
			strncpy(tmp, c->callerid, sizeof(tmp) - 1);
		if (c->callerid && strlen(c->callerid))
			strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
		else
			strcpy(cdr->clid, "");
		name = NULL;
		num = NULL;
		ast_callerid_parse(tmp, &name, &num);
		if (num) {
			ast_shrink_phone_number(num);
			strncpy(cdr->src, num, sizeof(cdr->src) - 1);
		}
		/* Copy account code et-al */	
		strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
		/* Destination information */
		strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
		strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
	}
	return 0;
}

int ast_cdr_amaflags2int(char *flag)
{
	if (!strcasecmp(flag, "default"))
		return 0;
	if (!strcasecmp(flag, "omit"))
		return AST_CDR_OMIT;
	if (!strcasecmp(flag, "billing"))
		return AST_CDR_BILLING;
	if (!strcasecmp(flag, "documentation"))
		return AST_CDR_DOCUMENTATION;
	return -1;
}

void ast_cdr_post(struct ast_cdr *cdr)
{
	char *chan;
	struct ast_cdr_beitem *i;
	if (cdr) {
		chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
		if (cdr->posted)
			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
		if (!cdr->end.tv_sec && !cdr->end.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
		if (!cdr->start.tv_sec && !cdr->start.tv_usec)
			ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
		cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
		if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
			cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
		} else
			cdr->billsec = 0;
		cdr->posted = 1;
		ast_mutex_lock(&cdrlock);
		i = bes;
		while(i) {
			i->be(cdr);
			i = i->next;
		}
		ast_mutex_unlock(&cdrlock);
	}
}

void ast_cdr_reset(struct ast_cdr *cdr, int post)
{
	if (cdr) {
		/* Post if requested */
		if (post) {
			ast_cdr_end(cdr);
			ast_cdr_post(cdr);
		}
		/* Reset to initial state */
		cdr->posted = 0;
		memset(&cdr->start, 0, sizeof(cdr->start));
		memset(&cdr->end, 0, sizeof(cdr->end));
		memset(&cdr->answer, 0, sizeof(cdr->answer));
		cdr->billsec = 0;
		cdr->duration = 0;
		ast_cdr_start(cdr);
		cdr->disposition = AST_CDR_NOANSWER;
	}
}