<p>dexter has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-mgw/+/25125">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">mgcp_client: refactor function init_socket<br><br>The function init_socket has an arbitrary retry count when opening the<br>socket. After each retry the local port is incremented by one. The<br>intention behind this is to find a useable local port in case the<br>configured port is used by another process.<br><br>The maximum number of retrys is hardcoded. The upcomming MGW pooling<br>patch requires to set the maximum retry count.<br><br>Change-Id: Ifd65511daa92fbe610f52da1c4c3b6a7c761d890<br>Related: SYS#5091<br>---<br>M src/libosmo-mgcp-client/mgcp_client.c<br>1 file changed, 18 insertions(+), 12 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/25/25125/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 66dac21..e5fe5f0 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>@@ -796,40 +796,46 @@</span><br><span>      return mgcp;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static int init_socket(struct mgcp_client *mgcp)</span><br><span style="color: hsl(120, 100%, 40%);">+static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)</span><br><span> {</span><br><span>     int rc;</span><br><span>      struct osmo_wqueue *wq;</span><br><span style="color: hsl(0, 100%, 40%);">- int i;</span><br><span style="color: hsl(120, 100%, 40%);">+        unsigned int i;</span><br><span> </span><br><span>  wq = &mgcp->wq;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      for (i = 0; i < 100; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+        for (i = 0; i < retry_n_ports + 1; i++) {</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                /* Initalize socket with the currently configured port</span><br><span style="color: hsl(0, 100%, 40%);">-           * number */</span><br><span style="color: hsl(120, 100%, 40%);">+          /* Initialize socket with the currently configured port number */</span><br><span>            rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,</span><br><span>                                     mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,</span><br><span>                                        OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);</span><br><span>            if (rc > 0)</span><br><span>                       return rc;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-          /* If there is a different port than the default port</span><br><span style="color: hsl(0, 100%, 40%);">-            * configured then we assume that the user has choosen</span><br><span style="color: hsl(0, 100%, 40%);">-           * that port conciously and we will not try to resolve</span><br><span style="color: hsl(0, 100%, 40%);">-           * this by silently choosing a different port. */</span><br><span style="color: hsl(120, 100%, 40%);">+             /* If there is a different port than the default port configured then we assume that the user has</span><br><span style="color: hsl(120, 100%, 40%);">+              * chosen that port conciously and we will not try to resolve this by silently choosing a different</span><br><span style="color: hsl(120, 100%, 40%);">+            * port. */</span><br><span>          if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)</span><br><span>                  return -EINVAL;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+           /* Last try failed */</span><br><span style="color: hsl(120, 100%, 40%);">+         if (i == retry_n_ports) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     LOGP(DLMGCP, LOGL_NOTICE, "MGCPGW failed to bind to %s:%d -- check configuration!\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                            mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);</span><br><span style="color: hsl(120, 100%, 40%);">+                        if (retry_n_ports == 0)</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>          /* Choose a new port number to try next */</span><br><span>           LOGP(DLMGCP, LOGL_NOTICE,</span><br><span style="color: hsl(0, 100%, 40%);">-                    "MGCPGW failed to bind to %s:%u, retrying with port %u\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                  "MGCPGW failed to bind to %s:%d, retrying with port %d -- check configuration!\n",</span><br><span>                 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,</span><br><span>                     mgcp->actual.local_port + 1);</span><br><span>                mgcp->actual.local_port++;</span><br><span>        }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %u times -- check configuration!\n", i);</span><br><span>         return -EINVAL;</span><br><span> }</span><br><span> </span><br><span>@@ -889,7 +895,7 @@</span><br><span> </span><br><span>   osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      rc = init_socket(mgcp);</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = init_socket(mgcp, 99);</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/c/osmo-mgw/+/25125">change 25125</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/c/osmo-mgw/+/25125"/><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-Change-Id: Ifd65511daa92fbe610f52da1c4c3b6a7c761d890 </div>
<div style="display:none"> Gerrit-Change-Number: 25125 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: dexter <pmaier@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>