Browse code

topos: restore stripped headers when receiving a dlg request

Daniel-Constantin Mierla authored on 29/03/2016 22:30:57
Showing 1 changed files
... ...
@@ -33,6 +33,7 @@
33 33
 #include "../../data_lump.h"
34 34
 #include "../../forward.h"
35 35
 #include "../../trim.h"
36
+#include "../../dset.h"
36 37
 #include "../../msg_translator.h"
37 38
 #include "../../parser/parse_rr.h"
38 39
 #include "../../parser/parse_uri.h"
... ...
@@ -255,6 +256,33 @@ int tps_skip_msg(sip_msg_t *msg)
255 256
 	return 0;
256 257
 }
257 258
 
259
+/**
260
+ *
261
+ */
262
+int tps_dlg_message_update(sip_msg_t *msg, tps_data_t *ptsd)
263
+{
264
+	if(parse_sip_msg_uri(msg)<0) {
265
+		LM_ERR("failed to parse r-uri\n");
266
+		return -1;
267
+	}
268
+	if(msg->parsed_uri.user.len<10) {
269
+		LM_DBG("not an expected user format\n");
270
+		return 1;
271
+	}
272
+	if(memcmp(msg->parsed_uri.user.s, "atpsh-", 6)==0) {
273
+		ptsd->a_uuid = msg->parsed_uri.user;
274
+		return 0;
275
+	}
276
+	if(memcmp(msg->parsed_uri.user.s, "btpsh-", 6)==0) {
277
+		ptsd->a_uuid = msg->parsed_uri.user;
278
+		ptsd->b_uuid = msg->parsed_uri.user;
279
+		return 0;
280
+	}
281
+	LM_DBG("not an expected user prefix\n");
282
+
283
+	return 1;
284
+}
285
+
258 286
 /**
259 287
  *
260 288
  */
... ...
@@ -534,16 +562,113 @@ int tps_reappend_rr(sip_msg_t *msg, tps_data_t *ptsd, str *hbody)
534 562
 	return 0;
535 563
 }
536 564
 
565
+/**
566
+ *
567
+ */
568
+int tps_reappend_route(sip_msg_t *msg, tps_data_t *ptsd, str *hbody, int rev)
569
+{
570
+	str hname = str_init("Route");
571
+
572
+	if(tps_add_headers(msg, &hname, hbody, 0)<0) {
573
+		return -1;
574
+	}
575
+
576
+	return 0;
577
+}
578
+
537 579
 /**
538 580
  *
539 581
  */
540 582
 int tps_request_received(sip_msg_t *msg, int dialog)
541 583
 {
584
+	tps_data_t mtsd;
585
+	tps_data_t stsd;
586
+	str lkey;
587
+	str ftag;
588
+	str nuri;
589
+	uint32_t direction = TPS_DIR_DOWNSTREAM;
590
+	int ret;
591
+
542 592
 	if(dialog==0) {
543 593
 		/* nothing to do for initial request */
544 594
 		return 0;
545 595
 	}
596
+
597
+	memset(&mtsd, 0, sizeof(tps_data_t));
598
+	memset(&stsd, 0, sizeof(tps_data_t));
599
+
600
+	if(tps_pack_message(msg, &mtsd)<0) {
601
+		LM_ERR("failed to extract and pack the headers\n");
602
+		return -1;
603
+	}
604
+
605
+	ret = tps_dlg_message_update(msg, &mtsd);
606
+	if(ret<0) {
607
+		LM_ERR("failed to update on dlg message\n");
608
+		return -1;
609
+	}
610
+
611
+	lkey = msg->callid->body;
612
+
613
+	tps_storage_lock_get(&lkey);
614
+
615
+	if(tps_storage_load_dialog(msg, &mtsd, &stsd)<0) {
616
+		goto error;
617
+	}
618
+
619
+	/* detect direction - get from-tag */
620
+	if(parse_from_header(msg)<0 || msg->from==NULL) {
621
+		LM_ERR("failed getting 'from' header!\n");
622
+		goto error;
623
+	}
624
+	ftag = get_from(msg)->tag_value;
625
+
626
+	if(stsd.a_tag.len!=ftag.len) {
627
+		direction = TPS_DIR_UPSTREAM;
628
+	} else {
629
+		if(memcpy(stsd.a_tag.s, ftag.s, ftag.len)==0) {
630
+			direction = TPS_DIR_DOWNSTREAM;
631
+		} else {
632
+			direction = TPS_DIR_UPSTREAM;
633
+		}
634
+	}
635
+	mtsd.direction = direction;
636
+
637
+	tps_storage_lock_release(&lkey);
638
+
639
+	if(direction == TPS_DIR_UPSTREAM) {
640
+		nuri = stsd.a_contact;
641
+	} else {
642
+		nuri = stsd.b_contact;
643
+	}
644
+	if(nuri.len>0) {
645
+		if(rewrite_uri(msg, &nuri)<0) {
646
+			LM_ERR("failed to update r-uri\n");
647
+			return -1;
648
+		}
649
+	}
650
+
651
+	if(tps_reappend_route(msg, &stsd, &stsd.s_rr,
652
+				(direction==TPS_DIR_UPSTREAM)?0:1)<0) {
653
+		LM_ERR("failed to reappend s-route\n");
654
+		return -1;
655
+	}
656
+	if(direction == TPS_DIR_UPSTREAM) {
657
+		if(tps_reappend_route(msg, &stsd, &stsd.a_rr, 0)<0) {
658
+			LM_ERR("failed to reappend a-route\n");
659
+			return -1;
660
+		}
661
+	} else {
662
+		if(tps_reappend_route(msg, &stsd, &stsd.b_rr, 0)<0) {
663
+			LM_ERR("failed to reappend b-route\n");
664
+			return -1;
665
+		}
666
+	}
546 667
 	return 0;
668
+
669
+error:
670
+	tps_storage_lock_release(&lkey);
671
+	return -1;
547 672
 }
548 673
 
549 674
 /**