Commit 1a0d5bad authored by johan's avatar johan

Zrtp cache access thread safe

restricted do regular zrtp operations and getPeerStatus

To be completed for lime v1 access
parent 32219076
......@@ -6697,7 +6697,8 @@ void linphone_core_set_zrtp_secrets_file(LinphoneCore *lc, const char* file){
char *bkpFile = reinterpret_cast<char *>(bctbx_malloc(strlen(file)+6));
sprintf(bkpFile,"%s.bkp", file);
char *selfURI = linphone_address_as_string_uri_only(linphone_proxy_config_get_identity_address(proxy));
if ((ret = ms_zrtp_cache_migration((void *)cacheXml, linphone_core_get_zrtp_cache_db(lc), selfURI)) == 0) {
zrtpCacheAccess zrtpCacheInfo = linphone_core_get_zrtp_cache_access(lc);
if ((ret = ms_zrtp_cache_migration((void *)cacheXml, zrtpCacheInfo.db, selfURI)) == 0) {
ms_message("LIME/ZRTP cache migration successfull, obsolete xml file kept as backup in %s", bkpFile);
} else {
ms_error("LIME/ZRTP cache migration failed(returned -%x), start with a fresh cache, old one kept as backup in %s", -ret, bkpFile);
......@@ -6735,6 +6736,18 @@ const char *linphone_core_get_zrtp_secrets_file(LinphoneCore *lc){
return lc->zrtp_secrets_cache;
}
zrtpCacheAccess linphone_core_get_zrtp_cache_access(LinphoneCore *lc){
zrtpCacheAccess ret;
#ifdef SQLITE_STORAGE_ENABLED
ret.db = (void *)lc->zrtp_cache_db;
ret.dbMutex = &(lc->zrtp_cache_db_mutex);
#else /* SQITE_STORAGE_ENABLED */
ret.db = NULL;
ret.dbMutex = NULL
#endif /* SQLITE_STORAGE_ENABLED */
return ret;
}
void *linphone_core_get_zrtp_cache_db(LinphoneCore *lc){
#ifdef SQLITE_STORAGE_ENABLED
return (void *)lc->zrtp_cache_db;
......@@ -6747,7 +6760,7 @@ LinphoneZrtpPeerStatus linphone_core_get_zrtp_status(LinphoneCore *lc, const cha
#ifdef SQLITE_STORAGE_ENABLED
int status = MS_ZRTP_PEER_STATUS_UNKNOWN;
if (lc->zrtp_cache_db) {
status = ms_zrtp_get_peer_status(lc->zrtp_cache_db, peerUri);
status = ms_zrtp_get_peer_status(lc->zrtp_cache_db, peerUri, &(lc->zrtp_cache_db_mutex));
}
switch (status) {
case MS_ZRTP_PEER_STATUS_UNKNOWN:
......@@ -6768,6 +6781,7 @@ static void linphone_core_zrtp_cache_close(LinphoneCore *lc) {
#ifdef SQLITE_STORAGE_ENABLED
if (lc->zrtp_cache_db) {
sqlite3_close(lc->zrtp_cache_db);
bctbx_mutex_destroy(&(lc->zrtp_cache_db_mutex));
lc->zrtp_cache_db = NULL;
}
#endif /* SQLITE_STORAGE_ENABLED */
......@@ -6784,6 +6798,8 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) {
linphone_core_zrtp_cache_close(lc);
bctbx_mutex_init(&(lc->zrtp_cache_db_mutex), NULL);
ret = _linphone_sqlite3_open(fileName, &db);
if (ret != SQLITE_OK) {
errmsg = sqlite3_errmsg(db);
......@@ -6795,7 +6811,7 @@ void linphone_core_zrtp_cache_db_init(LinphoneCore *lc, const char *fileName) {
goto end;
}
ret = ms_zrtp_initCache((void *)db); /* this may perform an update, check return value */
ret = ms_zrtp_initCache((void *)db, &(lc->zrtp_cache_db_mutex)); /* this may perform an update, check return value */
if (ret == MSZRTP_CACHE_SETUP || ret == MSZRTP_CACHE_UPDATE) {
/* After updating schema, database need to be closed/reopenned */
......
......@@ -826,6 +826,7 @@ namespace LinphonePrivate {
#define LINPHONE_CORE_STRUCT_FIELDS \
LINPHONE_CORE_STRUCT_BASE_FIELDS \
sqlite3 *zrtp_cache_db; \
bctbx_mutex_t zrtp_cache_db_mutex; \
sqlite3 *logs_db; \
sqlite3 *friends_db; \
bool_t debug_storage;
......
......@@ -4260,7 +4260,22 @@ LINPHONE_PUBLIC const char *linphone_core_get_zrtp_secrets_file(LinphoneCore *lc
* @return An sqlite3 pointer cast to a void one or NULL if cache is not available(not enabled at compile or access failed)
* @ingroup initializing
*/
LINPHONE_PUBLIC void *linphone_core_get_zrtp_cache_db(LinphoneCore *lc);
LINPHONE_PUBLIC LINPHONE_DEPRECATED void *linphone_core_get_zrtp_cache_db(LinphoneCore *lc);
struct _zrtpCacheAccess{
void *db; /**< points to the zrtp cache sqlite database, is cast into a void * to support cacheless build */
bctbx_mutex_t *dbMutex; /**< the mutex used to prevent conflicting access to the database */
};
typedef struct _zrtpCacheAccess zrtpCacheAccess;
/**
* Get a pointer to a structure holding pointers to access zrtp/lime cache.
* The structure will hold a sqlite db pointer and a bctoolbox mutex pointer
*
* @param[in] lc #LinphoneCore object.
* @return a structure holding db pointer(NULL is cache is not available by built or runtime error) and the mutex associated to it
*/
LINPHONE_PUBLIC zrtpCacheAccess linphone_core_get_zrtp_cache_access(LinphoneCore *lc);
/**
* Get the zrtp sas validation status for a peer uri.
......
......@@ -2426,10 +2426,14 @@ void MediaSessionPrivate::initializeAudioStream () {
char *selfUri = ms_strdup_printf("%s:%s@%s" , linphone_address_get_scheme(selfAddr)
, linphone_address_get_username(selfAddr)
, linphone_address_get_domain(selfAddr));
MSZrtpParams params;
zrtpCacheAccess zrtpCacheInfo = linphone_core_get_zrtp_cache_access(q->getCore()->getCCore());
memset(&params, 0, sizeof(MSZrtpParams));
/* media encryption of current params will be set later when zrtp is activated */
params.zidCacheDB = linphone_core_get_zrtp_cache_db(q->getCore()->getCCore());
params.zidCacheDB = zrtpCacheInfo.db;
params.zidCacheDBMutex = zrtpCacheInfo.dbMutex;
params.peerUri = peerUri;
params.selfUri = selfUri;
/* Get key lifespan from config file, default is 0:forever valid */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment