bsc: Allocate the bsc with talloc instead

Allocate the bsc with talloc to have a nice root context for
everything in the system.
diff --git a/src/Makefile.am b/src/Makefile.am
index 8071b72..edd1723 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -12,12 +12,12 @@
 cellmgr_ng_SOURCES = main.c mtp_layer3.c thread.c input/ipaccess.c pcap.c \
 		     bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c links.c \
 		     msc_conn.c link_udp.c snmp_mtp.c debug.c vty_interface.c isup.c \
-		     mtp_link.c counter.c sccp_state.c
+		     mtp_link.c counter.c sccp_state.c bsc.c
 cellmgr_ng_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto
 
 osmo_stp_SOURCES = main_stp.c mtp_layer3.c thread.c pcap.c link_udp.c snmp_mtp.c \
 		   debug.c vty_interface.c links.c isup.c sctp_m2ua.c \
-		   mtp_link.c counter.c
+		   mtp_link.c counter.c bsc.c
 osmo_stp_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto -lm2ua -lsctp
diff --git a/src/bsc.c b/src/bsc.c
new file mode 100644
index 0000000..c26c6ec
--- /dev/null
+++ b/src/bsc.c
@@ -0,0 +1,54 @@
+/* Everything related to the global BSC */
+/*
+ * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2010-2011 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 <bsc_data.h>
+#include <cellmgr_debug.h>
+#include <mtp_level3.h>
+
+#include <osmocore/talloc.h>
+
+struct bsc_data *bsc_data_create()
+{
+	struct bsc_data *bsc;
+
+	bsc = talloc_zero(NULL, struct bsc_data);
+	if (!bsc) {
+		LOGP(DINP, LOGL_ERROR, "Failed to create the BSC.\n");
+		return NULL;
+	}
+
+	INIT_LLIST_HEAD(&bsc->links);
+	bsc->dpc = 1;
+	bsc->opc = 0;
+	bsc->sccp_opc = -1;
+	bsc->isup_opc = -1;
+	bsc->udp_port = 3456;
+	bsc->udp_ip = NULL;
+	bsc->udp_nr_links = 1;
+	bsc->src_port = 1313;
+	bsc->ni_ni = MTP_NI_NATION_NET;
+	bsc->ni_spare = 0;
+	bsc->setup = 0;
+	bsc->pcap_fd = -1;
+	bsc->udp_reset_timeout = 180;
+
+	return bsc;
+}
diff --git a/src/links.c b/src/links.c
index f300b83..b694b8c 100644
--- a/src/links.c
+++ b/src/links.c
@@ -28,7 +28,7 @@
 
 #include <osmocore/talloc.h>
 
-extern struct bsc_data bsc;
+extern struct bsc_data *bsc;
 
 int is_one_up(struct mtp_link_set *set)
 {
@@ -89,9 +89,9 @@
 {
 	struct mtp_link_set *set = _set;
 	struct mtp_link *data;
-	bsc.setup = 1;
+	bsc->setup = 1;
 
-	if (msc_init(&bsc.msc_forward, 1) != 0) {
+	if (msc_init(&bsc->msc_forward, 1) != 0) {
 		fprintf(stderr, "Failed to init MSC part.\n");
 		exit(3);
 	}
diff --git a/src/main.c b/src/main.c
index d3f80e9..d23961f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -61,7 +61,7 @@
 
 static char *config = "cellmgr_ng.cfg";
 
-struct bsc_data bsc;
+struct bsc_data *bsc;
 extern void cell_vty_init(void);
 
 static void print_usage()
@@ -84,8 +84,8 @@
 
 	printf("Terminating.\n");
 	handled = 1;
-	if (bsc.setup) {
-		llist_for_each_entry(set, &bsc.links, entry)
+	if (bsc && bsc->setup) {
+		llist_for_each_entry(set, &bsc->links, entry)
 			link_shutdown_all(set);
 	}
 
@@ -98,7 +98,7 @@
 static void sigusr2()
 {
 	printf("Closing the MSC connection on demand.\n");
-	msc_close_connection(&bsc.msc_forward);
+	msc_close_connection(&bsc->msc_forward);
 }
 
 static void print_help()
@@ -134,14 +134,14 @@
 			print_help();
 			exit(0);
 		case 'p':
-			if (bsc.pcap_fd >= 0)
-				close(bsc.pcap_fd);
-			bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
-			if (bsc.pcap_fd < 0) {
+			if (bsc->pcap_fd >= 0)
+				close(bsc->pcap_fd);
+			bsc->pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
+			if (bsc->pcap_fd < 0) {
 				fprintf(stderr, "Failed to open PCAP file.\n");
 				exit(0);
 			}
-			mtp_pcap_write_header(bsc.pcap_fd);
+			mtp_pcap_write_header(bsc->pcap_fd);
 			break;
 		case 'c':
 			config = optarg;
@@ -173,22 +173,7 @@
 {
 	int rc;
 	struct mtp_link_set *set;
-	INIT_LLIST_HEAD(&bsc.links);
 
-	bsc.app = APP_CELLMGR;
-	bsc.dpc = 1;
-	bsc.opc = 0;
-	bsc.sccp_opc = -1;
-	bsc.isup_opc = -1;
-	bsc.udp_port = 3456;
-	bsc.udp_ip = NULL;
-	bsc.udp_nr_links = 1;
-	bsc.src_port = 1313;
-	bsc.ni_ni = MTP_NI_NATION_NET;
-	bsc.ni_spare = 0;
-	bsc.setup = 0;
-	bsc.pcap_fd = -1;
-	bsc.udp_reset_timeout = 180;
 
 	mtp_link_set_init();
 	thread_init();
@@ -208,8 +193,13 @@
 
 	sccp_set_log_area(DSCCP);
 
+	bsc = bsc_data_create();
+	if (!bsc)
+		return -1;
+	bsc->app = APP_CELLMGR;
+
 	/* msc data */
-	bsc_msc_forward_init(&bsc, &bsc.msc_forward);
+	bsc_msc_forward_init(bsc, &bsc->msc_forward);
 
 	handle_options(argc, argv);
 
@@ -228,13 +218,13 @@
 	if (rc < 0)
 		return rc;
 
-	set = link_init(&bsc);
+	set = link_init(bsc);
 	if (!set)
 		return -1;
 
-	llist_add(&set->entry, &bsc.links);
-	set->fw = &bsc.msc_forward;
-	bsc.msc_forward.bsc = set;
+	llist_add(&set->entry, &bsc->links);
+	set->fw = &bsc->msc_forward;
+	bsc->msc_forward.bsc = set;
 
         while (1) {
 		bsc_select_main(0);
diff --git a/src/main_stp.c b/src/main_stp.c
index 9a5eb89..43491b8 100644
--- a/src/main_stp.c
+++ b/src/main_stp.c
@@ -63,7 +63,7 @@
 
 static char *config = "osmo_stp.cfg";
 
-struct bsc_data bsc;
+struct bsc_data *bsc;
 extern void cell_vty_init(void);
 
 /*
@@ -111,8 +111,8 @@
 
 	printf("Terminating.\n");
 	handled = 1;
-	if (bsc.setup) {
-		llist_for_each_entry(set, &bsc.links, entry)
+	if (bsc && bsc->setup) {
+		llist_for_each_entry(set, &bsc->links, entry)
 			link_shutdown_all(set);
 	}
 	exit(0);
@@ -154,14 +154,14 @@
 			print_help();
 			exit(0);
 		case 'p':
-			if (bsc.pcap_fd >= 0)
-				close(bsc.pcap_fd);
-			bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
-			if (bsc.pcap_fd < 0) {
+			if (bsc->pcap_fd >= 0)
+				close(bsc->pcap_fd);
+			bsc->pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
+			if (bsc->pcap_fd < 0) {
 				fprintf(stderr, "Failed to open PCAP file.\n");
 				exit(0);
 			}
-			mtp_pcap_write_header(bsc.pcap_fd);
+			mtp_pcap_write_header(bsc->pcap_fd);
 			break;
 		case 'c':
 			config = optarg;
@@ -307,22 +307,6 @@
 	struct mtp_link_set *set;
 	struct mtp_link_set *m2ua_set;
 	struct mtp_m2ua_link *lnk;
-	INIT_LLIST_HEAD(&bsc.links);
-
-	bsc.app = APP_STP;
-	bsc.dpc = 1;
-	bsc.opc = 0;
-	bsc.sccp_opc = -1;
-	bsc.isup_opc = -1;
-	bsc.udp_port = 3456;
-	bsc.udp_ip = NULL;
-	bsc.src_port = 1313;
-	bsc.ni_ni = MTP_NI_NATION_NET;
-	bsc.ni_spare = 0;
-	bsc.udp_nr_links = 1;
-	bsc.setup = 0;
-	bsc.pcap_fd = -1;
-	bsc.udp_reset_timeout = 180;
 
 	mtp_link_set_init();
 	thread_init();
@@ -343,6 +327,11 @@
 	sccp_set_log_area(DSCCP);
 	m2ua_set_log_area(DM2UA);
 
+	bsc = bsc_data_create();
+	if (!bsc)
+		return -1;
+	bsc->app = APP_STP;
+
 	handle_options(argc, argv);
 
 	signal(SIGPIPE, SIG_IGN);
@@ -359,15 +348,15 @@
 	if (rc < 0)
 		return rc;
 
-	if (inject_init(&bsc) != 0) {
+	if (inject_init(bsc) != 0) {
 		LOGP(DINP, LOGL_NOTICE, "Failed to initialize inject interface.\n");
 		return -1;
 	}
 
-	set = link_init(&bsc);
+	set = link_init(bsc);
 	if (!set)
 		return -1;
-	llist_add(&set->entry, &bsc.links);
+	llist_add(&set->entry, &bsc->links);
 
 	m2ua_set = mtp_link_set_alloc();
 	m2ua_set->dpc = 92;
@@ -375,15 +364,15 @@
 	m2ua_set->sccp_opc = 9;
 	m2ua_set->isup_opc = 9;
 	m2ua_set->ni = 3;
-	m2ua_set->bsc = &bsc;
-	m2ua_set->pcap_fd = bsc.pcap_fd;
+	m2ua_set->bsc = bsc;
+	m2ua_set->pcap_fd = bsc->pcap_fd;
 	m2ua_set->name = talloc_strdup(m2ua_set, "M2UA");
-	llist_add(&m2ua_set->entry, &bsc.links);
+	llist_add(&m2ua_set->entry, &bsc->links);
 
 	/* setup things */
-	set->pass_all_isup = bsc.isup_pass;
+	set->pass_all_isup = bsc->isup_pass;
 	set->forward = m2ua_set;
-	m2ua_set->pass_all_isup = bsc.isup_pass;
+	m2ua_set->pass_all_isup = bsc->isup_pass;
 	m2ua_set->forward = set;
 
 	lnk = sctp_m2ua_transp_create("0.0.0.0", 2904);
diff --git a/src/sctp_m2ua.c b/src/sctp_m2ua.c
index 2bafdab..2b8ac07 100644
--- a/src/sctp_m2ua.c
+++ b/src/sctp_m2ua.c
@@ -30,8 +30,6 @@
 #include <string.h>
 #include <unistd.h>
 
-extern struct bsc_data bsc;
-
 static void link_down(struct mtp_link *link)
 {
 	rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_ERROR]);
diff --git a/src/vty_interface.c b/src/vty_interface.c
index d9eb898..3d2b6a6 100644
--- a/src/vty_interface.c
+++ b/src/vty_interface.c
@@ -43,7 +43,7 @@
 #undef PACKAGE_STRING
 #include <cellmgr_config.h>
 
-extern struct bsc_data bsc;
+extern struct bsc_data *bsc;
 
 static struct vty_app_info vty_info = {
 	.name 		= "Cellmgr-ng",
@@ -65,22 +65,22 @@
 static int config_write_cell(struct vty *vty)
 {
 	vty_out(vty, "cellmgr%s", VTY_NEWLINE);
-	vty_out(vty, " mtp dpc %d%s", bsc.dpc, VTY_NEWLINE);
-	vty_out(vty, " mtp opc %d%s", bsc.opc, VTY_NEWLINE);
-	vty_out(vty, " mtp sccp-opc %d%s", bsc.sccp_opc, VTY_NEWLINE);
-	vty_out(vty, " mtp ni %d%s", bsc.ni_ni, VTY_NEWLINE);
-	vty_out(vty, " mtp spare %d%s", bsc.ni_spare, VTY_NEWLINE);
-	vty_out(vty, " mtp sltm once %d%s", bsc.once, VTY_NEWLINE);
-	if (bsc.udp_ip)
-		vty_out(vty, " udp dest ip %s%s", bsc.udp_ip, VTY_NEWLINE);
-	vty_out(vty, " udp dest port %d%s", bsc.udp_port, VTY_NEWLINE);
-	vty_out(vty, " udp src port %d%s", bsc.src_port, VTY_NEWLINE);
-	vty_out(vty, " udp reset %d%s", bsc.udp_reset_timeout, VTY_NEWLINE);
-	vty_out(vty, " udp number-links %d%s", bsc.udp_nr_links, VTY_NEWLINE);
-	vty_out(vty, " msc ip %s%s", bsc.msc_forward.msc_address, VTY_NEWLINE);
-	vty_out(vty, " msc ip-dscp %d%s", bsc.msc_forward.msc_ip_dscp, VTY_NEWLINE);
-	vty_out(vty, " msc token %s%s", bsc.msc_forward.token, VTY_NEWLINE);
-	vty_out(vty, " isup pass-through %d%s", bsc.isup_pass, VTY_NEWLINE);
+	vty_out(vty, " mtp dpc %d%s", bsc->dpc, VTY_NEWLINE);
+	vty_out(vty, " mtp opc %d%s", bsc->opc, VTY_NEWLINE);
+	vty_out(vty, " mtp sccp-opc %d%s", bsc->sccp_opc, VTY_NEWLINE);
+	vty_out(vty, " mtp ni %d%s", bsc->ni_ni, VTY_NEWLINE);
+	vty_out(vty, " mtp spare %d%s", bsc->ni_spare, VTY_NEWLINE);
+	vty_out(vty, " mtp sltm once %d%s", bsc->once, VTY_NEWLINE);
+	if (bsc->udp_ip)
+		vty_out(vty, " udp dest ip %s%s", bsc->udp_ip, VTY_NEWLINE);
+	vty_out(vty, " udp dest port %d%s", bsc->udp_port, VTY_NEWLINE);
+	vty_out(vty, " udp src port %d%s", bsc->src_port, VTY_NEWLINE);
+	vty_out(vty, " udp reset %d%s", bsc->udp_reset_timeout, VTY_NEWLINE);
+	vty_out(vty, " udp number-links %d%s", bsc->udp_nr_links, VTY_NEWLINE);
+	vty_out(vty, " msc ip %s%s", bsc->msc_forward.msc_address, VTY_NEWLINE);
+	vty_out(vty, " msc ip-dscp %d%s", bsc->msc_forward.msc_ip_dscp, VTY_NEWLINE);
+	vty_out(vty, " msc token %s%s", bsc->msc_forward.token, VTY_NEWLINE);
+	vty_out(vty, " isup pass-through %d%s", bsc->isup_pass, VTY_NEWLINE);
 
 	return CMD_SUCCESS;
 }
@@ -96,7 +96,7 @@
       "mtp dpc DPC_NR",
       "Set the DPC to be used.")
 {
-	bsc.dpc = atoi(argv[0]);
+	bsc->dpc = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -104,7 +104,7 @@
       "mtp opc OPC_NR",
       "Set the OPC to be used.")
 {
-	bsc.opc = atoi(argv[0]);
+	bsc->opc = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -112,7 +112,7 @@
       "mtp sccp-opc OPC_NR",
       "Set the SCCP OPC to be used.")
 {
-	bsc.sccp_opc = atoi(argv[0]);
+	bsc->sccp_opc = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -120,7 +120,7 @@
       "mtp ni NR",
       "Set the MTP NI to be used.\n" "NR")
 {
-	bsc.ni_ni = atoi(argv[0]);
+	bsc->ni_ni = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -128,7 +128,7 @@
       "mtp spare NR",
       "Set the MTP Spare to be used.\n" "NR")
 {
-	bsc.ni_spare = atoi(argv[0]);
+	bsc->ni_spare = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -147,7 +147,7 @@
 	}
 
 	addr = (struct in_addr *) hosts->h_addr_list[0];
-	bsc.udp_ip = talloc_strdup(NULL, inet_ntoa(*addr));
+	bsc->udp_ip = talloc_strdup(NULL, inet_ntoa(*addr));
 	return CMD_SUCCESS;
 }
 
@@ -155,7 +155,7 @@
       "udp dest port PORT_NR",
       "If UDP mode is used specify the UDP dest port")
 {
-	bsc.udp_port = atoi(argv[0]);
+	bsc->udp_port = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -163,7 +163,7 @@
       "udp src port PORT_NR",
       "Set the UDP source port to be used.")
 {
-	bsc.src_port = atoi(argv[0]);
+	bsc->src_port = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -171,7 +171,7 @@
       "udp reset TIMEOUT",
       "Set the timeout to take the link down")
 {
-	bsc.udp_reset_timeout = atoi(argv[0]);
+	bsc->udp_reset_timeout = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -179,7 +179,7 @@
       "udp number-links <1-32>",
       "Set the number of links to use\n")
 {
-	bsc.udp_nr_links = atoi(argv[0]);
+	bsc->udp_nr_links = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -187,7 +187,7 @@
       "mtp sltm once (0|1)",
       "Send SLTMs until the link is established.")
 {
-	bsc.once = !!atoi(argv[0]);
+	bsc->once = !!atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -206,7 +206,7 @@
 
 	addr = (struct in_addr *) hosts->h_addr_list[0];
 
-	bsc.msc_forward.msc_address = talloc_strdup(NULL, inet_ntoa(*addr));
+	bsc->msc_forward.msc_address = talloc_strdup(NULL, inet_ntoa(*addr));
 	return CMD_SUCCESS;
 }
 
@@ -215,7 +215,7 @@
       "Set the IP DSCP on the A-link\n"
       "Set the DSCP in IP packets to the MSC")
 {
-	bsc.msc_forward.msc_ip_dscp = atoi(argv[0]);
+	bsc->msc_forward.msc_ip_dscp = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -228,7 +228,7 @@
       "msc token TOKEN",
       "Set the Token to be used for the MSC")
 {
-	bsc.msc_forward.token = talloc_strdup(NULL, argv[0]);
+	bsc->msc_forward.token = talloc_strdup(NULL, argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -236,7 +236,7 @@
       "timeout ping NR",
       "Set the PING interval. Negative to disable it")
 {
-	bsc.msc_forward.ping_time = atoi(argv[0]);
+	bsc->msc_forward.ping_time = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -244,7 +244,7 @@
       "timeout pong NR",
       "Set the PING interval. Negative to disable it")
 {
-	bsc.msc_forward.pong_time = atoi(argv[0]);
+	bsc->msc_forward.pong_time = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -252,7 +252,7 @@
       "timeout msc NR",
       "Set the MSC connect timeout")
 {
-	bsc.msc_forward.msc_time = atoi(argv[0]);
+	bsc->msc_forward.msc_time = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -264,10 +264,10 @@
 {
 	struct mtp_link_set *set;
 
-	bsc.isup_pass = atoi(argv[0]);
+	bsc->isup_pass = atoi(argv[0]);
 
-	llist_for_each_entry(set, &bsc.links, entry)
-		set->pass_all_isup = bsc.isup_pass;
+	llist_for_each_entry(set, &bsc->links, entry)
+		set->pass_all_isup = bsc->isup_pass;
 
 	return CMD_SUCCESS;
 }
@@ -291,7 +291,7 @@
 {
 	struct mtp_link_set *set;
 
-	llist_for_each_entry(set, &bsc.links, entry)
+	llist_for_each_entry(set, &bsc->links, entry)
 		dump_stats(vty, set);
 
 	return CMD_SUCCESS;
@@ -330,7 +330,7 @@
 {
 	struct mtp_link_set *set;
 
-	llist_for_each_entry(set, &bsc.links, entry)
+	llist_for_each_entry(set, &bsc->links, entry)
 		dump_state(vty, set);
 	return CMD_SUCCESS;
 }
@@ -340,8 +340,8 @@
       SHOW_STR "Display the status of the MSC\n")
 {
 	vty_out(vty, "MSC link is %s and had %s.%s",
-		bsc.msc_forward.msc_link_down == 0 ? "up" : "down",
-		bsc.msc_forward.first_contact == 1 ? "no contact" : "contact",
+		bsc->msc_forward.msc_link_down == 0 ? "up" : "down",
+		bsc->msc_forward.first_contact == 1 ? "no contact" : "contact",
 		VTY_NEWLINE);
 	return CMD_SUCCESS;
 }
@@ -365,7 +365,7 @@
 	struct mtp_link_set *set = NULL;
 	int i;
 
-	set = find_link_set(&bsc.links, argv[0]);
+	set = find_link_set(&bsc->links, argv[0]);
 
 	if (!set) {
 		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
@@ -392,7 +392,7 @@
 {
 	struct mtp_link_set *set = NULL;
 
-	set = find_link_set(&bsc.links, argv[0]);
+	set = find_link_set(&bsc->links, argv[0]);
 
 	if (!set) {
 		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
@@ -400,7 +400,7 @@
 	}
 
 
-	if (set->pcap_fd >= 0 && bsc.pcap_fd != set->pcap_fd)
+	if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd)
 		close(set->pcap_fd);
 	set->pcap_fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT,
 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
@@ -420,14 +420,14 @@
 {
 	struct mtp_link_set *set = NULL;
 
-	set = find_link_set(&bsc.links, argv[0]);
+	set = find_link_set(&bsc->links, argv[0]);
 
 	if (!set) {
 		vty_out(vty, "Failed to find linkset.%s", VTY_NEWLINE);
 		return CMD_WARNING;
 	}
 
-	if (set->pcap_fd >= 0 && bsc.pcap_fd != set->pcap_fd)
+	if (set->pcap_fd >= 0 && bsc->pcap_fd != set->pcap_fd)
 		close(set->pcap_fd);
 	set->pcap_fd = -1;
 	return CMD_SUCCESS;
@@ -436,7 +436,7 @@
 #define FIND_LINK(vty, type, nr) ({						\
 	struct mtp_link_set *set = NULL;					\
 	struct mtp_link *link = NULL, *tmp;					\
-	set = find_link_set(&bsc.links, type);					\
+	set = find_link_set(&bsc->links, type);					\
 	if (!set) {								\
 		vty_out(vty, "Unknown linkset %s.%s", type, VTY_NEWLINE);	\
 		return CMD_WARNING;						\
@@ -488,7 +488,7 @@
       "allow-inject (0|1)",
       "Allow to inject messages\n" "Disable\n" "Enable\n")
 {
-	bsc.allow_inject = atoi(argv[0]);
+	bsc->allow_inject = atoi(argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -534,7 +534,7 @@
 	install_element_ve(&show_linksets_cmd);
 	install_element_ve(&show_slc_cmd);
 
-	if (bsc.app != APP_STP) {
+	if (bsc->app != APP_STP) {
 		install_element_ve(&show_msc_cmd);
 	}
 }