Change in osmo-mgw[master]: mgcp_client: increment local port number when port is in use

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

dexter gerrit-no-reply at lists.osmocom.org
Thu Aug 2 10:53:22 UTC 2018


dexter has uploaded this change for review. ( https://gerrit.osmocom.org/10307


Change subject: mgcp_client: increment local port number when port is in use
......................................................................

mgcp_client: increment local port number when port is in use

The IETF has designated port 2727 for call agents (clients). This
works as long as only one call agent is running. As soon as two
call agents (e.g. osmo-bsc and osmo-msc) run on the same machine.
The port numbers will collide.

To avoid such a situation we will first try the IETF port and if
we fail to use it we increment the port number until we found a
usable port. However, we should only do this if the user has not
configured a non standard port. (The rationale behind this is that
if there is a non standard port configured the choice must have
been made conciously by the user and therefor we should fail hard
so that the user gets aware of the problem.)

Change-Id: Iaa5f41fdb43ec6bf4feaefa174fd82622e37d4d0
Related: OS#2874
---
M src/libosmo-mgcp-client/mgcp_client.c
1 file changed, 37 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/07/10307/1

diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index 00b52f8..3663163 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -779,6 +779,42 @@
 	return mgcp;
 }
 
+static int init_socket(struct mgcp_client *mgcp)
+{
+	int rc;
+	struct osmo_wqueue *wq;
+	int i;
+
+	wq = &mgcp->wq;
+
+	for (i = 0; i < 100; i++) {
+
+		/* Initalize socket with the currently configured port
+		 * number */
+		rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, 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 (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT)
+			return -EINVAL;
+
+		/* Choose a new port number to try next */
+		LOGP(DLMGCP, LOGL_NOTICE,
+		     "MGCPGW faild to bind to port %u, retrying with port %u -- check configuration!\n",
+		     mgcp->actual.local_port, mgcp->actual.local_port + 1);
+		mgcp->actual.local_port++;
+	}
+
+	LOGP(DLMGCP, LOGL_FATAL, "MGCPGW faild to find a port to bind on %i times.\n", i);
+	return -EINVAL;
+}
+
 /*! Initalize client connection (opens socket only, no request is sent yet)
  *  \param[in,out] mgcp MGCP client descriptor.
  *  \returns 0 on success, -EINVAL on error. */
@@ -795,10 +831,7 @@
 
 	wq = &mgcp->wq;
 
-	rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, 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);
+	rc = init_socket(mgcp);
 	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/10307
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-mgw
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaa5f41fdb43ec6bf4feaefa174fd82622e37d4d0
Gerrit-Change-Number: 10307
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180802/aec86e7b/attachment.htm>


More information about the gerrit-log mailing list