<p>dexter has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/10307">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">mgcp_client: increment local port number when port is in use<br><br>The IETF has designated port 2727 for call agents (clients). This<br>works as long as only one call agent is running. As soon as two<br>call agents (e.g. osmo-bsc and osmo-msc) run on the same machine.<br>The port numbers will collide.<br><br>To avoid such a situation we will first try the IETF port and if<br>we fail to use it we increment the port number until we found a<br>usable port. However, we should only do this if the user has not<br>configured a non standard port. (The rationale behind this is that<br>if there is a non standard port configured the choice must have<br>been made conciously by the user and therefor we should fail hard<br>so that the user gets aware of the problem.)<br><br>Change-Id: Iaa5f41fdb43ec6bf4feaefa174fd82622e37d4d0<br>Related: OS#2874<br>---<br>M src/libosmo-mgcp-client/mgcp_client.c<br>1 file changed, 37 insertions(+), 4 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/07/10307/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c</span><br><span>index 00b52f8..3663163 100644</span><br><span>--- a/src/libosmo-mgcp-client/mgcp_client.c</span><br><span>+++ b/src/libosmo-mgcp-client/mgcp_client.c</span><br><span>@@ -779,6 +779,42 @@</span><br><span>    return mgcp;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int init_socket(struct mgcp_client *mgcp)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+       struct osmo_wqueue *wq;</span><br><span style="color: hsl(120, 100%, 40%);">+       int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      wq = &mgcp->wq;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      for (i = 0; i < 100; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+              /* Initalize socket with the currently configured port</span><br><span style="color: hsl(120, 100%, 40%);">+                 * number */</span><br><span style="color: hsl(120, 100%, 40%);">+          rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,</span><br><span style="color: hsl(120, 100%, 40%);">+                                        mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,</span><br><span style="color: hsl(120, 100%, 40%);">+                                         OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);</span><br><span style="color: hsl(120, 100%, 40%);">+             if (rc > 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                        return rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /* If there is a different port than the default port</span><br><span style="color: hsl(120, 100%, 40%);">+          * configured then we assume that the user has choosen</span><br><span style="color: hsl(120, 100%, 40%);">+                 * that port conciously and we will not try to resolve</span><br><span style="color: hsl(120, 100%, 40%);">+                 * this by silently choosing a different port. */</span><br><span style="color: hsl(120, 100%, 40%);">+             if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT)</span><br><span style="color: hsl(120, 100%, 40%);">+                     return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Choose a new port number to try next */</span><br><span style="color: hsl(120, 100%, 40%);">+            LOGP(DLMGCP, LOGL_NOTICE,</span><br><span style="color: hsl(120, 100%, 40%);">+                  "MGCPGW faild to bind to port %u, retrying with port %u -- check configuration!\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                 mgcp->actual.local_port, mgcp->actual.local_port + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+             mgcp->actual.local_port++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   LOGP(DLMGCP, LOGL_FATAL, "MGCPGW faild to find a port to bind on %i times.\n", i);</span><br><span style="color: hsl(120, 100%, 40%);">+  return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! Initalize client connection (opens socket only, no request is sent yet)</span><br><span>  *  \param[in,out] mgcp MGCP client descriptor.</span><br><span>  *  \returns 0 on success, -EINVAL on error. */</span><br><span>@@ -795,10 +831,7 @@</span><br><span> </span><br><span>         wq = &mgcp->wq;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,</span><br><span style="color: hsl(0, 100%, 40%);">-                              mgcp->actual.local_addr, mgcp->actual.local_port,</span><br><span style="color: hsl(0, 100%, 40%);">-                                 mgcp->actual.remote_addr, mgcp->actual.remote_port,</span><br><span style="color: hsl(0, 100%, 40%);">-                               OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = init_socket(mgcp);</span><br><span>      if (rc < 0) {</span><br><span>             LOGP(DLMGCP, LOGL_FATAL,</span><br><span>                  "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/10307">change 10307</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/10307"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-mgw </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Iaa5f41fdb43ec6bf4feaefa174fd82622e37d4d0 </div>
<div style="display:none"> Gerrit-Change-Number: 10307 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: dexter <pmaier@sysmocom.de> </div>