static int cfg_lock_helper(str *lkey, int mode) { unsigned int pos; if(_cfg_lock_set==NULL) { LM_ERR("lock set not initialized (attempt to do op: %d on: %.*s) -" " see param lock_set_size\n", mode, lkey->len, lkey->s); return -1; } pos = core_case_hash(lkey, 0, _cfg_lock_size); LM_DBG("cfg_lock mode %d on %u (%.*s)\n", mode, pos, lkey->len, lkey->s); if(mode==0) { /* Lock */ lock_set_get(_cfg_lock_set, pos); } else if (mode == 1) { /* Unlock */ lock_set_release(_cfg_lock_set, pos); } else { int res; /* Trylock */ res = lock_set_try(_cfg_lock_set, pos); if (res != 0) { LM_DBG("Failed to trylock \n"); /* Failed to lock */ return -1; } LM_DBG("Succeeded with trylock \n"); /* Succeeded in locking */ return 1; } return 1; }
static int cfg_lock_helper(str *lkey, int mode) { unsigned int pos; pos = core_case_hash(lkey, 0, _cfg_lock_size); LM_DBG("cfg_lock mode %d on %u\n", mode, pos); if(mode==0) { /* Lock */ lock_set_get(_cfg_lock_set, pos); } else if (mode == 1) { /* Unlock */ lock_set_release(_cfg_lock_set, pos); } else { int res; /* Trylock */ res = lock_set_try(_cfg_lock_set, pos); if (res != 0) { LM_DBG("Failed to trylock \n"); /* Failed to lock */ return -1; } LM_DBG("Succeeded with trylock \n"); /* Succeeded in locking */ return 1; } return 1; }
int sql_init_con(str *name, str *url) { sql_con_t *sc; unsigned int conid; *_sql_empty_buf = '\0'; conid = core_case_hash(name, 0, 0); sc = _sql_con_root; while(sc) { if(conid==sc->conid && sc->name.len==name->len && strncmp(sc->name.s, name->s, name->len)==0) { LM_ERR("duplicate connection name\n"); return -1; } sc = sc->next; } sc = (sql_con_t*)pkg_malloc(sizeof(sql_con_t)); if(sc==NULL) { LM_ERR("no pkg memory\n"); return -1; } memset(sc, 0, sizeof(sql_con_t)); sc->conid = conid; sc->name = *name; sc->db_url = *url; sc->next = _sql_con_root; _sql_con_root = sc; return 0; }
/** * search memeber */ imc_member_p imc_get_member(imc_room_p room, str* user, str* domain) { imc_member_p imp = NULL; unsigned int hashid; if(room==NULL || user == NULL || user->s==NULL || user->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return NULL; } hashid = core_case_hash(user, domain, 0); imp = room->members; while(imp) { if(imp->hashid==hashid && imp->user.len==user->len && imp->domain.len==domain->len && !strncasecmp(imp->user.s, user->s, user->len) && !strncasecmp(imp->domain.s, domain->s, domain->len)) { LM_DBG("found member\n"); return imp; } imp = imp->next; } return NULL; }
/*! Init connection structure and place it in structure */ curl_con_t *curl_init_con(str *name) { curl_con_t *cc; unsigned int conid; conid = core_case_hash(name, 0, 0); LM_DBG("curl_init_con curlcon: [%.*s] ID %u\n", name->len, name->s, conid); cc = _curl_con_root; while(cc) { if(conid==cc->conid && cc->name.len == name->len && strncmp(cc->name.s, name->s, name->len)==0) { LM_ERR("duplicate Curl connection name\n"); return NULL; } cc = cc->next; } cc = (curl_con_t*) pkg_malloc(sizeof(curl_con_t)); if(cc == NULL) { LM_ERR("no pkg memory\n"); return NULL; } memset(cc, 0, sizeof(curl_con_t)); cc->next = _curl_con_root; cc->conid = conid; _curl_con_root = cc; cc->name = *name; LM_INFO("CURL: Added connection [%.*s]\n", name->len, name->s); return cc; }
sql_result_t* sql_get_result(str *name) { sql_result_t *sr; unsigned int resid; resid = core_case_hash(name, 0, 0); sr = _sql_result_root; while(sr) { if(sr->resid==resid && sr->name.len==name->len && strncmp(sr->name.s, name->s, name->len)==0) return sr; sr = sr->next; } sr = (sql_result_t*)pkg_malloc(sizeof(sql_result_t)); if(sr==NULL) { LM_ERR("no pkg memory\n"); return NULL; } memset(sr, 0, sizeof(sql_result_t)); sr->name = *name; sr->resid = resid; sr->next = _sql_result_root; _sql_result_root = sr; return sr; }
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; }
/** * add member */ imc_member_p imc_add_member(imc_room_p room, str* user, str* domain, int flags) { imc_member_p imp = NULL; int size; if(room==NULL || user == NULL || user->s==NULL || user->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return NULL; } /* struct size + "sip:" + user name len + "@" + domain len + '\0' */ size = sizeof(imc_member_t) + (user->len+domain->len+6)*sizeof(char); imp = (imc_member_p)shm_malloc(size); if(imp== NULL) { LM_ERR("out of shm memory\n"); return NULL; } memset(imp, 0, size); imp->uri.len = 4 /*sip:*/ + user->len + 1 /*@*/ + domain->len; imp->uri.s = (char*)(((char*)imp)+sizeof(imc_member_t)); memcpy(imp->uri.s, "sip:", 4); memcpy(imp->uri.s+4, user->s, user->len); imp->uri.s[4+user->len] = '@'; memcpy(imp->uri.s+5+user->len, domain->s, domain->len); imp->uri.s[imp->uri.len] = '\0'; LM_DBG("[uri]= %.*s\n", imp->uri.len, imp->uri.s); imp->user.len = user->len; imp->user.s = imp->uri.s+4; LM_DBG("[user]= %.*s\n", imp->user.len, imp->user.s); imp->domain.len = domain->len; imp->domain.s = imp->uri.s+5+user->len; imp->flags = flags; imp->hashid = core_case_hash(&imp->user, &imp->domain, 0); room->nr_of_members++; if(room->members==NULL) room->members = imp; else { imp->next = room->members->next; if((room->members)->next!=NULL) ((room->members)->next)->prev = imp; imp->prev = room->members; room->members->next=imp; } return imp; }
/** * add room */ imc_room_p imc_add_room(str* name, str* domain, int flags) { imc_room_p irp = NULL; int size; int hidx; if(name == NULL || name->s==NULL || name->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return NULL; } /* struct size + "sip:" + name len + "@" + domain len + '\0' */ size = sizeof(imc_room_t) + (name->len+domain->len+6)*sizeof(char); irp = (imc_room_p)shm_malloc(size); if(irp==NULL) { LM_ERR("no more shm memory left\n"); return NULL; } memset(irp, 0, size); irp->uri.len = 4 /*sip:*/ + name->len + 1 /*@*/ + domain->len; irp->uri.s = (char*)(((char*)irp)+sizeof(imc_room_t)); memcpy(irp->uri.s, "sip:", 4); memcpy(irp->uri.s+4, name->s, name->len); irp->uri.s[4+name->len] = '@'; memcpy(irp->uri.s+5+name->len, domain->s, domain->len); irp->uri.s[irp->uri.len] = '\0'; irp->name.len = name->len; irp->name.s = irp->uri.s+4; irp->domain.len = domain->len; irp->domain.s = irp->uri.s+5+name->len; irp->flags = flags; irp->hashid = core_case_hash(&irp->name, &irp->domain, 0); hidx = imc_get_hentry(irp->hashid, imc_hash_size); lock_get(&_imc_htable[hidx].lock); if(_imc_htable[hidx].rooms!=NULL) { irp->next = _imc_htable[hidx].rooms; _imc_htable[hidx].rooms->prev = irp; _imc_htable[hidx].rooms = irp; } else { _imc_htable[hidx].rooms = irp; } return irp; }
/** * delete room */ int imc_del_room(str* name, str* domain) { imc_room_p irp = NULL; imc_member_p imp=NULL, imp_temp=NULL; unsigned int hashid; int hidx; if(name == NULL || name->s==NULL || name->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return -1; } hashid = core_case_hash(name, domain, 0); hidx = imc_get_hentry(hashid, imc_hash_size); lock_get(&_imc_htable[hidx].lock); irp = _imc_htable[hidx].rooms; while(irp) { if(irp->hashid==hashid && irp->name.len==name->len && irp->domain.len==domain->len && !strncasecmp(irp->name.s, name->s, name->len) && !strncasecmp(irp->domain.s, domain->s, domain->len)) { if(irp->prev==NULL) _imc_htable[hidx].rooms = irp->next; else irp->prev->next = irp->next; if(irp->next!=NULL) irp->next->prev = irp->prev; /* delete members */ imp = irp->members; while(imp){ imp_temp = imp->next; shm_free(imp); imp = imp_temp; } shm_free(irp); goto done; } irp = irp->next; } done: lock_release(&_imc_htable[hidx].lock); return 0; }
sql_con_t* sql_get_connection(str *name) { sql_con_t *sc; unsigned int conid; conid = core_case_hash(name, 0, 0); sc = _sql_con_root; while(sc) { if(conid==sc->conid && sc->name.len==name->len && strncmp(sc->name.s, name->s, name->len)==0) return sc; sc = sc->next; } return NULL; }
int delete_phtable(str* pres_uri, int event) { unsigned int hash_code; pres_entry_t* p= NULL, *prev_p= NULL; 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== 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; }
dbcl_cls_t *dbcl_get_cluster(str *name) { dbcl_cls_t *sc; unsigned int clsid; clsid = core_case_hash(name, 0, 0); sc = _dbcl_cls_root; while(sc) { if(clsid==sc->clsid && sc->name.len==name->len && strncmp(sc->name.s, name->s, name->len)==0) { LM_DBG("cluster found [%.*s]\n", name->len, name->s); return sc; } sc = sc->next; } return NULL; }
/*! Find CURL connection by name */ curl_con_t* curl_get_connection(str *name) { curl_con_t *cc; unsigned int conid; conid = core_case_hash(name, 0, 0); LM_DBG("curl_get_connection looking for curlcon: [%.*s] ID %u\n", name->len, name->s, conid); cc = _curl_con_root; while(cc) { LM_DBG("---> curl_get_connection comparing with curlcon: [%.*s]\n", cc->name.len, cc->name.s); LM_DBG("---> curl_get_connection comparing conid %u with cc->conid %u \n", conid, cc->conid); if(conid==cc->conid && cc->name.len==name->len && strncmp(cc->name.s, name->s, name->len)==0) { return cc; } cc = cc->next; } LM_DBG("curl_get_connection no success in looking for curlcon: [%.*s]\n", name->len, name->s); return NULL; }
int dbcl_init_con(str *name, str *url) { dbcl_con_t *sc; unsigned int conid; conid = core_case_hash(name, 0, 0); sc = _dbcl_con_root; while(sc) { if(conid==sc->conid && sc->name.len==name->len && strncmp(sc->name.s, name->s, name->len)==0) { LM_ERR("duplicate connection name\n"); return -1; } sc = sc->next; } sc = (dbcl_con_t*)pkg_malloc(sizeof(dbcl_con_t)); if(sc==NULL) { LM_ERR("no pkg memory\n"); return -1; } memset(sc, 0, sizeof(dbcl_con_t)); sc->conid = conid; sc->name = *name; sc->db_url = *url; sc->sinfo = (dbcl_shared_t*)shm_malloc(sizeof(dbcl_shared_t)); if(sc->sinfo==NULL) { LM_ERR("no shm memory\n"); return -1; } memset(sc->sinfo, 0, sizeof(dbcl_shared_t)); sc->next = _dbcl_con_root; _dbcl_con_root = sc; return 0; }
int dbcl_init_cls(str *name, str *cons) { dbcl_cls_t *sc; unsigned int clsid; clsid = core_case_hash(name, 0, 0); sc = _dbcl_cls_root; while(sc) { if(clsid==sc->clsid && sc->name.len==name->len && strncmp(sc->name.s, name->s, name->len)==0) { LM_ERR("duplicate cluster name\n"); return -1; } sc = sc->next; } sc = (dbcl_cls_t*)pkg_malloc(sizeof(dbcl_cls_t)); if(sc==NULL) { LM_ERR("no pkg memory\n"); return -1; } memset(sc, 0, sizeof(dbcl_cls_t)); sc->clsid = clsid; sc->name = *name; /* parse cls con list */ if(dbcl_cls_set_connections(sc, cons)<0) { LM_ERR("unable to add connections to cluster definition\n"); pkg_free(sc); return -1; } sc->next = _dbcl_cls_root; _dbcl_cls_root = sc; return 0; }
/** * search room */ imc_room_p imc_get_room(str* name, str* domain) { imc_room_p irp = NULL; unsigned int hashid; int hidx; if(name == NULL || name->s==NULL || name->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return NULL; } hashid = core_case_hash(name, domain, 0); hidx = imc_get_hentry(hashid, imc_hash_size); lock_get(&_imc_htable[hidx].lock); irp = _imc_htable[hidx].rooms; while(irp) { if(irp->hashid==hashid && irp->name.len==name->len && irp->domain.len==domain->len && !strncasecmp(irp->name.s, name->s, name->len) && !strncasecmp(irp->domain.s, domain->s, domain->len)) { return irp; } irp = irp->next; } /* no room */ lock_release(&_imc_htable[hidx].lock); return NULL; }
/** * delete member */ int imc_del_member(imc_room_p room, str* user, str* domain) { imc_member_p imp = NULL; unsigned int hashid; if(room==NULL || user == NULL || user->s==NULL || user->len<=0 || domain == NULL || domain->s==NULL || domain->len<=0) { LM_ERR("invalid parameters\n"); return -1; } hashid = core_case_hash(user, domain, 0); imp = room->members; while(imp) { if(imp->hashid==hashid && imp->user.len==user->len && imp->domain.len==domain->len && !strncasecmp(imp->user.s, user->s, user->len) && !strncasecmp(imp->domain.s, domain->s, domain->len)) { if(imp->prev==NULL) room->members = imp->next; else imp->prev->next = imp->next; if(imp->next!=NULL) imp->next->prev = imp->prev; shm_free(imp); room->nr_of_members--; return 0; } imp = imp->next; } return 0; }
int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc) { subs_t subs; db_key_t query_cols[6], result_cols[5]; db_val_t query_vals[6]; int n_result_cols= 0, n_query_cols = 0; db1_res_t* result= NULL; db_row_t *row; db_val_t *row_vals ; int i; str w_user, w_domain, reason= {0, 0}; unsigned int status; int status_col, w_user_col, w_domain_col, reason_col; subs_t* subs_array= NULL,* s; unsigned int hash_code; int err_ret= -1; int n= 0; typedef struct ws { int status; str reason; str w_user; str w_domain; }ws_t; ws_t* ws_list= NULL; LM_DBG("start\n"); if(ev->content_type.s== NULL) { ev= contains_event(&ev->name, NULL); if(ev== NULL) { LM_ERR("wrong event parameter\n"); return 0; } } memset(&subs, 0, sizeof(subs_t)); subs.pres_uri= pres_uri; subs.event= ev; subs.auth_rules_doc= rules_doc; /* update in watchers_table */ query_cols[n_query_cols]= &str_presentity_uri_col; query_vals[n_query_cols].nul= 0; query_vals[n_query_cols].type= DB1_STR; query_vals[n_query_cols].val.str_val= pres_uri; n_query_cols++; query_cols[n_query_cols]= &str_event_col; query_vals[n_query_cols].nul= 0; query_vals[n_query_cols].type= DB1_STR; query_vals[n_query_cols].val.str_val= ev->name; n_query_cols++; result_cols[status_col= n_result_cols++]= &str_status_col; result_cols[reason_col= n_result_cols++]= &str_reason_col; result_cols[w_user_col= n_result_cols++]= &str_watcher_username_col; result_cols[w_domain_col= n_result_cols++]= &str_watcher_domain_col; if (pa_dbf.use_table(pa_db, &watchers_table) < 0) { LM_ERR( "in use_table\n"); goto done; } if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols,n_query_cols, n_result_cols, 0, &result)< 0) { LM_ERR( "in sql query\n"); goto done; } if(result== NULL) return 0; if(result->n<= 0) { err_ret= 0; goto done; } LM_DBG("found %d record-uri in watchers_table\n", result->n); hash_code= core_case_hash(&pres_uri, &ev->name, shtable_size); subs.db_flag= hash_code; /*must do a copy as sphere_check requires database queries */ if(sphere_enable) { n= result->n; ws_list= (ws_t*)pkg_malloc(n * sizeof(ws_t)); if(ws_list== NULL) { LM_ERR("No more private memory\n"); goto done; } memset(ws_list, 0, n * sizeof(ws_t)); for(i= 0; i< result->n ; i++) { row= &result->rows[i]; row_vals = ROW_VALUES(row); status= row_vals[status_col].val.int_val; reason.s= (char*)row_vals[reason_col].val.string_val; reason.len= reason.s?strlen(reason.s):0; w_user.s= (char*)row_vals[w_user_col].val.string_val; w_user.len= strlen(w_user.s); w_domain.s= (char*)row_vals[w_domain_col].val.string_val; w_domain.len= strlen(w_domain.s); if(reason.len) { ws_list[i].reason.s = (char*)pkg_malloc(reason.len* sizeof(char)); if(ws_list[i].reason.s== NULL) { LM_ERR("No more private memory\n"); goto done; } memcpy(ws_list[i].reason.s, reason.s, reason.len); ws_list[i].reason.len= reason.len; } else ws_list[i].reason.s= NULL; ws_list[i].w_user.s = (char*)pkg_malloc(w_user.len* sizeof(char)); if(ws_list[i].w_user.s== NULL) { LM_ERR("No more private memory\n"); goto done; } memcpy(ws_list[i].w_user.s, w_user.s, w_user.len); ws_list[i].w_user.len= w_user.len; ws_list[i].w_domain.s = (char*)pkg_malloc(w_domain.len* sizeof(char)); if(ws_list[i].w_domain.s== NULL) { LM_ERR("No more private memory\n"); goto done; } memcpy(ws_list[i].w_domain.s, w_domain.s, w_domain.len); ws_list[i].w_domain.len= w_domain.len; ws_list[i].status= status; } pa_dbf.free_result(pa_db, result); result= NULL; for(i=0; i< n; i++) { subs.watcher_user = ws_list[i].w_user; subs.watcher_domain = ws_list[i].w_domain; subs.status = ws_list[i].status; memset(&subs.reason, 0, sizeof(str)); if( pres_update_status(subs, reason, query_cols, query_vals, n_query_cols, &subs_array)< 0) { LM_ERR("failed to update watcher status\n"); goto done; } } for(i=0; i< n; i++) { pkg_free(ws_list[i].w_user.s); pkg_free(ws_list[i].w_domain.s); if(ws_list[i].reason.s) pkg_free(ws_list[i].reason.s); } ws_list= NULL; goto send_notify; } for(i = 0; i< result->n; i++) { row= &result->rows[i]; row_vals = ROW_VALUES(row); status= row_vals[status_col].val.int_val; reason.s= (char*)row_vals[reason_col].val.string_val; reason.len= reason.s?strlen(reason.s):0; w_user.s= (char*)row_vals[w_user_col].val.string_val; w_user.len= strlen(w_user.s); w_domain.s= (char*)row_vals[w_domain_col].val.string_val; w_domain.len= strlen(w_domain.s); subs.watcher_user= w_user; subs.watcher_domain= w_domain; subs.status= status; memset(&subs.reason, 0, sizeof(str)); if( pres_update_status(subs,reason, query_cols, query_vals, n_query_cols, &subs_array)< 0) { LM_ERR("failed to update watcher status\n"); goto done; } } pa_dbf.free_result(pa_db, result); result= NULL; send_notify: if (pres_notifier_processes == 0) { s= subs_array; while(s) { if(notify(s, NULL, NULL, 0)< 0) { LM_ERR( "sending Notify request\n"); goto done; } /* delete from database also */ if(s->status== TERMINATED_STATUS) { if(pres_db_delete_status(s)<0) { LM_ERR("failed to delete terminated " "dialog from database\n"); goto done; } } s= s->next; } } free_subs_list(subs_array, PKG_MEM_TYPE, 0); return 0; done: if(result) pa_dbf.free_result(pa_db, result); free_subs_list(subs_array, PKG_MEM_TYPE, 0); if(ws_list) { for(i= 0; i< n; i++) { if(ws_list[i].w_user.s) pkg_free(ws_list[i].w_user.s); else break; if(ws_list[i].w_domain.s) pkg_free(ws_list[i].w_domain.s); if(ws_list[i].reason.s) pkg_free(ws_list[i].reason.s); } } return err_ret; }
static int update_pw_dialogs_dbonlymode(subs_t* subs, subs_t** subs_array) { db_key_t query_cols[5], db_cols[3]; db_val_t query_vals[5], db_vals[3]; db_key_t result_cols[24]; int n_query_cols=0, n_result_cols=0, n_update_cols=0; int event_col, pres_uri_col, watcher_user_col, watcher_domain_col; int r_pres_uri_col,r_to_user_col,r_to_domain_col; int r_from_user_col,r_from_domain_col,r_callid_col; int r_to_tag_col,r_from_tag_col,r_sockinfo_col; int r_event_id_col,r_local_contact_col,r_contact_col; int r_record_route_col, r_reason_col; int r_event_col, r_local_cseq_col, r_remote_cseq_col; int r_status_col, r_version_col; int r_expires_col, r_watcher_user_col, r_watcher_domain_col; db1_res_t *result= NULL; db_val_t *row_vals; db_row_t *rows; int nr_rows, loop; subs_t s, *cs; str ev_sname; if(pa_db == NULL) { LM_ERR("null database connection\n"); return(-1); } if (pa_dbf.use_table(pa_db, &active_watchers_table) < 0) { LM_ERR("use table failed\n"); return(-1); } query_cols[event_col=n_query_cols]= &str_event_col; query_vals[event_col].nul= 0; query_vals[event_col].type= DB1_STR; query_vals[event_col].val.str_val= subs->event->name ; n_query_cols++; query_cols[pres_uri_col=n_query_cols]= &str_presentity_uri_col; query_vals[pres_uri_col].nul= 0; query_vals[pres_uri_col].type= DB1_STR; query_vals[pres_uri_col].val.str_val= subs->pres_uri; n_query_cols++; query_cols[watcher_user_col=n_query_cols]= &str_watcher_username_col; query_vals[watcher_user_col].nul= 0; query_vals[watcher_user_col].type= DB1_STR; query_vals[watcher_user_col].val.str_val= subs->watcher_user; n_query_cols++; query_cols[watcher_domain_col=n_query_cols]= &str_watcher_domain_col; query_vals[watcher_domain_col].nul= 0; query_vals[watcher_domain_col].type= DB1_STR; query_vals[watcher_domain_col].val.str_val= subs->watcher_domain; n_query_cols++; result_cols[r_to_user_col=n_result_cols++] = &str_to_user_col; result_cols[r_to_domain_col=n_result_cols++] = &str_to_domain_col; result_cols[r_from_user_col=n_result_cols++] = &str_from_user_col; result_cols[r_from_domain_col=n_result_cols++] = &str_from_domain_col; result_cols[r_watcher_user_col=n_result_cols++] = &str_watcher_username_col; result_cols[r_watcher_domain_col=n_result_cols++] = &str_watcher_domain_col; result_cols[r_callid_col=n_result_cols++] = &str_callid_col; result_cols[r_to_tag_col=n_result_cols++] = &str_to_tag_col; result_cols[r_from_tag_col=n_result_cols++] = &str_from_tag_col; result_cols[r_sockinfo_col=n_result_cols++] = &str_socket_info_col; result_cols[r_event_id_col=n_result_cols++] = &str_event_id_col; result_cols[r_local_contact_col=n_result_cols++] = &str_local_contact_col; result_cols[r_record_route_col=n_result_cols++] = &str_record_route_col; result_cols[r_reason_col=n_result_cols++] = &str_reason_col; result_cols[r_local_cseq_col=n_result_cols++] = &str_local_cseq_col; result_cols[r_version_col=n_result_cols++] = &str_version_col; result_cols[r_expires_col=n_result_cols++] = &str_expires_col; result_cols[r_event_col=n_result_cols++] = &str_event_col; result_cols[r_pres_uri_col=n_result_cols++] = &str_presentity_uri_col; result_cols[r_contact_col=n_result_cols++] = &str_contact_col; /* these ones are unused for some reason !!! */ result_cols[r_remote_cseq_col=n_result_cols++] = &str_remote_cseq_col; result_cols[r_status_col=n_result_cols++] = &str_status_col; /*********************************************/ if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols, n_query_cols, n_result_cols, 0, &result )< 0) { LM_ERR("Can't query db\n"); if(result) pa_dbf.free_result(pa_db, result); return(-1); } if(result == NULL) return(-1); nr_rows = RES_ROW_N(result); LM_DBG("found %d matching dialogs\n", nr_rows); if (nr_rows <= 0) { pa_dbf.free_result(pa_db, result); return 0; } rows = RES_ROWS(result); /* get the results and fill in return data structure */ for (loop=0; loop <nr_rows; loop++) { row_vals = ROW_VALUES(&rows[loop]); memset(&s, 0, sizeof(subs_t)); s.status= subs->status; s.reason.s= subs->reason.s; s.reason.len= s.reason.s?strlen(s.reason.s):0; //>>>>>>>>>> s.pres_uri.s= (char*)row_vals[r_pres_uri_col].val.string_val; s.pres_uri.len= s.pres_uri.s?strlen(s.pres_uri.s):0; s.to_user.s= (char*)row_vals[r_to_user_col].val.string_val; s.to_user.len= s.to_user.s?strlen(s.to_user.s):0; s.to_domain.s= (char*)row_vals[r_to_domain_col].val.string_val; s.to_domain.len= s.to_domain.s?strlen(s.to_domain.s):0; s.from_user.s= (char*)row_vals[r_from_user_col].val.string_val; s.from_user.len= s.from_user.s?strlen(s.from_user.s):0; s.from_domain.s= (char*)row_vals[r_from_domain_col].val.string_val; s.from_domain.len= s.from_domain.s?strlen(s.from_domain.s):0; s.watcher_user.s= (char*)row_vals[r_watcher_user_col].val.string_val; s.watcher_user.len= s.watcher_user.s?strlen(s.watcher_user.s):0; s.watcher_domain.s= (char*)row_vals[r_watcher_domain_col].val.string_val; s.watcher_domain.len= s.watcher_domain.s?strlen(s.watcher_domain.s):0; s.event_id.s=(char*)row_vals[r_event_id_col].val.string_val; s.event_id.len= (s.event_id.s)?strlen(s.event_id.s):0; s.to_tag.s= (char*)row_vals[r_to_tag_col].val.string_val; s.to_tag.len= s.to_tag.s?strlen(s.to_tag.s):0; s.from_tag.s= (char*)row_vals[r_from_tag_col].val.string_val; s.from_tag.len= s.from_tag.s?strlen(s.from_tag.s):0; s.callid.s= (char*)row_vals[r_callid_col].val.string_val; s.callid.len= s.callid.s?strlen(s.callid.s):0; s.record_route.s= (char*)row_vals[r_record_route_col].val.string_val; s.record_route.len= (s.record_route.s)?strlen(s.record_route.s):0; s.contact.s= (char*)row_vals[r_contact_col].val.string_val; s.contact.len= s.contact.s?strlen(s.contact.s):0; s.sockinfo_str.s = (char*)row_vals[r_sockinfo_col].val.string_val; s.sockinfo_str.len = s.sockinfo_str.s?strlen(s.sockinfo_str.s):0; s.local_contact.s = (char*)row_vals[r_local_contact_col].val.string_val; s.local_contact.len = s.local_contact.s?strlen(s.local_contact.s):0; ev_sname.s= (char*)row_vals[r_event_col].val.string_val; ev_sname.len= ev_sname.s?strlen(ev_sname.s):0; s.event = contains_event(&ev_sname, NULL); if(s.event == NULL) { LM_ERR("event not found and set to NULL\n"); } s.local_cseq = row_vals[r_local_cseq_col].val.int_val; s.expires = row_vals[r_expires_col].val.int_val; if( s.expires > (int)time(NULL) + expires_offset) s.expires -= (int)time(NULL); else s.expires = 0; s.version = row_vals[r_version_col].val.int_val; cs = mem_copy_subs(&s, PKG_MEM_TYPE); if (cs == NULL) { LM_ERR("while copying subs_t structure\n"); /* tidy up and return */ pa_dbf.free_result(pa_db, result); return(-1); } cs->local_cseq++; cs->next= (*subs_array); (*subs_array)= cs; printf_subs(cs); } pa_dbf.free_result(pa_db, result); if (pres_notifier_processes == 0 && subs->status == TERMINATED_STATUS) { /* delete the records */ if(pa_dbf.delete(pa_db, query_cols, 0, query_vals, n_query_cols)< 0) { LM_ERR("sql delete failed\n"); return(-1); } return(0); } /* otherwise we update the records */ db_cols[n_update_cols] = &str_status_col; db_vals[n_update_cols].type = DB1_INT; db_vals[n_update_cols].nul = 0; db_vals[n_update_cols].val.int_val = subs->status; n_update_cols++; db_cols[n_update_cols] = &str_reason_col; db_vals[n_update_cols].type = DB1_STR; db_vals[n_update_cols].nul = 0; db_vals[n_update_cols].val.str_val= subs->reason; n_update_cols++; db_cols[n_update_cols] = &str_updated_col; db_vals[n_update_cols].type = DB1_INT; db_vals[n_update_cols].nul = 0; if (subs->callid.len == 0 || subs->from_tag.len == 0) { db_vals[n_update_cols].val.int_val = (int) ((rand() / (RAND_MAX + 1.0)) * (pres_waitn_time * pres_notifier_poll_rate * pres_notifier_processes)); } else { db_vals[n_update_cols].val.int_val = core_case_hash(&subs->callid, &subs->from_tag, 0) % (pres_waitn_time * pres_notifier_poll_rate * pres_notifier_processes); } n_update_cols++; if(pa_dbf.update(pa_db, query_cols, 0, query_vals, db_cols,db_vals,n_query_cols,n_update_cols) < 0) { LM_ERR("DB update failed\n"); return(-1); } return(0); }
int add_rule2hash(dpl_node_t * rule, dp_connection_list_t *conn, int index) { dpl_id_p crt_idp; dpl_index_p indexp; int new_id, bucket = 0; if(!conn){ LM_ERR("data not allocated\n"); return -1; } new_id = 0; crt_idp = select_dpid(conn, rule->dpid, index); /*didn't find a dpl_id*/ if(!crt_idp){ crt_idp = shm_malloc(sizeof(dpl_id_t) + (DP_INDEX_HASH_SIZE+1) * sizeof(dpl_index_t)); if(!crt_idp){ LM_ERR("out of shm memory (crt_idp)\n"); return -1; } memset(crt_idp, 0, sizeof(dpl_id_t) + (DP_INDEX_HASH_SIZE+1) * sizeof(dpl_index_t)); crt_idp->dp_id = rule->dpid; crt_idp->rule_hash = (dpl_index_t*)(crt_idp + 1); new_id = 1; LM_DBG("new dpl_id %i\n", rule->dpid); } switch (rule->matchop) { case REGEX_OP: indexp = &crt_idp->rule_hash[DP_INDEX_HASH_SIZE]; break; case EQUAL_OP: if (rule->match_exp.s == NULL || rule->match_exp.len == 0) { LM_ERR("NULL matching expressions in database not accepted!!!\n"); return -1; } bucket = core_case_hash(&rule->match_exp, NULL, DP_INDEX_HASH_SIZE); indexp = &crt_idp->rule_hash[bucket]; break; default: LM_ERR("SKIPPED RULE. Unsupported match operator (%d).\n", rule->matchop); goto err; } /* Add the new rule to the corresponding bucket */ rule->next = 0; if(!indexp->first_rule) indexp->first_rule = rule; if(indexp->last_rule) indexp->last_rule->next = rule; indexp->last_rule = rule; if(new_id){ crt_idp->next = conn->hash[conn->next_index]; conn->hash[conn->next_index] = crt_idp; } LM_DBG("added the rule id %i pr %i next %p to the " " %i bucket\n", rule->dpid, rule->pr, rule->next, rule->matchop == REGEX_OP ? DP_INDEX_HASH_SIZE : bucket); return 0; err: if(new_id) shm_free(crt_idp); return -1; }
int sql_do_query(sql_con_t *con, str *query, sql_result_t *res) { db1_res_t* db_res = NULL; int i, j; str sv; if(res) sql_reset_result(res); if(query==NULL) { LM_ERR("bad parameters\n"); return -1; } if(con->dbf.raw_query(con->dbh, query, &db_res)!=0) { LM_ERR("cannot do the query [%.*s]\n", (query->len>32)?32:query->len, query->s); return -1; } if(db_res==NULL || RES_ROW_N(db_res)<=0 || RES_COL_N(db_res)<=0) { LM_DBG("no result after query\n"); con->dbf.free_result(con->dbh, db_res); return 2; } if(!res) { LM_DBG("no sqlresult parameter, ignoring result from query\n"); con->dbf.free_result(con->dbh, db_res); return 3; } res->ncols = RES_COL_N(db_res); res->nrows = RES_ROW_N(db_res); LM_DBG("rows [%d] cols [%d]\n", res->nrows, res->ncols); res->cols = (sql_col_t*)pkg_malloc(res->ncols*sizeof(sql_col_t)); if(res->cols==NULL) { res->ncols = 0; res->nrows = 0; LM_ERR("no more memory\n"); return -1; } memset(res->cols, 0, res->ncols*sizeof(sql_col_t)); for(i=0; i<res->ncols; i++) { res->cols[i].name.len = (RES_NAMES(db_res)[i])->len; res->cols[i].name.s = (char*)pkg_malloc((res->cols[i].name.len+1) *sizeof(char)); if(res->cols[i].name.s==NULL) { LM_ERR("no more memory\n"); goto error; } memcpy(res->cols[i].name.s, RES_NAMES(db_res)[i]->s, res->cols[i].name.len); res->cols[i].name.s[res->cols[i].name.len]='\0'; res->cols[i].colid = core_case_hash(&res->cols[i].name, 0, 0); } res->vals = (sql_val_t**)pkg_malloc(res->nrows*sizeof(sql_val_t*)); if(res->vals==NULL) { LM_ERR("no more memory\n"); goto error; } memset(res->vals, 0, res->nrows*sizeof(sql_val_t*)); for(i=0; i<res->nrows; i++) { res->vals[i] = (sql_val_t*)pkg_malloc(res->ncols*sizeof(sql_val_t)); if(res->vals[i]==NULL) { LM_ERR("no more memory\n"); goto error; } memset(res->vals[i], 0, res->ncols*sizeof(sql_val_t)); for(j=0; j<res->ncols; j++) { if(RES_ROWS(db_res)[i].values[j].nul) { res->vals[i][j].flags = PV_VAL_NULL; continue; } sv.s = NULL; sv.len = 0; switch(RES_ROWS(db_res)[i].values[j].type) { case DB1_STRING: res->vals[i][j].flags = PV_VAL_STR; sv.s= (char*)RES_ROWS(db_res)[i].values[j].val.string_val; sv.len=strlen(sv.s); break; case DB1_STR: res->vals[i][j].flags = PV_VAL_STR; sv.len= RES_ROWS(db_res)[i].values[j].val.str_val.len; sv.s= (char*)RES_ROWS(db_res)[i].values[j].val.str_val.s; break; case DB1_BLOB: res->vals[i][j].flags = PV_VAL_STR; sv.len= RES_ROWS(db_res)[i].values[j].val.blob_val.len; sv.s= (char*)RES_ROWS(db_res)[i].values[j].val.blob_val.s; break; case DB1_INT: res->vals[i][j].flags = PV_VAL_INT; res->vals[i][j].value.n = (int)RES_ROWS(db_res)[i].values[j].val.int_val; break; case DB1_DATETIME: res->vals[i][j].flags = PV_VAL_INT; res->vals[i][j].value.n = (int)RES_ROWS(db_res)[i].values[j].val.time_val; break; case DB1_BITMAP: res->vals[i][j].flags = PV_VAL_INT; res->vals[i][j].value.n = (int)RES_ROWS(db_res)[i].values[j].val.bitmap_val; break; case DB1_BIGINT: res->vals[i][j].flags = PV_VAL_STR; res->vals[i][j].value.s.len = 21*sizeof(char); res->vals[i][j].value.s.s = (char*)pkg_malloc(res->vals[i][j].value.s.len); if(res->vals[i][j].value.s.s==NULL) { LM_ERR("no more memory\n"); goto error; } db_longlong2str(RES_ROWS(db_res)[i].values[j].val.ll_val, res->vals[i][j].value.s.s, &res->vals[i][j].value.s.len); break; default: res->vals[i][j].flags = PV_VAL_NULL; } if(res->vals[i][j].flags == PV_VAL_STR && sv.s) { if(sv.len<=0) { res->vals[i][j].value.s.s = _sql_empty_buf; res->vals[i][j].value.s.len = 0; continue; } res->vals[i][j].value.s.s = (char*)pkg_malloc(sv.len*sizeof(char)); if(res->vals[i][j].value.s.s==NULL) { LM_ERR("no more memory\n"); goto error; } memcpy(res->vals[i][j].value.s.s, sv.s, sv.len); res->vals[i][j].value.s.len = sv.len; } } } con->dbf.free_result(con->dbh, db_res); return 1; error: con->dbf.free_result(con->dbh, db_res); sql_reset_result(res); return -1; }
int translate(struct sip_msg *msg, str input, str * output, dpl_id_p idp, str * attrs) { dpl_node_p rulep, rrulep; int string_res = -1, regexp_res = -1, bucket; if(!input.s || !input.len) { LM_ERR("invalid input string\n"); return -1; } bucket = core_case_hash(&input, NULL, DP_INDEX_HASH_SIZE); /* try to match the input in the corresponding string bucket */ for (rulep = idp->rule_hash[bucket].first_rule; rulep; rulep=rulep->next) { LM_DBG("Equal operator testing\n"); if(rulep->match_exp.len != input.len) continue; LM_DBG("Comparing (input %.*s) with (rule %.*s) [%d] and timerec %.*s\n", input.len, input.s, rulep->match_exp.len, rulep->match_exp.s, rulep->match_flags, rulep->timerec.len, rulep->timerec.s); // Check for Time Period if Set if(rulep->parsed_timerec) { LM_DBG("Timerec exists for rule checking: %.*s\n", rulep->timerec.len, rulep->timerec.s); // Doesn't matches time period continue with next rule if(!check_time(rulep->parsed_timerec)) { LM_DBG("Time rule doesn't match: skip next!\n"); continue; } } if (rulep->match_flags & DP_CASE_INSENSITIVE) { string_res = strncasecmp(rulep->match_exp.s,input.s,input.len); } else { string_res = strncmp(rulep->match_exp.s,input.s,input.len); } if (string_res == 0) { break; } } /* try to match the input in the regexp bucket */ for (rrulep = idp->rule_hash[DP_INDEX_HASH_SIZE].first_rule; rrulep; rrulep=rrulep->next) { // Check for Time Period if Set if(rrulep->parsed_timerec) { LM_DBG("Timerec exists for rule checking: %.*s\n", rrulep->timerec.len, rrulep->timerec.s); // Doesn't matches time period continue with next rule if(!check_time(rrulep->parsed_timerec)) { LM_DBG("Time rule doesn't match: skip next!\n"); continue; } } regexp_res = (test_match(input, rrulep->match_comp, matches, MAX_MATCHES) >= 0 ? 0 : -1); LM_DBG("Regex operator testing. Got result: %d\n", regexp_res); if (regexp_res == 0) { break; } } if (string_res != 0 && regexp_res != 0) { LM_DBG("No matching rule for input %.*s\n", input.len, input.s); return -1; } /* pick the rule with lowest table index if both match and prio are equal */ if (string_res == 0 && regexp_res == 0) { if (rrulep->pr < rulep->pr) { rulep = rrulep; } else if (rrulep->pr == rulep->pr && rrulep->table_id < rulep->table_id) { rulep = rrulep; } } if (!rulep) rulep = rrulep; LM_DBG("Found a matching rule %p: pr %i, match_exp %.*s\n", rulep, rulep->pr, rulep->match_exp.len, rulep->match_exp.s); if(attrs){ attrs->len = 0; attrs->s = 0; if(rulep->attrs.len>0) { LM_DBG("the rule's attrs are %.*s\n", rulep->attrs.len, rulep->attrs.s); if(rulep->attrs.len >= DP_MAX_ATTRS_LEN) { LM_ERR("EXCEEDED Max attribute length.\n"); return -1; } attrs->s = dp_attrs_buf; memcpy(attrs->s, rulep->attrs.s, rulep->attrs.len*sizeof(char)); attrs->len = rulep->attrs.len; attrs->s[attrs->len] = '\0'; LM_DBG("the copied attributes are: %.*s\n", attrs->len, attrs->s); } } if(rule_translate(msg, input, rulep, output)!=0){ LM_ERR("could not build the output\n"); return -1; } 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_case_hash(&pres_uri, NULL, phtable_size); lock_get(&pres_htable[hash_code].lock); p= search_phtable(&pres_uri, presentity->event->evp->type, 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)*sizeof(char)); 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 retrieve 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; }