[asterisk-commits] murf: branch group/newcdr r163035 - /team/group/newcdr/cel/cel_tds.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Dec 11 09:16:36 CST 2008


Author: murf
Date: Thu Dec 11 09:16:36 2008
New Revision: 163035

URL: http://svn.digium.com/view/asterisk?view=rev&rev=163035
Log:
Recomposed the TDS module, based on the current CDR module

Modified:
    team/group/newcdr/cel/cel_tds.c

Modified: team/group/newcdr/cel/cel_tds.c
URL: http://svn.digium.com/view/asterisk/team/group/newcdr/cel/cel_tds.c?view=diff&rev=163035&r1=163034&r2=163035
==============================================================================
--- team/group/newcdr/cel/cel_tds.c (original)
+++ team/group/newcdr/cel/cel_tds.c Thu Dec 11 09:16:36 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2008, Digium, Inc.
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -19,19 +19,17 @@
  * \brief FreeTDS CEL logger
  *
  * See also
- * \arg \ref Config_cel
+ * \arg \ref Config_cdr
  * \arg http://www.freetds.org/
- * \ingroup cel_drivers
+ * \ingroup cdr_drivers
  */
 
 /*! \verbatim
  *
- * Table Structure for `cel`
+ * Table Structure for `cdr`
  *
- * Created on: 05/20/2004 16:16
- * Last changed on: 07/27/2004 20:01
-
-CREATE TABLE [dbo].[cel] (
+
+CREATE TABLE [dbo].[cdr] (
 	[accountcode] [varchar] (20) NULL ,
 	[cidname] [varchar] (80) NULL ,
 	[cidnum] [varchar] (80) NULL ,
@@ -82,34 +80,44 @@
 
 #define DATE_FORMAT "%Y/%m/%d %T"
 
-static char *config = "CEL freeTDS.conf (MSSQL)";
-
-static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *charset = NULL, *language = NULL;
-static char *table = NULL;
-
-static int connected = 0;
+static char *config = "cel_tds.conf";
+
 static struct ast_event_sub *event_sub = 0;
 
+struct cel_tds_config {
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(hostname);
+		AST_STRING_FIELD(database);
+		AST_STRING_FIELD(username);
+		AST_STRING_FIELD(password);
+		AST_STRING_FIELD(table);
+		AST_STRING_FIELD(charset);
+		AST_STRING_FIELD(language);
+	);
+	DBPROCESS *dbproc;
+	unsigned int connected:1;
+	unsigned int has_userfield:1;
+};
+
 AST_MUTEX_DEFINE_STATIC(tds_lock);
 
-static TDSSOCKET *tds;
-static TDSLOGIN *login;
-static TDSCONTEXT *context;
+static struct cel_tds_config *settings;
 
 static char *anti_injection(const char *, int);
-static void get_date(char *, struct timeval );
+static void get_date(char *, size_t len, struct timeval);
+
+static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...)
+	__attribute__((format(printf, 2, 3)));
 
 static int mssql_connect(void);
 static int mssql_disconnect(void);
 
 static void tds_log(const struct ast_event *event, void *userdata)
 {
-	char sqlcmd[2048], start[80];
+	char start[80];
 	char *accountcode_ai, *clidnum_ai, *exten_ai, *context_ai, *clid_ai, *channel_ai, *app_ai, *appdata_ai, *uniqueid_ai, *linkedid_ai, *cidani_ai, *cidrdnis_ai, *ciddnid_ai, *peer_ai, *userfield_ai;
-	int retried = 0;
-#ifdef FREETDS_PRE_0_62
-	TDS_INT result_type;
-#endif
+	RETCODE erc;
+	int attempt = 1;
 	enum ast_cel_eventtype eventtype;
 	const char *userdefname = 0;
 	struct timeval eventtime = {0};
@@ -117,7 +125,7 @@
 	const char *exten, *context, *channame, *appname, *appdata, *accountcode, *uniqueid, *linkedid, *userfield, *peer;
 	const char *eventname;
 	unsigned int amaflag;
-	
+
 	eventtype = (enum ast_cel_eventtype)ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE);
 	eventtime.tv_sec = (time_t)ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TIME);
 	eventtime.tv_usec = (time_t)ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
@@ -142,8 +150,6 @@
 
 	ast_mutex_lock(&tds_lock);
 
-	memset(sqlcmd, 0, 2048);
-
 	if (eventtype == CEL_USER_DEFINED)
 		eventname = userdefname;
 	else
@@ -165,12 +171,25 @@
 	userfield_ai = anti_injection(userfield, 32);
 	peer_ai = anti_injection(peer, 32);
 
-	get_date(start, eventtime);
-
-	sprintf(
-		sqlcmd,
-		"INSERT INTO %s "
-		"("
+	get_date(start, sizeof(start), eventtime);
+
+
+retry:
+	/* Ensure that we are connected */
+	if (!settings->connected) {
+		ast_log(LOG_NOTICE, "Attempting to reconnect to %s (Attempt %d)\n", settings->hostname, attempt);
+		if (mssql_connect()) {
+			/* Connect failed */
+			if (attempt++ < 3) {
+				goto retry;
+			}
+			goto done;
+		}
+	}
+
+	erc = dbfcmd(settings->dbproc,
+				 "INSERT INTO %s "
+				 "("
 			"accountcode, "
 			"cidnum, "
 			"cidname, "
@@ -188,10 +207,9 @@
 			"uniqueid"
 			"linkedid"
 			"userfield"
-			"peer"
-		") "
-		"VALUES "
-		"("
+			"peer"				 ") "
+				 "VALUES "
+				 "("
 			"'%s', "	/* accountcode */
 			"'%s', "	/* clidnum */
 			"'%s', "	/* clid */
@@ -210,49 +228,42 @@
 			"'%s', "	/* linkedid */
 			"'%s', "	/* userfield */
 			"'%s'"		/* peer */
-		")",
-		table,
-		accountcode_ai,
-		clidnum_ai,
-		clid_ai,
-		cidani_ai,
-		cidrdnis_ai,
-		ciddnid_ai,
-		exten_ai,
-		context_ai,
-		channel_ai,
-		app_ai,
-		appdata_ai,
-		start,
-		eventname,
-		ast_cel_flags2str(amaflag),
-		uniqueid_ai,
-		linkedid_ai,
-		userfield_ai,
-		peer_ai
-	);
-
-	do {
-		if (!connected) {
-			if (mssql_connect())
-				ast_log(LOG_ERROR, "Failed to reconnect to SQL database.\n");
-			else
-				ast_log(LOG_WARNING, "Reconnected to SQL database.\n");
-
-			retried = 1;	/* note that we have now tried */
+				 ")",
+		settings->table, accountcode_ai, clidnum_ai, clid_ai, cidani_ai, cidrdnis_ai, ciddnid_ai,
+		exten_ai, context_ai, channel_ai, app_ai, appdata_ai, start, eventname,
+		ast_cel_flags2str(amaflag), uniqueid_ai, linkedid_ai, userfield_ai, peer_ai
+		);
+
+	if (erc == FAIL) {
+		if (attempt++ < 3) {
+			ast_log(LOG_NOTICE, "Failed to build INSERT statement, retrying...\n");
+			mssql_disconnect();
+			goto retry;
+		} else {
+			ast_log(LOG_ERROR, "Failed to build INSERT statement, no CEL was logged.\n");
+			goto done;
 		}
-
-#ifdef FREETDS_PRE_0_62
-		if (!connected || (tds_submit_query(tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
-#else
-		if (!connected || (tds_submit_query(tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(tds) != TDS_SUCCEED))
-#endif
-		{
-			ast_log(LOG_ERROR, "Failed to insert Call Data Record into SQL database.\n");
-
-			mssql_disconnect();	/* this is ok even if we are already disconnected */
+	}
+
+	if (dbsqlexec(settings->dbproc) == FAIL) {
+		if (attempt++ < 3) {
+			ast_log(LOG_NOTICE, "Failed to execute INSERT statement, retrying...\n");
+			mssql_disconnect();
+			goto retry;
+		} else {
+			ast_log(LOG_ERROR, "Failed to execute INSERT statement, no CEL was logged.\n");
+			goto done;
 		}
-	} while (!connected && !retried);
+	}
+
+	/* Consume any results we might get back (this is more of a sanity check than
+	 * anything else, since an INSERT shouldn't return results). */
+	while (dbresults(settings->dbproc) != NO_MORE_RESULTS) {
+		while (dbnextrow(settings->dbproc) != NO_MORE_ROWS);
+	}
+
+done:
+	ast_mutex_unlock(&tds_lock);
 
 	free(accountcode_ai);
 	free(clidnum_ai);
@@ -270,289 +281,332 @@
 	free(userfield_ai);
 	free(peer_ai);
 
-	ast_mutex_unlock(&tds_lock);
-
 	return;
 }
 
 static char *anti_injection(const char *str, int len)
 {
 	/* Reference to http://www.nextgenss.com/papers/advanced_sql_injection.pdf */
-
 	char *buf;
 	char *buf_ptr, *srh_ptr;
 	char *known_bad[] = {"select", "insert", "update", "delete", "drop", ";", "--", "\0"};
 	int idx;
 
-	if ((buf = malloc(len + 1)) == NULL)
-	{
-		ast_log(LOG_ERROR, "cel_tds:  Out of memory error\n");
+	if (!(buf = ast_calloc(1, len + 1))) {
+		ast_log(LOG_ERROR, "Out of memory\n");
 		return NULL;
 	}
-	memset(buf, 0, len);
 
 	buf_ptr = buf;
 
 	/* Escape single quotes */
-	for (; *str && strlen(buf) < len; str++)
-	{
-		if (*str == '\'')
+	for (; *str && strlen(buf) < len; str++) {
+		if (*str == '\'') {
 			*buf_ptr++ = '\'';
+		}
 		*buf_ptr++ = *str;
 	}
 	*buf_ptr = '\0';
 
 	/* Erase known bad input */
-	for (idx=0; *known_bad[idx]; idx++)
-	{
-		while((srh_ptr = strcasestr(buf, known_bad[idx])))
-		{
-			memmove(srh_ptr, srh_ptr+strlen(known_bad[idx]), strlen(srh_ptr+strlen(known_bad[idx]))+1);
+	for (idx = 0; *known_bad[idx]; idx++) {
+		while ((srh_ptr = strcasestr(buf, known_bad[idx]))) {
+			memmove(srh_ptr, srh_ptr + strlen(known_bad[idx]), strlen(srh_ptr + strlen(known_bad[idx])) + 1);
 		}
 	}
 
 	return buf;
 }
 
-static void get_date(char *dateField, struct timeval t)
-{
-	struct ast_tm tm;
-	char buf[80];
-
+static void get_date(char *dateField, size_t len, struct timeval when)
+{
 	/* To make sure we have date variable if not insert null to SQL */
-	if (!ast_tvzero(t))
-	{
-		ast_localtime(&t, &tm, NULL);
-		ast_strftime(buf, 80, DATE_FORMAT, &tm);
-		sprintf(dateField, "'%s'", buf);
-	}
-	else
-	{
-		strcpy(dateField, "null");
-	}
+	if (!ast_tvzero(when)) {
+		struct ast_tm tm;
+		ast_localtime(&when, &tm, NULL);
+		ast_strftime(dateField, len, "'" DATE_FORMAT "'", &tm);
+	} else {
+		ast_copy_string(dateField, "null", len);
+	}
+}
+
+static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...)
+{
+	va_list ap;
+	char *buffer;
+
+	va_start(ap, fmt);
+	if (ast_vasprintf(&buffer, fmt, ap) < 0) {
+		va_end(ap);
+		return 1;
+	}
+	va_end(ap);
+
+	if (dbfcmd(dbproc, buffer) == FAIL) {
+		free(buffer);
+		return 1;
+	}
+
+	free(buffer);
+
+	if (dbsqlexec(dbproc) == FAIL) {
+		return 1;
+	}
+
+	/* Consume the result set (we don't really care about the result, though) */
+	while (dbresults(dbproc) != NO_MORE_RESULTS) {
+		while (dbnextrow(dbproc) != NO_MORE_ROWS);
+	}
+
+	return 0;
 }
 
 static int mssql_disconnect(void)
 {
-	if (tds) {
-		tds_free_socket(tds);
-		tds = NULL;
-	}
-
-	if (context) {
-		tds_free_context(context);
-		context = NULL;
-	}
-
-	if (login) {
-		tds_free_login(login);
-		login = NULL;
-	}
-
-	connected = 0;
+	if (settings->dbproc) {
+		dbclose(settings->dbproc);
+		settings->dbproc = NULL;
+	}
+
+	settings->connected = 0;
 
 	return 0;
 }
 
 static int mssql_connect(void)
 {
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
-	TDSCONNECTION *connection = NULL;
-#else
-	TDSCONNECTINFO *connection = NULL;
-#endif
-	char query[128];
-
-	/* Connect to M$SQL Server */
-	if (!(login = tds_alloc_login()))
-	{
-		ast_log(LOG_ERROR, "tds_alloc_login() failed.\n");
+	LOGINREC *login;
+
+	if ((login = dblogin()) == NULL) {
+		ast_log(LOG_ERROR, "Unable to allocate login structure for db-lib\n");
 		return -1;
 	}
-	
-	tds_set_server(login, hostname);
-	tds_set_user(login, dbuser);
-	tds_set_passwd(login, password);
-	tds_set_app(login, "TSQL");
-	tds_set_library(login, "TDS-Library");
-#ifndef FREETDS_PRE_0_62
-	tds_set_client_charset(login, charset);
-#endif
-	tds_set_language(login, language);
-	tds_set_packet(login, 512);
-	tds_set_version(login, 7, 0);
-
-#ifdef FREETDS_0_64
-	if (!(context = tds_alloc_context(NULL)))
-#else
-	if (!(context = tds_alloc_context()))
-#endif
-	{
-		ast_log(LOG_ERROR, "tds_alloc_context() failed.\n");
-		goto connect_fail;
-	}
-
-	if (!(tds = tds_alloc_socket(context, 512))) {
-		ast_log(LOG_ERROR, "tds_alloc_socket() failed.\n");
-		goto connect_fail;
-	}
-
-	tds_set_parent(tds, NULL);
-	connection = tds_read_config_info(tds, login, context->locale);
-	if (!connection)
-	{
-		ast_log(LOG_ERROR, "tds_read_config() failed.\n");
-		goto connect_fail;
-	}
-
-	if (tds_connect(tds, connection) == TDS_FAIL)
-	{
-		ast_log(LOG_ERROR, "Failed to connect to MSSQL server.\n");
-		tds = NULL;	/* freed by tds_connect() on error */
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
-		tds_free_connection(connection);
-#else
-		tds_free_connect(connection);
-#endif
-		connection = NULL;
-		goto connect_fail;
-	}
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
-	tds_free_connection(connection);
-#else
-	tds_free_connect(connection);
-#endif
-	connection = NULL;
-
-	sprintf(query, "USE %s", dbname);
-#ifdef FREETDS_PRE_0_62
-	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
-#else
-	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds) != TDS_SUCCEED))
-#endif
-	{
-		ast_log(LOG_ERROR, "Could not change database (%s)\n", dbname);
-		goto connect_fail;
-	}
-
-	connected = 1;
+
+	DBSETLAPP(login,     "TSQL");
+	DBSETLUSER(login,    (char *) settings->username);
+	DBSETLPWD(login,     (char *) settings->password);
+	DBSETLCHARSET(login, (char *) settings->charset);
+	DBSETLNATLANG(login, (char *) settings->language);
+
+	if ((settings->dbproc = dbopen(login, (char *) settings->hostname)) == NULL) {
+		ast_log(LOG_ERROR, "Unable to connect to %s\n", settings->hostname);
+		dbloginfree(login);
+		return -1;
+	}
+
+	dbloginfree(login);
+
+	if (dbuse(settings->dbproc, (char *) settings->database) == FAIL) {
+		ast_log(LOG_ERROR, "Unable to select database %s\n", settings->database);
+		goto failed;
+	}
+
+	if (execute_and_consume(settings->dbproc, "SELECT 1 FROM [%s]", settings->table)) {
+		ast_log(LOG_ERROR, "Unable to find table '%s'\n", settings->table);
+		goto failed;
+	}
+
+	/* Check to see if we have a userfield column in the table */
+	if (execute_and_consume(settings->dbproc, "SELECT userfield FROM [%s] WHERE 1 = 0", settings->table)) {
+		ast_log(LOG_NOTICE, "Unable to find 'userfield' column in table '%s'\n", settings->table);
+		settings->has_userfield = 0;
+	} else {
+		settings->has_userfield = 1;
+	}
+
+	settings->connected = 1;
+
 	return 0;
 
-connect_fail:
-	mssql_disconnect();
+failed:
+	dbclose(settings->dbproc);
+	settings->dbproc = NULL;
 	return -1;
 }
 
 static int tds_unload_module(void)
 {
-	mssql_disconnect();
-
 	if (event_sub)
 		ast_event_unsubscribe(event_sub);
 	event_sub = 0;
 
-	if (hostname) free(hostname);
-	if (dbname) free(dbname);
-	if (dbuser) free(dbuser);
-	if (password) free(password);
-	if (charset) free(charset);
-	if (language) free(language);
-	if (table) free(table);
+	if (settings) {
+		ast_mutex_lock(&tds_lock);
+		mssql_disconnect();
+		ast_mutex_unlock(&tds_lock);
+
+		ast_string_field_free_memory(settings);
+		ast_free(settings);
+	}
+
+	dbexit();
 
 	return 0;
 }
 
+static int tds_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
+{
+	ast_log(LOG_ERROR, "%s (%d)\n", dberrstr, dberr);
+
+	if (oserr != DBNOERR) {
+		ast_log(LOG_ERROR, "%s (%d)\n", oserrstr, oserr);
+	}
+
+	return INT_CANCEL;
+}
+
+static int tds_message_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
+{
+	ast_debug(1, "Msg %d, Level %d, State %d, Line %d\n", msgno, severity, msgstate, line);
+	ast_log(LOG_NOTICE, "%s\n", msgtext);
+
+	return 0;
+}
+
 static int tds_load_module(int reload)
 {
 	struct ast_config *cfg;
-	struct ast_variable *var;
 	const char *ptr = NULL;
 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
-#ifdef FREETDS_PRE_0_62
-	TDS_INT result_type;
-#endif
 
 	cfg = ast_config_load(config, config_flags);
-	if (!cfg) {
-		ast_log(LOG_NOTICE, "Unable to load config for MSSQL CEL's: %s\n", config);
+	if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
+		ast_log(LOG_NOTICE, "Unable to load TDS config for CELs: %s\n", config);
 		return 0;
-	}
-
-	var = ast_variable_browse(cfg, "global");
-	if (!var) /* nothing configured */
+	} else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
 		return 0;
 
+	if (!ast_variable_browse(cfg, "global")) {
+		/* nothing configured */
+		ast_config_destroy(cfg);
+		return 0;
+	}
+
+	ast_mutex_lock(&tds_lock);
+
+	/* Clear out any existing settings */
+	ast_string_field_init(settings, 0);
+
 	ptr = ast_variable_retrieve(cfg, "global", "hostname");
-	if (ptr)
-		hostname = strdup(ptr);
-	else
-		ast_log(LOG_ERROR,"Database server hostname not specified.\n");
+	if (ptr) {
+		ast_string_field_set(settings, hostname, ptr);
+	} else {
+		ast_log(LOG_ERROR, "Failed to connect: Database server hostname not specified.\n");
+		goto failed;
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "dbname");
-	if (ptr)
-		dbname = strdup(ptr);
-	else
-		ast_log(LOG_ERROR,"Database dbname not specified.\n");
+	if (ptr) {
+		ast_string_field_set(settings, database, ptr);
+	} else {
+		ast_log(LOG_ERROR, "Failed to connect: Database dbname not specified.\n");
+		goto failed;
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "user");
-	if (ptr)
-		dbuser = strdup(ptr);
-	else
-		ast_log(LOG_ERROR,"Database dbuser not specified.\n");
+	if (ptr) {
+		ast_string_field_set(settings, username, ptr);
+	} else {
+		ast_log(LOG_ERROR, "Failed to connect: Database dbuser not specified.\n");
+		goto failed;
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "password");
-	if (ptr)
-		password = strdup(ptr);
-	else
-		ast_log(LOG_ERROR,"Database password not specified.\n");
+	if (ptr) {
+		ast_string_field_set(settings, password, ptr);
+	} else {
+		ast_log(LOG_ERROR, "Failed to connect: Database password not specified.\n");
+		goto failed;
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "charset");
-	if (ptr)
-		charset = strdup(ptr);
-	else
-		charset = strdup("iso_1");
+	if (ptr) {
+		ast_string_field_set(settings, charset, ptr);
+	} else {
+		ast_string_field_set(settings, charset, "iso_1");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "language");
-	if (ptr)
-		language = strdup(ptr);
-	else
-		language = strdup("us_english");
-
-	ptr = ast_variable_retrieve(cfg,"global","table");
-	if (ptr == NULL) {
-		if (option_debug)
-			ast_log(LOG_DEBUG,"cel_tds: table not specified.  Assuming cel\n");
-		ptr = "cel";
-	}
-	table = strdup(ptr);
-
+	if (ptr) {
+		ast_string_field_set(settings, language, ptr);
+	} else {
+		ast_string_field_set(settings, language, "us_english");
+	}
+
+	ptr = ast_variable_retrieve(cfg, "global", "table");
+	if (ptr) {
+		ast_string_field_set(settings, table, ptr);
+	} else {
+		ast_log(LOG_NOTICE, "Table name not specified, using 'cel' by default.\n");
+		ast_string_field_set(settings, table, "cel");
+	}
+
+	mssql_disconnect();
+
+	if (mssql_connect()) {
+		/* We failed to connect (mssql_connect takes care of logging it) */
+		goto failed;
+	}
+
+	ast_mutex_unlock(&tds_lock);
 	ast_config_destroy(cfg);
 
-	mssql_connect();
+	return 1;
+
+failed:
+	ast_mutex_unlock(&tds_lock);
+	ast_config_destroy(cfg);
+
+	return 0;
+}
+
+static int reload(void)
+{
+	return tds_load_module(1);
+}
+
+static int load_module(void)
+{
+	if (dbinit() == FAIL) {
+		ast_log(LOG_ERROR, "Failed to initialize FreeTDS db-lib\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	dberrhandle(tds_error_handler);
+	dbmsghandle(tds_message_handler);
+
+	settings = ast_calloc(1, sizeof(*settings));
+
+	if (!settings || ast_string_field_init(settings, 256)) {
+		if (settings) {
+			ast_free(settings);
+			settings = NULL;
+		}
+		dbexit();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	if (!tds_load_module(0)) {
+		ast_string_field_free_memory(settings);
+		ast_free(settings);
+		settings = NULL;
+		dbexit();
+		return AST_MODULE_LOAD_DECLINE;
+	}
 
 	/* Register MSSQL CEL handler */
 	event_sub = ast_event_subscribe(AST_EVENT_CEL, tds_log, "CEL TDS logging backend", NULL, AST_EVENT_IE_END);
 	if (!event_sub)
 	{
 		ast_log(LOG_ERROR, "Unable to register MSSQL CEL handling\n");
+		ast_string_field_free_memory(settings);
+		ast_free(settings);
+		settings = NULL;
+		dbexit();
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	return 0;
-}
-
-static int reload(void)
-{
-	tds_unload_module();
-	return tds_load_module(1);
-}
-
-static int load_module(void)
-{
-	if(!tds_load_module(0))
-		return AST_MODULE_LOAD_DECLINE;
-	else 
-		return AST_MODULE_LOAD_SUCCESS;
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
@@ -560,7 +614,7 @@
 	return tds_unload_module();
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "MSSQL CEL Backend",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "FreeTDS CEL Backend",
 		.load = load_module,
 		.unload = unload_module,
 		.reload = reload,




More information about the asterisk-commits mailing list