always unlock
[openwrt.git] / openwrt / package / asterisk / patches / mysql+postgres-support.diff
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
4 @@ -79,11 +80,17 @@
5 endif
6 endif
7
8 +app_sql_mysql.o: app_sql_mysql.c
9 + $(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c -o $@ $<
10 +
11 +app_sql_mysql.so: app_sql_mysql.o
12 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lmysqlclient -lz $(MYSQL_LFLAGS)
13 +
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 $@ $<
17
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)
21
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
27 @@ -0,0 +1,443 @@
28 +/*
29 + * Asterisk -- A telephony toolkit for Linux.
30 + *
31 + * Connect to PostgreSQL
32 + *
33 + * Copyright (C) 2004, Constantine Filin and Christos Ricudis
34 + *
35 + * Christos Ricudis <ricudis@itc.auth.gr>
36 + * Constantine Filin <cf@intermedia.net>
37 + *
38 + * This program is free software, distributed under the terms of
39 + * the GNU General Public License
40 + */
41 +
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>
50 +#include <stdlib.h>
51 +#include <unistd.h>
52 +#include <string.h>
53 +#include <stdlib.h>
54 +#include <sys/types.h>
55 +#include <stdio.h>
56 +#include <unistd.h>
57 +
58 +#include <mysql/mysql.h>
59 +
60 +#define EXTRA_LOG 0
61 +
62 +static char *tdesc = "Simple Mysql Interface";
63 +
64 +static char *app = "MYSQL";
65 +
66 +static char *synopsis = "Do several mySQLy things";
67 +
68 +static char *descrip =
69 +"MYSQL(): Do several mySQLy things\n"
70 +"Syntax:\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"
74 +" in ${var}\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" ;
87 +
88 +/*
89 +EXAMPLES OF USE :
90 +
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})
99 +*/
100 +
101 +STANDARD_LOCAL_USER;
102 +LOCAL_USER_DECL;
103 +
104 +AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
105 +
106 +extern void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
107 +
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
112 +
113 +struct ast_MYSQL_id {
114 + int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
115 + int identifier;
116 + void *data;
117 + AST_LIST_ENTRY(ast_MYSQL_id) entries;
118 +} *ast_MYSQL_id;
119 +
120 +AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
121 +
122 +/* helpful procs */
123 +static void *find_identifier(int identifier,int identifier_type) {
124 + struct MYSQLidshead *headp;
125 + struct ast_MYSQL_id *i;
126 + void *res=NULL;
127 + int found=0;
128 +
129 + headp=&_mysql_ids_head;
130 +
131 + if (AST_LIST_LOCK(headp)) {
132 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
133 + } else {
134 + AST_LIST_TRAVERSE(headp,i,entries) {
135 + if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
136 + found=1;
137 + res=i->data;
138 + break;
139 + }
140 + }
141 + if (!found) {
142 + ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
143 + }
144 + AST_LIST_UNLOCK(headp);
145 + }
146 +
147 + return res;
148 +}
149 +
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;
154 +
155 + headp=&_mysql_ids_head;
156 + i=NULL;
157 + j=NULL;
158 +
159 + if (AST_LIST_LOCK(headp)) {
160 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
161 + return(-1);
162 + } else {
163 + i=malloc(sizeof(struct ast_MYSQL_id));
164 + AST_LIST_TRAVERSE(headp,j,entries) {
165 + if (j->identifier>maxidentifier) {
166 + maxidentifier=j->identifier;
167 + }
168 + }
169 + i->identifier=maxidentifier+1;
170 + i->identifier_type=identifier_type;
171 + i->data=data;
172 + AST_LIST_INSERT_HEAD(headp,i,entries);
173 + AST_LIST_UNLOCK(headp);
174 + }
175 + return i->identifier;
176 +}
177 +
178 +static int del_identifier(int identifier,int identifier_type) {
179 + struct ast_MYSQL_id *i;
180 + struct MYSQLidshead *headp;
181 + int found=0;
182 +
183 + headp=&_mysql_ids_head;
184 +
185 + if (AST_LIST_LOCK(headp)) {
186 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
187 + } else {
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);
192 + free(i);
193 + found=1;
194 + break;
195 + }
196 + }
197 + AST_LIST_UNLOCK(headp);
198 + }
199 +
200 + if (found==0) {
201 + ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
202 + return(-1);
203 + } else {
204 + return(0);
205 + }
206 +}
207 +
208 +static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
209 + if( id>=0 ) {
210 + char s[100] = "";
211 + snprintf(s, sizeof(s)-1, "%d", id);
212 +#if EXTRA_LOG
213 + ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
214 +#endif
215 + pbx_builtin_setvar_helper(chan,varname,s);
216 + }
217 + return id;
218 +}
219 +
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));
222 +}
223 +
224 +static int safe_scan_int( char** data, char* delim, int def ) {
225 + char* end;
226 + int res = def;
227 + char* s = strsep(data,delim);
228 + if( s ) {
229 + res = strtol(s,&end,10);
230 + if (*end) res = def; /* not an integer */
231 + }
232 + return res;
233 +}
234 +
235 +/* MYSQL operations */
236 +static int aMYSQL_connect(struct ast_channel *chan, char *data) {
237 +
238 + MYSQL *mysql;
239 +
240 + char *connid_var;
241 + char *dbhost;
242 + char *dbuser;
243 + char *dbpass;
244 + char *dbname;
245 +
246 + strsep(&data," "); // eat the first token, we already know it :P
247 +
248 + connid_var=strsep(&data," ");
249 + dbhost=strsep(&data," ");
250 + dbuser=strsep(&data," ");
251 + dbpass=strsep(&data," ");
252 + dbname=strsep(&data,"\n");
253 +
254 + if( connid_var && dbhost && dbuser && dbpass && dbname ) {
255 + mysql = mysql_init(NULL);
256 + if (mysql) {
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);
259 + return 0;
260 + }
261 + else {
262 + ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
263 + }
264 + }
265 + else {
266 + ast_log(LOG_WARNING,"myslq_init returned NULL\n");
267 + }
268 + }
269 + else {
270 + ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
271 + }
272 +
273 + return -1;
274 +}
275 +
276 +static int aMYSQL_query(struct ast_channel *chan, char *data) {
277 +
278 + MYSQL *mysql;
279 + MYSQL_RES *mysqlres;
280 +
281 + char *resultid_var;
282 + int connid;
283 + char *querystring;
284 +
285 + strsep(&data," "); // eat the first token, we already know it :P
286 +
287 + resultid_var = strsep(&data," ");
288 + connid = safe_scan_int(&data," ",-1);
289 + querystring = strsep(&data,"\n");
290 +
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);
296 + return 0;
297 + }
298 + else if( mysql_field_count(mysql)==0 ) {
299 + return 0; // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
300 + }
301 + else {
302 + ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
303 + }
304 + }
305 + else {
306 + ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
307 + }
308 + }
309 + else {
310 + ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
311 + }
312 +
313 + return -1;
314 +}
315 +
316 +
317 +static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
318 +
319 + MYSQL_RES *mysqlres;
320 + MYSQL_ROW mysqlrow;
321 +
322 + char *fetchid_var,*s5,*s6;
323 + int resultid,numFields,j;
324 +
325 + strsep(&data," "); // eat the first token, we already know it :P
326 +
327 + fetchid_var = strsep(&data," ");
328 + resultid = safe_scan_int(&data," ",-1);
329 +
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," ");
337 + if (s5==NULL) {
338 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
339 + break;
340 + }
341 + s6=mysqlrow[j];
342 + pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
343 + }
344 +#ifdef EXTRA_LOG
345 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
346 +#endif
347 + set_asterisk_int(chan,fetchid_var,1); // try more rows
348 + } else {
349 +#if EXTRA_LOG
350 + ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
351 +#endif
352 + set_asterisk_int(chan,fetchid_var,0); // no more rows
353 + }
354 + return 0;
355 + }
356 + else {
357 + ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
358 + }
359 + }
360 + else {
361 + ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
362 + }
363 +
364 + return -1;
365 +}
366 +
367 +static int aMYSQL_clear(struct ast_channel *chan, char *data) {
368 +
369 + MYSQL_RES *mysqlres;
370 +
371 + int id;
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);
376 + } else {
377 + mysql_free_result(mysqlres);
378 + del_identifier(id,AST_MYSQL_ID_RESID);
379 + }
380 +
381 + return 0;
382 +}
383 +
384 +static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
385 +
386 + MYSQL *mysql;
387 + int id;
388 + strsep(&data," "); // eat the first token, we already know it :P
389 +
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);
393 + } else {
394 + mysql_close(mysql);
395 + del_identifier(id,AST_MYSQL_ID_CONNID);
396 + }
397 +
398 + return 0;
399 +}
400 +
401 +static int MYSQL_exec(struct ast_channel *chan, void *data)
402 +{
403 + struct localuser *u;
404 + int result;
405 +
406 +#if EXTRA_LOG
407 + fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
408 +#endif
409 +
410 + if (!data) {
411 + ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
412 + return -1;
413 + }
414 +
415 + LOCAL_USER_ADD(u);
416 + result=0;
417 +
418 + ast_mutex_lock(&_mysql_mutex);
419 +
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));
430 + } else {
431 + ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
432 + result=-1;
433 + }
434 +
435 + ast_mutex_unlock(&_mysql_mutex);
436 +
437 + LOCAL_USER_REMOVE(u);
438 + return result;
439 +
440 +}
441 +
442 +int unload_module(void)
443 +{
444 + STANDARD_HANGUP_LOCALUSERS;
445 + return ast_unregister_application(app);
446 +}
447 +
448 +int load_module(void)
449 +{
450 + struct MYSQLidshead *headp = &_mysql_ids_head;
451 + AST_LIST_HEAD_INIT(headp);
452 + return ast_register_application(app, MYSQL_exec, synopsis, descrip);
453 +}
454 +
455 +char *description(void)
456 +{
457 + return tdesc;
458 +}
459 +
460 +int usecount(void)
461 +{
462 + int res;
463 + STANDARD_USECOUNT(res);
464 + return res;
465 +}
466 +
467 +char *key()
468 +{
469 + return ASTERISK_GPL_KEY;
470 +}
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
474 @@ -37,36 +37,36 @@
475 #
476 # unixODBC stuff...
477 #
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)
482
483 #
484 # FreeTDS stuff...
485 #
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)
490
491 #
492 # PGSQL stuff... Autoconf anyone??
493 #
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)
507 MLFLAGS=
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)
518
519 #
520 # SQLIte stuff...
521 #
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)
524
525 all: depend $(MODS)
526
527 @@ -89,8 +89,17 @@
528 cdr_tds.so: cdr_tds.o
529 $(CC) $(SOLINK) -o $@ $< -ltds $(MLFLAGS)
530
531 +cdr_mysql.o: cdr_mysql.c
532 + $(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c -o $@ $<
533 +
534 +cdr_mysql.so: cdr_mysql.o
535 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lmysqlclient -lz $(MYSQL_LFLAGS)
536 +
537 +cdr_pgsql.o: cdr_pgsql.c
538 + $(CC) $(CFLAGS) $(PGSQL_CFLAGS) -c -o $@ $<
539 +
540 cdr_pgsql.so: cdr_pgsql.o
541 - $(CC) $(SOLINK) -o $@ $< -lpq -lz $(MLFLAGS)
542 + $(CC) $(SOLINK) -o $@ $< $(LDFLAGS_EXTRA) -lpq -lz $(PGSQL_LFLAGS)
543
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
549 @@ -0,0 +1,450 @@
550 +/*
551 + * Asterisk -- A telephony toolkit for Linux.
552 + *
553 + * MySQL CDR logger
554 + *
555 + * James Sharp <jsharp@psychoses.org>
556 + *
557 + * Modified August 2003
558 + * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
559 + *
560 + * This program is free software, distributed under the terms of
561 + * the GNU General Public License.
562 + *
563 + */
564 +
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"
574 +
575 +#include <stdio.h>
576 +#include <string.h>
577 +
578 +#include <stdlib.h>
579 +#include <unistd.h>
580 +#include <time.h>
581 +
582 +#include <mysql/mysql.h>
583 +#include <mysql/errmsg.h>
584 +
585 +#define DATE_FORMAT "%Y-%m-%d %T"
586 +
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;
598 +
599 +AST_MUTEX_DEFINE_STATIC(mysql_lock);
600 +
601 +static MYSQL mysql;
602 +
603 +static char cdr_mysql_status_help[] =
604 +"Usage: cdr mysql status\n"
605 +" Shows current connection status for cdr_mysql\n";
606 +
607 +static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
608 +{
609 + if (connected) {
610 + char status[256], status2[100] = "";
611 + int ctime = time(NULL) - connect_time;
612 + if (dbport)
613 + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
614 + else if (dbsock)
615 + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
616 + else
617 + snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
618 +
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);
631 + } else {
632 + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
633 + }
634 + if (records == totalrecords)
635 + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
636 + else
637 + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
638 + return RESULT_SUCCESS;
639 + } else {
640 + ast_cli(fd, "Not currently connected to a MySQL server.\n");
641 + return RESULT_FAILURE;
642 + }
643 +}
644 +
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 };
649 +
650 +static int mysql_log(struct ast_cdr *cdr)
651 +{
652 + struct tm tm;
653 + struct timeval tv;
654 + struct localuser *u;
655 + char *userfielddata = NULL;
656 + char sqlcmd[2048], timestr[128];
657 +
658 + ast_mutex_lock(&mysql_lock);
659 +
660 + memset(sqlcmd,0,2048);
661 +
662 + localtime_r(&cdr->start.tv_sec,&tm);
663 + strftime(timestr,128,DATE_FORMAT,&tm);
664 +
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)) {
669 + connected = 1;
670 + connect_time = time(NULL);
671 + records = 0;
672 + } else {
673 + ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s. Call will not be logged\n", hostname);
674 + }
675 + } else {
676 + /* Long connection - ping the server */
677 + int error;
678 + if ((error = mysql_ping(&mysql))) {
679 + connected = 0;
680 + records = 0;
681 + switch (error) {
682 + case CR_SERVER_GONE_ERROR:
683 + ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n");
684 + break;
685 + default:
686 + ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error\n");
687 + }
688 + }
689 + }
690 +
691 + if (connected) {
692 + char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
693 +#ifdef MYSQL_LOGUNIQUEID
694 + char *uniqueid=NULL;
695 +#endif
696 +
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));
713 +#endif
714 +
715 + if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
716 + mysql_real_escape_string(&mysql, userfielddata, cdr->userfield, strlen(cdr->userfield));
717 +
718 + /* Check for all alloca failures above at once */
719 +#ifdef MYSQL_LOGUNIQUEID
720 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
721 +#else
722 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
723 +#endif
724 + ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
725 + ast_mutex_unlock(&mysql_lock);
726 + return -1;
727 + }
728 +
729 + ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n");
730 +
731 + if (userfield && userfielddata)
732 + {
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);
735 +#else
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);
737 +#endif
738 + }
739 + else
740 + {
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);
743 +#else
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);
745 +#endif
746 + }
747 +
748 + ast_log(LOG_DEBUG,"cdr_mysql: SQL command as follows: %s\n",sqlcmd);
749 +
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);
753 + return -1;
754 + } else {
755 + records++;
756 + totalrecords++;
757 + }
758 + }
759 + ast_mutex_unlock(&mysql_lock);
760 + return 0;
761 +}
762 +
763 +char *description(void)
764 +{
765 + return desc;
766 +}
767 +
768 +static int my_unload_module(void)
769 +{
770 + ast_cli_unregister(&cdr_mysql_status_cli);
771 + if (connected) {
772 + mysql_close(&mysql);
773 + connected = 0;
774 + records = 0;
775 + }
776 + if (hostname && hostname_alloc) {
777 + free(hostname);
778 + hostname = NULL;
779 + hostname_alloc = 0;
780 + }
781 + if (dbname && dbname_alloc) {
782 + free(dbname);
783 + dbname = NULL;
784 + dbname_alloc = 0;
785 + }
786 + if (dbuser && dbuser_alloc) {
787 + free(dbuser);
788 + dbuser = NULL;
789 + dbuser_alloc = 0;
790 + }
791 + if (dbsock && dbsock_alloc) {
792 + free(dbsock);
793 + dbsock = NULL;
794 + dbsock_alloc = 0;
795 + }
796 + if (dbtable && dbtable_alloc) {
797 + free(dbtable);
798 + dbtable = NULL;
799 + dbtable_alloc = 0;
800 + }
801 + if (password && password_alloc) {
802 + free(password);
803 + password = NULL;
804 + password_alloc = 0;
805 + }
806 + dbport = 0;
807 + ast_cdr_unregister(name);
808 + return 0;
809 +}
810 +
811 +static int my_load_module(void)
812 +{
813 + int res;
814 + struct ast_config *cfg;
815 + struct ast_variable *var;
816 + char *tmp;
817 +
818 + cfg = ast_config_load(config);
819 + if (!cfg) {
820 + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
821 + return 0;
822 + }
823 +
824 + var = ast_variable_browse(cfg, "global");
825 + if (!var) {
826 + /* nothing configured */
827 + return 0;
828 + }
829 +
830 + tmp = ast_variable_retrieve(cfg,"global","hostname");
831 + if (tmp) {
832 + hostname = malloc(strlen(tmp) + 1);
833 + if (hostname != NULL) {
834 + hostname_alloc = 1;
835 + strcpy(hostname,tmp);
836 + } else {
837 + ast_log(LOG_ERROR,"Out of memory error.\n");
838 + return -1;
839 + }
840 + } else {
841 + ast_log(LOG_WARNING,"MySQL server hostname not specified. Assuming localhost\n");
842 + hostname = "localhost";
843 + }
844 +
845 + tmp = ast_variable_retrieve(cfg,"global","dbname");
846 + if (tmp) {
847 + dbname = malloc(strlen(tmp) + 1);
848 + if (dbname != NULL) {
849 + dbname_alloc = 1;
850 + strcpy(dbname,tmp);
851 + } else {
852 + ast_log(LOG_ERROR,"Out of memory error.\n");
853 + return -1;
854 + }
855 + } else {
856 + ast_log(LOG_WARNING,"MySQL database not specified. Assuming asteriskcdrdb\n");
857 + dbname = "asteriskcdrdb";
858 + }
859 +
860 + tmp = ast_variable_retrieve(cfg,"global","user");
861 + if (tmp) {
862 + dbuser = malloc(strlen(tmp) + 1);
863 + if (dbuser != NULL) {
864 + dbuser_alloc = 1;
865 + strcpy(dbuser,tmp);
866 + } else {
867 + ast_log(LOG_ERROR,"Out of memory error.\n");
868 + return -1;
869 + }
870 + } else {
871 + ast_log(LOG_WARNING,"MySQL database user not specified. Assuming root\n");
872 + dbuser = "root";
873 + }
874 +
875 + tmp = ast_variable_retrieve(cfg,"global","sock");
876 + if (tmp) {
877 + dbsock = malloc(strlen(tmp) + 1);
878 + if (dbsock != NULL) {
879 + dbsock_alloc = 1;
880 + strcpy(dbsock,tmp);
881 + } else {
882 + ast_log(LOG_ERROR,"Out of memory error.\n");
883 + return -1;
884 + }
885 + } else {
886 + ast_log(LOG_WARNING,"MySQL database sock file not specified. Using default\n");
887 + dbsock = NULL;
888 + }
889 +
890 + tmp = ast_variable_retrieve(cfg,"global","table");
891 + if (tmp) {
892 + dbtable = malloc(strlen(tmp) + 1);
893 + if (dbtable != NULL) {
894 + dbtable_alloc = 1;
895 + strcpy(dbtable,tmp);
896 + } else {
897 + ast_log(LOG_ERROR,"Out of memory error.\n");
898 + return -1;
899 + }
900 + } else {
901 + ast_log(LOG_NOTICE,"MySQL database table not specified. Assuming \"cdr\"\n");
902 + dbtable = "cdr";
903 + }
904 +
905 + tmp = ast_variable_retrieve(cfg,"global","password");
906 + if (tmp) {
907 + password = malloc(strlen(tmp) + 1);
908 + if (password != NULL) {
909 + password_alloc = 1;
910 + strcpy(password,tmp);
911 + } else {
912 + ast_log(LOG_ERROR,"Out of memory error.\n");
913 + return -1;
914 + }
915 + } else {
916 + ast_log(LOG_WARNING,"MySQL database password not specified. Assuming blank\n");
917 + password = "";
918 + }
919 +
920 + tmp = ast_variable_retrieve(cfg,"global","port");
921 + if (tmp) {
922 + if (sscanf(tmp,"%d",&dbport) < 1) {
923 + ast_log(LOG_WARNING,"Invalid MySQL port number. Using default\n");
924 + dbport = 0;
925 + }
926 + }
927 +
928 + tmp = ast_variable_retrieve(cfg,"global","userfield");
929 + if (tmp) {
930 + if (sscanf(tmp,"%d",&userfield) < 1) {
931 + ast_log(LOG_WARNING,"Invalid MySQL configurtation file\n");
932 + userfield = 0;
933 + }
934 + }
935 +
936 + ast_config_destroy(cfg);
937 +
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);
940 + if (dbsock)
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);
945 +
946 + mysql_init(&mysql);
947 +
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);
950 + connected = 0;
951 + records = 0;
952 + } else {
953 + ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n");
954 + connected = 1;
955 + records = 0;
956 + connect_time = time(NULL);
957 + }
958 +
959 + res = ast_cdr_register(name, desc, mysql_log);
960 + if (res) {
961 + ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
962 + } else {
963 + res = ast_cli_register(&cdr_mysql_status_cli);
964 + }
965 +
966 + return res;
967 +}
968 +
969 +int load_module(void)
970 +{
971 + return my_load_module();
972 +}
973 +
974 +int unload_module(void)
975 +{
976 + return my_unload_module();
977 +}
978 +
979 +int reload(void)
980 +{
981 + my_unload_module();
982 + return my_load_module();
983 +}
984 +
985 +int usecount(void)
986 +{
987 + /* Simplistic use count */
988 + if (ast_mutex_trylock(&mysql_lock)) {
989 + return 1;
990 + } else {
991 + ast_mutex_unlock(&mysql_lock);
992 + return 0;
993 + }
994 +}
995 +
996 +char *key()
997 +{
998 + return ASTERISK_GPL_KEY;
999 +}
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
1003 @@ -0,0 +1,21 @@
1004 +;
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
1008 +;
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
1014 +; file.
1015 +;
1016 +;[global]
1017 +;hostname=database.host.name
1018 +;dbname=asteriskcdrdb
1019 +;table=cdr
1020 +;password=password
1021 +;user=asteriskcdruser
1022 +;port=3306
1023 +;sock=/tmp/mysql.sock
1024 +;userfield=1
This page took 0.093414 seconds and 5 git commands to generate.