(closes issue #11594)
[asterisk-bristuff.git] / cdr / cdr_odbc.c
blob2cd559af895e044fa122d3a4094fd4b112c72083
1 /*
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.
19 /*! \file
21 * \brief ODBC CDR Backend
23 * \author Brian K. West <brian@bkw.org>
25 * See also:
26 * \arg http://www.unixodbc.org
27 * \arg \ref Config_cdr
28 * \ingroup cdr_drivers
31 /*** MODULEINFO
32 <depend>unixodbc</depend>
33 <depend>ltdl</depend>
34 ***/
36 #include "asterisk.h"
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <time.h>
48 #ifndef __CYGWIN__
49 #include <sql.h>
50 #include <sqlext.h>
51 #include <sqltypes.h>
52 #else
53 #include <windows.h>
54 #include <w32api/sql.h>
55 #include <w32api/sqlext.h>
56 #include <w32api/sqltypes.h>
57 #endif
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);
90 connected = 0;
93 static int odbc_log(struct ast_cdr *cdr)
95 int ODBC_res;
96 char sqlcmd[2048] = "", timestr[128];
97 int res = 0;
98 struct tm tm;
100 if (usegmtime)
101 gmtime_r(&cdr->start.tv_sec,&tm);
102 else
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);
108 if (loguniqueid) {
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);
113 } else {
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);
120 if (!connected) {
121 res = odbc_init();
122 if (res < 0) {
123 odbc_disconnect();
124 ast_mutex_unlock(&odbc_lock);
125 return 0;
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);
135 odbc_disconnect();
136 ast_mutex_unlock(&odbc_lock);
137 return 0;
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);
150 odbc_disconnect();
151 ast_mutex_unlock(&odbc_lock);
152 return 0;
155 SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, &timestr, 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);
168 else
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);
173 if (loguniqueid) {
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);
178 if (connected) {
179 res = odbc_do_query();
180 if (res < 0) {
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);
186 res = odbc_init();
187 if (res < 0) {
188 if (option_verbose > 10)
189 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
190 odbc_disconnect();
191 } else {
192 if (option_verbose > 10)
193 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
194 res = odbc_do_query();
195 if (res < 0) {
196 if (option_verbose > 10)
197 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
201 } else {
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);
207 return 0;
210 static int odbc_unload_module(void)
212 ast_mutex_lock(&odbc_lock);
213 if (connected) {
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);
217 odbc_disconnect();
219 if (dsn) {
220 if (option_verbose > 10)
221 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
222 free(dsn);
224 if (username) {
225 if (option_verbose > 10)
226 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
227 free(username);
229 if (password) {
230 if (option_verbose > 10)
231 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
232 free(password);
234 if (table) {
235 if (option_verbose > 10)
236 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
237 free(table);
240 ast_cdr_unregister(name);
241 ast_mutex_unlock(&odbc_lock);
242 return 0;
245 static int odbc_load_module(void)
247 int res = 0;
248 struct ast_config *cfg;
249 struct ast_variable *var;
250 const char *tmp;
252 ast_mutex_lock(&odbc_lock);
254 cfg = ast_config_load(config);
255 if (!cfg) {
256 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
257 res = AST_MODULE_LOAD_DECLINE;
258 goto out;
261 var = ast_variable_browse(cfg, "global");
262 if (!var) {
263 /* nothing configured */
264 goto out;
267 tmp = ast_variable_retrieve(cfg,"global","dsn");
268 if (tmp == NULL) {
269 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified. Assuming asteriskdb\n");
270 tmp = "asteriskdb";
272 dsn = strdup(tmp);
273 if (dsn == NULL) {
274 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
275 res = -1;
276 goto out;
279 tmp = ast_variable_retrieve(cfg,"global","dispositionstring");
280 if (tmp) {
281 dispositionstring = ast_true(tmp);
282 } else {
283 dispositionstring = 0;
286 tmp = ast_variable_retrieve(cfg,"global","username");
287 if (tmp) {
288 username = strdup(tmp);
289 if (username == NULL) {
290 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
291 res = -1;
292 goto out;
296 tmp = ast_variable_retrieve(cfg,"global","password");
297 if (tmp) {
298 password = strdup(tmp);
299 if (password == NULL) {
300 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
301 res = -1;
302 goto out;
306 tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
307 if (tmp) {
308 loguniqueid = ast_true(tmp);
309 if (loguniqueid) {
310 ast_log(LOG_DEBUG,"cdr_odbc: Logging uniqueid\n");
311 } else {
312 ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
314 } else {
315 ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
316 loguniqueid = 0;
319 tmp = ast_variable_retrieve(cfg,"global","usegmtime");
320 if (tmp) {
321 usegmtime = ast_true(tmp);
322 if (usegmtime) {
323 ast_log(LOG_DEBUG,"cdr_odbc: Logging in GMT\n");
324 } else {
325 ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
327 } else {
328 ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
329 usegmtime = 0;
332 tmp = ast_variable_retrieve(cfg,"global","table");
333 if (tmp == NULL) {
334 ast_log(LOG_WARNING,"cdr_odbc: table not specified. Assuming cdr\n");
335 tmp = "cdr";
337 table = strdup(tmp);
338 if (table == NULL) {
339 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
340 res = -1;
341 goto out;
344 if (option_verbose > 2) {
345 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn);
346 if (username)
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");
351 else
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);
356 res = odbc_init();
357 if (res < 0) {
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);
364 if (res) {
365 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
367 out:
368 if (cfg)
369 ast_config_destroy(cfg);
370 ast_mutex_unlock(&odbc_lock);
371 return res;
374 static int odbc_do_query(void)
376 int ODBC_res;
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);
384 odbc_disconnect();
385 return -1;
386 } else {
387 if (option_verbose > 10)
388 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
389 connected = 1;
391 return 0;
394 static int odbc_init(void)
396 int ODBC_res;
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");
403 connected = 0;
404 return -1;
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);
413 connected = 0;
414 return -1;
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);
423 connected = 0;
424 return -1;
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);
438 connected = 0;
439 return -1;
440 } else {
441 if (option_verbose > 10)
442 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
443 connected = 1;
445 return 0;
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",
465 .load = load_module,
466 .unload = unload_module,
467 .reload = reload,