static void hashT_clean(unsigned int ticks,void *param) { int i; time_t now; ua_pres_t* p= NULL, *q= NULL; if (dbmode==PUA_DB_ONLY) { clean_puadb(update_period, min_expires ); return; } now = time(NULL); for(i= 0;i< HASH_SIZE; i++) { lock_get(&HashT->p_records[i].lock); p= HashT->p_records[i].entity->next; while(p) { print_ua_pres(p); if(p->expires- update_period < now ) { if((p->desired_expires> p->expires + min_expires) || (p->desired_expires== 0 )) { if(update_pua(p)< 0) { LM_ERR("while updating record\n"); lock_release(&HashT->p_records[i].lock); return; } p= p->next; continue; } if(p->expires < now - 10) { q= p->next; LM_DBG("Found expired: uri= %.*s\n", p->pres_uri->len, p->pres_uri->s); delete_htable(p, i); p= q; } else p= p->next; } else p= p->next; } lock_release(&HashT->p_records[i].lock); } }
static void hashT_clean(unsigned int ticks,void *param) { int i; time_t now; ua_pres_t* p= NULL, *q= NULL; now = time(NULL); for(i= 0;i< HASH_SIZE; i++) { lock_get(&HashT->p_records[i].lock); p= HashT->p_records[i].entity->next; while(p) { print_ua_pres(p); LM_DBG("---\n"); if(p->expires -update_period < now ) { if((p->desired_expires> p->expires + 5) || (p->desired_expires== 0 )) { LM_DBG("Desired expires greater than expires -> send a " "refresh PUBLISH desired_expires=%d - expires=%d\n", p->desired_expires, p->expires); if(update_pua(p, i, 0)< 0) { LM_ERR("while updating record\n"); lock_release(&HashT->p_records[i].lock); return; } p= p->next; continue; } LM_DBG("Found expired: uri= %.*s\n", p->pres_uri->len, p->pres_uri->s); if(update_pua(p, i, 1)< 0) { LM_ERR("while updating record\n"); } /* delete it */ q = p->next; delete_htable_safe(p, p->hash_index); p = q; } else p= p->next; } lock_release(&HashT->p_records[i].lock); } }
static int db_restore(void) { ua_pres_t* p= NULL; db_key_t result_cols[19]; db1_res_t *res= NULL; db_row_t *row = NULL; db_val_t *row_vals= NULL; str pres_uri, pres_id; str etag, tuple_id; str watcher_uri, call_id; str to_tag, from_tag, remote_contact; str record_route, contact, extra_headers; int size= 0, i; int n_result_cols= 0; int puri_col,pid_col,expires_col,flag_col,etag_col, desired_expires_col; int watcher_col,callid_col,totag_col,fromtag_col,cseq_col,remote_contact_col; int event_col,contact_col,tuple_col,record_route_col, extra_headers_col; int version_col; if (dbmode==PUA_DB_ONLY) { LM_ERR( "db_restore shouldn't be called in PUA_DB_ONLY mode\n" ); return(-1); } result_cols[puri_col=n_result_cols++] = &str_pres_uri_col; result_cols[pid_col=n_result_cols++] = &str_pres_id_col; result_cols[expires_col=n_result_cols++]= &str_expires_col; result_cols[flag_col=n_result_cols++] = &str_flag_col; result_cols[etag_col=n_result_cols++] = &str_etag_col; result_cols[tuple_col=n_result_cols++] = &str_tuple_id_col; result_cols[watcher_col=n_result_cols++]= &str_watcher_uri_col; result_cols[callid_col=n_result_cols++] = &str_call_id_col; result_cols[totag_col=n_result_cols++] = &str_to_tag_col; result_cols[fromtag_col=n_result_cols++]= &str_from_tag_col; result_cols[cseq_col= n_result_cols++] = &str_cseq_col; result_cols[event_col= n_result_cols++] = &str_event_col; result_cols[record_route_col= n_result_cols++] = &str_record_route_col; result_cols[contact_col= n_result_cols++] = &str_contact_col; result_cols[remote_contact_col= n_result_cols++] = &str_remote_contact_col; result_cols[extra_headers_col= n_result_cols++] = &str_extra_headers_col; result_cols[desired_expires_col= n_result_cols++] = &str_desired_expires_col; result_cols[version_col= n_result_cols++] = &str_version_col; if(!pua_db) { LM_ERR("null database connection\n"); return -1; } if(pua_dbf.use_table(pua_db, &db_table)< 0) { LM_ERR("in use table\n"); return -1; } if(db_fetch_query(&pua_dbf, pua_fetch_rows, pua_db, 0, 0, 0, result_cols, 0, n_result_cols, 0, &res)< 0) { LM_ERR("while querrying table\n"); if(res) { pua_dbf.free_result(pua_db, res); res = NULL; } return -1; } if(res==NULL) return -1; if(res->n<=0) { LM_INFO("the query returned no result\n"); pua_dbf.free_result(pua_db, res); res = NULL; return 0; } do { LM_DBG("found %d db entries\n", res->n); for(i =0 ; i< res->n ; i++) { row = &res->rows[i]; row_vals = ROW_VALUES(row); pres_uri.s= (char*)row_vals[puri_col].val.string_val; pres_uri.len = strlen(pres_uri.s); LM_DBG("pres_uri= %.*s\n", pres_uri.len, pres_uri.s); memset(&etag, 0, sizeof(str)); memset(&tuple_id, 0, sizeof(str)); memset(&watcher_uri, 0, sizeof(str)); memset(&call_id, 0, sizeof(str)); memset(&to_tag, 0, sizeof(str)); memset(&from_tag, 0, sizeof(str)); memset(&record_route, 0, sizeof(str)); memset(&pres_id, 0, sizeof(str)); memset(&contact, 0, sizeof(str)); memset(&remote_contact, 0, sizeof(str)); memset(&extra_headers, 0, sizeof(str)); pres_id.s= (char*)row_vals[pid_col].val.string_val; if(pres_id.s) pres_id.len = strlen(pres_id.s); if(row_vals[etag_col].val.string_val) { etag.s= (char*)row_vals[etag_col].val.string_val; etag.len = strlen(etag.s); tuple_id.s= (char*)row_vals[tuple_col].val.string_val; tuple_id.len = strlen(tuple_id.s); } if(row_vals[watcher_col].val.string_val) { watcher_uri.s= (char*)row_vals[watcher_col].val.string_val; watcher_uri.len = strlen(watcher_uri.s); call_id.s= (char*)row_vals[callid_col].val.string_val; call_id.len = strlen(call_id.s); to_tag.s= (char*)row_vals[totag_col].val.string_val; to_tag.len = strlen(to_tag.s); from_tag.s= (char*)row_vals[fromtag_col].val.string_val; from_tag.len = strlen(from_tag.s); if(row_vals[record_route_col].val.string_val) { record_route.s= (char*)row_vals[record_route_col].val.string_val; record_route.len= strlen(record_route.s); } contact.s= (char*)row_vals[contact_col].val.string_val; contact.len = strlen(contact.s); remote_contact.s= (char*)row_vals[remote_contact_col].val.string_val; remote_contact.len = strlen(remote_contact.s); } extra_headers.s= (char*)row_vals[extra_headers_col].val.string_val; if(extra_headers.s) extra_headers.len= strlen(extra_headers.s); else extra_headers.len= 0; size= sizeof(ua_pres_t)+ sizeof(str)+ (pres_uri.len+ pres_id.len+ tuple_id.len)* sizeof(char); if(extra_headers.s) size+= sizeof(str)+ extra_headers.len* sizeof(char); if(watcher_uri.s) size+= sizeof(str)+ (watcher_uri.len+ call_id.len+ to_tag.len+ from_tag.len+ record_route.len+ contact.len)* sizeof(char); p= (ua_pres_t*)shm_malloc(size); if(p== NULL) { LM_ERR("no more share memmory"); goto error; } memset(p, 0, size); size= sizeof(ua_pres_t); p->pres_uri= (str*)((char*)p+ size); size+= sizeof(str); 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; size+= pres_uri.len; if(pres_id.s) { p->id.s= (char*)p + size; memcpy(p->id.s, pres_id.s, pres_id.len); p->id.len= pres_id.len; size+= pres_id.len; } if(tuple_id.s && tuple_id.len) { p->tuple_id.s= (char*)p + size; memcpy(p->tuple_id.s, tuple_id.s, tuple_id.len); p->tuple_id.len= tuple_id.len; size+= tuple_id.len; } if(watcher_uri.s && watcher_uri.len) { p->watcher_uri= (str*)((char*)p+ size); size+= sizeof(str); p->watcher_uri->s= (char*)p+ size; memcpy(p->watcher_uri->s, watcher_uri.s, watcher_uri.len); p->watcher_uri->len= watcher_uri.len; size+= watcher_uri.len; p->to_tag.s= (char*)p+ size; memcpy(p->to_tag.s, to_tag.s, to_tag.len); p->to_tag.len= to_tag.len; size+= to_tag.len; p->from_tag.s= (char*)p+ size; memcpy(p->from_tag.s, from_tag.s, from_tag.len); p->from_tag.len= from_tag.len; size+= from_tag.len; p->call_id.s= (char*)p + size; memcpy(p->call_id.s, call_id.s, call_id.len); p->call_id.len= call_id.len; size+= call_id.len; if(record_route.s && record_route.len) { p->record_route.s= (char*)p + size; memcpy(p->record_route.s, record_route.s, record_route.len); p->record_route.len= record_route.len; size+= record_route.len; } p->contact.s= (char*)p + size; memcpy(p->contact.s, contact.s, contact.len); p->contact.len= contact.len; size+= contact.len; p->cseq= row_vals[cseq_col].val.int_val; p->remote_contact.s= (char*)shm_malloc(remote_contact.len* sizeof(char)); if(p->remote_contact.s== NULL) { LM_ERR("No more shared memory\n"); goto error; } memcpy(p->remote_contact.s, remote_contact.s, remote_contact.len); p->remote_contact.len= remote_contact.len; p->version= row_vals[version_col].val.int_val; } if(extra_headers.s) { p->extra_headers= (str*)((char*)p+ size); size+= sizeof(str); p->extra_headers->s= (char*)p+ size; memcpy(p->extra_headers->s, extra_headers.s, extra_headers.len); p->extra_headers->len= extra_headers.len; size+= extra_headers.len; } LM_DBG("size= %d\n", size); p->event= row_vals[event_col].val.int_val; p->expires= row_vals[expires_col].val.int_val; p->desired_expires= row_vals[desired_expires_col].val.int_val; p->flag|= row_vals[flag_col].val.int_val; memset(&p->etag, 0, sizeof(str)); if(etag.s && etag.len) { /* alloc separately */ p->etag.s= (char*)shm_malloc(etag.len* sizeof(char)); if(p->etag.s== NULL) { LM_ERR("no more share memory\n"); goto error; } memcpy(p->etag.s, etag.s, etag.len); p->etag.len= etag.len; } print_ua_pres(p); insert_htable(p); } } while((db_fetch_next(&pua_dbf, pua_fetch_rows, pua_db, &res)==1) && (RES_ROW_N(res)>0)); pua_dbf.free_result(pua_db, res); res = NULL; if(pua_dbf.delete(pua_db, 0, 0 , 0, 0) < 0) { LM_ERR("while deleting information from db\n"); goto error; } return 0; error: if(res) pua_dbf.free_result(pua_db, res); if(p) shm_free(p); return -1; }
ua_pres_t* search_htable(ua_pres_t* pres, unsigned int hash_code) { ua_pres_t* p= NULL,* L= NULL; L= HashT->p_records[hash_code].entity; LM_DBG("core_hash= %u\n", hash_code); LM_DBG("Searched:\n"); print_ua_pres(pres); LM_DBG("\n"); for(p= L->next; p; p=p->next) { LM_DBG("Found\n"); print_ua_pres(p); LM_DBG("\n"); if((p->flag & pres->flag) && (p->event & pres->event)) { if((p->pres_uri->len==pres->pres_uri->len) && (strncmp(p->pres_uri->s, pres->pres_uri->s,pres->pres_uri->len)==0)) { if(pres->id.s && pres->id.len) { if(!(pres->id.len== p->id.len && strncmp(p->id.s, pres->id.s,pres->id.len)==0)) continue; } if(pres->watcher_uri) { if(p->watcher_uri->len==pres->watcher_uri->len && (strncmp(p->watcher_uri->s, pres->watcher_uri->s, pres->watcher_uri->len )==0)) { /* if to_uri defined check it also */ if(pres->to_uri.s) { if(pres->to_uri.len == p->to_uri.len && strncmp(pres->to_uri.s, p->to_uri.s, p->to_uri.len) == 0) break; } else break; } } else { if(pres->etag.s) { if(pres->etag.len== p->etag.len && strncmp(p->etag.s, pres->etag.s,pres->etag.len)==0) break; } else { LM_DBG("no etag restriction\n"); break; } } } } } if (p && p->expires < (int)time(NULL) && !(p->expires==0 && p->waiting_reply && p->etag.len==0) ) /* presentities with expires=0, waiting for reply and no etag are newly added * presentities which were not yet confirmed (no reply received for first PUBLISH) * and we should find such records ! -bogdan */ return NULL; LM_DBG("got presentity [%p]\n", p); return p; }
static int db_restore(void) { ua_pres_t* p= NULL; db_key_t result_cols[20]; db_res_t *res= NULL; db_row_t *row = NULL; db_val_t *row_vals= NULL; str pres_uri, pres_id, to_uri; str etag, tuple_id; str watcher_uri, call_id; str to_tag, from_tag, remote_contact; str record_route, contact, extra_headers; int size= 0, i; int n_result_cols= 0; int puri_col,touri_col,pid_col,expires_col,flag_col,etag_col, desired_expires_col; int watcher_col,callid_col,totag_col,fromtag_col,cseq_col,remote_contact_col; int event_col,contact_col,tuple_col,record_route_col, extra_headers_col; int version_col; int no_rows = 10; result_cols[puri_col=n_result_cols++] = &str_pres_uri_col; result_cols[touri_col=n_result_cols++] = &str_to_uri_col; result_cols[pid_col=n_result_cols++] = &str_pres_id_col; result_cols[expires_col=n_result_cols++]= &str_expires_col; result_cols[flag_col=n_result_cols++] = &str_flag_col; result_cols[etag_col=n_result_cols++] = &str_etag_col; result_cols[tuple_col=n_result_cols++] = &str_tuple_id_col; result_cols[watcher_col=n_result_cols++]= &str_watcher_uri_col; result_cols[callid_col=n_result_cols++] = &str_call_id_col; result_cols[totag_col=n_result_cols++] = &str_to_tag_col; result_cols[fromtag_col=n_result_cols++]= &str_from_tag_col; result_cols[cseq_col= n_result_cols++] = &str_cseq_col; result_cols[event_col= n_result_cols++] = &str_event_col; result_cols[record_route_col= n_result_cols++] = &str_record_route_col; result_cols[contact_col= n_result_cols++] = &str_contact_col; result_cols[remote_contact_col= n_result_cols++] = &str_remote_contact_col; result_cols[extra_headers_col= n_result_cols++] = &str_extra_headers_col; result_cols[desired_expires_col= n_result_cols++] = &str_desired_expires_col; result_cols[version_col= n_result_cols++] = &str_version_col; if(!pua_db) { LM_ERR("null database connection\n"); return -1; } if(pua_dbf.use_table(pua_db, &db_table)< 0) { LM_ERR("in use table\n"); return -1; } if (DB_CAPABILITY(pua_dbf, DB_CAP_FETCH)) { if(pua_dbf.query(pua_db,0, 0, 0, result_cols,0, n_result_cols, 0,0)< 0) { LM_ERR("while querying table\n"); return -1; } no_rows = estimate_available_rows( 128+128+8+8+4+32+64+64+128+ 128+64+64+16+64, n_result_cols); if (no_rows==0) no_rows = 10; if(pua_dbf.fetch_result(pua_db, &res, no_rows)<0) { LM_ERR("Error fetching rows\n"); return -1; } } else { if(pua_dbf.query(pua_db,0, 0, 0,result_cols,0,n_result_cols,0,&res)< 0) { LM_ERR("while querrying table\n"); if(res) { pua_dbf.free_result(pua_db, res); res = NULL; } return -1; } } if(res== NULL) return -1; if(res->n<=0) { LM_INFO("the query returned no result\n"); pua_dbf.free_result(pua_db, res); res = NULL; return 0; } LM_DBG("found %d db entries\n", res->n); do { for(i =0 ; i< res->n ; i++) { row = &res->rows[i]; row_vals = ROW_VALUES(row); if(row_vals[expires_col].val.int_val < time(NULL)) continue; pres_uri.s= (char*)row_vals[puri_col].val.string_val; pres_uri.len = strlen(pres_uri.s); LM_DBG("pres_uri= %.*s\n", pres_uri.len, pres_uri.s); memset(&etag, 0, sizeof(str)); memset(&tuple_id, 0, sizeof(str)); memset(&watcher_uri, 0, sizeof(str)); memset(&to_uri, 0, sizeof(str)); memset(&call_id, 0, sizeof(str)); memset(&to_tag, 0, sizeof(str)); memset(&from_tag, 0, sizeof(str)); memset(&record_route, 0, sizeof(str)); memset(&pres_id, 0, sizeof(str)); memset(&contact, 0, sizeof(str)); memset(&remote_contact, 0, sizeof(str)); memset(&extra_headers, 0, sizeof(str)); pres_id.s= (char*)row_vals[pid_col].val.string_val; if(pres_id.s) pres_id.len = strlen(pres_id.s); if(row_vals[etag_col].val.string_val) { etag.s= (char*)row_vals[etag_col].val.string_val; etag.len = strlen(etag.s); tuple_id.s= (char*)row_vals[tuple_col].val.string_val; tuple_id.len = strlen(tuple_id.s); } if(row_vals[watcher_col].val.string_val) { watcher_uri.s= (char*)row_vals[watcher_col].val.string_val; watcher_uri.len = strlen(watcher_uri.s); to_uri.s= (char*)row_vals[touri_col].val.string_val; if(to_uri.s == NULL) to_uri = pres_uri; else to_uri.len = strlen(to_uri.s); LM_DBG("to_uri= %.*s\n", to_uri.len, to_uri.s); call_id.s= (char*)row_vals[callid_col].val.string_val; call_id.len = strlen(call_id.s); to_tag.s= (char*)row_vals[totag_col].val.string_val; to_tag.len = strlen(to_tag.s); from_tag.s= (char*)row_vals[fromtag_col].val.string_val; from_tag.len = strlen(from_tag.s); if(row_vals[record_route_col].val.string_val) { record_route.s= (char*) row_vals[record_route_col].val.string_val; record_route.len= strlen(record_route.s); } contact.s= (char*)row_vals[contact_col].val.string_val; contact.len = strlen(contact.s); remote_contact.s= (char*)row_vals[remote_contact_col].val.string_val; if(remote_contact.s) remote_contact.len = strlen(remote_contact.s); } extra_headers.s= (char*)row_vals[extra_headers_col].val.string_val; if(extra_headers.s) extra_headers.len= strlen(extra_headers.s); else extra_headers.len= 0; size= sizeof(ua_pres_t)+ sizeof(str)+ (pres_uri.len+ pres_id.len+ tuple_id.len)* sizeof(char); if(watcher_uri.s) size+= sizeof(str)+ to_uri.len + watcher_uri.len+ call_id.len+ to_tag.len+ from_tag.len+ record_route.len+ contact.len; p= (ua_pres_t*)shm_malloc(size); if(p== NULL) { LM_ERR("no more shared memmory"); goto error; } memset(p, 0, size); size= sizeof(ua_pres_t); p->pres_uri= (str*)((char*)p+ size); size+= sizeof(str); 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; size+= pres_uri.len; if(pres_id.s) { CONT_COPY(p, p->id, pres_id); } if(watcher_uri.s && watcher_uri.len) { p->watcher_uri= (str*)((char*)p+ size); size+= sizeof(str); p->watcher_uri->s= (char*)p+ size; memcpy(p->watcher_uri->s, watcher_uri.s, watcher_uri.len); p->watcher_uri->len= watcher_uri.len; size+= watcher_uri.len; CONT_COPY(p, p->to_uri, to_uri); CONT_COPY(p, p->to_tag, to_tag); CONT_COPY(p, p->from_tag, from_tag); CONT_COPY(p, p->call_id, call_id); if(record_route.s && record_route.len) { CONT_COPY(p, p->record_route, record_route); } CONT_COPY(p, p->contact, contact); p->cseq= row_vals[cseq_col].val.int_val; p->remote_contact.s= (char*)shm_malloc(remote_contact.len); if(p->remote_contact.s== NULL) { LM_ERR("No more shared memory\n"); goto error; } memcpy(p->remote_contact.s, remote_contact.s, remote_contact.len); p->remote_contact.len= remote_contact.len; p->version= row_vals[version_col].val.int_val; } LM_DBG("size= %d\n", size); p->event= row_vals[event_col].val.int_val; p->expires= row_vals[expires_col].val.int_val; p->desired_expires= row_vals[desired_expires_col].val.int_val; p->flag|= row_vals[flag_col].val.int_val; memset(&p->etag, 0, sizeof(str)); if(etag.s && etag.len) { /* alloc separately */ p->etag.s= (char*)shm_malloc(etag.len); if(p->etag.s== NULL) { LM_ERR("no more share memory\n"); goto error; } memcpy(p->etag.s, etag.s, etag.len); p->etag.len= etag.len; } memset(&p->extra_headers, 0, sizeof(str)); if(extra_headers.s && extra_headers.len) { /* alloc separately */ p->extra_headers.s= (char*)shm_malloc(extra_headers.len); if(p->extra_headers.s== NULL) { LM_ERR("no more share memory\n"); goto error; } memcpy(p->extra_headers.s, extra_headers.s, extra_headers.len); p->extra_headers.len= extra_headers.len; } print_ua_pres(p); insert_htable(p); } /* end for(all rows)*/ if (DB_CAPABILITY(pua_dbf, DB_CAP_FETCH)) { if(pua_dbf.fetch_result(pua_db, &res, no_rows)<0) { LM_ERR( "fetching rows (1)\n"); goto error; } } else { break; } } while(RES_ROW_N(res)>0); pua_dbf.free_result(pua_db, res); res = NULL; if(pua_dbf.delete(pua_db, 0, 0 , 0, 0) < 0) { LM_ERR("while deleting information from db\n"); goto error; } return 0; error: if(res) pua_dbf.free_result(pua_db, res); if(p) { if(p->remote_contact.s) shm_free(p->remote_contact.s); if(p->extra_headers.s) shm_free(p->extra_headers.s); if(p->etag.s) shm_free(p->etag.s); shm_free(p); } return -1; }