Change in osmo-mgw[master]: mgcp_client: refactor function init_socket

laforge gerrit-no-reply at lists.osmocom.org
Tue Aug 17 12:12:27 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-mgw/+/25125 )

Change subject: mgcp_client: refactor function init_socket
......................................................................

mgcp_client: refactor function init_socket

The function init_socket has an arbitrary retry count when opening the
socket. After each retry the local port is incremented by one. The
intention behind this is to find a useable local port in case the
configured port is used by another process.

The maximum number of retrys is hardcoded. The upcomming MGW pooling
patch requires to set the maximum retry count.

Change-Id: Ifd65511daa92fbe610f52da1c4c3b6a7c761d890
Related: SYS#5091
---
M src/libosmo-mgcp-client/mgcp_client.c
1 file changed, 23 insertions(+), 17 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  osmith: Looks good to me, but someone else must approve



diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index 225b1ee..9b6c133 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -796,40 +796,46 @@
 	return mgcp;
 }
 
-static int init_socket(struct mgcp_client *mgcp)
+static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)
 {
 	int rc;
 	struct osmo_wqueue *wq;
-	int i;
+	unsigned int i;
 
 	wq = &mgcp->wq;
 
-	for (i = 0; i < 100; i++) {
+	for (i = 0; i < retry_n_ports + 1; i++) {
 
-		/* Initalize socket with the currently configured port
-		 * number */
+		/* Initialize socket with the currently configured port number */
 		rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
 					 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
 					 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
 		if (rc > 0)
 			return rc;
 
-		/* If there is a different port than the default port
-		 * configured then we assume that the user has choosen
-		 * that port conciously and we will not try to resolve
-		 * this by silently choosing a different port. */
+		/* If there is a different port than the default port configured then we assume that the user has
+		 * chosen that port conciously and we will not try to resolve this by silently choosing a different
+		 * port. */
 		if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
 			return -EINVAL;
 
-		/* Choose a new port number to try next */
-		LOGP(DLMGCP, LOGL_NOTICE,
-		     "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
-		     mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
-		     mgcp->actual.local_port + 1);
-		mgcp->actual.local_port++;
+		if (i == retry_n_ports) {
+			/* Last try failed */
+			LOGP(DLMGCP, LOGL_NOTICE, "MGCPGW failed to bind to %s:%d -- check configuration!\n",
+			     mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);
+			if (retry_n_ports == 0)
+				return -EINVAL;
+		} else {
+			/* Choose a new port number to try next */
+			LOGP(DLMGCP, LOGL_NOTICE,
+			     "MGCPGW failed to bind to %s:%d, retrying with port %d -- check configuration!\n",
+			     mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
+			     mgcp->actual.local_port + 1);
+			mgcp->actual.local_port++;
+		}
 	}
 
-	LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
+	LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %u times -- check configuration!\n", i);
 	return -EINVAL;
 }
 
@@ -889,7 +895,7 @@
 
 	osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
 
-	rc = init_socket(mgcp);
+	rc = init_socket(mgcp, 99);
 	if (rc < 0) {
 		LOGP(DLMGCP, LOGL_FATAL,
 		     "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-mgw/+/25125
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-mgw
Gerrit-Branch: master
Gerrit-Change-Id: Ifd65511daa92fbe610f52da1c4c3b6a7c761d890
Gerrit-Change-Number: 25125
Gerrit-PatchSet: 6
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <dwillmann at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210817/8e3b2593/attachment.htm>


More information about the gerrit-log mailing list