00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <asterisk.h>
00019
00020 #include <stdlib.h>
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <sys/types.h>
00025 #include <stdio.h>
00026 #include <unistd.h>
00027
00028 #include <mysql/mysql.h>
00029
00030 #include <asterisk/file.h>
00031 #include <asterisk/logger.h>
00032 #include <asterisk/channel.h>
00033 #include <asterisk/pbx.h>
00034 #include <asterisk/module.h>
00035 #include <asterisk/linkedlists.h>
00036 #include <asterisk/chanvars.h>
00037 #include <asterisk/lock.h>
00038
00039 #define AST_MODULE "app_addon_sql_mysql"
00040
00041 #define EXTRA_LOG 0
00042
00043 static char *app = "MYSQL";
00044
00045 static char *synopsis = "Do several mySQLy things";
00046
00047 static char *descrip =
00048 "MYSQL(): Do several mySQLy things\n"
00049 "Syntax:\n"
00050 " MYSQL(Connect connid dhhost dbuser dbpass dbname)\n"
00051 " Connects to a database. Arguments contain standard MySQL parameters\n"
00052 " passed to function mysql_real_connect. Connection identifer returned\n"
00053 " in ${var}\n"
00054 " MYSQL(Query resultid ${connid} query-string)\n"
00055 " Executes standard MySQL query contained in query-string using established\n"
00056 " connection identified by ${connection_identifier}. Result of query is\n"
00057 " is stored in ${var}.\n"
00058 " MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
00059 " Fetches a single row from a result set contained in ${result_identifier}.\n"
00060 " Assigns returned fields to ${var1} ... ${varn}. ${fetchid} is set TRUE\n"
00061 " if additional rows exist in result set.\n"
00062 " MYSQL(Clear ${resultid})\n"
00063 " Frees memory and datastructures associated with result set.\n"
00064 " MYSQL(Disconnect ${connid})\n"
00065 " Disconnects from named connection to MySQL.\n"
00066 " On exit, always returns 0. Sets MYSQL_STATUS to 0 on success and -1 on error.\n";
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
00082
00083 #define AST_MYSQL_ID_DUMMY 0
00084 #define AST_MYSQL_ID_CONNID 1
00085 #define AST_MYSQL_ID_RESID 2
00086 #define AST_MYSQL_ID_FETCHID 3
00087
00088 struct ast_MYSQL_id {
00089 int identifier_type;
00090 int identifier;
00091 void *data;
00092 AST_LIST_ENTRY(ast_MYSQL_id) entries;
00093 } *ast_MYSQL_id;
00094
00095 AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
00096
00097
00098 static void *find_identifier(int identifier,int identifier_type) {
00099 struct MYSQLidshead *headp;
00100 struct ast_MYSQL_id *i;
00101 void *res=NULL;
00102 int found=0;
00103
00104 headp=&_mysql_ids_head;
00105
00106 if (AST_LIST_LOCK(headp)) {
00107 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00108 } else {
00109 AST_LIST_TRAVERSE(headp,i,entries) {
00110 if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
00111 found=1;
00112 res=i->data;
00113 break;
00114 }
00115 }
00116 if (!found) {
00117 ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
00118 }
00119 AST_LIST_UNLOCK(headp);
00120 }
00121
00122 return res;
00123 }
00124
00125 static int add_identifier(int identifier_type,void *data) {
00126 struct ast_MYSQL_id *i,*j;
00127 struct MYSQLidshead *headp;
00128 int maxidentifier=0;
00129
00130 headp=&_mysql_ids_head;
00131 i=NULL;
00132 j=NULL;
00133
00134 if (AST_LIST_LOCK(headp)) {
00135 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00136 return(-1);
00137 } else {
00138 i=malloc(sizeof(struct ast_MYSQL_id));
00139 AST_LIST_TRAVERSE(headp,j,entries) {
00140 if (j->identifier>maxidentifier) {
00141 maxidentifier=j->identifier;
00142 }
00143 }
00144 i->identifier=maxidentifier+1;
00145 i->identifier_type=identifier_type;
00146 i->data=data;
00147 AST_LIST_INSERT_HEAD(headp,i,entries);
00148 AST_LIST_UNLOCK(headp);
00149 }
00150 return i->identifier;
00151 }
00152
00153 static int del_identifier(int identifier,int identifier_type) {
00154 struct ast_MYSQL_id *i;
00155 struct MYSQLidshead *headp;
00156 int found=0;
00157
00158 headp=&_mysql_ids_head;
00159
00160 if (AST_LIST_LOCK(headp)) {
00161 ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00162 } else {
00163 AST_LIST_TRAVERSE(headp,i,entries) {
00164 if ((i->identifier==identifier) &&
00165 (i->identifier_type==identifier_type)) {
00166 AST_LIST_REMOVE(headp,i,entries);
00167 free(i);
00168 found=1;
00169 break;
00170 }
00171 }
00172 AST_LIST_UNLOCK(headp);
00173 }
00174
00175 if (found==0) {
00176 ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
00177 return(-1);
00178 } else {
00179 return(0);
00180 }
00181 }
00182
00183 static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
00184 if( id>=0 ) {
00185 char s[100] = "";
00186 snprintf(s, sizeof(s)-1, "%d", id);
00187 #if EXTRA_LOG
00188 ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
00189 #endif
00190 pbx_builtin_setvar_helper(chan,varname,s);
00191 }
00192 return id;
00193 }
00194
00195 static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data) {
00196 return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
00197 }
00198
00199 static int safe_scan_int( char** data, char* delim, int def ) {
00200 char* end;
00201 int res = def;
00202 char* s = strsep(data,delim);
00203 if( s ) {
00204 res = strtol(s,&end,10);
00205 if (*end) res = def;
00206 }
00207 return res;
00208 }
00209
00210
00211 static int aMYSQL_connect(struct ast_channel *chan, char *data) {
00212
00213 MYSQL *mysql;
00214
00215 char *connid_var;
00216 char *dbhost;
00217 char *dbuser;
00218 char *dbpass;
00219 char *dbname;
00220
00221 strsep(&data," ");
00222
00223 connid_var=strsep(&data," ");
00224 dbhost=strsep(&data," ");
00225 dbuser=strsep(&data," ");
00226 dbpass=strsep(&data," ");
00227 dbname=strsep(&data,"\n");
00228
00229 if( connid_var && dbhost && dbuser && dbpass && dbname ) {
00230 mysql = mysql_init(NULL);
00231 if (mysql) {
00232 if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
00233 add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
00234 return 0;
00235 }
00236 else {
00237 ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
00238 }
00239 }
00240 else {
00241 ast_log(LOG_WARNING,"myslq_init returned NULL\n");
00242 }
00243 }
00244 else {
00245 ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
00246 }
00247
00248 return -1;
00249 }
00250
00251 static int aMYSQL_query(struct ast_channel *chan, char *data) {
00252
00253 MYSQL *mysql;
00254 MYSQL_RES *mysqlres;
00255
00256 char *resultid_var;
00257 int connid;
00258 char *querystring;
00259
00260 strsep(&data," ");
00261
00262 resultid_var = strsep(&data," ");
00263 connid = safe_scan_int(&data," ",-1);
00264 querystring = strsep(&data,"\n");
00265
00266 if (resultid_var && (connid>=0) && querystring) {
00267 if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
00268 mysql_query(mysql,querystring);
00269 if ((mysqlres=mysql_use_result(mysql))!=NULL) {
00270 add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
00271 return 0;
00272 }
00273 else if( mysql_field_count(mysql)==0 ) {
00274 return 0;
00275 }
00276 else {
00277 ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
00278 }
00279 }
00280 else {
00281 ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
00282 }
00283 }
00284 else {
00285 ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
00286 }
00287
00288 return -1;
00289 }
00290
00291
00292 static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
00293
00294 MYSQL_RES *mysqlres;
00295 MYSQL_ROW mysqlrow;
00296
00297 char *fetchid_var,*s5,*s6;
00298 int resultid,numFields,j;
00299
00300 strsep(&data," ");
00301
00302 fetchid_var = strsep(&data," ");
00303 resultid = safe_scan_int(&data," ",-1);
00304
00305 if (fetchid_var && (resultid>=0) ) {
00306 if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
00307
00308 if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
00309 numFields=mysql_num_fields(mysqlres);
00310 for (j=0;j<numFields;j++) {
00311 s5=strsep(&data," ");
00312 if (s5==NULL) {
00313 ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
00314 break;
00315 }
00316 s6=mysqlrow[j];
00317 pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
00318 }
00319 #if EXTRA_LOG
00320 ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
00321 #endif
00322 set_asterisk_int(chan,fetchid_var,1);
00323 } else {
00324 #if EXTRA_LOG
00325 ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
00326 #endif
00327 set_asterisk_int(chan,fetchid_var,0);
00328 }
00329 return 0;
00330 }
00331 else {
00332 ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
00333 }
00334 }
00335 else {
00336 ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
00337 }
00338
00339 return -1;
00340 }
00341
00342 static int aMYSQL_clear(struct ast_channel *chan, char *data) {
00343
00344 MYSQL_RES *mysqlres;
00345
00346 int id;
00347 strsep(&data," ");
00348 id = safe_scan_int(&data," \n",-1);
00349 if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
00350 ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
00351 } else {
00352 mysql_free_result(mysqlres);
00353 del_identifier(id,AST_MYSQL_ID_RESID);
00354 }
00355
00356 return 0;
00357 }
00358
00359 static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
00360
00361 MYSQL *mysql;
00362 int id;
00363 strsep(&data," ");
00364
00365 id = safe_scan_int(&data," \n",-1);
00366 if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
00367 ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
00368 } else {
00369 mysql_close(mysql);
00370 del_identifier(id,AST_MYSQL_ID_CONNID);
00371 }
00372
00373 return 0;
00374 }
00375
00376 static int MYSQL_exec(struct ast_channel *chan, void *data)
00377 {
00378 struct ast_module_user *u;
00379 int result;
00380 char sresult[10];
00381
00382 #if EXTRA_LOG
00383 fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
00384 #endif
00385
00386 if (!data) {
00387 ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
00388 return -1;
00389 }
00390
00391 u = ast_module_user_add(chan);
00392 result=0;
00393
00394 ast_mutex_lock(&_mysql_mutex);
00395
00396 if (strncasecmp("connect",data,strlen("connect"))==0) {
00397 result=aMYSQL_connect(chan,ast_strdupa(data));
00398 } else if (strncasecmp("query",data,strlen("query"))==0) {
00399 result=aMYSQL_query(chan,ast_strdupa(data));
00400 } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
00401 result=aMYSQL_fetch(chan,ast_strdupa(data));
00402 } else if (strncasecmp("clear",data,strlen("clear"))==0) {
00403 result=aMYSQL_clear(chan,ast_strdupa(data));
00404 } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
00405 result=aMYSQL_disconnect(chan,ast_strdupa(data));
00406 } else {
00407 ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
00408 result=-1;
00409 }
00410
00411 ast_mutex_unlock(&_mysql_mutex);
00412
00413 ast_module_user_remove(u);
00414 snprintf(sresult, sizeof(sresult), "%d", result);
00415 pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
00416 return 0;
00417 }
00418
00419 static int unload_module(void)
00420 {
00421 ast_module_user_hangup_all();
00422 return ast_unregister_application(app);
00423 }
00424
00425 static int load_module(void)
00426 {
00427 struct MYSQLidshead *headp = &_mysql_ids_head;
00428 AST_LIST_HEAD_INIT(headp);
00429 return ast_register_application(app, MYSQL_exec, synopsis, descrip);
00430 }
00431
00432 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Mysql Interface");