db.c File Reference


Detailed Description

Interrogating-CSCF - Database operations.

Author:
Dragos Vingarzan vingarzan -at- fokus dot fraunhofer dot de

Definition in file db.c.

#include "db.h"
#include "../../db/db.h"
#include "../../mem/shm_mem.h"
#include "mod.h"

Go to the source code of this file.

Functions

int icscf_db_bind (char *db_url)
 Bind to the database module.
int icscf_db_init (char *db_url, char *db_table_nds, char *db_table_scscf, char *db_table_capabilities)
 Init the database connection.
void icscf_db_close ()
 Close the database connection.
static int icscf_db_check_init (db_con_t *db_hdl)
 Simply check if the database connection was initialized and connect if not.
int icscf_db_get_nds (str *d[])
 Get the NDS list from the database.
int icscf_db_get_scscf (scscf_capabilities *cap[])
 Get the S-CSCF names from the database and create the S-CSCF set.
int icscf_db_get_capabilities (scscf_capabilities *cap[], int cap_cnt)
 Get the S-CSCF capabilities from the database and fill the S-CSCF set.

Variables

static db_func_t dbf
 db function bindings
char * icscf_db_url
 DB URL.
char * icscf_db_nds_table
 NDS table in DB.
char * icscf_db_scscf_table
 S-CSCF table in db.
char * icscf_db_capabilities_table
 S-CSCF capabilities table in db.
static db_con_t * hdl_nds = 0
 handle for the NDS table
static db_con_t * hdl_scscf = 0
 handle for the S-CSCF table
static db_con_t * hdl_capabilities = 0
 handle for the S-CSCF capabilities table


Function Documentation

int icscf_db_bind ( char *  db_url  ) 

Bind to the database module.

Parameters:
db_url - URL of the database
Returns:
0 on success, -1 on error

Definition at line 80 of file db.c.

References dbf, icscf_db_url, and M_NAME.

Referenced by icscf_mod_init().

00081 {
00082     if (bind_dbmod(icscf_db_url, &dbf)) {
00083         LOG(L_CRIT, "CRIT:"M_NAME":icscf_db_bind: cannot bind to database module! "
00084         "Did you forget to load a database module ?\n");
00085         return -1;
00086     }
00087     return 0;
00088 }

int icscf_db_init ( char *  db_url,
char *  db_table_nds,
char *  db_table_scscf,
char *  db_table_capabilities 
)

Init the database connection.

Parameters:
db_url - URL of the database
db_table_nds - name of the NDS table
db_table_scscf - name of the S-CSCF table
db_table_capabilities - name of the S-CSCF capabilities table
Returns:
0 on success, -1 on error

Definition at line 99 of file db.c.

References dbf, hdl_capabilities, hdl_nds, hdl_scscf, and M_NAME.

Referenced by icscf_db_check_init(), and icscf_mod_child_init().

00103 {
00104     if (dbf.init==0){
00105         LOG(L_CRIT, "BUG:"M_NAME":icscf_db_init: unbound database module\n");
00106         return -1;
00107     }
00108     /* NDS */
00109     hdl_nds=dbf.init(db_url);
00110     if (hdl_nds==0){
00111         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot initialize database "
00112             "connection\n");
00113         goto error;
00114     }   
00115     if (dbf.use_table(hdl_nds, db_table_nds)<0) {
00116         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot select table \"%s\"\n",db_table_nds);
00117         goto error;
00118     }
00119     /* S_CSCF */
00120     hdl_scscf=dbf.init(db_url);
00121     if (hdl_scscf==0){
00122         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot initialize database "
00123             "connection\n");
00124         goto error;
00125     }   
00126     if (dbf.use_table(hdl_scscf, db_table_scscf)<0) {
00127         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot select table \"%s\"\n",db_table_scscf);
00128         goto error;
00129     }
00130     /* Capabilities */
00131     hdl_capabilities=dbf.init(db_url);
00132     if (hdl_capabilities==0){
00133         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot initialize database "
00134             "connection\n");
00135         goto error;
00136     }   
00137     if (dbf.use_table(hdl_capabilities, db_table_capabilities)<0) {
00138         LOG(L_CRIT,"ERR:"M_NAME":icscf_db_init: cannot select table \"%s\"\n",db_table_capabilities);
00139         goto error;
00140     }
00141 
00142     return 0;
00143 
00144 error:
00145     if (hdl_nds){
00146         dbf.close(hdl_nds);
00147         hdl_nds=0;
00148     }
00149     if (hdl_scscf){
00150         dbf.close(hdl_scscf);
00151         hdl_nds=0;
00152     }
00153     if (hdl_capabilities){
00154         dbf.close(hdl_capabilities);
00155         hdl_nds=0;
00156     }
00157     return -1;
00158 }

void icscf_db_close (  ) 

Close the database connection.

Definition at line 163 of file db.c.

References dbf, and hdl_nds.

00164 {
00165     if (hdl_nds && dbf.close){
00166         dbf.close(hdl_nds);
00167         hdl_nds=0;
00168     }
00169 }

static int icscf_db_check_init ( db_con_t *  db_hdl  )  [inline, static]

Simply check if the database connection was initialized and connect if not.

Parameters:
db_hdl - database handle to test
Returns:
1 if connected, 0 if not

Definition at line 176 of file db.c.

References icscf_db_capabilities_table, icscf_db_init(), icscf_db_nds_table, icscf_db_scscf_table, and icscf_db_url.

Referenced by icscf_db_get_capabilities(), icscf_db_get_nds(), and icscf_db_get_scscf().

00177 {
00178     if (db_hdl) return 1;
00179     return (icscf_db_init( icscf_db_url,
00180         icscf_db_nds_table,
00181         icscf_db_scscf_table,
00182         icscf_db_capabilities_table)==0);       
00183 }

int icscf_db_get_nds ( str *  d[]  ) 

Get the NDS list from the database.

Parameters:
d - array of string to fill with the db contents
Returns:
1 on success, 0 on error

Definition at line 190 of file db.c.

References dbf, hdl_nds, icscf_db_check_init(), if, M_NAME, and NULL.

Referenced by I_NDS_get_trusted_domains().

00191 {
00192     db_key_t   keys_ret[] = {"trusted_domain"};
00193     db_res_t   * res = 0 ;  
00194     str s;
00195     int i;
00196 
00197     if (!icscf_db_check_init(hdl_nds))
00198         goto error;
00199 
00200     DBG("DBG:"M_NAME":icscf_db_get_nds: fetching list of NDS for I-CSCF \n");
00201 
00202     if (dbf.query(hdl_nds, 0, 0, 0, keys_ret, 0, 1, NULL, & res) < 0) {
00203         LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_nds: db_query failed\n");
00204         goto error;
00205     }
00206 
00207     if (res->n == 0) {
00208         DBG("DBG:"M_NAME":icscf_db_get_nds: I-CSCF has no NDS trusted domains in db\n");
00209         *d=shm_malloc(sizeof(str));
00210         if (*d==NULL){
00211             LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_nds: failed shm_malloc for 0 domains\n");
00212             goto error;
00213         }   
00214         (*d)[0].s=0;
00215         (*d)[0].len=0;
00216     }
00217     else {
00218         *d=shm_malloc(sizeof(str)*(res->n+1));
00219         if (*d==NULL){
00220             LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_nds: failed shm_malloc for %d domains\n",
00221                 res->n);
00222             goto error;
00223         }   
00224         for(i=0;i<res->n;i++){
00225             s.s = (char*) res->rows[i].values[0].val.string_val;
00226             s.len = strlen(s.s);
00227             (*d)[i].s = shm_malloc(s.len);
00228             if ((*d)[i].s==NULL) {
00229                 LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_nds: failed shm_malloc for %d bytes\n",
00230                     s.len);
00231                 (*d)[i].len = 0;
00232             }else{
00233                 (*d)[i].len = s.len;
00234                 memcpy((*d)[i].s,s.s,s.len);
00235             }
00236         }
00237         (*d)[res->n].s=0;
00238         (*d)[res->n].len=0;
00239     }
00240 
00241     LOG(L_INFO, "INF:"M_NAME":icscf_db_get_nds: Loaded %d trusted domains\n",
00242         res->n);
00243 
00244     dbf.free_result( hdl_nds, res);
00245     return 1;
00246 error:
00247     if (res)
00248         dbf.free_result( hdl_nds, res);
00249     *d=shm_malloc(sizeof(str));
00250     if (*d==NULL)
00251         LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_nds: failed shm_malloc for 0 domains\n");
00252     else {
00253         (*d)[0].s=0;
00254         (*d)[0].len=0;
00255     }
00256     return 0;
00257 }

int icscf_db_get_scscf ( scscf_capabilities cap[]  ) 

Get the S-CSCF names from the database and create the S-CSCF set.

Parameters:
cap - array of scscf_capabilities to fill with the db contents for the S-CSCF names
Returns:
1 on success, 0 on error

Definition at line 265 of file db.c.

References dbf, hdl_scscf, icscf_db_check_init(), id_s_cscf, if, M_NAME, and scscf_name.

Referenced by I_get_capabilities().

00266 {
00267     db_key_t   keys_ret[] = {"id","s_cscf_uri"};
00268     db_key_t   key_ord = "id";
00269     db_res_t   * res = 0 ;  
00270     int i;
00271 
00272     *cap = 0;
00273         
00274     if (!icscf_db_check_init(hdl_scscf))
00275         goto error;
00276 
00277     DBG("DBG:"M_NAME":icscf_db_get_scscf: fetching S-CSCFs \n");
00278 
00279     if (dbf.query(hdl_scscf, 0, 0, 0, keys_ret, 0, 2, key_ord, & res) < 0) {
00280         LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_scscf: db_query failed\n");
00281         goto error;
00282     }
00283 
00284     if (res->n == 0) {
00285         LOG(L_ERR,"ERR:"M_NAME":icscf_db_get_scscf:  no S-CSCFs found\n");
00286         goto error;
00287     }
00288     else {
00289         *cap = shm_malloc(sizeof(scscf_capabilities)*res->n);
00290         if (!(*cap)) {
00291             LOG(L_ERR,"ERR:"M_NAME":icscf_db_get_scscf: Error allocating %d bytes\n",
00292                 sizeof(scscf_capabilities)*res->n);
00293             goto error;
00294         }
00295         memset((*cap),0,sizeof(scscf_capabilities)*res->n);
00296         for(i=0;i<res->n;i++){
00297             (*cap)[i].id_s_cscf = res->rows[i].values[0].val.int_val;
00298             (*cap)[i].scscf_name.len = strlen(res->rows[i].values[1].val.string_val);
00299             (*cap)[i].scscf_name.s = shm_malloc((*cap)[i].scscf_name.len);
00300             if (!(*cap)[i].scscf_name.s){
00301                 LOG(L_ERR,"ERR:"M_NAME":icscf_db_get_scscf: Error allocating %d bytes\n",
00302                     (*cap)[i].scscf_name.len);
00303                 (*cap)[i].scscf_name.len=0;
00304                 goto error;
00305             }
00306             memcpy((*cap)[i].scscf_name.s,res->rows[i].values[1].val.string_val,
00307                 (*cap)[i].scscf_name.len);
00308         }
00309     }
00310 
00311     dbf.free_result( hdl_scscf, res);
00312     
00313     // return the size of scscf set  
00314     return i;
00315     
00316 error:
00317     if (res)
00318         dbf.free_result( hdl_scscf, res);
00319     return 0;
00320 }

int icscf_db_get_capabilities ( scscf_capabilities cap[],
int  cap_cnt 
)

Get the S-CSCF capabilities from the database and fill the S-CSCF set.

Parameters:
cap - array of scscf_capabilities to fill with capabilities
Returns:
1 on success, 0 on error

Definition at line 327 of file db.c.

References dbf, hdl_capabilities, icscf_db_check_init(), and M_NAME.

Referenced by I_get_capabilities().

00328 {
00329 //  db_key_t   keys_cmp[] = {"icscf"};
00330     db_key_t   keys_ret[] = {"id_s_cscf","capability"};
00331     db_key_t   key_ord = "id_s_cscf";
00332     db_res_t   * res = 0 ;  
00333     int i,j;
00334     int ccnt=0;
00335     int cnt;
00336 
00337 
00338     if (!icscf_db_check_init(hdl_capabilities))
00339         goto error;
00340 
00341     DBG("DBG:"M_NAME":icscf_db_get_capabilities: fetching list of Capabilities for I-CSCF\n");
00342 
00343 
00344     if (dbf.query(hdl_capabilities, 0, 0, 0, keys_ret, 0, 2, key_ord, & res) < 0) {
00345         LOG(L_ERR, "ERR:"M_NAME":icscf_db_get_capabilities: db_query failed\n");
00346         goto error;
00347     }
00348 
00349     if (res->n == 0) {
00350         DBG("DBG:"M_NAME":icscf_db_get_capabilities: No Capabilites found... not critical...\n");
00351         return 1;
00352     }
00353     else {
00354         for(i=0;i<cap_cnt;i++){
00355             cnt = 0;
00356             for(j=0;j<res->n;j++)
00357                 if (res->rows[j].values[0].val.int_val == (*cap)[i].id_s_cscf)
00358                     cnt++;
00359             (*cap)[i].capabilities = shm_malloc(sizeof(int)*cnt);
00360             if (!(*cap)[i].capabilities) {
00361                 LOG(L_ERR,"ERR:"M_NAME":icscf_db_get_capabilities: Error allocating %d bytes\n",
00362                     sizeof(int)*cnt);
00363                 (*cap)[i].cnt=0;
00364                     goto error;
00365             }           
00366             cnt=0;
00367             for(j=0;j<res->n;j++)
00368                 if (res->rows[j].values[0].val.int_val == (*cap)[i].id_s_cscf) {
00369                     (*cap)[i].capabilities[cnt++]=res->rows[j].values[1].val.int_val;
00370                     ccnt++;
00371                 }
00372             (*cap)[i].cnt=cnt;                  
00373         }
00374             
00375     } 
00376     LOG(L_INFO, "INF:"M_NAME":icscf_db_get_capabilities: Loaded %d capabilities for %d S-CSCFs (%d invalid entries in db)\n",
00377         ccnt,cap_cnt,res->n-ccnt);
00378     dbf.free_result( hdl_capabilities, res);
00379     return 1;
00380     
00381 error:
00382     if (res)
00383         dbf.free_result( hdl_capabilities, res);
00384     return 0;
00385 }


Variable Documentation

db_func_t dbf [static]

db function bindings

Definition at line 63 of file db.c.

Referenced by icscf_db_bind(), icscf_db_close(), icscf_db_get_capabilities(), icscf_db_get_nds(), icscf_db_get_scscf(), and icscf_db_init().

char* icscf_db_url

DB URL.

Definition at line 95 of file mod.c.

Referenced by icscf_db_bind(), icscf_db_check_init(), icscf_mod_child_init(), and icscf_mod_init().

char* icscf_db_nds_table

NDS table in DB.

Definition at line 96 of file mod.c.

Referenced by icscf_db_check_init(), and icscf_mod_child_init().

char* icscf_db_scscf_table

S-CSCF table in db.

Definition at line 97 of file mod.c.

Referenced by icscf_db_check_init(), and icscf_mod_child_init().

char* icscf_db_capabilities_table

S-CSCF capabilities table in db.

Definition at line 98 of file mod.c.

Referenced by icscf_db_check_init(), and icscf_mod_child_init().

db_con_t* hdl_nds = 0 [static]

handle for the NDS table

Definition at line 69 of file db.c.

Referenced by icscf_db_close(), icscf_db_get_nds(), and icscf_db_init().

db_con_t* hdl_scscf = 0 [static]

handle for the S-CSCF table

Definition at line 70 of file db.c.

Referenced by icscf_db_get_scscf(), and icscf_db_init().

db_con_t* hdl_capabilities = 0 [static]

handle for the S-CSCF capabilities table

Definition at line 71 of file db.c.

Referenced by icscf_db_get_capabilities(), and icscf_db_init().


Generated on Thu Oct 23 04:14:41 2008 for Open IMS Core CSCFs by  doxygen 1.5.2