route_struct.c
c07508e2
 /*
  * $Id$
  *
  * route structures helping functions
  */
 
 
 #include  "route_struct.h"
 
 #include <stdio.h>
3e429f5c
 #include <stdlib.h>
63fa628f
 #include <sys/types.h>
09e52ebb
 #include <netinet/in.h>
c07508e2
 
7268726e
 #include "dprint.h"
 
03150098
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
 #endif
 
c07508e2
 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
 {
 	struct expr * e;
 	e=(struct expr*)malloc(sizeof (struct expr));
 	if (e==0) goto error;
 	e->type=EXP_T;
 	e->op=op;
 	e->l.expr=left;
 	e->r.expr=right;
 	return e;
 error:
7268726e
 	LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
c07508e2
 	return 0;
 }
 
 
 struct expr* mk_elem(int op, int subtype, int operand, void* param)
 {
 	struct expr * e;
 	e=(struct expr*)malloc(sizeof (struct expr));
 	if (e==0) goto error;
 	e->type=ELEM_T;
 	e->op=op;
 	e->subtype=subtype;
 	e->l.operand=operand;
 	e->r.param=param;
 	return e;
 error:
7268726e
 	LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
c07508e2
 	return 0;
 }
 
 
 
 struct action* mk_action(int type, int p1_type, int p2_type, void* p1, void* p2)
 {
 	struct action* a;
 	a=(struct action*)malloc(sizeof(struct action));
 	if (a==0) goto  error;
 	a->type=type;
 	a->p1_type=p1_type;
 	a->p2_type=p2_type;
 	a->p1.string=(char*) p1;
 	a->p2.string=(char*) p2;
 	a->next=0;
 	return a;
 	
 error:
7268726e
 	LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
c07508e2
 	return 0;
 
 }
 
 
 struct action* append_action(struct action* a, struct action* b)
 {
 	struct action *t;
 	if (b==0) return a;
 	if (a==0) return b;
 	
 	for(t=a;t->next;t=t->next);
 	t->next=b;
 	return a;
 }
 
09e52ebb
 
 
 struct net* mk_net(unsigned long ip, unsigned long mask)
 {
 	struct net* n;
 
 	n=(struct net*)malloc(sizeof(struct net));
 	if (n==0) goto error;
 	n->ip=ip;
 	n->mask=mask;
 	return n;
 error:
7268726e
 	LOG(L_CRIT, "ERROR: mk_net_mask: memory allocation failure\n");
09e52ebb
 	return 0;
 }
 
c07508e2
 	
 	
09e52ebb
 
 void print_ip(unsigned ip)
 {
7268726e
 	DBG("%d.%d.%d.%d", ((unsigned char*)&ip)[0],
09e52ebb
 						  ((unsigned char*)&ip)[1],
 						  ((unsigned char*)&ip)[2],
 						  ((unsigned char*)&ip)[3]);
 }
 
 
 void print_net(struct net* net)
 {
 	if (net==0){
7268726e
 		LOG(L_WARN, "ERROR: print net: null pointer\n");
09e52ebb
 		return;
 	}
7268726e
 	print_ip(net->ip); DBG("/"); print_ip(net->mask);
09e52ebb
 }
 
 
 
c07508e2
 void print_expr(struct expr* exp)
 {
 	if (exp==0){
7268726e
 		LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
c07508e2
 		return;
 	}
 	if (exp->type==ELEM_T){
 		switch(exp->l.operand){
 			case METHOD_O:
7268726e
 				DBG("method");
c07508e2
 				break;
 			case URI_O:
7268726e
 				DBG("uri");
c07508e2
 				break;
 			case SRCIP_O:
7268726e
 				DBG("srcip");
c07508e2
 				break;
 			case DSTIP_O:
7268726e
 				DBG("dstip");
c07508e2
 				break;
 			default:
7268726e
 				DBG("UNKNOWN");
c07508e2
 		}
 		switch(exp->op){
 			case EQUAL_OP:
7268726e
 				DBG("==");
c07508e2
 				break;
 			case MATCH_OP:
7268726e
 				DBG("=~");
09e52ebb
 				break;
c07508e2
 			default:
7268726e
 				DBG("<UNKNOWN>");
c07508e2
 		}
 		switch(exp->subtype){
 			case NOSUBTYPE: 
7268726e
 					DBG("N/A");
c07508e2
 					break;
 			case STRING_ST:
7268726e
 					DBG("\"%s\"", (char*)exp->r.param);
c07508e2
 					break;
 			case NET_ST:
09e52ebb
 					print_net((struct net*)exp->r.param);
 					break;
 			case IP_ST:
 					print_ip(exp->r.intval);
c07508e2
 					break;
 			default:
7268726e
 					DBG("type<%d>", exp->subtype);
c07508e2
 		}
 	}else if (exp->type==EXP_T){
 		switch(exp->op){
 			case AND_OP:
7268726e
 					DBG("AND( ");
c07508e2
 					print_expr(exp->l.expr);
7268726e
 					DBG(", ");
c07508e2
 					print_expr(exp->r.expr);
7268726e
 					DBG(" )");
c07508e2
 					break;
 			case OR_OP:
7268726e
 					DBG("OR( ");
c07508e2
 					print_expr(exp->l.expr);
7268726e
 					DBG(", ");
c07508e2
 					print_expr(exp->r.expr);
7268726e
 					DBG(" )");
c07508e2
 					break;
 			case NOT_OP:	
7268726e
 					DBG("NOT( ");
c07508e2
 					print_expr(exp->l.expr);
7268726e
 					DBG(" )");
c07508e2
 					break;
 			default:
7268726e
 					DBG("UNKNOWN_EXP ");
c07508e2
 		}
 					
 	}else{
7268726e
 		DBG("ERROR:print_expr: unknown type\n");
c07508e2
 	}
 }
 					
 
 					
 
 void print_action(struct action* a)
 {
 	struct action* t;
 	for(t=a; t!=0;t=t->next){
 		switch(t->type){
 			case FORWARD_T:
7268726e
 					DBG("forward(");
c07508e2
 					break;
 			case SEND_T:
7268726e
 					DBG("send(");
c07508e2
 					break;
 			case DROP_T:
7268726e
 					DBG("drop(");
c07508e2
 					break;
 			case LOG_T:
7268726e
 					DBG("log(");
c07508e2
 					break;
 			case ERROR_T:
7268726e
 					DBG("error(");
c07508e2
 					break;
a15c363f
 			case ROUTE_T:
7268726e
 					DBG("route(");
a15c363f
 					break;
 			case EXEC_T:
7268726e
 					DBG("exec(");
 					break;
 			case SET_HOST_T:
 					DBG("sethost(");
 					break;
 			case SET_HOSTPORT_T:
 					DBG("sethostport(");
 					break;
 			case SET_USER_T:
 					DBG("setuser(");
 					break;
 			case SET_USERPASS_T:
 					DBG("setuserpass(");
 					break;
 			case SET_PORT_T:
 					DBG("setport(");
 					break;
 			case SET_URI_T:
 					DBG("seturi(");
a15c363f
 					break;
c07508e2
 			default:
7268726e
 					DBG("UNKNOWN(");
c07508e2
 		}
 		switch(t->p1_type){
 			case STRING_ST:
7268726e
 					DBG("\"%s\"", t->p1.string);
c07508e2
 					break;
 			case NUMBER_ST:
7268726e
 					DBG("%d",t->p1.number);
c07508e2
 					break;
a15c363f
 			case IP_ST:
4ac74c03
 					print_ip(t->p1.number);
a15c363f
 					break;
c07508e2
 			default:
7268726e
 					DBG("type<%d>", t->p1_type);
c07508e2
 		}
 		switch(t->p2_type){
 			case NOSUBTYPE:
 					break;
 			case STRING_ST:
7268726e
 					DBG(", \"%s\"", t->p2.string);
c07508e2
 					break;
 			case NUMBER_ST:
7268726e
 					DBG(", %d",t->p2.number);
c07508e2
 					break;
 			default:
7268726e
 					DBG(", type<%d>", t->p2_type);
c07508e2
 		}
7268726e
 		DBG("); ");
c07508e2
 	}
 }