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 356
 			}
417 357
 		}
418 358
 	}
359
+	return new_len;
360
+}
419 361
 
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 362
 
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){
363
+
364
+/* another helper functions, adds/Removes the lump,
365
+	code moved form build_req_from_req  */
366
+
367
+static /*inline*/ void process_lumps(	struct lump* l,	char* new_buf, 
368
+									int* new_buf_offs, char* orig,
369
+									int* orig_offs)
370
+{
371
+	struct lump *t;
372
+	struct lump *r;
373
+	int size;
374
+	int offset;
375
+	int s_offset;
376
+	
377
+	offset=*new_buf_offs;
378
+	s_offset=*orig_offs;
379
+	
380
+	for (t=l;t;t=t->next){
446 381
 		switch(t->op){
447 382
 			case LUMP_ADD:
448 383
 				/* just add it here! */
... ...
@@ -533,6 +468,107 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
533 468
 							"unknown op (%x)\n", t->op);
534 469
 		}
535 470
 	}
471
+	*new_buf_offs=offset;
472
+	*orig_offs=s_offset;
473
+}
474
+
475
+
476
+
477
+char * build_req_buf_from_sip_req( struct sip_msg* msg,
478
+								unsigned int *returned_len,
479
+								struct socket_info* send_sock)
480
+{
481
+	unsigned int len, new_len, received_len, uri_len, via_len;
482
+	char* line_buf;
483
+	char* received_buf;
484
+	char* new_buf;
485
+	char* orig;
486
+	char* buf;
487
+	char  backup;
488
+	unsigned int offset, s_offset, size;
489
+	struct lump* anchor;
490
+
491
+	uri_len=0;
492
+	orig=msg->orig;
493
+	buf=msg->buf;
494
+	len=msg->len;
495
+	received_len=0;
496
+	new_buf=0;
497
+	received_buf=0;
498
+
499
+
500
+	line_buf = via_builder( msg, &via_len, send_sock);
501
+	if (!line_buf){
502
+		LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
503
+		goto error1;
504
+	}
505
+	/* check if received needs to be added */
506
+	backup = msg->via1->host.s[msg->via1->host.len];
507
+	msg->via1->host.s[msg->via1->host.len] = 0;
508
+	if (check_address(&msg->src_ip, msg->via1->host.s, received_dns)!=0){
509
+		if ((received_buf=received_builder(msg,&received_len))==0)
510
+			goto error;
511
+	}
512
+	msg->via1->host.s[msg->via1->host.len] = backup;
513
+
514
+	/* add via header to the list */
515
+	/* try to add it before msg. 1st via */
516
+	/* add first via, as an anchor for second via*/
517
+	anchor=anchor_lump(&(msg->add_rm), msg->via1->hdr.s-buf, 0, HDR_VIA);
518
+	if (anchor==0) goto error;
519
+	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)
520
+		goto error;
521
+	/* if received needs to be added, add anchor after host and add it */
522
+	if (received_len){
523
+		if (msg->via1->params.s){
524
+				size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
525
+															  for ';' */
526
+		}else{
527
+				size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
528
+				if (msg->via1->port!=0){
529
+					/*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
530
+					size += msg->via1->port_str.len + 1; /* +1 for ':'*/
531
+				}
532
+			#ifdef USE_IPV6
533
+				if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
534
+			#endif
535
+		}
536
+		anchor=anchor_lump(&(msg->add_rm),msg->via1->hdr.s-buf+size,0,
537
+				HDR_VIA);
538
+		if (anchor==0) goto error;
539
+		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA)
540
+				==0 ) goto error;
541
+	}
542
+
543
+	/* compute new msg len and fix overlapping zones*/
544
+	new_len=len+lumps_len(msg->add_rm);
545
+
546
+	if (msg->new_uri.s){
547
+		uri_len=msg->new_uri.len;
548
+		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
549
+	}
550
+	new_buf=(char*)local_malloc(new_len+1);
551
+	if (new_buf==0){
552
+		ser_error=E_OUT_OF_MEM;
553
+		LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
554
+		goto error;
555
+	}
556
+
557
+	offset=s_offset=0;
558
+	if (msg->new_uri.s){
559
+		/* copy message up to uri */
560
+		size=msg->first_line.u.request.uri.s-buf;
561
+		memcpy(new_buf, orig, size);
562
+		offset+=size;
563
+		s_offset+=size;
564
+		/* add our uri */
565
+		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
566
+		offset+=uri_len;
567
+		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
568
+	}
569
+	new_buf[new_len]=0;
570
+	/* copy msg adding/removing lumps */
571
+	process_lumps(msg->add_rm, new_buf, &offset, orig, &s_offset);
536 572
 	/* copy the rest of the message */
537 573
 	memcpy(new_buf+offset, orig+s_offset, len-s_offset);
538 574
 	new_buf[new_len]=0;
... ...
@@ -557,7 +593,7 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
557 593
 {
558 594
 	unsigned int new_len, via_len;
559 595
 	char* new_buf;
560
-	unsigned offset, s_offset, size;
596
+	unsigned offset, s_offset, via_offset;
561 597
 	char* orig;
562 598
 	char* buf;
563 599
 	unsigned int len;
... ...
@@ -568,18 +604,24 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
568 604
 	new_buf=0;
569 605
 	/* we must remove the first via */
570 606
 	via_len=msg->via1->bsize;
571
-	size=msg->via1->hdr.s-buf;
572
-	DBG("via len: %d, initial size: %d\n", via_len, size);
607
+	via_offset=msg->via1->hdr.s-buf;
608
+	DBG("via len: %d, initial via offset: %d\n", via_len, via_offset);
573 609
 	if (msg->via1->next){
574 610
 		/* add hdr size*/
575
-		size+=msg->via1->hdr.len+1;
576
-	    DBG(" adjusted via len: %d, initial size: %d\n",
577
-				via_len, size);
611
+		via_offset+=msg->via1->hdr.len+1;
612
+	    DBG(" adjusted via len: %d, initial offset: %d\n",
613
+				via_len, via_offset);
578 614
 	}else{
579 615
 		/* add hdr size ("Via:")*/
580 616
 		via_len+=msg->via1->hdr.len+1;
581 617
 	}
582
-	new_len=len-via_len;
618
+	/* remove the first via*/
619
+	if (del_lump( &(msg->repl_add_rm), via_offset, via_len, 0)==0){
620
+		LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
621
+					"via\n");
622
+		goto error;
623
+	}
624
+	new_len=len+lumps_len(msg->repl_add_rm);
583 625
 
584 626
 	DBG(" old size: %d, new size: %d\n", len, new_len);
585 627
 	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 631
 		goto error;
590 632
 	}
591 633
 	new_buf[new_len]=0; /* debug: print the message */
592
-	memcpy(new_buf, orig, size);
593
-	offset=size;
594
-	s_offset=size+via_len;
634
+	offset=s_offset=0;
635
+	process_lumps(msg->repl_add_rm, new_buf, &offset, orig, &s_offset);
636
+	/* copy the rest of the message */
595 637
 	memcpy(new_buf+offset,orig+s_offset, len-s_offset);
596 638
 	 /* send it! */
597 639
 	DBG(" copied size: orig:%d, new: %d, rest: %d\n",