2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2003-2005, Digium, Inc.
6 * Brian K. West <brian@bkw.org>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief ODBC CDR Backend
23 * \author Brian K. West <brian@bkw.org>
26 * \arg http://www.unixodbc.org
27 * \arg \ref Config_cdr
28 * \ingroup cdr_drivers
32 <depend>unixodbc</depend>
38 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
40 #include <sys/types.h>
54 #include <w32api/sql.h>
55 #include <w32api/sqlext.h>
56 #include <w32api/sqltypes.h>
59 #include "asterisk/config.h"
60 #include "asterisk/options.h"
61 #include "asterisk/channel.h"
62 #include "asterisk/cdr.h"
63 #include "asterisk/module.h"
64 #include "asterisk/logger.h"
66 #define DATE_FORMAT "%Y-%m-%d %T"
68 static char *name
= "ODBC";
69 static char *config
= "cdr_odbc.conf";
70 static char *dsn
= NULL
, *username
= NULL
, *password
= NULL
, *table
= NULL
;
71 static int loguniqueid
= 0;
72 static int usegmtime
= 0;
73 static int dispositionstring
= 0;
74 static int connected
= 0;
76 AST_MUTEX_DEFINE_STATIC(odbc_lock
);
78 static int odbc_do_query(void);
79 static int odbc_init(void);
81 static SQLHENV ODBC_env
= SQL_NULL_HANDLE
; /* global ODBC Environment */
82 static SQLHDBC ODBC_con
; /* global ODBC Connection Handle */
83 static SQLHSTMT ODBC_stmt
; /* global ODBC Statement Handle */
85 static void odbc_disconnect(void)
87 SQLDisconnect(ODBC_con
);
88 SQLFreeHandle(SQL_HANDLE_DBC
, ODBC_con
);
89 SQLFreeHandle(SQL_HANDLE_ENV
, ODBC_env
);
93 static int odbc_log(struct ast_cdr
*cdr
)
96 char sqlcmd
[2048] = "", timestr
[128];
101 gmtime_r(&cdr
->start
.tv_sec
,&tm
);
103 ast_localtime(&cdr
->start
.tv_sec
, &tm
, NULL
);
105 ast_mutex_lock(&odbc_lock
);
106 strftime(timestr
, sizeof(timestr
), DATE_FORMAT
, &tm
);
107 memset(sqlcmd
,0,2048);
109 snprintf(sqlcmd
,sizeof(sqlcmd
),"INSERT INTO %s "
110 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
111 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
112 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table
);
114 snprintf(sqlcmd
,sizeof(sqlcmd
),"INSERT INTO %s "
115 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
116 "duration,billsec,disposition,amaflags,accountcode) "
117 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table
);
124 ast_mutex_unlock(&odbc_lock
);
129 ODBC_res
= SQLAllocHandle(SQL_HANDLE_STMT
, ODBC_con
, &ODBC_stmt
);
131 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
132 if (option_verbose
> 10)
133 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Failure in AllocStatement %d\n", ODBC_res
);
134 SQLFreeHandle(SQL_HANDLE_STMT
, ODBC_stmt
);
136 ast_mutex_unlock(&odbc_lock
);
140 /* We really should only have to do this once. But for some
141 strange reason if I don't it blows holes in memory like
142 like a shotgun. So we just do this so its safe. */
144 ODBC_res
= SQLPrepare(ODBC_stmt
, (unsigned char *)sqlcmd
, SQL_NTS
);
146 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
147 if (option_verbose
> 10)
148 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error in PREPARE %d\n", ODBC_res
);
149 SQLFreeHandle(SQL_HANDLE_STMT
, ODBC_stmt
);
151 ast_mutex_unlock(&odbc_lock
);
155 SQLBindParameter(ODBC_stmt
, 1, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(timestr
), 0, ×tr
, 0, NULL
);
156 SQLBindParameter(ODBC_stmt
, 2, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->clid
), 0, cdr
->clid
, 0, NULL
);
157 SQLBindParameter(ODBC_stmt
, 3, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->src
), 0, cdr
->src
, 0, NULL
);
158 SQLBindParameter(ODBC_stmt
, 4, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->dst
), 0, cdr
->dst
, 0, NULL
);
159 SQLBindParameter(ODBC_stmt
, 5, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->dcontext
), 0, cdr
->dcontext
, 0, NULL
);
160 SQLBindParameter(ODBC_stmt
, 6, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->channel
), 0, cdr
->channel
, 0, NULL
);
161 SQLBindParameter(ODBC_stmt
, 7, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->dstchannel
), 0, cdr
->dstchannel
, 0, NULL
);
162 SQLBindParameter(ODBC_stmt
, 8, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->lastapp
), 0, cdr
->lastapp
, 0, NULL
);
163 SQLBindParameter(ODBC_stmt
, 9, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->lastdata
), 0, cdr
->lastdata
, 0, NULL
);
164 SQLBindParameter(ODBC_stmt
, 10, SQL_PARAM_INPUT
, SQL_C_SLONG
, SQL_INTEGER
, 0, 0, &cdr
->duration
, 0, NULL
);
165 SQLBindParameter(ODBC_stmt
, 11, SQL_PARAM_INPUT
, SQL_C_SLONG
, SQL_INTEGER
, 0, 0, &cdr
->billsec
, 0, NULL
);
166 if (dispositionstring
)
167 SQLBindParameter(ODBC_stmt
, 12, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, strlen(ast_cdr_disp2str(cdr
->disposition
)) + 1, 0, ast_cdr_disp2str(cdr
->disposition
), 0, NULL
);
169 SQLBindParameter(ODBC_stmt
, 12, SQL_PARAM_INPUT
, SQL_C_SLONG
, SQL_INTEGER
, 0, 0, &cdr
->disposition
, 0, NULL
);
170 SQLBindParameter(ODBC_stmt
, 13, SQL_PARAM_INPUT
, SQL_C_SLONG
, SQL_INTEGER
, 0, 0, &cdr
->amaflags
, 0, NULL
);
171 SQLBindParameter(ODBC_stmt
, 14, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->accountcode
), 0, cdr
->accountcode
, 0, NULL
);
174 SQLBindParameter(ODBC_stmt
, 15, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->uniqueid
), 0, cdr
->uniqueid
, 0, NULL
);
175 SQLBindParameter(ODBC_stmt
, 16, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, sizeof(cdr
->userfield
), 0, cdr
->userfield
, 0, NULL
);
179 res
= odbc_do_query();
181 if (option_verbose
> 10)
182 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Query FAILED Call not logged!\n");
183 if (option_verbose
> 10)
184 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Reconnecting to dsn %s\n", dsn
);
185 SQLDisconnect(ODBC_con
);
188 if (option_verbose
> 10)
189 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: %s has gone away!\n", dsn
);
192 if (option_verbose
> 10)
193 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Trying Query again!\n");
194 res
= odbc_do_query();
196 if (option_verbose
> 10)
197 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Query FAILED Call not logged!\n");
202 if (option_verbose
> 10)
203 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Query FAILED Call not logged!\n");
205 SQLFreeHandle(SQL_HANDLE_STMT
, ODBC_stmt
);
206 ast_mutex_unlock(&odbc_lock
);
210 static int odbc_unload_module(void)
212 ast_mutex_lock(&odbc_lock
);
214 if (option_verbose
> 10)
215 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Disconnecting from %s\n", dsn
);
216 SQLFreeHandle(SQL_HANDLE_STMT
, ODBC_stmt
);
220 if (option_verbose
> 10)
221 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: free dsn\n");
225 if (option_verbose
> 10)
226 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: free username\n");
230 if (option_verbose
> 10)
231 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: free password\n");
235 if (option_verbose
> 10)
236 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: free table\n");
240 ast_cdr_unregister(name
);
241 ast_mutex_unlock(&odbc_lock
);
245 static int odbc_load_module(void)
248 struct ast_config
*cfg
;
249 struct ast_variable
*var
;
252 ast_mutex_lock(&odbc_lock
);
254 cfg
= ast_config_load(config
);
256 ast_log(LOG_WARNING
, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config
);
257 res
= AST_MODULE_LOAD_DECLINE
;
261 var
= ast_variable_browse(cfg
, "global");
263 /* nothing configured */
267 tmp
= ast_variable_retrieve(cfg
,"global","dsn");
269 ast_log(LOG_WARNING
,"cdr_odbc: dsn not specified. Assuming asteriskdb\n");
274 ast_log(LOG_ERROR
,"cdr_odbc: Out of memory error.\n");
279 tmp
= ast_variable_retrieve(cfg
,"global","dispositionstring");
281 dispositionstring
= ast_true(tmp
);
283 dispositionstring
= 0;
286 tmp
= ast_variable_retrieve(cfg
,"global","username");
288 username
= strdup(tmp
);
289 if (username
== NULL
) {
290 ast_log(LOG_ERROR
,"cdr_odbc: Out of memory error.\n");
296 tmp
= ast_variable_retrieve(cfg
,"global","password");
298 password
= strdup(tmp
);
299 if (password
== NULL
) {
300 ast_log(LOG_ERROR
,"cdr_odbc: Out of memory error.\n");
306 tmp
= ast_variable_retrieve(cfg
,"global","loguniqueid");
308 loguniqueid
= ast_true(tmp
);
310 ast_log(LOG_DEBUG
,"cdr_odbc: Logging uniqueid\n");
312 ast_log(LOG_DEBUG
,"cdr_odbc: Not logging uniqueid\n");
315 ast_log(LOG_DEBUG
,"cdr_odbc: Not logging uniqueid\n");
319 tmp
= ast_variable_retrieve(cfg
,"global","usegmtime");
321 usegmtime
= ast_true(tmp
);
323 ast_log(LOG_DEBUG
,"cdr_odbc: Logging in GMT\n");
325 ast_log(LOG_DEBUG
,"cdr_odbc: Not logging in GMT\n");
328 ast_log(LOG_DEBUG
,"cdr_odbc: Not logging in GMT\n");
332 tmp
= ast_variable_retrieve(cfg
,"global","table");
334 ast_log(LOG_WARNING
,"cdr_odbc: table not specified. Assuming cdr\n");
339 ast_log(LOG_ERROR
,"cdr_odbc: Out of memory error.\n");
344 if (option_verbose
> 2) {
345 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: dsn is %s\n",dsn
);
348 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: username is %s\n",username
);
349 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: password is [secret]\n");
352 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: retreiving username and password from odbc config\n");
353 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: table is %s\n",table
);
358 ast_log(LOG_ERROR
, "cdr_odbc: Unable to connect to datasource: %s\n", dsn
);
359 if (option_verbose
> 2) {
360 ast_verbose( VERBOSE_PREFIX_3
"cdr_odbc: Unable to connect to datasource: %s\n", dsn
);
363 res
= ast_cdr_register(name
, ast_module_info
->description
, odbc_log
);
365 ast_log(LOG_ERROR
, "cdr_odbc: Unable to register ODBC CDR handling\n");
369 ast_config_destroy(cfg
);
370 ast_mutex_unlock(&odbc_lock
);
374 static int odbc_do_query(void)
378 ODBC_res
= SQLExecute(ODBC_stmt
);
380 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
381 if (option_verbose
> 10)
382 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error in Query %d\n", ODBC_res
);
383 SQLFreeHandle(SQL_HANDLE_STMT
, ODBC_stmt
);
387 if (option_verbose
> 10)
388 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Query Successful!\n");
394 static int odbc_init(void)
398 if (ODBC_env
== SQL_NULL_HANDLE
|| connected
== 0) {
399 ODBC_res
= SQLAllocHandle(SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &ODBC_env
);
400 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
401 if (option_verbose
> 10)
402 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error AllocHandle\n");
407 ODBC_res
= SQLSetEnvAttr(ODBC_env
, SQL_ATTR_ODBC_VERSION
, (void*)SQL_OV_ODBC3
, 0);
409 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
410 if (option_verbose
> 10)
411 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error SetEnv\n");
412 SQLFreeHandle(SQL_HANDLE_ENV
, ODBC_env
);
417 ODBC_res
= SQLAllocHandle(SQL_HANDLE_DBC
, ODBC_env
, &ODBC_con
);
419 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
420 if (option_verbose
> 10)
421 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error AllocHDB %d\n", ODBC_res
);
422 SQLFreeHandle(SQL_HANDLE_ENV
, ODBC_env
);
426 SQLSetConnectAttr(ODBC_con
, SQL_LOGIN_TIMEOUT
, (SQLPOINTER
*)10, 0);
429 /* Note that the username and password could be NULL here, but that is allowed in ODBC.
430 In this case, the default username and password will be used from odbc.conf */
431 ODBC_res
= SQLConnect(ODBC_con
, (SQLCHAR
*)dsn
, SQL_NTS
, (SQLCHAR
*)username
, SQL_NTS
, (SQLCHAR
*)password
, SQL_NTS
);
433 if ((ODBC_res
!= SQL_SUCCESS
) && (ODBC_res
!= SQL_SUCCESS_WITH_INFO
)) {
434 if (option_verbose
> 10)
435 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Error SQLConnect %d\n", ODBC_res
);
436 SQLFreeHandle(SQL_HANDLE_DBC
, ODBC_con
);
437 SQLFreeHandle(SQL_HANDLE_ENV
, ODBC_env
);
441 if (option_verbose
> 10)
442 ast_verbose( VERBOSE_PREFIX_4
"cdr_odbc: Connected to %s\n", dsn
);
448 static int load_module(void)
450 return odbc_load_module();
453 static int unload_module(void)
455 return odbc_unload_module();
458 static int reload(void)
460 odbc_unload_module();
461 return odbc_load_module();
464 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "ODBC CDR Backend",
466 .unload
= unload_module
,