Browse code

- added reply lump processing

Andrei Pelinescu-Onciul authored on 11/07/2002 17:46:58
Showing 3 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 VERSION = 0
9 9
 PATCHLEVEL = 8
10 10
 SUBLEVEL = 7
11
-EXTRAVERSION = -4-ipv6
11
+EXTRAVERSION = -5-ipv6
12 12
 
13 13
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
14 14
 OS = $(shell uname -s)
... ...
@@ -3,6 +3,7 @@
3 3
  *
4 4
  */
5 5
 
6
+#include <stdio.h>
6 7
 #include "error.h"
7 8
 
8 9
 /* current function's error; */
... ...
@@ -293,78 +293,18 @@ char* received_builder(struct sip_msg *msg, int *received_len)
293 293
 
294 294
 
295 295
 
296
-
297
-char * build_req_buf_from_sip_req( struct sip_msg* msg,
298
-								unsigned int *returned_len,
299
-								struct socket_info* send_sock)
296
+/* computes the "unpacked" len of a lump list,
297
+   code moved from build_req_from_req */
298
+static inline int lumps_len(struct lump* l)
300 299
 {
301
-	unsigned int len, new_len, received_len, uri_len, via_len;
302
-	char* line_buf;
303
-	char* received_buf;
304
-	char* new_buf;
305
-	char* orig;
306
-	char* buf;
307
-	char  backup;
308
-	unsigned int offset, s_offset, size;
309
-	struct lump *t,*r;
310
-	struct lump* anchor;
311
-
312
-	uri_len=0;
313
-	orig=msg->orig;
314
-	buf=msg->buf;
315
-	len=msg->len;
316
-	received_len=0;
317
-	new_buf=0;
318
-	received_buf=0;
319
-
320
-
321
-	line_buf = via_builder( msg, &via_len, send_sock);
322
-	if (!line_buf){
323
-		LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
324
-		goto error1;
325
-	}
326
-	/* check if received needs to be added */
327
-	backup = msg->via1->host.s[msg->via1->host.len];
328
-	msg->via1->host.s[msg->via1->host.len] = 0;
329
-	if (check_address(&msg->src_ip, msg->via1->host.s, received_dns)!=0){
330
-		if ((received_buf=received_builder(msg,&received_len))==0)
331
-			goto error;
332
-	}
333
-	msg->via1->host.s[msg->via1->host.len] = backup;
300
+	int s_offset;
301
+	int new_len;
302
+	struct lump* t;
303
+	struct lump* r;
334 304
 
335
-	/* add via header to the list */
336
-	/* try to add it before msg. 1st via */
337
-	/* add first via, as an anchor for second via*/
338
-	anchor=anchor_lump(&(msg->add_rm), msg->via1->hdr.s-buf, 0, HDR_VIA);
339
-	if (anchor==0) goto error;
340
-	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)
341
-		goto error;
342
-	/* if received needs to be added, add anchor after host and add it */
343
-	if (received_len){
344
-		if (msg->via1->params.s){
345
-				size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
346
-															  for ';' */
347
-		}else{
348
-				size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
349
-				if (msg->via1->port!=0){
350
-					/*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
351
-					size += msg->via1->port_str.len + 1; /* +1 for ':'*/
352
-				}
353
-			#ifdef USE_IPV6
354
-				if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
355
-			#endif
356
-		}
357
-		anchor=anchor_lump(&(msg->add_rm),msg->via1->hdr.s-buf+size,0,
358
-				HDR_VIA);
359
-		if (anchor==0) goto error;
360
-		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA)
361
-				==0 ) goto error;
362
-	}
363
-
364
-	/* compute new msg len and fix overlapping zones*/
365
-	new_len=len;
366 305
 	s_offset=0;
367
-	for(t=msg->add_rm;t;t=t->next){
306
+	new_len=0;
307
+	for(t=l;t;t=t->next){
368 308
 		for(r=t->before;r;r=r->before){
369 309
 			switch(r->op){
370 310
 				case LUMP_ADD:
... ...
@@ -416,33 +356,28 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
416 416
 			}
417 417
 		}
418 418
 	}
419
+	return new_len;
420
+}
419 421
 
420
-	if (msg->new_uri.s){
421
-		uri_len=msg->new_uri.len;
422
-		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
423
-	}
424
-	new_buf=(char*)local_malloc(new_len+1);
425
-	if (new_buf==0){
426
-		ser_error=E_OUT_OF_MEM;
427
-		LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
428
-		goto error;
429
-	}
430 422
 
431
-	offset=s_offset=0;
432
-	if (msg->new_uri.s){
433
-		/* copy message up to uri */
434
-		size=msg->first_line.u.request.uri.s-buf;
435
-		memcpy(new_buf, orig, size);
436
-		offset+=size;
437
-		s_offset+=size;
438
-		/* add our uri */
439
-		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
440
-		offset+=uri_len;
441
-		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
442
-	}
443
-	new_buf[new_len]=0;
444
-	/* copy msg adding/removing lumps */
445
-	for (t=msg->add_rm;t;t=t->next){
423
+
424
+/* another helper functions, adds/Removes the lump,
425
+	code moved form build_req_from_req  */
426
+
427
+static /*inline*/ void process_lumps(	struct lump* l,	char* new_buf, 
428
+									int* new_buf_offs, char* orig,
429
+									int* orig_offs)
430
+{
431
+	struct lump *t;
432
+	struct lump *r;
433
+	int size;
434
+	int offset;
435
+	int s_offset;
436
+	
437
+	offset=*new_buf_offs;
438
+	s_offset=*orig_offs;
439
+	
440
+	for (t=l;t;t=t->next){
446 441
 		switch(t->op){
447 442
 			case LUMP_ADD:
448 443
 				/* just add it here! */
... ...
@@ -533,6 +468,107 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
533 533
 							"unknown op (%x)\n", t->op);
534 534
 		}
535 535
 	}
536
+	*new_buf_offs=offset;
537
+	*orig_offs=s_offset;
538
+}
539
+
540
+
541
+
542
+char * build_req_buf_from_sip_req( struct sip_msg* msg,
543
+								unsigned int *returned_len,
544
+								struct socket_info* send_sock)
545
+{
546
+	unsigned int len, new_len, received_len, uri_len, via_len;
547
+	char* line_buf;
548
+	char* received_buf;
549
+	char* new_buf;
550
+	char* orig;
551
+	char* buf;
552
+	char  backup;
553
+	unsigned int offset, s_offset, size;
554
+	struct lump* anchor;
555
+
556
+	uri_len=0;
557
+	orig=msg->orig;
558
+	buf=msg->buf;
559
+	len=msg->len;
560
+	received_len=0;
561
+	new_buf=0;
562
+	received_buf=0;
563
+
564
+
565
+	line_buf = via_builder( msg, &via_len, send_sock);
566
+	if (!line_buf){
567
+		LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
568
+		goto error1;
569
+	}
570
+	/* check if received needs to be added */
571
+	backup = msg->via1->host.s[msg->via1->host.len];
572
+	msg->via1->host.s[msg->via1->host.len] = 0;
573
+	if (check_address(&msg->src_ip, msg->via1->host.s, received_dns)!=0){
574
+		if ((received_buf=received_builder(msg,&received_len))==0)
575
+			goto error;
576
+	}
577
+	msg->via1->host.s[msg->via1->host.len] = backup;
578
+
579
+	/* add via header to the list */
580
+	/* try to add it before msg. 1st via */
581
+	/* add first via, as an anchor for second via*/
582
+	anchor=anchor_lump(&(msg->add_rm), msg->via1->hdr.s-buf, 0, HDR_VIA);
583
+	if (anchor==0) goto error;
584
+	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)
585
+		goto error;
586
+	/* if received needs to be added, add anchor after host and add it */
587
+	if (received_len){
588
+		if (msg->via1->params.s){
589
+				size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
590
+															  for ';' */
591
+		}else{
592
+				size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
593
+				if (msg->via1->port!=0){
594
+					/*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
595
+					size += msg->via1->port_str.len + 1; /* +1 for ':'*/
596
+				}
597
+			#ifdef USE_IPV6
598
+				if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
599
+			#endif
600
+		}
601
+		anchor=anchor_lump(&(msg->add_rm),msg->via1->hdr.s-buf+size,0,
602
+				HDR_VIA);
603
+		if (anchor==0) goto error;
604
+		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA)
605
+				==0 ) goto error;
606
+	}
607
+
608
+	/* compute new msg len and fix overlapping zones*/
609
+	new_len=len+lumps_len(msg->add_rm);
610
+
611
+	if (msg->new_uri.s){
612
+		uri_len=msg->new_uri.len;
613
+		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
614
+	}
615
+	new_buf=(char*)local_malloc(new_len+1);
616
+	if (new_buf==0){
617
+		ser_error=E_OUT_OF_MEM;
618
+		LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
619
+		goto error;
620
+	}
621
+
622
+	offset=s_offset=0;
623
+	if (msg->new_uri.s){
624
+		/* copy message up to uri */
625
+		size=msg->first_line.u.request.uri.s-buf;
626
+		memcpy(new_buf, orig, size);
627
+		offset+=size;
628
+		s_offset+=size;
629
+		/* add our uri */
630
+		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
631
+		offset+=uri_len;
632
+		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
633
+	}
634
+	new_buf[new_len]=0;
635
+	/* copy msg adding/removing lumps */
636
+	process_lumps(msg->add_rm, new_buf, &offset, orig, &s_offset);
536 637
 	/* copy the rest of the message */
537 638
 	memcpy(new_buf+offset, orig+s_offset, len-s_offset);
538 639
 	new_buf[new_len]=0;
... ...
@@ -557,7 +593,7 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
557 557
 {
558 558
 	unsigned int new_len, via_len;
559 559
 	char* new_buf;
560
-	unsigned offset, s_offset, size;
560
+	unsigned offset, s_offset, via_offset;
561 561
 	char* orig;
562 562
 	char* buf;
563 563
 	unsigned int len;
... ...
@@ -568,18 +604,24 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
568 568
 	new_buf=0;
569 569
 	/* we must remove the first via */
570 570
 	via_len=msg->via1->bsize;
571
-	size=msg->via1->hdr.s-buf;
572
-	DBG("via len: %d, initial size: %d\n", via_len, size);
571
+	via_offset=msg->via1->hdr.s-buf;
572
+	DBG("via len: %d, initial via offset: %d\n", via_len, via_offset);
573 573
 	if (msg->via1->next){
574 574
 		/* add hdr size*/
575
-		size+=msg->via1->hdr.len+1;
576
-	    DBG(" adjusted via len: %d, initial size: %d\n",
577
-				via_len, size);
575
+		via_offset+=msg->via1->hdr.len+1;
576
+	    DBG(" adjusted via len: %d, initial offset: %d\n",
577
+				via_len, via_offset);
578 578
 	}else{
579 579
 		/* add hdr size ("Via:")*/
580 580
 		via_len+=msg->via1->hdr.len+1;
581 581
 	}
582
-	new_len=len-via_len;
582
+	/* remove the first via*/
583
+	if (del_lump( &(msg->repl_add_rm), via_offset, via_len, 0)==0){
584
+		LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
585
+					"via\n");
586
+		goto error;
587
+	}
588
+	new_len=len+lumps_len(msg->repl_add_rm);
583 589
 
584 590
 	DBG(" old size: %d, new size: %d\n", len, new_len);
585 591
 	new_buf=(char*)local_malloc(new_len+1);/* +1 is for debugging
... ...
@@ -589,9 +631,9 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
589 589
 		goto error;
590 590
 	}
591 591
 	new_buf[new_len]=0; /* debug: print the message */
592
-	memcpy(new_buf, orig, size);
593
-	offset=size;
594
-	s_offset=size+via_len;
592
+	offset=s_offset=0;
593
+	process_lumps(msg->repl_add_rm, new_buf, &offset, orig, &s_offset);
594
+	/* copy the rest of the message */
595 595
 	memcpy(new_buf+offset,orig+s_offset, len-s_offset);
596 596
 	 /* send it! */
597 597
 	DBG(" copied size: orig:%d, new: %d, rest: %d\n",