[asterisk-commits] seanbright: branch group/NoLossCDR-Redux2 r107008 - /team/group/NoLossCDR-Red...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Mar 8 17:29:52 CST 2008


Author: seanbright
Date: Sat Mar  8 17:29:52 2008
New Revision: 107008

URL: http://svn.digium.com/view/asterisk?view=rev&rev=107008
Log:
Compiles but doesn't run yet.  Checker-outters beware.

Modified:
    team/group/NoLossCDR-Redux2/cdr/cdr_tds.c

Modified: team/group/NoLossCDR-Redux2/cdr/cdr_tds.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_tds.c?view=diff&rev=107008&r1=107007&r2=107008
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_tds.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_tds.c Sat Mar  8 17:29:52 2008
@@ -81,25 +81,32 @@
 
 #define DATE_FORMAT "%Y/%m/%d %T"
 
+struct tds_config {
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(hostname);
+		AST_STRING_FIELD(database_name);
+		AST_STRING_FIELD(username);
+		AST_STRING_FIELD(password);
+		AST_STRING_FIELD(charset);
+		AST_STRING_FIELD(language);
+		AST_STRING_FIELD(table_name);
+	);
+
+	TDSSOCKET *socket;
+	TDSLOGIN *login;
+	TDSCONTEXT *context;
+	ast_rwlock_t lock;
+	int connected:1;
+};
+
 static char *name = "mssql";
 static char *config = "cdr_tds.conf";
 
-static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *charset = NULL, *language = NULL;
-static char *table = NULL;
-
-static int connected = 0;
-
-AST_MUTEX_DEFINE_STATIC(tds_lock);
-
-static TDSSOCKET *tds;
-static TDSLOGIN *login;
-static TDSCONTEXT *context;
-
 static char *anti_injection(const char *, int);
 static void get_date(char *, struct timeval);
 
-static int mssql_connect(void);
-static int mssql_disconnect(void);
+static int mssql_connect(struct tds_config *conf);
+static int mssql_disconnect(struct tds_config *conf);
 
 static int tds_log(struct ast_cdr *cdr, void *data)
 {
@@ -107,11 +114,12 @@
 	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
 	int res = 0;
 	int retried = 0;
+	struct tds_config *conf = data;
 #ifdef FREETDS_PRE_0_62
 	TDS_INT result_type;
 #endif
 
-	ast_mutex_lock(&tds_lock);
+	ast_rwlock_rdlock(&conf->lock);
 
 	memset(sqlcmd, 0, 2048);
 
@@ -172,7 +180,7 @@
 			"'%s', "	/* amaflags */
 			"'%s'"		/* uniqueid */
 		")",
-		table,
+		conf->table_name,
 		accountcode,
 		src,
 		dst,
@@ -193,8 +201,8 @@
 	);
 
 	do {
-		if (!connected) {
-			if (mssql_connect())
+		if (!conf->connected) {
+			if (mssql_connect(conf))
 				ast_log(LOG_ERROR, "Failed to reconnect to SQL database.\n");
 			else
 				ast_log(LOG_WARNING, "Reconnected to SQL database.\n");
@@ -203,16 +211,16 @@
 		}
 
 #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))
+		if (!conf->connected || (tds_submit_query(conf->socket, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(conf->socket, &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))
+		if (!conf->connected || (tds_submit_query(conf->socket, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(conf->socket) != 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 */
+			mssql_disconnect(conf);	/* this is ok even if we are already disconnected */
 		}
-	} while (!connected && !retried);
+	} while (!conf->connected && !retried);
 
 	ast_free(accountcode);
 	ast_free(src);
@@ -225,7 +233,7 @@
 	ast_free(lastdata);
 	ast_free(uniqueid);
 
-	ast_mutex_unlock(&tds_lock);
+	ast_rwlock_unlock(&conf->lock);
 
 	return res;
 }
@@ -241,7 +249,7 @@
 
 	if ((buf = ast_malloc(len + 1)) == NULL)
 	{
-		ast_log(LOG_ERROR, "cdr_tds:  Out of memory error\n");
+		ast_log(LOG_ERROR, "Out of memory\n");
 		return NULL;
 	}
 	memset(buf, 0, len);
@@ -287,29 +295,29 @@
 	}
 }
 
-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;
+static int mssql_disconnect(struct tds_config *conf)
+{
+	if (conf->socket) {
+		tds_free_socket(conf->socket);
+		conf->socket = NULL;
+	}
+
+	if (conf->context) {
+		tds_free_context(conf->context);
+		conf->context = NULL;
+	}
+
+	if (conf->login) {
+		tds_free_login(conf->login);
+		conf->login = NULL;
+	}
+
+	conf->connected = 0;
 
 	return 0;
 }
 
-static int mssql_connect(void)
+static int mssql_connect(struct tds_config *conf)
 {
 #if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
 	TDSCONNECTION *connection = NULL;
@@ -319,51 +327,49 @@
 	char query[128];
 
 	/* Connect to M$SQL Server */
-	if (!(login = tds_alloc_login()))
+	if (!(conf->login = tds_alloc_login()))
 	{
 		ast_log(LOG_ERROR, "tds_alloc_login() failed.\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");
+	tds_set_server(conf->login, conf->hostname);
+	tds_set_user(conf->login, conf->username);
+	tds_set_passwd(conf->login, conf->password);
+	tds_set_app(conf->login, "TSQL");
+	tds_set_library(conf->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);
+	tds_set_client_charset(conf->login, conf->charset);
+#endif
+	tds_set_language(conf->login, conf->language);
+	tds_set_packet(conf->login, 512);
+	tds_set_version(conf->login, 7, 0);
 
 #ifdef FREETDS_0_64
-	if (!(context = tds_alloc_context(NULL)))
+	if (!(conf->context = tds_alloc_context(NULL)))
 #else
-	if (!(context = tds_alloc_context()))
+	if (!(conf->context = tds_alloc_context()))
 #endif
 	{
 		ast_log(LOG_ERROR, "tds_alloc_context() failed.\n");
 		goto connect_fail;
 	}
 
-	if (!(tds = tds_alloc_socket(context, 512))) {
+	if (!(conf->socket = tds_alloc_socket(conf->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)
-	{
+	tds_set_parent(conf->socket, NULL);
+	connection = tds_read_config_info(conf->socket, conf->login, conf->context->locale);
+	if (!connection) {
 		ast_log(LOG_ERROR, "tds_read_config() failed.\n");
 		goto connect_fail;
 	}
 
-	if (tds_connect(tds, connection) == TDS_FAIL)
-	{
+	if (tds_connect(conf->socket, connection) == TDS_FAIL) {
 		ast_log(LOG_ERROR, "Failed to connect to MSSQL server.\n");
-		tds = NULL;	/* freed by tds_connect() on error */
+		conf->socket = NULL;	/* freed by tds_connect() on error */
 #if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
 		tds_free_connection(connection);
 #else
@@ -379,39 +385,44 @@
 #endif
 	connection = NULL;
 
-	sprintf(query, "USE %s", dbname);
+	sprintf(query, "USE %s", conf->database_name);
 #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))
+	if ((tds_submit_query(conf->socket, query) != TDS_SUCCEED) || (tds_process_simple_query(conf->socket, &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);
+	if ((tds_submit_query(conf->socket, query) != TDS_SUCCEED) || (tds_process_simple_query(conf->socket) != TDS_SUCCEED))
+#endif
+	{
+		ast_log(LOG_ERROR, "Could not change database (%s)\n", conf->database_name);
 		goto connect_fail;
 	}
 
-	connected = 1;
+	conf->connected = 1;
 	return 0;
 
 connect_fail:
-	mssql_disconnect();
+	mssql_disconnect(conf);
 	return -1;
 }
 
+static void free_config(void *config)
+{
+	struct tds_config *conf = config;
+
+	if (!conf) {
+		return;
+	}
+
+	mssql_disconnect(conf);
+
+	ast_string_field_free_memory(conf);
+	ast_free(conf);
+
+	return;
+}
+
 static int tds_unload_module(void)
 {
-	mssql_disconnect();
-
 	ast_cdr_unregister(name, NULL);
-
-	if (hostname) ast_free(hostname);
-	if (dbname) ast_free(dbname);
-	if (dbuser) ast_free(dbuser);
-	if (password) ast_free(password);
-	if (charset) ast_free(charset);
-	if (language) ast_free(language);
-	if (table) ast_free(table);
-
 	return 0;
 }
 
@@ -422,6 +433,7 @@
 	struct ast_variable *var;
 	const char *ptr = NULL;
 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+	struct tds_config *conf = NULL;
 #ifdef FREETDS_PRE_0_62
 	TDS_INT result_type;
 #endif
@@ -430,8 +442,20 @@
 	if (!cfg) {
 		ast_log(LOG_NOTICE, "Unable to load config for MSSQL CDR's: %s\n", config);
 		return 0;
-	} else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
+	} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
 		return 0;
+	}
+
+	conf = ast_calloc(1, sizeof(*conf));
+
+	if (!conf || ast_string_field_init(conf, 140)) {
+		if (conf) {
+			ast_free(conf);
+		}
+		return 0;
+	}
+
+	ast_rwlock_init(&conf->lock);
 
 	var = ast_variable_browse(cfg, "global");
 	if (!var) /* nothing configured */ {
@@ -441,67 +465,62 @@
 	
 	ptr = ast_variable_retrieve(cfg, "global", "hostname");
 	if (ptr) {
-		if (hostname)
-			ast_free(hostname);
-		hostname = ast_strdup(ptr);
-	} else
+		ast_string_field_set(conf, hostname, ptr);
+	} else {
 		ast_log(LOG_ERROR, "Database server hostname not specified.\n");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "dbname");
 	if (ptr) {
-		if (dbname)
-			ast_free(dbname);
-		dbname = ast_strdup(ptr);
-	} else
+		ast_string_field_set(conf, database_name, ptr);
+	} else {
 		ast_log(LOG_ERROR, "Database dbname not specified.\n");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "user");
 	if (ptr) {
-		if (dbuser)
-			ast_free(dbuser);
-		dbuser = ast_strdup(ptr);
-	} else
+		ast_string_field_set(conf, username, ptr);
+	} else {
 		ast_log(LOG_ERROR, "Database dbuser not specified.\n");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "password");
 	if (ptr) {
-		if (password)
-			ast_free(password);
-		password = ast_strdup(ptr);
-	} else
-		ast_log(LOG_ERROR,"Database password not specified.\n");
+		ast_string_field_set(conf, password, ptr);
+	} else {
+		ast_log(LOG_ERROR, "Database password not specified.\n");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "charset");
-	if (charset)
-		ast_free(charset);
-	if (ptr)
-		charset = ast_strdup(ptr);
-	else
-		charset = ast_strdup("iso_1");
-
-	if (language)
-		ast_free(language);
+	if (ptr) {
+		ast_string_field_set(conf, charset, ptr);
+	} else {
+		ast_string_field_set(conf, charset, "iso_1");
+	}
+
 	ptr = ast_variable_retrieve(cfg, "global", "language");
-	if (ptr)
-		language = ast_strdup(ptr);
-	else
-		language = ast_strdup("us_english");
+	if (ptr) {
+		ast_string_field_set(conf, language, ptr);
+	} else {
+		ast_string_field_set(conf, language, "us_english");
+	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "table");
-	if (ptr == NULL) {
-		ast_debug(1, "cdr_tds: table not specified.  Assuming cdr\n");
-		ptr = "cdr";
-	}
-	if (table)
-		ast_free(table);
-	table = ast_strdup(ptr);
+	if (ptr) {
+		ast_string_field_set(conf, table_name, ptr);
+	} else {
+		ast_debug(1, "table not specified.  Assuming cdr\n");
+		ast_string_field_set(conf, table_name, "cdr");
+	}
 
 	ast_config_destroy(cfg);
 
-	ast_mutex_lock(&tds_lock);
-	mssql_disconnect();
-	mssql_connect();
-	ast_mutex_unlock(&tds_lock);
+	ast_rwlock_rdlock(&conf->lock);
+	mssql_disconnect(conf);
+	mssql_connect(conf);
+	ast_rwlock_unlock(&conf->lock);
+
+	ast_cdr_register(name, NULL, ast_module_info->description, tds_log, free_config, conf);
 
 	return res;
 }
@@ -513,9 +532,10 @@
 
 static int load_module(void)
 {
-	if (!tds_load_module(0))
+	if (!tds_load_module(0)) {
 		return AST_MODULE_LOAD_DECLINE;
-	ast_cdr_register(name, NULL, ast_module_info->description, tds_log, NULL, NULL);
+	}
+
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list