int b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key, str* tuple_key) { b2b_dlg_t* dlg; unsigned int hash_index, local_index; b2b_table table; int ret; if(!callid || !callid->s || !callid->len){ LM_ERR("Wrong callid param\n"); return -1; } if(!from_tag || !from_tag->s || !from_tag->len){ LM_ERR("Wrong from_tag param\n"); return -1; } if(!to_tag || !to_tag->s || !to_tag->len){ LM_ERR("Wrong to_tag param\n"); return -1; } if(!tuple_key || !tuple_key->s || tuple_key->len<B2BL_MAX_KEY_LEN) { LM_ERR("Wrong tuple param\n"); return -1; } /* check if the to tag has the b2b key format * -> meaning that it is a server request */ if(b2b_parse_key(to_tag, &hash_index, &local_index)>=0) table = server_htable; else if (b2b_parse_key(callid, &hash_index, &local_index)>=0) table = client_htable; else return -1; /* to tag and/or callid are not part of this B2B */ lock_get(&table[hash_index].lock); dlg=b2b_search_htable_dlg(table, hash_index, local_index, to_tag, from_tag, callid); if(dlg){ memcpy(tuple_key->s, dlg->param.s, dlg->param.len); tuple_key->len = dlg->param.len; if (entity_key) { if (table == server_htable) { entity_key->s = to_tag->s; entity_key->len = to_tag->len; } else { entity_key->s = callid->s; entity_key->len = callid->len; } } LM_DBG("got tuple [%.*s] for entity [%.*s]\n", tuple_key->len, tuple_key->s, (entity_key?entity_key->len:0), (entity_key?entity_key->s:NULL)); ret = 0; } else { ret = -1; } lock_release(&table[hash_index].lock); return ret; }
int b2b_restore_logic_info(enum b2b_entity_type type, str* key, b2b_notify_t cback) { b2b_dlg_t* dlg; b2b_table table; unsigned int hash_index, local_index; if(server_htable== NULL) { LM_ERR("You have to load b2b_entities module before b2b_logic module\n"); return -1; } if(type == B2B_SERVER) { table = server_htable; } else { table = client_htable; } if(b2b_parse_key(key, &hash_index, &local_index) < 0) { LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s); return -1; } dlg = b2b_search_htable(table, hash_index, local_index); if(dlg == NULL) { LM_ERR("No dialog found for key [%.*s]\n", key->len, key->s); return -1; } dlg->b2b_cback = cback; return 0; }
int b2b_update_b2bl_param(enum b2b_entity_type type, str* key, str* param) { b2b_dlg_t* dlg; b2b_table table; unsigned int hash_index, local_index; if(!param) { LM_ERR("NULL param\n"); return -1; } if(param->len > B2BL_MAX_KEY_LEN) { LM_ERR("parameter too long, received [%d], maximum [%d]\n", param->len, B2BL_MAX_KEY_LEN); return -1; } if(type == B2B_SERVER) { table = server_htable; } else { table = client_htable; } if(b2b_parse_key(key, &hash_index, &local_index) < 0) { LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s); return -1; } lock_get(&table[hash_index].lock); dlg = b2b_search_htable(table, hash_index, local_index); if(dlg == NULL) { LM_ERR("No dialog found\n"); lock_release(&table[hash_index].lock); return -1; } memcpy(dlg->param.s, param->s, param->len); dlg->param.len = param->len; lock_release(&table[hash_index].lock); return 0; }
int b2b_entities_restore(void) { db_res_t *result= NULL; db_row_t *rows = NULL; db_val_t *row_vals= NULL; int i; dlg_leg_t leg, *new_leg; b2b_dlg_t dlg, *shm_dlg= NULL; unsigned int hash_index, local_index; int nr_rows; str* b2b_key; str sockinfo_str; str host; int port, proto; b2b_table htable; int type; int no_rows = 10; if(b2be_db == NULL) { LM_DBG("NULL database connection\n"); return 0; } if(b2be_dbf.use_table(b2be_db, &b2be_dbtable)< 0) { LM_ERR("sql use table failed\n"); return -1; } if (DB_CAPABILITY(b2be_dbf, DB_CAP_FETCH)) { if(b2be_dbf.query(b2be_db,0,0,0,qcols, 0, DB_COLS_NO, 0, 0) < 0) { LM_ERR("Error while querying (fetch) database\n"); return -1; } no_rows = estimate_available_rows( DB_COLS_NO*128, DB_COLS_NO); if (no_rows==0) no_rows = 10; if(b2be_dbf.fetch_result(b2be_db,&result,no_rows)<0) { LM_ERR("fetching rows failed\n"); return -1; } } else { if (b2be_dbf.query (b2be_db, 0, 0, 0,qcols,0, DB_COLS_NO, 0, &result) < 0) { LM_ERR("querying presentity\n"); return -1; } } nr_rows = RES_ROW_N(result); do { LM_DBG("loading information from database %i records\n", nr_rows); rows = RES_ROWS(result); /* for every row */ for(i=0; i<nr_rows; i++) { row_vals = ROW_VALUES(rows +i); memset(&dlg, 0, sizeof(b2b_dlg_t)); type = row_vals[0].val.int_val; dlg.tag[1].s = (char*)row_vals[2].val.string_val; dlg.tag[1].len = dlg.tag[1].s?strlen(dlg.tag[1].s):0; dlg.callid.s = (char*)row_vals[3].val.string_val; dlg.callid.len = dlg.callid.s?strlen(dlg.callid.s):0; if(type == B2B_SERVER)/* extract hash and local index */ { htable = server_htable; if(b2b_parse_key(&dlg.tag[1], &hash_index, &local_index) < 0) { LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.tag[1].len, dlg.tag[1].s); goto error; } } else { htable = client_htable; if(b2b_parse_key(&dlg.callid, &hash_index, &local_index) < 0) { LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.callid.len, dlg.callid.s); goto error; } } dlg.id = local_index; dlg.state = row_vals[13].val.int_val; dlg.ruri.s = (char*)row_vals[4].val.string_val; dlg.ruri.len = dlg.ruri.s?strlen(dlg.ruri.s):0; dlg.from_uri.s = (char*)row_vals[5].val.string_val; dlg.from_uri.len = strlen(dlg.from_uri.s); dlg.from_dname.s = (char*)row_vals[6].val.string_val; dlg.from_dname.len = strlen(dlg.from_dname.s); dlg.to_uri.s = (char*)row_vals[7].val.string_val; dlg.to_uri.len = strlen(dlg.to_uri.s); dlg.to_dname.s = (char*)row_vals[8].val.string_val; dlg.to_dname.len = dlg.to_dname.s?strlen(dlg.to_dname.s):0; dlg.tag[0].s = (char*)row_vals[1].val.string_val; dlg.tag[0].len = dlg.tag[0].s?strlen(dlg.tag[0].s):0; dlg.cseq[0] = row_vals[14].val.int_val; dlg.cseq[1] = row_vals[15].val.int_val; dlg.route_set[0].s = (char*)row_vals[9].val.string_val; dlg.route_set[0].len = dlg.route_set[0].s?strlen(dlg.route_set[0].s):0; dlg.route_set[1].s = (char*)row_vals[10].val.string_val; dlg.route_set[1].len = dlg.route_set[1].s?strlen(dlg.route_set[1].s):0; dlg.contact[0].s = (char*)row_vals[19].val.string_val; dlg.contact[0].len = dlg.contact[0].s?strlen(dlg.contact[0].s):0; dlg.contact[1].s = (char*)row_vals[20].val.string_val; dlg.contact[1].len = dlg.contact[1].s?strlen(dlg.contact[1].s):0; dlg.last_method = row_vals[16].val.int_val; dlg.last_reply_code = row_vals[17].val.int_val; dlg.last_invite_cseq = row_vals[18].val.int_val; dlg.param.s = (char*)row_vals[12].val.string_val; dlg.param.len = strlen(dlg.param.s); sockinfo_str.s = (char*)row_vals[11].val.string_val; if(sockinfo_str.s) { sockinfo_str.len = strlen(sockinfo_str.s); if(sockinfo_str.len) { if (parse_phostport (sockinfo_str.s, sockinfo_str.len, &host.s, &host.len, &port, &proto )< 0) { LM_ERR("bad format for stored sockinfo string [%.*s]\n", sockinfo_str.len, sockinfo_str.s); goto error; } dlg.send_sock = grep_sock_info(&host, (unsigned short) port, (unsigned short) proto); } } dlg.db_flag = NO_UPDATEDB_FLAG; shm_dlg = b2b_dlg_copy(&dlg); if(shm_dlg == NULL) { LM_ERR("Failed to create new dialog structure\n"); goto error; } b2b_key= b2b_htable_insert(htable,shm_dlg,hash_index,type, 1); if(b2b_key == NULL) { LM_ERR("Failed to insert new record\n"); goto error; } pkg_free(b2b_key); memset(&leg, 0, sizeof(dlg_leg_t)); leg.tag.s= (char*)row_vals[21].val.string_val; if(!leg.tag.s) continue; leg.tag.len = strlen(leg.tag.s); leg.cseq = row_vals[22].val.int_val; leg.contact.s = (char*)row_vals[23].val.string_val; leg.contact.len = leg.contact.s?strlen(leg.contact.s):0; leg.route_set.s = (char*)row_vals[24].val.string_val; leg.route_set.len = leg.route_set.s?strlen(leg.route_set.s):0; new_leg = b2b_dup_leg(&leg, SHM_MEM_TYPE); if(new_leg== NULL) { LM_ERR("Failed to construct b2b leg structure\n"); goto error; } shm_dlg->legs = new_leg; } /* any more data to be fetched ?*/ if (DB_CAPABILITY(b2be_dbf, DB_CAP_FETCH)) { if (b2be_dbf.fetch_result( b2be_db, &result, no_rows) < 0) { LM_ERR("fetching more rows failed\n"); goto error; } nr_rows = RES_ROW_N(result); } else { nr_rows = 0; } }while (nr_rows>0); b2be_dbf.free_result(b2be_db, result); return 0; error: if(result) b2be_dbf.free_result(b2be_db, result); return -1; }