libosmo-mgcp-client: extend the mgcp_client for MGW pooling

At the moment the MGCP Client only supports one MGW per application.
Depending on the requirements of the application one MGW might not offer
the performance needed. Lets add support for an MGCP Client pool that is
backward compatible to existing applications.

Change-Id: Icaaba0e470e916eefddfee750b83f5f65291a6b0
Related: SYS#5091
diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index 9b6c133..2bed90f 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -875,8 +875,9 @@
 
 /*! Initialize client connection (opens socket)
  *  \param[in,out] mgcp MGCP client descriptor.
+ *  \param[in] retry_n_ports number of consecutive local ports that should be used to retry on failure.
  *  \returns 0 on success, -EINVAL on error. */
-int mgcp_client_connect(struct mgcp_client *mgcp)
+int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
 {
 	struct osmo_wqueue *wq;
 	int rc;
@@ -895,7 +896,7 @@
 
 	osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
 
-	rc = init_socket(mgcp, 99);
+	rc = init_socket(mgcp, retry_n_ports);
 	if (rc < 0) {
 		LOGP(DLMGCP, LOGL_FATAL,
 		     "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
@@ -921,6 +922,35 @@
 	return rc;
 }
 
+/*! Initialize client connection (opens socket)
+ *  \param[in,out] mgcp MGCP client descriptor.
+ *  \returns 0 on success, -EINVAL on error. */
+int mgcp_client_connect(struct mgcp_client *mgcp)
+{
+	return mgcp_client_connect2(mgcp, 99);
+}
+
+/*! Terminate client connection
+ *  \param[in,out] mgcp MGCP client descriptor.
+ *  \returns 0 on success, -EINVAL on error. */
+void mgcp_client_disconnect(struct mgcp_client *mgcp)
+{
+	struct osmo_wqueue *wq;
+
+	if (!mgcp) {
+		LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
+		return;
+	}
+
+	wq = &mgcp->wq;
+	osmo_wqueue_clear(wq);
+	LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
+	close(wq->bfd.fd);
+	wq->bfd.fd = -1;
+	if (osmo_fd_is_registered(&wq->bfd))
+		osmo_fd_unregister(&wq->bfd);
+}
+
 /*! Get the IP-Aaddress of the associated MGW as string.
  *  \param[in] mgcp MGCP client descriptor.
  *  \returns a pointer to the address string. */