int insert_phtable(str* pres_uri, int event, char* sphere) { unsigned int hash_code; pres_entry_t* p= NULL; int size; hash_code= core_case_hash(pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); p= search_phtable(pres_uri, event, hash_code); if(p) { p->publ_count++; lock_release(&pres_htable[hash_code].lock); return 0; } size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char); p= (pres_entry_t*)shm_malloc(size); if(p== NULL) { lock_release(&pres_htable[hash_code].lock); ERR_MEM(SHARE_MEM); } memset(p, 0, size); size= sizeof(pres_entry_t); p->pres_uri.s= (char*)p+ size; memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len); p->pres_uri.len= pres_uri->len; if(sphere) { p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char)); if(p->sphere== NULL) { lock_release(&pres_htable[hash_code].lock); shm_free(p); ERR_MEM(SHARE_MEM); } strcpy(p->sphere, sphere); } p->event= event; p->publ_count=1; /* link the item in the hash table */ p->next= pres_htable[hash_code].entries->next; pres_htable[hash_code].entries->next= p; lock_release(&pres_htable[hash_code].lock); return 0; error: return -1; }
int delete_phtable(str* pres_uri, int event) { unsigned int hash_code; pres_entry_t* p= NULL, *prev_p= NULL; hash_code= core_hash(pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); p= search_phtable(pres_uri, event, hash_code); if(p== NULL) { LM_DBG("record not found\n"); lock_release(&pres_htable[hash_code].lock); return 0; } p->publ_count--; if(p->publ_count== 0) { /* delete record */ prev_p= pres_htable[hash_code].entries; while(prev_p->next) { if(prev_p->next== p) break; prev_p= prev_p->next; } if(prev_p->next== NULL) { LM_ERR("record not found\n"); lock_release(&pres_htable[hash_code].lock); return -1; } prev_p->next= p->next; if(p->sphere) shm_free(p->sphere); shm_free(p); } lock_release(&pres_htable[hash_code].lock); return 0; }
int update_phtable(presentity_t* presentity, str pres_uri, str body) { char* sphere= NULL; unsigned int hash_code; pres_entry_t* p; int ret= 0; str* xcap_doc= NULL; /* get new sphere */ sphere= extract_sphere(body); if(sphere==NULL) { LM_DBG("no sphere defined in new body\n"); return 0; } /* search for record in hash table */ hash_code= core_hash(&pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); p= search_phtable(&pres_uri, presentity->event->evp->parsed, hash_code); if(p== NULL) { lock_release(&pres_htable[hash_code].lock); goto done; } if(p->sphere) { if(strcmp(p->sphere, sphere)!= 0) { /* new sphere definition */ shm_free(p->sphere); } else { /* no change in sphere definition */ lock_release(&pres_htable[hash_code].lock); pkg_free(sphere); return 0; } } p->sphere= (char*)shm_malloc(strlen(sphere)+ 1); if(p->sphere== NULL) { lock_release(&pres_htable[hash_code].lock); ret= -1; goto done; } strcpy(p->sphere, sphere); lock_release(&pres_htable[hash_code].lock); /* call for watchers status update */ if(presentity->event->get_rules_doc(&presentity->user, &presentity->domain, &xcap_doc)< 0) { LM_ERR("failed to retreive xcap document\n"); ret= -1; goto done; } update_watchers_status(pres_uri, presentity->event, xcap_doc); done: if(xcap_doc) { if(xcap_doc->s) pkg_free(xcap_doc->s); pkg_free(xcap_doc); } if(sphere) pkg_free(sphere); return ret; }
int contains_presence(str* pres_uri) { unsigned int hash_code; db_key_t query_cols[6]; db_val_t query_vals[6]; db_key_t result_cols[6]; db_res_t *result = NULL; int n_result_cols = 0; int n_query_cols = 0; struct sip_uri uri; static str query_str = str_init("received_time"); int ret = -1; hash_code= core_hash(pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); if ( search_phtable(pres_uri, EVENT_PRESENCE, hash_code)!=NULL ) { ret = 1; } lock_release(&pres_htable[hash_code].lock); if ( ret== -1 && fallback2db ) { if(parse_uri(pres_uri->s, pres_uri->len, &uri)< 0) { LM_ERR("failed to parse presentity uri\n"); goto done; } query_cols[n_query_cols] = &str_domain_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val = uri.host; n_query_cols++; query_cols[n_query_cols] = &str_username_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val = uri.user; n_query_cols++; query_cols[n_query_cols] = &str_event_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val.s= "presence"; query_vals[n_query_cols].val.str_val.len= 8; n_query_cols++; result_cols[n_result_cols++] = &str_body_col; result_cols[n_result_cols++] = &str_extra_hdrs_col; pa_dbf.use_table(pa_db, &presentity_table); if (pa_dbf.query (pa_db, query_cols, 0, query_vals, result_cols, n_query_cols, n_result_cols, &query_str, &result)<0) { LM_ERR("failed to query %.*s table\n", presentity_table.len, presentity_table.s); goto done; } if(result== NULL) goto done; if (result->n<=0 ) { LM_DBG("no published record found in database\n"); goto done; } ret = 1; } done: if(result) pa_dbf.free_result(pa_db, result); return ret; }
char* get_sphere(str* pres_uri) { // static db_ps_t my_ps = NULL; unsigned int hash_code; char* sphere= NULL; pres_entry_t* p; db_key_t query_cols[6]; db_val_t query_vals[6]; db_key_t result_cols[6]; db_res_t *result = NULL; db_row_t *row= NULL ; db_val_t *row_vals; int n_result_cols = 0; int n_query_cols = 0; struct sip_uri uri; str body; static str query_str = str_init("received_time"); if(!sphere_enable) return NULL; /* search in hash table*/ hash_code= core_hash(pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); p= search_phtable(pres_uri, EVENT_PRESENCE, hash_code); if(p) { if(p->sphere) { sphere= (char*)pkg_malloc(strlen(p->sphere)); if(sphere== NULL) { lock_release(&pres_htable[hash_code].lock); ERR_MEM(PKG_MEM_STR); } strcpy(sphere, p->sphere); } lock_release(&pres_htable[hash_code].lock); return sphere; } lock_release(&pres_htable[hash_code].lock); /* if record not found and fallback2db query database*/ if(!fallback2db) { return NULL; } if(parse_uri(pres_uri->s, pres_uri->len, &uri)< 0) { LM_ERR("failed to parse presentity uri\n"); goto error; } query_cols[n_query_cols] = &str_domain_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val = uri.host; n_query_cols++; query_cols[n_query_cols] = &str_username_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val = uri.user; n_query_cols++; query_cols[n_query_cols] = &str_event_col; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val.s= "presence"; query_vals[n_query_cols].val.str_val.len= 8; n_query_cols++; result_cols[n_result_cols++] = &str_body_col; result_cols[n_result_cols++] = &str_extra_hdrs_col; if (pa_dbf.use_table(pa_db, &presentity_table) < 0) { LM_ERR("in use_table\n"); return NULL; } // CON_PS_REFERENCE(pa_db) = &my_ps; if (pa_dbf.query (pa_db, query_cols, 0, query_vals, result_cols, n_query_cols, n_result_cols, &query_str , &result) < 0) { LM_ERR("failed to query %.*s table\n", presentity_table.len, presentity_table.s); if(result) pa_dbf.free_result(pa_db, result); return NULL; } if(result== NULL) return NULL; if (result->n<=0 ) { LM_DBG("no published record found in database\n"); pa_dbf.free_result(pa_db, result); return NULL; } row = &result->rows[result->n-1]; row_vals = ROW_VALUES(row); if(row_vals[0].val.string_val== NULL) { LM_ERR("NULL notify body record\n"); goto error; } body.s= (char*)row_vals[0].val.string_val; body.len= strlen(body.s); if(body.len== 0) { LM_ERR("Empty notify body record\n"); goto error; } sphere= extract_sphere(body); pa_dbf.free_result(pa_db, result); return sphere; error: if(result) pa_dbf.free_result(pa_db, result); return NULL; }