| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  |  * Copyright (C) 1999 - 2009, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Includes code and algorithms from the Zapata library. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \brief Custom Comma Separated Value CDR records. | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \arg See also \ref AstCDR | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Logs in LOG_DIR/cdr_custom | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup cdr_drivers | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 21:46:45 +00:00
										 |  |  | /*! \li \ref cdr_custom.c uses the configuration file \ref cdr_custom.conf
 | 
					
						
							|  |  |  |  * \addtogroup configuration_file Configuration Files | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \page cdr_custom.conf cdr_custom.conf | 
					
						
							|  |  |  |  * \verbinclude cdr_custom.conf.sample | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-10 23:42:45 +00:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2005-06-06 21:09:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-20 23:16:15 +00:00
										 |  |  | #include "asterisk/paths.h"	/* use ast_config_AST_LOG_DIR */
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/cdr.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2008-03-25 22:51:55 +00:00
										 |  |  | #include "asterisk/lock.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-29 18:53:01 +00:00
										 |  |  | #include "asterisk/threadstorage.h"
 | 
					
						
							|  |  |  | #include "asterisk/strings.h"
 | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define CUSTOM_LOG_DIR "/cdr_custom"
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | #define CONFIG         "cdr_custom.conf"
 | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-29 18:53:01 +00:00
										 |  |  | AST_THREADSTORAGE(custom_buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 15:55:53 +00:00
										 |  |  | static const char name[] = "cdr-custom"; | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | struct cdr_custom_config { | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	AST_DECLARE_STRING_FIELDS( | 
					
						
							|  |  |  | 		AST_STRING_FIELD(filename); | 
					
						
							|  |  |  | 		AST_STRING_FIELD(format); | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	ast_mutex_t lock; | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 	AST_RWLIST_ENTRY(cdr_custom_config) list; | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | static AST_RWLIST_HEAD_STATIC(sinks, cdr_custom_config); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void free_config(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 	struct cdr_custom_config *sink; | 
					
						
							| 
									
										
										
										
											2016-06-01 13:57:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) { | 
					
						
							|  |  |  | 		ast_mutex_destroy(&sink->lock); | 
					
						
							| 
									
										
										
										
											2016-06-01 13:57:53 -05:00
										 |  |  | 		ast_string_field_free_memory(sink); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 		ast_free(sink); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | static int load_config(void) | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_config *cfg; | 
					
						
							|  |  |  | 	struct ast_variable *var; | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	struct ast_flags config_flags = { 0 }; | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	cfg = ast_config_load(CONFIG, config_flags); | 
					
						
							|  |  |  | 	if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { | 
					
						
							| 
									
										
										
										
											2009-05-18 15:55:53 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n"); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2008-11-19 19:25:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	var = ast_variable_browse(cfg, "mappings"); | 
					
						
							|  |  |  | 	while (var) { | 
					
						
							|  |  |  | 		if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) { | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 			struct cdr_custom_config *sink = ast_calloc_with_stringfields(1, struct cdr_custom_config, 1024); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (!sink) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n"); | 
					
						
							|  |  |  | 				res = -2; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ast_string_field_build(sink, format, "%s\n", var->value); | 
					
						
							|  |  |  | 			ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name); | 
					
						
							|  |  |  | 			ast_mutex_init(&sink->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			AST_RWLIST_INSERT_TAIL(&sinks, sink, list); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno); | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 		var = var->next; | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int custom_log(struct ast_cdr *cdr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-23 15:16:59 +00:00
										 |  |  | 	struct ast_channel *dummy; | 
					
						
							| 
									
										
										
										
											2009-04-29 18:53:01 +00:00
										 |  |  | 	struct ast_str *str; | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 	struct cdr_custom_config *config; | 
					
						
							| 
									
										
										
										
											2009-04-29 18:53:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */ | 
					
						
							|  |  |  | 	if (!(str = ast_str_thread_get(&custom_buf, 16))) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 15:28:53 +00:00
										 |  |  | 	dummy = ast_dummy_channel_alloc(); | 
					
						
							| 
									
										
										
										
											2009-05-23 15:16:59 +00:00
										 |  |  | 	if (!dummy) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We need to dup here since the cdr actually belongs to the other channel,
 | 
					
						
							|  |  |  | 	   so when we release this channel we don't want the CDR getting cleaned | 
					
						
							|  |  |  | 	   up prematurely. */ | 
					
						
							| 
									
										
										
										
											2012-02-20 23:43:27 +00:00
										 |  |  | 	ast_channel_cdr_set(dummy, ast_cdr_dup(cdr)); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AST_RWLIST_RDLOCK(&sinks); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&sinks, config, list) { | 
					
						
							|  |  |  | 		FILE *out; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-23 15:16:59 +00:00
										 |  |  | 		ast_str_substitute_variables(&str, 0, dummy, config->format); | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* Even though we have a lock on the list, we could be being chased by
 | 
					
						
							|  |  |  | 		   another thread and this lock ensures that we won't step on anyone's | 
					
						
							|  |  |  | 		   toes.  Once each CDR backend gets it's own thread, this lock can be | 
					
						
							|  |  |  | 		   removed. */ | 
					
						
							|  |  |  | 		ast_mutex_lock(&config->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Because of the absolutely unconditional need for the
 | 
					
						
							|  |  |  | 		   highest reliability possible in writing billing records, | 
					
						
							|  |  |  | 		   we open write and close the log file each time */ | 
					
						
							|  |  |  | 		if ((out = fopen(config->filename, "a"))) { | 
					
						
							|  |  |  | 			fputs(ast_str_buffer(str), out); | 
					
						
							|  |  |  | 			fflush(out); /* be particularly anal here */ | 
					
						
							|  |  |  | 			fclose(out); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ast_mutex_unlock(&config->lock); | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AST_RWLIST_UNLOCK(&sinks); | 
					
						
							| 
									
										
										
										
											2008-03-25 22:51:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 19:40:12 +00:00
										 |  |  | 	ast_channel_unref(dummy); | 
					
						
							| 
									
										
										
										
											2009-05-23 15:16:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-27 20:04:17 +00:00
										 |  |  | 	if (ast_cdr_unregister(name)) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (AST_RWLIST_WRLOCK(&sinks)) { | 
					
						
							|  |  |  | 		ast_cdr_register(name, ast_module_info->description, custom_log); | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	free_config(); | 
					
						
							|  |  |  | 	AST_RWLIST_UNLOCK(&sinks); | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 15:55:53 +00:00
										 |  |  | static enum ast_module_load_result load_module(void) | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	if (AST_RWLIST_WRLOCK(&sinks)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n"); | 
					
						
							| 
									
										
										
										
											2017-04-11 10:07:39 -06:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	load_config(); | 
					
						
							|  |  |  | 	AST_RWLIST_UNLOCK(&sinks); | 
					
						
							|  |  |  | 	ast_cdr_register(name, ast_module_info->description, custom_log); | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	if (AST_RWLIST_WRLOCK(&sinks)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n"); | 
					
						
							| 
									
										
										
										
											2017-04-11 10:07:39 -06:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2009-05-18 14:54:43 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	free_config(); | 
					
						
							|  |  |  | 	load_config(); | 
					
						
							|  |  |  | 	AST_RWLIST_UNLOCK(&sinks); | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2005-03-24 21:57:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend", | 
					
						
							| 
									
										
										
										
											2015-05-04 23:57:57 -04:00
										 |  |  | 	.support_level = AST_MODULE_SUPPORT_CORE, | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							|  |  |  | 	.reload = reload, | 
					
						
							|  |  |  | 	.load_pri = AST_MODPRI_CDR_DRIVER, | 
					
						
							| 
									
										
										
										
											2018-02-16 22:11:42 -05:00
										 |  |  | 	.requires = "cdr", | 
					
						
							| 
									
										
										
										
											2015-05-04 23:57:57 -04:00
										 |  |  | ); |