Browse code

- finally a working version

Andrei Pelinescu-Onciul authored on 21/09/2001 20:24:13
Showing 18 changed files
... ...
@@ -13,13 +13,13 @@ NAME=sip_router
13 13
 
14 14
 
15 15
 CC=gcc
16
-CFLAGS=-O2
16
+CFLAGS=-O2 -Wcast-align  #-Wmissing-prototypes  -Wall
17 17
 LEX=lex
18 18
 YACC=bison
19 19
 YACC_FLAGS=-d
20 20
 # on linux and freebsd keep it empty (e.g. LIBS= )
21 21
 # on solaris add -lxnet (e.g. LIBS= -lxnet)
22
-LIBS=
22
+LIBS=-lfl
23 23
 ALLDEP=Makefile
24 24
 
25 25
 MKDEP=gcc -M
... ...
@@ -35,14 +35,15 @@ MKDEP=gcc -M
35 35
 	$(MKDEP) $< >$@
36 36
 
37 37
 # normal rules
38
+$(NAME): $(objs)
39
+	$(CC) $(CFLAGS) $(objs) -o $(NAME) $(LIBS)
40
+
38 41
 lex.yy.c: cfg.lex $(ALLDEP)
39 42
 	$(LEX) $<
40 43
 
41 44
 cfg.tab.c: cfg.y
42 45
 	$(YACC) $(YACC_FLAGS) $<
43 46
 
44
-$(NAME): $(objs)
45
-	$(CC) $(CFLAGS) $(LIBS) $(objs) -o $(NAME)
46 47
 
47 48
 .PHONY: all
48 49
 all: $(NAME)
... ...
@@ -9,6 +9,9 @@
9 9
 #include "error.h"
10 10
 #include "dprint.h"
11 11
 #include "proxy.h"
12
+#include "forward.h"
13
+#include "udp_server.h"
14
+#include "route.h"
12 15
 
13 16
 #include <netdb.h>
14 17
 #include <stdlib.h>
... ...
@@ -20,14 +23,17 @@ int do_action(struct action* a, struct sip_msg* msg)
20 20
 	int ret;
21 21
 	struct sockaddr_in* to;
22 22
 	struct proxy_l* p;
23
+	struct route_elem* re;
23 24
 
25
+	ret=E_BUG;
24 26
 	switch (a->type){
25 27
 		case DROP_T:
26 28
 				ret=0;
27 29
 			break;
28 30
 		case FORWARD_T:
29
-			if (a->p1_type!= PROXY_ST){
30
-				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
31
+			if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
32
+				LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
33
+						a->p1_type, a->p2_type);
31 34
 				ret=E_BUG;
32 35
 				break;
33 36
 			}
... ...
@@ -42,8 +48,9 @@ int do_action(struct action* a, struct sip_msg* msg)
42 42
 				ret=E_OUT_OF_MEM;
43 43
 				break;
44 44
 			}
45
-			if (a->p1_type!= PROXY_ST){
46
-				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
45
+			if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
46
+				LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
47
+						a->p1_type, a->p2_type);
47 48
 				ret=E_BUG;
48 49
 				break;
49 50
 			}
... ...
@@ -60,7 +67,8 @@ int do_action(struct action* a, struct sip_msg* msg)
60 60
 			to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
61 61
 			p->tx++;
62 62
 			p->tx_bytes+=msg->len;
63
-			ret=udp_send(msg->orig, msg->len, to, sizeof(struct sockaddr));
63
+			ret=udp_send(msg->orig, msg->len, (struct sockaddr*)to,
64
+					sizeof(struct sockaddr_in));
64 65
 			free(to);
65 66
 			if (ret<0){
66 67
 				p->errors++;
... ...
@@ -69,20 +77,56 @@ int do_action(struct action* a, struct sip_msg* msg)
69 69
 			
70 70
 			break;
71 71
 		case LOG_T:
72
-			LOG(a->p2.number, a->p1.string);
72
+			if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){
73
+				LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
74
+						a->p1_type, a->p2_type);
75
+				ret=E_BUG;
76
+				break;
77
+			}
78
+			LOG(a->p1.number, a->p2.string);
73 79
 			ret=1;
74 80
 			break;
75 81
 		case ERROR_T:
82
+			if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){
83
+				LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
84
+						a->p1_type, a->p2_type);
85
+				ret=E_BUG;
86
+				break;
87
+			}
76 88
 			LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
77 89
 					"not implemented yet\n", a->p1.string, a->p2.string);
78 90
 			ret=1;
79 91
 			break;
80 92
 		case ROUTE_T:
81
-			LOG(L_NOTICE, "WARNING: do_action: route(%d) not implemented "
82
-							"yet\n", a->p1.number);
93
+			if (a->p1_type!=NUMBER_ST){
94
+				LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
95
+						a->p1_type);
96
+				ret=E_BUG;
97
+				break;
98
+			}
99
+			if ((a->p1.number>RT_NO)||(a->p1.number<0)){
100
+				LOG(L_ERR, "ERROR: invalid routing table number in"
101
+							"route(%d)\n", a->p1.number);
102
+				ret=E_CFG;
103
+				break;
104
+			}
105
+			re=route_match(msg, &rlist[a->p1.number]);
106
+			if (re==0){
107
+				LOG(L_INFO, "WARNING: do_action: route(%d): no new route"
108
+						" found\n", a->p1.number);
109
+				ret=1;
110
+				break;
111
+			}
112
+			ret=((ret=run_actions(re->actions, msg))<0)?ret:1;
83 113
 			break;
84 114
 		case EXEC_T:
85
-			LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,",
115
+			if (a->p1_type!=STRING_ST){
116
+				LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
117
+						a->p1_type);
118
+				ret=E_BUG;
119
+				break;
120
+			}
121
+			LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
86 122
 						" using dumb version...\n", a->p1.string);
87 123
 			ret=system(a->p1.string);
88 124
 			if (ret!=0){
... ...
@@ -103,7 +147,16 @@ int run_actions(struct action* a, struct sip_msg* msg)
103 103
 {
104 104
 	struct action* t;
105 105
 	int ret;
106
-	
106
+	static int rec_lev=0;
107
+
108
+	rec_lev++;
109
+	if (rec_lev>ROUTE_MAX_REC_LEV){
110
+		LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
111
+					" giving up!\n", rec_lev);
112
+		ret=E_UNSPEC;
113
+		goto error;
114
+	}
115
+		
107 116
 	if (a==0){
108 117
 		LOG(L_ERR, "WARNING: run_actions: null action list\n");
109 118
 		ret=0;
... ...
@@ -114,9 +167,13 @@ int run_actions(struct action* a, struct sip_msg* msg)
114 114
 		if(ret==0) break;
115 115
 		else if (ret<0){ ret=-1; goto error; }
116 116
 	}
117
-	ret=0;
117
+	
118
+	rec_lev--;
119
+	return 0;
120
+	
118 121
 
119 122
 error:
123
+	rec_lev--;
120 124
 	return ret;
121 125
 }
122 126
 
... ...
@@ -214,7 +214,7 @@ EAT_ABLE	[\ \t\b\r]
214 214
 static char* addstr(char * src, char ** dest)
215 215
 {
216 216
 	char *tmp;
217
-	int len1, len2;
217
+	unsigned len1, len2;
218 218
 	
219 219
 	if (*dest==0){
220 220
 		*dest=strdup(src);
... ...
@@ -6,7 +6,12 @@
6 6
 
7 7
 %{
8 8
 
9
+#include <stdlib.h>
9 10
 #include "route_struct.h"
11
+#include "globals.h"
12
+#include "route.h"
13
+
14
+void yyerror(char* s);
10 15
 
11 16
 %}
12 17
 
... ...
@@ -17,12 +22,13 @@
17 17
 	struct expr* expr;
18 18
 	struct action* action;
19 19
 	struct net* net;
20
+	struct route_elem* route_el;
20 21
 }
21 22
 
22 23
 /* terminals */
23 24
 
24 25
 
25
-/* keywors */
26
+/* keywords */
26 27
 %token FORWARD
27 28
 %token SEND
28 29
 %token DROP
... ...
@@ -78,6 +84,8 @@
78 78
 %type <uval> ipv4
79 79
 %type <net> net4
80 80
 %type <strval> host
81
+%type <route_el> rules;
82
+%type <route_el> rule;
81 83
 
82 84
 
83 85
 
... ...
@@ -87,9 +95,9 @@
87 87
 cfg:	statements
88 88
 	;
89 89
 
90
-statements:	statements statement {printf("got <> <>\n");}
91
-		| statement {printf("got a statement<>\n"); }
92
-		| statements error { yyerror(""); }
90
+statements:	statements statement {}
91
+		| statement {}
92
+		| statements error { yyerror(""); YYABORT;}
93 93
 	;
94 94
 
95 95
 statement:	assign_stm CR
... ...
@@ -125,20 +133,29 @@ ipv4:	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER {
125 125
 														"address");
126 126
 												$$=0;
127 127
 											}else{
128
-												$$=($1<<24)|($3<<16)|
129
-													($5<<8)|$7;
128
+												$$=htonl( ($1<<24)|
129
+													($3<<16)| ($5<<8)|$7 );
130 130
 											}
131 131
 												}
132 132
 	;
133 133
 
134
-route_stm:	ROUTE LBRACE rules RBRACE 
135
-		| ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE
134
+route_stm:	ROUTE LBRACE rules RBRACE { push($3, &rlist[DEFAULT_RT]); }
135
+
136
+		| ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE { 
137
+										if (($3<RT_NO) && ($3>=0)){
138
+											push($6, &rlist[$3]);
139
+										}else{
140
+											yyerror("invalid routing"
141
+													"table number");
142
+											YYABORT; }
143
+										}
136 144
 		| ROUTE error { yyerror("invalid  route  statement"); }
137 145
 	;
138 146
 
139
-rules:	rules rule
140
-	| rule
141
-	| rules error { yyerror("invalid rule"); }
147
+rules:	rules rule { push($2, &$1); $$=$1; 
148
+						printf(": rules->rules(%x) rule(%x)\n", $1,$2);}
149
+	| rule {$$=$1; printf(": rules->rule (%x)\n",$1) }
150
+	| rules error { $$=0; yyerror("invalid rule"); }
142 151
 	 ;
143 152
 
144 153
 rule:	condition	actions CR {
... ...
@@ -146,9 +163,16 @@ rule:	condition	actions CR {
146 146
 								printf("expr: "); print_expr($1);
147 147
 								printf("\n  -> actions: ");
148 148
 								print_action($2); printf("\n");
149
-							   }
150
-	| CR  /* null rule */
151
-	| condition error { yyerror("bad actions in rule"); }
149
+
150
+								$$=0;
151
+								if (add_rule($1, $2, &$$)<0) {
152
+									yyerror("error calling add_rule");
153
+									YYABORT;
154
+								}
155
+								printf(": rule -> condition actions CR\n");
156
+							  }
157
+	| CR  /* null rule */		{ $$=0; printf(": rule-> CR!\n"); }
158
+	| condition error { $$=0; yyerror("bad actions in rule"); }
152 159
 	;
153 160
 
154 161
 condition:	exp {$$=$1;}
... ...
@@ -242,7 +266,7 @@ net4:	ipv4 SLASH ipv4	{ $$=mk_net($1, $3); }
242 242
 								yyerror("invalid bit number in netmask");
243 243
 								$$=0;
244 244
 							}else{
245
-								$$=mk_net($1, (1<<$3)-1);
245
+								$$=mk_net($1, htonl((1<<$3)-1));
246 246
 							}
247 247
 						}
248 248
 	| ipv4				{ $$=mk_net($1, 0xffffffff); }
... ...
@@ -396,7 +420,7 @@ cmd:		FORWARD LPAREN host RPAREN	{ $$=mk_action(	FORWARD_T,
396 396
 extern int line;
397 397
 extern int column;
398 398
 extern int startcolumn;
399
-yyerror(char* s)
399
+void yyerror(char* s)
400 400
 {
401 401
 	fprintf(stderr, "parse error (%d,%d-%d): %s\n", line, startcolumn, 
402 402
 			column, s);
403 403
deleted file mode 100644
... ...
@@ -1,129 +0,0 @@
1
-/*
2
- * $Id$
3
- */
4
-
5
-#include <errno.h>
6
-#include <string.h>
7
-#include <stdio.h>
8
-
9
-#include "cfg_parser.h"
10
-#include "msg_parser.h" /* parse_hostport */
11
-#include "dprint.h"
12
-#include "parser_f.h"
13
-#include "route.h"
14
-
15
-
16
-
17
-
18
-/* params: null terminated text line => fills cl
19
- * returns 0, or on error -1. */
20
-int cfg_parse_line(char* line, struct cfg_line* cl)
21
-{
22
-	/* format:
23
-		line = rule | comment
24
-		comment = SP* '#'.*
25
-		rule = SP* method_re SP* uri_re SP* ip_address comment?
26
-	*/
27
-		
28
-	char* tmp;
29
-	char* end;
30
-	
31
-	end=line+strlen(line);
32
-	tmp=eat_space(line, end-line);
33
-	if ((tmp==end)||(is_empty(tmp, end-tmp))) {
34
-		cl->type=CFG_EMPTY;
35
-		goto skip;
36
-	}
37
-	if (*tmp=='#'){
38
-		cl->type=CFG_COMMENT;
39
-		goto skip;
40
-	}
41
-	cl->method=tmp;
42
-	tmp=eat_token(cl->method,end-cl->method);
43
-	if (tmp==end) goto error;
44
-	*tmp=0;
45
-	tmp++;
46
-	cl->uri=eat_space(tmp,end-tmp);
47
-	if (tmp==end) goto error;
48
-	tmp=eat_token(cl->uri,end-cl->uri);
49
-	if (tmp==end) goto error;
50
-	*tmp=0;
51
-	tmp++;
52
-	cl->address=eat_space(tmp,end-tmp);
53
-	if (tmp==end) goto error;
54
-	tmp=eat_token(cl->address, end-cl->address);
55
-	if (tmp<end) {
56
-		*tmp=0;
57
-		if (tmp+1<end){
58
-			if (!is_empty(tmp+1,end-tmp-1)){
59
-				/* check if comment */
60
-				tmp=eat_space(tmp+1, end-tmp-1);
61
-				if (*tmp!='#'){
62
-					/* extra chars at the end of line */
63
-					goto error;
64
-				}
65
-			}
66
-		}
67
-	}
68
-	/* find port */
69
-	if (parse_hostport(cl->address, &tmp, &cl->port)==0){
70
-			goto error;
71
-	}
72
-	
73
-	cl->type=CFG_RULE;
74
-skip:
75
-	return 0;
76
-error:
77
-	cl->type=CFG_ERROR;
78
-	return -1;
79
-}
80
-
81
-
82
-
83
-/* parses the cfg, returns 0 on success, line no otherwise */
84
-int cfg_parse_stream(FILE* stream)
85
-{
86
-	int line;
87
-	struct cfg_line cl;
88
-	char buf[MAX_LINE_SIZE];
89
-	int ret;
90
-
91
-	line=1;
92
-	while(!feof(stream)){
93
-		if (fgets(buf, MAX_LINE_SIZE, stream)){
94
-			cfg_parse_line(buf, &cl);
95
-			switch (cl.type){
96
-				case CFG_RULE:
97
-					if ((ret=add_rule(&cl, &rlist))!=0){
98
-						LOG(L_CRIT, 
99
-								"ERROR: could not compile rule at line %d\n",
100
-								line);
101
-						LOG(L_CRIT, " ----: add_rule returned %d\n", ret);
102
-						goto error;
103
-					}
104
-					break;
105
-				case CFG_COMMENT:
106
-				case CFG_SKIP:
107
-					break;
108
-				case CFG_ERROR:
109
-					LOG(L_CRIT, "ERROR: bad config line (%d):%s\n", line, buf);
110
-					goto error;
111
-					break;
112
-			}
113
-			line++;
114
-		}else{
115
-			if (ferror(stream)){
116
-				LOG(L_CRIT,
117
-						"ERROR: reading configuration: %s\n",
118
-						strerror(errno));
119
-				goto error;
120
-			}
121
-			break;
122
-		}
123
-	}
124
-	return 0;
125
-
126
-error:
127
-	return line;
128
-}
129
-
130 1
deleted file mode 100644
... ...
@@ -1,30 +0,0 @@
1
-/*
2
- * $Id$
3
- */
4
-
5
-#ifndef  cfg_parser_h
6
-#define cfg_parser_h
7
-
8
-#include <stdio.h>
9
-
10
-#define CFG_EMPTY   0
11
-#define CFG_COMMENT 1
12
-#define CFG_SKIP    2
13
-#define CFG_RULE    3
14
-#define CFG_ERROR  -1
15
-
16
-#define MAX_LINE_SIZE 800
17
-
18
-struct cfg_line{
19
-	int type;
20
-	char* method;
21
-	char* uri;
22
-	char* address;
23
-	short int port;
24
-};
25
-
26
-
27
-int cfg_parse_line(char* line, struct cfg_line* cl);
28
-int cfg_parse_stream(FILE* stream);
29
-
30
-#endif
... ...
@@ -22,7 +22,10 @@
22 22
 #define CHILD_NO    8
23 23
 
24 24
 #define RT_NO 10 /* routing tables number */
25
+#define DEFAULT_RT 0 /* default routing table */
25 26
 
26 27
 #define MAX_REC_LEV 100 /* maximum number of recursive calls */
28
+#define ROUTE_MAX_REC_LEV 10 /* maximum number of recursive calls
29
+							   for route()*/
27 30
 
28 31
 #endif
... ...
@@ -5,10 +5,12 @@
5 5
 #ifndef error_h
6 6
 #define error_h
7 7
 
8
+#define E_UNSPEC      -1
8 9
 #define E_OUT_OF_MEM  -2
9 10
 #define E_BAD_RE      -3
10 11
 #define E_BAD_ADDRESS -4
11 12
 #define E_BUG         -5
13
+#define E_CFG         -6
12 14
 
13 15
 
14 16
 #endif
... ...
@@ -4,6 +4,8 @@
4 4
 
5 5
 
6 6
 #include <string.h>
7
+#include <stdio.h>
8
+#include <stdlib.h>
7 9
 #include <sys/types.h>
8 10
 #include <sys/socket.h>
9 11
 #include <netdb.h>
... ...
@@ -38,17 +40,18 @@ int check_address(unsigned long ip, char *name, int resolver)
38 38
 	if (resolver&DO_DNS){ 
39 39
 		/* try all names ips */
40 40
 		he=gethostbyname(name);
41
-		for(i=0; he->h_addr_list[i];i++){
41
+		for(i=0;he && he->h_addr_list[i];i++){
42 42
 			if (*(unsigned long*)he->h_addr_list[i]==ip)
43 43
 				return 0;
44 44
 		}
45 45
 	}
46 46
 	if (resolver&DO_REV_DNS){
47
+	print_ip(ip);
47 48
 		/* try reverse dns */
48 49
 		he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET);
49
-		if (strcmp(he->h_name, name)==0)
50
+		if (he && (strcmp(he->h_name, name)==0))
50 51
 			return 0;
51
-		for (i=0; he->h_aliases[i];i++){
52
+		for (i=0; he && he->h_aliases[i];i++){
52 53
 			if (strcmp(he->h_aliases[i],name)==0)
53 54
 				return 0;
54 55
 		}
... ...
@@ -58,9 +61,7 @@ int check_address(unsigned long ip, char *name, int resolver)
58 58
 
59 59
 
60 60
 
61
-int forward_request( struct sip_msg* msg,
62
-					 struct proxy_l * p,
63
-					 unsigned long source_ip)
61
+int forward_request( struct sip_msg* msg, struct proxy_l * p)
64 62
 {
65 63
 	unsigned int len, new_len, via_len, received_len;
66 64
 	char line_buf[MAX_VIA_LINE_SIZE];
... ...
@@ -68,12 +69,14 @@ int forward_request( struct sip_msg* msg,
68 68
 	char* new_buf;
69 69
 	char* orig;
70 70
 	char* buf;
71
-	int offset, s_offset, size;
71
+	unsigned int offset, s_offset, size;
72 72
 	struct sockaddr_in* to;
73
+	unsigned long source_ip;
73 74
 
74 75
 	orig=msg->orig;
75 76
 	buf=msg->buf;
76 77
 	len=msg->len;
78
+	source_ip=msg->src_ip;
77 79
 	received_len=0;
78 80
 	new_buf=0;
79 81
 	to=0;
... ...
@@ -146,7 +149,8 @@ int forward_request( struct sip_msg* msg,
146 146
 
147 147
 	p->tx++;
148 148
 	p->tx_bytes+=new_len;
149
-	if (udp_send(new_buf, new_len, to, sizeof(struct sockaddr))==-1){
149
+	if (udp_send(new_buf, new_len, (struct sockaddr*) to,
150
+				sizeof(struct sockaddr_in))==-1){
150 151
 			p->errors++;
151 152
 			p->ok=0;
152 153
 			goto error;
... ...
@@ -171,7 +175,7 @@ int forward_reply(struct sip_msg* msg)
171 171
 
172 172
 	unsigned int new_len, via_len,r;
173 173
 	char* new_buf;
174
-	int offset, s_offset, size;
174
+	unsigned offset, s_offset, size;
175 175
 	struct hostent* he;
176 176
 	struct sockaddr_in* to;
177 177
 	char* orig;
... ...
@@ -245,7 +249,8 @@ int forward_reply(struct sip_msg* msg)
245 245
 	to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
246 246
 	to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
247 247
 	
248
-	if (udp_send(new_buf,new_len, to, sizeof(struct sockaddr))==-1)
248
+	if (udp_send(new_buf,new_len, (struct sockaddr*) to, 
249
+					sizeof(struct sockaddr_in))==-1)
249 250
 		goto error;
250 251
 	
251 252
 	free(new_buf);
... ...
@@ -13,8 +13,7 @@
13 13
 
14 14
 int check_address(unsigned long ip, char *name, int resolver);
15 15
 
16
-int forward_request( struct sip_msg* msg,  struct proxy_l* p,
17
-					 unsigned long source_ip);
16
+int forward_request( struct sip_msg* msg,  struct proxy_l* p);
18 17
 
19 18
 int forward_reply( struct sip_msg* msg);
20 19
 
... ...
@@ -3,7 +3,9 @@
3 3
  */
4 4
 
5 5
 #include <stdio.h>
6
+#include <stdlib.h>
6 7
 #include <errno.h>
8
+#include <ctype.h>
7 9
 #include <string.h>
8 10
 #include <netdb.h>
9 11
 #include <unistd.h>
... ...
@@ -20,7 +22,7 @@
20 20
 
21 21
 
22 22
 static char id[]="@(#) $Id$";
23
-static char version[]="sip_router 0.3";
23
+static char version[]="sip_router 0.5";
24 24
 static char help_msg[]= "\
25 25
 Usage: sip_router -l address [-l address] [options]\n\
26 26
 Options:\n\
... ...
@@ -90,6 +92,12 @@ int process_no = 0;
90 90
 #define MAX_FD 32 /* maximum number of inherited open file descriptors,
91 91
 		    (normally it shouldn't  be bigger  than 3) */
92 92
 
93
+
94
+extern FILE* yyin;
95
+extern int yyparse();
96
+
97
+
98
+
93 99
 /* daemon init, return 0 on success, -1 on error */
94 100
 int daemonize(char*  name)
95 101
 {
... ...
@@ -226,8 +234,8 @@ int main(int argc, char** argv)
226 226
 					}
227 227
 					break;
228 228
 			case 'n':
229
-					children_no=strtol(optarg, tmp, 10);
230
-					if (tmp &&(*tmp)){
229
+					children_no=strtol(optarg, &tmp, 10);
230
+					if ((tmp==0) ||(*tmp)){
231 231
 						fprintf(stderr, "bad process number: -n %s\n", optarg);
232 232
 						goto error;
233 233
 					}
... ...
@@ -251,6 +259,7 @@ int main(int argc, char** argv)
251 251
 					break;
252 252
 			case 'V':
253 253
 					printf("version: %s\n", version);
254
+					printf("%s\n",id);
254 255
 					exit(0);
255 256
 					break;
256 257
 			case 'h':
... ...
@@ -319,13 +328,14 @@ int main(int argc, char** argv)
319 319
 				strerror(errno));
320 320
 		goto error;
321 321
 	}
322
-
323
-	if (cfg_parse_stream(cfg_stream)!=0){
322
+	
323
+	yyin=cfg_stream;
324
+	if (yyparse()!=0){
324 325
 		fprintf(stderr, "ERROR: config parser failure\n");
325 326
 		goto error;
326 327
 	}
327 328
 	
328
-		
329
+	
329 330
 	print_rl();
330 331
 
331 332
 
... ...
@@ -6,6 +6,7 @@
6 6
  */
7 7
 
8 8
 #include <string.h>
9
+#include <stdlib.h>
9 10
 
10 11
 #include "msg_parser.h"
11 12
 #include "parser_f.h"
... ...
@@ -351,7 +352,6 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
351 351
 	struct hdr_field hf;
352 352
 	struct via_body vb1, vb2;
353 353
 	int offset;
354
-	int r;
355 354
 
356 355
 	
357 356
 	/* eat crlf from the beginning */
... ...
@@ -11,6 +11,7 @@
11 11
 #include "dprint.h"
12 12
 
13 13
 #include <string.h>
14
+#include <stdlib.h>
14 15
 
15 16
 
16 17
 struct proxy_l* proxies=0;
... ...
@@ -19,7 +20,7 @@ struct proxy_l* proxies=0;
19 19
 
20 20
 /* searches for the proxy named 'name', on port 'port'
21 21
    returns: pointer to proxy_l on success or 0 if not found */ 
22
-struct proxy_l* find_proxy(char *name, unsigned short port)
22
+static struct proxy_l* find_proxy(char *name, unsigned short port)
23 23
 {
24 24
 	struct proxy_l* t;
25 25
 	for(t=proxies; t; t=t->next)
... ...
@@ -31,9 +32,10 @@ struct proxy_l* find_proxy(char *name, unsigned short port)
31 31
 
32 32
 
33 33
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
34
-int hostent_cpy(struct hostent *dst, struct hostent* src)
34
+static int hostent_cpy(struct hostent *dst, struct hostent* src)
35 35
 {
36
-	int len, r,ret,i,len2;
36
+	unsigned len,len2;
37
+	int r,ret,i;
37 38
 
38 39
 	/* start copying the host entry.. */
39 40
 	/* copy h_name */
... ...
@@ -3,12 +3,14 @@
3 3
  */
4 4
 
5 5
 #include <string.h>
6
+#include <stdlib.h>
6 7
 
7 8
 #include "receive.h"
8 9
 #include "dprint.h"
9 10
 #include "route.h"
10 11
 #include "msg_parser.h"
11 12
 #include "forward.h"
13
+#include "action.h"
12 14
 
13 15
 
14 16
 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)
50 50
 		re->tx++;
51 51
 		/* send msg */
52 52
 		DBG(" found route \n");
53
-		if (run_actions(re->actions)<0){
53
+		if (run_actions(re->actions, &msg)<0){
54 54
 			LOG(L_WARN, "WARNING: receive_msg: "
55 55
 					"error while trying actions\n");
56 56
 			goto error;
... ...
@@ -5,6 +5,7 @@
5 5
  *
6 6
  */
7 7
  
8
+#include <stdlib.h>
8 9
 #include <sys/types.h>
9 10
 #include <regex.h>
10 11
 #include <netdb.h>
... ...
@@ -25,7 +26,7 @@ struct route_elem* rlist[RT_NO];
25 25
 
26 26
  void free_re(struct route_elem* r)
27 27
 {
28
-	int i;
28
+	/*int i;*/
29 29
 	if (r){
30 30
 		/*
31 31
 			regfree(&(r->method));
... ...
@@ -59,11 +60,10 @@ struct route_elem* init_re()
59 59
 }
60 60
 
61 61
 
62
-
62
+/* adds re list to head; re must be null terminated (last re->next=0))*/
63 63
 void push(struct route_elem* re, struct route_elem** head)
64 64
 {
65 65
 	struct route_elem *t;
66
-	re->next=0;
67 66
 	if (*head==0){
68 67
 		*head=re;
69 68
 		return;
... ...
@@ -90,7 +90,7 @@ void clear_rlist(struct route_elem** rl)
90 90
 
91 91
 /* traverses an expr tree and compiles the REs where necessary) 
92 92
  * returns: 0 for ok, <0 if errors */
93
-int fix_expr(struct expr* exp)
93
+static int fix_expr(struct expr* exp)
94 94
 {
95 95
 	regex_t* re;
96 96
 	int ret;
... ...
@@ -147,7 +147,7 @@ int fix_expr(struct expr* exp)
147 147
 
148 148
 
149 149
 /* adds the proxies in the proxy list & resolves the hostnames */
150
-int fix_actions(struct action* a)
150
+static int fix_actions(struct action* a)
151 151
 {
152 152
 	struct action *t;
153 153
 	struct proxy_l* p;
... ...
@@ -159,6 +159,7 @@ int fix_actions(struct action* a)
159 159
 			case SEND_T:
160 160
 					switch(t->p1_type){
161 161
 						case NUMBER_ST:
162
+						case IP_ST: /* for now ip_st==number_st*/
162 163
 							tmp=strdup(inet_ntoa(
163 164
 										*(struct in_addr*)&t->p1.number));
164 165
 							if (tmp==0){
... ...
@@ -177,7 +178,8 @@ int fix_actions(struct action* a)
177 177
 							break;
178 178
 						default:
179 179
 							LOG(L_CRIT, "BUG: fix_actions: invalid type"
180
-									" (should be string or number)\n");
180
+									"%d (should be string or number)\n",
181
+										t->type);
181 182
 							return E_BUG;
182 183
 					}
183 184
 					break;
... ...
@@ -189,7 +191,7 @@ int fix_actions(struct action* a)
189 189
 
190 190
 
191 191
 /* eval_elem helping function, returns str op param */
192
-int comp_str(char* str, void* param, int op, int subtype)
192
+static int comp_str(char* str, void* param, int op, int subtype)
193 193
 {
194 194
 	int ret;
195 195
 	
... ...
@@ -221,7 +223,7 @@ error:
221 221
 
222 222
 
223 223
 /* eval_elem helping function, returns a op param */
224
-int comp_ip(unsigned a, void* param, int op, int subtype)
224
+static int comp_ip(unsigned a, void* param, int op, int subtype)
225 225
 {
226 226
 	struct hostent* he;
227 227
 	char ** h;
... ...
@@ -266,7 +268,7 @@ error:
266 266
 
267 267
 
268 268
 /* returns: 0/1 (false/true) or -1 on error */
269
-int eval_elem(struct expr* e, struct sip_msg* msg)
269
+static int eval_elem(struct expr* e, struct sip_msg* msg)
270 270
 {
271 271
 
272 272
 	int ret;
... ...
@@ -304,7 +306,7 @@ error:
304 304
 
305 305
 
306 306
 
307
-int eval_expr(struct expr* e, struct sip_msg* msg)
307
+static int eval_expr(struct expr* e, struct sip_msg* msg)
308 308
 {
309 309
 	static int rec_lev=0;
310 310
 	int ret;
... ...
@@ -359,16 +361,14 @@ int add_rule(struct expr* e, struct action* a, struct route_elem** head)
359 359
 {
360 360
 	
361 361
 	struct route_elem* re;
362
-	struct hostent * he;
363 362
 	int ret;
364
-	int i,len, len2;
365 363
 
366 364
 	re=init_re();
367 365
 	if (re==0) return E_OUT_OF_MEM;
368 366
 	LOG(L_DBG, "add_rule: fixing expr...\n");
369 367
 	if ((ret=fix_expr(e))!=0) goto error;
370 368
 	LOG(L_DBG, "add_rule: fixing actions...\n");
371
-	if ((ret=fix_action(a))!=0) goto error;
369
+	if ((ret=fix_actions(a))!=0) goto error;
372 370
 	re->condition=e;
373 371
 	re->actions=a;
374 372
 	
... ...
@@ -404,18 +404,20 @@ void print_rl()
404 404
 	struct route_elem* t;
405 405
 	int i,j;
406 406
 
407
-	if (rlist==0){
408
-		printf("the routing table is empty\n");
409
-		return;
410
-	}
411
-	
412
-	for (t=rlist[0],i=0; t; i++, t=t->next){
413
-		printf("%2d.condition: ");
414
-		print_expr(t->condition);
415
-		printf("\n  -> ");
416
-		print_action(t->actions);
417
-		printf("\n    Statistics: tx=%d, errors=%d, tx_bytes=%d\n",
418
-				t->tx, t->errors, t->tx_bytes);
407
+	for(j=0; j<RT_NO; j++){
408
+		if (rlist[j]==0){
409
+			if (j==0) printf("WARNING: the main routing table is empty\n");
410
+			continue;
411
+		}
412
+		printf("routing table %d:\n",j);
413
+		for (t=rlist[j],i=0; t; i++, t=t->next){
414
+			printf("%2d.condition: ",i);
415
+			print_expr(t->condition);
416
+			printf("\n  -> ");
417
+			print_action(t->actions);
418
+			printf("\n    Statistics: tx=%d, errors=%d, tx_bytes=%d\n",
419
+					t->tx, t->errors, t->tx_bytes);
420
+		}
419 421
 	}
420 422
 
421 423
 }
... ...
@@ -8,6 +8,7 @@
8 8
 #include  "route_struct.h"
9 9
 
10 10
 #include <stdio.h>
11
+#include <stdlib.h>
11 12
 #include <netinet/in.h>
12 13
 
13 14
 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
... ...
@@ -96,7 +97,6 @@ error:
96 96
 
97 97
 void print_ip(unsigned ip)
98 98
 {
99
-	ip=htonl(ip);
100 99
 	printf("%d.%d.%d.%d", ((unsigned char*)&ip)[0],
101 100
 						  ((unsigned char*)&ip)[1],
102 101
 						  ((unsigned char*)&ip)[2],
... ...
@@ -162,7 +162,7 @@ void print_expr(struct expr* exp)
162 162
 					print_ip(exp->r.intval);
163 163
 					break;
164 164
 			default:
165
-					printf("UNKNOWN");
165
+					printf("type<%d>", exp->subtype);
166 166
 		}
167 167
 	}else if (exp->type==EXP_T){
168 168
 		switch(exp->op){
... ...
@@ -2,6 +2,8 @@
2 2
  * $Id$
3 3
  */
4 4
 
5
+#include <stdlib.h>
6
+#include <string.h>
5 7
 #include <sys/types.h>
6 8
 #include <sys/socket.h>
7 9
 #include <netinet/in.h>
... ...
@@ -11,6 +13,7 @@
11 11
 #include "udp_server.h"
12 12
 #include "config.h"
13 13
 #include "dprint.h"
14
+#include "receive.h"
14 15
 
15 16
 
16 17
 int udp_sock;
... ...
@@ -33,7 +36,7 @@ int udp_init(unsigned long ip, unsigned short port)
33 33
 
34 34
 	udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
35 35
 	if (udp_sock==-1){
36
-		LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror());
36
+		LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror(errno));
37 37
 		goto error;
38 38
 	}
39 39
 	/* set sock opts? */
... ...
@@ -41,12 +44,12 @@ int udp_init(unsigned long ip, unsigned short port)
41 41
 	if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR,
42 42
 					(void*)&optval, sizeof(optval)) ==-1)
43 43
 	{
44
-		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror());
44
+		LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno));
45 45
 		goto error;
46 46
 	}
47 47
 
48 48
 	if (bind(udp_sock, (struct sockaddr*) addr, sizeof(struct sockaddr))==-1){
49
-		LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror());
49
+		LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror(errno));
50 50
 		goto error;
51 51
 	}
52 52
 
... ...
@@ -62,7 +65,7 @@ error:
62 62
 
63 63
 int udp_rcv_loop()
64 64
 {
65
-	int len;
65
+	unsigned len;
66 66
 	char buf[BUF_SIZE+1];
67 67
 	struct sockaddr* from;
68 68
 	int fromlen;
... ...
@@ -77,7 +80,8 @@ int udp_rcv_loop()
77 77
 		fromlen=sizeof(struct sockaddr);
78 78
 		len=recvfrom(udp_sock, buf, BUF_SIZE, 0, from, &fromlen);
79 79
 		if (len==-1){
80
-			LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n", strerror());
80
+			LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n",
81
+						strerror(errno));
81 82
 			if (errno==EINTR)	goto skip;
82 83
 			else goto error;
83 84
 		}
... ...
@@ -101,14 +105,14 @@ error:
101 101
 
102 102
 
103 103
 /* which socket to use? main socket or new one? */
104
-int udp_send(char *buf, int len, struct sockaddr*  to, int tolen)
104
+int udp_send(char *buf, unsigned len, struct sockaddr*  to, unsigned tolen)
105 105
 {
106 106
 
107 107
 	int n;
108 108
 again:
109 109
 	n=sendto(udp_sock, buf, len, 0, to, tolen);
110 110
 	if (n==-1){
111
-		LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror());
111
+		LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror(errno));
112 112
 		if (errno==EINTR) goto again;
113 113
 	}
114 114
 	return n;
... ...
@@ -10,6 +10,7 @@
10 10
 extern int udp_sock;
11 11
 
12 12
 int udp_init(unsigned long ip, unsigned short port);
13
+int udp_send(char *buf, unsigned len, struct sockaddr*  to, unsigned tolen);
13 14
 int udp_rcv_loop();
14 15
 
15 16