You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/04/04 00:11:33 UTC

[incubator-nuttx-apps] branch master updated (d7df165 -> c6b678f)

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

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


    from d7df165  examples/udp: When a host PC is used, take its implementation of hton/ntoh functions.
     new 70bb13c  netutils/dhcpc: configurable timeout and retry count.
     new c6b678f  netutils/dhcpc: fix nxstyle warning

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 netutils/dhcpc/Kconfig |  13 +++++
 netutils/dhcpc/dhcpc.c | 125 ++++++++++++++++++++++++++++++-------------------
 2 files changed, 90 insertions(+), 48 deletions(-)


[incubator-nuttx-apps] 02/02: netutils/dhcpc: fix nxstyle warning

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c6b678fa60fad150acf58f0bb54de12419c3db62
Author: chao.an <an...@xiaomi.com>
AuthorDate: Fri Apr 3 14:45:11 2020 +0800

    netutils/dhcpc: fix nxstyle warning
    
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 netutils/dhcpc/dhcpc.c | 107 ++++++++++++++++++++++++++++---------------------
 1 file changed, 61 insertions(+), 46 deletions(-)

diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c
index d39c51f..b4fc52a 100644
--- a/netutils/dhcpc/dhcpc.c
+++ b/netutils/dhcpc/dhcpc.c
@@ -64,7 +64,6 @@
 
 /* Configuration */
 
-
 /* DHCP Definitions */
 
 #define STATE_INITIAL           0
@@ -143,8 +142,15 @@ struct dhcpc_state_s
  * Private Data
  ****************************************************************************/
 
-static const uint8_t xid[4]          = {0xad, 0xde, 0x12, 0x23};
-static const uint8_t magic_cookie[4] = {99, 130, 83, 99};
+static const uint8_t xid[4] =
+{
+  0xad, 0xde, 0x12, 0x23
+};
+
+static const uint8_t magic_cookie[4] =
+{
+  99, 130, 83, 99
+};
 
 /****************************************************************************
  * Private Functions
@@ -226,7 +232,8 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
   pdhcpc->packet.hlen  = pdhcpc->ds_maclen;
   memcpy(pdhcpc->packet.xid, xid, 4);
   memcpy(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen);
-  memset(&pdhcpc->packet.chaddr[pdhcpc->ds_maclen], 0, 16 - pdhcpc->ds_maclen);
+  memset(&pdhcpc->packet.chaddr[pdhcpc->ds_maclen],
+         0, 16 - pdhcpc->ds_maclen);
   memcpy(pdhcpc->packet.options, magic_cookie, sizeof(magic_cookie));
 
   /* Add the common header options */
@@ -278,7 +285,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
     }
 
   pend = dhcpc_addend(pend);
-  len  = pend - (uint8_t*)&pdhcpc->packet;
+  len  = pend - (uint8_t *)&pdhcpc->packet;
 
   /* Send the request */
 
@@ -287,7 +294,7 @@ static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
   addr.sin_addr.s_addr = serverid;
 
   return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
-                (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
+                (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
 }
 
 /****************************************************************************
@@ -302,33 +309,38 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
 
   while (optptr < end)
     {
-      switch(*optptr)
+      switch (*optptr)
         {
           case DHCP_OPTION_SUBNET_MASK:
+
             /* Get subnet mask in network order */
 
             memcpy(&presult->netmask.s_addr, optptr + 2, 4);
             break;
 
           case DHCP_OPTION_ROUTER:
+
             /* Get the default router address in network order */
 
             memcpy(&presult->default_router.s_addr, optptr + 2, 4);
             break;
 
           case DHCP_OPTION_DNS_SERVER:
+
             /* Get the DNS server address in network order */
 
             memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
             break;
 
           case DHCP_OPTION_MSG_TYPE:
+
             /* Get message type */
 
             type = *(optptr + 2);
             break;
 
           case DHCP_OPTION_SERVER_ID:
+
             /* Get server address in network order */
 
             memcpy(&presult->serverid.s_addr, optptr + 2, 4);
@@ -364,7 +376,8 @@ static uint8_t dhcpc_parsemsg(FAR struct dhcpc_state_s *pdhcpc, int buflen,
 {
   if (pdhcpc->packet.op == DHCP_REPLY &&
       memcmp(pdhcpc->packet.xid, xid, sizeof(xid)) == 0 &&
-      memcmp(pdhcpc->packet.chaddr, pdhcpc->ds_macaddr, pdhcpc->ds_maclen) == 0)
+      memcmp(pdhcpc->packet.chaddr,
+             pdhcpc->ds_macaddr, pdhcpc->ds_maclen) == 0)
     {
       memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
       return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4], buflen);
@@ -390,8 +403,9 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
   int ret;
 
   ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
-        ((uint8_t*)macaddr)[0], ((uint8_t*)macaddr)[1], ((uint8_t*)macaddr)[2],
-        ((uint8_t*)macaddr)[3], ((uint8_t*)macaddr)[4], ((uint8_t*)macaddr)[5]);
+        ((uint8_t *)macaddr)[0], ((uint8_t *)macaddr)[1],
+        ((uint8_t *)macaddr)[2], ((uint8_t *)macaddr)[3],
+        ((uint8_t *)macaddr)[4], ((uint8_t *)macaddr)[5]);
 
   /* Allocate an internal DHCP structure */
 
@@ -410,7 +424,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
       pdhcpc->sockfd = socket(PF_INET, SOCK_DGRAM, 0);
       if (pdhcpc->sockfd < 0)
         {
-          ninfo("socket handle %d\n",ret);
+          ninfo("socket handle %d\n", ret);
           free(pdhcpc);
           return NULL;
         }
@@ -421,11 +435,11 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
       addr.sin_port        = HTONS(DHCPC_CLIENT_PORT);
       addr.sin_addr.s_addr = INADDR_ANY;
 
-      ret = bind(pdhcpc->sockfd, (struct sockaddr*)&addr,
+      ret = bind(pdhcpc->sockfd, (struct sockaddr *)&addr,
                  sizeof(struct sockaddr_in));
       if (ret < 0)
         {
-          ninfo("bind status %d\n",ret);
+          ninfo("bind status %d\n", ret);
           close(pdhcpc->sockfd);
           free(pdhcpc);
           return NULL;
@@ -440,7 +454,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
                        sizeof(struct timeval));
       if (ret < 0)
         {
-          ninfo("setsockopt(RCVTIMEO) status %d\n",ret);
+          ninfo("setsockopt(RCVTIMEO) status %d\n", ret);
           close(pdhcpc->sockfd);
           free(pdhcpc);
           return NULL;
@@ -457,13 +471,12 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
                        pdhcpc->interface, strlen(pdhcpc->interface));
       if (ret < 0)
         {
-          ninfo("setsockopt(BINDTODEVICE) status %d\n",ret);
+          ninfo("setsockopt(BINDTODEVICE) status %d\n", ret);
           close(pdhcpc->sockfd);
           free(pdhcpc);
           return NULL;
         }
 #endif
-
     }
 
   return (FAR void *)pdhcpc;
@@ -557,8 +570,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
                   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.
+                  /* Temporarily use the address offered by the server
+                   * and break out of the loop.
                    */
 
                   netlib_set_ipv4addr(pdhcpc->interface,
@@ -567,9 +580,9 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
                 }
             }
 
-          /* 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.
+          /* 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.
            */
 
           else if (errno != EAGAIN)
@@ -579,7 +592,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
               return ERROR;
             }
         }
-      while (state == STATE_INITIAL && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
+      while (state == STATE_INITIAL &&
+             retries < CONFIG_NETUTILS_DHCPC_RETRIES);
 
       /* If no DHCPOFFER recveived here, error out */
 
@@ -617,8 +631,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
 
               msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
 
-              /* The ACK response means that the server has accepted our request
-               * and we have the lease.
+              /* The ACK response means that the server has accepted
+               * our request and we have the lease.
                */
 
               if (msgtype == DHCPACK)
@@ -638,9 +652,9 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
                   break;
                 }
 
-              /* 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.
+              /* 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 (msgtype == DHCPOFFER)
@@ -659,8 +673,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
 
           /* 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).
+           * time). Then break out and send the DISCOVER command again
+           * (at most 3 times).
            */
 
           else if (errno != EAGAIN)
@@ -671,30 +685,31 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
               return ERROR;
             }
         }
-      while (state == STATE_HAVE_OFFER && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
+      while (state == STATE_HAVE_OFFER &&
+             retries < CONFIG_NETUTILS_DHCPC_RETRIES);
     }
   while (state != STATE_HAVE_LEASE);
 
   ninfo("Got IP address %d.%d.%d.%d\n",
-        (presult->ipaddr.s_addr       ) & 0xff,
-        (presult->ipaddr.s_addr >> 8  ) & 0xff,
-        (presult->ipaddr.s_addr >> 16 ) & 0xff,
-        (presult->ipaddr.s_addr >> 24 ) & 0xff);
+        (presult->ipaddr.s_addr)       & 0xff,
+        (presult->ipaddr.s_addr >> 8)  & 0xff,
+        (presult->ipaddr.s_addr >> 16) & 0xff,
+        (presult->ipaddr.s_addr >> 24) & 0xff);
   ninfo("Got netmask %d.%d.%d.%d\n",
-        (presult->netmask.s_addr       ) & 0xff,
-        (presult->netmask.s_addr >> 8  ) & 0xff,
-        (presult->netmask.s_addr >> 16 ) & 0xff,
-        (presult->netmask.s_addr >> 24 ) & 0xff);
+        (presult->netmask.s_addr)       & 0xff,
+        (presult->netmask.s_addr >> 8)  & 0xff,
+        (presult->netmask.s_addr >> 16) & 0xff,
+        (presult->netmask.s_addr >> 24) & 0xff);
   ninfo("Got DNS server %d.%d.%d.%d\n",
-        (presult->dnsaddr.s_addr       ) & 0xff,
-        (presult->dnsaddr.s_addr >> 8  ) & 0xff,
-        (presult->dnsaddr.s_addr >> 16 ) & 0xff,
-        (presult->dnsaddr.s_addr >> 24 ) & 0xff);
+        (presult->dnsaddr.s_addr)       & 0xff,
+        (presult->dnsaddr.s_addr >> 8)  & 0xff,
+        (presult->dnsaddr.s_addr >> 16) & 0xff,
+        (presult->dnsaddr.s_addr >> 24) & 0xff);
   ninfo("Got default router %d.%d.%d.%d\n",
-        (presult->default_router.s_addr       ) & 0xff,
-        (presult->default_router.s_addr >> 8  ) & 0xff,
-        (presult->default_router.s_addr >> 16 ) & 0xff,
-        (presult->default_router.s_addr >> 24 ) & 0xff);
+        (presult->default_router.s_addr)       & 0xff,
+        (presult->default_router.s_addr >> 8)  & 0xff,
+        (presult->default_router.s_addr >> 16) & 0xff,
+        (presult->default_router.s_addr >> 24) & 0xff);
   ninfo("Lease expires in %d seconds\n", presult->lease_time);
   return OK;
 }


[incubator-nuttx-apps] 01/02: netutils/dhcpc: configurable timeout and retry count.

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 70bb13ce3b3866189f004c7e169116331654babc
Author: chao.an <an...@xiaomi.com>
AuthorDate: Mon Mar 30 18:50:47 2020 +0800

    netutils/dhcpc: configurable timeout and retry count.
    
    Change-Id: I92e77bd24ddd7ffff39de9215f4b7c05a7b55bee
    Signed-off-by: chao.an <an...@xiaomi.com>
---
 netutils/dhcpc/Kconfig | 13 +++++++++++++
 netutils/dhcpc/dhcpc.c | 22 ++++++++++++++++++----
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/netutils/dhcpc/Kconfig b/netutils/dhcpc/Kconfig
index 304cfc3..1a2839e 100644
--- a/netutils/dhcpc/Kconfig
+++ b/netutils/dhcpc/Kconfig
@@ -16,4 +16,17 @@ config NETUTILS_DHCPC_HOST_NAME
 	string "DHCP client host name"
 	default "nuttx"
 
+config NETUTILS_DHCPC_RECV_TIMEOUT
+	int "Number of receive timeout in second"
+	default 3
+	---help---
+		This is the timeout value when dhcp client receives response
+
+config NETUTILS_DHCPC_RETRIES
+	int "Number of retries for dhcp client request"
+	default 3
+	---help---
+		This setting determines how many times resolver retries request
+		until failing.
+
 endif
diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c
index 8ca64b3..d39c51f 100644
--- a/netutils/dhcpc/dhcpc.c
+++ b/netutils/dhcpc/dhcpc.c
@@ -433,7 +433,7 @@ FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
 
       /* Configure for read timeouts */
 
-      tv.tv_sec  = 10;
+      tv.tv_sec  = CONFIG_NETUTILS_DHCPC_RECV_TIMEOUT;
       tv.tv_usec = 0;
 
       ret = setsockopt(pdhcpc->sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
@@ -516,6 +516,10 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
       newaddr.s_addr = INADDR_ANY;
       netlib_set_ipv4addr(pdhcpc->interface, &newaddr);
 
+      /* Loop sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES times */
+
+      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
@@ -533,6 +537,8 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
               return ERROR;
             }
 
+          retries++;
+
           /* Get the DHCPOFFER response */
 
           result = recv(pdhcpc->sockfd, &pdhcpc->packet,
@@ -573,10 +579,18 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
               return ERROR;
             }
         }
-      while (state == STATE_INITIAL);
+      while (state == STATE_INITIAL && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
 
+      /* If no DHCPOFFER recveived here, error out */
 
-      /* Loop sending the REQUEST up to three times (if there is no response) */
+      if (state == STATE_INITIAL)
+        {
+          return ERROR;
+        }
+
+      /* Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times
+       * (if there is no response)
+       */
 
       retries = 0;
       do
@@ -657,7 +671,7 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
               return ERROR;
             }
         }
-      while (state == STATE_HAVE_OFFER && retries < 3);
+      while (state == STATE_HAVE_OFFER && retries < CONFIG_NETUTILS_DHCPC_RETRIES);
     }
   while (state != STATE_HAVE_LEASE);