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