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