Browse code

- 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 authored on 24/01/2003 17:56:42
Showing 4 changed files
... ...
@@ -88,6 +88,9 @@
88 88
 #define RPORT ";rport="
89 89
 #define RPORT_LEN 7
90 90
 
91
+#define ID_PARAM ";i="
92
+#define ID_PARAM_LEN 3
93
+
91 94
 #define SRV_PREFIX "_sip._udp."
92 95
 #define SRV_PREFIX_LEN 10
93 96
 
... ...
@@ -26,8 +26,10 @@
26 26
  *
27 27
  * History:
28 28
  * -------
29
- * 2001-01-23 support for determination of outbound interface added :
29
+ * 2003-01-23 support for determination of outbound interface added :
30 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
31 33
  *
32 34
  */
33 35
 
... ...
@@ -153,7 +155,7 @@ struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
153 153
 #ifdef USE_TCP
154 154
 	if (proto==PROTO_TCP){
155 155
 		/* on tcp just use the "main address", we don't really now the
156
-		 * sending address (we can find it out, but we'll find also to see
156
+		 * sending address (we can find it out, but we'll need also to see
157 157
 		 * if we listen on it, and if yes on which port -> too complicated*/
158 158
 		switch(to->s.sa_family){
159 159
 			case AF_INET:	send_sock=sendipv4_tcp;
... ...
@@ -288,10 +290,6 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
288 288
        if it is turned on, we don't care about reboot; we simply put a simple
289 289
 	   value in there; better for performance
290 290
 	*/
291
-
292
-#ifdef USE_TCP
293
-	if (msg->rcv.proto==PROTO_TCP) id=msg->rcv.proto_reserved1;
294
-#endif
295 291
 	if (syn_branch ) {
296 292
 		*msg->add_to_branch_s='0';
297 293
 		msg->add_to_branch_len=1;
... ...
@@ -367,19 +365,28 @@ int update_sock_struct_from_via( union sockaddr_union* to,
367 367
 {
368 368
 	struct hostent* he;
369 369
 	str* name;
370
+	int err;
370 371
 	unsigned short port;
371 372
 
372
-
373
+	port=0;
374
+	if (via->rport && via->rport->value.s){
375
+		port=str2s(via->rport->value.s, via->rport->value.len, &err);
376
+		if (err){
377
+			LOG(L_NOTICE, "ERROR: forward_reply: bad rport value(%.*s)\n",
378
+					via->rport->value.len, via->rport->value.s);
379
+			port=0;
380
+		}
381
+	}
373 382
 	if (via->received){
374 383
 		DBG("update_sock_struct_from_via: using 'received'\n");
375 384
 		name=&(via->received->value);
376 385
 		/* making sure that we won't do SRV lookup on "received"
377 386
 		 * (possible if no DNS_IP_HACK is used)*/
378
-		port=via->port?via->port:SIP_PORT; 
387
+		if (port==0) port=via->port?via->port:SIP_PORT; 
379 388
 	}else{
380 389
 		DBG("update_sock_struct_from_via: using via host\n");
381 390
 		name=&(via->host);
382
-		port=via->port;
391
+		if (port==0) port=via->port;
383 392
 	}
384 393
 	/* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
385 394
 	   strings; -jiri 
... ...
@@ -397,6 +404,7 @@ int update_sock_struct_from_via( union sockaddr_union* to,
397 397
 				name->len, name->s);
398 398
 		return -1;
399 399
 	}
400
+		
400 401
 	hostent2su(to, he, 0, htons(port));
401 402
 	return 1;
402 403
 }
... ...
@@ -413,7 +421,6 @@ int forward_reply(struct sip_msg* msg)
413 413
 	int proto;
414 414
 #ifdef USE_TCP
415 415
 	char* s;
416
-	char* p;
417 416
 	int len;
418 417
 	int id;
419 418
 #endif
... ...
@@ -479,24 +486,13 @@ int forward_reply(struct sip_msg* msg)
479 479
 #ifdef USE_TCP
480 480
 	 else if (proto==PROTO_TCP){
481 481
 		 id=0;
482
-		/* find id in branch if it exists */
483
-		if ((msg->via1->branch)&&(msg->via1->branch->value.len>MCOOKIE_LEN) &&
484
-			(memcmp(msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)==0)){
485
-			DBG("forward_reply: found branch\n");
486
-			s=msg->via1->branch->value.s+MCOOKIE_LEN;
487
-			len=msg->via1->branch->value.len-MCOOKIE_LEN;
488
-			for (p=s; p<s+len  && *p!=BRANCH_SEPARATOR; p++);
489
-			p++;
490
-			for(;p<s+len && *p!=BRANCH_SEPARATOR; p++);
491
-			p++;
492
-			if (p<s+len){
493
-				/* we found the second BRANCH_SEPARATOR, p points after it */
494
-				len-=(int)(p-s);
495
-				id=reverse_hex2int(p, len);
496
-				DBG("forward_reply: id= %x\n", id);
497
-			}else{
498
-				DBG("forward_reply: no id in branch\n");
499
-			}
482
+		/* find id in i param if it exists */
483
+		if (msg->via1->i&&msg->via1->i->value.s){
484
+			s=msg->via1->i->value.s;
485
+			len=msg->via1->i->value.len;
486
+			DBG("forward_reply: i=%.*s\n",len, s);
487
+			id=reverse_hex2int(s, len);
488
+			DBG("forward_reply: id= %x\n", id);
500 489
 		}		
501 490
 				
502 491
 		if (tcp_send(new_buf, new_len,  to, id)==-1)
... ...
@@ -32,6 +32,8 @@
32 32
  * 2003-01-23 added rport patches, contributed by 
33 33
  *             Maxim Sobolev <sobomax@FreeBSD.org> and heavily modified by me
34 34
  *             (andrei)
35
+ * 2003-01-24 added i param to via of outgoing requests (used by tcp),
36
+ *             modified via_builder params (andrei)
35 37
  *
36 38
  */
37 39
 
... ...
@@ -240,6 +242,37 @@ char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
240 240
 
241 241
 
242 242
 
243
+char* id_builder(struct sip_msg* msg, unsigned int *id_len)
244
+{
245
+	char* buf;
246
+	int len, value_len;
247
+	char revhex[sizeof(int)*2];
248
+	char* p;
249
+	int size;
250
+	
251
+	size=sizeof(int)*2;
252
+	p=&revhex[0];
253
+	if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
254
+		LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
255
+		return 0;
256
+	}
257
+	value_len=p-&revhex[0];
258
+	len=ID_PARAM_LEN+value_len+1; /* place for ending \0 */
259
+	buf=pkg_malloc(sizeof(char)*len);
260
+	if (buf==0){
261
+		ser_error=E_OUT_OF_MEM;
262
+		LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
263
+		return 0;
264
+	}
265
+	memcpy(buf, ID_PARAM, ID_PARAM_LEN);
266
+	memcpy(buf+ID_PARAM_LEN, revhex, value_len);
267
+	buf[len]=0; /* null terminate it */
268
+	*id_len=len;
269
+	return buf;
270
+}
271
+
272
+
273
+
243 274
 /* computes the "unpacked" len of a lump list,
244 275
    code moved from build_req_from_req */
245 276
 static inline int lumps_len(struct lump* l)
... ...
@@ -436,7 +469,19 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
436 436
 	unsigned int offset, s_offset, size;
437 437
 	struct lump* anchor;
438 438
 	int r;
439
-
439
+	str branch;
440
+	str extra_params;
441
+	
442
+#ifdef USE_TCP
443
+	char* id_buf;
444
+	int id_len;
445
+	
446
+	
447
+	id_buf=0;
448
+	id_len=0;
449
+#endif
450
+	extra_params.len=0;
451
+	extra_params.s=0;
440 452
 	uri_len=0;
441 453
 	orig=msg->orig;
442 454
 	buf=msg->buf;
... ...
@@ -446,10 +491,25 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
446 446
 	new_buf=0;
447 447
 	received_buf=0;
448 448
 	rport_buf=0;
449
+	line_buf=0;
449 450
 
450
-
451
-	line_buf = via_builder( &via_len, send_sock, 
452
-		msg->add_to_branch_s, msg->add_to_branch_len, proto);
451
+	
452
+#ifdef USE_TCP
453
+	/* add id if tcp */
454
+	if (msg->rcv.proto==PROTO_TCP){
455
+		if  ((id_buf=id_builder(msg, &id_len))==0){
456
+			LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
457
+							" id_builder failed\n");
458
+			goto error01; /* free everything */
459
+		}
460
+		extra_params.s=id_buf;
461
+		extra_params.len=id_len;
462
+	}
463
+#endif
464
+	branch.s=msg->add_to_branch_s;
465
+	branch.len=msg->add_to_branch_len;
466
+	line_buf = via_builder( &via_len, send_sock, &branch,
467
+							extra_params.len?&extra_params:0, proto);
453 468
 	if (!line_buf){
454 469
 		LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
455 470
 		goto error00;
... ...
@@ -558,6 +618,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
558 558
 
559 559
 error01:
560 560
 	pkg_free(line_buf);
561
+#ifdef USE_TCP
562
+	if (id_buf) pkg_free(id_buf);
563
+#endif
561 564
 error02:
562 565
 	if (received_buf) pkg_free(received_buf);
563 566
 error03:
... ...
@@ -934,7 +997,7 @@ int branch_builder( unsigned int hash_index,
934 934
 
935 935
 char* via_builder( unsigned int *len, 
936 936
 	struct socket_info* send_sock,
937
-	char *branch, int branch_len, int proto )
937
+	str* branch, str* extra_params, int proto )
938 938
 {
939 939
 	unsigned int  via_len, extra_len;
940 940
 	char               *line_buf;
... ...
@@ -942,9 +1005,11 @@ char* via_builder( unsigned int *len,
942 942
 
943 943
 
944 944
 	max_len=MY_VIA_LEN+send_sock->address_str.len /* space in MY_VIA */
945
-		+2 /* just in case it it a v6 address ... [ ] */
945
+		+2 /* just in case it is a v6 address ... [ ] */
946 946
 		+send_sock->port_no_str.len
947
-		+MY_BRANCH_LEN+branch_len+CRLF_LEN+1;
947
+		+(branch)?(MY_BRANCH_LEN+branch->len):0
948
+		+(extra_params)?extra_params->len:0
949
+		+CRLF_LEN+1;
948 950
 	line_buf=pkg_malloc( max_len );
949 951
 	if (line_buf==0){
950 952
 		ser_error=E_OUT_OF_MEM;
... ...
@@ -982,10 +1047,18 @@ char* via_builder( unsigned int *len,
982 982
 	}
983 983
 
984 984
 	/* branch parameter */
985
-	memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
986
-	via_len+=MY_BRANCH_LEN;
987
-	memcpy(line_buf+via_len, branch, branch_len );
988
-	via_len+=branch_len;
985
+	if (branch){
986
+		memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
987
+		via_len+=MY_BRANCH_LEN;
988
+		memcpy(line_buf+via_len, branch->s, branch->len );
989
+		via_len+=branch->len;
990
+	}
991
+	/* extra params  */
992
+	if (extra_params){
993
+		memcpy(line_buf+via_len, extra_params->s, extra_params->len);
994
+		via_len+=extra_params->len;
995
+	}
996
+	
989 997
 	memcpy(line_buf+via_len, CRLF, CRLF_LEN);
990 998
 	via_len+=CRLF_LEN;
991 999
 	line_buf[via_len]=0; /* null terminate the string*/
... ...
@@ -57,12 +57,8 @@ char * build_res_buf_from_sip_req(	unsigned int code ,
57 57
 
58 58
 char* via_builder( unsigned int *len,
59 59
 	struct socket_info* send_sock,
60
-	char *branch, int branch_len, int proto );
60
+	str *branch, str* extra_params, int proto );
61 61
 
62
-#ifdef _OBSOLETED
63
-char* via_builder( struct sip_msg *msg ,
64
-				unsigned int *len, struct socket_info* send_sock);
65
-#endif
66 62
 
67 63
 int branch_builder( unsigned int hash_index, 
68 64
 	/* only either parameter useful */