Browse code

- created a protocol independent send fucntion: msg_send - replaced the calls to tcp_send &udp_send w/ msg_send in forward.c - updated sl/sl_send_reply to work with tcp (not tested)

Andrei Pelinescu-Onciul authored on 11/02/2003 14:02:50
Showing 4 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 VERSION = 0
9 9
 PATCHLEVEL = 8
10 10
 SUBLEVEL =   11
11
-EXTRAVERSION = pre6-tcp3
11
+EXTRAVERSION = pre6-tcp4
12 12
 
13 13
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
14 14
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -26,11 +26,14 @@
26 26
  *
27 27
  * History:
28 28
  * -------
29
- * 2003-01-23 support for determination of outbound interface added :
30
- *            get_out_socket (jiri)
31
- * 2003-01-24 reply to rport support added, contributed by
32
- *             Maxim Sobolev <sobomax@FreeBSD.org> and modified by andrei
33
- *
29
+ * 2001-??-??  created by andrei
30
+ * ????-??-??  lots of changes by a lot of people
31
+ * 2003-01-23  support for determination of outbound interface added :
32
+ *              get_out_socket (jiri)
33
+ * 2003-01-24  reply to rport support added, contributed by
34
+ *              Maxim Sobolev <sobomax@FreeBSD.org> and modified by andrei
35
+ * 2003-02-11  removed calls to upd_send & tcp_send & replaced them with
36
+ *              calls to msg_send (andrei)
34 37
  */
35 38
 
36 39
 
... ...
@@ -50,20 +53,15 @@
50 53
 #include "parser/msg_parser.h"
51 54
 #include "route.h"
52 55
 #include "dprint.h"
53
-#include "udp_server.h"
54 56
 #include "globals.h"
55 57
 #include "data_lump.h"
56 58
 #include "ut.h"
57 59
 #include "mem/mem.h"
58 60
 #include "msg_translator.h"
59 61
 #include "sr_module.h"
60
-#include "stats.h"
61 62
 #include "ip_addr.h"
62 63
 #include "resolve.h"
63 64
 #include "name_alias.h"
64
-#ifdef USE_TCP
65
-#include "tcp_server.h"
66
-#endif
67 65
 
68 66
 #ifdef DEBUG_DMALLOC
69 67
 #include <dmalloc.h>
... ...
@@ -315,34 +313,14 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
315 313
 	DBG("Sending:\n%.*s.\n", (int)len, buf);
316 314
 	DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
317 315
 	
318
-	
319
-	if (proto==PROTO_UDP){
320
-		if (udp_send(send_sock, buf, len,  to)==-1){
321
-				ser_error=E_SEND;
322
-				p->errors++;
323
-				p->ok=0;
324
-				STATS_TX_DROPS;
325
-				goto error1;
326
-		}
327
-	}
328
-#ifdef USE_TCP
329
-	 else if (proto==PROTO_TCP){
330
-		if (tcp_send(buf, len, to, 0)<0){
331
-				ser_error=E_SEND;
332
-				p->errors++;
333
-				p->ok=0;
334
-				STATS_TX_DROPS;
335
-				goto error1;
336
-		}
337
-	}
338
-#endif
339
-	 else{
340
-		LOG(L_CRIT, "BUG: forward_request: unknown proto %d\n", proto);
316
+	if (msg_send(send_sock, proto, to, 0, buf, len)<0){
341 317
 		ser_error=E_SEND;
318
+		p->errors++;
319
+		p->ok=0;
342 320
 		STATS_TX_DROPS;
343 321
 		goto error1;
344 322
 	}
345
-
323
+	
346 324
 	/* sent requests stats */
347 325
 	STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
348 326
 	
... ...
@@ -410,22 +388,23 @@ int update_sock_struct_from_via( union sockaddr_union* to,
410 388
 }
411 389
 
412 390
 
391
+
413 392
 /* removes first via & sends msg to the second */
414 393
 int forward_reply(struct sip_msg* msg)
415 394
 {
416 395
 	char* new_buf;
417 396
 	union sockaddr_union* to;
418
-	struct socket_info* send_sock;
419 397
 	unsigned int new_len;
420 398
 	struct sr_module *mod;
421 399
 	int proto;
400
+	int id; /* used only by tcp*/
422 401
 #ifdef USE_TCP
423 402
 	char* s;
424 403
 	int len;
425
-	int id;
426 404
 #endif
427 405
 	
428 406
 	to=0;
407
+	id=0;
429 408
 	new_buf=0;
430 409
 	/*check if first via host = us */
431 410
 	if (check_via){
... ...
@@ -470,22 +449,10 @@ int forward_reply(struct sip_msg* msg)
470 449
 
471 450
 	proto=msg->via2->proto;
472 451
 	if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
473
-	send_sock=get_send_socket(to, proto);
474
-	if (send_sock==0){
475
-		LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
476
-		goto error;
477
-	}
478 452
 
479
-	if (proto==PROTO_UDP){
480
-		if (udp_send(send_sock, new_buf,new_len,  to)==-1)
481
-		{
482
-			STATS_TX_DROPS;
483
-			goto error;
484
-		}
485
-	}
453
+
486 454
 #ifdef USE_TCP
487
-	 else if (proto==PROTO_TCP){
488
-		 id=0;
455
+	if (proto==PROTO_TCP){
489 456
 		/* find id in i param if it exists */
490 457
 		if (msg->via1->i&&msg->via1->i->value.s){
491 458
 			s=msg->via1->i->value.s;
... ...
@@ -495,19 +462,11 @@ int forward_reply(struct sip_msg* msg)
495 462
 			DBG("forward_reply: id= %x\n", id);
496 463
 		}		
497 464
 				
498
-		if (tcp_send(new_buf, new_len,  to, id)<0)
499
-		{
500
-			STATS_TX_DROPS;
501
-			goto error;
502
-		}
503 465
 	} 
504 466
 #endif
505
-	else{
506
-		LOG(L_CRIT, "BUG: forward_reply: unknown proto %d\n", proto);
507
-		goto error;
508
-	}
467
+	if (msg_send(0, proto, to, id, new_buf, new_len)<0) goto error;
509 468
 #ifdef STATS
510
-		STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
469
+	STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
511 470
 #endif
512 471
 
513 472
 	DBG(" reply forwarded to %.*s:%d\n", 
... ...
@@ -24,6 +24,13 @@
24 24
  * along with this program; if not, write to the Free Software 
25 25
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 26
  */
27
+/*
28
+ * History:
29
+ * -------
30
+ * 2001-??-?? created by andrei
31
+ * ????-??-?? lots of changes by a lot of people
32
+ * 2003-02-11 added inline msg_send (andrei)
33
+ */
27 34
 
28 35
 
29 36
 
... ...
@@ -35,6 +42,12 @@
35 42
 #include "proxy.h"
36 43
 #include "ip_addr.h"
37 44
 
45
+#include "stats.h"
46
+#include "udp_server.h"
47
+#ifdef USE_TCP
48
+#include "tcp_server.h"
49
+#endif
50
+
38 51
 
39 52
 struct socket_info* get_send_socket(union sockaddr_union* su, int proto);
40 53
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
... ...
@@ -48,4 +61,50 @@ int update_sock_struct_from_via( union sockaddr_union* to,
48 61
 
49 62
 int forward_reply( struct sip_msg* msg);
50 63
 
64
+
65
+
66
+/* params:
67
+ *  send_sock= 0 if already known (e.g. for udp in some cases), non-0 otherwise
68
+ *  proto=TCP|UDP
69
+ *  to = destination,
70
+ *  id - only used on tcp, it will force sending on connection "id" if id!=0 
71
+ *       and the connection exists, else it will send to "to" 
72
+ *       (usefull for sending replies on  the same connection as the request
73
+ *       that generated them; use 0 if you don't want this)
74
+ * returns: 0 if ok, -1 on error*/
75
+static inline int msg_send(	struct socket_info* send_sock, int proto,
76
+							union sockaddr_union* to, int id,
77
+							char* buf, int len)
78
+{
79
+	
80
+	if (proto==PROTO_UDP){
81
+		if (send_sock==0) send_sock=get_send_socket(to, proto);
82
+		if (send_sock==0){
83
+			LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
84
+			goto error;
85
+		}
86
+		if (udp_send(send_sock, buf, len, to)==-1){
87
+			STATS_TX_DROPS;
88
+			LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
89
+			goto error;
90
+		}
91
+	}
92
+#ifdef USE_TCP
93
+	else if (proto==PROTO_TCP){
94
+		if (tcp_send(buf, len, to, id)<0){
95
+			STATS_TX_DROPS;
96
+			LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
97
+			goto error;
98
+		}
99
+	}
100
+#endif
101
+	else{
102
+			LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", proto);
103
+			goto error;
104
+	}
105
+	return 0;
106
+error:
107
+	return -1;
108
+}
109
+
51 110
 #endif
... ...
@@ -92,7 +92,7 @@ struct receive_info{
92 92
 	short src_port;
93 93
 	short dst_port;
94 94
 	int proto;
95
-	int proto_reserved1;
95
+	int proto_reserved1; /* tcp stores the connection id here */
96 96
 	int proto_reserved2;
97 97
 	union sockaddr_union src_su; /* usefull for replies*/
98 98
 	struct socket_info* bind_address; /* sock_info structure on which