mtp: Make the link_data be a child of the link_set

Change the order of the link and linkset. The link will be
below the linkset. This change should make it more easy to
introduce multiple linksets.
diff --git a/include/bsc_data.h b/include/bsc_data.h
index 76e6b44..d734177 100644
--- a/include/bsc_data.h
+++ b/include/bsc_data.h
@@ -96,7 +96,9 @@
 
 	int setup;
 
-	struct link_data link;
+	int pcap_fd;
+	int udp_reset_timeout;
+	struct mtp_link_set *link_set;
 
 	const char *token;
 
diff --git a/include/mtp_data.h b/include/mtp_data.h
index 5afcb92..f2aeaec 100644
--- a/include/mtp_data.h
+++ b/include/mtp_data.h
@@ -25,6 +25,7 @@
 #include <osmocore/utils.h>
 
 struct bsc_data;
+struct link_data;
 
 /* MTP Level3 timers */
 
@@ -63,6 +64,8 @@
 
 	struct timer_list delay_timer;
 
+	struct link_data *link;
+
 	/* custom data */
 	struct bsc_data *bsc;
 };
diff --git a/src/links.c b/src/links.c
index 233f323..1102ee6 100644
--- a/src/links.c
+++ b/src/links.c
@@ -25,6 +25,8 @@
 #include <mtp_data.h>
 #include <snmp_mtp.h>
 
+#include <osmocore/talloc.h>
+
 extern struct bsc_data bsc;
 
 void mtp_link_down(struct link_data *link)
@@ -42,15 +44,15 @@
 {
 }
 
-void mtp_link_set_submit(struct mtp_link_set *link, struct msgb *msg)
+void mtp_link_set_submit(struct mtp_link_set *set, struct msgb *msg)
 {
-	bsc.link.write(&bsc.link, msg);
+	set->link->write(set->link, msg);
 }
 
-void mtp_link_set_restart(struct mtp_link_set *link)
+void mtp_link_set_restart(struct mtp_link_set *set)
 {
 	LOGP(DINP, LOGL_ERROR, "Need to restart the SS7 link.\n");
-	bsc.link.reset(&bsc.link);
+	set->link->reset(set->link);
 }
 
 static void start_rest(void *start)
@@ -62,21 +64,26 @@
 		exit(3);
 	}
 
-	bsc.link.start(&bsc.link);
+	bsc.link_set->link->start(bsc.link_set->link);
 }
 
 int link_init(struct bsc_data *bsc)
 {
-	bsc->link.the_link = mtp_link_set_alloc();
-	bsc->link.the_link->dpc = bsc->dpc;
-	bsc->link.the_link->opc = bsc->opc;
-	bsc->link.the_link->sccp_opc = bsc->sccp_opc > -1 ? bsc->sccp_opc : bsc->opc;
-	bsc->link.the_link->sltm_once = bsc->once;
-	bsc->link.the_link->ni = bsc->ni_ni;
-	bsc->link.the_link->spare = bsc->ni_spare;
-	bsc->link.the_link->bsc = bsc;
-	bsc->link.bsc = bsc;
-	bsc->link.udp.link_index = 1;
+	bsc->link_set = mtp_link_set_alloc();
+	bsc->link_set->dpc = bsc->dpc;
+	bsc->link_set->opc = bsc->opc;
+	bsc->link_set->sccp_opc = bsc->sccp_opc > -1 ? bsc->sccp_opc : bsc->opc;
+	bsc->link_set->sltm_once = bsc->once;
+	bsc->link_set->ni = bsc->ni_ni;
+	bsc->link_set->spare = bsc->ni_spare;
+	bsc->link_set->bsc = bsc;
+
+	bsc->link_set->link = talloc_zero(bsc->link_set, struct link_data);
+	bsc->link_set->link->bsc = bsc;
+	bsc->link_set->link->udp.link_index = 1;
+	bsc->link_set->link->pcap_fd = bsc->pcap_fd;
+	bsc->link_set->link->udp.reset_timeout = bsc->udp_reset_timeout;
+	bsc->link_set->link->the_link = bsc->link_set;
 
 	if (!bsc->src_port) {
 		LOGP(DINP, LOGL_ERROR, "You need to set a UDP address.\n");
@@ -86,12 +93,12 @@
 	LOGP(DINP, LOGL_NOTICE, "Using UDP MTP mode.\n");
 
 	/* setup SNMP first, it is blocking */
-	bsc->link.udp.session = snmp_mtp_session_create(bsc->udp_ip);
-	if (!bsc->link.udp.session)
+	bsc->link_set->link->udp.session = snmp_mtp_session_create(bsc->udp_ip);
+	if (!bsc->link_set->link->udp.session)
 		return -1;
 
 	/* now connect to the transport */
-	if (link_udp_init(&bsc->link, bsc->src_port, bsc->udp_ip, bsc->udp_port) != 0)
+	if (link_udp_init(bsc->link_set->link, bsc->src_port, bsc->udp_ip, bsc->udp_port) != 0)
 		return -1;
 
 	/*
@@ -100,11 +107,11 @@
 	 * SLTM and it begins a reset. Then we will take it up
 	 * again and do the usual business.
 	 */
-	snmp_mtp_deactivate(bsc->link.udp.session,
-			    bsc->link.udp.link_index);
+	snmp_mtp_deactivate(bsc->link_set->link->udp.session,
+			    bsc->link_set->link->udp.link_index);
 	bsc->start_timer.cb = start_rest;
 	bsc->start_timer.data = &bsc;
-	bsc_schedule_timer(&bsc->start_timer, bsc->link.udp.reset_timeout, 0);
+	bsc_schedule_timer(&bsc->start_timer, bsc->link_set->link->udp.reset_timeout, 0);
 	LOGP(DMSC, LOGL_NOTICE, "Making sure SLTM will timeout.\n");
 
 	return 0;
diff --git a/src/main.c b/src/main.c
index 7a66cd8..a8edb97 100644
--- a/src/main.c
+++ b/src/main.c
@@ -194,7 +194,7 @@
 		free_con(con);
 	}
 
-	bsc->link.clear_queue(&bsc->link);
+	bsc->link_set->link->clear_queue(bsc->link_set->link);
 }
 
 void bsc_resources_released(struct bsc_data *bsc)
@@ -210,9 +210,9 @@
 	/* no reset */
 	if (bsc->reset_count > 0) {
 		LOGP(DINP, LOGL_ERROR, "The BSC did not answer the GSM08.08 reset. Restart MTP\n");
-		mtp_link_set_stop(bsc->link.the_link);
+		mtp_link_set_stop(bsc->link_set);
 		clear_connections(bsc);
-		bsc->link.reset(&bsc->link);
+		bsc->link_set->link->reset(bsc->link_set->link);
 		bsc_resources_released(bsc);
 		return;
 	}
@@ -224,7 +224,7 @@
 	}
 
 	++bsc->reset_count;
-	mtp_link_set_submit_sccp_data(bsc->link.the_link, -1, msg->l2h, msgb_l2len(msg));
+	mtp_link_set_submit_sccp_data(bsc->link_set, -1, msg->l2h, msgb_l2len(msg));
 	msgb_free(msg);
 	bsc_schedule_timer(&bsc->reset_timeout, 20, 0);
 }
@@ -269,7 +269,7 @@
 			continue;
 
 		/* wait for the clear commands */
-		mtp_link_set_submit_sccp_data(bsc->link.the_link, con->sls, msg->l2h, msgb_l2len(msg));
+		mtp_link_set_submit_sccp_data(bsc->link_set, con->sls, msg->l2h, msgb_l2len(msg));
 		msgb_free(msg);
 	}
 
@@ -319,7 +319,7 @@
 	if (!msg)
 		return;
 
-	mtp_link_set_submit_sccp_data(bsc.link.the_link, sls, msg->l2h, msgb_l2len(msg));
+	mtp_link_set_submit_sccp_data(bsc.link_set, sls, msg->l2h, msgb_l2len(msg));
 	msgb_free(msg);
 }
 
@@ -500,7 +500,7 @@
 	++con->rls_tries;
 	LOGP(DINP, LOGL_DEBUG, "Sending RLSD for 0x%x the %d time.\n",
 	     sccp_src_ref_to_int(&con->src_ref), con->rls_tries);
-	mtp_link_set_submit_sccp_data(bsc.link.the_link, con->sls, rlsd->l2h, msgb_l2len(rlsd));
+	mtp_link_set_submit_sccp_data(bsc.link_set, con->sls, rlsd->l2h, msgb_l2len(rlsd));
 	msgb_free(rlsd);
 }
 
@@ -547,7 +547,7 @@
 	printf("Terminating.\n");
 	handled = 1;
 	if (bsc.setup)
-		bsc.link.shutdown(&bsc.link);
+		bsc.link_set->link->shutdown(bsc.link_set->link);
 	exit(0);
 
 out:
@@ -593,14 +593,14 @@
 			print_help();
 			exit(0);
 		case 'p':
-			if (bsc.link.pcap_fd >= 0)
-				close(bsc.link.pcap_fd);
-			bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
-			if (bsc.link.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.link.pcap_fd);
+			mtp_pcap_write_header(bsc.pcap_fd);
 			break;
 		case 'c':
 			config = optarg;
@@ -650,8 +650,8 @@
 
 	bsc.setup = 0;
 	bsc.msc_address = "127.0.0.1";
-	bsc.link.pcap_fd = -1;
-	bsc.link.udp.reset_timeout = 180;
+	bsc.pcap_fd = -1;
+	bsc.udp_reset_timeout = 180;
 	bsc.ping_time = 20;
 	bsc.pong_time = 5;
 	bsc.msc_time = 20;
diff --git a/src/main_udt.c b/src/main_udt.c
index 40fe616..b8d754e 100644
--- a/src/main_udt.c
+++ b/src/main_udt.c
@@ -104,7 +104,7 @@
 	printf("Terminating.\n");
 	handled = 1;
 	if (bsc.setup)
-		bsc.link.shutdown(&bsc.link);
+		bsc.link_set->link->shutdown(bsc.link_set->link);
 	exit(0);
 
 out:
@@ -150,14 +150,14 @@
 			print_help();
 			exit(0);
 		case 'p':
-			if (bsc.link.pcap_fd >= 0)
-				close(bsc.link.pcap_fd);
-			bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
-			if (bsc.link.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.link.pcap_fd);
+			mtp_pcap_write_header(bsc.pcap_fd);
 			break;
 		case 'c':
 			config = optarg;
@@ -207,8 +207,8 @@
 
 	bsc.setup = 0;
 	bsc.msc_address = "127.0.0.1";
-	bsc.link.pcap_fd = -1;
-	bsc.link.udp.reset_timeout = 180;
+	bsc.pcap_fd = -1;
+	bsc.udp_reset_timeout = 180;
 	bsc.ping_time = 20;
 	bsc.pong_time = 5;
 	bsc.msc_time = 20;
diff --git a/src/msc_conn.c b/src/msc_conn.c
index 89b39c7..bc05744 100644
--- a/src/msc_conn.c
+++ b/src/msc_conn.c
@@ -146,7 +146,7 @@
 	hh = (struct ipaccess_head *) msg->data;
 	ipaccess_rcvmsg_base(msg, bfd);
 
-	link = bsc->link.the_link;
+	link = bsc->link_set;
 
 	/* initialize the networking. This includes sending a GSM08.08 message */
 	if (hh->proto == IPAC_PROTO_IPACCESS) {
diff --git a/src/vty_interface.c b/src/vty_interface.c
index 6ddf549..0cf5929 100644
--- a/src/vty_interface.c
+++ b/src/vty_interface.c
@@ -72,7 +72,7 @@
 		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.link.udp.reset_timeout, VTY_NEWLINE);
+	vty_out(vty, " udp reset %d%s", bsc.udp_reset_timeout, VTY_NEWLINE);
 	vty_out(vty, " msc ip %s%s", bsc.msc_address, VTY_NEWLINE);
 	vty_out(vty, " msc ip-dscp %d%s", bsc.msc_ip_dscp, VTY_NEWLINE);
 	vty_out(vty, " msc token %s%s", bsc.token, VTY_NEWLINE);
@@ -166,7 +166,7 @@
       "udp reset TIMEOUT",
       "Set the timeout to take the link down")
 {
-	bsc.link.udp.reset_timeout = atoi(argv[0]);
+	bsc.udp_reset_timeout = atoi(argv[0]);
 	return CMD_SUCCESS;
 }