Example #1
0
int frd_reload_data(void)
{
	dr_head_p new_head, old_head;

	if ((new_head = drb.create_head()) == NULL) {
		LM_ERR ("cannot create dr_head\n");
		return -1;
	}

	free_list_t *new_list = NULL, *old_list;

	if (frd_load_data(new_head, &new_list) != 0) {
		LM_ERR("cannot load fraud data\n");
		return -1;
	}

	old_head = *dr_head;
	old_list = free_list;
	++frd_data_rev;
	lock_start_write(frd_data_lock);
	*dr_head = new_head;
	free_list = new_list;
	lock_stop_write(frd_data_lock);
	frd_destroy_data_unsafe(old_head, old_list);
	return 0;
}
Example #2
0
/* reloads data from the db */
static int reload_data(void)
{
	int n;
	struct tls_domain *tls_client_domains_tmp;
	struct tls_domain *tls_server_domains_tmp;

	tls_client_domains_tmp = NULL;
	tls_server_domains_tmp = NULL;

	load_info(&dr_dbf, db_hdl, &tls_db_table, &tls_server_domains_tmp,
		&tls_client_domains_tmp);

	/*
	 * now initialize tls virtual domains
	 */
	if ((n = init_tls_domains(tls_server_domains_tmp))) {
		return n;
	}
	if ((n = init_tls_domains(tls_client_domains_tmp))) {
		return n;
	}

	lock_start_write(dom_lock);

	tls_release_all_domains(tls_client_domains);
	tls_release_all_domains(tls_server_domains);
	tls_client_domains = tls_client_domains_tmp;
	tls_server_domains = tls_server_domains_tmp;

	lock_stop_write(dom_lock);
	return 0;
}
Example #3
0
static struct mi_root* clusterer_reload(struct mi_root* root, void *param)
{
	cluster_info_t *new_info;
	cluster_info_t *old_info;

	if (!db_mode) {
		LM_ERR("Running in non-DB mode\n");
		return init_mi_tree(400, "Non-DB mode", 11);
	}

	if (load_db_info(&dr_dbf, db_hdl, &db_table, &new_info) != 0) {
		LM_ERR("Failed to load info from DB\n");
		return init_mi_tree(500, "Failed to reload", 16);
	}

	lock_start_write(cl_list_lock);
	old_info = *cluster_list;
	*cluster_list = new_info;
	lock_stop_write(cl_list_lock);

	if (old_info)
		free_info(old_info);

	LM_INFO("Reloaded DB info\n");

	return init_mi_tree(200, MI_SSTR(MI_OK));
}
Example #4
0
/* setting a connection status */
static int set_state(int cluster_id, int machine_id,
					 enum cl_machine_state state, int proto)
{
	table_entry_value_t *head_table;
	int is_ok = 1;

	LM_DBG("setting node with c_id %d m_id %d proto %d with state %d\n",
			cluster_id, machine_id, proto, state);

	/* finding the machine */
	lock_start_write(ref_lock);

	head_table = clusterer_find_nodes(cluster_id, proto);

	/* if the protocol is not specified */
	for (; head_table; head_table = head_table->next) {
		if (head_table->machine_id == machine_id) {
			head_table->dirty_bit = 1;
			if (state == CLUSTERER_STATE_OFF) {
				head_table->no_tries++;
				head_table->last_attempt = time(0);
				if (head_table->no_tries == head_table->failed_attempts) {
					head_table->state = CLUSTERER_STATE_OFF;
				}
			} else {
				head_table->state = state;
			}
			is_ok = 0;
			break;
		}
	}

	lock_stop_write(ref_lock);
	return is_ok;
}
Example #5
0
void utimer_routine(utime_t uticks , void *set)
{
	struct timer_link *tl, *tmp_tl;
	int                id;

	lock_start_write( timertable[(long)set].ex_lock );

	for( id=RT_T1_TO_1 ; id<NR_OF_TIMER_LISTS ; id++ )
	{
		/* to waste as little time in lock as possible, detach list
		   with expired items and process them after leaving the lock */
		tl=check_and_split_time_list( &timertable[(long)set].timers[ id ], uticks);
		/* process items now */
		switch (id)
		{
			case RT_T1_TO_1:
			case RT_T1_TO_2:
			case RT_T1_TO_3:
			case RT_T2:
				run_handler_for_each(tl,retransmission_handler);
				break;
		}
	}
	lock_stop_write( timertable[(long)set].ex_lock );
}
Example #6
0
void timer_routine(unsigned int ticks , void *set)
{
	struct timer_link *tl, *tmp_tl;
	int                id;

	lock_start_write( timertable[(long)set].ex_lock );

	for( id=0 ; id<RT_T1_TO_1 ; id++ )
	{
		/* to waste as little time in lock as possible, detach list
		   with expired items and process them after leaving the lock */
		tl=check_and_split_time_list( &timertable[(long)set].timers[ id ], ticks);
		/* process items now */
		switch (id)
		{
			case FR_TIMER_LIST:
			case FR_INV_TIMER_LIST:
				run_handler_for_each(tl,final_response_handler);
				break;
			case WT_TIMER_LIST:
				run_handler_for_each(tl,wait_handler);
				break;
			case DELETE_LIST:
				run_handler_for_each(tl,delete_handler);
				break;
		}
	}
	lock_stop_write( timertable[(long)set].ex_lock );
}
Example #7
0
static inline int lb_reload_data( void )
{
	struct lb_data *new_data;
	struct lb_data *old_data;

	new_data = load_lb_data();
	if ( new_data==0 ) {
		LM_CRIT("failed to load load-balancing info\n");
		return -1;
	}

	lock_start_write( ref_lock );

	/* no more activ readers -> do the swapping */
	old_data = *curr_data;
	*curr_data = new_data;

	lock_stop_write( ref_lock );

	/* destroy old data */
	if (old_data) {
		/* copy the state of the destinations from the old set
		 * (for the matching ids) */
		lb_inherit_state( old_data, new_data);
		free_lb_data( old_data );
	}

	/* generate new blacklist from the routing info */
	populate_lb_bls((*curr_data)->dsts);

	return 0;
}
Example #8
0
struct mi_root *mi_set_shtag_active(struct mi_root *cmd_tree, void *param)
{
	struct mi_node* node;
	struct dlg_sharing_tag *tag;

	if (!dialog_repl_cluster)
		return init_mi_tree(400, MI_SSTR("Dialog replication disabled"));

	node = cmd_tree->node.kids;
	if (node == NULL || !node->value.s || !node->value.len)
		return init_mi_tree(400, MI_SSTR(MI_MISSING_PARM));

	lock_start_write(shtags_lock);

	if ((tag = get_shtag_unsafe(&node->value)) == NULL)
		return init_mi_tree(500, MI_SSTR("Unable to set sharing tag"));

	tag->state = SHTAG_STATE_ACTIVE;

	lock_stop_write(shtags_lock);

	if (send_shtag_active_info(&node->value, 0) < 0)
		LM_WARN("Failed to broadcast message about tag [%.*s] going active\n",
			node->value.len, node->value.s);

	return init_mi_tree( 200, MI_SSTR(MI_OK));
}
Example #9
0
static int receive_shtag_active_msg(bin_packet_t *packet)
{
	str tag_name;
	struct dlg_sharing_tag *tag;

	bin_pop_str(packet, &tag_name);

	lock_start_write(shtags_lock);

	if ((tag = get_shtag_unsafe(&tag_name)) == NULL) {
		LM_ERR("Unable to fetch sharing tag\n");
		lock_stop_write(shtags_lock);
		return -1;
	}

	/* directly go to backup state when another
	 * node in the cluster is to active */
	tag->state = SHTAG_STATE_BACKUP;

	tag->send_active_msg = 0;
	free_active_msgs_info(tag);

	lock_stop_write(shtags_lock);

	return 0;
}
Example #10
0
void tls_release_domain(struct tls_domain* dom)
{
	if (!dom || !(dom->type & TLS_DOMAIN_DB))
		return;

	if (dom_lock)
		lock_start_write(dom_lock);

	tls_release_domain_aux(dom);

	if (dom_lock)
		lock_stop_write(dom_lock);
}
Example #11
0
static void lb_update_max_loads(unsigned int ticks, void *param)
{
	struct lb_dst *dst;
	int ri, old, psz;

	LM_DBG("updating max loads...\n");

	lock_start_write(ref_lock);
	for (dst = (*curr_data)->dsts; dst; dst = dst->next) {
		if (!dst->fs_sock)
			continue;

		lock_start_read(dst->fs_sock->stats_lk);
		for (ri = 0; ri < dst->rmap_no; ri++) {
			if (dst->rmap[ri].fs_enabled) {
				psz = lb_dlg_binds.get_profile_size(
				            dst->rmap[ri].resource->profile, &dst->profile_id);
				old = dst->rmap[ri].max_load;

				/*
				 * The normal case. OpenSIPS sees, at _most_, the same number
				 * of sessions as FreeSWITCH does. Any differences must be
				 * subtracted from the remote "max sessions" value
				 */
				if (psz < dst->fs_sock->stats.max_sess) {
					dst->rmap[ri].max_load =
					(dst->fs_sock->stats.id_cpu / (float)100) *
						(dst->fs_sock->stats.max_sess -
						 (dst->fs_sock->stats.sess - psz));
				} else {
					dst->rmap[ri].max_load =
					(dst->fs_sock->stats.id_cpu / (float)100) *
						dst->fs_sock->stats.max_sess;
				}
				LM_DBG("load update on FS (%p) %s:%d: "
				       "%d -> %d (%d %d %.3f), prof=%d\n",
				       dst->fs_sock, dst->fs_sock->host.s, dst->fs_sock->port,
				       old, dst->rmap[ri].max_load, dst->fs_sock->stats.sess,
				       dst->fs_sock->stats.max_sess,
				       dst->fs_sock->stats.id_cpu, psz);
			}
		}
		lock_stop_read(dst->fs_sock->stats_lk);
	}
	lock_stop_write(ref_lock);
}
Example #12
0
static void update_nodes_handler(unsigned int ticks, void *param)
{
	/* data */
	table_entry_t *head_table;
	table_entry_info_t *info;
	table_entry_value_t *value;
	struct module_timestamp *head;
	uint64_t ctime;

	if(clusterer_modules == NULL)
		return;

	ctime = time(0);

	lock_start_write(ref_lock);
	head_table = *tdata;
	while (head_table != NULL) {
		info = head_table->info;
		while (info != NULL) {
			value = info->value;
			while (value != NULL) {
				head = value->in_timestamps;
				while (head != NULL) {
					if (head->state == CLUSTERER_STATE_PROBE && (ctime - head->timestamp) > head->up->timeout) {
						head->up->cb(SERVER_TIMEOUT, NULL, value->id);
						head->timestamp = head->timestamp + head->up->timeout;
						head->state = CLUSTERER_STATE_OFF;
					}
					if (head->state == CLUSTERER_STATE_OFF && (ctime - head->timestamp) > head->up->duration) {
						LM_DBG("node c_id %d m_id %d is up again\n", head_table->cluster_id, value->machine_id);
						head->state = CLUSTERER_STATE_PROBE;
						head->timestamp = ctime;
					}
					head = head->next;
				}
				value = value->next;
			}
			info = info->next;
		}
		head_table = head_table->next;
	}
	lock_stop_write(ref_lock);
}
Example #13
0
mi_response_t *clusterer_reload(const mi_params_t *params,
								struct mi_handler *async_hdl)
{
	cluster_info_t *new_info;
	cluster_info_t *old_info;

	if (!db_mode) {
		LM_ERR("Running in non-DB mode\n");
		return init_mi_error(400, MI_SSTR("Non-DB mode"));
	}

	if (load_db_info(&dr_dbf, db_hdl, &db_table, &new_info) != 0) {
		LM_ERR("Failed to load info from DB\n");
		return init_mi_error(500, MI_SSTR("Failed to reload"));
	}

	lock_start_write(cl_list_lock);
	if (preserve_reg_caps(new_info) < 0) {
		lock_stop_write(cl_list_lock);
		LM_ERR("Failed to preserve registered capabilities\n");

		if (new_info)
			free_info(new_info);

		return init_mi_error(500, "Failed to reload", 16);
	}
	old_info = *cluster_list;
	*cluster_list = new_info;
	lock_stop_write(cl_list_lock);

	if (old_info)
		free_info(old_info);

	LM_INFO("Reloaded DB info\n");

	/* check if the cluster IDs in the the sharing tag list are valid */
	shtag_validate_list();

	return init_mi_result_ok();
}
Example #14
0
static int set_in_timestamp(struct module_list *module, int machine_id)
{
	table_entry_value_t *values;
	int is_ok = 1;
	uint64_t ctime = time(0);
	struct module_timestamp *head;

	LM_DBG("setting timestamp for node with c_id %d m_id %d proto%d\n",
			module->accept_cluster_id, machine_id, module->proto);

	/* finding the machine */
	lock_start_write(ref_lock);

	/* if the protocol is not specified */
	for (values = module->values; values; values = values->next) {
		if (values->machine_id == machine_id) {
			is_ok = 0;
			for (head = values->in_timestamps; head; head = head->next) {
				if (head->up == module) {
					if (head->state == CLUSTERER_STATE_OFF) {
						LM_DBG("state for node with clusterer_id %d is 2\n",
								values->id);
						is_ok = -1;
					} else
						head->timestamp = ctime;

					break;
				}
			}

			break;
		}
	}

	lock_stop_write(ref_lock);
	return is_ok;
}
Example #15
0
static int pdt_load_db(void)
{
	db_key_t db_cols[3] = {&sdomain_column, &prefix_column, &domain_column};
	str p, d, sdomain;
	db_res_t* db_res = NULL;
	int i, ret;
	pdt_tree_t *_ptree_new = NULL;
	pdt_tree_t *old_tree = NULL;
	int no_rows = 10;

	if(db_con==NULL)
	{
		LM_ERR("no db connection\n");
		return -1;
	}

	if (pdt_dbf.use_table(db_con, &db_table) < 0)
	{
		LM_ERR("failed to use_table\n");
		return -1;
	}

	if (DB_CAPABILITY(pdt_dbf, DB_CAP_FETCH)) {
		if(pdt_dbf.query(db_con,0,0,0,db_cols,0,3,&sdomain_column,0) < 0)
		{
			LM_ERR("Error while querying db\n");
			return -1;
		}
		no_rows = estimate_available_rows( 64+16+64, 3);
		if (no_rows==0) no_rows = 10;
		if(pdt_dbf.fetch_result(db_con, &db_res, no_rows)<0)
		{
			LM_ERR("Error while fetching result\n");
			if (db_res)
				pdt_dbf.free_result(db_con, db_res);
			goto error;
		} else {
			if(RES_ROW_N(db_res)==0)
			{
				return 0;
			}
		}
	} else {
		if((ret=pdt_dbf.query(db_con, NULL, NULL, NULL, db_cols,
				0, 3, &sdomain_column, &db_res))!=0
			|| RES_ROW_N(db_res)<=0 )
		{
			pdt_dbf.free_result(db_con, db_res);
			if( ret==0)
			{
				return 0;
			} else {
				goto error;
			}
		}
	}

	do {
		for(i=0; i<RES_ROW_N(db_res); i++)
		{
			/* check for NULL values ?!?! */
			sdomain.s = (char*)(RES_ROWS(db_res)[i].values[0].val.string_val);
			sdomain.len = strlen(sdomain.s);

			p.s = (char*)(RES_ROWS(db_res)[i].values[1].val.string_val);
			p.len = strlen(p.s);

			d.s = (char*)(RES_ROWS(db_res)[i].values[2].val.string_val);
			d.len = strlen(d.s);

			if(p.s==NULL || d.s==NULL || sdomain.s==NULL ||
					p.len<=0 || d.len<=0 || sdomain.len<=0)
			{
				LM_ERR("Error - bad values in db\n");
				continue;
			}

			if(pdt_check_domain!=0 && _ptree_new!=NULL
					&& pdt_check_pd(_ptree_new, &sdomain, &p, &d)==1)
			{
				LM_ERR("sdomain [%.*s]: prefix [%.*s] or domain <%.*s> "
					"duplicated\n", sdomain.len, sdomain.s, p.len, p.s,
					d.len, d.s);
				continue;
			}

			if(pdt_add_to_tree(&_ptree_new, &sdomain, &p, &d)<0)
			{
				LM_ERR("Error adding info to tree\n");
				goto error;
			}
	 	}
		if (DB_CAPABILITY(pdt_dbf, DB_CAP_FETCH)) {
			if(pdt_dbf.fetch_result(db_con, &db_res, no_rows)<0) {
				LM_ERR("Error while fetching!\n");
				if (db_res)
					pdt_dbf.free_result(db_con, db_res);
				goto error;
			}
		} else {
			break;
		}
	}  while(RES_ROW_N(db_res)>0);
	pdt_dbf.free_result(db_con, db_res);


	/* block all readers */
	lock_start_write( pdt_lock );

	old_tree = *_ptree;
	*_ptree = _ptree_new;

	lock_stop_write( pdt_lock );

	/* free old data */
	if (old_tree!=NULL)
		pdt_free_tree(old_tree);

	return 0;

error:
	pdt_dbf.free_result(db_con, db_res);
	if (_ptree_new!=NULL)
		pdt_free_tree(_ptree_new);
	return -1;
}
Example #16
0
/* reloads data from the db */
static int reload_data(void)
{
	struct module_list* modules;
	table_entry_t *new_data;
	table_entry_t *old_data;
	table_entry_t *new_head;
	table_entry_t *old_head;
	table_entry_info_t *new_info;
	table_entry_info_t *old_info;
	table_entry_value_t *new_value;
	table_entry_value_t *old_value;

	struct module_timestamp *aux;

	new_data = load_info(&dr_dbf, db_hdl, &db_table);
	if (!new_data) {
		LM_CRIT("failed to load routing info\n");
		return -1;
	}

	lock_start_write(ref_lock);

	/* no more active readers -> do the swapping */

	for (old_head = *tdata; old_head; old_head = old_head->next) {
		for (new_head = new_data; new_head; new_head = new_head->next) {
			if (old_head->cluster_id != new_head->cluster_id)
				continue;

			for (old_info = old_head->info; old_info; old_info = old_info->next) {
				for (new_info = new_head->info; new_info; new_info = new_info->next) {
					if (old_info->proto != new_info->proto)
						continue;

					for (old_value = old_info->value; old_value; old_value = old_value->next) {
						for (new_value = new_info->value; new_value; new_value = new_value->next) {
							if (su_cmp(&new_value->addr, &old_value->addr)) {
								aux = new_value->in_timestamps;
								new_value->in_timestamps = old_value->in_timestamps;
								old_value->in_timestamps = aux;
								break;
							}
						}
					}
				}
			}
		}
	}

	old_data = *tdata;
	*tdata = new_data;

	for (modules = clusterer_modules; modules; modules = modules->next)
		modules->values = clusterer_find_nodes(modules->accept_cluster_id, modules->proto);

	lock_stop_write(ref_lock);

	/* free old data */
	if (old_data)
		free_data(old_data);

	return 0;
}
Example #17
0
/* synchronize backend with the db */
static void update_db_handler(unsigned int ticks, void* param)
{
	/* data */
	table_entry_t *head_table;
	table_entry_value_t *value;
	table_entry_info_t *info;
	/* columns to be compared ( clusterer_id_col ) */
	db_key_t key_cmp;
	/* with values */
	db_val_t val_cmp;
	/* columns to be set */
	db_key_t key_set[3];
	/* with values */
	db_val_t val_set[3];
	int i;

	CON_OR_RESET(db_hdl);

	/* table to use*/
	if (dr_dbf.use_table(db_hdl, &db_table) < 0) {
		LM_ERR("cannot select table \"%.*s\"\n", db_table.len, db_table.s);
		return;
	}

	val_cmp.type = DB_INT;
	val_cmp.nul = 0;

	for (i = 0; i < 2; ++i) {
		val_set[i].type = DB_INT;
		val_set[i].nul = 0;
	}

	val_set[2].type = DB_BIGINT;
	val_set[2].nul = 0;

	key_cmp = &id_col;

	key_set[0] = &state_col;
	key_set[1] = &no_tries_col;
	key_set[2] = &last_attempt_col;


	lock_start_write(ref_lock);

	head_table = *tdata;
	/* iterating through backend storage to find all data that
	 * must be synchronized with the db */
	while (head_table != NULL) {
		info = head_table->info;
		while (info != NULL) {
			value = info->value;
			while (value != NULL) {
				if (value->dirty_bit == 1) {
					LM_DBG("setting row with primary key %d the status %d\n",
						value->id, value->state);

					val_cmp.val.int_val = value->id;
					val_set[0].val.int_val = value->state;
					val_set[1].val.int_val = value->no_tries;
					val_set[2].val.int_val = value->last_attempt;

					/* updating */
					if (dr_dbf.update(db_hdl, &key_cmp, &op_eq, &val_cmp, key_set, val_set, 1, 3) < 0) {
						LM_ERR("DB update failed\n");
					} else {
						/* only if the query is successful the data is synchronized */
						value->dirty_bit = 0;
					}
				}
				value = value->next;
			}
			info = info->next;
		}
		head_table = head_table->next;
	}

	lock_stop_write(ref_lock);

}
Example #18
0
/*load rules from DB*/
int dp_load_db(void)
{
	int i, nr_rows;
	db_res_t * res = 0;
	db_val_t * values;
	db_row_t * rows;
	db_key_t query_cols[DP_TABLE_COL_NO] = {
		&dpid_column,	&pr_column,
		&match_op_column,	&match_exp_column,	&match_len_column,
		&subst_exp_column,	&repl_exp_column,	&attrs_column };
	db_key_t order = &pr_column;
	dpl_node_t *rule;
	int no_rows = 10;

	if( (*crt_idx) != (*next_idx)){
		LM_WARN("a load command already generated, aborting reload...\n");
		return 0;
	}

	if (dp_dbf.use_table(dp_db_handle, &dp_table_name) < 0){
		LM_ERR("error in use_table\n");
		return -1;
	}

	if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) {
		if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, 
				DP_TABLE_COL_NO, order, 0) < 0){
			LM_ERR("failed to query database!\n");
			return -1;
		}
		no_rows = estimate_available_rows( 4+4+4+64+4+64+64+128,
			DP_TABLE_COL_NO);
		if (no_rows==0) no_rows = 10;
		if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) {
			LM_ERR("failed to fetch\n");
			if (res)
				dp_dbf.free_result(dp_db_handle, res);
			return -1;
		}
	} else {
		/*select the whole table and all the columns*/
		if(dp_dbf.query(dp_db_handle,0,0,0,query_cols, 0, 
			DP_TABLE_COL_NO, order, &res) < 0){
				LM_ERR("failed to query database\n");
			return -1;
		}
	}

	nr_rows = RES_ROW_N(res);

	lock_start_write( ref_lock );

	*next_idx = ((*crt_idx) == 0)? 1:0;

	if(nr_rows == 0){
		LM_WARN("no data in the db\n");
		goto end;
	}

	do {
		for(i=0; i<RES_ROW_N(res); i++){
			rows = RES_ROWS(res);
			values = ROW_VALUES(rows+i);

			if ((rule = build_rule(values)) == NULL ) {
				LM_WARN(" failed to build rule -> skipping\n");
				continue;
			}

			if(add_rule2hash(rule , *next_idx) != 0) {
				LM_ERR("add_rule2hash failed\n");
				goto err2;
			}
		}

		if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) {
			if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) {
				LM_ERR("failure while fetching!\n");
				if (res)
					dp_dbf.free_result(dp_db_handle, res);
				lock_stop_write( ref_lock );
				return -1;
			}
		} else {
			break;
		}
	}  while(RES_ROW_N(res)>0);
	

end:
	destroy_hash(*crt_idx);
	/*update data*/
	*crt_idx = *next_idx;

	/* release the exclusive writing access */
	lock_stop_write( ref_lock );

	list_hash(*crt_idx);

	dp_dbf.free_result(dp_db_handle, res);
	return 0;

err2:
	if(rule)	destroy_rule(rule);
	destroy_hash(*next_idx);
	dp_dbf.free_result(dp_db_handle, res);
	*next_idx = *crt_idx; 
	/* if lock defined - release the exclusive writing access */
	if(ref_lock)
		/* release the readers */
		lock_stop_write( ref_lock );
	return -1;
}
Example #19
0
/* select data from emergency_routing and put in memory
* coluns keys: srid(selectiveRoutingID), resn(routingESN) and npa.
* coluns translate: esgwri 
*/
int get_db_routing(str table_name, rw_lock_t *ref_lock ){
    db_key_t query_cols[] = {&id_col, &srid_col, &resn_col, &npa_col, &esgwri_col};
    db_res_t * res;
    db_val_t * values;
    db_row_t * rows;
    str esgwri;
    str SRID;
    int RESN;
    int NPA;
    int nr_rows, i, size, id;
    struct esrn_routing *esrn_cell, *old_list, *it, *aux, *new_list;
    struct esrn_routing *init_esrn = NULL;

    db_funcs.use_table(db_con, &table_name);

    /* select value from routing table
    *  the keys of routing table: selectiveRoutingID, routingESN, npa
    *  the result of routing lookup: esgwri
    */
    if (db_funcs.query(db_con, 0, 0, 0, query_cols, 0, 5, 0, &res) != 0) {
        LM_ERR("Failure to issue query\n");
        return -1;
    }

    nr_rows = RES_ROW_N(res);
    rows = RES_ROWS(res);

    new_list = NULL;
    LM_DBG("NUMBER OF LINES ROUTING %d \n", nr_rows);

    for (i = 0; i < nr_rows; i++) {
        values = ROW_VALUES(rows + i);

        if (VAL_NULL(values) ||
                (VAL_TYPE(values) != DB_INT)) {
            LM_ERR("Invalid value returned 1\n");
            goto end;
        }

        id = VAL_INT(values);

        if (VAL_NULL(values + 1) ||
                (VAL_TYPE(values + 1) != DB_STR && VAL_TYPE(values + 1) != DB_STRING)) {
            LM_ERR("Invalid translated returned 2\n");
            goto end;
        }

        if (VAL_TYPE(values + 1) == DB_STR) {
            SRID = VAL_STR(values + 1);
        } else {
            SRID.s = (char*) VAL_STRING(values + 1);
            SRID.len = strlen(SRID.s);
        }

        if (VAL_NULL(values + 2) ||
                (VAL_TYPE(values + 2) != DB_INT)) {
            LM_ERR("Invalid translated returned 3\n");
            goto end;
        }

        RESN = VAL_INT(values + 2);

        if (VAL_NULL(values + 3) ||
                (VAL_TYPE(values + 3) != DB_INT)) {
            LM_ERR("Invalid translated returned 4\n");
            goto end;
        }

        NPA = VAL_INT(values + 3);

        if (VAL_NULL(values + 4) ||
                (VAL_TYPE(values + 4) != DB_STR && VAL_TYPE(values + 4) != DB_STRING)) {
            LM_ERR("Invalid translated returned 5\n");
            goto end;
        }

        if (VAL_TYPE(values + 4) == DB_STR) {
            esgwri = VAL_STR(values + 4);
        } else {
            esgwri.s = (char*) VAL_STRING(values + 4);
            esgwri.len = strlen(esgwri.s);
        }

        size = sizeof (struct esrn_routing)+SRID.len + esgwri.len;
        esrn_cell = shm_malloc(size);
        if (!esrn_cell) {
            LM_ERR("no more shm\n");
            goto end;
        }           

        memset(esrn_cell, 0, size);

        esrn_cell->srid.len = SRID.len;
        esrn_cell->srid.s = (char *) (esrn_cell + 1);
        memcpy(esrn_cell->srid.s, SRID.s, SRID.len);
      
        esrn_cell->resn = RESN;
        esrn_cell->npa = NPA;
        esrn_cell->esgwri.len = esgwri.len;
        esrn_cell->esgwri.s = (char *) (esrn_cell + 1) + SRID.len;
        memcpy(esrn_cell->esgwri.s, esgwri.s, esgwri.len);       

        LM_DBG("-SRID %.*s \n", SRID.len, SRID.s);
        LM_DBG("-RESN %d \n", RESN);
        LM_DBG("-NPA %d \n", NPA);
        LM_DBG("-esgwri %.*s \n", esgwri.len, esgwri.s); 


        if (new_list != NULL) {
            new_list->next = esrn_cell;
            new_list = esrn_cell;
        } else {
            new_list = esrn_cell;
            init_esrn = new_list;
        }

    }

    new_list = init_esrn;


    lock_start_write(ref_lock);
    old_list = *db_esrn_esgwri;
    *db_esrn_esgwri = init_esrn;
    lock_stop_write(ref_lock);

    it = old_list;
    while (it) {
        aux = it;
        it = it->next;
        shm_free(aux);
    }

end:
    db_funcs.free_result(db_con, res);

    return 1;

}
Example #20
0
/*load rules from DB*/
int dp_load_db(dp_connection_list_p dp_conn)
{
	int i, nr_rows;
	db_res_t * res = 0;
	db_val_t * values;
	db_row_t * rows;
	db_key_t query_cols[DP_TABLE_COL_NO] = {
		&dpid_column,		&pr_column,
		&match_op_column,	&match_exp_column,	&match_flags_column,
		&subst_exp_column,	&repl_exp_column,	&attrs_column,	&timerec_column };
	db_key_t order = &pr_column;
	/* disabled condition */
	db_key_t cond_cols[1] = { &disabled_column };
	db_val_t cond_val[1];

	dpl_node_t *rule;
	int no_rows = 10;


	lock_start_write( dp_conn->ref_lock );

	if( dp_conn->crt_index != dp_conn->next_index){
		LM_WARN("a load command already generated, aborting reload...\n");
		lock_stop_write( dp_conn->ref_lock );
		return 0;
	}

	dp_conn->next_index = dp_conn->crt_index == 0 ? 1 : 0;

	lock_stop_write( dp_conn->ref_lock );

	if (dp_conn->dp_dbf.use_table(*dp_conn->dp_db_handle, &dp_conn->table_name) < 0){
		LM_ERR("error in use_table\n");
		goto err1;
	}

	VAL_TYPE(cond_val) = DB_INT;
	VAL_NULL(cond_val) = 0;
	VAL_INT(cond_val) = 0;

	if (DB_CAPABILITY(dp_conn->dp_dbf, DB_CAP_FETCH)) {
		if(dp_conn->dp_dbf.query(*dp_conn->dp_db_handle,cond_cols,
				0,cond_val,query_cols,1,
					DP_TABLE_COL_NO, order, 0) < 0){
			LM_ERR("failed to query database!\n");

			goto err1;
		}
		no_rows = estimate_available_rows( 4+4+4+64+4+64+64+128,
			DP_TABLE_COL_NO);
		if (no_rows==0) no_rows = 10;
		if(dp_conn->dp_dbf.fetch_result(*dp_conn->dp_db_handle,
						&res, no_rows)<0) {
			LM_ERR("failed to fetch\n");
			if (res)
				dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res);

			goto err1;
		}
	} else {
		/*select the whole table and all the columns*/
		if(dp_conn->dp_dbf.query(*dp_conn->dp_db_handle,
				cond_cols,0,cond_val,query_cols,1,
			DP_TABLE_COL_NO, order, &res) < 0){
				LM_ERR("failed to query database\n");

			goto err1;
		}
	}

	nr_rows = RES_ROW_N(res);



	if(nr_rows == 0){
		LM_WARN("no data in the db\n");
		goto end;
	}

	do {
		for(i=0; i<RES_ROW_N(res); i++){
			rows = RES_ROWS(res);
			values = ROW_VALUES(rows+i);

			if ((rule = build_rule(values)) == NULL) {
				LM_WARN(" failed to build rule -> skipping\n");
				continue;
			}

			rule->table_id = i;

			if(add_rule2hash(rule , dp_conn, dp_conn->next_index) != 0) {
				LM_ERR("add_rule2hash failed\n");
				goto err2;
			}
		}


		if (DB_CAPABILITY(dp_conn->dp_dbf, DB_CAP_FETCH)) {
			if(dp_conn->dp_dbf.fetch_result(*dp_conn->dp_db_handle,
							&res, no_rows)<0) {
				LM_ERR("failure while fetching!\n");
				if (res)
					dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res);
				goto err1;
			}
		} else {
			break;
		}
	}  while(RES_ROW_N(res)>0);


end:


	/*update data*/
	lock_start_write( dp_conn->ref_lock );

	destroy_hash(&dp_conn->hash[dp_conn->crt_index]);

	dp_conn->crt_index = dp_conn->next_index;

	lock_stop_write( dp_conn->ref_lock );

	list_hash(dp_conn->hash[dp_conn->crt_index], dp_conn->ref_lock);

	dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res);
	return 0;

err1:

	lock_start_write( dp_conn->ref_lock );

	dp_conn->next_index = dp_conn->crt_index;

	lock_stop_write( dp_conn->ref_lock );

	return -1;

err2:
	if(rule)	destroy_rule(rule);
	destroy_hash(&dp_conn->hash[dp_conn->next_index]);
	dp_conn->dp_dbf.free_result(*dp_conn->dp_db_handle, res);

	lock_start_write( dp_conn->ref_lock );

	dp_conn->next_index = dp_conn->crt_index;
	/* if lock defined - release the exclusive writing access */

	lock_stop_write( dp_conn->ref_lock );
	return -1;
}
Example #21
0
/* select data from emergency_service_prvider for put in xml to VPC
* coluns key: attribution and nodeIP.
* coluns attributs: OrganizationName, hostId, nenaId, contact, certUri. 
*/
int get_db_provider(str table_name, rw_lock_t *ref_lock ){
    db_key_t query_cols[] = {&id_col, &organizationName_col, &hostId_col, &nenaId_col, &contact_col, &certUri_col, &nodeIP_col, &attribution_col};
    db_res_t * res;
    db_val_t * values;
    db_row_t * rows;
    str OrganizationName;
    str hostId;
    str nenaId;
    str contact;
    str certUri;
    str nodeIP;
    int attribution;
    int nr_rows, i, size, id;
    struct service_provider *provider_cell, *old_list, *it, *aux, *new_list;
    struct service_provider *init_provider = NULL;

    db_funcs.use_table(db_con, &table_name);

    if (db_funcs.query(db_con, 0, 0, 0, query_cols, 0, 8, 0, &res) != 0) {
        LM_ERR("Failure to issue query\n");
        return -1;
    }

    nr_rows = RES_ROW_N(res);
    rows = RES_ROWS(res);
    new_list = NULL;
    LM_DBG("NUMBER OF LINES %d \n", nr_rows);

    for (i = 0; i < nr_rows; i++) {
        values = ROW_VALUES(rows + i);

        if (VAL_NULL(values) ||
                (VAL_TYPE(values) != DB_INT)) {
            LM_ERR("Invalid value returned 1\n");
            goto end;
        }

        id = VAL_INT(values);

        if (VAL_NULL(values + 1) ||
                (VAL_TYPE(values + 1) != DB_STR && VAL_TYPE(values + 1) != DB_STRING)) {
            LM_ERR("Invalid translated returned 2\n");
            goto end;
        }

        if (VAL_TYPE(values + 1) == DB_STR) {
            OrganizationName = VAL_STR(values + 1);
        } else {
            OrganizationName.s = (char*) VAL_STRING(values + 1);
            OrganizationName.len = strlen(OrganizationName.s);
        }

        if (VAL_NULL(values + 2) ||
                (VAL_TYPE(values + 2) != DB_STR && VAL_TYPE(values + 2) != DB_STRING)) {
            LM_ERR("Invalid translated returned 2\n");
            goto end;
        }

        if (VAL_TYPE(values + 2) == DB_STR) {
            hostId = VAL_STR(values + 2);
        } else {
            hostId.s = (char*) VAL_STRING(values + 2);
            hostId.len = strlen(hostId.s);
        }

        if (VAL_NULL(values + 3) ||
                (VAL_TYPE(values + 3) != DB_STR && VAL_TYPE(values + 3) != DB_STRING)) {
            LM_ERR("Invalid translated returned 3\n");
            goto end;
        }

        if (VAL_TYPE(values + 3) == DB_STR) {
            nenaId = VAL_STR(values + 3);
        } else {
            nenaId.s = (char*) VAL_STRING(values + 3);
            nenaId.len = strlen(nenaId.s);
        }

        if (VAL_NULL(values + 4) ||
                (VAL_TYPE(values + 4) != DB_STR && VAL_TYPE(values + 4) != DB_STRING)) {
            LM_ERR("Invalid translated returned 4\n");
            goto end;
        }

        if (VAL_TYPE(values + 4) == DB_STR) {
            contact = VAL_STR(values + 4);
        } else {
            contact.s = (char*) VAL_STRING(values + 4);
            contact.len = strlen(contact.s);
        }        

        if (VAL_NULL(values + 5) ||
                (VAL_TYPE(values + 5) != DB_STR && VAL_TYPE(values + 5) != DB_STRING)) {
            LM_ERR("Invalid translated returned 3\n");
            goto end;
        }

        if (VAL_TYPE(values + 5) == DB_STR) {
            certUri = VAL_STR(values + 5);
        } else {
            certUri.s = (char*) VAL_STRING(values + 5);
            certUri.len = strlen(certUri.s);
        }

        if (VAL_NULL(values + 6) ||
                (VAL_TYPE(values + 6) != DB_STR && VAL_TYPE(values + 6) != DB_STRING)) {
            LM_ERR("Invalid translated returned 6\n");
            goto end;
        }

        if (VAL_TYPE(values + 6) == DB_STR) {
            nodeIP = VAL_STR(values + 6);
        } else {
            nodeIP.s = (char*) VAL_STRING(values + 6);
            nodeIP.len = strlen(nodeIP.s);
        } 

        if (VAL_NULL(values + 7) ||
                (VAL_TYPE(values + 7) != DB_INT)) {
            LM_ERR("Invalid translated returned 7\n");
            goto end;
        }
        attribution = VAL_INT(values + 7);

        if (attribution == 0){
            if (hostId.len == 0 || contact.len == 0) {
                LM_ERR("source_hostname and source_contact are mandatory \n");
                mandatory_parm[0] = '1';
                mandatory_parm[1] = 0;
            } else{
                mandatory_parm[0] = '0';
                mandatory_parm[1] = 0;                
            }
        }       

        size = sizeof (struct service_provider)+ nodeIP.len + OrganizationName.len + hostId.len + nenaId.len + contact.len + certUri.len;
        provider_cell = shm_malloc(size);
        if (!provider_cell) {
            LM_ERR("no more shm\n");
            goto end;
        }

        memset(provider_cell, 0, size);

        provider_cell->nodeIP.len = nodeIP.len;
        provider_cell->nodeIP.s = (char *) (provider_cell + 1);
        memcpy(provider_cell->nodeIP.s, nodeIP.s, nodeIP.len);

        provider_cell->OrganizationName.len = OrganizationName.len;
        provider_cell->OrganizationName.s = (char *) (provider_cell + 1) + nodeIP.len;
        memcpy(provider_cell->OrganizationName.s, OrganizationName.s, OrganizationName.len);

        provider_cell->hostId.len = hostId.len;
        provider_cell->hostId.s = (char *) (provider_cell + 1) + nodeIP.len + OrganizationName.len;
        memcpy(provider_cell->hostId.s, hostId.s, hostId.len); 

        provider_cell->nenaId.len = nenaId.len;
        provider_cell->nenaId.s = (char *) (provider_cell + 1) + nodeIP.len + OrganizationName.len + hostId.len;
        memcpy(provider_cell->nenaId.s, nenaId.s, nenaId.len);

        provider_cell->contact.len = contact.len;
        provider_cell->contact.s = (char *) (provider_cell + 1) + nodeIP.len + OrganizationName.len + hostId.len + nenaId.len;
        memcpy(provider_cell->contact.s, contact.s, contact.len);  

        provider_cell->certUri.len = certUri.len;
        provider_cell->certUri.s = (char *) (provider_cell + 1) + nodeIP.len + OrganizationName.len + hostId.len + nenaId.len + contact.len;
        memcpy(provider_cell->certUri.s, certUri.s, certUri.len); 

        provider_cell->attribution = attribution;             
   
        if (new_list != NULL) {
            new_list->next = provider_cell;
            new_list = provider_cell;
        } else {
            new_list = provider_cell;
            init_provider = new_list;
        }       
    }
    
    new_list = init_provider;

    lock_start_write(ref_lock);
    old_list = *db_service_provider;
    *db_service_provider = init_provider;
    lock_stop_write(ref_lock);

    it = old_list;
    while (it) {
        aux = it;
        it = it->next;
        shm_free(aux);
    }

end:
    db_funcs.free_result(db_con, res);

    return 1;
}