int bdb_con_connect(db_con_t* con) { bdb_con_t *bcon; bdb_uri_t *buri; bcon = DB_GET_PAYLOAD(con); buri = DB_GET_PAYLOAD(con->uri); /* Do not reconnect already connected connections */ if (bcon->flags & BDB_CONNECTED) return 0; DBG("bdb: Connecting to %s\n", buri->uri); /* create BDB environment */ bcon->dbp = bdblib_get_db(&buri->path); if(bcon->dbp == NULL) { ERR("bdb: error binding to DB %s\n", buri->uri); return -1; } DBG("bdb: Successfully bound to %s\n", buri->uri); bcon->flags |= BDB_CONNECTED; return 0; }
/* * Initialize database connection */ db_con_t* bdb_init(const char* _sqlurl) { db_con_t* _res; str _s; char bdb_path[BDB_PATH_LEN]; if (!_sqlurl) return NULL; _s.s = (char*)_sqlurl; _s.len = strlen(_sqlurl); if(_s.len <= BDB_ID_LEN || strncmp(_s.s, BDB_ID, BDB_ID_LEN)!=0) { LM_ERR("invalid database URL - should be:" " <%s[/]path/to/directory>\n", BDB_ID); return NULL; } _s.s += BDB_ID_LEN; _s.len -= BDB_ID_LEN; if(_s.s[0]!='/') { if(sizeof(CFG_DIR)+_s.len+2 > BDB_PATH_LEN) { LM_ERR("path to database is too long\n"); return NULL; } strcpy(bdb_path, CFG_DIR); bdb_path[sizeof(CFG_DIR)] = '/'; strncpy(&bdb_path[sizeof(CFG_DIR)+1], _s.s, _s.len); _s.len += sizeof(CFG_DIR); _s.s = bdb_path; } _res = pkg_malloc(sizeof(db_con_t)+sizeof(bdb_con_t)); if (!_res) { LM_ERR("No private memory left\n"); return NULL; } memset(_res, 0, sizeof(db_con_t) + sizeof(bdb_con_t)); _res->tail = (unsigned long)((char*)_res+sizeof(db_con_t)); LM_INFO("using database at: %.*s", _s.len, _s.s); BDB_CON_CONNECTION(_res) = bdblib_get_db(&_s); if (!BDB_CON_CONNECTION(_res)) { LM_ERR("cannot get the link to database\n"); return NULL; } return _res; }