Browse code

- compiles ok

Andrei Pelinescu-Onciul authored on 21/09/2001 15:24:24
Showing 10 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,124 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+
5
+
6
+#include "action.h"
7
+#include "config.h"
8
+#include "error.h"
9
+#include "dprint.h"
10
+#include "proxy.h"
11
+
12
+#include <netdb.h>
13
+#include <stdlib.h>
14
+
15
+/* ret= 0 if action -> end of lis t(e.g DROP), >0
16
+   and >0 on error */
17
+int do_action(struct action* a, struct sip_msg* msg)
18
+{
19
+	int ret;
20
+	struct sockaddr_in* to;
21
+	struct proxy_l* p;
22
+
23
+	switch (a->type){
24
+		case DROP_T:
25
+				ret=0;
26
+			break;
27
+		case FORWARD_T:
28
+			if (a->p1_type!= PROXY_ST){
29
+				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
30
+				ret=E_BUG;
31
+				break;
32
+			}
33
+			ret=forward_request(msg, (struct proxy_l*)a->p1.data);
34
+			if (ret>=0) ret=1;
35
+			break;
36
+		case SEND_T:
37
+			to=(struct sockaddr_in*) malloc(sizeof(struct sockaddr));
38
+			if (to==0){
39
+				LOG(L_ERR, "ERROR: do_action: "
40
+							"memory allocation failure\n");
41
+				ret=E_OUT_OF_MEM;
42
+				break;
43
+			}
44
+			if (a->p1_type!= PROXY_ST){
45
+				LOG(L_CRIT, "BUG: do_action: bad type %d\n", a->p1_type);
46
+				ret=E_BUG;
47
+				break;
48
+			}
49
+			
50
+			p=(struct proxy_l*)a->p1.data;
51
+			
52
+			to->sin_family = AF_INET;
53
+			to->sin_port=(p->port)?htons(p->port):htons(SIP_PORT);
54
+			if (p->ok==0){
55
+				if (p->host.h_addr_list[p->addr_idx+1])
56
+					p->addr_idx++;
57
+				p->ok=1;
58
+			}
59
+			to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
60
+			p->tx++;
61
+			p->tx_bytes+=msg->len;
62
+			ret=udp_send(msg->orig, msg->len, to, sizeof(struct sockaddr));
63
+			free(to);
64
+			if (ret<0){
65
+				p->errors++;
66
+				p->ok=0;
67
+			}else ret=1;
68
+			
69
+			break;
70
+		case LOG_T:
71
+			LOG(a->p2.number, a->p1.string);
72
+			ret=1;
73
+			break;
74
+		case ERROR_T:
75
+			LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
76
+					"not implemented yet\n", a->p1.string, a->p2.string);
77
+			ret=1;
78
+			break;
79
+		case ROUTE_T:
80
+			LOG(L_NOTICE, "WARNING: do_action: route(%d) not implemented "
81
+							"yet\n", a->p1.number);
82
+			break;
83
+		case EXEC_T:
84
+			LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,",
85
+						" using dumb version...\n", a->p1.string);
86
+			ret=system(a->p1.string);
87
+			if (ret!=0){
88
+				LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
89
+			}
90
+			ret=1;
91
+			break;
92
+		default:
93
+			LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
94
+	}
95
+	return ret;
96
+}
97
+
98
+
99
+
100
+/* returns: 0 on success, -1 on error */
101
+int run_actions(struct action* a, struct sip_msg* msg)
102
+{
103
+	struct action* t;
104
+	int ret;
105
+	
106
+	if (a==0){
107
+		LOG(L_ERR, "WARNING: run_actions: null action list\n");
108
+		ret=0;
109
+	}
110
+
111
+	for (t=a; t!=0; t=t->next){
112
+		ret=do_action(t, msg);
113
+		if(ret==0) break;
114
+		else if (ret<0){ ret=-1; goto error; }
115
+	}
116
+	ret=0;
117
+
118
+error:
119
+	return ret;
120
+}
121
+
122
+
123
+
0 124
new file mode 100644
... ...
@@ -0,0 +1,19 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ */
4
+
5
+#ifndef action_h
6
+#define action_h
7
+
8
+#include "msg_parser.h"
9
+#include "route_struct.h"
10
+
11
+int do_action(struct action* a, struct sip_msg* msg);
12
+int run_actions(struct action* a, struct sip_msg* msg);
13
+
14
+
15
+
16
+
17
+
18
+#endif
... ...
@@ -58,19 +58,22 @@ int check_address(unsigned long ip, char *name, int resolver)
58 58
 
59 59
 
60 60
 
61
-int forward_request(char * orig, char* buf, 
62
-					 unsigned int len,
63
-					 struct sip_msg* msg,
64
-					 struct route_elem* re,
61
+int forward_request( struct sip_msg* msg,
62
+					 struct proxy_l * p,
65 63
 					 unsigned long source_ip)
66 64
 {
67
-	unsigned int new_len, via_len, received_len;
65
+	unsigned int len, new_len, via_len, received_len;
68 66
 	char line_buf[MAX_VIA_LINE_SIZE];
69 67
 	char received_buf[MAX_RECEIVED_SIZE];
70 68
 	char* new_buf;
69
+	char* orig;
70
+	char* buf;
71 71
 	int offset, s_offset, size;
72 72
 	struct sockaddr_in* to;
73 73
 
74
+	orig=msg->orig;
75
+	buf=msg->buf;
76
+	len=msg->len;
74 77
 	received_len=0;
75 78
 	new_buf=0;
76 79
 	to=0;
... ...
@@ -131,21 +134,21 @@ int forward_request(char * orig, char* buf,
131 131
 			len, new_len, via_len, received_len);
132 132
 
133 133
 	to->sin_family = AF_INET;
134
-	to->sin_port = (re->port)?htons(re->port):htons(SIP_PORT);
134
+	to->sin_port = (p->port)?htons(p->port):htons(SIP_PORT);
135 135
 	/* if error try next ip address if possible */
136
-	if (re->ok==0){
137
-		if (re->host.h_addr_list[re->current_addr_idx+1])
138
-			re->current_addr_idx++;
139
-		re->ok=1;
136
+	if (p->ok==0){
137
+		if (p->host.h_addr_list[p->addr_idx+1])
138
+			p->addr_idx++;
139
+		p->ok=1;
140 140
 	}
141 141
 	/* ? not 64bit clean?*/
142
-	to->sin_addr.s_addr=*((long*)re->host.h_addr_list[re->current_addr_idx]);
142
+	to->sin_addr.s_addr=*((long*)p->host.h_addr_list[p->addr_idx]);
143 143
 
144
-	re->tx++;
145
-	re->tx_bytes+=new_len;
144
+	p->tx++;
145
+	p->tx_bytes+=new_len;
146 146
 	if (udp_send(new_buf, new_len, to, sizeof(struct sockaddr))==-1){
147
-			re->errors++;
148
-			re->ok=0;
147
+			p->errors++;
148
+			p->ok=0;
149 149
 			goto error;
150 150
 	}
151 151
 
... ...
@@ -162,9 +165,7 @@ error:
162 162
 
163 163
 
164 164
 /* removes first via & sends msg to the second */
165
-int forward_reply(char * orig, char* buf, 
166
-					 unsigned int len,
167
-					 struct sip_msg* msg)
165
+int forward_reply(struct sip_msg* msg)
168 166
 {
169 167
 
170 168
 
... ...
@@ -173,7 +174,14 @@ int forward_reply(char * orig, char* buf,
173 173
 	int offset, s_offset, size;
174 174
 	struct hostent* he;
175 175
 	struct sockaddr_in* to;
176
+	char* orig;
177
+	char* buf;
178
+	unsigned int len;
179
+	
176 180
 
181
+	orig=msg->orig;
182
+	buf=msg->buf;
183
+	len=msg->len;
177 184
 	new_buf=0;
178 185
 	to=0;
179 186
 	to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
... ...
@@ -8,15 +8,14 @@
8 8
 
9 9
 #include "msg_parser.h"
10 10
 #include "route.h"
11
+#include "proxy.h"
11 12
 
12 13
 
13 14
 int check_address(unsigned long ip, char *name, int resolver);
14 15
 
15
-int forward_request(char * orig, char* buf, unsigned int len,
16
-					 struct sip_msg* msg,  struct route_elem* re,
16
+int forward_request( struct sip_msg* msg,  struct proxy_l* p,
17 17
 					 unsigned long source_ip);
18 18
 
19
-int forward_reply(char * orig, char* buf, unsigned int len, 
20
-					struct sip_msg* msg);
19
+int forward_reply( struct sip_msg* msg);
21 20
 
22 21
 #endif
... ...
@@ -64,8 +64,15 @@ struct sip_msg{
64 64
 	struct msg_start first_line;
65 65
 	struct via_body via1;
66 66
 	struct via_body via2;
67
+
67 68
 	unsigned int src_ip;
68 69
 	unsigned int dst_ip;
70
+	char* orig; /* original message copy */
71
+	char* buf;  /* scratch pad, holds a modfied message,
72
+				   via, etc. point into it */
73
+				   
74
+	unsigned int len; /* message len (orig) */
75
+	
69 76
 };
70 77
 
71 78
 
... ...
@@ -8,6 +8,7 @@
8 8
 
9 9
 #include "proxy.h"
10 10
 #include "error.h"
11
+#include "dprint.h"
11 12
 
12 13
 #include <string.h>
13 14
 
... ...
@@ -30,9 +31,9 @@ struct proxy_l* find_proxy(char *name, unsigned short port)
30 30
 
31 31
 
32 32
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
33
-int hostent_cpy(struct hostent *dst, struct hosten* src)
33
+int hostent_cpy(struct hostent *dst, struct hostent* src)
34 34
 {
35
-	int len, r;
35
+	int len, r,ret,i,len2;
36 36
 
37 37
 	/* start copying the host entry.. */
38 38
 	/* copy h_name */
... ...
@@ -55,7 +56,7 @@ int hostent_cpy(struct hostent *dst, struct hosten* src)
55 55
 	memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );
56 56
 	for (i=0;i<len;i++){
57 57
 		len2=strlen(src->h_aliases[i])+1;
58
-		dst->.h_aliases[i]=(char*)malloc(sizeof(char)*len2);
58
+		dst->h_aliases[i]=(char*)malloc(sizeof(char)*len2);
59 59
 		if (dst->h_aliases==0){
60 60
 			ret=E_OUT_OF_MEM;
61 61
 			free(dst->h_name);
... ...
@@ -71,19 +72,19 @@ int hostent_cpy(struct hostent *dst, struct hosten* src)
71 71
 	if (dst->h_addr_list==0){
72 72
 		ret=E_OUT_OF_MEM;
73 73
 		free(dst->h_name);
74
-		for(r=0; h_aliases[r]; r++)	free(dst->h_aliases[r]);
75
-		free h_aliases[r];
74
+		for(r=0; dst->h_aliases[r]; r++)	free(dst->h_aliases[r]);
75
+		free(dst->h_aliases[r]);
76 76
 		free(dst->h_aliases);
77 77
 		goto error;
78 78
 	}
79
-	memset((void*)dst->.h_addr_list, 0, sizeof(char*) * (len+1) );
79
+	memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );
80 80
 	for (i=0;i<len;i++){
81 81
 		dst->h_addr_list[i]=(char*)malloc(sizeof(char)*src->h_length);
82 82
 		if (dst->h_addr_list[i]==0){
83 83
 			ret=E_OUT_OF_MEM;
84 84
 			free(dst->h_name);
85
-			for(r=0; h_aliases[r]; r++)	free(dst->h_aliases[r]);
86
-			free h_aliases[r];
85
+			for(r=0; dst->h_aliases[r]; r++)	free(dst->h_aliases[r]);
86
+			free(dst->h_aliases[r]);
87 87
 			free(dst->h_aliases);
88 88
 			for (r=0; r<i;r++) free(dst->h_addr_list[r]);
89 89
 			free(dst->h_addr_list);
... ...
@@ -94,7 +95,7 @@ int hostent_cpy(struct hostent *dst, struct hosten* src)
94 94
 
95 95
 	/* copy h_addr_type & length */
96 96
 	dst->h_addrtype=src->h_addrtype;
97
-	dst->host.h_length=src->h_length;
97
+	dst->h_length=src->h_length;
98 98
 	/*finished hostent copy */
99 99
 	
100 100
 	return 0;
... ...
@@ -108,8 +109,8 @@ error:
108 108
 
109 109
 struct proxy_l* add_proxy(char* name, unsigned short port)
110 110
 {
111
-	proxy_l* p;
112
-	struct hostent he;
111
+	struct proxy_l* p;
112
+	struct hostent* he;
113 113
 	
114 114
 	if ((p=find_proxy(name, port))!=0) return p;
115 115
 	p=(struct proxy_l*) malloc(sizeof(struct proxy_l));
... ...
@@ -117,7 +118,7 @@ struct proxy_l* add_proxy(char* name, unsigned short port)
117 117
 		LOG(L_CRIT, "ERROR: add_proxy: memory allocation failure\n");
118 118
 		goto error;
119 119
 	}
120
-	memset(p,0,sizeof(struct_proxy_l));
120
+	memset(p,0,sizeof(struct proxy_l));
121 121
 	p->name=name;
122 122
 	p->port=port;
123 123
 	he=gethostbyname(name);
... ...
@@ -27,6 +27,8 @@ struct proxy_l{
27 27
 
28 28
 extern struct proxy_l* proxies;
29 29
 
30
+struct proxy_l* add_proxy(char* name, unsigned short port);
31
+
30 32
 
31 33
 #endif
32 34
 
... ...
@@ -15,15 +15,18 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
15 15
 {
16 16
 	struct sip_msg msg;
17 17
 	struct route_elem *re;
18
-	char * orig;
19 18
 
19
+	/* fill in msg */
20
+	msg.buf=buf;
21
+	msg.len=len;
22
+	msg.src_ip=src_ip;
20 23
 	/* make a copy of the message */
21
-	orig=(char*) malloc(len);
22
-	if (orig==0){
24
+	msg.orig=(char*) malloc(len);
25
+	if (msg.orig==0){
23 26
 		LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
24 27
 		goto error1;
25 28
 	}
26
-	memcpy(orig, buf, len);
29
+	memcpy(msg.orig, buf, len);
27 30
 	
28 31
 	if (parse_msg(buf,len, &msg)!=0){
29 32
 		goto error;
... ...
@@ -38,10 +41,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
38 38
 		/* check if neccesarry to add receive? */
39 39
 		
40 40
 		/* find route */
41
-		re=route_match(  msg.first_line.u.request.method,
42
-						 msg.first_line.u.request.uri,
43
-						 &rlist
44
-					  );
41
+		re=route_match( &msg, &rlist[0]);
45 42
 		if (re==0){
46 43
 			/* no route found, send back error msg? */
47 44
 			LOG(L_WARN, "WARNING: receive_msg: no route found!\n");
... ...
@@ -49,8 +49,12 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
49 49
 		}
50 50
 		re->tx++;
51 51
 		/* send msg */
52
-		DBG(" found route to: %s\n", re->host.h_name);
53
-		forward_request(orig, buf, len, &msg, re, src_ip);
52
+		DBG(" found route \n");
53
+		if (run_actions(re->actions)<0){
54
+			LOG(L_WARN, "WARNING: receive_msg: "
55
+					"error while trying actions\n");
56
+			goto error;
57
+		}
54 58
 	}else if (msg.first_line.type==SIP_REPLY){
55 59
 		/* sanity checks */
56 60
 		if (msg.via1.error!=VIA_PARSE_OK){
... ...
@@ -64,17 +68,17 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
64 64
 		/* check if via1 == us */
65 65
 		
66 66
 		/* send the msg */
67
-		if (forward_reply(orig, buf, len, &msg)==0){
67
+		if (forward_reply(&msg)==0){
68 68
 			DBG(" reply forwarded to %s:%d\n", 
69 69
 						msg.via2.host,
70 70
 						(unsigned short) msg.via2.port);
71 71
 		}
72 72
 	}
73 73
 skip:
74
-	free(orig);
74
+	free(msg.orig);
75 75
 	return 0;
76 76
 error:
77
-	free(orig);
77
+	free(msg.orig);
78 78
 error1:
79 79
 	return -1;
80 80
 }
... ...
@@ -15,8 +15,8 @@
15 15
 #include <netdb.h>
16 16
 
17 17
 #include "route.h"
18
-#include "cfg_parser.h"
19 18
 #include "dprint.h"
19
+#include "proxy.h"
20 20
 
21 21
 /* main routing list */
22 22
 struct route_elem* rlist[RT_NO];
... ...
@@ -150,7 +150,7 @@ int fix_expr(struct expr* exp)
150 150
 int fix_actions(struct action* a)
151 151
 {
152 152
 	struct action *t;
153
-	struct proxy* p;
153
+	struct proxy_l* p;
154 154
 	char *tmp;
155 155
 	
156 156
 	for(t=a; t!=0; t=t->next){
... ...
@@ -234,7 +234,7 @@ void print_action(struct action* a)
234 234
 					printf("%d",t->p1.number);
235 235
 					break;
236 236
 			case IP_ST:
237
-					print_ip(t->p1.data);
237
+					print_ip(t->p1.number);
238 238
 					break;
239 239
 			default:
240 240
 					printf("type<%d>", t->p1_type);