Browse code

new clonning function added

Bogdan-Andrei Iancu authored on 18/12/2001 00:30:46
Showing 4 changed files
... ...
@@ -46,7 +46,7 @@ ARCH = $(shell uname -s)
46 46
 #		malloc etc.)
47 47
 DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
48 48
 	 -DDNS_IP_HACK  -DPKG_MALLOC -DSHM_MEM  -DSHM_MMAP \
49
-	 -DNO_DEBUG #-DDBG_QM_MALLOC
49
+	  #-DNO_DEBUG #-DDBG_QM_MALLOC
50 50
 #-DEXTRA_DEBUG
51 51
 # -DUSE_SHM_MEM
52 52
 #-DNO_DEBUG 
... ...
@@ -7,6 +7,11 @@
7 7
 #include "../../dprint.h"
8 8
 #include "../../mem.h"
9 9
 
10
+char*   translate_pointer( char* new_buf , char *org_buf , char* p);
11
+struct via_body* via_body_cloner( char* new_buf , char *org_buf , struct via_body *org_via);
12
+struct hdr_field* header_cloner( struct sip_msg *new_msg , struct sip_msg *org_msg, struct hdr_field *hdr);
13
+
14
+struct via_body* via_body_cloner_2( char* new_buf , char *org_buf , struct via_body *org_via, char **p);
10 15
 
11 16
 
12 17
 struct sip_msg* sip_msg_cloner_1( struct sip_msg *org_msg )
... ...
@@ -79,7 +84,7 @@ struct sip_msg* sip_msg_cloner_1( struct sip_msg *org_msg )
79 84
     }
80 85
 
81 86
     /* new_uri  ( str type )*/
82
-	if (org_msg->new_uri.s){ 
87
+	if (org_msg->new_uri.s){
83 88
 		if (!(new_msg->new_uri.s = (char*)sh_malloc( org_msg->new_uri.len )))
84 89
 		{
85 90
 			DBG("DEBUG: sip_msg_cloner: new_msg->new_uri.s allocation failed\n");
... ...
@@ -115,7 +120,7 @@ struct sip_msg* sip_msg_cloner_1( struct sip_msg *org_msg )
115 120
 			DBG("DEBUG: sip_msg_cloner:  new_msg->via1 allocation failed\n");
116 121
 			goto hf_error;
117 122
 		    }
118
-		    
123
+
119 124
 		    new_hdr->parsed  = (void*)new_msg->via1;
120 125
 		     if ( new_msg->via1->next )
121 126
 		        new_msg->via2 = new_msg->via1->next;
... ...
@@ -471,6 +476,233 @@ void sip_msg_free_1(struct sip_msg* msg)
471 476
 
472 477
 struct sip_msg*  sip_msg_cloner_2( struct sip_msg *org_msg )
473 478
 {
479
+   unsigned int         len;
480
+   struct hdr_field    *hdr,*new_hdr,*last_hdr;
481
+   struct via_body    *via;
482
+   struct via_param *prm;
483
+   struct sip_msg     *new_msg;
484
+   char                       *p;
485
+
486
+
487
+   /*computing the length of entire sip_msg structure*/
488
+   len = sizeof( struct sip_msg );
489
+   /*we will keep only the original msg*/
490
+   len += org_msg->len;
491
+   /*the new uri (if any)*/
492
+   if (org_msg->new_uri.s && org_msg->new_uri.len)
493
+      len+= org_msg->new_uri.len;
494
+   /*all the headers*/
495
+   for( hdr=org_msg->headers ; hdr ; hdr=hdr->next )
496
+   {
497
+      /*sze of header struct*/
498
+      len += sizeof( struct hdr_field);
499
+      switch (hdr->type)
500
+      {
501
+         case HDR_CSEQ:
502
+                   len+=sizeof(struct cseq_body);
503
+                   break;
504
+         case HDR_VIA:
505
+                   for (via=(struct via_body*)hdr->parsed;via;via=via->next)
506
+                   {
507
+                      len+=sizeof(struct via_body);
508
+                      /*via param*/
509
+                      for(prm=via->param_lst;prm;prm=prm->next)
510
+                         len+=sizeof(struct via_param );
511
+                   }
512
+                   break;
513
+      }
514
+   }
515
+
516
+
517
+   p=(char *)sh_malloc(len);
518
+   if (!p)
519
+   {
520
+      LOG(L_ERR , "ERROR: sip_msg_cloner_2: cannot allocate memory\n" );
521
+      return 0;
522
+   }
523
+
524
+   /*filling up the new structure*/
525
+   new_msg = (struct sip_msg*)p;
526
+   /*sip msg structure*/
527
+   memcpy( new_msg , org_msg , sizeof(struct sip_msg) );
528
+   p += sizeof(struct sip_msg);
529
+   new_msg->add_rm = new_msg->repl_add_rm = 0;
530
+   /*new_uri*/
531
+   if (org_msg->new_uri.s && org_msg->new_uri.len)
532
+   {
533
+      new_msg->new_uri.s = p;
534
+      memcpy( p , org_msg->new_uri.s , org_msg->new_uri.len);
535
+      p += org_msg->new_uri.len;
536
+   }
537
+   /*message buffers(org and scratch pad)*/
538
+   memcpy( p , org_msg->orig , org_msg->len);
539
+   new_msg->orig = new_msg->buf = p;
540
+   p += new_msg->len;
541
+   /*unparsed and eoh pointer*/
542
+   new_msg->unparsed = translate_pointer( new_msg->buf , org_msg->buf , org_msg->unparsed );
543
+   new_msg->eoh = translate_pointer( new_msg->buf , org_msg->buf , org_msg->eoh );
544
+   /* first line, updating the pointers*/
545
+   if ( org_msg->first_line.type==SIP_REQUEST )
546
+   {
547
+      new_msg->first_line.u.request.method.s = translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.request.method.s );
548
+      new_msg->first_line.u.request.uri.s = translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.request.uri.s );
549
+      new_msg->first_line.u.request.version.s = translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.request.version.s );
550
+   }
551
+   else if ( org_msg->first_line.type==SIP_REPLY )
552
+   {
553
+      new_msg->first_line.u.reply.version.s = translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.reply.version.s );
554
+      new_msg->first_line.u.reply.status.s =  translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.reply.status.s );
555
+      new_msg->first_line.u.reply.reason.s =  translate_pointer( new_msg->buf , org_msg->buf , org_msg->first_line.u.reply.reason.s );
556
+    }
557
+
558
+   /*headers list*/
559
+   new_msg->via1=0;
560
+   new_msg->via2=0;
561
+   for( hdr=org_msg->headers,last_hdr=0 ; hdr ; hdr=hdr->next )
562
+   {
563
+      new_hdr = (struct hdr_field*)p;
564
+      memcpy(new_hdr, hdr, sizeof(struct hdr_field) );
565
+      p += sizeof( struct hdr_field);
566
+      new_hdr->name.s =  translate_pointer( new_msg->buf , org_msg->buf , hdr->name.s );
567
+      new_hdr->body.s =  translate_pointer( new_msg->buf , org_msg->buf , hdr->body.s );
568
+
569
+     switch (hdr->type)
570
+      {
571
+         case HDR_VIA:
572
+                if ( !new_msg->via1 )
573
+                   {
574
+                       new_msg->h_via1 = new_hdr;
575
+                       new_msg->via1 = via_body_cloner_2( new_msg->buf , org_msg->buf , (struct via_body*)hdr->parsed , &p);
576
+                       new_hdr->parsed  = (void*)new_msg->via1;
577
+                   if ( new_msg->via1->next )
578
+                            new_msg->via2 = new_msg->via1->next;
579
+                   }
580
+                else if ( !new_msg->via2 && new_msg->via1 )
581
+                   {
582
+                       new_msg->h_via2 = new_hdr;
583
+                       if ( new_msg->via1->next )
584
+                           new_hdr->parsed = (void*)new_msg->via1->next;
585
+                       else{
586
+                           new_msg->via2 = via_body_cloner_2( new_msg->buf , org_msg->buf , (struct via_body*)hdr->parsed , &p);
587
+                           new_hdr->parsed  = (void*)new_msg->via2;
588
+                       }
589
+                   }
590
+                else if ( new_msg->via2 && new_msg->via1 )
591
+                   {
592
+                       new_hdr->parsed  = new_msg->via1 = via_body_cloner_2( new_msg->buf , org_msg->buf , (struct via_body*)hdr->parsed , &p);
593
+                   }
594
+                   break;
595
+         case HDR_CSEQ:
596
+                   new_hdr->parsed = p;
597
+                   p +=sizeof(struct cseq_body);
598
+                   memcpy( new_hdr->parsed , hdr->parsed , sizeof(struct cseq_body) );
599
+                   ((struct cseq_body*)new_hdr->parsed)->number.s =  translate_pointer( new_msg->buf , org_msg->buf , ((struct cseq_body*)hdr->parsed)->number.s );
600
+                   ((struct cseq_body*)new_hdr->parsed)->method.s =  translate_pointer( new_msg->buf , org_msg->buf , ((struct cseq_body*)hdr->parsed)->method.s );
601
+                   new_msg->cseq = new_hdr;
602
+                   break;
603
+         case HDR_CALLID:
604
+                   new_msg->callid = new_hdr;
605
+                   break;
606
+         case HDR_TO:
607
+                   new_msg->to = new_hdr;
608
+                   break;
609
+         case HDR_FROM:
610
+                   new_msg->from = new_hdr;
611
+                   break;
612
+         case HDR_CONTACT:
613
+                   new_msg->contact = new_hdr;
614
+                   break;
615
+      }
616
+
617
+     if ( last_hdr )
618
+      {
619
+          last_hdr->next = new_hdr;
620
+          last_hdr=last_hdr->next;
621
+      }
622
+      else
623
+      {
624
+           last_hdr=new_hdr;
625
+           new_msg->headers =new_hdr;
626
+      }
627
+      last_hdr->next = 0;
628
+      new_msg->last_header = last_hdr;
629
+   
630
+   }
631
+
632
+
633
+   return new_msg;
634
+}
635
+
636
+
637
+
638
+struct via_body* via_body_cloner_2( char* new_buf , char *org_buf , struct via_body *org_via, char **p)
639
+{
640
+    struct via_body *new_via;
641
+
642
+    /* clones the via_body structure */
643
+    new_via = (struct via_body*)(*p);
644
+    memcpy( new_via , org_via , sizeof( struct via_body) );
645
+    (*p) += sizeof( struct via_body );
646
+
647
+    /* hdr (str type) */
648
+    new_via->hdr.s = translate_pointer( new_buf , org_buf , org_via->hdr.s );
649
+    /* name (str type) */
650
+    new_via->name.s = translate_pointer( new_buf , org_buf , org_via->name.s );
651
+    /* version (str type) */
652
+    new_via->version.s = translate_pointer( new_buf , org_buf , org_via->version.s );
653
+    /* transport (str type) */
654
+    new_via->transport.s = translate_pointer( new_buf , org_buf , org_via->transport.s );
655
+    /* host (str type) */
656
+    new_via->host.s = translate_pointer( new_buf , org_buf , org_via->host.s );
657
+    /* port_str (str type) */
658
+    new_via->port_str.s = translate_pointer( new_buf , org_buf , org_via->port_str.s );
659
+    /* params (str type) */
660
+    new_via->params.s = translate_pointer( new_buf , org_buf , org_via->params.s );
661
+    /* comment (str type) */
662
+    new_via->comment.s = translate_pointer( new_buf , org_buf , org_via->comment.s );
663
+
664
+
665
+    if ( org_via->param_lst )
666
+    {
667
+       struct via_param *vp, *new_vp, *last_new_vp;
668
+       for( vp=org_via->param_lst, last_new_vp=0 ; vp ; vp=vp->next )
669
+       {
670
+           new_vp = (struct via_param*)(*p);
671
+           memcpy( new_vp , vp , sizeof(struct via_param));
672
+           (*p) += sizeof(struct via_param);
673
+           new_vp->name.s = translate_pointer( new_buf , org_buf , vp->name.s );
674
+           new_vp->value.s = translate_pointer( new_buf , org_buf , vp->value.s );
675
+
676
+           if (new_vp->type==PARAM_BRANCH)
677
+              new_via->branch = new_vp;
678
+
679
+           if (last_new_vp)
680
+              last_new_vp->next = new_vp;
681
+           else
682
+              new_via->param_lst = new_vp;
683
+
684
+           last_new_vp = new_vp;
685
+           last_new_vp->next = NULL;
686
+       }
687
+       new_via->last_param = new_vp;
688
+    }
689
+
690
+
691
+   if ( org_via->next )
692
+       new_via->next = via_body_cloner_2( new_buf , org_buf , org_via->next , p );
693
+
694
+   return new_via;
695
+}
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+void sip_msg_free_2(struct sip_msg* msg)
704
+{
705
+   sh_free( (char*)msg );
474 706
 }
475 707
 
476 708
 
... ...
@@ -11,10 +11,10 @@
11 11
 #include "sh_malloc.h"
12 12
 
13 13
 #define sip_msg_cloner(p_msg) \
14
-    sip_msg_cloner_1(p_msg)
14
+    sip_msg_cloner_2(p_msg)
15 15
 
16 16
 #define sip_msg_free(p_msg) \
17
-    sip_msg_free_1(p_msg)
17
+    sip_msg_free_2(p_msg)
18 18
 
19 19
 
20 20
 struct sip_msg*  sip_msg_cloner_1( struct sip_msg *org_msg );
... ...
@@ -22,8 +22,5 @@ struct sip_msg*  sip_msg_cloner_2( struct sip_msg *org_msg );
22 22
 void                     sip_msg_free_1( struct sip_msg *org_msg );
23 23
 void                     sip_msg_free_2( struct sip_msg *org_msg );
24 24
 
25
-char*   translate_pointer( char* new_buf , char *org_buf , char* p);
26
-struct via_body* via_body_cloner( char* new_buf , char *org_buf , struct via_body *org_via);
27
-struct hdr_field* header_cloner( struct sip_msg *new_msg , struct sip_msg *org_msg, struct hdr_field *hdr);
28 25
 
29 26
 #endif
... ...
@@ -1,4 +1,4 @@
1
-debug=3          # debug level (cmd line: -dddddddddd)
1
+debug=9          # debug level (cmd line: -dddddddddd)
2 2
 check_via=yes     # (cmd. line: -v)
3 3
 dns=on           # (cmd. line: -r)
4 4
 rev_dns=yes      # (cmd. line: -R)
... ...
@@ -12,13 +12,13 @@ loop_checks=1
12 12
 
13 13
 #modules
14 14
 loadmodule "modules/print/print.so"
15
-#loadmodule "modules/tm/tm.so"
15
+loadmodule "modules/tm/tm.so"
16 16
 
17 17
 route{
18 18
 	if ( t_lookup_request()) {
19 19
 		if ( method=="ACK" )	{
20 20
 			log("SER: ACK received -> t_release\n");
21
-			if (! t_forward("195.37.78.233", "6666" )) {
21
+			if (! t_forward("iptel.org", "5060" )) {
22 22
 				log("SER: WARNING: bad forward\n");
23 23
 			};
24 24
 			if (! t_release()) {
... ...
@@ -34,9 +34,9 @@ route{
34 34
 		if (! t_add_transaction()){
35 35
 			log("ERROR in ser: t_add_transaction\n");
36 36
 			if (method=="BYE") {
37
-				forward("195.37.78.233", 5555);
37
+				forward("iptel.org", 5060);
38 38
 			}else{
39
-				forward("195.37.78.233", 6666 );
39
+				forward("iptel.org", 5060 );
40 40
 			};
41 41
 		} else {
42 42
 			if (method=="CANCEL") {
... ...
@@ -53,11 +53,11 @@ route{
53 53
 			};
54 54
 			if (method=="BYE") {
55 55
 				log("SER: BYE received, HACK: forwarding to client\n");
56
-				if (! t_forward("195.37.78.233", "5555")){
56
+				if (! t_forward("iptel.org", "5060")){
57 57
 					log("SER:ERROR: t_forward (..., 5555)\n");
58 58
 				};
59 59
 			}else{
60
-				if (! t_forward("195.37.78.233", "6666" )){
60
+				if (! t_forward("iptel.org", "5060" )){
61 61
 					log("SER:ERROR: t_forward (..., 6666)\n");
62 62
 				};
63 63
 			};