ss7: Create a SS7 application that is responsible for the routing
diff --git a/include/Makefile.am b/include/Makefile.am
index ce617e7..a4cdfb3 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,6 +1,6 @@
 noinst_HEADERS = mtp_level3.h mtp_data.h ipaccess.h thread.h mtp_pcap.h \
 		 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
+                 isup_types.h counter.h msc_connection.h ss7_application.h
 
 SUBDIRS = mgcp
diff --git a/include/bsc_data.h b/include/bsc_data.h
index ac799f1..fc1dcdf 100644
--- a/include/bsc_data.h
+++ b/include/bsc_data.h
@@ -67,12 +67,6 @@
 	struct snmp_mtp_session *session;
 };
 
-enum {
-	APP_CELLMGR,
-	APP_RELAY,
-	APP_STP,
-};
-
 struct bsc_data {
 	int app;
 
@@ -116,6 +110,10 @@
 	/* MSCs */
 	struct llist_head mscs;
 	int num_mscs;
+
+	/* application */
+	struct llist_head apps;
+	int num_apps;
 };
 
 /* bsc related functions */
@@ -125,7 +123,6 @@
 void mtp_linkset_up(struct mtp_link_set *);
 
 /* connection tracking and action */
-void update_con_state(struct msc_connection *msc, int rc, struct sccp_parse_result *result, struct msgb *msg, int from_msc, int sls);
 
 /* udp init */
 int link_global_init(struct mtp_udp_data *data, int src_port);
diff --git a/include/bsc_sccp.h b/include/bsc_sccp.h
index 9c3bb11..f7489bb 100644
--- a/include/bsc_sccp.h
+++ b/include/bsc_sccp.h
@@ -30,7 +30,7 @@
 
 #include <osmocom/sccp/sccp.h>
 
-struct msc_connection;
+struct ss7_application;
 
 /*
  * One SCCP connection.
@@ -55,7 +55,8 @@
 	/* how often did we send a RLSD this */
 	unsigned int rls_tries;
 
-	/* MTP link this was coming in */
+	/* Link to the SS7 Application */
+	struct ss7_application *app;
 	struct mtp_link_set *link;
 
 	/* sls id */
@@ -63,10 +64,10 @@
 };
 
 void free_con(struct active_sccp_con *con);
-struct active_sccp_con *find_con_by_dest_ref(struct msc_connection *, struct sccp_source_reference *ref);
-struct active_sccp_con *find_con_by_src_ref(struct msc_connection *,struct sccp_source_reference *src_ref);
-struct active_sccp_con *find_con_by_src_dest_ref(struct msc_connection *, struct sccp_source_reference *src_ref,
+struct active_sccp_con *find_con_by_dest_ref(struct ss7_application *, struct sccp_source_reference *ref);
+struct active_sccp_con *find_con_by_src_ref(struct ss7_application *,struct sccp_source_reference *src_ref);
+struct active_sccp_con *find_con_by_src_dest_ref(struct ss7_application *, struct sccp_source_reference *src_ref,
 						 struct sccp_source_reference *dst_ref);
-unsigned int sls_for_src_ref(struct msc_connection *, struct sccp_source_reference *ref);
+unsigned int sls_for_src_ref(struct ss7_application *, struct sccp_source_reference *ref);
 
 #endif
diff --git a/include/msc_connection.h b/include/msc_connection.h
index 51a5326..c7957ec 100644
--- a/include/msc_connection.h
+++ b/include/msc_connection.h
@@ -55,16 +55,12 @@
 	int pong_time;
 	struct timer_list ping_timeout;
 	struct timer_list pong_timeout;
-	struct timer_list reset_timeout;
 
 	/* mgcp messgaes */
 	struct write_queue mgcp_agent;
 
 	/* application pointer */
-	struct llist_head sccp_connections;
-	struct mtp_link_set *target_link;
-	int forward_only;
-	int reset_count;
+	struct ss7_application *app;
 };
 
 /* msc related functions */
diff --git a/include/mtp_data.h b/include/mtp_data.h
index 6ed31c3..48b0ddb 100644
--- a/include/mtp_data.h
+++ b/include/mtp_data.h
@@ -25,10 +25,10 @@
 #include <osmocore/utils.h>
 
 struct bsc_data;
-struct msc_connection;
 struct mtp_link;
 struct mtp_level_3_mng *mng;
 struct rate_ctr_group;
+struct ss7_application;
 
 /* MTP Level3 timers */
 
@@ -76,8 +76,7 @@
 
 	/* custom data */
 	struct bsc_data *bsc;
-	struct msc_connection *fw;
-	struct mtp_link_set *forward;
+	struct ss7_application *app;
 };
 
 /**
diff --git a/include/ss7_application.h b/include/ss7_application.h
new file mode 100644
index 0000000..76117e6
--- /dev/null
+++ b/include/ss7_application.h
@@ -0,0 +1,82 @@
+/* Stuff to handle the SS7 application */
+/*
+ * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * 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 SS7_APPLICATION_H
+#define SS7_APPLICATION_H
+
+#include <osmocore/linuxlist.h>
+#include <osmocore/timer.h>
+
+struct bsc_data;
+struct msc_connection;
+struct mtp_link_set;
+struct mtp_link;
+
+enum ss7_set_type {
+	SS7_SET_LINKSET,
+	SS7_SET_MSC,
+};
+
+enum ss7_app_type {
+	APP_CELLMGR,
+	APP_RELAY,
+	APP_STP,
+};
+
+struct ss7_application_route {
+	int type;
+	int nr;
+
+	/* maybe they were resolved */
+	struct mtp_link_set *set;
+	struct msc_connection *msc;
+};
+
+struct ss7_application {
+	/* handling */
+	struct llist_head entry;
+	int nr;
+	char *name;
+
+	/* app type */
+	int type;
+
+	/* for the routing */
+	struct ss7_application_route route_src;
+	struct ss7_application_route route_dst;
+
+	struct bsc_data *bsc;
+
+	/* handling for the NAT/State handling */
+	struct llist_head sccp_connections;
+	struct timer_list reset_timeout;
+	struct mtp_link_set *target_link;
+	int forward_only;
+	int reset_count;
+};
+
+
+struct ss7_application *ss7_application_alloc(struct bsc_data *);
+struct ss7_application *ss7_application_num(struct bsc_data *, int nr);
+int ss7_application_setup(struct ss7_application *, int type,
+			  int src_type, int src_num,
+			  int dst_type, int dst_num);
+
+#endif