Pārlūkot izejas kodu

- replies will be sent to rport if present
- finished adding new via param (i=tcp_connection_id_in_rev_hex)
[ not it's not only parsed but also used ]

Andrei Pelinescu-Onciul 22 gadi atpakaļ
vecāks
revīzija
e1e6c91475
4 mainītis faili ar 112 papildinājumiem un 44 dzēšanām
  1. 3 0
      config.h
  2. 24 28
      forward.c
  3. 84 11
      msg_translator.c
  4. 1 5
      msg_translator.h

+ 3 - 0
config.h

@@ -88,6 +88,9 @@
 #define RPORT ";rport="
 #define RPORT_LEN 7
 
+#define ID_PARAM ";i="
+#define ID_PARAM_LEN 3
+
 #define SRV_PREFIX "_sip._udp."
 #define SRV_PREFIX_LEN 10
 

+ 24 - 28
forward.c

@@ -26,8 +26,10 @@
  *
  * History:
  * -------
- * 2001-01-23 support for determination of outbound interface added :
+ * 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
  *
  */
 
@@ -153,7 +155,7 @@ struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
 #ifdef USE_TCP
 	if (proto==PROTO_TCP){
 		/* on tcp just use the "main address", we don't really now the
-		 * sending address (we can find it out, but we'll find also to see
+		 * sending address (we can find it out, but we'll need also to see
 		 * if we listen on it, and if yes on which port -> too complicated*/
 		switch(to->s.sa_family){
 			case AF_INET:	send_sock=sendipv4_tcp;
@@ -288,10 +290,6 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
        if it is turned on, we don't care about reboot; we simply put a simple
 	   value in there; better for performance
 	*/
-
-#ifdef USE_TCP
-	if (msg->rcv.proto==PROTO_TCP) id=msg->rcv.proto_reserved1;
-#endif
 	if (syn_branch ) {
 		*msg->add_to_branch_s='0';
 		msg->add_to_branch_len=1;
@@ -367,19 +365,28 @@ int update_sock_struct_from_via( union sockaddr_union* to,
 {
 	struct hostent* he;
 	str* name;
+	int err;
 	unsigned short port;
 
-
+	port=0;
+	if (via->rport && via->rport->value.s){
+		port=str2s(via->rport->value.s, via->rport->value.len, &err);
+		if (err){
+			LOG(L_NOTICE, "ERROR: forward_reply: bad rport value(%.*s)\n",
+					via->rport->value.len, via->rport->value.s);
+			port=0;
+		}
+	}
 	if (via->received){
 		DBG("update_sock_struct_from_via: using 'received'\n");
 		name=&(via->received->value);
 		/* making sure that we won't do SRV lookup on "received"
 		 * (possible if no DNS_IP_HACK is used)*/
-		port=via->port?via->port:SIP_PORT; 
+		if (port==0) port=via->port?via->port:SIP_PORT; 
 	}else{
 		DBG("update_sock_struct_from_via: using via host\n");
 		name=&(via->host);
-		port=via->port;
+		if (port==0) port=via->port;
 	}
 	/* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
 	   strings; -jiri 
@@ -397,6 +404,7 @@ int update_sock_struct_from_via( union sockaddr_union* to,
 				name->len, name->s);
 		return -1;
 	}
+		
 	hostent2su(to, he, 0, htons(port));
 	return 1;
 }
@@ -413,7 +421,6 @@ int forward_reply(struct sip_msg* msg)
 	int proto;
 #ifdef USE_TCP
 	char* s;
-	char* p;
 	int len;
 	int id;
 #endif
@@ -479,24 +486,13 @@ int forward_reply(struct sip_msg* msg)
 #ifdef USE_TCP
 	 else if (proto==PROTO_TCP){
 		 id=0;
-		/* find id in branch if it exists */
-		if ((msg->via1->branch)&&(msg->via1->branch->value.len>MCOOKIE_LEN) &&
-			(memcmp(msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)==0)){
-			DBG("forward_reply: found branch\n");
-			s=msg->via1->branch->value.s+MCOOKIE_LEN;
-			len=msg->via1->branch->value.len-MCOOKIE_LEN;
-			for (p=s; p<s+len  && *p!=BRANCH_SEPARATOR; p++);
-			p++;
-			for(;p<s+len && *p!=BRANCH_SEPARATOR; p++);
-			p++;
-			if (p<s+len){
-				/* we found the second BRANCH_SEPARATOR, p points after it */
-				len-=(int)(p-s);
-				id=reverse_hex2int(p, len);
-				DBG("forward_reply: id= %x\n", id);
-			}else{
-				DBG("forward_reply: no id in branch\n");
-			}
+		/* find id in i param if it exists */
+		if (msg->via1->i&&msg->via1->i->value.s){
+			s=msg->via1->i->value.s;
+			len=msg->via1->i->value.len;
+			DBG("forward_reply: i=%.*s\n",len, s);
+			id=reverse_hex2int(s, len);
+			DBG("forward_reply: id= %x\n", id);
 		}		
 				
 		if (tcp_send(new_buf, new_len,  to, id)==-1)

+ 84 - 11
msg_translator.c

@@ -32,6 +32,8 @@
  * 2003-01-23 added rport patches, contributed by 
  *             Maxim Sobolev <[email protected]> and heavily modified by me
  *             (andrei)
+ * 2003-01-24 added i param to via of outgoing requests (used by tcp),
+ *             modified via_builder params (andrei)
  *
  */
 
@@ -240,6 +242,37 @@ char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
 
 
 
+char* id_builder(struct sip_msg* msg, unsigned int *id_len)
+{
+	char* buf;
+	int len, value_len;
+	char revhex[sizeof(int)*2];
+	char* p;
+	int size;
+	
+	size=sizeof(int)*2;
+	p=&revhex[0];
+	if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
+		LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
+		return 0;
+	}
+	value_len=p-&revhex[0];
+	len=ID_PARAM_LEN+value_len+1; /* place for ending \0 */
+	buf=pkg_malloc(sizeof(char)*len);
+	if (buf==0){
+		ser_error=E_OUT_OF_MEM;
+		LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
+		return 0;
+	}
+	memcpy(buf, ID_PARAM, ID_PARAM_LEN);
+	memcpy(buf+ID_PARAM_LEN, revhex, value_len);
+	buf[len]=0; /* null terminate it */
+	*id_len=len;
+	return buf;
+}
+
+
+
 /* computes the "unpacked" len of a lump list,
    code moved from build_req_from_req */
 static inline int lumps_len(struct lump* l)
@@ -436,7 +469,19 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
 	unsigned int offset, s_offset, size;
 	struct lump* anchor;
 	int r;
-
+	str branch;
+	str extra_params;
+	
+#ifdef USE_TCP
+	char* id_buf;
+	int id_len;
+	
+	
+	id_buf=0;
+	id_len=0;
+#endif
+	extra_params.len=0;
+	extra_params.s=0;
 	uri_len=0;
 	orig=msg->orig;
 	buf=msg->buf;
@@ -446,10 +491,25 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
 	new_buf=0;
 	received_buf=0;
 	rport_buf=0;
+	line_buf=0;
 
-
-	line_buf = via_builder( &via_len, send_sock, 
-		msg->add_to_branch_s, msg->add_to_branch_len, proto);
+	
+#ifdef USE_TCP
+	/* add id if tcp */
+	if (msg->rcv.proto==PROTO_TCP){
+		if  ((id_buf=id_builder(msg, &id_len))==0){
+			LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
+							" id_builder failed\n");
+			goto error01; /* free everything */
+		}
+		extra_params.s=id_buf;
+		extra_params.len=id_len;
+	}
+#endif
+	branch.s=msg->add_to_branch_s;
+	branch.len=msg->add_to_branch_len;
+	line_buf = via_builder( &via_len, send_sock, &branch,
+							extra_params.len?&extra_params:0, proto);
 	if (!line_buf){
 		LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
 		goto error00;
@@ -558,6 +618,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
 
 error01:
 	pkg_free(line_buf);
+#ifdef USE_TCP
+	if (id_buf) pkg_free(id_buf);
+#endif
 error02:
 	if (received_buf) pkg_free(received_buf);
 error03:
@@ -934,7 +997,7 @@ int branch_builder( unsigned int hash_index,
 
 char* via_builder( unsigned int *len, 
 	struct socket_info* send_sock,
-	char *branch, int branch_len, int proto )
+	str* branch, str* extra_params, int proto )
 {
 	unsigned int  via_len, extra_len;
 	char               *line_buf;
@@ -942,9 +1005,11 @@ char* via_builder( unsigned int *len,
 
 
 	max_len=MY_VIA_LEN+send_sock->address_str.len /* space in MY_VIA */
-		+2 /* just in case it it a v6 address ... [ ] */
+		+2 /* just in case it is a v6 address ... [ ] */
 		+send_sock->port_no_str.len
-		+MY_BRANCH_LEN+branch_len+CRLF_LEN+1;
+		+(branch)?(MY_BRANCH_LEN+branch->len):0
+		+(extra_params)?extra_params->len:0
+		+CRLF_LEN+1;
 	line_buf=pkg_malloc( max_len );
 	if (line_buf==0){
 		ser_error=E_OUT_OF_MEM;
@@ -982,10 +1047,18 @@ char* via_builder( unsigned int *len,
 	}
 
 	/* branch parameter */
-	memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
-	via_len+=MY_BRANCH_LEN;
-	memcpy(line_buf+via_len, branch, branch_len );
-	via_len+=branch_len;
+	if (branch){
+		memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
+		via_len+=MY_BRANCH_LEN;
+		memcpy(line_buf+via_len, branch->s, branch->len );
+		via_len+=branch->len;
+	}
+	/* extra params  */
+	if (extra_params){
+		memcpy(line_buf+via_len, extra_params->s, extra_params->len);
+		via_len+=extra_params->len;
+	}
+	
 	memcpy(line_buf+via_len, CRLF, CRLF_LEN);
 	via_len+=CRLF_LEN;
 	line_buf[via_len]=0; /* null terminate the string*/

+ 1 - 5
msg_translator.h

@@ -57,12 +57,8 @@ char * build_res_buf_from_sip_req(	unsigned int code ,
 
 char* via_builder( unsigned int *len,
 	struct socket_info* send_sock,
-	char *branch, int branch_len, int proto );
+	str *branch, str* extra_params, int proto );
 
-#ifdef _OBSOLETED
-char* via_builder( struct sip_msg *msg ,
-				unsigned int *len, struct socket_info* send_sock);
-#endif
 
 int branch_builder( unsigned int hash_index, 
 	/* only either parameter useful */