... | ... |
@@ -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 | 23 |
int ret; |
21 | 24 |
struct sockaddr_in* to; |
22 | 25 |
struct proxy_l* p; |
26 |
+ struct route_elem* re; |
|
23 | 27 |
|
28 |
+ ret=E_BUG; |
|
24 | 29 |
switch (a->type){ |
25 | 30 |
case DROP_T: |
26 | 31 |
ret=0; |
27 | 32 |
break; |
28 | 33 |
case FORWARD_T: |
29 |
- if (a->p1_type!= PROXY_ST){ |
|
30 |
- LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type); |
|
34 |
+ if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){ |
|
35 |
+ LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n", |
|
36 |
+ a->p1_type, a->p2_type); |
|
31 | 37 |
ret=E_BUG; |
32 | 38 |
break; |
33 | 39 |
} |
... | ... |
@@ -42,8 +48,9 @@ int do_action(struct action* a, struct sip_msg* msg) |
42 | 48 |
ret=E_OUT_OF_MEM; |
43 | 49 |
break; |
44 | 50 |
} |
45 |
- if (a->p1_type!= PROXY_ST){ |
|
46 |
- LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type); |
|
51 |
+ if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){ |
|
52 |
+ LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n", |
|
53 |
+ a->p1_type, a->p2_type); |
|
47 | 54 |
ret=E_BUG; |
48 | 55 |
break; |
49 | 56 |
} |
... | ... |
@@ -60,7 +67,8 @@ int do_action(struct action* a, struct sip_msg* msg) |
60 | 67 |
to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]); |
61 | 68 |
p->tx++; |
62 | 69 |
p->tx_bytes+=msg->len; |
63 |
- ret=udp_send(msg->orig, msg->len, to, sizeof(struct sockaddr)); |
|
70 |
+ ret=udp_send(msg->orig, msg->len, (struct sockaddr*)to, |
|
71 |
+ sizeof(struct sockaddr_in)); |
|
64 | 72 |
free(to); |
65 | 73 |
if (ret<0){ |
66 | 74 |
p->errors++; |
... | ... |
@@ -69,20 +77,56 @@ int do_action(struct action* a, struct sip_msg* msg) |
69 | 77 |
|
70 | 78 |
break; |
71 | 79 |
case LOG_T: |
72 |
- LOG(a->p2.number, a->p1.string); |
|
80 |
+ if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){ |
|
81 |
+ LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n", |
|
82 |
+ a->p1_type, a->p2_type); |
|
83 |
+ ret=E_BUG; |
|
84 |
+ break; |
|
85 |
+ } |
|
86 |
+ LOG(a->p1.number, a->p2.string); |
|
73 | 87 |
ret=1; |
74 | 88 |
break; |
75 | 89 |
case ERROR_T: |
90 |
+ if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){ |
|
91 |
+ LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n", |
|
92 |
+ a->p1_type, a->p2_type); |
|
93 |
+ ret=E_BUG; |
|
94 |
+ break; |
|
95 |
+ } |
|
76 | 96 |
LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") " |
77 | 97 |
"not implemented yet\n", a->p1.string, a->p2.string); |
78 | 98 |
ret=1; |
79 | 99 |
break; |
80 | 100 |
case ROUTE_T: |
81 |
- LOG(L_NOTICE, "WARNING: do_action: route(%d) not implemented " |
|
82 |
- "yet\n", a->p1.number); |
|
101 |
+ if (a->p1_type!=NUMBER_ST){ |
|
102 |
+ LOG(L_CRIT, "BUG: do_action: bad route() type %d\n", |
|
103 |
+ a->p1_type); |
|
104 |
+ ret=E_BUG; |
|
105 |
+ break; |
|
106 |
+ } |
|
107 |
+ if ((a->p1.number>RT_NO)||(a->p1.number<0)){ |
|
108 |
+ LOG(L_ERR, "ERROR: invalid routing table number in" |
|
109 |
+ "route(%d)\n", a->p1.number); |
|
110 |
+ ret=E_CFG; |
|
111 |
+ break; |
|
112 |
+ } |
|
113 |
+ re=route_match(msg, &rlist[a->p1.number]); |
|
114 |
+ if (re==0){ |
|
115 |
+ LOG(L_INFO, "WARNING: do_action: route(%d): no new route" |
|
116 |
+ " found\n", a->p1.number); |
|
117 |
+ ret=1; |
|
118 |
+ break; |
|
119 |
+ } |
|
120 |
+ ret=((ret=run_actions(re->actions, msg))<0)?ret:1; |
|
83 | 121 |
break; |
84 | 122 |
case EXEC_T: |
85 |
- LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,", |
|
123 |
+ if (a->p1_type!=STRING_ST){ |
|
124 |
+ LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n", |
|
125 |
+ a->p1_type); |
|
126 |
+ ret=E_BUG; |
|
127 |
+ break; |
|
128 |
+ } |
|
129 |
+ LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented," |
|
86 | 130 |
" using dumb version...\n", a->p1.string); |
87 | 131 |
ret=system(a->p1.string); |
88 | 132 |
if (ret!=0){ |
... | ... |
@@ -103,7 +147,16 @@ int run_actions(struct action* a, struct sip_msg* msg) |
103 | 147 |
{ |
104 | 148 |
struct action* t; |
105 | 149 |
int ret; |
106 |
- |
|
150 |
+ static int rec_lev=0; |
|
151 |
+ |
|
152 |
+ rec_lev++; |
|
153 |
+ if (rec_lev>ROUTE_MAX_REC_LEV){ |
|
154 |
+ LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)" |
|
155 |
+ " giving up!\n", rec_lev); |
|
156 |
+ ret=E_UNSPEC; |
|
157 |
+ goto error; |
|
158 |
+ } |
|
159 |
+ |
|
107 | 160 |
if (a==0){ |
108 | 161 |
LOG(L_ERR, "WARNING: run_actions: null action list\n"); |
109 | 162 |
ret=0; |
... | ... |
@@ -114,9 +167,13 @@ int run_actions(struct action* a, struct sip_msg* msg) |
114 | 167 |
if(ret==0) break; |
115 | 168 |
else if (ret<0){ ret=-1; goto error; } |
116 | 169 |
} |
117 |
- ret=0; |
|
170 |
+ |
|
171 |
+ rec_lev--; |
|
172 |
+ return 0; |
|
173 |
+ |
|
118 | 174 |
|
119 | 175 |
error: |
176 |
+ rec_lev--; |
|
120 | 177 |
return ret; |
121 | 178 |
} |
122 | 179 |
|
... | ... |
@@ -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 | 22 |
struct expr* expr; |
18 | 23 |
struct action* action; |
19 | 24 |
struct net* net; |
25 |
+ struct route_elem* route_el; |
|
20 | 26 |
} |
21 | 27 |
|
22 | 28 |
/* terminals */ |
23 | 29 |
|
24 | 30 |
|
25 |
-/* keywors */ |
|
31 |
+/* keywords */ |
|
26 | 32 |
%token FORWARD |
27 | 33 |
%token SEND |
28 | 34 |
%token DROP |
... | ... |
@@ -78,6 +84,8 @@ |
78 | 84 |
%type <uval> ipv4 |
79 | 85 |
%type <net> net4 |
80 | 86 |
%type <strval> host |
87 |
+%type <route_el> rules; |
|
88 |
+%type <route_el> rule; |
|
81 | 89 |
|
82 | 90 |
|
83 | 91 |
|
... | ... |
@@ -87,9 +95,9 @@ |
87 | 95 |
cfg: statements |
88 | 96 |
; |
89 | 97 |
|
90 |
-statements: statements statement {printf("got <> <>\n");} |
|
91 |
- | statement {printf("got a statement<>\n"); } |
|
92 |
- | statements error { yyerror(""); } |
|
98 |
+statements: statements statement {} |
|
99 |
+ | statement {} |
|
100 |
+ | statements error { yyerror(""); YYABORT;} |
|
93 | 101 |
; |
94 | 102 |
|
95 | 103 |
statement: assign_stm CR |
... | ... |
@@ -125,20 +133,29 @@ ipv4: NUMBER DOT NUMBER DOT NUMBER DOT NUMBER { |
125 | 133 |
"address"); |
126 | 134 |
$$=0; |
127 | 135 |
}else{ |
128 |
- $$=($1<<24)|($3<<16)| |
|
129 |
- ($5<<8)|$7; |
|
136 |
+ $$=htonl( ($1<<24)| |
|
137 |
+ ($3<<16)| ($5<<8)|$7 ); |
|
130 | 138 |
} |
131 | 139 |
} |
132 | 140 |
; |
133 | 141 |
|
134 |
-route_stm: ROUTE LBRACE rules RBRACE |
|
135 |
- | ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE |
|
142 |
+route_stm: ROUTE LBRACE rules RBRACE { push($3, &rlist[DEFAULT_RT]); } |
|
143 |
+ |
|
144 |
+ | ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE { |
|
145 |
+ if (($3<RT_NO) && ($3>=0)){ |
|
146 |
+ push($6, &rlist[$3]); |
|
147 |
+ }else{ |
|
148 |
+ yyerror("invalid routing" |
|
149 |
+ "table number"); |
|
150 |
+ YYABORT; } |
|
151 |
+ } |
|
136 | 152 |
| ROUTE error { yyerror("invalid route statement"); } |
137 | 153 |
; |
138 | 154 |
|
139 |
-rules: rules rule |
|
140 |
- | rule |
|
141 |
- | rules error { yyerror("invalid rule"); } |
|
155 |
+rules: rules rule { push($2, &$1); $$=$1; |
|
156 |
+ printf(": rules->rules(%x) rule(%x)\n", $1,$2);} |
|
157 |
+ | rule {$$=$1; printf(": rules->rule (%x)\n",$1) } |
|
158 |
+ | rules error { $$=0; yyerror("invalid rule"); } |
|
142 | 159 |
; |
143 | 160 |
|
144 | 161 |
rule: condition actions CR { |
... | ... |
@@ -146,9 +163,16 @@ rule: condition actions CR { |
146 | 163 |
printf("expr: "); print_expr($1); |
147 | 164 |
printf("\n -> actions: "); |
148 | 165 |
print_action($2); printf("\n"); |
149 |
- } |
|
150 |
- | CR /* null rule */ |
|
151 |
- | condition error { yyerror("bad actions in rule"); } |
|
166 |
+ |
|
167 |
+ $$=0; |
|
168 |
+ if (add_rule($1, $2, &$$)<0) { |
|
169 |
+ yyerror("error calling add_rule"); |
|
170 |
+ YYABORT; |
|
171 |
+ } |
|
172 |
+ printf(": rule -> condition actions CR\n"); |
|
173 |
+ } |
|
174 |
+ | CR /* null rule */ { $$=0; printf(": rule-> CR!\n"); } |
|
175 |
+ | condition error { $$=0; yyerror("bad actions in rule"); } |
|
152 | 176 |
; |
153 | 177 |
|
154 | 178 |
condition: exp {$$=$1;} |
... | ... |
@@ -242,7 +266,7 @@ net4: ipv4 SLASH ipv4 { $$=mk_net($1, $3); } |
242 | 266 |
yyerror("invalid bit number in netmask"); |
243 | 267 |
$$=0; |
244 | 268 |
}else{ |
245 |
- $$=mk_net($1, (1<<$3)-1); |
|
269 |
+ $$=mk_net($1, htonl((1<<$3)-1)); |
|
246 | 270 |
} |
247 | 271 |
} |
248 | 272 |
| ipv4 { $$=mk_net($1, 0xffffffff); } |
... | ... |
@@ -396,7 +420,7 @@ cmd: FORWARD LPAREN host RPAREN { $$=mk_action( FORWARD_T, |
396 | 420 |
extern int line; |
397 | 421 |
extern int column; |
398 | 422 |
extern int startcolumn; |
399 |
-yyerror(char* s) |
|
423 |
+void yyerror(char* s) |
|
400 | 424 |
{ |
401 | 425 |
fprintf(stderr, "parse error (%d,%d-%d): %s\n", line, startcolumn, |
402 | 426 |
column, s); |
403 | 427 |
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 | 0 |
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 |
... | ... |
@@ -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 | 40 |
if (resolver&DO_DNS){ |
39 | 41 |
/* try all names ips */ |
40 | 42 |
he=gethostbyname(name); |
41 |
- for(i=0; he->h_addr_list[i];i++){ |
|
43 |
+ for(i=0;he && he->h_addr_list[i];i++){ |
|
42 | 44 |
if (*(unsigned long*)he->h_addr_list[i]==ip) |
43 | 45 |
return 0; |
44 | 46 |
} |
45 | 47 |
} |
46 | 48 |
if (resolver&DO_REV_DNS){ |
49 |
+ print_ip(ip); |
|
47 | 50 |
/* try reverse dns */ |
48 | 51 |
he=gethostbyaddr((char*)&ip, sizeof(ip), AF_INET); |
49 |
- if (strcmp(he->h_name, name)==0) |
|
52 |
+ if (he && (strcmp(he->h_name, name)==0)) |
|
50 | 53 |
return 0; |
51 |
- for (i=0; he->h_aliases[i];i++){ |
|
54 |
+ for (i=0; he && he->h_aliases[i];i++){ |
|
52 | 55 |
if (strcmp(he->h_aliases[i],name)==0) |
53 | 56 |
return 0; |
54 | 57 |
} |
... | ... |
@@ -58,9 +61,7 @@ int check_address(unsigned long ip, char *name, int resolver) |
58 | 61 |
|
59 | 62 |
|
60 | 63 |
|
61 |
-int forward_request( struct sip_msg* msg, |
|
62 |
- struct proxy_l * p, |
|
63 |
- unsigned long source_ip) |
|
64 |
+int forward_request( struct sip_msg* msg, struct proxy_l * p) |
|
64 | 65 |
{ |
65 | 66 |
unsigned int len, new_len, via_len, received_len; |
66 | 67 |
char line_buf[MAX_VIA_LINE_SIZE]; |
... | ... |
@@ -68,12 +69,14 @@ int forward_request( struct sip_msg* msg, |
68 | 69 |
char* new_buf; |
69 | 70 |
char* orig; |
70 | 71 |
char* buf; |
71 |
- int offset, s_offset, size; |
|
72 |
+ unsigned int offset, s_offset, size; |
|
72 | 73 |
struct sockaddr_in* to; |
74 |
+ unsigned long source_ip; |
|
73 | 75 |
|
74 | 76 |
orig=msg->orig; |
75 | 77 |
buf=msg->buf; |
76 | 78 |
len=msg->len; |
79 |
+ source_ip=msg->src_ip; |
|
77 | 80 |
received_len=0; |
78 | 81 |
new_buf=0; |
79 | 82 |
to=0; |
... | ... |
@@ -146,7 +149,8 @@ int forward_request( struct sip_msg* msg, |
146 | 149 |
|
147 | 150 |
p->tx++; |
148 | 151 |
p->tx_bytes+=new_len; |
149 |
- if (udp_send(new_buf, new_len, to, sizeof(struct sockaddr))==-1){ |
|
152 |
+ if (udp_send(new_buf, new_len, (struct sockaddr*) to, |
|
153 |
+ sizeof(struct sockaddr_in))==-1){ |
|
150 | 154 |
p->errors++; |
151 | 155 |
p->ok=0; |
152 | 156 |
goto error; |
... | ... |
@@ -171,7 +175,7 @@ int forward_reply(struct sip_msg* msg) |
171 | 175 |
|
172 | 176 |
unsigned int new_len, via_len,r; |
173 | 177 |
char* new_buf; |
174 |
- int offset, s_offset, size; |
|
178 |
+ unsigned offset, s_offset, size; |
|
175 | 179 |
struct hostent* he; |
176 | 180 |
struct sockaddr_in* to; |
177 | 181 |
char* orig; |
... | ... |
@@ -245,7 +249,8 @@ int forward_reply(struct sip_msg* msg) |
245 | 249 |
to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT); |
246 | 250 |
to->sin_addr.s_addr=*((long*)he->h_addr_list[0]); |
247 | 251 |
|
248 |
- if (udp_send(new_buf,new_len, to, sizeof(struct sockaddr))==-1) |
|
252 |
+ if (udp_send(new_buf,new_len, (struct sockaddr*) to, |
|
253 |
+ sizeof(struct sockaddr_in))==-1) |
|
249 | 254 |
goto error; |
250 | 255 |
|
251 | 256 |
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 | 22 |
|
21 | 23 |
|
22 | 24 |
static char id[]="@(#) $Id$"; |
23 |
-static char version[]="sip_router 0.3"; |
|
25 |
+static char version[]="sip_router 0.5"; |
|
24 | 26 |
static char help_msg[]= "\ |
25 | 27 |
Usage: sip_router -l address [-l address] [options]\n\ |
26 | 28 |
Options:\n\ |
... | ... |
@@ -90,6 +92,12 @@ int process_no = 0; |
90 | 92 |
#define MAX_FD 32 /* maximum number of inherited open file descriptors, |
91 | 93 |
(normally it shouldn't be bigger than 3) */ |
92 | 94 |
|
95 |
+ |
|
96 |
+extern FILE* yyin; |
|
97 |
+extern int yyparse(); |
|
98 |
+ |
|
99 |
+ |
|
100 |
+ |
|
93 | 101 |
/* daemon init, return 0 on success, -1 on error */ |
94 | 102 |
int daemonize(char* name) |
95 | 103 |
{ |
... | ... |
@@ -226,8 +234,8 @@ int main(int argc, char** argv) |
226 | 234 |
} |
227 | 235 |
break; |
228 | 236 |
case 'n': |
229 |
- children_no=strtol(optarg, tmp, 10); |
|
230 |
- if (tmp &&(*tmp)){ |
|
237 |
+ children_no=strtol(optarg, &tmp, 10); |
|
238 |
+ if ((tmp==0) ||(*tmp)){ |
|
231 | 239 |
fprintf(stderr, "bad process number: -n %s\n", optarg); |
232 | 240 |
goto error; |
233 | 241 |
} |
... | ... |
@@ -251,6 +259,7 @@ int main(int argc, char** argv) |
251 | 259 |
break; |
252 | 260 |
case 'V': |
253 | 261 |
printf("version: %s\n", version); |
262 |
+ printf("%s\n",id); |
|
254 | 263 |
exit(0); |
255 | 264 |
break; |
256 | 265 |
case 'h': |
... | ... |
@@ -319,13 +328,14 @@ int main(int argc, char** argv) |
319 | 328 |
strerror(errno)); |
320 | 329 |
goto error; |
321 | 330 |
} |
322 |
- |
|
323 |
- if (cfg_parse_stream(cfg_stream)!=0){ |
|
331 |
+ |
|
332 |
+ yyin=cfg_stream; |
|
333 |
+ if (yyparse()!=0){ |
|
324 | 334 |
fprintf(stderr, "ERROR: config parser failure\n"); |
325 | 335 |
goto error; |
326 | 336 |
} |
327 | 337 |
|
328 |
- |
|
338 |
+ |
|
329 | 339 |
print_rl(); |
330 | 340 |
|
331 | 341 |
|
... | ... |
@@ -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 | 352 |
struct hdr_field hf; |
352 | 353 |
struct via_body vb1, vb2; |
353 | 354 |
int offset; |
354 |
- int r; |
|
355 | 355 |
|
356 | 356 |
|
357 | 357 |
/* 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 | 20 |
|
20 | 21 |
/* searches for the proxy named 'name', on port 'port' |
21 | 22 |
returns: pointer to proxy_l on success or 0 if not found */ |
22 |
-struct proxy_l* find_proxy(char *name, unsigned short port) |
|
23 |
+static struct proxy_l* find_proxy(char *name, unsigned short port) |
|
23 | 24 |
{ |
24 | 25 |
struct proxy_l* t; |
25 | 26 |
for(t=proxies; t; t=t->next) |
... | ... |
@@ -31,9 +32,10 @@ struct proxy_l* find_proxy(char *name, unsigned short port) |
31 | 32 |
|
32 | 33 |
|
33 | 34 |
/* copies a hostent structure*, returns 0 on success, <0 on error*/ |
34 |
-int hostent_cpy(struct hostent *dst, struct hostent* src) |
|
35 |
+static int hostent_cpy(struct hostent *dst, struct hostent* src) |
|
35 | 36 |
{ |
36 |
- int len, r,ret,i,len2; |
|
37 |
+ unsigned len,len2; |
|
38 |
+ int r,ret,i; |
|
37 | 39 |
|
38 | 40 |
/* start copying the host entry.. */ |
39 | 41 |
/* 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 | 52 |
re->tx++; |
51 | 53 |
/* send msg */ |
52 | 54 |
DBG(" found route \n"); |
53 |
- if (run_actions(re->actions)<0){ |
|
55 |
+ if (run_actions(re->actions, &msg)<0){ |
|
54 | 56 |
LOG(L_WARN, "WARNING: receive_msg: " |
55 | 57 |
"error while trying actions\n"); |
56 | 58 |
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 | 26 |
|
26 | 27 |
void free_re(struct route_elem* r) |
27 | 28 |
{ |
28 |
- int i; |
|
29 |
+ /*int i;*/ |
|
29 | 30 |
if (r){ |
30 | 31 |
/* |
31 | 32 |
regfree(&(r->method)); |
... | ... |
@@ -59,11 +60,10 @@ struct route_elem* init_re() |
59 | 60 |
} |
60 | 61 |
|
61 | 62 |
|
62 |
- |
|
63 |
+/* adds re list to head; re must be null terminated (last re->next=0))*/ |
|
63 | 64 |
void push(struct route_elem* re, struct route_elem** head) |
64 | 65 |
{ |
65 | 66 |
struct route_elem *t; |
66 |
- re->next=0; |
|
67 | 67 |
if (*head==0){ |
68 | 68 |
*head=re; |
69 | 69 |
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 | 178 |
break; |
178 | 179 |
default: |
179 | 180 |
LOG(L_CRIT, "BUG: fix_actions: invalid type" |
180 |
- " (should be string or number)\n"); |
|
181 |
+ "%d (should be string or number)\n", |
|
182 |
+ t->type); |
|
181 | 183 |
return E_BUG; |
182 | 184 |
} |
183 | 185 |
break; |
... | ... |
@@ -189,7 +191,7 @@ int fix_actions(struct action* a) |
189 | 191 |
|
190 | 192 |
|
191 | 193 |
/* eval_elem helping function, returns str op param */ |
192 |
-int comp_str(char* str, void* param, int op, int subtype) |
|
194 |
+static int comp_str(char* str, void* param, int op, int subtype) |
|
193 | 195 |
{ |
194 | 196 |
int ret; |
195 | 197 |
|
... | ... |
@@ -221,7 +223,7 @@ error: |
221 | 223 |
|
222 | 224 |
|
223 | 225 |
/* eval_elem helping function, returns a op param */ |
224 |
-int comp_ip(unsigned a, void* param, int op, int subtype) |
|
226 |
+static int comp_ip(unsigned a, void* param, int op, int subtype) |
|
225 | 227 |
{ |
226 | 228 |
struct hostent* he; |
227 | 229 |
char ** h; |
... | ... |
@@ -266,7 +268,7 @@ error: |
266 | 268 |
|
267 | 269 |
|
268 | 270 |
/* returns: 0/1 (false/true) or -1 on error */ |
269 |
-int eval_elem(struct expr* e, struct sip_msg* msg) |
|
271 |
+static int eval_elem(struct expr* e, struct sip_msg* msg) |
|
270 | 272 |
{ |
271 | 273 |
|
272 | 274 |
int ret; |
... | ... |
@@ -304,7 +306,7 @@ error: |
304 | 306 |
|
305 | 307 |
|
306 | 308 |
|
307 |
-int eval_expr(struct expr* e, struct sip_msg* msg) |
|
309 |
+static int eval_expr(struct expr* e, struct sip_msg* msg) |
|
308 | 310 |
{ |
309 | 311 |
static int rec_lev=0; |
310 | 312 |
int ret; |
... | ... |
@@ -359,16 +361,14 @@ int add_rule(struct expr* e, struct action* a, struct route_elem** head) |
359 | 361 |
{ |
360 | 362 |
|
361 | 363 |
struct route_elem* re; |
362 |
- struct hostent * he; |
|
363 | 364 |
int ret; |
364 |
- int i,len, len2; |
|
365 | 365 |
|
366 | 366 |
re=init_re(); |
367 | 367 |
if (re==0) return E_OUT_OF_MEM; |
368 | 368 |
LOG(L_DBG, "add_rule: fixing expr...\n"); |
369 | 369 |
if ((ret=fix_expr(e))!=0) goto error; |
370 | 370 |
LOG(L_DBG, "add_rule: fixing actions...\n"); |
371 |
- if ((ret=fix_action(a))!=0) goto error; |
|
371 |
+ if ((ret=fix_actions(a))!=0) goto error; |
|
372 | 372 |
re->condition=e; |
373 | 373 |
re->actions=a; |
374 | 374 |
|
... | ... |
@@ -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 | 97 |
|
97 | 98 |
void print_ip(unsigned ip) |
98 | 99 |
{ |
99 |
- ip=htonl(ip); |
|
100 | 100 |
printf("%d.%d.%d.%d", ((unsigned char*)&ip)[0], |
101 | 101 |
((unsigned char*)&ip)[1], |
102 | 102 |
((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 | 13 |
#include "udp_server.h" |
12 | 14 |
#include "config.h" |
13 | 15 |
#include "dprint.h" |
16 |
+#include "receive.h" |
|
14 | 17 |
|
15 | 18 |
|
16 | 19 |
int udp_sock; |
... | ... |
@@ -33,7 +36,7 @@ int udp_init(unsigned long ip, unsigned short port) |
33 | 36 |
|
34 | 37 |
udp_sock = socket(PF_INET, SOCK_DGRAM, 0); |
35 | 38 |
if (udp_sock==-1){ |
36 |
- LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror()); |
|
39 |
+ LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror(errno)); |
|
37 | 40 |
goto error; |
38 | 41 |
} |
39 | 42 |
/* set sock opts? */ |
... | ... |
@@ -41,12 +44,12 @@ int udp_init(unsigned long ip, unsigned short port) |
41 | 44 |
if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR, |
42 | 45 |
(void*)&optval, sizeof(optval)) ==-1) |
43 | 46 |
{ |
44 |
- LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror()); |
|
47 |
+ LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror(errno)); |
|
45 | 48 |
goto error; |
46 | 49 |
} |
47 | 50 |
|
48 | 51 |
if (bind(udp_sock, (struct sockaddr*) addr, sizeof(struct sockaddr))==-1){ |
49 |
- LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror()); |
|
52 |
+ LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror(errno)); |
|
50 | 53 |
goto error; |
51 | 54 |
} |
52 | 55 |
|
... | ... |
@@ -62,7 +65,7 @@ error: |
62 | 65 |
|
63 | 66 |
int udp_rcv_loop() |
64 | 67 |
{ |
65 |
- int len; |
|
68 |
+ unsigned len; |
|
66 | 69 |
char buf[BUF_SIZE+1]; |
67 | 70 |
struct sockaddr* from; |
68 | 71 |
int fromlen; |
... | ... |
@@ -77,7 +80,8 @@ int udp_rcv_loop() |
77 | 80 |
fromlen=sizeof(struct sockaddr); |
78 | 81 |
len=recvfrom(udp_sock, buf, BUF_SIZE, 0, from, &fromlen); |
79 | 82 |
if (len==-1){ |
80 |
- LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n", strerror()); |
|
83 |
+ LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n", |
|
84 |
+ strerror(errno)); |
|
81 | 85 |
if (errno==EINTR) goto skip; |
82 | 86 |
else goto error; |
83 | 87 |
} |
... | ... |
@@ -101,14 +105,14 @@ error: |
101 | 105 |
|
102 | 106 |
|
103 | 107 |
/* which socket to use? main socket or new one? */ |
104 |
-int udp_send(char *buf, int len, struct sockaddr* to, int tolen) |
|
108 |
+int udp_send(char *buf, unsigned len, struct sockaddr* to, unsigned tolen) |
|
105 | 109 |
{ |
106 | 110 |
|
107 | 111 |
int n; |
108 | 112 |
again: |
109 | 113 |
n=sendto(udp_sock, buf, len, 0, to, tolen); |
110 | 114 |
if (n==-1){ |
111 |
- LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror()); |
|
115 |
+ LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror(errno)); |
|
112 | 116 |
if (errno==EINTR) goto again; |
113 | 117 |
} |
114 | 118 |
return n; |