瀏覽代碼

- 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 22 年之前
父節點
當前提交
c030910cb4
共有 4 個文件被更改,包括 80 次插入62 次删除
  1. 1 1
      Makefile.defs
  2. 19 60
      forward.c
  3. 59 0
      forward.h
  4. 1 1
      ip_addr.h

+ 1 - 1
Makefile.defs

@@ -8,7 +8,7 @@
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL =   11
-EXTRAVERSION = pre6-tcp3
+EXTRAVERSION = pre6-tcp4
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")

+ 19 - 60
forward.c

@@ -26,11 +26,14 @@
  *
  * History:
  * -------
- * 2003-01-23 support for determination of outbound interface added :
- *            get_out_socket (jiri)
- * 2003-01-24 reply to rport support added, contributed by
- *             Maxim Sobolev <[email protected]> and modified by andrei
- *
+ * 2001-??-??  created by andrei
+ * ????-??-??  lots of changes by a lot of people
+ * 2003-01-23  support for determination of outbound interface added :
+ *              get_out_socket (jiri)
+ * 2003-01-24  reply to rport support added, contributed by
+ *              Maxim Sobolev <[email protected]> and modified by andrei
+ * 2003-02-11  removed calls to upd_send & tcp_send & replaced them with
+ *              calls to msg_send (andrei)
  */
 
 
@@ -50,20 +53,15 @@
 #include "parser/msg_parser.h"
 #include "route.h"
 #include "dprint.h"
-#include "udp_server.h"
 #include "globals.h"
 #include "data_lump.h"
 #include "ut.h"
 #include "mem/mem.h"
 #include "msg_translator.h"
 #include "sr_module.h"
-#include "stats.h"
 #include "ip_addr.h"
 #include "resolve.h"
 #include "name_alias.h"
-#ifdef USE_TCP
-#include "tcp_server.h"
-#endif
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
@@ -315,34 +313,14 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
 	DBG("Sending:\n%.*s.\n", (int)len, buf);
 	DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
 	
-	
-	if (proto==PROTO_UDP){
-		if (udp_send(send_sock, buf, len,  to)==-1){
-				ser_error=E_SEND;
-				p->errors++;
-				p->ok=0;
-				STATS_TX_DROPS;
-				goto error1;
-		}
-	}
-#ifdef USE_TCP
-	 else if (proto==PROTO_TCP){
-		if (tcp_send(buf, len, to, 0)<0){
-				ser_error=E_SEND;
-				p->errors++;
-				p->ok=0;
-				STATS_TX_DROPS;
-				goto error1;
-		}
-	}
-#endif
-	 else{
-		LOG(L_CRIT, "BUG: forward_request: unknown proto %d\n", proto);
+	if (msg_send(send_sock, proto, to, 0, buf, len)<0){
 		ser_error=E_SEND;
+		p->errors++;
+		p->ok=0;
 		STATS_TX_DROPS;
 		goto error1;
 	}
-
+	
 	/* sent requests stats */
 	STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
 	
@@ -410,22 +388,23 @@ int update_sock_struct_from_via( union sockaddr_union* to,
 }
 
 
+
 /* removes first via & sends msg to the second */
 int forward_reply(struct sip_msg* msg)
 {
 	char* new_buf;
 	union sockaddr_union* to;
-	struct socket_info* send_sock;
 	unsigned int new_len;
 	struct sr_module *mod;
 	int proto;
+	int id; /* used only by tcp*/
 #ifdef USE_TCP
 	char* s;
 	int len;
-	int id;
 #endif
 	
 	to=0;
+	id=0;
 	new_buf=0;
 	/*check if first via host = us */
 	if (check_via){
@@ -470,22 +449,10 @@ int forward_reply(struct sip_msg* msg)
 
 	proto=msg->via2->proto;
 	if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
-	send_sock=get_send_socket(to, proto);
-	if (send_sock==0){
-		LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
-		goto error;
-	}
 
-	if (proto==PROTO_UDP){
-		if (udp_send(send_sock, new_buf,new_len,  to)==-1)
-		{
-			STATS_TX_DROPS;
-			goto error;
-		}
-	}
+
 #ifdef USE_TCP
-	 else if (proto==PROTO_TCP){
-		 id=0;
+	if (proto==PROTO_TCP){
 		/* find id in i param if it exists */
 		if (msg->via1->i&&msg->via1->i->value.s){
 			s=msg->via1->i->value.s;
@@ -495,19 +462,11 @@ int forward_reply(struct sip_msg* msg)
 			DBG("forward_reply: id= %x\n", id);
 		}		
 				
-		if (tcp_send(new_buf, new_len,  to, id)<0)
-		{
-			STATS_TX_DROPS;
-			goto error;
-		}
 	} 
 #endif
-	else{
-		LOG(L_CRIT, "BUG: forward_reply: unknown proto %d\n", proto);
-		goto error;
-	}
+	if (msg_send(0, proto, to, id, new_buf, new_len)<0) goto error;
 #ifdef STATS
-		STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
+	STATS_TX_RESPONSE(  (msg->first_line.u.reply.statuscode/100) );
 #endif
 
 	DBG(" reply forwarded to %.*s:%d\n", 

+ 59 - 0
forward.h

@@ -24,6 +24,13 @@
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+/*
+ * History:
+ * -------
+ * 2001-??-?? created by andrei
+ * ????-??-?? lots of changes by a lot of people
+ * 2003-02-11 added inline msg_send (andrei)
+ */
 
 
 
@@ -35,6 +42,12 @@
 #include "proxy.h"
 #include "ip_addr.h"
 
+#include "stats.h"
+#include "udp_server.h"
+#ifdef USE_TCP
+#include "tcp_server.h"
+#endif
+
 
 struct socket_info* get_send_socket(union sockaddr_union* su, int proto);
 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,
 
 int forward_reply( struct sip_msg* msg);
 
+
+
+/* params:
+ *  send_sock= 0 if already known (e.g. for udp in some cases), non-0 otherwise
+ *  proto=TCP|UDP
+ *  to = destination,
+ *  id - only used on tcp, it will force sending on connection "id" if id!=0 
+ *       and the connection exists, else it will send to "to" 
+ *       (usefull for sending replies on  the same connection as the request
+ *       that generated them; use 0 if you don't want this)
+ * returns: 0 if ok, -1 on error*/
+static inline int msg_send(	struct socket_info* send_sock, int proto,
+							union sockaddr_union* to, int id,
+							char* buf, int len)
+{
+	
+	if (proto==PROTO_UDP){
+		if (send_sock==0) send_sock=get_send_socket(to, proto);
+		if (send_sock==0){
+			LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
+			goto error;
+		}
+		if (udp_send(send_sock, buf, len, to)==-1){
+			STATS_TX_DROPS;
+			LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
+			goto error;
+		}
+	}
+#ifdef USE_TCP
+	else if (proto==PROTO_TCP){
+		if (tcp_send(buf, len, to, id)<0){
+			STATS_TX_DROPS;
+			LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
+			goto error;
+		}
+	}
+#endif
+	else{
+			LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", proto);
+			goto error;
+	}
+	return 0;
+error:
+	return -1;
+}
+
 #endif

+ 1 - 1
ip_addr.h

@@ -92,7 +92,7 @@ struct receive_info{
 	short src_port;
 	short dst_port;
 	int proto;
-	int proto_reserved1;
+	int proto_reserved1; /* tcp stores the connection id here */
 	int proto_reserved2;
 	union sockaddr_union src_su; /* usefull for replies*/
 	struct socket_info* bind_address; /* sock_info structure on which