#include <asterisk.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <stdio.h>#include <pgsql/libpq-fe.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>

Go to the source code of this file.
Data Structures | |
| struct | ast_PGSQL_id |
| struct | PGSQLidshead |
Defines | |
| #define | EXTRA_LOG 0 |
Enumerations | |
| enum | id_type { AST_PGSQL_ID_DUMMY = 0, AST_PGSQL_ID_CONNID, AST_PGSQL_ID_RESID, AST_PGSQL_ID_FETCHID } |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static int | add_identifier (const enum id_type identifier_type, void *data) |
| static int | aPGSQL_clear (struct ast_channel *chan, void *data) |
| static int | aPGSQL_connect (struct ast_channel *chan, void *data) |
| static int | aPGSQL_disconnect (struct ast_channel *chan, void *data) |
| static int | aPGSQL_fetch (struct ast_channel *chan, void *data) |
| static int | aPGSQL_query (struct ast_channel *chan, void *data) |
| static int | aPGSQL_reset (struct ast_channel *chan, void *data) |
| static int | del_identifier (const int identifier, const enum id_type identifier_type) |
| static void * | find_identifier (const int identifier, const enum id_type identifier_type) |
| static int | load_module (void) |
| static int | PGSQL_exec (struct ast_channel *chan, void *data) |
| 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 PostgreSQL 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 ast_mutex_t | _pgsql_mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) |
| static char * | app = "PGSQL" |
| static const struct ast_module_info * | ast_module_info = &__mod_info |
| static char * | descrip |
| static char * | synopsis = "Do several PgSQLy things" |
| #define EXTRA_LOG 0 |
Definition at line 42 of file app_addon_sql_pgsql.c.
| enum id_type |
Definition at line 112 of file app_addon_sql_pgsql.c.
00112 { 00113 AST_PGSQL_ID_DUMMY = 0, 00114 AST_PGSQL_ID_CONNID, 00115 AST_PGSQL_ID_RESID, 00116 AST_PGSQL_ID_FETCHID 00117 };
| static void __reg_module | ( | void | ) | [static] |
Definition at line 517 of file app_addon_sql_pgsql.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 517 of file app_addon_sql_pgsql.c.
| static int add_identifier | ( | const enum id_type | identifier_type, | |
| void * | data | |||
| ) | [static] |
Definition at line 156 of file app_addon_sql_pgsql.c.
References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_malloc, ast_PGSQL_id::data, ast_PGSQL_id::entries, ast_PGSQL_id::identifier, ast_PGSQL_id::identifier_type, and LOG_WARNING.
Referenced by aPGSQL_connect(), aPGSQL_fetch(), and aPGSQL_query().
00157 { 00158 struct ast_PGSQL_id *i, *j; 00159 struct PGSQLidshead *headp; 00160 int maxidentifier = 0; 00161 00162 headp = &PGSQLidshead; 00163 i = NULL; 00164 j = NULL; 00165 00166 if (AST_LIST_LOCK(headp)) { 00167 ast_log(LOG_WARNING, "Unable to lock identifiers list\n"); 00168 return -1; 00169 } 00170 00171 if (!(i = ast_malloc(sizeof(*i)))) { 00172 AST_LIST_UNLOCK(headp); 00173 return -1; 00174 } 00175 00176 AST_LIST_TRAVERSE(headp, j, entries) { 00177 if (j->identifier > maxidentifier) 00178 maxidentifier = j->identifier; 00179 } 00180 i->identifier = maxidentifier + 1; 00181 i->identifier_type = identifier_type; 00182 i->data = data; 00183 AST_LIST_INSERT_HEAD(headp, i, entries); 00184 AST_LIST_UNLOCK(headp); 00185 00186 return i->identifier; 00187 }
| static int aPGSQL_clear | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 408 of file app_addon_sql_pgsql.c.
References ast_log(), AST_PGSQL_ID_RESID, ast_strdupa, del_identifier(), find_identifier(), id, LOG_WARNING, and s.
Referenced by PGSQL_exec().
00409 { 00410 char *s; 00411 PGresult *PGSQLres; 00412 int id; 00413 char *stringp = NULL; 00414 00415 if (!(stringp = ast_strdupa(data))) 00416 return -1; 00417 00418 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00419 s = strsep(&stringp, " "); 00420 id = atoi(s); 00421 if (!(PGSQLres = find_identifier(id, AST_PGSQL_ID_RESID))) { 00422 ast_log(LOG_WARNING, "aPGSQL_clear : Invalid result identifier %d passed in aPGSQL_clear\n", id); 00423 } else { 00424 PQclear(PGSQLres); 00425 del_identifier(id, AST_PGSQL_ID_RESID); 00426 } 00427 00428 return 0; 00429 }
| static int aPGSQL_connect | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 220 of file app_addon_sql_pgsql.c.
References add_identifier(), ast_log(), AST_PGSQL_ID_CONNID, ast_strdupa, id, LOG_WARNING, pbx_builtin_setvar_helper(), s, and var.
Referenced by PGSQL_exec().
00221 { 00222 char s[100] = ""; 00223 char *optionstring; 00224 char *var; 00225 int res = 0; 00226 PGconn *PGSQLconn; 00227 int id; 00228 char *stringp = NULL; 00229 00230 if (!(stringp = ast_strdupa(data))) 00231 return -1; 00232 00233 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00234 var = strsep(&stringp, " "); 00235 optionstring = strsep(&stringp, "\n"); 00236 00237 PGSQLconn = PQconnectdb(optionstring); 00238 if (PQstatus(PGSQLconn) == CONNECTION_BAD) { 00239 ast_log(LOG_WARNING, "Connection to database using '%s' failed. postgress reports : %s\n", optionstring, PQerrorMessage(PGSQLconn)); 00240 res = -1; 00241 } else { 00242 ast_log(LOG_WARNING, "Adding identifier\n"); 00243 id = add_identifier(AST_PGSQL_ID_CONNID, PGSQLconn); 00244 snprintf(s, sizeof(s), "%d", id); 00245 pbx_builtin_setvar_helper(chan, var, s); 00246 } 00247 00248 return res; 00249 }
| static int aPGSQL_disconnect | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 431 of file app_addon_sql_pgsql.c.
References ast_log(), AST_PGSQL_ID_CONNID, ast_strdupa, del_identifier(), find_identifier(), id, LOG_WARNING, and s.
Referenced by PGSQL_exec().
00432 { 00433 char *s; 00434 PGconn *PGSQLconn; 00435 int id; 00436 char *stringp = NULL; 00437 00438 if (!(stringp = ast_strdupa(data))) 00439 return -1; 00440 00441 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00442 s = strsep(&stringp, " "); 00443 id = atoi(s); 00444 if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { 00445 ast_log(LOG_WARNING, "aPGSQL_disconnect : Invalid connection identifier %d passed in aPGSQL_disconnect\n", id); 00446 } else { 00447 PQfinish(PGSQLconn); 00448 del_identifier(id, AST_PGSQL_ID_CONNID); 00449 } 00450 00451 return 0; 00452 }
| static int aPGSQL_fetch | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 299 of file app_addon_sql_pgsql.c.
References add_identifier(), AST_LIST_TRAVERSE, ast_log(), ast_malloc, AST_PGSQL_ID_FETCHID, AST_PGSQL_ID_RESID, ast_strdupa, ast_var_name(), ast_var_value(), del_identifier(), ast_PGSQL_id::entries, find_identifier(), free, id, LOG_WARNING, pbx_builtin_setvar_helper(), s, var, and ast_channel::varshead.
Referenced by PGSQL_exec().
00300 { 00301 char *s2, *s3, *s4, *fetchid_var; 00302 const char *s5 = NULL; 00303 char s[100]; 00304 char *var; 00305 int res = 0; 00306 PGresult *PGSQLres; 00307 int id, id1, i, j, fnd; 00308 int *identp = NULL; 00309 int nres; 00310 struct ast_var_t *variables; 00311 struct varshead *headp; 00312 char *stringp = NULL; 00313 00314 headp = &chan->varshead; 00315 00316 if (!(stringp = ast_strdupa(data))) 00317 return -1; 00318 00319 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00320 fetchid_var = strsep(&stringp, " "); 00321 while (1) { /* ugly trick to make branches with break; */ 00322 var = fetchid_var; /* fetchid */ 00323 fnd = 0; 00324 00325 AST_LIST_TRAVERSE(headp, variables, entries) { 00326 if (!(strncasecmp(ast_var_name(variables), fetchid_var, strlen(fetchid_var)))) { 00327 s5 = ast_var_value(variables); 00328 fnd = 1; 00329 break; 00330 } 00331 } 00332 00333 if (!fnd) { 00334 s5 = "0"; 00335 pbx_builtin_setvar_helper(chan, fetchid_var, s5); 00336 } 00337 00338 s2 = strsep(&stringp, " "); 00339 id = atoi(s2); /* resultid */ 00340 if (!(PGSQLres = find_identifier(id, AST_PGSQL_ID_RESID))) { 00341 ast_log(LOG_WARNING, "ast_PGSQL_fetch : Invalid result identifier %d passed in aPGSQL_fetch\n", id); 00342 res = -1; 00343 break; 00344 } 00345 id = atoi(s5); /*fetchid */ 00346 if (!(identp = find_identifier(id, AST_PGSQL_ID_FETCHID))) { 00347 i = 0; /* fetching the very first row */ 00348 } else { 00349 i = *identp; 00350 free(identp); 00351 del_identifier(id, AST_PGSQL_ID_FETCHID); /* will re-add it a bit later */ 00352 } 00353 00354 if (i < PQntuples(PGSQLres)) { 00355 nres = PQnfields(PGSQLres); 00356 ast_log(LOG_WARNING, "ast_PGSQL_fetch : nres = %d i = %d ;\n", nres, i); 00357 for (j = 0; j < nres; j++) { 00358 if (!(s3 = strsep(&stringp, " "))) { 00359 ast_log(LOG_WARNING, "ast_PGSQL_fetch : More tuples (%d) than variables (%d)\n", nres, j); 00360 break; 00361 } 00362 if (!(s4 = PQgetvalue(PGSQLres, i, j))) { 00363 ast_log(LOG_WARNING, "ast_PGSQL_fetch : PQgetvalue(res, %d, %d) returned NULL in ast_PGSQL_fetch\n", i, j); 00364 break; 00365 } 00366 ast_log(LOG_WARNING, "ast_PGSQL_fetch : ===setting variable '%s' to '%s'\n", s3, s4); 00367 pbx_builtin_setvar_helper(chan, s3, s4); 00368 } 00369 if (!(identp = ast_malloc(sizeof(int)))) 00370 return -1; 00371 *identp = ++i; /* advance to the next row */ 00372 id1 = add_identifier(AST_PGSQL_ID_FETCHID, identp); 00373 } else { 00374 ast_log(LOG_WARNING, "ast_PGSQL_fetch : EOF\n"); 00375 id1 = 0; /* no more rows */ 00376 } 00377 snprintf(s, sizeof(s), "%d", id1); 00378 ast_log(LOG_WARNING, "ast_PGSQL_fetch : Setting var '%s' to value '%s'\n", fetchid_var, s); 00379 pbx_builtin_setvar_helper(chan, fetchid_var, s); 00380 break; 00381 } 00382 00383 return res; 00384 }
| static int aPGSQL_query | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 251 of file app_addon_sql_pgsql.c.
References add_identifier(), ast_log(), AST_PGSQL_ID_CONNID, AST_PGSQL_ID_RESID, ast_strdupa, find_identifier(), id, LOG_WARNING, pbx_builtin_setvar_helper(), s, and var.
Referenced by PGSQL_exec().
00252 { 00253 char *s2, *s3; 00254 char s[100] = ""; 00255 char *querystring; 00256 char *var; 00257 int res = 0, nres; 00258 PGconn *PGSQLconn; 00259 PGresult *PGSQLres; 00260 int id, id1; 00261 char *stringp = NULL; 00262 00263 if (!(stringp = ast_strdupa(data))) 00264 return -1; 00265 00266 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00267 s2 = strsep(&stringp, " "); 00268 while (1) { /* ugly trick to make branches with break; */ 00269 var = s2; 00270 s3 = strsep(&stringp, " "); 00271 id = atoi(s3); 00272 querystring = strsep(&stringp, "\n"); 00273 if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { 00274 ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aPGSQL_query\n", id); 00275 res = -1; 00276 break; 00277 } 00278 if (!(PGSQLres = PQexec(PGSQLconn, querystring))) { 00279 ast_log(LOG_WARNING, "aPGSQL_query: Connection Error (connection identifier = %d, error message : %s)\n", id, PQerrorMessage(PGSQLconn)); 00280 res = -1; 00281 break; 00282 } 00283 if (PQresultStatus(PGSQLres) == PGRES_BAD_RESPONSE || PQresultStatus(PGSQLres) == PGRES_NONFATAL_ERROR || PQresultStatus(PGSQLres) == PGRES_FATAL_ERROR) { 00284 ast_log(LOG_WARNING, "aPGSQL_query: Query Error (connection identifier : %d, error message : %s)\n", id, PQcmdStatus(PGSQLres)); 00285 res = -1; 00286 break; 00287 } 00288 nres = PQnfields(PGSQLres); 00289 id1 = add_identifier(AST_PGSQL_ID_RESID, PGSQLres); 00290 snprintf(s, sizeof(s), "%d", id1); 00291 pbx_builtin_setvar_helper(chan, var, s); 00292 break; 00293 } 00294 00295 return res; 00296 }
| static int aPGSQL_reset | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 386 of file app_addon_sql_pgsql.c.
References ast_log(), AST_PGSQL_ID_CONNID, ast_strdupa, find_identifier(), id, LOG_WARNING, and s.
Referenced by PGSQL_exec().
00387 { 00388 char *s; 00389 PGconn *PGSQLconn; 00390 int id; 00391 char *stringp = NULL; 00392 00393 if (!(stringp = ast_strdupa(data))) 00394 return -1; 00395 00396 strsep(&stringp, " "); /* eat the first token, we already know it :P */ 00397 s = strsep(&stringp, " "); 00398 id = atoi(s); 00399 if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { 00400 ast_log(LOG_WARNING, "aPGSQL_reset : Invalid connection identifier %d passed in aPGSQL_reset\n", id); 00401 } else { 00402 PQreset(PGSQLconn); 00403 } 00404 00405 return 0; 00406 }
| static int del_identifier | ( | const int | identifier, | |
| const enum id_type | identifier_type | |||
| ) | [static] |
Definition at line 189 of file app_addon_sql_pgsql.c.
References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_PGSQL_id::entries, free, ast_PGSQL_id::identifier, ast_PGSQL_id::identifier_type, and LOG_WARNING.
Referenced by aPGSQL_clear(), aPGSQL_disconnect(), and aPGSQL_fetch().
00190 { 00191 struct ast_PGSQL_id *i; 00192 struct PGSQLidshead *headp; 00193 int found = 0; 00194 00195 headp = &PGSQLidshead; 00196 00197 if (AST_LIST_LOCK(headp)) { 00198 ast_log(LOG_WARNING, "Unable to lock identifiers list\n"); 00199 } else { 00200 AST_LIST_TRAVERSE(headp, i, entries) { 00201 if ((i->identifier == identifier) && (i->identifier_type == identifier_type)) { 00202 AST_LIST_REMOVE(headp, i, entries); 00203 free(i); 00204 found = 1; 00205 break; 00206 } 00207 } 00208 AST_LIST_UNLOCK(headp); 00209 } 00210 00211 if (!found) { 00212 ast_log(LOG_WARNING, "Could not find identifier %d, identifier_type %d in list to delete\n", identifier, identifier_type); 00213 return -1; 00214 } else { 00215 return 0; 00216 } 00217 }
| static void* find_identifier | ( | const int | identifier, | |
| const enum id_type | identifier_type | |||
| ) | [static] |
Definition at line 128 of file app_addon_sql_pgsql.c.
References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_PGSQL_id::data, ast_PGSQL_id::identifier, ast_PGSQL_id::identifier_type, and LOG_WARNING.
Referenced by aPGSQL_clear(), aPGSQL_disconnect(), aPGSQL_fetch(), aPGSQL_query(), and aPGSQL_reset().
00129 { 00130 struct PGSQLidshead *headp; 00131 struct ast_PGSQL_id *i; 00132 void *res = NULL; 00133 int found = 0; 00134 00135 headp = &PGSQLidshead; 00136 00137 if (AST_LIST_LOCK(headp)) { 00138 ast_log(LOG_WARNING, "Unable to lock identifiers list\n"); 00139 } else { 00140 AST_LIST_TRAVERSE(headp, i, entries) { 00141 if ((i->identifier == identifier) && (i->identifier_type == identifier_type)) { 00142 found = 1; 00143 res = i->data; 00144 break; 00145 } 00146 } 00147 if (!found) { 00148 ast_log(LOG_WARNING, "Identifier %d, identifier_type %d not found in identifier list\n", identifier, identifier_type); 00149 } 00150 AST_LIST_UNLOCK(headp); 00151 } 00152 00153 return res; 00154 }
| static int load_module | ( | void | ) | [static] |
Definition at line 510 of file app_addon_sql_pgsql.c.
References app, AST_LIST_HEAD_INIT, ast_register_application(), descrip, PGSQL_exec(), and synopsis.
00511 { 00512 struct PGSQLidshead *headp = &PGSQLidshead; 00513 AST_LIST_HEAD_INIT(headp); 00514 return ast_register_application(app, PGSQL_exec, synopsis, descrip); 00515 }
| static int PGSQL_exec | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 455 of file app_addon_sql_pgsql.c.
References _pgsql_mutex, aPGSQL_clear(), aPGSQL_connect(), aPGSQL_disconnect(), aPGSQL_fetch(), aPGSQL_query(), aPGSQL_reset(), ast_log(), ast_module_user_add, ast_module_user_remove, ast_mutex_lock(), ast_mutex_unlock(), LOG_WARNING, pbx_builtin_setvar_helper(), and result.
Referenced by load_module().
00456 { 00457 struct ast_module_user *u; 00458 int result; 00459 char sresult[12]; // "-2147483646" 00460 00461 #if EXTRA_LOG 00462 fprintf(stderr, "PGSQL_exec: data=%s\n", (char *) data); 00463 #endif 00464 00465 if (!data) { 00466 ast_log(LOG_WARNING, "APP_PGSQL requires an argument (see manual)\n"); 00467 return -1; 00468 } 00469 00470 u = ast_module_user_add(chan); 00471 result = 0; 00472 00473 ast_mutex_lock(&_pgsql_mutex); 00474 00475 if (!(strncasecmp("connect", data, strlen("connect")))) { 00476 result = (aPGSQL_connect(chan, data)); 00477 } else if (!(strncasecmp("query", data, strlen("query")))) { 00478 result = (aPGSQL_query(chan, data)); 00479 } else if (!(strncasecmp("fetch", data, strlen("fetch")))) { 00480 result = (aPGSQL_fetch(chan, data)); 00481 } else if (!(strncasecmp("reset", data, strlen("reset")))) { 00482 result = (aPGSQL_reset(chan, data)); 00483 } else if (!(strncasecmp("clear", data, strlen("clear")))) { 00484 result = (aPGSQL_clear(chan, data)); 00485 /* 00486 } else if (!(strncasecmp("debug", data, strlen("debug")))) { 00487 result = (aPGSQL_debug(chan, data)); 00488 */ 00489 } else if (!(strncasecmp("disconnect", data, strlen("disconnect")))) { 00490 result = (aPGSQL_disconnect(chan, data)); 00491 } else { 00492 ast_log(LOG_WARNING, "Unknown APP_PGSQL argument : %s\n", (char *) data); 00493 result = -1; 00494 } 00495 00496 ast_mutex_unlock(&_pgsql_mutex); 00497 00498 ast_module_user_remove(u); 00499 snprintf(sresult, sizeof(sresult), "%d", result); 00500 pbx_builtin_setvar_helper(chan, "PGSQL_STATUS", sresult); 00501 return 0; 00502 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 504 of file app_addon_sql_pgsql.c.
References app, ast_module_user_hangup_all, and ast_unregister_application().
00505 { 00506 ast_module_user_hangup_all(); 00507 return ast_unregister_application(app); 00508 }
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Simple PostgreSQL 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 517 of file app_addon_sql_pgsql.c.
ast_mutex_t _pgsql_mutex = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static] |
char* app = "PGSQL" [static] |
Definition at line 44 of file app_addon_sql_pgsql.c.
Referenced by load_module(), and unload_module().
const struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 517 of file app_addon_sql_pgsql.c.
char* descrip [static] |
char* synopsis = "Do several PgSQLy things" [static] |
1.5.9