Example #1
0
void rpc_mtree_reload(rpc_t* rpc, void* c)
{
	str tname = {0, 0};
	m_tree_t *pt = NULL;
	int treloaded = 0;

	if(db_table.len>0)
	{
		/* re-loading all information from database */
		if(mt_load_db_trees()!=0)
		{
			LM_ERR("cannot re-load mtrees from database\n");
			goto error;
		}
	} else {
		if(!mt_defined_trees())
		{
			LM_ERR("empty mtree list\n");
			goto error;
		}

		/* read tree name */
		if (rpc->scan(c, "S", &tname) != 1) {
			tname.s = 0;
			tname.len = 0;
		} else {
			if(*tname.s=='.') {
				tname.s = 0;
				tname.len = 0;
			}
		}

		pt = mt_get_first_tree();

		while(pt!=NULL)
		{
			if(tname.s==NULL
					|| (tname.s!=NULL && pt->tname.len>=tname.len
						&& strncmp(pt->tname.s, tname.s, tname.len)==0))
			{
				/* re-loading table from database */
				if(mt_load_db(pt)!=0)
				{
					LM_ERR("cannot re-load mtree from database\n");
					goto error;
				}
				treloaded = 1;
			}
			pt = pt->next;
		}
		if(treloaded == 0) {
			rpc->fault(c, 500, "No Mtree Name Matching");
		}
	}

	return;

error:
	rpc->fault(c, 500, "Mtree Reload Failed");
}
Example #2
0
/**
 * "mt_reload" syntax :
 * \n
 */
static struct mi_root* mt_mi_reload(struct mi_root *cmd_tree, void *param)
{
	str tname = {0, 0};
	m_tree_t *pt;
	struct mi_node* node = NULL;

	if(db_table.len>0)
	{
		/* re-loading all information from database */
		if(mt_load_db_trees()!=0)
		{
			LM_ERR("cannot re-load info from database\n");
			goto error;
		}
	} else {
		if(!mt_defined_trees())
		{
			LM_ERR("empty tree list\n");
			return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
		}

		/* read tree name */
		node = cmd_tree->node.kids;
		if(node != NULL)
		{
			tname = node->value;
			if(tname.s == NULL || tname.len== 0)
				return init_mi_tree( 404, "domain not found", 16);

			if(*tname.s=='.') {
				tname.s = 0;
				tname.len = 0;
			}
		}

		pt = mt_get_first_tree();

		while(pt!=NULL)
		{
			if(tname.s==NULL
					|| (tname.s!=NULL && pt->tname.len>=tname.len
						&& strncmp(pt->tname.s, tname.s, tname.len)==0))
			{
				/* re-loading table from database */
				if(mt_load_db(pt)!=0)
				{
					LM_ERR("cannot re-load info from database\n");	
					goto error;
				}
			}
			pt = pt->next;
		}
	}

	return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);

error:
	return init_mi_tree( 500, "Failed to reload",16);
}
Example #3
0
struct mi_root* mt_mi_summary(struct mi_root* cmd_tree, void* param)
{
	m_tree_t *pt;
	struct mi_root* rpl_tree = NULL;
	struct mi_node* node = NULL;
	struct mi_attr* attr= NULL;
	str val;

	if(!mt_defined_trees())
	{
		LM_ERR("empty tree list\n");
		return init_mi_tree( 500, "No trees", 8);
	}

	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
	if(rpl_tree == NULL)
		return 0;

	pt = mt_get_first_tree();

	while(pt!=NULL)
	{
		node = add_mi_node_child(&rpl_tree->node, 0, "MT", 2, 0, 0);
		if(node == NULL)
			goto error;
		attr = add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
				pt->tname.s, pt->tname.len);
		if(attr == NULL)
			goto error;
		val.s = int2str(pt->type, &val.len);
		attr = add_mi_attr(node, MI_DUP_VALUE, "TTYPE", 5,
				val.s, val.len);
		if(attr == NULL)
			goto error;
		val.s = int2str(pt->memsize, &val.len);
		attr = add_mi_attr(node, MI_DUP_VALUE, "MEMSIZE", 7,
				val.s, val.len);
		if(attr == NULL)
			goto error;
		val.s = int2str(pt->nrnodes, &val.len);
		attr = add_mi_attr(node, MI_DUP_VALUE, "NRNODES", 7,
				val.s, val.len);
		if(attr == NULL)
			goto error;
		val.s = int2str(pt->nritems, &val.len);
		attr = add_mi_attr(node, MI_DUP_VALUE, "NRITEMS", 7,
				val.s, val.len);
		if(attr == NULL)
			goto error;

		pt = pt->next;
	}

	return rpl_tree;
error:
	free_mi_tree(rpl_tree);
	return 0;
}
Example #4
0
struct mi_root* mt_mi_list(struct mi_root* cmd_tree, void* param)
{
	str tname = {0, 0};
	m_tree_t *pt;
	struct mi_node* node = NULL;
	struct mi_root* rpl_tree = NULL;
	struct mi_node* rpl = NULL;
	static char code_buf[MT_MAX_DEPTH+1];
	int len;

	if(!mt_defined_trees())
	{
		LM_ERR("empty tree list\n");
		return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
	}

	/* read tree name */
	node = cmd_tree->node.kids;
	if(node != NULL)
	{
		tname = node->value;
		if(tname.s == NULL || tname.len== 0)
			return init_mi_tree( 404, "domain not found", 16);

		if(*tname.s=='.') {
			tname.s = 0;
			tname.len = 0;
		}
	}

	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
	if(rpl_tree == NULL)
		return 0;
	rpl = &rpl_tree->node;

	pt = mt_get_first_tree();

	while(pt!=NULL)
	{
		if(tname.s==NULL || 
				(tname.s!=NULL && pt->tname.len>=tname.len && 
				 strncmp(pt->tname.s, tname.s, tname.len)==0))
		{
			len = 0;
			if(mt_print_mi_node(pt, pt->head, rpl, code_buf, len)<0)
				goto error;
		}
		pt = pt->next;
	}

	return rpl_tree;

error:
	free_mi_tree(rpl_tree);
	return 0;
}
Example #5
0
void rpc_mtree_summary(rpc_t* rpc, void* c) 
{
	m_tree_t *pt;
	void* th;
	void* ih;

	if(!mt_defined_trees())
	{
		rpc->fault(c, 500, "Empty tree list.");
		return;
	}

	if (rpc->add(c, "{", &th) < 0)
	{
		rpc->fault(c, 500, "Internal error creating rpc");
		return;
	}
	pt = mt_get_first_tree();

	while(pt!=NULL)
	{
		if(rpc->struct_add(th, "s{",
					"table", pt->tname.s,
					"item", &ih) < 0)
		{
			rpc->fault(c, 500, "Internal error creating rpc ih");
			return;
		}

		if(rpc->struct_add(ih, "d", "ttype", pt->type) < 0 ) {
			rpc->fault(c, 500, "Internal error adding type");
			return;
		}
		if(rpc->struct_add(ih, "d", "memsize", pt->memsize) < 0 ) {
			rpc->fault(c, 500, "Internal error adding memsize");
			return;
		}
		if(rpc->struct_add(ih, "d", "nrnodes", pt->nrnodes) < 0 ) {
			rpc->fault(c, 500, "Internal error adding nodes");
			return;
		}
		if(rpc->struct_add(ih, "d", "nritems", pt->nritems) < 0 ) {
			rpc->fault(c, 500, "Internal error adding items");
			return;
		}
		pt = pt->next;
	}
	return;
}
Example #6
0
/**
 * "mtree.list" syntax :
 *    tname
 *
 * 	- '.' (dot) means NULL value and will match anything
 */
void rpc_mtree_list(rpc_t* rpc, void* ctx)
{
	str tname = {0, 0};
	m_tree_t *pt;
	static char code_buf[MT_MAX_DEPTH+1];
	int len;

	if(!mt_defined_trees())
	{
		rpc->fault(ctx, 500, "Empty tree list.");
		return;
	}

	if(rpc->scan(ctx, "*.S", &tname)!=1) {
		tname.s = NULL;
		tname.len = 0;
	}

	pt = mt_get_first_tree();

	while(pt!=NULL)
	{
		if(tname.s==NULL ||
				(tname.s!=NULL && pt->tname.len>=tname.len &&
					strncmp(pt->tname.s, tname.s, tname.len)==0))
		{
			len = 0;
			code_buf[0] = '\0';
			if(rpc_mtree_print_node(rpc, ctx, pt, pt->head, code_buf, len)<0) {
				goto error;
			}
		}
		pt = pt->next;
	}

	return;

error:
	LM_ERR("failed to build rpc response\n");
	return;
}
Example #7
0
/* RPC commands */
void rpc_mtree_summary(rpc_t* rpc, void* c)
{
	str tname = {0, 0};
	m_tree_t *pt;
	void* th;
	void* ih;
	int found;

	if(!mt_defined_trees())
	{
		rpc->fault(c, 500, "Empty tree list");
		return;
	}

	/* read optional tree name */
	if(rpc->scan(c, "*S", &tname)==0)
	{
		tname.s = NULL;
		tname.len = 0;
	}

	pt = mt_get_first_tree();
	if(pt==NULL)
	{
		rpc->fault(c, 404, "No tree");
		return;
	}

	found = 0;
	while(pt!=NULL)
	{
		if(tname.s==NULL
				|| (tname.s!=NULL && pt->tname.len>=tname.len
					&& strncmp(pt->tname.s, tname.s, tname.len)==0))
		{
			found = 1;
			if (rpc->add(c, "{", &th) < 0)
			{
				rpc->fault(c, 500, "Internal error creating rpc");
				return;
			}
			if(rpc->struct_add(th, "s{",
						"table", pt->tname.s,
						"item", &ih) < 0)
			{
				rpc->fault(c, 500, "Internal error creating rpc ih");
				return;
			}
			if(rpc->struct_add(ih, "d", "ttype", pt->type) < 0 ) {
				rpc->fault(c, 500, "Internal error adding type");
				return;
			}
			if(rpc->struct_add(ih, "d", "memsize", pt->memsize) < 0 ) {
				rpc->fault(c, 500, "Internal error adding memsize");
				return;
			}
			if(rpc->struct_add(ih, "d", "nrnodes", pt->nrnodes) < 0 ) {
				rpc->fault(c, 500, "Internal error adding nodes");
				return;
			}
			if(rpc->struct_add(ih, "d", "nritems", pt->nritems) < 0 ) {
				rpc->fault(c, 500, "Internal error adding items");
				return;
			}
			if(rpc->struct_add(ih, "d", "reload_count",
						(int)pt->reload_count) < 0 ) {
				rpc->fault(c, 500, "Internal error adding items");
				return;
			}
			if(rpc->struct_add(ih, "d", "reload_time",
						(int)pt->reload_time) < 0 ) {
				rpc->fault(c, 500, "Internal error adding items");
				return;
			}
		}
		pt = pt->next;
	}

	if(found==0)
	{
		rpc->fault(c, 404, "Tree not found");
		return;
	}

	return;
}
Example #8
0
/**
 * init module function
 */
static int mod_init(void)
{
	m_tree_t *pt = NULL;

	if(mtree_init_rpc()!=0)
	{
		LM_ERR("failed to register RPC commands\n");
		return -1;
	}

	if(pv_parse_spec(&value_param, &pv_value)<00
			|| !(pv_is_w(&pv_value)))
	{
		LM_ERR("cannot parse value pv or is read only\n");
		return -1;
	}

	if (pv_parse_spec(&values_param, &pv_values) <0
			|| pv_values.type != PVT_AVP) {
		LM_ERR("cannot parse values avp\n");
		return -1;
	}

	if(pv_parse_spec(&dstid_param, &pv_dstid)<0
			|| pv_dstid.type!=PVT_AVP)
	{
		LM_ERR("cannot parse dstid avp\n");
		return -1;
	}

	if(pv_parse_spec(&weight_param, &pv_weight)<0
			|| pv_weight.type!=PVT_AVP)
	{
		LM_ERR("cannot parse dstid avp\n");
		return -1;
	}

	if(pv_parse_spec(&count_param, &pv_count)<0
			|| !(pv_is_w(&pv_weight)))
	{
		LM_ERR("cannot parse count pv or is read-only\n");
		return -1;
	}

	if(mt_fetch_rows<=0)
		mt_fetch_rows = 1000;

	if(mt_char_list.len<=0)
	{
		LM_ERR("invalid prefix char list\n");
		return -1;
	}
	LM_DBG("mt_char_list=%s \n", mt_char_list.s);
	mt_char_table_init();

	/* binding to mysql module */
	if(db_bind_mod(&db_url, &mt_dbf))
	{
		LM_ERR("database module not found\n");
		return -1;
	}

	if (!DB_CAPABILITY(mt_dbf, DB_CAP_ALL))
	{
		LM_ERR("database module does not "
				"implement all functions needed by the module\n");
		return -1;
	}

	/* open a connection with the database */
	db_con = mt_dbf.init(&db_url);
	if(db_con==NULL)
	{
		LM_ERR("failed to connect to the database\n");
		return -1;
	}

	LM_DBG("database connection opened successfully\n");

	if ( (mt_lock=lock_alloc())==0) {
		LM_CRIT("failed to alloc lock\n");
		goto error1;
	}
	if (lock_init(mt_lock)==0 ) {
		LM_CRIT("failed to init lock\n");
		goto error1;
	}

	if(mt_defined_trees())
	{
		LM_DBG("static trees defined\n");

		pt = mt_get_first_tree();

		while(pt!=NULL)
		{
			LM_DBG("loading from tree <%.*s>\n",
					pt->tname.len, pt->tname.s);

			/* loading all information from database */
			if(mt_load_db(pt)!=0)
			{
				LM_ERR("cannot load info from database\n");
				goto error1;
			}
			pt = pt->next;
		}
	} else {
		if(db_table.len<=0)
		{
			LM_ERR("no trees table defined\n");
			goto error1;
		}
		if(mt_init_list_head()<0)
		{
			LM_ERR("unable to init trees list head\n");
			goto error1;
		}
		/* loading all information from database */
		if(mt_load_db_trees()!=0)
		{
			LM_ERR("cannot load trees from database\n");
			goto error1;
		}
	}
	mt_dbf.close(db_con);
	db_con = 0;

#if 0
	mt_print_tree(mt_get_first_tree());
#endif

	/* success code */
	return 0;

error1:
	if (mt_lock)
	{
		lock_destroy( mt_lock );
		lock_dealloc( mt_lock );
		mt_lock = 0;
	}
	mt_destroy_trees();

	if(db_con!=NULL)
		mt_dbf.close(db_con);
	db_con = 0;
	return -1;
}