Browse code

core: helper function to get source address as uri or proxy format

Daniel-Constantin Mierla authored on 02/10/2013 12:23:53
Showing 2 changed files
... ...
@@ -605,3 +605,117 @@ int rewrite_uri(struct sip_msg* _m, str* _s)
605 605
 	return 1;
606 606
 }
607 607
 
608
+/**
609
+ * return src ip, port and proto as a SIP uri or proxy address
610
+ * - value stored in a static buffer
611
+ * - mode=0 return uri, mode=1 return proxy address
612
+ */
613
+int msg_get_src_addr(sip_msg_t *msg, str *uri, int mode)
614
+{
615
+	static char buf[80];
616
+	char* p;
617
+	str ip, port;
618
+	int len;
619
+	str proto;
620
+
621
+	if (msg==NULL || uri==NULL) {
622
+		LM_ERR("invalid parameter value\n");
623
+		return -1;
624
+	}
625
+
626
+	ip.s = ip_addr2a(&msg->rcv.src_ip);
627
+	ip.len = strlen(ip.s);
628
+
629
+	port.s = int2str(msg->rcv.src_port, &port.len);
630
+
631
+	switch(msg->rcv.proto) {
632
+		case PROTO_NONE:
633
+		case PROTO_UDP:
634
+			if(mode==0) {
635
+				proto.s = 0; /* Do not add transport parameter, UDP is default */
636
+				proto.len = 0;
637
+			} else {
638
+				proto.s = "udp";
639
+				proto.len = 3;
640
+			}
641
+		break;
642
+
643
+		case PROTO_TCP:
644
+			proto.s = "tcp";
645
+			proto.len = 3;
646
+		break;
647
+
648
+		case PROTO_TLS:
649
+			proto.s = "tls";
650
+			proto.len = 3;
651
+		break;
652
+
653
+		case PROTO_SCTP:
654
+			proto.s = "sctp";
655
+			proto.len = 4;
656
+		break;
657
+
658
+		case PROTO_WS:
659
+		case PROTO_WSS:
660
+			proto.s = "ws";
661
+			proto.len = 2;
662
+		break;
663
+
664
+		default:
665
+			LM_ERR("unknown transport protocol\n");
666
+		return -1;
667
+	}
668
+
669
+	len = ip.len + 2*(msg->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
670
+	if (mode==0) {
671
+		len += 4;
672
+		if(proto.s) {
673
+			len += TRANSPORT_PARAM_LEN;
674
+			len += proto.len;
675
+		}
676
+	} else {
677
+		len += proto.len + 1;
678
+	}
679
+
680
+	if (len > 79) {
681
+		LM_ERR("buffer too small\n");
682
+		return -1;
683
+	}
684
+
685
+	p = buf;
686
+	if(mode==0) {
687
+		memcpy(p, "sip:", 4);
688
+		p += 4;
689
+	} else {
690
+		memcpy(p, proto.s, proto.len);
691
+		p += proto.len;
692
+		*p++ = ':';
693
+	}
694
+
695
+	if (msg->rcv.src_ip.af==AF_INET6)
696
+		*p++ = '[';
697
+	memcpy(p, ip.s, ip.len);
698
+	p += ip.len;
699
+	if (msg->rcv.src_ip.af==AF_INET6)
700
+		*p++ = ']';
701
+
702
+	*p++ = ':';
703
+
704
+	memcpy(p, port.s, port.len);
705
+	p += port.len;
706
+
707
+	if (mode==0 && proto.s) {
708
+		memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
709
+		p += TRANSPORT_PARAM_LEN;
710
+
711
+		memcpy(p, proto.s, proto.len);
712
+		p += proto.len;
713
+	}
714
+
715
+	uri->s = buf;
716
+	uri->len = len;
717
+	uri->s[uri->len] = '\0';
718
+
719
+	return 0;
720
+}
721
+
... ...
@@ -169,6 +169,11 @@ char* print_dset(struct sip_msg* msg, int* len);
169 169
 void set_ruri_q(qvalue_t q);
170 170
 
171 171
 
172
+/*! \brief
173
+ * Get src ip, port and proto as SIP uri or proxy address
174
+ */
175
+int msg_get_src_addr(sip_msg_t *msg, str *uri, int mode);
176
+
172 177
 /*! \brief
173 178
  * Get the q value of the Request-URI
174 179
  */