Mon Apr 30 07:36:54 2007

Asterisk developer's documentation


app_addon_sql_mysql.c File Reference

#include <asterisk.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <mysql/mysql.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/linkedlists.h>
#include <asterisk/chanvars.h>
#include <asterisk/lock.h>

Include dependency graph for app_addon_sql_mysql.c:

Go to the source code of this file.

Data Structures

struct  ast_MYSQL_id

Defines

#define AST_MODULE   "app_addon_sql_mysql"
#define AST_MYSQL_ID_CONNID   1
#define AST_MYSQL_ID_DUMMY   0
#define AST_MYSQL_ID_FETCHID   3
#define AST_MYSQL_ID_RESID   2
#define EXTRA_LOG   0

Functions

static int add_identifier (int identifier_type, void *data)
static int add_identifier_and_set_asterisk_int (struct ast_channel *chan, char *varname, int identifier_type, void *data)
static int aMYSQL_clear (struct ast_channel *chan, char *data)
static int aMYSQL_connect (struct ast_channel *chan, char *data)
static int aMYSQL_disconnect (struct ast_channel *chan, char *data)
static int aMYSQL_fetch (struct ast_channel *chan, char *data)
static int aMYSQL_query (struct ast_channel *chan, char *data)
 AST_LIST_HEAD (MYSQLidshead, ast_MYSQL_id)
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Simple Mysql Interface")
 AST_MUTEX_DEFINE_STATIC (_mysql_mutex)
static int del_identifier (int identifier, int identifier_type)
static int load_module (void)
static int MYSQL_exec (struct ast_channel *chan, void *data)
static int safe_scan_int (char **data, char *delim, int def)
static int set_asterisk_int (struct ast_channel *chan, char *varname, int id)
static int unload_module (void)

Variables

static char * app = "MYSQL"
static char * descrip
static char * synopsis = "Do several mySQLy things"


Define Documentation

#define AST_MODULE   "app_addon_sql_mysql"

Definition at line 39 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_CONNID   1

Definition at line 84 of file app_addon_sql_mysql.c.

Referenced by aMYSQL_connect(), aMYSQL_disconnect(), and aMYSQL_query().

#define AST_MYSQL_ID_DUMMY   0

Definition at line 83 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_FETCHID   3

Definition at line 86 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_RESID   2

Definition at line 85 of file app_addon_sql_mysql.c.

Referenced by aMYSQL_clear(), aMYSQL_fetch(), and aMYSQL_query().

#define EXTRA_LOG   0

Definition at line 41 of file app_addon_sql_mysql.c.


Function Documentation

static int add_identifier ( int  identifier_type,
void *  data 
) [static]

Definition at line 125 of file app_addon_sql_mysql.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_MYSQL_id::data, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, LOG_WARNING, and malloc.

Referenced by add_identifier_and_set_asterisk_int().

00125                                                           {
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 }

static int add_identifier_and_set_asterisk_int ( struct ast_channel chan,
char *  varname,
int  identifier_type,
void *  data 
) [static]

Definition at line 195 of file app_addon_sql_mysql.c.

References add_identifier(), and set_asterisk_int().

Referenced by aMYSQL_connect(), and aMYSQL_query().

00195                                                                                                                          {
00196    return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
00197 }

static int aMYSQL_clear ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 342 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_RESID, del_identifier(), id, LOG_WARNING, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00342                                                               {
00343 
00344    MYSQL_RES *mysqlres;
00345 
00346    int id;
00347    strsep(&data," "); // eat the first token, we already know it :P 
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 }

static int aMYSQL_connect ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 211 of file app_addon_sql_mysql.c.

References add_identifier_and_set_asterisk_int(), ast_log(), AST_MYSQL_ID_CONNID, dbhost, dbname, dbpass, dbuser, LOG_WARNING, mysql, and strsep().

Referenced by MYSQL_exec().

00211                                                                 {
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 }

static int aMYSQL_disconnect ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 359 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_CONNID, del_identifier(), id, LOG_WARNING, mysql, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00359                                                                    {
00360    
00361    MYSQL *mysql;
00362    int id;
00363    strsep(&data," "); // eat the first token, we already know it :P 
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 }

static int aMYSQL_fetch ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 292 of file app_addon_sql_mysql.c.

References ast_log(), AST_MYSQL_ID_RESID, LOG_WARNING, pbx_builtin_setvar_helper(), safe_scan_int(), set_asterisk_int(), and strsep().

Referenced by MYSQL_exec().

00292                                                               {
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," "); // eat the first token, we already know it :P 
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          /* Grab the next row */
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); // try more rows
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); // no more rows
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 }

static int aMYSQL_query ( struct ast_channel chan,
char *  data 
) [static]

Definition at line 251 of file app_addon_sql_mysql.c.

References add_identifier_and_set_asterisk_int(), ast_log(), AST_MYSQL_ID_CONNID, AST_MYSQL_ID_RESID, LOG_WARNING, mysql, safe_scan_int(), and strsep().

Referenced by MYSQL_exec().

00251                                                               {
00252    
00253    MYSQL       *mysql;
00254    MYSQL_RES   *mysqlres;
00255 
00256    char *resultid_var;
00257    int connid;
00258    char *querystring;
00259 
00260    strsep(&data," "); // eat the first token, we already know it :P 
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;  // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
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 }

AST_LIST_HEAD ( MYSQLidshead  ,
ast_MYSQL_id   
)

Definition at line 95 of file app_addon_sql_mysql.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_MYSQL_id::data, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, and LOG_WARNING.

00098                                                                  {
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 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Simple Mysql Interface"   
)

AST_MUTEX_DEFINE_STATIC ( _mysql_mutex   ) 

static int del_identifier ( int  identifier,
int  identifier_type 
) [static]

Definition at line 153 of file app_addon_sql_mysql.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), free, ast_MYSQL_id::identifier, ast_MYSQL_id::identifier_type, and LOG_WARNING.

Referenced by aMYSQL_clear(), and aMYSQL_disconnect().

00153                                                               {
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 }

static int load_module ( void   )  [static]

Definition at line 425 of file app_addon_sql_mysql.c.

References app, AST_LIST_HEAD_INIT, ast_register_application(), descrip, MYSQL_exec(), and synopsis.

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 }

static int MYSQL_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 376 of file app_addon_sql_mysql.c.

References aMYSQL_clear(), aMYSQL_connect(), aMYSQL_disconnect(), aMYSQL_fetch(), aMYSQL_query(), ast_log(), ast_module_user_add, ast_module_user_remove, ast_mutex_lock(), ast_mutex_unlock(), ast_strdupa, ast_module_user::chan, LOG_WARNING, pbx_builtin_setvar_helper(), and result.

Referenced by load_module().

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 }

static int safe_scan_int ( char **  data,
char *  delim,
int  def 
) [static]

Definition at line 199 of file app_addon_sql_mysql.c.

References s, and strsep().

Referenced by aMYSQL_clear(), aMYSQL_disconnect(), aMYSQL_fetch(), and aMYSQL_query().

00199                                                               {
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 }

static int set_asterisk_int ( struct ast_channel chan,
char *  varname,
int  id 
) [static]

Definition at line 183 of file app_addon_sql_mysql.c.

References ast_log(), LOG_WARNING, pbx_builtin_setvar_helper(), and s.

Referenced by add_identifier_and_set_asterisk_int(), and aMYSQL_fetch().

00183                                                                              {
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 }

static int unload_module ( void   )  [static]

Definition at line 419 of file app_addon_sql_mysql.c.

References app, ast_module_user_hangup_all, and ast_unregister_application().

00420 {
00421    ast_module_user_hangup_all();
00422    return ast_unregister_application(app);
00423 }


Variable Documentation

char* app = "MYSQL" [static]

Definition at line 43 of file app_addon_sql_mysql.c.

char* descrip [static]

Definition at line 47 of file app_addon_sql_mysql.c.

char* synopsis = "Do several mySQLy things" [static]

Definition at line 45 of file app_addon_sql_mysql.c.


Generated on Mon Apr 30 07:36:55 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1