Exemplo n.º 1
0
int pdt_print_mi_node(pdt_node_t *pt, struct mi_node* rpl, char *code,
		int len, str *sdomain, str *sd, str *sp)
{
	int i;
	struct mi_node* node = NULL;
	struct mi_attr* attr= NULL;
	str *cl;

	if(pt==NULL || len>=PDT_MAX_DEPTH)
		return 0;
	
	cl = pdt_get_char_list();

	for(i=0; i<cl->len; i++)
	{
		code[len]=cl->s[i];
		if(pt[i].domain.s!=NULL)
		{
			if((sp->s==NULL && sd->s==NULL)
				|| (sp->s==NULL && (sd->s!=NULL && pt[i].domain.len==sd->len
						&& strncasecmp(pt[i].domain.s, sd->s, sd->len)==0)) 
				|| (sd->s==NULL && (len+1>=sp->len
						&& strncmp(code, sp->s, sp->len)==0))
				|| ((sp->s!=NULL && len+1>=sp->len
						&& strncmp(code, sp->s, sp->len)==0)
						&& (sd->s!=NULL && pt[i].domain.len>=sd->len
						&& strncasecmp(pt[i].domain.s, sd->s, sd->len)==0)))
			{
				node = add_mi_node_child(rpl, 0, "PDT", 3, 0, 0);
				if(node == NULL)
					goto error;

				attr = add_mi_attr(node, MI_DUP_VALUE, "SDOMAIN", 7,
						sdomain->s, sdomain->len);
				if(attr == NULL)
					goto error;
				attr = add_mi_attr(node, MI_DUP_VALUE, "PREFIX", 6,
							code, len+1);
				if(attr == NULL)
					goto error;
						
				attr = add_mi_attr(node, MI_DUP_VALUE,"DOMAIN", 6,
							pt[i].domain.s, pt[i].domain.len);
				if(attr == NULL)
					goto error;
			}
		}
		if(pdt_print_mi_node(pt[i].child, rpl, code, len+1, sdomain, sd, sp)<0)
			goto error;
	}
	return 0;
error:
	return -1;
}
Exemplo n.º 2
0
int pdt_rpc_print_node(rpc_t* rpc, void* ctx, void *ih, pdt_node_t *pt, char *code,
		int len, str *sdomain, str *tdomain, str *tprefix)
{
	int i;
	str *cl;
	str prefix;
	void* vh;

	if(pt==NULL || len>=PDT_MAX_DEPTH)
		return 0;
	
	cl = pdt_get_char_list();

	for(i=0; i<cl->len; i++)
	{
		code[len]=cl->s[i];
		if(pt[i].domain.s!=NULL)
		{
			if((tprefix->s==NULL && tdomain->s==NULL)
				|| (tprefix->s==NULL && (tdomain->s!=NULL && pt[i].domain.len==tdomain->len
						&& strncasecmp(pt[i].domain.s, tdomain->s, tdomain->len)==0))
				|| (tdomain->s==NULL && (len+1>=tprefix->len
						&& strncmp(code, tprefix->s, tprefix->len)==0))
				|| ((tprefix->s!=NULL && len+1>=tprefix->len
						&& strncmp(code, tprefix->s, tprefix->len)==0)
						&& (tdomain->s!=NULL && pt[i].domain.len>=tdomain->len
						&& strncasecmp(pt[i].domain.s, tdomain->s, tdomain->len)==0)))
			{
				if(rpc->struct_add(ih, "{",
						"ENTRY", &vh)<0)
				{
					LM_ERR("Internal error creating entry\n");
					return -1;
				}
				prefix.s = code;
				prefix.len = len + 1;
				if(rpc->struct_add(vh, "SS",
						"DOMAIN", &pt[i].domain,
						"PREFIX", &prefix)<0)
				{
					LM_ERR("Internal error filling entry struct\n");
					return -1;
				}
			}
		}
		if(pdt_rpc_print_node(rpc, ctx, ih, pt[i].child, code, len+1, sdomain,
					tdomain, tprefix)<0)
			goto error;
	}
	return 0;
error:
	return -1;
}
Exemplo n.º 3
0
/**
 * "pdt.list" parameters:
 *    sdomain
 *    prefix
 *    domain
 *
 * 	- '.' (dot) means NULL value and will match anything
 * 	- the comparison operation is 'START WITH' -- if domain is 'a' then
 * 	  all domains starting with 'a' are listed
 *
 * 	  Examples
 * 	  pdt_list o 2 .    - lists the entries where sdomain is starting with 'o', 
 * 	                      prefix is starting with '2' and domain is anything
 * 	  
 * 	  pdt_list . 2 open - lists the entries where sdomain is anything, prefix 
 * 	                      starts with '2' and domain starts with 'open'
 */
static void pdt_rpc_list(rpc_t* rpc, void* ctx)
{
	str sdomain = {0};
	str tprefix = {0};
	str tdomain = {0};
	pdt_tree_t *pt;
	unsigned int i;
	static char code_buf[PDT_MAX_DEPTH+1];
	int len;
	str *cl;
	pdt_tree_t **ptree;
	void* th;
	void* ih;

	ptree = pdt_get_ptree();

	if(ptree==NULL || *ptree==NULL)
	{
		LM_ERR("empty domain list\n");
		rpc->fault(ctx, 404, "No records");
		return;
	}

	len = rpc->scan(ctx, "*S.SS", &sdomain, &tprefix, &tdomain);
	if(len<0)
	{
		rpc->fault(ctx, 500, "Error Reading Parameters");
		return;
	}
	if(len<1 || sdomain.len==0 || (sdomain.len==1 && sdomain.s[0]=='.')) {
		sdomain.len = 0;
		sdomain.s = 0;
	}
	cl = pdt_get_char_list();
	if(len<2 || tprefix.len==0 || (tprefix.len==1 && tprefix.s[0]=='.')) {
		tprefix.len = 0;
		tprefix.s = 0;
	} else if(tprefix.len>0) {
		/* validate prefix */
		i = 0;
		while(tprefix.s!=NULL && i!=tprefix.len)
		{
			if(strpos(cl->s, tprefix.s[i]) < 0)
			{
				LM_ERR("bad prefix [%.*s]\n", tprefix.len, tprefix.s);
				rpc->fault(ctx, 400, "Bad Prefix");
				return;
			}
			i++;
		}
	}
	if(len<3 || tdomain.len==0 || (tdomain.len==1 && tdomain.s[0]=='.')) {
		tdomain.len = 0;
		tdomain.s = 0;
	}

	pt = *ptree;
	
	if (rpc->add(ctx, "{", &th) < 0)
	{
		rpc->fault(ctx, 500, "Internal error root reply");
		return;
	}
	while(pt!=NULL)
	{
		LM_ERR("---- 1 (%d [%.*s])\n", sdomain.len, sdomain.len, sdomain.s);
		if(sdomain.s==NULL || 
			(sdomain.s!=NULL && pt->sdomain.len>=sdomain.len && 
			 strncmp(pt->sdomain.s, sdomain.s, sdomain.len)==0))
		{
		LM_ERR("---- 2\n");
			len = 0;
			if(rpc->struct_add(th, "S{",
					"SDOMAIN", &pt->sdomain,
					"RECORDS",  &ih)<0)
			{
				rpc->fault(ctx, 500, "Internal error creating sdomain structure");
				return;
			}
			if(pdt_rpc_print_node(rpc, ctx, ih, pt->head, code_buf, len, &pt->sdomain,
						&tdomain, &tprefix)<0)
				goto error;
		}
		pt = pt->next;
	}
	return;
error:
	rpc->fault(ctx, 500, "Internal error printing records");
	return;
}
Exemplo n.º 4
0
struct mi_root* pdt_mi_list(struct mi_root* cmd_tree, void* param)
{
	str sd, sp, sdomain;
	pdt_tree_t *pt;
	struct mi_node* node = NULL;
	unsigned int i= 0;
	struct mi_root* rpl_tree = NULL;
	struct mi_node* rpl = NULL;
	static char code_buf[PDT_MAX_DEPTH+1];
	int len;
	str *cl;
	pdt_tree_t **ptree;

	ptree = pdt_get_ptree();

	if(ptree==NULL)
	{
		LM_ERR("empty domain list\n");
		return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
	}

	cl = pdt_get_char_list();

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

		if(*sdomain.s=='.')
			sdomain.s = 0;

		/* read prefix */
		node = node->next;
		if(node != NULL)
		{
			sp= node->value;
			if(sp.s== NULL || sp.len==0 || *sp.s=='.')
				sp.s = NULL;
			else {
				while(sp.s!=NULL && i!=sp.len)
				{
					if(strpos(cl->s, sp.s[i]) < 0)
					{
						LM_ERR("bad prefix [%.*s]\n", sp.len, sp.s);
						return init_mi_tree( 400, "bad prefix", 10);
					}
					i++;
				}
			}

			/* read domain */
			node= node->next;
			if(node != NULL)
			{
				sd= node->value;
				if(sd.s== NULL || sd.len==0 || *sd.s=='.')
					sd.s = NULL;
			}
		}
	}

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

	if(*ptree==0)
		return rpl_tree;

	pt = *ptree;
	
	while(pt!=NULL)
	{
		if(sdomain.s==NULL || 
			(sdomain.s!=NULL && pt->sdomain.len>=sdomain.len && 
			 strncmp(pt->sdomain.s, sdomain.s, sdomain.len)==0))
		{
			len = 0;
			if(pdt_print_mi_node(pt->head, rpl, code_buf, len, &pt->sdomain,
						&sd, &sp)<0)
				goto error;
		}
		pt = pt->next;
	}
	
	return rpl_tree;

error:
	free_mi_tree(rpl_tree);
	return 0;
}