Mon May 14 04:42:50 2007

Asterisk developer's documentation


app_addon_sql_mysql.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * Connect to MySQL
00005  * 
00006  * Copyright (C) 2004, Constantine Filin and Christos Ricudis
00007  *
00008  * Christos Ricudis <ricudis@itc.auth.gr>
00009  * Constantine Filin <cf@intermedia.net>
00010  *
00011  * This program is free software, distributed under the terms of
00012  * the GNU General Public License
00013  */
00014 /*** MODULEINFO
00015    <depend>mysqlclient</depend>
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 EXAMPLES OF USE : 
00070 
00071 exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit)
00072 exten => s,3,MYSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
00073 exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
00074 exten => s,5,GotoIf(${fetchid}?6:8)
00075 exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")  
00076 exten => s,7,Goto(s,4)
00077 exten => s,8,MYSQL(Clear ${resultid})
00078 exten => s,9,MYSQL(Disconnect ${connid})
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; /* 0=dummy, 1=connid, 2=resultid */
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 /* helpful procs */
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;  /* not an integer */
00206    }
00207    return res;
00208 }
00209 
00210 /* MYSQL operations */
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," "); // eat the first token, we already know it :P 
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    int mysql_query_res;
00260 
00261    strsep(&data," "); // eat the first token, we already know it :P 
00262 
00263    resultid_var = strsep(&data," ");
00264    connid       = safe_scan_int(&data," ",-1);
00265    querystring  = strsep(&data,"\n");
00266 
00267    if (resultid_var && (connid>=0) && querystring) {
00268       if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
00269          mysql_query_res = mysql_query(mysql,querystring);
00270          if (mysql_query_res != 0) {
00271             ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
00272          }
00273          else {
00274             if ((mysqlres=mysql_use_result(mysql))!=NULL) {
00275                add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
00276                return 0;
00277             }
00278             else if (mysql_field_count(mysql)==0) {
00279                return 0;  // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
00280             }
00281             else {
00282                ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
00283             }
00284          }
00285       }
00286       else {
00287          ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
00288       }
00289    }
00290    else {
00291       ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
00292    }
00293    
00294    return -1;
00295 }
00296 
00297 
00298 static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
00299    
00300    MYSQL_RES *mysqlres;
00301    MYSQL_ROW mysqlrow;
00302 
00303    char *fetchid_var,*s5,*s6;
00304    int resultid,numFields,j;
00305    
00306    strsep(&data," "); // eat the first token, we already know it :P 
00307 
00308    fetchid_var = strsep(&data," ");
00309    resultid    = safe_scan_int(&data," ",-1);
00310 
00311    if (fetchid_var && (resultid>=0) ) {
00312       if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
00313          /* Grab the next row */
00314          if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
00315             numFields=mysql_num_fields(mysqlres);
00316             for (j=0;j<numFields;j++) {
00317                s5=strsep(&data," ");
00318                if (s5==NULL) {
00319                   ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
00320                   break;
00321                }
00322                s6=mysqlrow[j];
00323                pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
00324             }
00325 #if EXTRA_LOG
00326             ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
00327 #endif
00328             set_asterisk_int(chan,fetchid_var,1); // try more rows
00329          } else {
00330 #if EXTRA_LOG
00331             ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
00332 #endif
00333             set_asterisk_int(chan,fetchid_var,0); // no more rows
00334          }
00335          return 0;
00336       }
00337       else {
00338          ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
00339       }
00340    }
00341    else {
00342       ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
00343    }
00344 
00345    return -1;
00346 }
00347 
00348 static int aMYSQL_clear(struct ast_channel *chan, char *data) {
00349 
00350    MYSQL_RES *mysqlres;
00351 
00352    int id;
00353    strsep(&data," "); // eat the first token, we already know it :P 
00354    id = safe_scan_int(&data," \n",-1);
00355    if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
00356       ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
00357    } else {
00358       mysql_free_result(mysqlres);
00359       del_identifier(id,AST_MYSQL_ID_RESID);
00360    }
00361 
00362    return 0;
00363 }
00364 
00365 static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
00366    
00367    MYSQL *mysql;
00368    int id;
00369    strsep(&data," "); // eat the first token, we already know it :P 
00370 
00371    id = safe_scan_int(&data," \n",-1);
00372    if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
00373       ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
00374    } else {
00375       mysql_close(mysql);
00376       del_identifier(id,AST_MYSQL_ID_CONNID);
00377    } 
00378 
00379    return 0;
00380 }
00381 
00382 static int MYSQL_exec(struct ast_channel *chan, void *data)
00383 {
00384    struct ast_module_user *u;
00385    int result;
00386    char sresult[10];
00387 
00388 #if EXTRA_LOG
00389    fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
00390 #endif
00391 
00392    if (!data) {
00393       ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
00394       return -1;
00395    }
00396 
00397    u = ast_module_user_add(chan);
00398    result=0;
00399 
00400    ast_mutex_lock(&_mysql_mutex);
00401 
00402    if (strncasecmp("connect",data,strlen("connect"))==0) {
00403       result=aMYSQL_connect(chan,ast_strdupa(data));
00404    } else   if (strncasecmp("query",data,strlen("query"))==0) {
00405       result=aMYSQL_query(chan,ast_strdupa(data));
00406    } else   if (strncasecmp("fetch",data,strlen("fetch"))==0) {
00407       result=aMYSQL_fetch(chan,ast_strdupa(data));
00408    } else   if (strncasecmp("clear",data,strlen("clear"))==0) {
00409       result=aMYSQL_clear(chan,ast_strdupa(data));
00410    } else   if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
00411       result=aMYSQL_disconnect(chan,ast_strdupa(data));
00412    } else {
00413       ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
00414       result=-1;  
00415    }
00416       
00417    ast_mutex_unlock(&_mysql_mutex);
00418 
00419    ast_module_user_remove(u);
00420    snprintf(sresult, sizeof(sresult), "%d", result);
00421    pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
00422    return 0;
00423 }
00424 
00425 static int unload_module(void)
00426 {
00427    ast_module_user_hangup_all();
00428    return ast_unregister_application(app);
00429 }
00430 
00431 static int load_module(void)
00432 {
00433    struct MYSQLidshead *headp = &_mysql_ids_head;
00434    AST_LIST_HEAD_INIT(headp);
00435    return ast_register_application(app, MYSQL_exec, synopsis, descrip);
00436 }
00437 
00438 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Mysql Interface");

Generated on Mon May 14 04:42:50 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1