From 70bb13ce3b3866189f004c7e169116331654babc Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Mon, 30 Mar 2020 18:50:47 +0800 Subject: [PATCH] netutils/dhcpc: configurable timeout and retry count. Change-Id: I92e77bd24ddd7ffff39de9215f4b7c05a7b55bee Signed-off-by: chao.an --- 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 304cfc384..1a2839e66 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 8ca64b3f2..d39c51fb1 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);