浏览代码

- finally a working version

Andrei Pelinescu-Onciul 24 年之前
父节点
当前提交
3e429f5c5a
共有 18 个文件被更改,包括 201 次插入248 次删除
  1. 5 4
      Makefile
  2. 68 11
      action.c
  3. 1 1
      cfg.lex
  4. 40 16
      cfg.y
  5. 0 129
      cfg_parser.c
  6. 0 30
      cfg_parser.h
  7. 3 0
      config.h
  8. 2 0
      error.h
  9. 15 10
      forward.c
  10. 1 2
      forward.h
  11. 16 6
      main.c
  12. 1 1
      msg_parser.c
  13. 5 3
      proxy.c
  14. 3 1
      receive.c
  15. 27 25
      route.c
  16. 2 2
      route_struct.c
  17. 11 7
      udp_server.c
  18. 1 0
      udp_server.h

+ 5 - 4
Makefile

@@ -13,13 +13,13 @@ NAME=sip_router
 
 
 CC=gcc
-CFLAGS=-O2
+CFLAGS=-O2 -Wcast-align  #-Wmissing-prototypes  -Wall
 LEX=lex
 YACC=bison
 YACC_FLAGS=-d
 # on linux and freebsd keep it empty (e.g. LIBS= )
 # on solaris add -lxnet (e.g. LIBS= -lxnet)
-LIBS=
+LIBS=-lfl
 ALLDEP=Makefile
 
 MKDEP=gcc -M
@@ -35,14 +35,15 @@ MKDEP=gcc -M
 	$(MKDEP) $< >$@
 
 # normal rules
+$(NAME): $(objs)
+	$(CC) $(CFLAGS) $(objs) -o $(NAME) $(LIBS)
+
 lex.yy.c: cfg.lex $(ALLDEP)
 	$(LEX) $<
 
 cfg.tab.c: cfg.y
 	$(YACC) $(YACC_FLAGS) $<
 
-$(NAME): $(objs)
-	$(CC) $(CFLAGS) $(LIBS) $(objs) -o $(NAME)
 
 .PHONY: all
 all: $(NAME)

+ 68 - 11
action.c

@@ -9,6 +9,9 @@
 #include "error.h"
 #include "dprint.h"
 #include "proxy.h"
+#include "forward.h"
+#include "udp_server.h"
+#include "route.h"
 
 #include <netdb.h>
 #include <stdlib.h>
@@ -20,14 +23,17 @@ int do_action(struct action* a, struct sip_msg* msg)
 	int ret;
 	struct sockaddr_in* to;
 	struct proxy_l* p;
+	struct route_elem* re;
 
+	ret=E_BUG;
 	switch (a->type){
 		case DROP_T:
 				ret=0;
 			break;
 		case FORWARD_T:
-			if (a->p1_type!= PROXY_ST){
-				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
+			if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
+				LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
+						a->p1_type, a->p2_type);
 				ret=E_BUG;
 				break;
 			}
@@ -42,8 +48,9 @@ int do_action(struct action* a, struct sip_msg* msg)
 				ret=E_OUT_OF_MEM;
 				break;
 			}
-			if (a->p1_type!= PROXY_ST){
-				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
+			if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
+				LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
+						a->p1_type, a->p2_type);
 				ret=E_BUG;
 				break;
 			}
@@ -60,7 +67,8 @@ int do_action(struct action* a, struct sip_msg* msg)
 			to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
 			p->tx++;
 			p->tx_bytes+=msg->len;
-			ret=udp_send(msg->orig, msg->len, to, sizeof(struct sockaddr));
+			ret=udp_send(msg->orig, msg->len, (struct sockaddr*)to,
+					sizeof(struct sockaddr_in));
 			free(to);
 			if (ret<0){
 				p->errors++;
@@ -69,20 +77,56 @@ int do_action(struct action* a, struct sip_msg* msg)
 			
 			break;
 		case LOG_T:
-			LOG(a->p2.number, a->p1.string);
+			if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){
+				LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
+						a->p1_type, a->p2_type);
+				ret=E_BUG;
+				break;
+			}
+			LOG(a->p1.number, a->p2.string);
 			ret=1;
 			break;
 		case ERROR_T:
+			if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){
+				LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
+						a->p1_type, a->p2_type);
+				ret=E_BUG;
+				break;
+			}
 			LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
 					"not implemented yet\n", a->p1.string, a->p2.string);
 			ret=1;
 			break;
 		case ROUTE_T:
-			LOG(L_NOTICE, "WARNING: do_action: route(%d) not implemented "
-							"yet\n", a->p1.number);
+			if (a->p1_type!=NUMBER_ST){
+				LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
+						a->p1_type);
+				ret=E_BUG;
+				break;
+			}
+			if ((a->p1.number>RT_NO)||(a->p1.number<0)){
+				LOG(L_ERR, "ERROR: invalid routing table number in"
+							"route(%d)\n", a->p1.number);
+				ret=E_CFG;
+				break;
+			}
+			re=route_match(msg, &rlist[a->p1.number]);
+			if (re==0){
+				LOG(L_INFO, "WARNING: do_action: route(%d): no new route"
+						" found\n", a->p1.number);
+				ret=1;
+				break;
+			}
+			ret=((ret=run_actions(re->actions, msg))<0)?ret:1;
 			break;
 		case EXEC_T:
-			LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,",
+			if (a->p1_type!=STRING_ST){
+				LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
+						a->p1_type);
+				ret=E_BUG;
+				break;
+			}
+			LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
 						" using dumb version...\n", a->p1.string);
 			ret=system(a->p1.string);
 			if (ret!=0){
@@ -103,7 +147,16 @@ int run_actions(struct action* a, struct sip_msg* msg)
 {
 	struct action* t;
 	int ret;
-	
+	static int rec_lev=0;
+
+	rec_lev++;
+	if (rec_lev>ROUTE_MAX_REC_LEV){
+		LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
+					" giving up!\n", rec_lev);
+		ret=E_UNSPEC;
+		goto error;
+	}
+		
 	if (a==0){
 		LOG(L_ERR, "WARNING: run_actions: null action list\n");
 		ret=0;
@@ -114,9 +167,13 @@ int run_actions(struct action* a, struct sip_msg* msg)
 		if(ret==0) break;
 		else if (ret<0){ ret=-1; goto error; }
 	}
-	ret=0;
+	
+	rec_lev--;
+	return 0;
+	
 
 error:
+	rec_lev--;
 	return ret;
 }
 

+ 1 - 1
cfg.lex

@@ -214,7 +214,7 @@ EAT_ABLE	[\ \t\b\r]
 static char* addstr(char * src, char ** dest)
 {
 	char *tmp;
-	int len1, len2;
+	unsigned len1, len2;
 	
 	if (*dest==0){
 		*dest=strdup(src);

+ 40 - 16
cfg.y

@@ -6,7 +6,12 @@
 
 %{
 
+#include <stdlib.h>
 #include "route_struct.h"
+#include "globals.h"
+#include "route.h"
+
+void yyerror(char* s);
 
 %}
 
@@ -17,12 +22,13 @@
 	struct expr* expr;
 	struct action* action;
 	struct net* net;
+	struct route_elem* route_el;
 }
 
 /* terminals */
 
 
-/* keywors */
+/* keywords */
 %token FORWARD
 %token SEND
 %token DROP
@@ -78,6 +84,8 @@
 %type <uval> ipv4
 %type <net> net4
 %type <strval> host
+%type <route_el> rules;
+%type <route_el> rule;
 
 
 
@@ -87,9 +95,9 @@
 cfg:	statements
 	;
 
-statements:	statements statement {printf("got <> <>\n");}
-		| statement {printf("got a statement<>\n"); }
-		| statements error { yyerror(""); }
+statements:	statements statement {}
+		| statement {}
+		| statements error { yyerror(""); YYABORT;}
 	;
 
 statement:	assign_stm CR
@@ -125,20 +133,29 @@ ipv4:	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER {
 														"address");
 												$$=0;
 											}else{
-												$$=($1<<24)|($3<<16)|
-													($5<<8)|$7;
+												$$=htonl( ($1<<24)|
+													($3<<16)| ($5<<8)|$7 );
 											}
 												}
 	;
 
-route_stm:	ROUTE LBRACE rules RBRACE 
-		| ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE
+route_stm:	ROUTE LBRACE rules RBRACE { push($3, &rlist[DEFAULT_RT]); }
+
+		| ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE { 
+										if (($3<RT_NO) && ($3>=0)){
+											push($6, &rlist[$3]);
+										}else{
+											yyerror("invalid routing"
+													"table number");
+											YYABORT; }
+										}
 		| ROUTE error { yyerror("invalid  route  statement"); }
 	;
 
-rules:	rules rule
-	| rule
-	| rules error { yyerror("invalid rule"); }
+rules:	rules rule { push($2, &$1); $$=$1; 
+						printf(": rules->rules(%x) rule(%x)\n", $1,$2);}
+	| rule {$$=$1; printf(": rules->rule (%x)\n",$1) }
+	| rules error { $$=0; yyerror("invalid rule"); }
 	 ;
 
 rule:	condition	actions CR {
@@ -146,9 +163,16 @@ rule:	condition	actions CR {
 								printf("expr: "); print_expr($1);
 								printf("\n  -> actions: ");
 								print_action($2); printf("\n");
-							   }
-	| CR  /* null rule */
-	| condition error { yyerror("bad actions in rule"); }
+
+								$$=0;
+								if (add_rule($1, $2, &$$)<0) {
+									yyerror("error calling add_rule");
+									YYABORT;
+								}
+								printf(": rule -> condition actions CR\n");
+							  }
+	| CR  /* null rule */		{ $$=0; printf(": rule-> CR!\n"); }
+	| condition error { $$=0; yyerror("bad actions in rule"); }
 	;
 
 condition:	exp {$$=$1;}
@@ -242,7 +266,7 @@ net4:	ipv4 SLASH ipv4	{ $$=mk_net($1, $3); }
 								yyerror("invalid bit number in netmask");
 								$$=0;
 							}else{
-								$$=mk_net($1, (1<<$3)-1);
+								$$=mk_net($1, htonl((1<<$3)-1));
 							}
 						}
 	| ipv4				{ $$=mk_net($1, 0xffffffff); }
@@ -396,7 +420,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
 extern int line;
 extern int column;
 extern int startcolumn;
-yyerror(char* s)
+void yyerror(char* s)
 {
 	fprintf(stderr, "parse error (%d,%d-%d): %s\n", line, startcolumn, 
 			column, s);

+ 0 - 129
cfg_parser.c

@@ -1,129 +0,0 @@
-/*
- * $Id$
- */
-
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "cfg_parser.h"
-#include "msg_parser.h" /* parse_hostport */
-#include "dprint.h"
-#include "parser_f.h"
-#include "route.h"
-
-
-
-
-/* params: null terminated text line => fills cl
- * returns 0, or on error -1. */
-int cfg_parse_line(char* line, struct cfg_line* cl)
-{
-	/* format:
-		line = rule | comment
-		comment = SP* '#'.*
-		rule = SP* method_re SP* uri_re SP* ip_address comment?
-	*/
-		
-	char* tmp;
-	char* end;
-	
-	end=line+strlen(line);
-	tmp=eat_space(line, end-line);
-	if ((tmp==end)||(is_empty(tmp, end-tmp))) {
-		cl->type=CFG_EMPTY;
-		goto skip;
-	}
-	if (*tmp=='#'){
-		cl->type=CFG_COMMENT;
-		goto skip;
-	}
-	cl->method=tmp;
-	tmp=eat_token(cl->method,end-cl->method);
-	if (tmp==end) goto error;
-	*tmp=0;
-	tmp++;
-	cl->uri=eat_space(tmp,end-tmp);
-	if (tmp==end) goto error;
-	tmp=eat_token(cl->uri,end-cl->uri);
-	if (tmp==end) goto error;
-	*tmp=0;
-	tmp++;
-	cl->address=eat_space(tmp,end-tmp);
-	if (tmp==end) goto error;
-	tmp=eat_token(cl->address, end-cl->address);
-	if (tmp<end) {
-		*tmp=0;
-		if (tmp+1<end){
-			if (!is_empty(tmp+1,end-tmp-1)){
-				/* check if comment */
-				tmp=eat_space(tmp+1, end-tmp-1);
-				if (*tmp!='#'){
-					/* extra chars at the end of line */
-					goto error;
-				}
-			}
-		}
-	}
-	/* find port */
-	if (parse_hostport(cl->address, &tmp, &cl->port)==0){
-			goto error;
-	}
-	
-	cl->type=CFG_RULE;
-skip:
-	return 0;
-error:
-	cl->type=CFG_ERROR;
-	return -1;
-}
-
-
-
-/* parses the cfg, returns 0 on success, line no otherwise */
-int cfg_parse_stream(FILE* stream)
-{
-	int line;
-	struct cfg_line cl;
-	char buf[MAX_LINE_SIZE];
-	int ret;
-
-	line=1;
-	while(!feof(stream)){
-		if (fgets(buf, MAX_LINE_SIZE, stream)){
-			cfg_parse_line(buf, &cl);
-			switch (cl.type){
-				case CFG_RULE:
-					if ((ret=add_rule(&cl, &rlist))!=0){
-						LOG(L_CRIT, 
-								"ERROR: could not compile rule at line %d\n",
-								line);
-						LOG(L_CRIT, " ----: add_rule returned %d\n", ret);
-						goto error;
-					}
-					break;
-				case CFG_COMMENT:
-				case CFG_SKIP:
-					break;
-				case CFG_ERROR:
-					LOG(L_CRIT, "ERROR: bad config line (%d):%s\n", line, buf);
-					goto error;
-					break;
-			}
-			line++;
-		}else{
-			if (ferror(stream)){
-				LOG(L_CRIT,
-						"ERROR: reading configuration: %s\n",
-						strerror(errno));
-				goto error;
-			}
-			break;
-		}
-	}
-	return 0;
-
-error:
-	return line;
-}
-

+ 0 - 30
cfg_parser.h

@@ -1,30 +0,0 @@
-/*
- * $Id$
- */
-
-#ifndef  cfg_parser_h
-#define cfg_parser_h
-
-#include <stdio.h>
-
-#define CFG_EMPTY   0
-#define CFG_COMMENT 1
-#define CFG_SKIP    2
-#define CFG_RULE    3
-#define CFG_ERROR  -1
-
-#define MAX_LINE_SIZE 800
-
-struct cfg_line{
-	int type;
-	char* method;
-	char* uri;
-	char* address;
-	short int port;
-};
-
-
-int cfg_parse_line(char* line, struct cfg_line* cl);
-int cfg_parse_stream(FILE* stream);
-
-#endif

+ 3 - 0
config.h

@@ -22,7 +22,10 @@
 #define CHILD_NO    8
 
 #define RT_NO 10 /* routing tables number */
+#define DEFAULT_RT 0 /* default routing table */
 
 #define MAX_REC_LEV 100 /* maximum number of recursive calls */
+#define ROUTE_MAX_REC_LEV 10 /* maximum number of recursive calls
+							   for route()*/
 
 #endif

+ 2 - 0
error.h

@@ -5,10 +5,12 @@
 #ifndef error_h
 #define error_h
 
+#define E_UNSPEC      -1
 #define E_OUT_OF_MEM  -2
 #define E_BAD_RE      -3
 #define E_BAD_ADDRESS -4
 #define E_BUG         -5
+#define E_CFG         -6
 
 
 #endif

+ 15 - 10
forward.c

@@ -4,6 +4,8 @@
 
 
 #include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
@@ -38,17 +40,18 @@ int check_address(unsigned long ip, char *name, int resolver)
 	if (resolver&DO_DNS){ 
 		/* try all names ips */
 		he=gethostbyname(name);
-		for(i=0; he->h_addr_list[i];i++){
+		for(i=0;he && he->h_addr_list[i];i++){
 			if (*(unsigned long*)he->h_addr_list[i]==ip)
 				return 0;
 		}
 	}
 	if (resolver&DO_REV_DNS){
+	print_ip(ip);
 		/* try reverse dns */
 		he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET);
-		if (strcmp(he->h_name, name)==0)
+		if (he && (strcmp(he->h_name, name)==0))
 			return 0;
-		for (i=0; he->h_aliases[i];i++){
+		for (i=0; he && he->h_aliases[i];i++){
 			if (strcmp(he->h_aliases[i],name)==0)
 				return 0;
 		}
@@ -58,9 +61,7 @@ int check_address(unsigned long ip, char *name, int resolver)
 
 
 
-int forward_request( struct sip_msg* msg,
-					 struct proxy_l * p,
-					 unsigned long source_ip)
+int forward_request( struct sip_msg* msg, struct proxy_l * p)
 {
 	unsigned int len, new_len, via_len, received_len;
 	char line_buf[MAX_VIA_LINE_SIZE];
@@ -68,12 +69,14 @@ int forward_request( struct sip_msg* msg,
 	char* new_buf;
 	char* orig;
 	char* buf;
-	int offset, s_offset, size;
+	unsigned int offset, s_offset, size;
 	struct sockaddr_in* to;
+	unsigned long source_ip;
 
 	orig=msg->orig;
 	buf=msg->buf;
 	len=msg->len;
+	source_ip=msg->src_ip;
 	received_len=0;
 	new_buf=0;
 	to=0;
@@ -146,7 +149,8 @@ int forward_request( struct sip_msg* msg,
 
 	p->tx++;
 	p->tx_bytes+=new_len;
-	if (udp_send(new_buf, new_len, to, sizeof(struct sockaddr))==-1){
+	if (udp_send(new_buf, new_len, (struct sockaddr*) to,
+				sizeof(struct sockaddr_in))==-1){
 			p->errors++;
 			p->ok=0;
 			goto error;
@@ -171,7 +175,7 @@ int forward_reply(struct sip_msg* msg)
 
 	unsigned int new_len, via_len,r;
 	char* new_buf;
-	int offset, s_offset, size;
+	unsigned offset, s_offset, size;
 	struct hostent* he;
 	struct sockaddr_in* to;
 	char* orig;
@@ -245,7 +249,8 @@ int forward_reply(struct sip_msg* msg)
 	to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
 	to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
 	
-	if (udp_send(new_buf,new_len, to, sizeof(struct sockaddr))==-1)
+	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
+					sizeof(struct sockaddr_in))==-1)
 		goto error;
 	
 	free(new_buf);

+ 1 - 2
forward.h

@@ -13,8 +13,7 @@
 
 int check_address(unsigned long ip, char *name, int resolver);
 
-int forward_request( struct sip_msg* msg,  struct proxy_l* p,
-					 unsigned long source_ip);
+int forward_request( struct sip_msg* msg,  struct proxy_l* p);
 
 int forward_reply( struct sip_msg* msg);
 

+ 16 - 6
main.c

@@ -3,7 +3,9 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
+#include <ctype.h>
 #include <string.h>
 #include <netdb.h>
 #include <unistd.h>
@@ -20,7 +22,7 @@
 
 
 static char id[]="@(#) $Id$";
-static char version[]="sip_router 0.3";
+static char version[]="sip_router 0.5";
 static char help_msg[]= "\
 Usage: sip_router -l address [-l address] [options]\n\
 Options:\n\
@@ -90,6 +92,12 @@ int process_no = 0;
 #define MAX_FD 32 /* maximum number of inherited open file descriptors,
 		    (normally it shouldn't  be bigger  than 3) */
 
+
+extern FILE* yyin;
+extern int yyparse();
+
+
+
 /* daemon init, return 0 on success, -1 on error */
 int daemonize(char*  name)
 {
@@ -226,8 +234,8 @@ int main(int argc, char** argv)
 					}
 					break;
 			case 'n':
-					children_no=strtol(optarg, tmp, 10);
-					if (tmp &&(*tmp)){
+					children_no=strtol(optarg, &tmp, 10);
+					if ((tmp==0) ||(*tmp)){
 						fprintf(stderr, "bad process number: -n %s\n", optarg);
 						goto error;
 					}
@@ -251,6 +259,7 @@ int main(int argc, char** argv)
 					break;
 			case 'V':
 					printf("version: %s\n", version);
+					printf("%s\n",id);
 					exit(0);
 					break;
 			case 'h':
@@ -319,13 +328,14 @@ int main(int argc, char** argv)
 				strerror(errno));
 		goto error;
 	}
-
-	if (cfg_parse_stream(cfg_stream)!=0){
+	
+	yyin=cfg_stream;
+	if (yyparse()!=0){
 		fprintf(stderr, "ERROR: config parser failure\n");
 		goto error;
 	}
 	
-		
+	
 	print_rl();
 
 

+ 1 - 1
msg_parser.c

@@ -6,6 +6,7 @@
  */
 
 #include <string.h>
+#include <stdlib.h>
 
 #include "msg_parser.h"
 #include "parser_f.h"
@@ -351,7 +352,6 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
 	struct hdr_field hf;
 	struct via_body vb1, vb2;
 	int offset;
-	int r;
 
 	
 	/* eat crlf from the beginning */

+ 5 - 3
proxy.c

@@ -11,6 +11,7 @@
 #include "dprint.h"
 
 #include <string.h>
+#include <stdlib.h>
 
 
 struct proxy_l* proxies=0;
@@ -19,7 +20,7 @@ struct proxy_l* proxies=0;
 
 /* searches for the proxy named 'name', on port 'port'
    returns: pointer to proxy_l on success or 0 if not found */ 
-struct proxy_l* find_proxy(char *name, unsigned short port)
+static struct proxy_l* find_proxy(char *name, unsigned short port)
 {
 	struct proxy_l* t;
 	for(t=proxies; t; t=t->next)
@@ -31,9 +32,10 @@ struct proxy_l* find_proxy(char *name, unsigned short port)
 
 
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
-int hostent_cpy(struct hostent *dst, struct hostent* src)
+static int hostent_cpy(struct hostent *dst, struct hostent* src)
 {
-	int len, r,ret,i,len2;
+	unsigned len,len2;
+	int r,ret,i;
 
 	/* start copying the host entry.. */
 	/* copy h_name */

+ 3 - 1
receive.c

@@ -3,12 +3,14 @@
  */
 
 #include <string.h>
+#include <stdlib.h>
 
 #include "receive.h"
 #include "dprint.h"
 #include "route.h"
 #include "msg_parser.h"
 #include "forward.h"
+#include "action.h"
 
 
 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
@@ -50,7 +52,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
 		re->tx++;
 		/* send msg */
 		DBG(" found route \n");
-		if (run_actions(re->actions)<0){
+		if (run_actions(re->actions, &msg)<0){
 			LOG(L_WARN, "WARNING: receive_msg: "
 					"error while trying actions\n");
 			goto error;

+ 27 - 25
route.c

@@ -5,6 +5,7 @@
  *
  */
  
+#include <stdlib.h>
 #include <sys/types.h>
 #include <regex.h>
 #include <netdb.h>
@@ -25,7 +26,7 @@ struct route_elem* rlist[RT_NO];
 
  void free_re(struct route_elem* r)
 {
-	int i;
+	/*int i;*/
 	if (r){
 		/*
 			regfree(&(r->method));
@@ -59,11 +60,10 @@ struct route_elem* init_re()
 }
 
 
-
+/* adds re list to head; re must be null terminated (last re->next=0))*/
 void push(struct route_elem* re, struct route_elem** head)
 {
 	struct route_elem *t;
-	re->next=0;
 	if (*head==0){
 		*head=re;
 		return;
@@ -90,7 +90,7 @@ void clear_rlist(struct route_elem** rl)
 
 /* traverses an expr tree and compiles the REs where necessary) 
  * returns: 0 for ok, <0 if errors */
-int fix_expr(struct expr* exp)
+static int fix_expr(struct expr* exp)
 {
 	regex_t* re;
 	int ret;
@@ -147,7 +147,7 @@ int fix_expr(struct expr* exp)
 
 
 /* adds the proxies in the proxy list & resolves the hostnames */
-int fix_actions(struct action* a)
+static int fix_actions(struct action* a)
 {
 	struct action *t;
 	struct proxy_l* p;
@@ -159,6 +159,7 @@ int fix_actions(struct action* a)
 			case SEND_T:
 					switch(t->p1_type){
 						case NUMBER_ST:
+						case IP_ST: /* for now ip_st==number_st*/
 							tmp=strdup(inet_ntoa(
 										*(struct in_addr*)&t->p1.number));
 							if (tmp==0){
@@ -177,7 +178,8 @@ int fix_actions(struct action* a)
 							break;
 						default:
 							LOG(L_CRIT, "BUG: fix_actions: invalid type"
-									" (should be string or number)\n");
+									"%d (should be string or number)\n",
+										t->type);
 							return E_BUG;
 					}
 					break;
@@ -189,7 +191,7 @@ int fix_actions(struct action* a)
 
 
 /* eval_elem helping function, returns str op param */
-int comp_str(char* str, void* param, int op, int subtype)
+static int comp_str(char* str, void* param, int op, int subtype)
 {
 	int ret;
 	
@@ -221,7 +223,7 @@ error:
 
 
 /* eval_elem helping function, returns a op param */
-int comp_ip(unsigned a, void* param, int op, int subtype)
+static int comp_ip(unsigned a, void* param, int op, int subtype)
 {
 	struct hostent* he;
 	char ** h;
@@ -266,7 +268,7 @@ error:
 
 
 /* returns: 0/1 (false/true) or -1 on error */
-int eval_elem(struct expr* e, struct sip_msg* msg)
+static int eval_elem(struct expr* e, struct sip_msg* msg)
 {
 
 	int ret;
@@ -304,7 +306,7 @@ error:
 
 
 
-int eval_expr(struct expr* e, struct sip_msg* msg)
+static int eval_expr(struct expr* e, struct sip_msg* msg)
 {
 	static int rec_lev=0;
 	int ret;
@@ -359,16 +361,14 @@ int add_rule(struct expr* e, struct action* a, struct route_elem** head)
 {
 	
 	struct route_elem* re;
-	struct hostent * he;
 	int ret;
-	int i,len, len2;
 
 	re=init_re();
 	if (re==0) return E_OUT_OF_MEM;
 	LOG(L_DBG, "add_rule: fixing expr...\n");
 	if ((ret=fix_expr(e))!=0) goto error;
 	LOG(L_DBG, "add_rule: fixing actions...\n");
-	if ((ret=fix_action(a))!=0) goto error;
+	if ((ret=fix_actions(a))!=0) goto error;
 	re->condition=e;
 	re->actions=a;
 	
@@ -404,18 +404,20 @@ void print_rl()
 	struct route_elem* t;
 	int i,j;
 
-	if (rlist==0){
-		printf("the routing table is empty\n");
-		return;
-	}
-	
-	for (t=rlist[0],i=0; t; i++, t=t->next){
-		printf("%2d.condition: ");
-		print_expr(t->condition);
-		printf("\n  -> ");
-		print_action(t->actions);
-		printf("\n    Statistics: tx=%d, errors=%d, tx_bytes=%d\n",
-				t->tx, t->errors, t->tx_bytes);
+	for(j=0; j<RT_NO; j++){
+		if (rlist[j]==0){
+			if (j==0) printf("WARNING: the main routing table is empty\n");
+			continue;
+		}
+		printf("routing table %d:\n",j);
+		for (t=rlist[j],i=0; t; i++, t=t->next){
+			printf("%2d.condition: ",i);
+			print_expr(t->condition);
+			printf("\n  -> ");
+			print_action(t->actions);
+			printf("\n    Statistics: tx=%d, errors=%d, tx_bytes=%d\n",
+					t->tx, t->errors, t->tx_bytes);
+		}
 	}
 
 }

+ 2 - 2
route_struct.c

@@ -8,6 +8,7 @@
 #include  "route_struct.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <netinet/in.h>
 
 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
@@ -96,7 +97,6 @@ error:
 
 void print_ip(unsigned ip)
 {
-	ip=htonl(ip);
 	printf("%d.%d.%d.%d", ((unsigned char*)&ip)[0],
 						  ((unsigned char*)&ip)[1],
 						  ((unsigned char*)&ip)[2],
@@ -162,7 +162,7 @@ void print_expr(struct expr* exp)
 					print_ip(exp->r.intval);
 					break;
 			default:
-					printf("UNKNOWN");
+					printf("type<%d>", exp->subtype);
 		}
 	}else if (exp->type==EXP_T){
 		switch(exp->op){

+ 11 - 7
udp_server.c

@@ -2,6 +2,8 @@
  * $Id$
  */
 
+#include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -11,6 +13,7 @@
 #include "udp_server.h"
 #include "config.h"
 #include "dprint.h"
+#include "receive.h"
 
 
 int udp_sock;
@@ -33,7 +36,7 @@ int udp_init(unsigned long ip, unsigned short port)
 
 	udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
 	if (udp_sock==-1){
-		LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror());
+		LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror(errno));
 		goto error;
 	}
 	/* set sock opts? */
@@ -41,12 +44,12 @@ int udp_init(unsigned long ip, unsigned short port)
 	if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR,
 					(void*)&optval, sizeof(optval)) ==-1)
 	{
-		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror());
+		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
 		goto error;
 	}
 
 	if (bind(udp_sock, (struct sockaddr*) addr, sizeof(struct sockaddr))==-1){
-		LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror());
+		LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror(errno));
 		goto error;
 	}
 
@@ -62,7 +65,7 @@ error:
 
 int udp_rcv_loop()
 {
-	int len;
+	unsigned len;
 	char buf[BUF_SIZE+1];
 	struct sockaddr* from;
 	int fromlen;
@@ -77,7 +80,8 @@ int udp_rcv_loop()
 		fromlen=sizeof(struct sockaddr);
 		len=recvfrom(udp_sock, buf, BUF_SIZE, 0, from, &fromlen);
 		if (len==-1){
-			LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n", strerror());
+			LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n",
+						strerror(errno));
 			if (errno==EINTR)	goto skip;
 			else goto error;
 		}
@@ -101,14 +105,14 @@ error:
 
 
 /* which socket to use? main socket or new one? */
-int udp_send(char *buf, int len, struct sockaddr*  to, int tolen)
+int udp_send(char *buf, unsigned len, struct sockaddr*  to, unsigned tolen)
 {
 
 	int n;
 again:
 	n=sendto(udp_sock, buf, len, 0, to, tolen);
 	if (n==-1){
-		LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror());
+		LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror(errno));
 		if (errno==EINTR) goto again;
 	}
 	return n;

+ 1 - 0
udp_server.h

@@ -10,6 +10,7 @@
 extern int udp_sock;
 
 int udp_init(unsigned long ip, unsigned short port);
+int udp_send(char *buf, unsigned len, struct sockaddr*  to, unsigned tolen);
 int udp_rcv_loop();