From afa03bd3107c6116f3252451def0498248cf8ffc Mon Sep 17 00:00:00 2001
From: Kinsey Moore <kmoore@digium.com>
Date: Tue, 12 Jun 2012 04:03:23 +0000
Subject: [PATCH] Parse ANI2 information from SIP From header parameters

ANI2 information is now parsed out of SIP From headers when present in
the oli, isup-oli, and ss7-oli parameters and is available via the
CALLERID(ani2) dialplan function.

(closes issue ASTERISK-19912)
Patch-by: Rob Gagnon
Review: https://reviewboard.asterisk.org/r/1947/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@368784 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 UPGRADE.txt         |  4 ++++
 channels/chan_sip.c | 49 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/UPGRADE.txt b/UPGRADE.txt
index 71eb5d8337..10b832171c 100644
--- a/UPGRADE.txt
+++ b/UPGRADE.txt
@@ -101,6 +101,10 @@ SIP
    should be vastly improved.  The HANGUPCAUSE hash should now be used instead
    of SIP_CAUSE. Because of this, the storesipcause option in sip.conf is also
    deprecated.
+ - The sip paramater for Originating Line Information (oli, isup-oli, and
+   ss7-oli) is now parsed out of the From header and copied into the channel's
+   ANI2 information field.  This is readable from the CALLERID(ani2) dialplan
+   function.
 
 chan_unistim
  - Due to massive update in chan_unistim phone keys functions and on-screen 
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 934f8a611a..5fd1bc11a3 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1515,6 +1515,7 @@ static int parse_request(struct sip_request *req);
 static const char *referstatus2str(enum referstatus rstatus) attribute_pure;
 static int method_match(enum sipmethod id, const char *name);
 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
+static void parse_oli(struct sip_request *req, struct ast_channel *chan);
 static const char *find_alias(const char *name, const char *_default);
 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
 static void lws2sws(struct ast_str *msgbuf);
@@ -23894,6 +23895,9 @@ static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int
 		ast_party_redirecting_free(&redirecting);
 	}
 
+	/* Check if OLI/ANI-II is present in From: */
+	parse_oli(req, p->owner);
+
 	/* Session-Timers */
 	if ((p->sipoptions & SIP_OPT_TIMER) && !ast_strlen_zero(sip_get_header(req, "Session-Expires"))) {
 		/* The UAC has requested session-timers for this session. Negotiate
@@ -24214,6 +24218,51 @@ request_invite_cleanup:
 	return res;
 }
 
+/*! \brief Check for the presence of OLI tag(s) in the From header and set on the channel
+ */
+static void parse_oli(struct sip_request *req, struct ast_channel *chan)
+{
+	const char *from = NULL;
+	const char *s = NULL;
+	int ani2 = 0;
+
+	if (!chan || !req) {
+		/* null pointers are not helpful */
+		return;
+	}
+
+	from = sip_get_header(req, "From");
+	if (ast_strlen_zero(from)) {
+		/* no From header */
+		return;
+	}
+
+	/* Look for the possible OLI tags. */
+	if ((s = strcasestr(from, ";isup-oli="))) {
+		s += 10;
+	} else if ((s = strcasestr(from, ";ss7-oli="))) {
+		s += 9;
+	} else if ((s = strcasestr(from, ";oli="))) {
+		s += 5;
+	}
+
+	if (ast_strlen_zero(s)) {
+		/* OLI tag is missing, or present with nothing following the '=' sign */
+		return;
+	}
+
+	/* just in case OLI is quoted */
+	if (*s == '\"') {
+		s++;
+	}
+
+	if (sscanf(s, "%d", &ani2)) {
+		ast_channel_caller(chan)->ani2 = ani2;
+	}
+
+	return;
+}
+
 /*! \brief  Find all call legs and bridge transferee with target
  *	called from handle_request_refer
  *
-- 
GitLab