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 50
 #include "parser/msg_parser.h"
51 51
 #include "route.h"
52 52
 #include "dprint.h"
53
-#include "udp_server.h"
54 53
 #include "globals.h"
55 54
 #include "data_lump.h"
56 55
 #include "ut.h"
57 56
 #include "mem/mem.h"
58 57
 #include "msg_translator.h"
59 58
 #include "sr_module.h"
60
-#include "stats.h"
61 59
 #include "ip_addr.h"
62 60
 #include "resolve.h"
63 61
 #include "name_alias.h"
64
-#ifdef USE_TCP
65
-#include "tcp_server.h"
66
-#endif
67 62
 
68 63
 #ifdef DEBUG_DMALLOC
69 64
 #include <dmalloc.h>
... ...
@@ -315,34 +313,14 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
315 315
 	DBG("Sending:\n%.*s.\n", (int)len, buf);
316 316
 	DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
317 317
 	
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);
318
+	if (msg_send(send_sock, proto, to, 0, buf, len)<0){
341 319
 		ser_error=E_SEND;
320
+		p->errors++;
321
+		p->ok=0;
342 322
 		STATS_TX_DROPS;
343 323
 		goto error1;
344 324
 	}
345
-
325
+	
346 326
 	/* sent requests stats */
347 327
 	STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
348 328
 	
... ...
@@ -410,22 +388,23 @@ int update_sock_struct_from_via( union sockaddr_union* to,
410 410
 }
411 411
 
412 412
 
413
+
413 414
 /* removes first via & sends msg to the second */
414 415
 int forward_reply(struct sip_msg* msg)
415 416
 {
416 417
 	char* new_buf;
417 418
 	union sockaddr_union* to;
418
-	struct socket_info* send_sock;
419 419
 	unsigned int new_len;
420 420
 	struct sr_module *mod;
421 421
 	int proto;
422
+	int id; /* used only by tcp*/
422 423
 #ifdef USE_TCP
423 424
 	char* s;
424 425
 	int len;
425
-	int id;
426 426
 #endif
427 427
 	
428 428
 	to=0;
429
+	id=0;
429 430
 	new_buf=0;
430 431
 	/*check if first via host = us */
431 432
 	if (check_via){
... ...
@@ -470,22 +449,10 @@ int forward_reply(struct sip_msg* msg)
470 470
 
471 471
 	proto=msg->via2->proto;
472 472
 	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 473
 
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
-	}
474
+
486 475
 #ifdef USE_TCP
487
-	 else if (proto==PROTO_TCP){
488
-		 id=0;
476
+	if (proto==PROTO_TCP){
489 477
 		/* find id in i param if it exists */
490 478
 		if (msg->via1->i&&msg->via1->i->value.s){
491 479
 			s=msg->via1->i->value.s;
... ...
@@ -495,19 +462,11 @@ int forward_reply(struct sip_msg* msg)
495 495
 			DBG("forward_reply: id= %x\n", id);
496 496
 		}		
497 497
 				
498
-		if (tcp_send(new_buf, new_len,  to, id)<0)
499
-		{
500
-			STATS_TX_DROPS;
501
-			goto error;
502
-		}
503 498
 	} 
504 499
 #endif
505
-	else{
506
-		LOG(L_CRIT, "BUG: forward_reply: unknown proto %d\n", proto);
507
-		goto error;
508
-	}
500
+	if (msg_send(0, proto, to, id, new_buf, new_len)<0) goto error;
509 501
 #ifdef STATS
510
-		STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
502
+	STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
511 503
 #endif
512 504
 
513 505
 	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 35
 #include "proxy.h"
36 36
 #include "ip_addr.h"
37 37
 
38
+#include "stats.h"
39
+#include "udp_server.h"
40
+#ifdef USE_TCP
41
+#include "tcp_server.h"
42
+#endif
43
+
38 44
 
39 45
 struct socket_info* get_send_socket(union sockaddr_union* su, int proto);
40 46
 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 48
 
49 49
 int forward_reply( struct sip_msg* msg);
50 50
 
51
+
52
+
53
+/* params:
54
+ *  send_sock= 0 if already known (e.g. for udp in some cases), non-0 otherwise
55
+ *  proto=TCP|UDP
56
+ *  to = destination,
57
+ *  id - only used on tcp, it will force sending on connection "id" if id!=0 
58
+ *       and the connection exists, else it will send to "to" 
59
+ *       (usefull for sending replies on  the same connection as the request
60
+ *       that generated them; use 0 if you don't want this)
61
+ * returns: 0 if ok, -1 on error*/
62
+static inline int msg_send(	struct socket_info* send_sock, int proto,
63
+							union sockaddr_union* to, int id,
64
+							char* buf, int len)
65
+{
66
+	
67
+	if (proto==PROTO_UDP){
68
+		if (send_sock==0) send_sock=get_send_socket(to, proto);
69
+		if (send_sock==0){
70
+			LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
71
+			goto error;
72
+		}
73
+		if (udp_send(send_sock, buf, len, to)==-1){
74
+			STATS_TX_DROPS;
75
+			LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
76
+			goto error;
77
+		}
78
+	}
79
+#ifdef USE_TCP
80
+	else if (proto==PROTO_TCP){
81
+		if (tcp_send(buf, len, to, id)<0){
82
+			STATS_TX_DROPS;
83
+			LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
84
+			goto error;
85
+		}
86
+	}
87
+#endif
88
+	else{
89
+			LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", proto);
90
+			goto error;
91
+	}
92
+	return 0;
93
+error:
94
+	return -1;
95
+}
96
+
51 97
 #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