Browse code

topos: use headers to pass dlg id between receive and send of dlg requests

- it is no longer in the context to be used directly

Daniel-Constantin Mierla authored on 30/03/2016 10:41:00
Showing 3 changed files
... ...
@@ -49,6 +49,7 @@
49 49
 extern int _tps_param_mask_callid;
50 50
 
51 51
 str _sr_hname_xbranch = str_init("P-SR-XBranch");
52
+str _sr_hname_xuuid = str_init("P-SR-XUID");
52 53
 
53 54
 /**
54 55
  *
... ...
@@ -155,7 +156,8 @@ int tps_add_headers(sip_msg_t *msg, str *hname, str *hbody, int hpos)
155 156
 	hs.len = hname->len + 2 + hbody->len;
156 157
 	hs.s  = (char*)pkg_malloc(hs.len + 3);
157 158
 	if (hs.s==NULL) {
158
-		LM_ERR("no pkg memory left\n");
159
+		LM_ERR("no pkg memory left (%.*s - %d)\n",
160
+				hname->len, hname->s, hs.len);
159 161
 		return -1;
160 162
 	}
161 163
 	memcpy(hs.s, hname->s, hname->len);
... ...
@@ -528,9 +530,6 @@ int tps_get_xbranch(sip_msg_t *msg, str *hbody)
528 530
 	if(parse_headers(msg, HDR_EOH_F, 0)<0) {
529 531
 		return -1;
530 532
 	}
531
-	if(tps_add_headers(msg, &_sr_hname_xbranch, hbody, 0)<0) {
532
-		return -1;
533
-	}
534 533
 
535 534
 	for (hf=msg->headers; hf; hf=hf->next)
536 535
 	{
... ...
@@ -548,6 +547,53 @@ int tps_get_xbranch(sip_msg_t *msg, str *hbody)
548 547
 }
549 548
 
550 549
 
550
+/**
551
+ *
552
+ */
553
+int tps_append_xuuid(sip_msg_t *msg, str *hbody)
554
+{
555
+	if(tps_add_headers(msg, &_sr_hname_xuuid, hbody, 0)<0) {
556
+		LM_ERR("failed to add xuuid header [%.*s]/%d\n",
557
+				hbody->len, hbody->s, hbody->len);
558
+		return -1;
559
+	}
560
+
561
+	return 0;
562
+}
563
+
564
+/**
565
+ *
566
+ */
567
+int tps_remove_xuuid(sip_msg_t *msg)
568
+{
569
+	return tps_remove_name_headers(msg, &_sr_hname_xuuid);
570
+}
571
+
572
+/**
573
+ *
574
+ */
575
+int tps_get_xuuid(sip_msg_t *msg, str *hbody)
576
+{
577
+	hdr_field_t *hf;
578
+	if(parse_headers(msg, HDR_EOH_F, 0)<0) {
579
+		return -1;
580
+	}
581
+
582
+	for (hf=msg->headers; hf; hf=hf->next)
583
+	{
584
+		if(_sr_hname_xuuid.len==hf->name.len
585
+				&& strncasecmp(_sr_hname_xuuid.s, hf->name.s,
586
+					hf->name.len)==0) {
587
+			break;
588
+		}
589
+	}
590
+	if(hf!=NULL) {
591
+		*hbody = hf->body;
592
+		return 0;
593
+	}
594
+	return -1;
595
+}
596
+
551 597
 /**
552 598
  *
553 599
  */
... ...
@@ -568,6 +614,39 @@ int tps_reappend_rr(sip_msg_t *msg, tps_data_t *ptsd, str *hbody)
568 614
 int tps_reappend_route(sip_msg_t *msg, tps_data_t *ptsd, str *hbody, int rev)
569 615
 {
570 616
 	str hname = str_init("Route");
617
+	int i;
618
+	int c;
619
+	str sb;
620
+
621
+	if(hbody==NULL || hbody->s==NULL || hbody->len<=0)
622
+		return 0;
623
+
624
+	if(rev==1) {
625
+		c = 0;
626
+		sb.len = 1;
627
+		for(i=hbody->len-2; i>=0; i++) {
628
+			if(hbody->s[i]==',') {
629
+				c = 1;
630
+				if(sb.len>0) {
631
+					sb.s = hbody->s + i + 1;
632
+					if(tps_add_headers(msg, &hname, &sb, 0)<0) {
633
+						return -1;
634
+					}
635
+				}
636
+				sb.len = 0;
637
+			}
638
+			sb.len++;
639
+		}
640
+		if(c==1) {
641
+			if(sb.len>0) {
642
+				sb.s = hbody->s;
643
+				if(tps_add_headers(msg, &hname, &sb, 0)<0) {
644
+					return -1;
645
+				}
646
+			}
647
+			return 0;
648
+		}
649
+	}
571 650
 
572 651
 	if(tps_add_headers(msg, &hname, hbody, 0)<0) {
573 652
 		return -1;
... ...
@@ -637,9 +716,9 @@ int tps_request_received(sip_msg_t *msg, int dialog)
637 716
 	tps_storage_lock_release(&lkey);
638 717
 
639 718
 	if(direction == TPS_DIR_UPSTREAM) {
640
-		nuri = stsd.a_contact;
641
-	} else {
642 719
 		nuri = stsd.b_contact;
720
+	} else {
721
+		nuri = stsd.a_contact;
643 722
 	}
644 723
 	if(nuri.len>0) {
645 724
 		if(rewrite_uri(msg, &nuri)<0) {
... ...
@@ -664,6 +743,9 @@ int tps_request_received(sip_msg_t *msg, int dialog)
664 743
 			return -1;
665 744
 		}
666 745
 	}
746
+	if(dialog!=0) {
747
+		tps_append_xuuid(msg, &stsd.a_uuid);
748
+	}
667 749
 	return 0;
668 750
 
669 751
 error:
... ...
@@ -751,12 +833,15 @@ error:
751 833
 int tps_request_sent(sip_msg_t *msg, int dialog, int local)
752 834
 {
753 835
 	tps_data_t mtsd;
836
+	tps_data_t btsd;
754 837
 	tps_data_t stsd;
755 838
 	tps_data_t *ptsd;
756 839
 	str lkey;
840
+	str xuuid;
757 841
 	int direction = TPS_DIR_DOWNSTREAM;
758 842
 
759 843
 	memset(&mtsd, 0, sizeof(tps_data_t));
844
+	memset(&btsd, 0, sizeof(tps_data_t));
760 845
 	memset(&stsd, 0, sizeof(tps_data_t));
761 846
 	ptsd = &mtsd;
762 847
 
... ...
@@ -765,15 +850,29 @@ int tps_request_sent(sip_msg_t *msg, int dialog, int local)
765 850
 		return -1;
766 851
 	}
767 852
 
853
+	if(dialog!=0) {
854
+		if(tps_get_xuuid(msg, &xuuid)<0) {
855
+			LM_DBG("no x-uuid header - nothing to do\n");
856
+			return 0;
857
+		}
858
+		mtsd.a_uuid = xuuid;
859
+		tps_remove_xuuid(msg);
860
+	}
861
+
768 862
 	lkey = msg->callid->body;
769 863
 
770 864
 	tps_storage_lock_get(&lkey);
771
-	if(dialog==0) {
772
-		if(tps_storage_load_branch(msg, &mtsd, &stsd)!=0) {
773
-			if(tps_storage_record(msg, ptsd)<0) {
774
-				goto error;
775
-			}
776
-		} else {
865
+
866
+	if(tps_storage_load_branch(msg, &mtsd, &btsd)!=0) {
867
+		if(tps_storage_record(msg, ptsd, dialog)<0) {
868
+			goto error;
869
+		}
870
+	} else {
871
+		ptsd = &btsd;
872
+	}
873
+
874
+	if(dialog!=0) {
875
+		if(tps_storage_load_dialog(msg, &btsd, &stsd)==0) {
777 876
 			ptsd = &stsd;
778 877
 		}
779 878
 	}
... ...
@@ -822,7 +921,7 @@ int tps_response_sent(sip_msg_t *msg)
822 921
 	tps_data_t btsd;
823 922
 	str lkey;
824 923
 	int direction = TPS_DIR_UPSTREAM;
825
-	str xvbranch;
924
+	str xvbranch = {0, 0};
826 925
 
827 926
 	memset(&mtsd, 0, sizeof(tps_data_t));
828 927
 	memset(&stsd, 0, sizeof(tps_data_t));
... ...
@@ -326,7 +326,7 @@ error:
326 326
 /**
327 327
  *
328 328
  */
329
-int tps_storage_record(sip_msg_t *msg, tps_data_t *td)
329
+int tps_storage_record(sip_msg_t *msg, tps_data_t *td, int dialog)
330 330
 {
331 331
 	int ret;
332 332
 
... ...
@@ -336,7 +336,7 @@ int tps_storage_record(sip_msg_t *msg, tps_data_t *td)
336 336
 	if(ret<0) goto error;
337 337
 	ret = tps_storage_link_msg(msg, td, TPS_DIR_DOWNSTREAM);
338 338
 	if(ret<0) goto error;
339
-	if(td->s_method_id!=METHOD_MESSAGE) {
339
+	if(dialog==0) {
340 340
 		ret = tps_db_insert_dialog(td);
341 341
 		if(ret<0) goto error;
342 342
 	}
... ...
@@ -80,7 +80,7 @@ int tps_storage_branch_find(sip_msg_t *msg, tps_data_t *td);
80 80
 int tps_storage_branch_save(sip_msg_t *msg, tps_data_t *td);
81 81
 int tps_storage_branch_rm(sip_msg_t *msg, tps_data_t *td);
82 82
 
83
-int tps_storage_record(sip_msg_t *msg, tps_data_t *td);
83
+int tps_storage_record(sip_msg_t *msg, tps_data_t *td, int dialog);
84 84
 int tps_storage_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd);
85 85
 int tps_storage_update_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd);
86 86
 int tps_storage_load_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd);