mgcp: Configure the HSCOMM routing based on the information we have

Configure the routing of audio ports if mgcp_mgw is configured
to do this. This allows to have multiple trunks, make virtual
ports go to a specific trunk as well.
diff --git a/src/Makefile.am b/src/Makefile.am
index 9670904..092a183 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,10 +4,10 @@
 
 sbin_PROGRAMS = cellmgr_ng osmo_stp mgcp_mgw
 
-mgcp_mgw_SOURCES = mgcp_ss7.c mgcp_ss7_vty.c thread.c debug.c \
+mgcp_mgw_SOURCES = mgcp_ss7.c mgcp_ss7_vty.c mgcp_snmp.c thread.c debug.c \
 		   mgcp/mgcp_protocol.c mgcp/mgcp_network.c mgcp/mgcp_vty.c
 mgcp_mgw_LDADD = $(LAFORGE_LIBS) $(NEXUSWARE_C7_LIBS) $(NEXUSWARE_UNIPORTE_LIBS) \
-		 $(LIBOSMOVTY_LIBS) $(LIBOSMOCORE_LIBS) -lpthread
+		 $(LIBOSMOVTY_LIBS) $(LIBOSMOCORE_LIBS) -lpthread -lnetsnmp -lcrypto
 
 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 \
diff --git a/src/mgcp_snmp.c b/src/mgcp_snmp.c
index 0a2d844..36cdea9 100644
--- a/src/mgcp_snmp.c
+++ b/src/mgcp_snmp.c
@@ -95,7 +95,7 @@
 int mgcp_snmp_connect(int port, int trunk, int timeslot)
 {
  	int status;
-	netsnmp_pdu *response;
+	netsnmp_pdu *response = NULL;
 	netsnmp_pdu *pdu;
 	int _rx_port, _tx_port;
 	char tx_port[10];
@@ -151,11 +151,18 @@
 	status = snmp_synch_response(g_ss, pdu, &response);
 	if (status == STAT_ERROR) {
 		snmp_sess_perror("set failed", g_ss);
+		goto failure;
 	} else if (status == STAT_TIMEOUT) {
 		fprintf(stderr, "Timeout for SNMP.\n");
+		goto failure;
  	}
  
 	if (response)
 		snmp_free_pdu(response);
 	return 0;
+
+failure:
+	if (response)
+		snmp_free_pdu(response);
+	return -1;
 }
diff --git a/src/mgcp_ss7.c b/src/mgcp_ss7.c
index 2061943..f7b2ff4 100644
--- a/src/mgcp_ss7.c
+++ b/src/mgcp_ss7.c
@@ -687,6 +687,12 @@
 		return NULL;
 	}
 
+	if (cfg->configure_trunks && mgcp_snmp_init() != 0) {
+		LOGP(DMGCP, LOGL_ERROR, "Failed to initialize SNMP.\n");
+		talloc_free(conf);
+		return NULL;
+	}
+
 	/* Now do the init of the trunks */
 	dsp_resource = 0;
 	for (i = 1; i < cfg->trunk.number_endpoints; ++i) {
@@ -696,6 +702,20 @@
 			continue;
 
 		dsp_resource += 1;
+
+		if (cfg->configure_trunks) {
+			int res;
+
+			res = mgcp_snmp_connect(dsp_resource,
+						cfg->trunk.target_trunk_start + multiplex,
+						timeslot);
+
+			if (res != 0) {
+				LOGP(DMGCP, LOGL_ERROR, "Failed to configure virtual trunk.\n");
+				talloc_free(conf);
+				return NULL;
+			}
+		}
 	}
 
 	llist_for_each_entry(trunk, &cfg->trunks, entry) {
@@ -708,6 +728,21 @@
 				continue;
 
 			dsp_resource += 1;
+
+			if (cfg->configure_trunks) {
+				int res;
+
+				res = mgcp_snmp_connect(dsp_resource,
+							trunk->trunk_nr + multiplex,
+							timeslot);
+
+				if (res != 0) {
+					LOGP(DMGCP, LOGL_ERROR,
+					     "Failed to configure virtual trunk.\n");
+					talloc_free(conf);
+					return NULL;
+				}
+			}
 		}
 	}