isup: Hook the reset scanner before we forward the ISUP message

Scan for two kind of reset messages in the ISUP messages. Forward
them to the mgcp_ss7.
diff --git a/include/Makefile.am b/include/Makefile.am
index c46e698..aff4e37 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -2,6 +2,7 @@
 		 mgcp_ss7.h bss_patch.h bssap_sccp.h bsc_data.h udp_input.h \
                  snmp_mtp.h cellmgr_debug.h bsc_sccp.h bsc_ussd.h sctp_m2ua.h \
                  isup_types.h counter.h msc_connection.h ss7_application.h \
-                 mgcp_patch.h ss7_vty.h dtmf_scheduler.h mgcp_callagent.h
+                 mgcp_patch.h ss7_vty.h dtmf_scheduler.h mgcp_callagent.h \
+                 isup_filter.h
 
 SUBDIRS = mgcp
diff --git a/include/bsc_data.h b/include/bsc_data.h
index ecd0747..b1e0fbe 100644
--- a/include/bsc_data.h
+++ b/include/bsc_data.h
@@ -1,7 +1,7 @@
 /* Everything related to the BSC connection */
 /*
- * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
- * (C) 2010-2011 by On-Waves
+ * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2010-2012 by On-Waves
  * All Rights Reserved
  *
  * This program is free software: you can redistribute it and/or modify
@@ -23,6 +23,7 @@
 #define BSC_DATA_H
 
 #include "mtp_data.h"
+#include "mgcp_callagent.h"
 
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/select.h>
@@ -92,6 +93,9 @@
 	struct llist_head mscs;
 	int num_mscs;
 
+	/* Simple send only mgcp agent */
+	struct mgcp_callagent mgcp_agent;
+
 	/* application */
 	struct llist_head apps;
 	int num_apps;
diff --git a/include/isup_filter.h b/include/isup_filter.h
new file mode 100644
index 0000000..2c56f73
--- /dev/null
+++ b/include/isup_filter.h
@@ -0,0 +1,29 @@
+/*
+ * (C) 2012 by Holger Hans Peter Freyther
+ * (C) 2012 by On-Waves
+ * All Rights Reserved
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef isup_filter_h
+#define isup_filter_h
+
+struct ss7_application;
+struct msgb;
+
+int isup_scan_for_reset(struct ss7_application *app, struct msgb *msg);
+
+#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index 4d6061c..d2e211d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,7 +16,7 @@
 		     msc_conn.c link_udp.c snmp_mtp.c debug.c isup.c \
 		     mtp_link.c counter.c sccp_state.c bsc.c ss7_application.c \
 		     vty_interface_legacy.c vty_interface_cmds.c mgcp_patch.c \
-		     mgcp_callagent.c  
+		     mgcp_callagent.c  isup_filter.c
 cellmgr_ng_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) \
 		   $(LIBOSMOSCCP_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto
@@ -26,7 +26,7 @@
 		   bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c input/ipaccess.c \
 		   mtp_link.c counter.c bsc.c ss7_application.c \
 		   vty_interface.c vty_interface_cmds.c mgcp_patch.c \
-		   mgcp_callagent.c
+		   mgcp_callagent.c isup_filter.c
 osmo_stp_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) \
 		 $(LIBOSMOSCCP_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto -lm2ua -lsctp
diff --git a/src/isup_filter.c b/src/isup_filter.c
new file mode 100644
index 0000000..db023b6
--- /dev/null
+++ b/src/isup_filter.c
@@ -0,0 +1,86 @@
+/*
+ * (C) 2012 by Holger Hans Peter Freyther
+ * (C) 2012 by On-Waves
+ * All Rights Reserved
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <isup_filter.h>
+#include <isup_types.h>
+#include <mgcp_callagent.h>
+#include <ss7_application.h>
+#include <bsc_data.h>
+
+
+#include <osmocom/core/msgb.h>
+
+#include <stdio.h>
+#include <string.h>
+
+static void send_reset(struct ss7_application *app, int endp, int range)
+{
+	char buf[1024];
+
+	snprintf(buf, sizeof(buf) - 1,
+			"RSIP 2 %s/%d@127.0.0.1 MGCP 1.0\r\n"
+			"R: %d\n", app->trunk_name, endp, range);
+	buf[sizeof(buf) - 1] = '\0';
+	mgcp_forward(&app->bsc->mgcp_agent,
+			(const uint8_t *) buf, strlen(buf));
+}
+
+
+static void reset_cic(struct ss7_application *app, int cic)
+{
+	return send_reset(app, cic, 1);
+}
+
+static void reset_cics(struct ss7_application *app, int cic, int range)
+{
+	return send_reset(app, cic, range);
+}
+
+/**
+ * Discover resets and forward them to the local MGCP gateway
+ */
+int isup_scan_for_reset(struct ss7_application *app, struct msgb *msg)
+{
+	struct isup_msg_hdr *hdr;
+	int range;
+
+	/* too small for an isup message? */
+	if (msgb_l3len(msg) < sizeof(*hdr))
+		return -1;
+
+	/* no trunk name, don't bother forwarding */
+	if (!app->trunk_name)
+		return 0;
+
+	hdr = (struct isup_msg_hdr *) msg->l3h;
+	switch (hdr->msg_type) {
+	case ISUP_MSG_GRS:
+		range = isup_parse_status(&hdr->data[0],
+				msgb_l3len(msg) - sizeof(*hdr));
+		if (range >= 0)
+			reset_cics(app, hdr->cic, range);
+		break;
+	case ISUP_MSG_RSC:
+		reset_cic(app, hdr->cic);
+		break;
+	}
+
+	return 0;
+}
diff --git a/src/main_stp.c b/src/main_stp.c
index fd20eb3..11f6255 100644
--- a/src/main_stp.c
+++ b/src/main_stp.c
@@ -70,6 +70,12 @@
 extern void cell_vty_init(void);
 extern void handle_options(int argc, char **argv);
 
+static void mgcp_destroy_cb(struct mgcp_callagent *agent, struct msgb *msg)
+{
+	/* we do not care about potential responses here */
+	msgb_free(msg);
+}
+
 static struct mtp_link_set *find_link_set(struct bsc_data *bsc,
 					  int len, const char *buf)
 {
@@ -261,6 +267,13 @@
 		return -1;
 	}
 
+	if (mgcp_create_port(&bsc->mgcp_agent) != 0) {
+		LOGP(DINP, LOGL_ERROR,
+			"Failed to create the MGCP call agent.\n");
+		return -1;
+	}
+	bsc->mgcp_agent.read_cb = mgcp_destroy_cb;
+
 	/* start all apps */
 	llist_for_each_entry(app, &bsc->apps, entry) {
 		LOGP(DINP, LOGL_NOTICE,
diff --git a/src/ss7_application.c b/src/ss7_application.c
index b28c376..cedc2d0 100644
--- a/src/ss7_application.c
+++ b/src/ss7_application.c
@@ -27,6 +27,7 @@
 #include <msc_connection.h>
 #include <sctp_m2ua.h>
 #include <counter.h>
+#include <isup_filter.h>
 
 #include <osmocom/core/talloc.h>
 
@@ -45,6 +46,7 @@
 	struct mtp_link_set *other;
 	other = set->app->route_src.set == set ?
 			set->app->route_dst.set : set->app->route_src.set;
+	isup_scan_for_reset(set->app, msg);
 	mtp_link_set_submit_isup_data(other, sls, msg->l3h, msgb_l3len(msg));
 }
 
diff --git a/src/vty_interface.c b/src/vty_interface.c
index 48e150e..001ba44 100644
--- a/src/vty_interface.c
+++ b/src/vty_interface.c
@@ -298,7 +298,9 @@
 
 	if (app->type == APP_STP) {
 		vty_out(vty, "  isup-pass-through %d%s", app->isup_pass, VTY_NEWLINE);
-		vty_out(vty, "  trunk-name %s%s", app->trunk_name, VTY_NEWLINE);
+		if (app->trunk_name)
+			vty_out(vty, "  trunk-name %s%s",
+				app->trunk_name, VTY_NEWLINE);
 	}
 
 	if (app->type == APP_CELLMGR && app->mgcp_domain_name) {