patch file cleanup... ick.. kaloz you owe me some love
[openwrt/svn-archive/archive.git] / net / asterisk / patches / 008-cdr_mysql.patch
1 diff -Nru asterisk-1.2.14.org/cdr/cdr_mysql.c asterisk-1.2.14/cdr/cdr_mysql.c
2 --- asterisk-1.2.14.org/cdr/cdr_mysql.c 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.14/cdr/cdr_mysql.c 2006-12-27 09:02:18.000000000 +0100
4 @@ -0,0 +1,493 @@
5 +/*
6 + * Asterisk -- A telephony toolkit for Linux.
7 + *
8 + * MySQL CDR logger
9 + *
10 + * James Sharp <jsharp@psychoses.org>
11 + *
12 + * Modified August 2003
13 + * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
14 + *
15 + * Modified August 6, 2005
16 + * Joseph Benden <joe@thrallingpenguin.com>
17 + * Added mysql connection timeout parameter
18 + * Added an automatic reconnect as to not lose a cdr record
19 + * Cleaned up the original code to match the coding guidelines
20 + *
21 + * This program is free software, distributed under the terms of
22 + * the GNU General Public License.
23 + *
24 + */
25 +
26 +#include <sys/types.h>
27 +
28 +#include <stdio.h>
29 +#include <string.h>
30 +
31 +#include <stdlib.h>
32 +#include <unistd.h>
33 +#include <time.h>
34 +
35 +#include <mysql.h>
36 +#include <errmsg.h>
37 +
38 +#include <sys/stat.h>
39 +#include <sys/types.h>
40 +#include <errno.h>
41 +
42 +#include <asterisk/config.h>
43 +#include <asterisk/options.h>
44 +#include <asterisk/channel.h>
45 +#include <asterisk/cdr.h>
46 +#include <asterisk/module.h>
47 +#include <asterisk/logger.h>
48 +#include <asterisk/cli.h>
49 +
50 +#define DATE_FORMAT "%Y-%m-%d %T"
51 +
52 +static char *desc = "MySQL CDR Backend";
53 +static char *name = "mysql";
54 +static char *config = "cdr_mysql.conf";
55 +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
56 +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
57 +static int dbport = 0;
58 +static int connected = 0;
59 +static time_t connect_time = 0;
60 +static int records = 0;
61 +static int totalrecords = 0;
62 +static int userfield = 0;
63 +static unsigned int timeout = 0;
64 +
65 +AST_MUTEX_DEFINE_STATIC(mysql_lock);
66 +
67 +static MYSQL mysql;
68 +
69 +static char cdr_mysql_status_help[] =
70 +"Usage: cdr mysql status\n"
71 +" Shows current connection status for cdr_mysql\n";
72 +
73 +static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
74 +{
75 + if (connected) {
76 + char status[256], status2[100] = "";
77 + int ctime = time(NULL) - connect_time;
78 + if (dbport)
79 + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
80 + else if (dbsock)
81 + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
82 + else
83 + snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
84 +
85 + if (dbuser && *dbuser)
86 + snprintf(status2, 99, " with username %s", dbuser);
87 + if (dbtable && *dbtable)
88 + snprintf(status2, 99, " using table %s", dbtable);
89 + if (ctime > 31536000) {
90 + ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
91 + } else if (ctime > 86400) {
92 + ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
93 + } else if (ctime > 3600) {
94 + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
95 + } else if (ctime > 60) {
96 + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
97 + } else {
98 + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
99 + }
100 + if (records == totalrecords)
101 + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
102 + else
103 + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
104 + return RESULT_SUCCESS;
105 + } else {
106 + ast_cli(fd, "Not currently connected to a MySQL server.\n");
107 + return RESULT_FAILURE;
108 + }
109 +}
110 +
111 +static struct ast_cli_entry cdr_mysql_status_cli =
112 + { { "cdr", "mysql", "status", NULL },
113 + handle_cdr_mysql_status, "Show connection status of cdr_mysql",
114 + cdr_mysql_status_help, NULL };
115 +
116 +static int mysql_log(struct ast_cdr *cdr)
117 +{
118 + struct tm tm;
119 + struct timeval tv;
120 + struct localuser *u;
121 + char *userfielddata = NULL;
122 + char sqlcmd[2048], timestr[128];
123 + char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
124 + int retries = 5;
125 +#ifdef MYSQL_LOGUNIQUEID
126 + char *uniqueid = NULL;
127 +#endif
128 +
129 + ast_mutex_lock(&mysql_lock);
130 +
131 + memset(sqlcmd, 0, 2048);
132 +
133 + localtime_r(&cdr->start.tv_sec, &tm);
134 + strftime(timestr, 128, DATE_FORMAT, &tm);
135 +
136 +db_reconnect:
137 + if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
138 + /* Attempt to connect */
139 + mysql_init(&mysql);
140 + /* Add option to quickly timeout the connection */
141 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
142 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
143 + }
144 + if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
145 + connected = 1;
146 + connect_time = time(NULL);
147 + records = 0;
148 + } else {
149 + ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.\n", hostname);
150 + connected = 0;
151 + }
152 + } else {
153 + /* Long connection - ping the server */
154 + int error;
155 + if ((error = mysql_ping(&mysql))) {
156 + connected = 0;
157 + records = 0;
158 + switch (error) {
159 + case CR_SERVER_GONE_ERROR:
160 + case CR_SERVER_LOST:
161 + ast_log(LOG_ERROR, "cdr_mysql: Server has gone away. Attempting to reconnect.\n");
162 + break;
163 + default:
164 + ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
165 + }
166 + retries--;
167 + if (retries)
168 + goto db_reconnect;
169 + else
170 + ast_log(LOG_ERROR, "cdr_mysql: Retried to connect fives times, giving up.\n");
171 + }
172 + }
173 +
174 + /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
175 + /* WARNING: This code previously used mysql_real_escape_string, but the use of said function
176 + requires an active connection to a database. If we are not connected, then this function
177 + cannot be used. This is a problem since we need to store off the SQL statement into our
178 + spool file for later restoration.
179 + So the question is, what's the best way to handle this? This works for now.
180 + */
181 + if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
182 + mysql_escape_string(clid, cdr->clid, strlen(cdr->clid));
183 + if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
184 + mysql_escape_string(dcontext, cdr->dcontext, strlen(cdr->dcontext));
185 + if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
186 + mysql_escape_string(channel, cdr->channel, strlen(cdr->channel));
187 + if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
188 + mysql_escape_string(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
189 + if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
190 + mysql_escape_string(lastapp, cdr->lastapp, strlen(cdr->lastapp));
191 + if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
192 + mysql_escape_string(lastdata, cdr->lastdata, strlen(cdr->lastdata));
193 +#ifdef MYSQL_LOGUNIQUEID
194 + if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
195 + mysql_escape_string(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
196 +#endif
197 + if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
198 + mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield));
199 +
200 + /* Check for all alloca failures above at once */
201 +#ifdef MYSQL_LOGUNIQUEID
202 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
203 +#else
204 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
205 +#endif
206 + ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
207 + ast_mutex_unlock(&mysql_lock);
208 + return -1;
209 + }
210 +
211 + ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n");
212 +
213 + if (userfield && userfielddata) {
214 +#ifdef MYSQL_LOGUNIQUEID
215 + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata);
216 +#else
217 + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata);
218 +#endif
219 + } else {
220 +#ifdef MYSQL_LOGUNIQUEID
221 + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid);
222 +#else
223 + sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode);
224 +#endif
225 + }
226 +
227 + ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd);
228 +
229 + if (connected) {
230 + if (mysql_real_query(&mysql, sqlcmd, strlen(sqlcmd))) {
231 + ast_log(LOG_ERROR, "mysql_cdr: Failed to insert into database: (%d) %s", mysql_errno(&mysql), mysql_error(&mysql));
232 + connected = 0;
233 + } else {
234 + records++;
235 + totalrecords++;
236 + }
237 + }
238 + ast_mutex_unlock(&mysql_lock);
239 + return 0;
240 +}
241 +
242 +char *description(void)
243 +{
244 + return desc;
245 +}
246 +
247 +static int my_unload_module(void)
248 +{
249 + ast_cli_unregister(&cdr_mysql_status_cli);
250 + if (connected) {
251 + mysql_close(&mysql);
252 + connected = 0;
253 + records = 0;
254 + }
255 + if (hostname && hostname_alloc) {
256 + free(hostname);
257 + hostname = NULL;
258 + hostname_alloc = 0;
259 + }
260 + if (dbname && dbname_alloc) {
261 + free(dbname);
262 + dbname = NULL;
263 + dbname_alloc = 0;
264 + }
265 + if (dbuser && dbuser_alloc) {
266 + free(dbuser);
267 + dbuser = NULL;
268 + dbuser_alloc = 0;
269 + }
270 + if (dbsock && dbsock_alloc) {
271 + free(dbsock);
272 + dbsock = NULL;
273 + dbsock_alloc = 0;
274 + }
275 + if (dbtable && dbtable_alloc) {
276 + free(dbtable);
277 + dbtable = NULL;
278 + dbtable_alloc = 0;
279 + }
280 + if (password && password_alloc) {
281 + free(password);
282 + password = NULL;
283 + password_alloc = 0;
284 + }
285 + dbport = 0;
286 + ast_cdr_unregister(name);
287 + return 0;
288 +}
289 +
290 +static int my_load_module(void)
291 +{
292 + int res;
293 + struct ast_config *cfg;
294 + struct ast_variable *var;
295 + char *tmp;
296 +
297 + cfg = ast_config_load(config);
298 + if (!cfg) {
299 + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
300 + return 0;
301 + }
302 +
303 + var = ast_variable_browse(cfg, "global");
304 + if (!var) {
305 + /* nothing configured */
306 + return 0;
307 + }
308 +
309 + tmp = ast_variable_retrieve(cfg, "global", "hostname");
310 + if (tmp) {
311 + hostname = malloc(strlen(tmp) + 1);
312 + if (hostname != NULL) {
313 + hostname_alloc = 1;
314 + strcpy(hostname, tmp);
315 + } else {
316 + ast_log(LOG_ERROR, "Out of memory error.\n");
317 + return -1;
318 + }
319 + } else {
320 + ast_log(LOG_WARNING, "MySQL server hostname not specified. Assuming localhost\n");
321 + hostname = "localhost";
322 + }
323 +
324 + tmp = ast_variable_retrieve(cfg, "global", "dbname");
325 + if (tmp) {
326 + dbname = malloc(strlen(tmp) + 1);
327 + if (dbname != NULL) {
328 + dbname_alloc = 1;
329 + strcpy(dbname, tmp);
330 + } else {
331 + ast_log(LOG_ERROR, "Out of memory error.\n");
332 + return -1;
333 + }
334 + } else {
335 + ast_log(LOG_WARNING, "MySQL database not specified. Assuming asteriskcdrdb\n");
336 + dbname = "asteriskcdrdb";
337 + }
338 +
339 + tmp = ast_variable_retrieve(cfg, "global", "user");
340 + if (tmp) {
341 + dbuser = malloc(strlen(tmp) + 1);
342 + if (dbuser != NULL) {
343 + dbuser_alloc = 1;
344 + strcpy(dbuser, tmp);
345 + } else {
346 + ast_log(LOG_ERROR, "Out of memory error.\n");
347 + return -1;
348 + }
349 + } else {
350 + ast_log(LOG_WARNING, "MySQL database user not specified. Assuming root\n");
351 + dbuser = "root";
352 + }
353 +
354 + tmp = ast_variable_retrieve(cfg, "global", "sock");
355 + if (tmp) {
356 + dbsock = malloc(strlen(tmp) + 1);
357 + if (dbsock != NULL) {
358 + dbsock_alloc = 1;
359 + strcpy(dbsock, tmp);
360 + } else {
361 + ast_log(LOG_ERROR, "Out of memory error.\n");
362 + return -1;
363 + }
364 + } else {
365 + ast_log(LOG_WARNING, "MySQL database sock file not specified. Using default\n");
366 + dbsock = NULL;
367 + }
368 +
369 + tmp = ast_variable_retrieve(cfg, "global", "table");
370 + if (tmp) {
371 + dbtable = malloc(strlen(tmp) + 1);
372 + if (dbtable != NULL) {
373 + dbtable_alloc = 1;
374 + strcpy(dbtable, tmp);
375 + } else {
376 + ast_log(LOG_ERROR, "Out of memory error.\n");
377 + return -1;
378 + }
379 + } else {
380 + ast_log(LOG_NOTICE, "MySQL database table not specified. Assuming \"cdr\"\n");
381 + dbtable = "cdr";
382 + }
383 +
384 + tmp = ast_variable_retrieve(cfg, "global", "password");
385 + if (tmp) {
386 + password = malloc(strlen(tmp) + 1);
387 + if (password != NULL) {
388 + password_alloc = 1;
389 + strcpy(password, tmp);
390 + } else {
391 + ast_log(LOG_ERROR, "Out of memory error.\n");
392 + return -1;
393 + }
394 + } else {
395 + ast_log(LOG_WARNING, "MySQL database password not specified. Assuming blank\n");
396 + password = "";
397 + }
398 +
399 + tmp = ast_variable_retrieve(cfg, "global", "port");
400 + if (tmp) {
401 + if (sscanf(tmp, "%d", &dbport) < 1) {
402 + ast_log(LOG_WARNING, "Invalid MySQL port number. Using default\n");
403 + dbport = 0;
404 + }
405 + }
406 +
407 + tmp = ast_variable_retrieve(cfg, "global", "timeout");
408 + if (tmp) {
409 + if (sscanf(tmp,"%d", &timeout) < 1) {
410 + ast_log(LOG_WARNING, "Invalid MySQL timeout number. Using default\n");
411 + timeout = 0;
412 + }
413 + }
414 +
415 + tmp = ast_variable_retrieve(cfg, "global", "userfield");
416 + if (tmp) {
417 + if (sscanf(tmp, "%d", &userfield) < 1) {
418 + ast_log(LOG_WARNING, "Invalid MySQL configurtation file\n");
419 + userfield = 0;
420 + }
421 + }
422 +
423 + ast_config_destroy(cfg);
424 +
425 + ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);
426 + ast_log(LOG_DEBUG, "cdr_mysql: got port of %d\n", dbport);
427 + ast_log(LOG_DEBUG, "cdr_mysql: got a timeout of %d\n", timeout);
428 + if (dbsock)
429 + ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
430 + ast_log(LOG_DEBUG, "cdr_mysql: got user of %s\n", dbuser);
431 + ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname);
432 + ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password);
433 +
434 + mysql_init(&mysql);
435 +
436 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
437 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
438 + }
439 +
440 + if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
441 + ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
442 + connected = 0;
443 + records = 0;
444 + } else {
445 + ast_log(LOG_DEBUG, "Successfully connected to MySQL database.\n");
446 + connected = 1;
447 + records = 0;
448 + connect_time = time(NULL);
449 + }
450 +
451 + res = ast_cdr_register(name, desc, mysql_log);
452 + if (res) {
453 + ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
454 + } else {
455 + res = ast_cli_register(&cdr_mysql_status_cli);
456 + }
457 +
458 + return res;
459 +}
460 +
461 +int load_module(void)
462 +{
463 + return my_load_module();
464 +}
465 +
466 +int unload_module(void)
467 +{
468 + return my_unload_module();
469 +}
470 +
471 +int reload(void)
472 +{
473 + int ret;
474 +
475 + ast_mutex_lock(&mysql_lock);
476 + my_unload_module();
477 + ret = my_load_module();
478 + ast_mutex_unlock(&mysql_lock);
479 +
480 + return ret;
481 +}
482 +
483 +int usecount(void)
484 +{
485 + /* Simplistic use count */
486 + if (ast_mutex_trylock(&mysql_lock)) {
487 + return 1;
488 + } else {
489 + ast_mutex_unlock(&mysql_lock);
490 + return 0;
491 + }
492 +}
493 +
494 +char *key()
495 +{
496 + return ASTERISK_GPL_KEY;
497 +}
498 diff -Nru asterisk-1.2.14.org/configs/cdr_mysql.conf.sample asterisk-1.2.14/configs/cdr_mysql.conf.sample
499 --- asterisk-1.2.14.org/configs/cdr_mysql.conf.sample 1970-01-01 01:00:00.000000000 +0100
500 +++ asterisk-1.2.14/configs/cdr_mysql.conf.sample 2006-12-27 09:02:18.000000000 +0100
501 @@ -0,0 +1,21 @@
502 +;
503 +; Note - if the database server is hosted on the same machine as the
504 +; asterisk server, you can achieve a local Unix socket connection by
505 +; setting hostname=localhost
506 +;
507 +; port and sock are both optional parameters. If hostname is specified
508 +; and is not "localhost", then cdr_mysql will attempt to connect to the
509 +; port specified or use the default port. If hostname is not specified
510 +; or if hostname is "localhost", then cdr_mysql will attempt to connect
511 +; to the socket file specified by sock or otherwise use the default socket
512 +; file.
513 +;
514 +;[global]
515 +;hostname=database.host.name
516 +;dbname=asteriskcdrdb
517 +;table=cdr
518 +;password=password
519 +;user=asteriskcdruser
520 +;port=3306
521 +;sock=/tmp/mysql.sock
522 +;userfield=1