fix reload problem - unload_module was not setting connected to 0 (issue #5498)
[asterisk/asterisk.git] / cdr / cdr_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2003-2005, Digium, Inc.
5  *
6  * Brian K. West <brian@bkw.org>
7  *
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.
13  *
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.
17  */
18
19 /*
20  *
21  * ODBC CDR Backend
22  * 
23  */
24
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <time.h>
32
33 #ifndef __CYGWIN__
34 #include <sql.h>
35 #include <sqlext.h>
36 #include <sqltypes.h>
37 #else
38 #include <windows.h>
39 #include <w32api/sql.h>
40 #include <w32api/sqlext.h>
41 #include <w32api/sqltypes.h>
42 #endif
43
44 #include "asterisk.h"
45
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47
48 #include "asterisk/config.h"
49 #include "asterisk/options.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/cdr.h"
52 #include "asterisk/module.h"
53 #include "asterisk/logger.h"
54
55 #define DATE_FORMAT "%Y-%m-%d %T"
56
57 static char *desc = "ODBC CDR Backend";
58 static char *name = "ODBC";
59 static char *config = "cdr_odbc.conf";
60 static char *dsn = NULL, *username = NULL, *password = NULL, *table = NULL;
61 static int loguniqueid = 0;
62 static int usegmtime = 0;
63 static int dispositionstring = 0;
64 static int connected = 0;
65
66 AST_MUTEX_DEFINE_STATIC(odbc_lock);
67
68 static int odbc_do_query(void);
69 static int odbc_init(void);
70
71 static SQLHENV  ODBC_env = SQL_NULL_HANDLE;     /* global ODBC Environment */
72 static SQLHDBC  ODBC_con;                       /* global ODBC Connection Handle */
73 static SQLHSTMT ODBC_stmt;                      /* global ODBC Statement Handle */
74
75 static int odbc_log(struct ast_cdr *cdr)
76 {
77         SQLINTEGER ODBC_err;
78         short int ODBC_mlen;
79         int ODBC_res;
80         char ODBC_msg[200], ODBC_stat[10];
81         char sqlcmd[2048] = "", timestr[128];
82         int res = 0;
83         struct tm tm;
84
85         if (usegmtime) 
86                 gmtime_r(&cdr->start.tv_sec,&tm);
87         else
88                 localtime_r(&cdr->start.tv_sec,&tm);
89
90         ast_mutex_lock(&odbc_lock);
91         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
92         memset(sqlcmd,0,2048);
93         if (loguniqueid) {
94                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
95                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
96                 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
97                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
98         } else {
99                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
100                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
101                 "duration,billsec,disposition,amaflags,accountcode) "
102                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
103         }
104
105         if (!connected) {
106                 res = odbc_init();
107                 if (res < 0) {
108                         connected = 0;
109                         ast_mutex_unlock(&odbc_lock);
110                         return 0;
111                 }                               
112         }
113
114         ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
115
116         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
117                 if (option_verbose > 10)
118                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
119                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
120                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);      
121                 connected = 0;
122                 ast_mutex_unlock(&odbc_lock);
123                 return 0;
124         }
125
126         /* We really should only have to do this once.  But for some
127            strange reason if I don't it blows holes in memory like
128            like a shotgun.  So we just do this so its safe. */
129
130         ODBC_res = SQLPrepare(ODBC_stmt, sqlcmd, SQL_NTS);
131         
132         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
133                 if (option_verbose > 10)
134                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
135                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
136                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
137                 connected = 0;
138                 ast_mutex_unlock(&odbc_lock);
139                 return 0;
140         }
141
142         SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, &timestr, 0, NULL);
143         SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
144         SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
145         SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
146         SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
147         SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
148         SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
149         SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
150         SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
151         SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
152         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
153         if (dispositionstring)
154                 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);
155         else
156                 SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
157         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
158         SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
159
160         if (loguniqueid) {
161                 SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
162                 SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
163         }
164
165         if (connected) {
166                 res = odbc_do_query();
167                 if (res < 0) {
168                         if (option_verbose > 10)                
169                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
170                         res = odbc_init();
171                         if (option_verbose > 10)
172                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
173                         if (res < 0) {
174                                 if (option_verbose > 10)
175                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
176                                 connected = 0;
177                         } else {
178                                 if (option_verbose > 10)
179                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
180                                 res = odbc_do_query();
181                                 if (res < 0) {
182                                         if (option_verbose > 10)
183                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
184                                 }
185                         }
186                 }
187         } else {
188                 if (option_verbose > 10)
189                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
190         }
191         SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
192         ast_mutex_unlock(&odbc_lock);
193         return 0;
194 }
195
196 char *description(void)
197 {
198         return desc;
199 }
200
201 static int odbc_unload_module(void)
202 {
203         ast_mutex_lock(&odbc_lock);
204         if (connected) {
205                 if (option_verbose > 10)
206                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
207                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
208                 SQLDisconnect(ODBC_con);
209                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
210                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
211                 connected = 0;
212         }
213         if (dsn) {
214                 if (option_verbose > 10)
215                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
216                 free(dsn);
217         }
218         if (username) {
219                 if (option_verbose > 10)
220                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
221                 free(username);
222         }
223         if (password) {
224                 if (option_verbose > 10)
225                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
226                 free(password);
227         }
228         if (table) {
229                 if (option_verbose > 10)
230                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
231                 free(table);
232         }
233
234         ast_cdr_unregister(name);
235         ast_mutex_unlock(&odbc_lock);
236         return 0;
237 }
238
239 static int odbc_load_module(void)
240 {
241         int res = 0;
242         struct ast_config *cfg;
243         struct ast_variable *var;
244         char *tmp;
245
246         ast_mutex_lock(&odbc_lock);
247
248         cfg = ast_config_load(config);
249         if (!cfg) {
250                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
251                 goto out;
252         }
253         
254         var = ast_variable_browse(cfg, "global");
255         if (!var) {
256                 /* nothing configured */
257                 goto out;
258         }
259
260         tmp = ast_variable_retrieve(cfg,"global","dsn");
261         if (tmp == NULL) {
262                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
263                 tmp = "asteriskdb";
264         }
265         dsn = strdup(tmp);
266         if (dsn == NULL) {
267                 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
268                 res = -1;
269                 goto out;
270         }
271
272         tmp = ast_variable_retrieve(cfg,"global","dispositionstring");
273         if (tmp) {
274                 dispositionstring = ast_true(tmp);
275         } else {
276                 dispositionstring = 0;
277         }
278                 
279         tmp = ast_variable_retrieve(cfg,"global","username");
280         if (tmp) {
281                 username = strdup(tmp);
282                 if (username == NULL) {
283                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
284                         res = -1;
285                         goto out;
286                 }
287         }
288
289         tmp = ast_variable_retrieve(cfg,"global","password");
290         if (tmp) {
291                 password = strdup(tmp);
292                 if (password == NULL) {
293                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
294                         res = -1;
295                         goto out;
296                 }
297         }
298
299         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
300         if (tmp) {
301                 loguniqueid = ast_true(tmp);
302                 if (loguniqueid) {
303                         ast_log(LOG_DEBUG,"cdr_odbc: Logging uniqueid\n");
304                 } else {
305                         ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
306                 }
307         } else {
308                 ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
309                 loguniqueid = 0;
310         }
311
312         tmp = ast_variable_retrieve(cfg,"global","usegmtime");
313         if (tmp) {
314                 usegmtime = ast_true(tmp);
315                 if (usegmtime) {
316                         ast_log(LOG_DEBUG,"cdr_odbc: Logging in GMT\n");
317                 } else {
318                         ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
319                 }
320         } else {
321                 ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
322                 usegmtime = 0;
323         }
324
325         tmp = ast_variable_retrieve(cfg,"global","table");
326         if (tmp == NULL) {
327                 ast_log(LOG_WARNING,"cdr_odbc: table not specified.  Assuming cdr\n");
328                 tmp = "cdr";
329         }
330         table = strdup(tmp);
331         if (table == NULL) {
332                 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
333                 res = -1;
334                 goto out;
335         }
336
337         ast_config_destroy(cfg);
338         if (option_verbose > 2) {
339                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn);
340                 if (username)
341                 {
342                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: username is %s\n",username);
343                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: password is [secret]\n");
344                 }
345                 else
346                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: retreiving username and password from odbc config\n");
347                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: table is %s\n",table);
348         }
349         
350         res = odbc_init();
351         if (res < 0) {
352                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
353                 if (option_verbose > 2) {
354                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
355                 }
356         }
357         res = ast_cdr_register(name, desc, odbc_log);
358         if (res) {
359                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
360         }
361 out:
362         ast_mutex_unlock(&odbc_lock);
363         return res;
364 }
365
366 static int odbc_do_query(void)
367 {
368         SQLINTEGER ODBC_err;
369         int ODBC_res;
370         short int ODBC_mlen;
371         char ODBC_msg[200], ODBC_stat[10];
372         
373         ODBC_res = SQLExecute(ODBC_stmt);
374         
375         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
376                 if (option_verbose > 10)
377                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
378                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
379                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
380                 connected = 0;
381                 return -1;
382         } else {
383                 if (option_verbose > 10)
384                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
385                 connected = 1;
386         }
387         return 0;
388 }
389
390 static int odbc_init(void)
391 {
392         SQLINTEGER ODBC_err;
393         short int ODBC_mlen;
394         int ODBC_res;
395         char ODBC_msg[200], ODBC_stat[10];
396
397         if (ODBC_env == SQL_NULL_HANDLE || connected == 0) {
398                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
399                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
400                         if (option_verbose > 10)
401                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
402                         connected = 0;
403                         return -1;
404                 }
405
406                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
407
408                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
409                         if (option_verbose > 10)
410                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
411                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
412                         connected = 0;
413                         return -1;
414                 }
415
416                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
417
418                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
419                         if (option_verbose > 10)
420                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
421                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
422                         connected = 0;
423                         return -1;
424                 }
425                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
426         }
427
428         /* Note that the username and password could be NULL here, but that is allowed in ODBC.
429            In this case, the default username and password will be used from odbc.conf */
430         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
431
432         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
433                 if (option_verbose > 10)
434                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
435                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
436                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
437                 connected = 0;
438                 return -1;
439         } else {
440                 if (option_verbose > 10)
441                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
442                 connected = 1;
443         }
444         return 0;
445 }
446
447 int load_module(void)
448 {
449         return odbc_load_module();
450 }
451
452 int unload_module(void)
453 {
454         return odbc_unload_module();
455 }
456
457 int reload(void)
458 {
459         odbc_unload_module();
460         return odbc_load_module();
461 }
462
463 int usecount(void)
464 {
465         /* Simplistic use count */
466         if (ast_mutex_trylock(&odbc_lock)) {
467                 return 1;
468         } else {
469                 ast_mutex_unlock(&odbc_lock);
470                 return 0;
471         }
472 }
473
474 char *key()
475 {
476         return ASTERISK_GPL_KEY;
477 }