Merge "Revert "AGI: Only defer frames when in an interception routine.""
[asterisk/asterisk.git] / cdr / cdr_tds.c
index 7563543..f3d0628 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2004 - 2005, Digium, Inc.
+ * Copyright (C) 2004 - 2006, Digium, Inc.
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -14,8 +14,8 @@
  * at the top of the source tree.
  */
 
-/*! \file
- *
+/*!
+ * \file
  * \brief FreeTDS CDR logger
  *
  * See also
@@ -24,7 +24,8 @@
  * \ingroup cdr_drivers
  */
 
-/*! \verbatim
+/*!
+ * \verbatim
  *
  * Table Structure for `cdr`
  *
@@ -48,485 +49,590 @@ CREATE TABLE [dbo].[cdr] (
        [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
 
 */
 
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-#include <math.h>
-
-#include <tds.h>
-#include <tdsconvert.h>
-#include <ctype.h>
+/*** MODULEINFO
+       <depend>freetds</depend>
+       <support_level>extended</support_level>
+ ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/config.h"
-#include "asterisk/options.h"
 #include "asterisk/channel.h"
 #include "asterisk/cdr.h"
 #include "asterisk/module.h"
-#include "asterisk/logger.h"
 
-#ifdef FREETDS_PRE_0_62
-#warning "You have older TDS, you should upgrade!"
-#endif
+#include <sqlfront.h>
+#include <sybdb.h>
 
 #define DATE_FORMAT "%Y/%m/%d %T"
 
-static char *desc = "MSSQL CDR Backend";
-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;
+static const char name[] = "FreeTDS (MSSQL)";
+static const char config[] = "cdr_tds.conf";
+
+struct cdr_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);
+               AST_STRING_FIELD(hrtime);
+       );
+       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 cdr_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 int tds_log(struct ast_cdr *cdr)
 {
-       char sqlcmd[2048], start[80], answer[80], end[80];
-       char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
-       int res = 0;
-       int retried = 0;
-#ifdef FREETDS_PRE_0_62
-       TDS_INT result_type;
-#endif
+       char start[80], answer[80], end[80];
+       char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid, *userfield = NULL;
+       RETCODE erc;
+       int res = -1;
+       int attempt = 1;
 
-       ast_mutex_lock(&tds_lock);
+       accountcode = anti_injection(cdr->accountcode, 20);
+       src         = anti_injection(cdr->src, 80);
+       dst         = anti_injection(cdr->dst, 80);
+       dcontext    = anti_injection(cdr->dcontext, 80);
+       clid        = anti_injection(cdr->clid, 80);
+       channel     = anti_injection(cdr->channel, 80);
+       dstchannel  = anti_injection(cdr->dstchannel, 80);
+       lastapp     = anti_injection(cdr->lastapp, 80);
+       lastdata    = anti_injection(cdr->lastdata, 80);
+       uniqueid    = anti_injection(cdr->uniqueid, 32);
+
+       get_date(start, sizeof(start), cdr->start);
+       get_date(answer, sizeof(answer), cdr->answer);
+       get_date(end, sizeof(end), cdr->end);
 
-       memset(sqlcmd, 0, 2048);
+       ast_mutex_lock(&tds_lock);
 
-       accountcode = anti_injection(cdr->accountcode, 20);
-       src = anti_injection(cdr->src, 80);
-       dst = anti_injection(cdr->dst, 80);
-       dcontext = anti_injection(cdr->dcontext, 80);
-       clid = anti_injection(cdr->clid, 80);
-       channel = anti_injection(cdr->channel, 80);
-       dstchannel = anti_injection(cdr->dstchannel, 80);
-       lastapp = anti_injection(cdr->lastapp, 80);
-       lastdata = anti_injection(cdr->lastdata, 80);
-       uniqueid = anti_injection(cdr->uniqueid, 32);
-
-       get_date(start, cdr->start);
-       get_date(answer, cdr->answer);
-       get_date(end, cdr->end);
-
-       sprintf(
-               sqlcmd,
-               "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 */
-               ")",
-               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) {
+               userfield = anti_injection(cdr->userfield, AST_MAX_USER_FIELD);
+       }
 
-       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");
+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;
+               }
+       }
 
-                       retried = 1;    /* note that we have now tried */
+       if (settings->has_userfield) {
+               if (settings->hrtime) {
+                       double hrbillsec = 0.0;
+                       double hrduration;
+
+                       if (!ast_tvzero(cdr->answer)) {
+                               hrbillsec = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
+                       }
+                       hrduration = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
+
+                       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, %lf, "
+                                        "%lf, '%s', '%s', '%s', '%s'"
+                                        ")",
+                                        settings->table,
+                                        accountcode, src, dst, dcontext, clid, channel,
+                                        dstchannel, lastapp, lastdata, start, answer, end, hrduration,
+                                        hrbillsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(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, 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_channel_amaflags2string(cdr->amaflags), uniqueid,
+                                        userfield
+                       );
+               }
+       } else {
+               if (settings->hrtime) {
+                       double hrbillsec = 0.0;
+                       double hrduration;
+
+                       if (!ast_tvzero(cdr->answer)) {
+                               hrbillsec = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
+                       }
+                       hrduration = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
+
+                       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, %lf, "
+                                        "%lf, '%s', '%s', '%s'"
+                                        ")",
+                                        settings->table,
+                                        accountcode, src, dst, dcontext, clid, channel,
+                                        dstchannel, lastapp, lastdata, start, answer, end, hrduration,
+                                        hrbillsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(cdr->amaflags), uniqueid
+                       );
+               } 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_channel_amaflags2string(cdr->amaflags), uniqueid
+                       );
                }
+       }
 
-#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");
+       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 CDR was logged.\n");
+                       goto done;
+               }
+       }
 
-                       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 CDR was logged.\n");
+                       goto done;
                }
-       } while (!connected && !retried);
-
-       free(accountcode);
-       free(src);
-       free(dst);
-       free(dcontext);
-       free(clid);
-       free(channel);
-       free(dstchannel);
-       free(lastapp);
-       free(lastdata);
-       free(uniqueid);
+       }
 
+       /* 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);
+       }
+
+       res = 0;
+
+done:
        ast_mutex_unlock(&tds_lock);
 
+       ast_free(accountcode);
+       ast_free(src);
+       ast_free(dst);
+       ast_free(dcontext);
+       ast_free(clid);
+       ast_free(channel);
+       ast_free(dstchannel);
+       ast_free(lastapp);
+       ast_free(lastdata);
+       ast_free(uniqueid);
+
+       if (userfield) {
+               ast_free(userfield);
+       }
+
        return res;
 }
 
 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, "cdr_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 tv)
+static void get_date(char *dateField, size_t len, struct timeval when)
 {
-       struct tm tm;
-       time_t t;
-       char buf[80];
-
        /* To make sure we have date variable if not insert null to SQL */
-       if (!ast_tvzero(tv))
-       {
-               t = tv.tv_sec;
-               localtime_r(&t, &tm);
-               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);
        }
 }
 
-const char *description(void)
+static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...)
 {
-       return desc;
-}
+       va_list ap;
+       char *buffer;
 
-static int mssql_disconnect(void)
-{
-       if (tds) {
-               tds_free_socket(tds);
-               tds = NULL;
+       va_start(ap, fmt);
+       if (ast_vasprintf(&buffer, fmt, ap) < 0) {
+               va_end(ap);
+               return 1;
        }
+       va_end(ap);
 
-       if (context) {
-               tds_free_context(context);
-               context = NULL;
+       if (dbfcmd(dbproc, buffer) == FAIL) {
+               ast_free(buffer);
+               return 1;
        }
 
-       if (login) {
-               tds_free_login(login);
-               login = NULL;
+       ast_free(buffer);
+
+       if (dbsqlexec(dbproc) == FAIL) {
+               return 1;
        }
 
-       connected = 0;
+       /* 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) {
+               dbclose(settings->dbproc);
+               settings->dbproc = NULL;
+       }
+
+       settings->connected = 0;
 
        return 0;
 }
 
 static int mssql_connect(void)
 {
-#ifdef FREETDS_0_63
-       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);
-
-       if (!(context = tds_alloc_context()))
-       {
-               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 */
-#ifdef FREETDS_0_63
-               tds_free_connection(connection);
-#else
-               tds_free_connect(connection);
-#endif
-               connection = NULL;
-               goto connect_fail;
-       }
-#ifdef FREETDS_0_63
-       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] WHERE 1 = 0", 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 (ast_cdr_unregister(name)) {
+               return -1;
+       }
 
-       ast_cdr_unregister(name);
+       if (settings) {
+               ast_mutex_lock(&tds_lock);
+               mssql_disconnect();
+               ast_mutex_unlock(&tds_lock);
+
+               ast_string_field_free_memory(settings);
+               ast_free(settings);
+       }
 
-       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);
+       dbexit();
 
        return 0;
 }
 
-static int tds_load_module(void)
+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)
 {
-       int res = 0;
        struct ast_config *cfg;
-       struct ast_variable *var;
-       char *ptr = NULL;
-#ifdef FREETDS_PRE_0_62
-       TDS_INT result_type;
-#endif
-
-       cfg = ast_config_load(config);
-       if (!cfg) {
-               ast_log(LOG_NOTICE, "Unable to load config for MSSQL CDR's: %s\n", config);
+       const char *ptr = NULL;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+
+       cfg = ast_config_load(config, config_flags);
+       if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_NOTICE, "Unable to load TDS config for CDRs: %s\n", config);
+               return 0;
+       } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
                return 0;
-       }
 
-       var = ast_variable_browse(cfg, "global");
-       if (!var) /* nothing configured */
+       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");
+       /* 'connection' is the new preferred configuration option */
+       ptr = ast_variable_retrieve(cfg, "global", "connection");
+       if (ptr) {
+               ast_string_field_set(settings, hostname, ptr);
+       } else {
+               /* But we keep 'hostname' for backwards compatibility */
+               ptr = ast_variable_retrieve(cfg, "global", "hostname");
+               if (ptr) {
+                       ast_string_field_set(settings, hostname, ptr);
+               } else {
+                       ast_log(LOG_ERROR, "Failed to connect: Database server connection 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");
+       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 == NULL) {
-               ast_log(LOG_DEBUG,"cdr_tds: table not specified.  Assuming cdr\n");
-               ptr = "cdr";
+       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 'cdr' by default.\n");
+               ast_string_field_set(settings, table, "cdr");
        }
-       table = strdup(ptr);
 
-       ast_config_destroy(cfg);
+       ptr = ast_variable_retrieve(cfg, "global", "hrtime");
+       if (ptr && ast_true(ptr)) {
+               ast_string_field_set(settings, hrtime, ptr);
+       } else {
+               ast_log(LOG_NOTICE, "High Resolution Time not found, using integers for billsec and duration fields by default.\n");
+       }
 
-       mssql_connect();
+       mssql_disconnect();
 
-       /* Register MSSQL CDR handler */
-       res = ast_cdr_register(name, desc, tds_log);
-       if (res)
-       {
-               ast_log(LOG_ERROR, "Unable to register MSSQL CDR handling\n");
+       if (mssql_connect()) {
+               /* We failed to connect (mssql_connect takes care of logging it) */
+               goto failed;
        }
 
-       return res;
-}
+       ast_mutex_unlock(&tds_lock);
+       ast_config_destroy(cfg);
 
-int reload(void)
-{
-       tds_unload_module();
-       return tds_load_module();
-}
+       return 1;
 
-int load_module(void)
-{
-       return tds_load_module();
+failed:
+       ast_mutex_unlock(&tds_lock);
+       ast_config_destroy(cfg);
+
+       return 0;
 }
 
-int unload_module(void)
+static int reload(void)
 {
-       return tds_unload_module();
+       return tds_load_module(1);
 }
 
-int usecount(void)
+static int load_module(void)
 {
-       /* Simplistic use count */
-       if (ast_mutex_trylock(&tds_lock)) {
-               return 1;
-       } else {
-               ast_mutex_unlock(&tds_lock);
-               return 0;
+       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_with_stringfields(1, struct cdr_tds_config, 256);
+
+       if (!settings) {
+               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;
        }
+
+       ast_cdr_register(name, ast_module_info->description, tds_log);
+
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
-const char *key()
+static int unload_module(void)
 {
-       return ASTERISK_GPL_KEY;
+       return tds_unload_module();
 }
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "FreeTDS CDR Backend",
+       .support_level = AST_MODULE_SUPPORT_EXTENDED,
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload,
+       .load_pri = AST_MODPRI_CDR_DRIVER,
+);