This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 98dfaa3  dhcpc abnormal block The logic changes are as follows: 1、Loop 
sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES times    and exit if 
failure 2、Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times    
and exit if failure
98dfaa3 is described below

commit 98dfaa3eff532be7f5ef91f73d10ce9c35d226e5
Author: songlinzhang <[email protected]>
AuthorDate: Tue Sep 28 14:41:29 2021 +0800

    dhcpc abnormal block
    The logic changes are as follows:
    1、Loop sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES times
       and exit if failure
    2、Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times
       and exit if failure
    
    Signed-off-by: songlinzhang <[email protected]>
---
 netutils/dhcpc/dhcpc.c | 257 +++++++++++++++++++++++++------------------------
 1 file changed, 129 insertions(+), 128 deletions(-)

diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c
index 372ee23..008f16a 100644
--- a/netutils/dhcpc/dhcpc.c
+++ b/netutils/dhcpc/dhcpc.c
@@ -579,177 +579,178 @@ int dhcpc_request(FAR void *handle, FAR struct 
dhcpc_state *presult)
   oldaddr.s_addr = 0;
   netlib_get_ipv4addr(pdhcpc->interface, &oldaddr);
 
-  /* Loop until we receive the lease (or an error occurs) */
+  /* Set the IP address to INADDR_ANY. */
 
-  do
-    {
-      /* Set the IP address to INADDR_ANY. */
+  newaddr.s_addr = INADDR_ANY;
+  netlib_set_ipv4addr(pdhcpc->interface, &newaddr);
 
-      newaddr.s_addr = INADDR_ANY;
-      netlib_set_ipv4addr(pdhcpc->interface, &newaddr);
+  /* Loop sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES
+   * times
+   */
 
-      /* Loop sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES
-       * times
-       */
+  retries = 0;
 
-      retries = 0;
+  /* Loop sending DISCOVER until we receive an OFFER from a DHCP
+   * server.  We will lock on to the first OFFER and decline any
+   * subsequent offers (which will happen if there are more than one
+   * DHCP servers on the network.
+   */
 
-      /* Loop sending DISCOVER until we receive an OFFER from a DHCP
-       * server.  We will lock on to the first OFFER and decline any
-       * subsequent offers (which will happen if there are more than one
-       * DHCP servers on the network.
-       */
+  state = STATE_INITIAL;
+  do
+    {
+      /* Send the DISCOVER command */
 
-      state = STATE_INITIAL;
-      do
+      ninfo("Broadcast DISCOVER\n");
+      if (dhcpc_sendmsg(pdhcpc, presult, DHCPDISCOVER) < 0)
         {
-          /* Send the DISCOVER command */
-
-          ninfo("Broadcast DISCOVER\n");
-          if (dhcpc_sendmsg(pdhcpc, presult, DHCPDISCOVER) < 0)
-            {
-              return ERROR;
-            }
+          return ERROR;
+        }
 
-          retries++;
+      retries++;
 
-          /* Get the DHCPOFFER response */
+      /* Get the DHCPOFFER response */
 
-          result = recv(pdhcpc->sockfd, &pdhcpc->packet,
-                        sizeof(struct dhcp_msg), 0);
-          if (result >= 0)
+      result = recv(pdhcpc->sockfd, &pdhcpc->packet,
+                    sizeof(struct dhcp_msg), 0);
+      if (result >= 0)
+        {
+          msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
+          if (msgtype == DHCPOFFER)
             {
-              msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
-              if (msgtype == DHCPOFFER)
-                {
-                  /* Save the servid from the presult so that it is not
-                   * clobbered by a new OFFER.
-                   */
-
-                  ninfo("Received OFFER from %08" PRIx32 "\n",
-                        (uint32_t)ntohl(presult->serverid.s_addr));
-                  pdhcpc->ipaddr.s_addr   = presult->ipaddr.s_addr;
-                  pdhcpc->serverid.s_addr = presult->serverid.s_addr;
-
-                  /* Temporarily use the address offered by the server
-                   * and break out of the loop.
-                   */
-
-                  netlib_set_ipv4addr(pdhcpc->interface,
-                                      &presult->ipaddr);
-                  state = STATE_HAVE_OFFER;
-                }
-            }
+              /* Save the servid from the presult so that it is not
+               * clobbered by a new OFFER.
+               */
 
-          /* An error has occurred.  If this was a timeout error (meaning
-           * that nothing was received on this socket for a long period
-           * of time). Then loop and send the DISCOVER command again.
-           */
+              ninfo("Received OFFER from %08" PRIx32 "\n",
+                    (uint32_t)ntohl(presult->serverid.s_addr));
+              pdhcpc->ipaddr.s_addr   = presult->ipaddr.s_addr;
+              pdhcpc->serverid.s_addr = presult->serverid.s_addr;
 
-          else if (errno != EAGAIN)
-            {
-              /* An error other than a timeout was received -- error out */
+              /* Temporarily use the address offered by the server
+               * and break out of the loop.
+               */
 
-              return ERROR;
+              netlib_set_ipv4addr(pdhcpc->interface,
+                                  &presult->ipaddr);
+              state = STATE_HAVE_OFFER;
             }
         }
-      while (state == STATE_INITIAL &&
-             retries < CONFIG_NETUTILS_DHCPC_RETRIES);
 
-      /* If no DHCPOFFER recveived here, error out */
+      /* An error has occurred.  If this was a timeout error (meaning
+       * that nothing was received on this socket for a long period
+       * of time). Then loop and send the DISCOVER command again.
+       */
 
-      if (state == STATE_INITIAL)
+      else if (errno != EAGAIN)
         {
+          /* An error other than a timeout was received -- error out */
+
           return ERROR;
         }
+    }
+  while (state == STATE_INITIAL &&
+         retries < CONFIG_NETUTILS_DHCPC_RETRIES);
 
-      /* Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times
-       * (if there is no response)
-       */
-
-      retries = 0;
-      do
-        {
-          /* Send the REQUEST message to obtain the lease that was offered to
-           * us.
-           */
-
-          ninfo("Send REQUEST\n");
-          if (dhcpc_sendmsg(pdhcpc, presult, DHCPREQUEST) < 0)
-            {
-              return ERROR;
-            }
+  /* If no DHCPOFFER recveived here, error out */
 
-          retries++;
+  if (state == STATE_INITIAL)
+    {
+      return ERROR;
+    }
 
-          /* Get the ACK/NAK response to the REQUEST (or timeout) */
+  /* Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times
+   * (if there is no response)
+   */
 
-          result = recv(pdhcpc->sockfd, &pdhcpc->packet,
-                        sizeof(struct dhcp_msg), 0);
-          if (result >= 0)
-            {
-              /* Parse the response */
+  retries = 0;
+  do
+    {
+      /* Send the REQUEST message to obtain the lease that was offered to
+       * us.
+       */
 
-              msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
+      ninfo("Send REQUEST\n");
+      if (dhcpc_sendmsg(pdhcpc, presult, DHCPREQUEST) < 0)
+        {
+          return ERROR;
+        }
 
-              /* The ACK response means that the server has accepted
-               * our request and we have the lease.
-               */
+      retries++;
 
-              if (msgtype == DHCPACK)
-                {
-                  ninfo("Received ACK\n");
-                  state = STATE_HAVE_LEASE;
-                }
+      /* Get the ACK/NAK response to the REQUEST (or timeout) */
 
-              /* NAK means the server has refused our request.  Break out of
-               * this loop with state == STATE_HAVE_OFFER and send DISCOVER
-               * again
-               */
+      result = recv(pdhcpc->sockfd, &pdhcpc->packet,
+                    sizeof(struct dhcp_msg), 0);
+      if (result >= 0)
+        {
+          /* Parse the response */
 
-              else if (msgtype == DHCPNAK)
-                {
-                  ninfo("Received NAK\n");
-                  break;
-                }
+          msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
 
-              /* If we get any OFFERs from other servers, then decline
-               * them now and continue waiting for the ACK from the server
-               * that we requested from.
-               */
+          /* The ACK response means that the server has accepted
+           * our request and we have the lease.
+           */
 
-              else if (msgtype == DHCPOFFER)
-                {
-                  ninfo("Received another OFFER, send DECLINE\n");
-                  dhcpc_sendmsg(pdhcpc, presult, DHCPDECLINE);
-                }
+          if (msgtype == DHCPACK)
+            {
+              ninfo("Received ACK\n");
+              state = STATE_HAVE_LEASE;
+            }
 
-              /* Otherwise, it is something that we do not recognize */
+          /* NAK means the server has refused our request.  Break out of
+           * this loop with state == STATE_HAVE_OFFER and send DISCOVER
+           * again
+           */
 
-              else
-                {
-                  ninfo("Ignoring msgtype=%d\n", msgtype);
-                }
+          else if (msgtype == DHCPNAK)
+            {
+              ninfo("Received NAK\n");
+              break;
             }
 
-          /* An error has occurred.  If this was a timeout error (meaning
-           * that nothing was received on this socket for a long period of
-           * time). Then break out and send the DISCOVER command again
-           * (at most 3 times).
+          /* If we get any OFFERs from other servers, then decline
+           * them now and continue waiting for the ACK from the server
+           * that we requested from.
            */
 
-          else if (errno != EAGAIN)
+          else if (msgtype == DHCPOFFER)
             {
-              /* An error other than a timeout was received */
+              ninfo("Received another OFFER, send DECLINE\n");
+              dhcpc_sendmsg(pdhcpc, presult, DHCPDECLINE);
+            }
+
+          /* Otherwise, it is something that we do not recognize */
 
-              netlib_set_ipv4addr(pdhcpc->interface, &oldaddr);
-              return ERROR;
+          else
+            {
+              ninfo("Ignoring msgtype=%d\n", msgtype);
             }
         }
-      while (state == STATE_HAVE_OFFER &&
-             retries < CONFIG_NETUTILS_DHCPC_RETRIES);
+
+      /* An error has occurred.  If this was a timeout error (meaning
+       * that nothing was received on this socket for a long period of
+       * time). Then break out and send the DISCOVER command again
+       * (at most 3 times).
+       */
+
+      else if (errno != EAGAIN)
+        {
+          /* An error other than a timeout was received */
+
+          netlib_set_ipv4addr(pdhcpc->interface, &oldaddr);
+          return ERROR;
+        }
+    }
+  while (state == STATE_HAVE_OFFER &&
+         retries < CONFIG_NETUTILS_DHCPC_RETRIES);
+
+  /* If no DHCPLEASE recveived here, error out */
+
+  if (state != STATE_HAVE_LEASE)
+    {
+      return ERROR;
     }
-  while (state != STATE_HAVE_LEASE);
 
   ninfo("Got IP address %d.%d.%d.%d\n",
         (int)((presult->ipaddr.s_addr)       & 0xff),

Reply via email to