From 595ab444af8717a3430f66299eb85a0b924f1c51 Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Wed, 2 Sep 2009 23:25:33 +0000
Subject: [PATCH] Made chan_dahdi able to ignore incoming calls that are not in
 a MSN list for ISDN PTMP CPE spans.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@215757 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 CHANGES                        |  2 ++
 channels/chan_dahdi.c          |  4 ++++
 channels/sig_pri.c             | 42 ++++++++++++++++++++++++++++++++++
 channels/sig_pri.h             |  1 +
 configs/chan_dahdi.conf.sample |  8 +++++++
 5 files changed, 57 insertions(+)

diff --git a/CHANGES b/CHANGES
index a59aff12e0..533c45cac5 100644
--- a/CHANGES
+++ b/CHANGES
@@ -189,6 +189,8 @@ libpri channel driver (chan_dahdi) DAHDI changes
    will update the redirecting-to presentation (COLR) when it becomes available.
  * Added Reverse Charging Indication receipt & transmission (requires latest
    LibPRI).
+ * Added the ability to ignore calls that are not in a Multiple Subscriber
+   Number (MSN) list for PTMP CPE interfaces.
 
 Asterisk Manager Interface
 --------------------------
diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index 57715cc39a..7f4c7f6d1a 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -10843,6 +10843,7 @@ static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf,
 						pris[span].pri.inbanddisconnect = conf->pri.pri.inbanddisconnect;
 #endif
 						pris[span].pri.facilityenable = conf->pri.pri.facilityenable;
+						ast_copy_string(pris[span].pri.msn_list, conf->pri.pri.msn_list, sizeof(pris[span].pri.msn_list));
 						ast_copy_string(pris[span].pri.idledial, conf->pri.pri.idledial, sizeof(pris[span].pri.idledial));
 						ast_copy_string(pris[span].pri.idleext, conf->pri.pri.idleext, sizeof(pris[span].pri.idleext));
 						ast_copy_string(pris[span].pri.internationalprefix, conf->pri.pri.internationalprefix, sizeof(pris[span].pri.internationalprefix));
@@ -15614,6 +15615,9 @@ static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct
 					ast_log(LOG_ERROR, "Unknown switchtype '%s' at line %d.\n", v->value, v->lineno);
 					return -1;
 				}
+			} else if (!strcasecmp(v->name, "msn")) {
+				ast_copy_string(confp->pri.pri.msn_list, v->value,
+					sizeof(confp->pri.pri.msn_list));
 			} else if (!strcasecmp(v->name, "nsf")) {
 				if (!strcasecmp(v->value, "sdn"))
 					confp->pri.pri.nsf = PRI_NSF_SDN;
diff --git a/channels/sig_pri.c b/channels/sig_pri.c
index 9276ebb155..7dcbd67937 100644
--- a/channels/sig_pri.c
+++ b/channels/sig_pri.c
@@ -999,6 +999,39 @@ void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
 		pri_restart(pri->dchans[index]);
 }
 
+/*!
+ * \internal
+ * \brief Determine if the given extension matches one of the MSNs in the pattern list.
+ * \since 1.6.3
+ *
+ * \param msn_patterns Comma separated list of MSN patterns to match.
+ * \param exten Extension to match in the MSN list.
+ *
+ * \retval 1 if matches.
+ * \retval 0 if no match.
+ */
+static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
+{
+	char *pattern;
+	char *msn_list;
+	char *list_tail;
+
+	msn_list = strdupa(msn_patterns);
+
+	list_tail = NULL;
+	pattern = strtok_r(msn_list, ",", &list_tail);
+	while (pattern) {
+		pattern = ast_strip(pattern);
+		if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
+			/* Extension matched the pattern. */
+			return 1;
+		}
+		pattern = strtok_r(NULL, ",", &list_tail);
+	}
+	/* Did not match any pattern in the list. */
+	return 0;
+}
+
 /*!
  * \internal
  * \brief Obtain the sig_pri owner channel lock if the owner exists.
@@ -1621,6 +1654,15 @@ static void *pri_dchannel(void *vpri)
 				break;
 #endif
 			case PRI_EVENT_RING:
+				if (!ast_strlen_zero(pri->msn_list)
+					&& !sig_pri_msn_match(pri->msn_list, e->ring.callednum)) {
+					/* The call is not for us so ignore it. */
+					ast_verb(3,
+						"Ignoring call to '%s' on span %d.  Its not in the MSN list: %s\n",
+						e->ring.callednum, pri->span, pri->msn_list);
+					pri_destroycall(pri->pri, e->ring.call);
+					break;
+				}
 				if (e->ring.channel == -1)
 					chanpos = pri_find_empty_chan(pri, 1);
 				else
diff --git a/channels/sig_pri.h b/channels/sig_pri.h
index 91de16f9aa..40dad9d86c 100644
--- a/channels/sig_pri.h
+++ b/channels/sig_pri.h
@@ -195,6 +195,7 @@ struct sig_pri_pri {
 	char privateprefix[20];					/*!< for private dialplans */
 	char unknownprefix[20];					/*!< for unknown dialplans */
 	long resetinterval;						/*!< Interval (in seconds) for resetting unused channels */
+	char msn_list[AST_MAX_EXTENSION];		/*!< Comma separated list of MSNs to handle.  Empty if disabled. */
 	char idleext[AST_MAX_EXTENSION];		/*!< Where to idle extra calls */
 	char idlecontext[AST_MAX_CONTEXT];		/*!< What context to use for idle */
 	char idledial[AST_MAX_EXTENSION];		/*!< What to dial before dumping */
diff --git a/configs/chan_dahdi.conf.sample b/configs/chan_dahdi.conf.sample
index fe81874caf..5f8d2b292a 100644
--- a/configs/chan_dahdi.conf.sample
+++ b/configs/chan_dahdi.conf.sample
@@ -67,6 +67,14 @@
 ;
 ;switchtype=euroisdn
 ;
+; MSNs for ISDN spans.  Asterisk will listen for the listed numbers on
+; incoming calls and ignore any calls not listed.
+; Here you can give a comma separated list of numbers or dialplan extension
+; patterns.  An empty list disables MSN matching to allow any incoming call.
+; Only set on PTMP CPE side of ISDN span if needed.
+; The default is an empty list.
+;msn=
+;
 ; Some switches (AT&T especially) require network specific facility IE.
 ; Supported values are currently 'none', 'sdn', 'megacom', 'tollfreemegacom', 'accunet'
 ;
-- 
GitLab