Forráskód Böngészése

lib/ims: fn to get IMPI from FROM header instead of TO

jaybeepee 10 éve
szülő
commit
cc9c6554e9
2 módosított fájl, 105 hozzáadás és 2 törlés
  1. 91 2
      lib/ims/ims_getters.c
  2. 14 0
      lib/ims/ims_getters.h

+ 91 - 2
lib/ims/ims_getters.c

@@ -109,6 +109,62 @@ contact_body_t *cscf_parse_contacts(struct sip_msg *msg)
 	if (!msg->contact) return 0;
 	return msg->contact->parsed;
 }
+/**
+ * Returns the Private Identity extracted from the Authorization header.
+ * If none found there takes the SIP URI in To without the "sip:" prefix
+ * \todo - remove the fallback case to the To header
+ * @param msg - the SIP message
+ * @param realm - the realm to match in an Authorization header
+ * @returns the str containing the private id, no mem dup
+ */
+str cscf_get_private_identity(struct sip_msg *msg, str realm) {
+	str pi = {0, 0};
+	struct hdr_field* h = 0;
+	int ret, i, res;
+
+	if (realm.len && realm.s) {
+		ret = find_credentials(msg, &realm, HDR_AUTHORIZATION_F, &h);
+		if (ret < 0) {
+			ret = find_credentials(msg, &realm, HDR_PROXYAUTH_F, &h);
+			if (ret < 0) {
+				goto fallback;
+			} else {
+				if (ret >0) {
+					goto fallback;
+				}
+				h = msg->proxy_auth;
+			}
+		} else {
+			if (ret > 0) {
+				goto fallback;
+			}
+		}
+	}
+
+	res = parse_credentials(h);
+	if (res != 0) {
+		LOG(L_ERR, "Error while parsing credentials\n");
+		return pi;
+	}
+
+	if (h) pi = ((auth_body_t*) h->parsed)->digest.username.whole;
+
+	goto done;
+
+fallback:
+	pi = cscf_get_public_identity(msg);
+	if (pi.len > 4 && strncasecmp(pi.s, "sip:", 4) == 0) {
+		pi.s += 4;
+		pi.len -= 4;
+	}
+	for (i = 0; i < pi.len; i++)
+		if (pi.s[i] == ';') {
+			pi.len = i;
+			break;
+		}
+done:
+	return pi;
+}
 
 /**
  * Returns the Private Identity extracted from the Authorization header.
@@ -118,7 +174,7 @@ contact_body_t *cscf_parse_contacts(struct sip_msg *msg)
  * @param realm - the realm to match in an Authorization header
  * @returns the str containing the private id, no mem dup
  */
-str cscf_get_private_identity(struct sip_msg *msg, str realm)
+str cscf_get_private_identity_from(struct sip_msg *msg, str realm)
 {
 	str pi={0,0};
 	struct hdr_field* h=0;
@@ -154,7 +210,7 @@ str cscf_get_private_identity(struct sip_msg *msg, str realm)
 	goto done;
 
 	fallback:
-	pi = cscf_get_public_identity(msg);
+	pi = cscf_get_public_identity_from(msg);
 	if (pi.len>4&&strncasecmp(pi.s,"sip:",4)==0) {pi.s+=4;pi.len-=4;}
 	for(i=0;i<pi.len;i++)
 		if (pi.s[i]==';') {
@@ -237,6 +293,39 @@ str cscf_get_public_identity(struct sip_msg *msg)
 	return pu;
 }
 
+/**
+ * Returns the Public Identity extracted from the From header
+ * @param msg - the SIP message
+ * @returns the str containing the public id, no mem dup
+ */
+str cscf_get_public_identity_from(struct sip_msg *msg)
+{
+	str pu={0,0};
+	struct to_body *from;
+	int i;
+
+	if (parse_headers(msg,HDR_FROM_F,0)!=0) {
+		return pu;
+	}
+
+	if ( get_from(msg) == NULL ) {
+		from = (struct to_body*) pkg_malloc(sizeof(struct to_body));
+		parse_to( msg->from->body.s, msg->from->body.s + msg->from->body.len, from );
+		msg->from->parsed = from;
+	}
+	else from=(struct to_body *) msg->from->parsed;
+
+	pu = from->uri;
+
+	/* truncate to sip:username@host or tel:number */
+	for(i=4;i<pu.len;i++)
+		if (pu.s[i]==';' || pu.s[i]=='?' ||pu.s[i]==':'){
+			pu.len = i;
+		}
+
+	return pu;
+}
+
 
 
 /**

+ 14 - 0
lib/ims/ims_getters.h

@@ -119,12 +119,26 @@ contact_body_t *cscf_parse_contacts(struct sip_msg *msg);
  * @returns the str containing the private id, no mem dup
  */
 str cscf_get_private_identity(struct sip_msg *msg, str realm);
+/**
+ * Returns the Private Identity extracted from the Authorization header.
+ * If none found there takes the SIP URI in from without the "sip:" prefix
+ * @param msg - the SIP message
+ * @param realm - the realm to match in an Authorization header
+ * @returns the str containing the private id, no mem dup
+ */
+str cscf_get_private_identity_from(struct sip_msg *msg, str realm);
 /**
  * Returns the Public Identity extracted from the To header
  * @param msg - the SIP message
  * @returns the str containing the public id, no mem dup
  */
 str cscf_get_public_identity(struct sip_msg *msg);
+/**
+ * Returns the Public Identity extracted from the From header
+ * @param msg - the SIP message
+ * @returns the str containing the public id, no mem dup
+ */
+str cscf_get_public_identity_from(struct sip_msg *msg);
 /**
  * Returns the expires value from the Expires header in the message.
  * It searches into the Expires header and if not found returns -1