Module: sip-router Branch: master Commit: 17bc823ded7489543686c1bae4253dadf96408d9 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=17bc823d...
Author: Juha Heinanen jh@tutpro.com Committer: Juha Heinanen jh@tutpro.com Date: Wed Jun 3 22:39:04 2009 +0300
* Improved presence database handling.
---
modules/utils/utils.c | 110 +++++++++++++++++++++----------------------- modules/utils/utils.h | 4 +- modules/utils/xcap_auth.c | 14 +++--- 3 files changed, 62 insertions(+), 66 deletions(-)
diff --git a/modules/utils/utils.c b/modules/utils/utils.c index 7088027..2366560 100644 --- a/modules/utils/utils.c +++ b/modules/utils/utils.c @@ -46,7 +46,7 @@
MODULE_VERSION
-#define XCAP_TABLE_VERSION 3 +#define XCAP_TABLE_VERSION 3
/* Module parameter variables */ int http_query_timeout = 4; @@ -68,8 +68,8 @@ static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param);
/* Database connection */ -db1_con_t *pxml_db = NULL; -db_func_t pxml_dbf; +db1_con_t *pres_dbh = NULL; +db_func_t pres_dbf;
/* Module management function prototypes */ static int mod_init(void); @@ -164,6 +164,51 @@ static void destroy_shmlock(void) }
+static void pres_db_close(void) { + if (pres_dbh) { + pres_dbf.close(pres_dbh); + pres_dbh = NULL; + } +} + +static int pres_db_init(void) { + if (!pres_db_url.s || !pres_db_url.len) { + LM_INFO("xcap_auth_status function is disabled\n"); + return 0; + } + if (db_bind_mod(&pres_db_url, &pres_dbf) < 0) { + LM_ERR("can't bind database module\n"); + return -1; + } + if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) { + LM_ERR("can't connect to database\n"); + return -1; + } + if (db_check_table_version(&pres_dbf, pres_dbh, &xcap_table, + XCAP_TABLE_VERSION) < 0) { + LM_ERR("during table version check\n"); + pres_db_close(); + return -1; + } + pres_db_close(); + return 0; +} + +static int pres_db_open(void) { + if (!pres_db_url.s || !pres_db_url.len) { + return 0; + } + if (pres_dbh) { + pres_dbf.close(pres_dbh); + } + if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) { + LM_ERR("can't connect to database\n"); + return -1; + } + return 0; +} + + /* Module initialization function */ static int mod_init(void) { @@ -224,38 +269,10 @@ static int mod_init(void) pres_db_url.s); xcap_table.len = xcap_table.s ? strlen(xcap_table.s) : 0;
- if (pres_db_url.len == 0) { - LM_DBG("xcap_auth_status() function disabled\n"); - return 0; - } - - /* binding to mysql module */ - if (db_bind_mod(&pres_db_url, &pxml_dbf)) { - LM_ERR("Database module not found\n"); - return -1; - } - - if (!DB_CAPABILITY(pxml_dbf, DB_CAP_ALL)) { - LM_ERR("Database module does not implement all functions" - " needed by xcap_auth_status() function\n"); - return -1; - } - - pxml_db = pxml_dbf.init(&pres_db_url); - if (!pxml_db) { - LM_ERR("while connecting to database\n"); - return -1; - } - - if (db_check_table_version(&pxml_dbf, pxml_db, &xcap_table, - XCAP_TABLE_VERSION) < 0) { - LM_ERR("error during table version check.\n"); + if(pres_db_init() < 0) { return -1; }
- pxml_dbf.close(pxml_db); - pxml_db = NULL; - return 0; }
@@ -263,30 +280,8 @@ static int mod_init(void) /* Child initialization function */ static int child_init(int rank) { - if (pres_db_url.len == 0) - return 0; - - if (pxml_dbf.init==0) { - LM_CRIT("database not bound\n"); - return -1; - } - - pxml_db = pxml_dbf.init(&pres_db_url); - if (pxml_db == NULL) { - LM_ERR("while connecting database\n"); - return -1; - } - - if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) { - LM_ERR("in use_table SQL operation\n"); - return -1; - } - - LM_DBG("database connection opened successfully\n"); - - return 0; -} - + return pres_db_open(); +}
static void destroy(void) { @@ -295,6 +290,8 @@ static void destroy(void) /* Cleanup forward */ conf_destroy(); destroy_shmlock(); + /* Close pres db */ + pres_db_close(); }
@@ -319,7 +316,6 @@ static int fixup_http_query(void** param, int param_no) LM_ERR("result pvar is not writeble\n"); return -1; } - LM_INFO("leaving fixup_http_query\n"); return 0; }
diff --git a/modules/utils/utils.h b/modules/utils/utils.h index 651de26..96d0286 100644 --- a/modules/utils/utils.h +++ b/modules/utils/utils.h @@ -30,7 +30,7 @@
extern int http_query_timeout; extern str xcap_table; -extern db1_con_t *pxml_db; -extern db_func_t pxml_dbf; +extern db1_con_t *pres_dbh; +extern db_func_t pres_dbf;
#endif /* UTILS_H */ diff --git a/modules/utils/xcap_auth.c b/modules/utils/xcap_auth.c index e527a5f..be419a8 100644 --- a/modules/utils/xcap_auth.c +++ b/modules/utils/xcap_auth.c @@ -367,17 +367,17 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
result_cols[xcap_doc_col= n_result_cols++] = &tmp4; - if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) { + if (pres_dbf.use_table(pres_dbh, &xcap_table) < 0) { LM_ERR("in use_table-[table]= %.*s\n", xcap_table.len, xcap_table.s); return -1; }
- if (pxml_dbf.query(pxml_db, query_cols, 0 , query_vals, result_cols, + if (pres_dbf.query(pres_dbh, query_cols, 0 , query_vals, result_cols, n_query_cols, 1, 0, &result) < 0) { LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n", user->len, user->s, domain->len, domain->s); if (result) - pxml_dbf.free_result(pxml_db, result); + pres_dbf.free_result(pres_dbh, result); return -1; }
@@ -388,7 +388,7 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc) LM_DBG("No document found in db table for [user]=%.*s" "\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s, domain->len, domain->s, type); - pxml_dbf.free_result(pxml_db, result); + pres_dbf.free_result(pres_dbh, result); return 0; } @@ -422,13 +422,13 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc) *rules_doc= doc;
if (result) - pxml_dbf.free_result(pxml_db, result); + pres_dbf.free_result(pres_dbh, result);
return 0;
error: if (result) - pxml_dbf.free_result(pxml_db, result); + pres_dbf.free_result(pres_dbh, result);
return -1;
@@ -449,7 +449,7 @@ int xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2) subs_t subs; int res;
- if (pxml_db == 0) { + if (pres_dbh == 0) { LM_ERR("function is disabled, to enable define pres_db_url\n"); return -1; }