1 diff -ruN asterisk-1.0.7-orig/apps/Makefile asterisk-1.0.7-2/apps/Makefile
2 --- asterisk-1.0.7-orig/apps/Makefile 2004-09-24 23:32:56.000000000 +0200
3 +++ asterisk-1.0.7-2/apps/Makefile 2005-03-19 17:38:06.000000000 +0100
8 +app_sql_mysql.o: app_sql_mysql.c
9 + $(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c -o $@ $<
11 +app_sql_mysql.so: app_sql_mysql.o
12 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lmysqlclient -lz $(MYSQL_LFLAGS)
14 app_sql_postgres.o: app_sql_postgres.c
15 - $(CC) -pipe -I/usr/local/pgsql/include $(CFLAGS) -c -o app_sql_postgres.o app_sql_postgres.c
16 + $(CC) $(CFLAGS) $(PGSQL_CFLAGS) -c -o $@ $<
18 app_sql_postgres.so: app_sql_postgres.o
19 - $(CC) $(SOLINK) -o $@ $< -L/usr/local/pgsql/lib -lpq
20 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lpq -lz $(PGSQL_LFLAGS)
22 app_sql_odbc.so: app_sql_odbc.o
23 $(CC) $(SOLINK) -o $@ $< -lodbc
24 diff -ruN asterisk-1.0.7-orig/apps/app_sql_mysql.c asterisk-1.0.7-2/apps/app_sql_mysql.c
25 --- asterisk-1.0.7-orig/apps/app_sql_mysql.c 1970-01-01 01:00:00.000000000 +0100
26 +++ asterisk-1.0.7-2/apps/app_sql_mysql.c 2005-03-19 18:01:13.000000000 +0100
29 + * Asterisk -- A telephony toolkit for Linux.
31 + * Connect to PostgreSQL
33 + * Copyright (C) 2004, Constantine Filin and Christos Ricudis
35 + * Christos Ricudis <ricudis@itc.auth.gr>
36 + * Constantine Filin <cf@intermedia.net>
38 + * This program is free software, distributed under the terms of
39 + * the GNU General Public License
42 +#include <asterisk/file.h>
43 +#include <asterisk/logger.h>
44 +#include <asterisk/channel.h>
45 +#include <asterisk/pbx.h>
46 +#include <asterisk/module.h>
47 +#include <asterisk/linkedlists.h>
48 +#include <asterisk/chanvars.h>
49 +#include <asterisk/lock.h>
54 +#include <sys/types.h>
58 +#include <mysql/mysql.h>
62 +static char *tdesc = "Simple Mysql Interface";
64 +static char *app = "MYSQL";
66 +static char *synopsis = "Do several mySQLy things";
68 +static char *descrip =
69 +"MYSQL(): Do several mySQLy things\n"
71 +" MYSQL(Connect connid dhhost dbuser dbpass dbname)\n"
72 +" Connects to a database. Arguments contain standard MySQL parameters\n"
73 +" passed to function mysql_real_connect. Connection identifer returned\n"
75 +" MYSQL(Query resultid ${connid} query-string)\n"
76 +" Executes standard MySQL query contained in query-string using established\n"
77 +" connection identified by ${connection_identifier}. Result of query is\n"
78 +" is stored in ${var}.\n"
79 +" MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
80 +" Fetches a single row from a result set contained in ${result_identifier}.\n"
81 +" Assigns returned fields to ${var1} ... ${varn}. ${fetchid} is set TRUE\n"
82 +" if additional rows exist in result set.\n"
83 +" MYSQL(Clear ${resultid})\n"
84 +" Frees memory and datastructures associated with result set.\n"
85 +" MYSQL(Disconnect ${connid})\n"
86 +" Disconnects from named connection to MySQL.\n" ;
91 +exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit)
92 +exten => s,3,MYSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
93 +exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
94 +exten => s,5,GotoIf(${fetchid}?6:8)
95 +exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")
96 +exten => s,7,Goto(s,4)
97 +exten => s,8,MYSQL(Clear ${resultid})
98 +exten => s,9,MYSQL(Disconnect ${connid})
101 +STANDARD_LOCAL_USER;
104 +AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
106 +extern void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
108 +#define AST_MYSQL_ID_DUMMY 0
109 +#define AST_MYSQL_ID_CONNID 1
110 +#define AST_MYSQL_ID_RESID 2
111 +#define AST_MYSQL_ID_FETCHID 3
113 +struct ast_MYSQL_id {
114 + int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
117 + AST_LIST_ENTRY(ast_MYSQL_id) entries;
120 +AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
123 +static void *find_identifier(int identifier,int identifier_type) {
124 + struct MYSQLidshead *headp;
125 + struct ast_MYSQL_id *i;
129 + headp=&_mysql_ids_head;
131 + if (AST_LIST_LOCK(headp)) {
132 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
134 + AST_LIST_TRAVERSE(headp,i,entries) {
135 + if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
142 + ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
144 + AST_LIST_UNLOCK(headp);
150 +static int add_identifier(int identifier_type,void *data) {
151 + struct ast_MYSQL_id *i,*j;
152 + struct MYSQLidshead *headp;
153 + int maxidentifier=0;
155 + headp=&_mysql_ids_head;
159 + if (AST_LIST_LOCK(headp)) {
160 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
163 + i=malloc(sizeof(struct ast_MYSQL_id));
164 + AST_LIST_TRAVERSE(headp,j,entries) {
165 + if (j->identifier>maxidentifier) {
166 + maxidentifier=j->identifier;
169 + i->identifier=maxidentifier+1;
170 + i->identifier_type=identifier_type;
172 + AST_LIST_INSERT_HEAD(headp,i,entries);
173 + AST_LIST_UNLOCK(headp);
175 + return i->identifier;
178 +static int del_identifier(int identifier,int identifier_type) {
179 + struct ast_MYSQL_id *i;
180 + struct MYSQLidshead *headp;
183 + headp=&_mysql_ids_head;
185 + if (AST_LIST_LOCK(headp)) {
186 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
188 + AST_LIST_TRAVERSE(headp,i,entries) {
189 + if ((i->identifier==identifier) &&
190 + (i->identifier_type==identifier_type)) {
191 + AST_LIST_REMOVE(headp,i,ast_MYSQL_id,entries);
197 + AST_LIST_UNLOCK(headp);
201 + ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
208 +static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
211 + snprintf(s, sizeof(s)-1, "%d", id);
213 + ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
215 + pbx_builtin_setvar_helper(chan,varname,s);
220 +static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data) {
221 + return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
224 +static int safe_scan_int( char** data, char* delim, int def ) {
227 + char* s = strsep(data,delim);
229 + res = strtol(s,&end,10);
230 + if (*end) res = def; /* not an integer */
235 +/* MYSQL operations */
236 +static int aMYSQL_connect(struct ast_channel *chan, char *data) {
246 + strsep(&data," "); // eat the first token, we already know it :P
248 + connid_var=strsep(&data," ");
249 + dbhost=strsep(&data," ");
250 + dbuser=strsep(&data," ");
251 + dbpass=strsep(&data," ");
252 + dbname=strsep(&data,"\n");
254 + if( connid_var && dbhost && dbuser && dbpass && dbname ) {
255 + mysql = mysql_init(NULL);
257 + if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
258 + add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
262 + ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
266 + ast_log(LOG_WARNING,"myslq_init returned NULL\n");
270 + ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
276 +static int aMYSQL_query(struct ast_channel *chan, char *data) {
279 + MYSQL_RES *mysqlres;
281 + char *resultid_var;
285 + strsep(&data," "); // eat the first token, we already know it :P
287 + resultid_var = strsep(&data," ");
288 + connid = safe_scan_int(&data," ",-1);
289 + querystring = strsep(&data,"\n");
291 + if (resultid_var && (connid>=0) && querystring) {
292 + if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
293 + mysql_query(mysql,querystring);
294 + if ((mysqlres=mysql_use_result(mysql))!=NULL) {
295 + add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
298 + else if( mysql_field_count(mysql)==0 ) {
299 + return 0; // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
302 + ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
306 + ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
310 + ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
317 +static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
319 + MYSQL_RES *mysqlres;
320 + MYSQL_ROW mysqlrow;
322 + char *fetchid_var,*s5,*s6;
323 + int resultid,numFields,j;
325 + strsep(&data," "); // eat the first token, we already know it :P
327 + fetchid_var = strsep(&data," ");
328 + resultid = safe_scan_int(&data," ",-1);
330 + if (fetchid_var && (resultid>=0) ) {
331 + if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
332 + /* Grab the next row */
333 + if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
334 + numFields=mysql_num_fields(mysqlres);
335 + for (j=0;j<numFields;j++) {
336 + s5=strsep(&data," ");
338 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
342 + pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
345 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
347 + set_asterisk_int(chan,fetchid_var,1); // try more rows
350 + ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
352 + set_asterisk_int(chan,fetchid_var,0); // no more rows
357 + ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
361 + ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
367 +static int aMYSQL_clear(struct ast_channel *chan, char *data) {
369 + MYSQL_RES *mysqlres;
372 + strsep(&data," "); // eat the first token, we already know it :P
373 + id = safe_scan_int(&data," \n",-1);
374 + if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
375 + ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
377 + mysql_free_result(mysqlres);
378 + del_identifier(id,AST_MYSQL_ID_RESID);
384 +static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
388 + strsep(&data," "); // eat the first token, we already know it :P
390 + id = safe_scan_int(&data," \n",-1);
391 + if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
392 + ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
394 + mysql_close(mysql);
395 + del_identifier(id,AST_MYSQL_ID_CONNID);
401 +static int MYSQL_exec(struct ast_channel *chan, void *data)
403 + struct localuser *u;
407 + fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
411 + ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
418 + ast_mutex_lock(&_mysql_mutex);
420 + if (strncasecmp("connect",data,strlen("connect"))==0) {
421 + result=aMYSQL_connect(chan,ast_strdupa(data));
422 + } else if (strncasecmp("query",data,strlen("query"))==0) {
423 + result=aMYSQL_query(chan,ast_strdupa(data));
424 + } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
425 + result=aMYSQL_fetch(chan,ast_strdupa(data));
426 + } else if (strncasecmp("clear",data,strlen("clear"))==0) {
427 + result=aMYSQL_clear(chan,ast_strdupa(data));
428 + } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
429 + result=aMYSQL_disconnect(chan,ast_strdupa(data));
431 + ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
435 + ast_mutex_unlock(&_mysql_mutex);
437 + LOCAL_USER_REMOVE(u);
442 +int unload_module(void)
444 + STANDARD_HANGUP_LOCALUSERS;
445 + return ast_unregister_application(app);
448 +int load_module(void)
450 + struct MYSQLidshead *headp = &_mysql_ids_head;
451 + AST_LIST_HEAD_INIT(headp);
452 + return ast_register_application(app, MYSQL_exec, synopsis, descrip);
455 +char *description(void)
463 + STANDARD_USECOUNT(res);
469 + return ASTERISK_GPL_KEY;
471 diff -ruN asterisk-1.0.7-orig/cdr/Makefile asterisk-1.0.7-2/cdr/Makefile
472 --- asterisk-1.0.7-orig/cdr/Makefile 2004-08-31 18:33:00.000000000 +0200
473 +++ asterisk-1.0.7-2/cdr/Makefile 2005-03-19 17:38:06.000000000 +0100
478 -MODS+=$(shell if [ -f "/usr/include/odbcinst.h" ]; then echo "cdr_odbc.so"; fi)
479 -MODS+=$(shell if [ -f "/usr/local/include/odbcinst.h" ]; then echo "cdr_odbc.so"; fi)
480 +#MODS+=$(shell if [ -f "/usr/include/odbcinst.h" ]; then echo "cdr_odbc.so"; fi)
481 +#MODS+=$(shell if [ -f "/usr/local/include/odbcinst.h" ]; then echo "cdr_odbc.so"; fi)
486 -MODS+=$(shell if [ -f "/usr/include/tds.h" ]; then echo "cdr_tds.so"; fi)
487 -MODS+=$(shell if [ -f "/usr/local/include/tds.h" ]; then echo "cdr_tds.so"; fi)
488 +#MODS+=$(shell if [ -f "/usr/include/tds.h" ]; then echo "cdr_tds.so"; fi)
489 +#MODS+=$(shell if [ -f "/usr/local/include/tds.h" ]; then echo "cdr_tds.so"; fi)
492 # PGSQL stuff... Autoconf anyone??
494 -MODS+=$(shell if [ -d /usr/local/pgsql/include ] || [ -d /usr/include/pgsql ] || [ -d /usr/local/include/pgsql ] || [ -d /opt/pgsql/include ] || [ -f /usr/include/libpq-fe.h ] ; then echo "cdr_pgsql.so"; fi)
495 -CFLAGS+=$(shell if [ -d /usr/local/pgsql/include ]; then echo "-I/usr/local/pgsql/include"; fi)
496 -CFLAGS+=$(shell if [ -d /usr/include/pgsql ]; then echo "-I/usr/include/pgsql"; fi)
497 -CFLAGS+=$(shell if [ -d /usr/include/postgresql ]; then echo "-I/usr/include/postgresql"; fi)
498 -CFLAGS+=$(shell if [ -d /usr/local/include/pgsql ]; then echo "-I/usr/local/include/pgsql"; fi)
499 -CFLAGS+=$(shell if [ -d /opt/pgsql/include ]; then echo "-I/opt/pgsql/include"; fi)
500 +#MODS+=$(shell if [ -d /usr/local/pgsql/include ] || [ -d /usr/include/pgsql ] || [ -d /usr/local/include/pgsql ] || [ -d /opt/pgsql/include ] || [ -f /usr/include/libpq-fe.h ] ; then echo "cdr_pgsql.so"; fi)
501 +#CFLAGS+=$(shell if [ -d /usr/local/pgsql/include ]; then echo "-I/usr/local/pgsql/include"; fi)
502 +#CFLAGS+=$(shell if [ -d /usr/include/pgsql ]; then echo "-I/usr/include/pgsql"; fi)
503 +#CFLAGS+=$(shell if [ -d /usr/include/postgresql ]; then echo "-I/usr/include/postgresql"; fi)
504 +#CFLAGS+=$(shell if [ -d /usr/local/include/pgsql ]; then echo "-I/usr/local/include/pgsql"; fi)
505 +#CFLAGS+=$(shell if [ -d /opt/pgsql/include ]; then echo "-I/opt/pgsql/include"; fi)
506 #CFLAGS+=$(shell if [ -f /usr/include/libpq-fe.h ]; then echo "-I/usr/include"; fi)
508 -MLFLAGS+=$(shell if [ -d /usr/lib/pgsql ]; then echo "-L/usr/lib/pgsql"; fi)
509 -MLFLAGS+=$(shell if [ -d /usr/local/pgsql/lib ]; then echo "-L/usr/local/pgsql/lib"; fi)
510 -MLFLAGS+=$(shell if [ -d /usr/local/lib/pgsql ]; then echo "-L/usr/local/lib/pgsql"; fi)
511 -MLFLAGS+=$(shell if [ -d /opt/pgsql/lib ]; then echo "-L/opt/pgsql/lib"; fi)
512 -MLFLAGS+=$(shell if [ -f /usr/lib/libpq.so ]; then echo "-L/usr/lib"; fi)
513 +#MLFLAGS+=$(shell if [ -d /usr/lib/pgsql ]; then echo "-L/usr/lib/pgsql"; fi)
514 +#MLFLAGS+=$(shell if [ -d /usr/local/pgsql/lib ]; then echo "-L/usr/local/pgsql/lib"; fi)
515 +#MLFLAGS+=$(shell if [ -d /usr/local/lib/pgsql ]; then echo "-L/usr/local/lib/pgsql"; fi)
516 +#MLFLAGS+=$(shell if [ -d /opt/pgsql/lib ]; then echo "-L/opt/pgsql/lib"; fi)
517 +#MLFLAGS+=$(shell if [ -f /usr/lib/libpq.so ]; then echo "-L/usr/lib"; fi)
522 -MODS+=$(shell if [ -f "/usr/include/sqlite.h" ]; then echo "cdr_sqlite.so"; fi)
523 +#MODS+=$(shell if [ -f "/usr/include/sqlite.h" ]; then echo "cdr_sqlite.so"; fi)
528 cdr_tds.so: cdr_tds.o
529 $(CC) $(SOLINK) -o $@ $< -ltds $(MLFLAGS)
531 +cdr_mysql.o: cdr_mysql.c
532 + $(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c -o $@ $<
534 +cdr_mysql.so: cdr_mysql.o
535 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lmysqlclient -lz $(MYSQL_LFLAGS)
537 +cdr_pgsql.o: cdr_pgsql.c
538 + $(CC) $(CFLAGS) $(PGSQL_CFLAGS) -c -o $@ $<
540 cdr_pgsql.so: cdr_pgsql.o
541 - $(CC) $(SOLINK) -o $@ $< -lpq -lz $(MLFLAGS)
542 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lpq -lz $(PGSQL_LFLAGS)
544 cdr_sqlite.so: cdr_sqlite.o
545 $(CC) $(SOLINK) -o $@ $< -lsqlite $(MLFLAGS)
546 diff -ruN asterisk-1.0.7-orig/cdr/cdr_mysql.c asterisk-1.0.7-2/cdr/cdr_mysql.c
547 --- asterisk-1.0.7-orig/cdr/cdr_mysql.c 1970-01-01 01:00:00.000000000 +0100
548 +++ asterisk-1.0.7-2/cdr/cdr_mysql.c 2005-03-19 17:46:30.000000000 +0100
551 + * Asterisk -- A telephony toolkit for Linux.
555 + * James Sharp <jsharp@psychoses.org>
557 + * Modified August 2003
558 + * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
560 + * This program is free software, distributed under the terms of
561 + * the GNU General Public License.
565 +#include <sys/types.h>
566 +#include <asterisk/config.h>
567 +#include <asterisk/options.h>
568 +#include <asterisk/channel.h>
569 +#include <asterisk/cdr.h>
570 +#include <asterisk/module.h>
571 +#include <asterisk/logger.h>
572 +#include <asterisk/cli.h>
573 +#include "../asterisk.h"
582 +#include <mysql/mysql.h>
583 +#include <mysql/errmsg.h>
585 +#define DATE_FORMAT "%Y-%m-%d %T"
587 +static char *desc = "MySQL CDR Backend";
588 +static char *name = "mysql";
589 +static char *config = "cdr_mysql.conf";
590 +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
591 +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
592 +static int dbport = 0;
593 +static int connected = 0;
594 +static time_t connect_time = 0;
595 +static int records = 0;
596 +static int totalrecords = 0;
597 +static int userfield = 0;
599 +AST_MUTEX_DEFINE_STATIC(mysql_lock);
603 +static char cdr_mysql_status_help[] =
604 +"Usage: cdr mysql status\n"
605 +" Shows current connection status for cdr_mysql\n";
607 +static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
610 + char status[256], status2[100] = "";
611 + int ctime = time(NULL) - connect_time;
613 + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
615 + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
617 + snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
619 + if (dbuser && *dbuser)
620 + snprintf(status2, 99, " with username %s", dbuser);
621 + if (dbtable && *dbtable)
622 + snprintf(status2, 99, " using table %s", dbtable);
623 + if (ctime > 31536000) {
624 + 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);
625 + } else if (ctime > 86400) {
626 + 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);
627 + } else if (ctime > 3600) {
628 + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
629 + } else if (ctime > 60) {
630 + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
632 + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
634 + if (records == totalrecords)
635 + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
637 + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
638 + return RESULT_SUCCESS;
640 + ast_cli(fd, "Not currently connected to a MySQL server.\n");
641 + return RESULT_FAILURE;
645 +static struct ast_cli_entry cdr_mysql_status_cli =
646 + { { "cdr", "mysql", "status", NULL },
647 + handle_cdr_mysql_status, "Show connection status of cdr_mysql",
648 + cdr_mysql_status_help, NULL };
650 +static int mysql_log(struct ast_cdr *cdr)
654 + struct localuser *u;
655 + char *userfielddata = NULL;
656 + char sqlcmd[2048], timestr[128];
658 + ast_mutex_lock(&mysql_lock);
660 + memset(sqlcmd,0,2048);
662 + localtime_r(&cdr->start.tv_sec,&tm);
663 + strftime(timestr,128,DATE_FORMAT,&tm);
665 + if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
666 + /* Attempt to connect */
667 + mysql_init(&mysql);
668 + if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
670 + connect_time = time(NULL);
673 + ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s. Call will not be logged\n", hostname);
676 + /* Long connection - ping the server */
678 + if ((error = mysql_ping(&mysql))) {
682 + case CR_SERVER_GONE_ERROR:
683 + ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n");
686 + ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error\n");
692 + char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
693 +#ifdef MYSQL_LOGUNIQUEID
694 + char *uniqueid=NULL;
697 + /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
698 + if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
699 + mysql_real_escape_string(&mysql, clid, cdr->clid, strlen(cdr->clid));
700 + if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
701 + mysql_real_escape_string(&mysql, dcontext, cdr->dcontext, strlen(cdr->dcontext));
702 + if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
703 + mysql_real_escape_string(&mysql, channel, cdr->channel, strlen(cdr->channel));
704 + if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
705 + mysql_real_escape_string(&mysql, dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
706 + if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
707 + mysql_real_escape_string(&mysql, lastapp, cdr->lastapp, strlen(cdr->lastapp));
708 + if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
709 + mysql_real_escape_string(&mysql, lastdata, cdr->lastdata, strlen(cdr->lastdata));
710 +#ifdef MYSQL_LOGUNIQUEID
711 + if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
712 + mysql_real_escape_string(&mysql, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
715 + if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
716 + mysql_real_escape_string(&mysql, userfielddata, cdr->userfield, strlen(cdr->userfield));
718 + /* Check for all alloca failures above at once */
719 +#ifdef MYSQL_LOGUNIQUEID
720 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
722 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
724 + ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
725 + ast_mutex_unlock(&mysql_lock);
729 + ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n");
731 + if (userfield && userfielddata)
733 +#ifdef MYSQL_LOGUNIQUEID
734 + 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);
736 + 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);
741 +#ifdef MYSQL_LOGUNIQUEID
742 + 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);
744 + 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);
748 + ast_log(LOG_DEBUG,"cdr_mysql: SQL command as follows: %s\n",sqlcmd);
750 + if (mysql_real_query(&mysql,sqlcmd,strlen(sqlcmd))) {
751 + ast_log(LOG_ERROR,"Failed to insert into database.");
752 + ast_mutex_unlock(&mysql_lock);
759 + ast_mutex_unlock(&mysql_lock);
763 +char *description(void)
768 +static int my_unload_module(void)
770 + ast_cli_unregister(&cdr_mysql_status_cli);
772 + mysql_close(&mysql);
776 + if (hostname && hostname_alloc) {
779 + hostname_alloc = 0;
781 + if (dbname && dbname_alloc) {
786 + if (dbuser && dbuser_alloc) {
791 + if (dbsock && dbsock_alloc) {
796 + if (dbtable && dbtable_alloc) {
801 + if (password && password_alloc) {
804 + password_alloc = 0;
807 + ast_cdr_unregister(name);
811 +static int my_load_module(void)
814 + struct ast_config *cfg;
815 + struct ast_variable *var;
818 + cfg = ast_config_load(config);
820 + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
824 + var = ast_variable_browse(cfg, "global");
826 + /* nothing configured */
830 + tmp = ast_variable_retrieve(cfg,"global","hostname");
832 + hostname = malloc(strlen(tmp) + 1);
833 + if (hostname != NULL) {
834 + hostname_alloc = 1;
835 + strcpy(hostname,tmp);
837 + ast_log(LOG_ERROR,"Out of memory error.\n");
841 + ast_log(LOG_WARNING,"MySQL server hostname not specified. Assuming localhost\n");
842 + hostname = "localhost";
845 + tmp = ast_variable_retrieve(cfg,"global","dbname");
847 + dbname = malloc(strlen(tmp) + 1);
848 + if (dbname != NULL) {
850 + strcpy(dbname,tmp);
852 + ast_log(LOG_ERROR,"Out of memory error.\n");
856 + ast_log(LOG_WARNING,"MySQL database not specified. Assuming asteriskcdrdb\n");
857 + dbname = "asteriskcdrdb";
860 + tmp = ast_variable_retrieve(cfg,"global","user");
862 + dbuser = malloc(strlen(tmp) + 1);
863 + if (dbuser != NULL) {
865 + strcpy(dbuser,tmp);
867 + ast_log(LOG_ERROR,"Out of memory error.\n");
871 + ast_log(LOG_WARNING,"MySQL database user not specified. Assuming root\n");
875 + tmp = ast_variable_retrieve(cfg,"global","sock");
877 + dbsock = malloc(strlen(tmp) + 1);
878 + if (dbsock != NULL) {
880 + strcpy(dbsock,tmp);
882 + ast_log(LOG_ERROR,"Out of memory error.\n");
886 + ast_log(LOG_WARNING,"MySQL database sock file not specified. Using default\n");
890 + tmp = ast_variable_retrieve(cfg,"global","table");
892 + dbtable = malloc(strlen(tmp) + 1);
893 + if (dbtable != NULL) {
895 + strcpy(dbtable,tmp);
897 + ast_log(LOG_ERROR,"Out of memory error.\n");
901 + ast_log(LOG_NOTICE,"MySQL database table not specified. Assuming \"cdr\"\n");
905 + tmp = ast_variable_retrieve(cfg,"global","password");
907 + password = malloc(strlen(tmp) + 1);
908 + if (password != NULL) {
909 + password_alloc = 1;
910 + strcpy(password,tmp);
912 + ast_log(LOG_ERROR,"Out of memory error.\n");
916 + ast_log(LOG_WARNING,"MySQL database password not specified. Assuming blank\n");
920 + tmp = ast_variable_retrieve(cfg,"global","port");
922 + if (sscanf(tmp,"%d",&dbport) < 1) {
923 + ast_log(LOG_WARNING,"Invalid MySQL port number. Using default\n");
928 + tmp = ast_variable_retrieve(cfg,"global","userfield");
930 + if (sscanf(tmp,"%d",&userfield) < 1) {
931 + ast_log(LOG_WARNING,"Invalid MySQL configurtation file\n");
936 + ast_config_destroy(cfg);
938 + ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
939 + ast_log(LOG_DEBUG,"cdr_mysql: got port of %d\n",dbport);
941 + ast_log(LOG_DEBUG,"cdr_mysql: got sock file of %s\n",dbsock);
942 + ast_log(LOG_DEBUG,"cdr_mysql: got user of %s\n",dbuser);
943 + ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
944 + ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);
946 + mysql_init(&mysql);
948 + if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
949 + ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
953 + ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n");
956 + connect_time = time(NULL);
959 + res = ast_cdr_register(name, desc, mysql_log);
961 + ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
963 + res = ast_cli_register(&cdr_mysql_status_cli);
969 +int load_module(void)
971 + return my_load_module();
974 +int unload_module(void)
976 + return my_unload_module();
981 + my_unload_module();
982 + return my_load_module();
987 + /* Simplistic use count */
988 + if (ast_mutex_trylock(&mysql_lock)) {
991 + ast_mutex_unlock(&mysql_lock);
998 + return ASTERISK_GPL_KEY;
1000 diff -ruN asterisk-1.0.7-orig/configs/cdr_mysql.conf.sample asterisk-1.0.7-2/configs/cdr_mysql.conf.sample
1001 --- asterisk-1.0.7-orig/configs/cdr_mysql.conf.sample 1970-01-01 01:00:00.000000000 +0100
1002 +++ asterisk-1.0.7-2/configs/cdr_mysql.conf.sample 2005-01-21 02:43:19.000000000 +0100
1005 +; Note - if the database server is hosted on the same machine as the
1006 +; asterisk server, you can achieve a local Unix socket connection by
1007 +; setting hostname=localhost
1009 +; port and sock are both optional parameters. If hostname is specified
1010 +; and is not "localhost", then cdr_mysql will attempt to connect to the
1011 +; port specified or use the default port. If hostname is not specified
1012 +; or if hostname is "localhost", then cdr_mysql will attempt to connect
1013 +; to the socket file specified by sock or otherwise use the default socket
1017 +;hostname=database.host.name
1018 +;dbname=asteriskcdrdb
1021 +;user=asteriskcdruser
1023 +;sock=/tmp/mysql.sock