[asterisk-commits] seanbright: trunk r137203 - in /trunk: UPGRADE.txt cdr/cdr_tds.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 11 09:25:16 CDT 2008


Author: seanbright
Date: Mon Aug 11 09:25:15 2008
New Revision: 137203

URL: http://svn.digium.com/view/asterisk?view=rev&rev=137203
Log:
Log the userfield CDR variable like the other CDR backends, assuming the
column is actually there.  If it's not, we still log everything else as
before.

(closes issue #13281)
Reported by: falves11

Modified:
    trunk/UPGRADE.txt
    trunk/cdr/cdr_tds.c

Modified: trunk/UPGRADE.txt
URL: http://svn.digium.com/view/asterisk/trunk/UPGRADE.txt?view=diff&rev=137203&r1=137202&r2=137203
==============================================================================
--- trunk/UPGRADE.txt (original)
+++ trunk/UPGRADE.txt Mon Aug 11 09:25:15 2008
@@ -168,7 +168,8 @@
   systemname was too long, the uniqueid would have been truncated.
 
 * The cdr_tds module now supports all versions of FreeTDS that contain
-  the db-lib frontend.
+  the db-lib frontend.  It will also now log the userfield variable if
+  the target database table contains a column for it.
 
 Formats:
 

Modified: trunk/cdr/cdr_tds.c
URL: http://svn.digium.com/view/asterisk/trunk/cdr/cdr_tds.c?view=diff&rev=137203&r1=137202&r2=137203
==============================================================================
--- trunk/cdr/cdr_tds.c (original)
+++ trunk/cdr/cdr_tds.c Mon Aug 11 09:25:15 2008
@@ -48,7 +48,8 @@
 	[billsec] [int] NULL ,
 	[disposition] [varchar] (20) NULL ,
 	[amaflags] [varchar] (16) NULL ,
-	[uniqueid] [varchar] (32) NULL
+	[uniqueid] [varchar] (32) NULL ,
+	[userfield] [varchar] (256) NULL
 ) ON [PRIMARY]
 
 \endverbatim
@@ -91,6 +92,7 @@
 	);
 	DBPROCESS *dbproc;
 	unsigned int connected:1;
+	unsigned int has_userfield:1;
 };
 
 AST_MUTEX_DEFINE_STATIC(tds_lock);
@@ -100,13 +102,16 @@
 static char *anti_injection(const char *, int);
 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 int tds_log(struct ast_cdr *cdr)
 {
 	char start[80], answer[80], end[80];
-	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
+	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid, *userfield = NULL;
 	RETCODE erc;
 	int res = -1;
 
@@ -127,6 +132,10 @@
 
 	ast_mutex_lock(&tds_lock);
 
+	if (settings->has_userfield) {
+		userfield = anti_injection(cdr->userfield, AST_MAX_USER_FIELD);
+	}
+
 	/* Ensure that we are connected */
 	if (!settings->connected) {
 		if (mssql_connect()) {
@@ -135,66 +144,46 @@
 		}
 	}
 
-	erc = dbfcmd(settings->dbproc,
-		"INSERT INTO %s "
-		"("
-			"accountcode, "
-			"src, "
-			"dst, "
-			"dcontext, "
-			"clid, "
-			"channel, "
-			"dstchannel, "
-			"lastapp, "
-			"lastdata, "
-			"start, "
-			"answer, "
-			"[end], "
-			"duration, "
-			"billsec, "
-			"disposition, "
-			"amaflags, "
-			"uniqueid"
-		") "
-		"VALUES "
-		"("
-			"'%s', "	/* accountcode */
-			"'%s', "	/* src */
-			"'%s', "	/* dst */
-			"'%s', "	/* dcontext */
-			"'%s', "	/* clid */
-			"'%s', "	/* channel */
-			"'%s', "	/* dstchannel */
-			"'%s', "	/* lastapp */
-			"'%s', "	/* lastdata */
-			"%s, "		/* start */
-			"%s, "		/* answer */
-			"%s, "		/* end */
-			"%ld, "		/* duration */
-			"%ld, "		/* billsec */
-			"'%s', "	/* disposition */
-			"'%s', "	/* amaflags */
-			"'%s'"		/* uniqueid */
-		")",
-		settings->table,
-		accountcode,
-		src,
-		dst,
-		dcontext,
-		clid,
-		channel,
-		dstchannel,
-		lastapp,
-		lastdata,
-		start,
-		answer,
-		end,
-		cdr->duration,
-		cdr->billsec,
-		ast_cdr_disp2str(cdr->disposition),
-		ast_cdr_flags2str(cdr->amaflags),
-		uniqueid
-	);
+	if (settings->has_userfield) {
+		erc = dbfcmd(settings->dbproc,
+					 "INSERT INTO %s "
+					 "("
+					 "accountcode, src, dst, dcontext, clid, channel, "
+					 "dstchannel, lastapp, lastdata, start, answer, [end], duration, "
+					 "billsec, disposition, amaflags, uniqueid, userfield"
+					 ") "
+					 "VALUES "
+					 "("
+					 "'%s', '%s', '%s', '%s', '%s', '%s', "
+					 "'%s', '%s', '%s', %s, %s, %s, %ld, "
+					 "%ld, '%s', '%s', '%s', '%s'"
+					 ")",
+					 settings->table,
+					 accountcode, src, dst, dcontext, clid, channel,
+					 dstchannel, lastapp, lastdata, start, answer, end, cdr->duration,
+					 cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), uniqueid,
+					 userfield
+			);
+	} else {
+		erc = dbfcmd(settings->dbproc,
+					 "INSERT INTO %s "
+					 "("
+					 "accountcode, src, dst, dcontext, clid, channel, "
+					 "dstchannel, lastapp, lastdata, start, answer, [end], duration, "
+					 "billsec, disposition, amaflags, uniqueid"
+					 ") "
+					 "VALUES "
+					 "("
+					 "'%s', '%s', '%s', '%s', '%s', '%s', "
+					 "'%s', '%s', '%s', %s, %s, %s, %ld, "
+					 "%ld, '%s', '%s', '%s'"
+					 ")",
+					 settings->table,
+					 accountcode, src, dst, dcontext, clid, channel,
+					 dstchannel, lastapp, lastdata, start, answer, end, cdr->duration,
+					 cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), uniqueid
+			);
+	}
 
 	if (erc == FAIL) {
 		ast_log(LOG_ERROR, "Failed to build INSERT statement, no CDR was logged.\n");
@@ -228,6 +217,10 @@
 	ast_free(lastdata);
 	ast_free(uniqueid);
 
+	if (userfield) {
+		ast_free(userfield);
+	}
+
 	return res;
 }
 
@@ -277,6 +270,37 @@
 	}
 }
 
+static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...)
+{
+	va_list ap;
+	char *buffer;
+
+	va_start(ap, fmt);
+	if (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 (settings->dbproc) {
@@ -317,19 +341,17 @@
 		goto failed;
 	}
 
-	if (dbfcmd(settings->dbproc, "SELECT 1 FROM [%s]", settings->table) == FAIL) {
-		ast_log(LOG_ERROR, "Unable to build query while verifying the existence of table '%s'\n", settings->table);
-		goto failed;
-	}
-
-	if (dbsqlexec(settings->dbproc) == FAIL) {
-		ast_log(LOG_ERROR, "Unable to verify existence of table '%s'\n", settings->table);
-		goto failed;
-	}
-
-	/* Consume the result set (we don't really care about the result, though) */
-	while (dbresults(settings->dbproc) != NO_MORE_RESULTS) {
-		while (dbnextrow(settings->dbproc) != NO_MORE_ROWS);
+	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;




More information about the asterisk-commits mailing list