Fri Jul 3 20:35:45 2009

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
struct  MYSQLidshead

Defines

#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 void __reg_module (void)
static void __unreg_module (void)
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)
static int del_identifier (int identifier, int identifier_type)
static void * find_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 struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Simple Mysql Interface" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
struct MYSQLidshead _mysql_ids_head
static ast_mutex_t _mysql_mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static char * app = "MYSQL"
static const struct
ast_module_info * 
ast_module_info = &__mod_info
static char * descrip
static char * synopsis = "Do several mySQLy things"


Define Documentation

#define AST_MYSQL_ID_CONNID   1

Definition at line 86 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 85 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_FETCHID   3

Definition at line 88 of file app_addon_sql_mysql.c.

#define AST_MYSQL_ID_RESID   2

Definition at line 87 of file app_addon_sql_mysql.c.

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

#define EXTRA_LOG   0

Definition at line 43 of file app_addon_sql_mysql.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 440 of file app_addon_sql_mysql.c.

static void __unreg_module ( void   )  [static]

Definition at line 440 of file app_addon_sql_mysql.c.

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

Definition at line 127 of file app_addon_sql_mysql.c.

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

Referenced by add_identifier_and_set_asterisk_int().

00127                                                           {
00128    struct ast_MYSQL_id *i,*j;
00129    struct MYSQLidshead *headp;
00130    int maxidentifier=0;
00131    
00132    headp=&_mysql_ids_head;
00133    i=NULL;
00134    j=NULL;
00135    
00136    if (AST_LIST_LOCK(headp)) {
00137       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00138       return(-1);
00139    } else {
00140       i=malloc(sizeof(struct ast_MYSQL_id));
00141       AST_LIST_TRAVERSE(headp,j,entries) {
00142          if (j->identifier>maxidentifier) {
00143             maxidentifier=j->identifier;
00144          }
00145       }
00146       i->identifier=maxidentifier+1;
00147       i->identifier_type=identifier_type;
00148       i->data=data;
00149       AST_LIST_INSERT_HEAD(headp,i,entries);
00150       AST_LIST_UNLOCK(headp);
00151    }
00152    return i->identifier;
00153 }

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

Definition at line 197 of file app_addon_sql_mysql.c.

References add_identifier(), and set_asterisk_int().

Referenced by aMYSQL_connect(), and aMYSQL_query().

00197                                                                                                                          {
00198    return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
00199 }

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

Definition at line 350 of file app_addon_sql_mysql.c.

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

Referenced by MYSQL_exec().

00350                                                               {
00351 
00352    MYSQL_RES *mysqlres;
00353 
00354    int id;
00355    strsep(&data," "); // eat the first token, we already know it :P 
00356    id = safe_scan_int(&data," \n",-1);
00357    if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
00358       ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
00359    } else {
00360       mysql_free_result(mysqlres);
00361       del_identifier(id,AST_MYSQL_ID_RESID);
00362    }
00363 
00364    return 0;
00365 }

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

Definition at line 213 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, and mysql.

Referenced by MYSQL_exec().

00213                                                                 {
00214    
00215    MYSQL *mysql;
00216 
00217    char *connid_var;
00218    char *dbhost;
00219    char *dbuser;
00220    char *dbpass;
00221    char *dbname;
00222     
00223    strsep(&data," "); // eat the first token, we already know it :P 
00224 
00225    connid_var=strsep(&data," ");
00226    dbhost=strsep(&data," ");
00227    dbuser=strsep(&data," ");
00228    dbpass=strsep(&data," ");
00229    dbname=strsep(&data,"\n");
00230    
00231    if( connid_var && dbhost && dbuser && dbpass && dbname ) {
00232       mysql = mysql_init(NULL);
00233       if (mysql) {
00234          if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
00235             add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
00236             return 0;
00237          }
00238          else {
00239             ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
00240          }
00241       }
00242       else {
00243          ast_log(LOG_WARNING,"myslq_init returned NULL\n");
00244       }
00245    }
00246    else {
00247       ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
00248    }
00249 
00250    return -1;
00251 }

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

Definition at line 367 of file app_addon_sql_mysql.c.

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

Referenced by MYSQL_exec().

00367                                                                    {
00368    
00369    MYSQL *mysql;
00370    int id;
00371    strsep(&data," "); // eat the first token, we already know it :P 
00372 
00373    id = safe_scan_int(&data," \n",-1);
00374    if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
00375       ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
00376    } else {
00377       mysql_close(mysql);
00378       del_identifier(id,AST_MYSQL_ID_CONNID);
00379    } 
00380 
00381    return 0;
00382 }

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

Definition at line 300 of file app_addon_sql_mysql.c.

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

Referenced by MYSQL_exec().

00300                                                               {
00301    
00302    MYSQL_RES *mysqlres;
00303    MYSQL_ROW mysqlrow;
00304 
00305    char *fetchid_var,*s5,*s6;
00306    int resultid,numFields,j;
00307    
00308    strsep(&data," "); // eat the first token, we already know it :P 
00309 
00310    fetchid_var = strsep(&data," ");
00311    resultid    = safe_scan_int(&data," ",-1);
00312 
00313    if (fetchid_var && (resultid>=0) ) {
00314       if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
00315          /* Grab the next row */
00316          if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
00317             numFields=mysql_num_fields(mysqlres);
00318             for (j=0;j<numFields;j++) {
00319                s5=strsep(&data," ");
00320                if (s5==NULL) {
00321                   ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
00322                   break;
00323                }
00324                s6=mysqlrow[j];
00325                pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
00326             }
00327 #if EXTRA_LOG
00328             ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
00329 #endif
00330             set_asterisk_int(chan,fetchid_var,1); // try more rows
00331          } else {
00332 #if EXTRA_LOG
00333             ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
00334 #endif
00335             set_asterisk_int(chan,fetchid_var,0); // no more rows
00336          }
00337          return 0;
00338       }
00339       else {
00340          ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
00341       }
00342    }
00343    else {
00344       ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
00345    }
00346 
00347    return -1;
00348 }

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

Definition at line 253 of file app_addon_sql_mysql.c.

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

Referenced by MYSQL_exec().

00253                                                               {
00254    
00255    MYSQL       *mysql;
00256    MYSQL_RES   *mysqlres;
00257 
00258    char *resultid_var;
00259    int connid;
00260    char *querystring;
00261    int mysql_query_res;
00262 
00263    strsep(&data," "); // eat the first token, we already know it :P 
00264 
00265    resultid_var = strsep(&data," ");
00266    connid       = safe_scan_int(&data," ",-1);
00267    querystring  = strsep(&data,"\n");
00268 
00269    if (resultid_var && (connid>=0) && querystring) {
00270       if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
00271          mysql_query_res = mysql_query(mysql,querystring);
00272          if (mysql_query_res != 0) {
00273             ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
00274          }
00275          else {
00276             if ((mysqlres=mysql_use_result(mysql))!=NULL) {
00277                add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
00278                return 0;
00279             }
00280             else if (mysql_field_count(mysql)==0) {
00281                return 0;  // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
00282             }
00283             else {
00284                ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
00285             }
00286          }
00287       }
00288       else {
00289          ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
00290       }
00291    }
00292    else {
00293       ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
00294    }
00295    
00296    return -1;
00297 }

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

Definition at line 155 of file app_addon_sql_mysql.c.

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

Referenced by aMYSQL_clear(), and aMYSQL_disconnect().

00155                                                               {
00156    struct ast_MYSQL_id *i;
00157    struct MYSQLidshead *headp;
00158    int found=0;
00159    
00160         headp=&_mysql_ids_head;
00161         
00162         if (AST_LIST_LOCK(headp)) {
00163       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00164    } else {
00165       AST_LIST_TRAVERSE(headp,i,entries) {
00166          if ((i->identifier==identifier) && 
00167              (i->identifier_type==identifier_type)) {
00168             AST_LIST_REMOVE(headp,i,entries);
00169             free(i);
00170             found=1;
00171             break;
00172          }
00173       }
00174       AST_LIST_UNLOCK(headp);
00175    }
00176                    
00177    if (found==0) {
00178       ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
00179       return(-1);
00180    } else {
00181       return(0);
00182    }
00183 }

static void* find_identifier ( int  identifier,
int  identifier_type 
) [static]

Definition at line 100 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.

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

00100                                                                  {
00101    struct MYSQLidshead *headp;
00102    struct ast_MYSQL_id *i;
00103    void *res=NULL;
00104    int found=0;
00105    
00106    headp=&_mysql_ids_head;
00107    
00108    if (AST_LIST_LOCK(headp)) {
00109       ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
00110    } else {
00111       AST_LIST_TRAVERSE(headp,i,entries) {
00112          if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
00113             found=1;
00114             res=i->data;
00115             break;
00116          }
00117       }
00118       if (!found) {
00119          ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
00120       }
00121       AST_LIST_UNLOCK(headp);
00122    }
00123    
00124    return res;
00125 }

static int load_module ( void   )  [static]

Definition at line 433 of file app_addon_sql_mysql.c.

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

00434 {
00435    struct MYSQLidshead *headp = &_mysql_ids_head;
00436    AST_LIST_HEAD_INIT(headp);
00437    return ast_register_application(app, MYSQL_exec, synopsis, descrip);
00438 }

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

Definition at line 384 of file app_addon_sql_mysql.c.

References _mysql_mutex, 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, LOG_WARNING, pbx_builtin_setvar_helper(), and result.

Referenced by load_module().

00385 {
00386    struct ast_module_user *u;
00387    int result;
00388    char sresult[10];
00389 
00390 #if EXTRA_LOG
00391    fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
00392 #endif
00393 
00394    if (!data) {
00395       ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
00396       return -1;
00397    }
00398 
00399    u = ast_module_user_add(chan);
00400    result=0;
00401 
00402    ast_mutex_lock(&_mysql_mutex);
00403 
00404    if (strncasecmp("connect",data,strlen("connect"))==0) {
00405       result=aMYSQL_connect(chan,ast_strdupa(data));
00406    } else   if (strncasecmp("query",data,strlen("query"))==0) {
00407       result=aMYSQL_query(chan,ast_strdupa(data));
00408    } else   if (strncasecmp("fetch",data,strlen("fetch"))==0) {
00409       result=aMYSQL_fetch(chan,ast_strdupa(data));
00410    } else   if (strncasecmp("clear",data,strlen("clear"))==0) {
00411       result=aMYSQL_clear(chan,ast_strdupa(data));
00412    } else   if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
00413       result=aMYSQL_disconnect(chan,ast_strdupa(data));
00414    } else {
00415       ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
00416       result=-1;  
00417    }
00418       
00419    ast_mutex_unlock(&_mysql_mutex);
00420 
00421    ast_module_user_remove(u);
00422    snprintf(sresult, sizeof(sresult), "%d", result);
00423    pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
00424    return 0;
00425 }

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

Definition at line 201 of file app_addon_sql_mysql.c.

References s.

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

00201                                                               {
00202    char* end;
00203    int res = def;
00204    char* s = strsep(data,delim);
00205    if( s ) {
00206       res = strtol(s,&end,10);
00207       if (*end) res = def;  /* not an integer */
00208    }
00209    return res;
00210 }

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

Definition at line 185 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().

00185                                                                              {
00186    if( id>=0 ) {
00187       char s[100] = "";
00188       snprintf(s, sizeof(s)-1, "%d", id);
00189 #if EXTRA_LOG
00190       ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
00191 #endif
00192       pbx_builtin_setvar_helper(chan,varname,s);
00193    }
00194    return id;
00195 }

static int unload_module ( void   )  [static]

Definition at line 427 of file app_addon_sql_mysql.c.

References app, ast_module_user_hangup_all, and ast_unregister_application().

00428 {
00429    ast_module_user_hangup_all();
00430    return ast_unregister_application(app);
00431 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Simple Mysql Interface" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 440 of file app_addon_sql_mysql.c.

ast_mutex_t _mysql_mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 83 of file app_addon_sql_mysql.c.

Referenced by MYSQL_exec().

char* app = "MYSQL" [static]

Definition at line 45 of file app_addon_sql_mysql.c.

Referenced by load_module(), and unload_module().

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 440 of file app_addon_sql_mysql.c.

char* descrip [static]

Definition at line 49 of file app_addon_sql_mysql.c.

Referenced by load_module().

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

Definition at line 47 of file app_addon_sql_mysql.c.

Referenced by load_module().


Generated on Fri Jul 3 20:35:46 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9