2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 10:26:22 +02:00
|
|
|
* apps/netutils/dhcpc/dhcpc.c
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2012-02-18 19:13:30 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Based heavily on portions of uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2022-09-05 16:18:20 +02:00
|
|
|
#include <sys/random.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-11-23 12:25:33 +01:00
|
|
|
#include <inttypes.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2021-10-11 09:43:26 +02:00
|
|
|
#include <pthread.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
2018-06-25 23:18:46 +02:00
|
|
|
#include <netinet/udp.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/dhcpc.h"
|
|
|
|
#include "netutils/netlib.h"
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-02 16:20:05 +02:00
|
|
|
/* Configuration */
|
|
|
|
|
|
|
|
/* DHCP Definitions */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#define STATE_INITIAL 0
|
|
|
|
#define STATE_HAVE_OFFER 1
|
|
|
|
#define STATE_HAVE_LEASE 2
|
|
|
|
|
|
|
|
#define DHCP_REQUEST 1
|
|
|
|
#define DHCP_REPLY 2
|
|
|
|
#define DHCP_HTYPE_ETHERNET 1
|
|
|
|
#define DHCP_HLEN_ETHERNET 6
|
|
|
|
#define DHCP_MSG_LEN 236
|
|
|
|
|
|
|
|
#define DHCPC_SERVER_PORT 67
|
|
|
|
#define DHCPC_CLIENT_PORT 68
|
|
|
|
|
|
|
|
#define DHCPDISCOVER 1
|
|
|
|
#define DHCPOFFER 2
|
|
|
|
#define DHCPREQUEST 3
|
|
|
|
#define DHCPDECLINE 4
|
|
|
|
#define DHCPACK 5
|
|
|
|
#define DHCPNAK 6
|
|
|
|
#define DHCPRELEASE 7
|
|
|
|
|
|
|
|
#define DHCP_OPTION_SUBNET_MASK 1
|
|
|
|
#define DHCP_OPTION_ROUTER 3
|
|
|
|
#define DHCP_OPTION_DNS_SERVER 6
|
2019-08-14 13:29:18 +02:00
|
|
|
#define DHCP_OPTION_HOST_NAME 12
|
2011-03-19 22:04:13 +01:00
|
|
|
#define DHCP_OPTION_REQ_IPADDR 50
|
|
|
|
#define DHCP_OPTION_LEASE_TIME 51
|
|
|
|
#define DHCP_OPTION_MSG_TYPE 53
|
|
|
|
#define DHCP_OPTION_SERVER_ID 54
|
|
|
|
#define DHCP_OPTION_REQ_LIST 55
|
2022-09-26 05:28:28 +02:00
|
|
|
#define DHCP_OPTION_CLIENT_ID 61
|
2011-03-19 22:04:13 +01:00
|
|
|
#define DHCP_OPTION_END 255
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 256
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct dhcp_msg
|
|
|
|
{
|
|
|
|
uint8_t op;
|
|
|
|
uint8_t htype;
|
|
|
|
uint8_t hlen;
|
|
|
|
uint8_t hops;
|
|
|
|
uint8_t xid[4];
|
|
|
|
uint16_t secs;
|
|
|
|
uint16_t flags;
|
|
|
|
uint8_t ciaddr[4];
|
|
|
|
uint8_t yiaddr[4];
|
|
|
|
uint8_t siaddr[4];
|
|
|
|
uint8_t giaddr[4];
|
|
|
|
uint8_t chaddr[16];
|
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
|
|
|
uint8_t sname[64];
|
|
|
|
uint8_t file[128];
|
|
|
|
#endif
|
|
|
|
uint8_t options[312];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dhcpc_state_s
|
|
|
|
{
|
2017-05-19 22:34:00 +02:00
|
|
|
FAR const char *interface;
|
2014-06-25 02:55:01 +02:00
|
|
|
int sockfd;
|
2022-09-05 16:18:20 +02:00
|
|
|
uint8_t xid[4];
|
2014-06-25 02:55:01 +02:00
|
|
|
struct in_addr ipaddr;
|
|
|
|
struct in_addr serverid;
|
|
|
|
struct dhcp_msg packet;
|
2021-10-11 09:43:26 +02:00
|
|
|
bool cancel;
|
|
|
|
pthread_t thread; /* Thread ID of the DHCPC thread */
|
|
|
|
dhcpc_callback_t callback; /* Thread callback of the DHCPC thread */
|
|
|
|
int maclen;
|
|
|
|
uint8_t macaddr[1];
|
2011-03-19 22:04:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-03 08:45:11 +02:00
|
|
|
static const uint8_t magic_cookie[4] =
|
|
|
|
{
|
|
|
|
99, 130, 83, 99
|
|
|
|
};
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_add<option>
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-14 13:29:18 +02:00
|
|
|
static FAR uint8_t *dhcpc_addhostname(FAR const char *hostname,
|
|
|
|
FAR uint8_t *optptr)
|
|
|
|
{
|
|
|
|
int len = strlen(hostname);
|
|
|
|
*optptr++ = DHCP_OPTION_HOST_NAME;
|
|
|
|
*optptr++ = len;
|
|
|
|
memcpy(optptr, hostname, len);
|
|
|
|
return optptr + len;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addmsgtype(FAR uint8_t *optptr, uint8_t type)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_MSG_TYPE;
|
|
|
|
*optptr++ = 1;
|
|
|
|
*optptr++ = type;
|
|
|
|
return optptr;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addserverid(FAR struct in_addr *serverid,
|
|
|
|
FAR uint8_t *optptr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_SERVER_ID;
|
|
|
|
*optptr++ = 4;
|
|
|
|
memcpy(optptr, &serverid->s_addr, 4);
|
|
|
|
return optptr + 4;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addreqipaddr(FAR struct in_addr *ipaddr,
|
|
|
|
FAR uint8_t *optptr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_REQ_IPADDR;
|
|
|
|
*optptr++ = 4;
|
|
|
|
memcpy(optptr, &ipaddr->s_addr, 4);
|
|
|
|
return optptr + 4;
|
|
|
|
}
|
|
|
|
|
2022-09-26 05:28:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addclientid(FAR uint8_t *clientid,
|
|
|
|
FAR uint8_t len,
|
|
|
|
FAR uint8_t *optptr)
|
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_CLIENT_ID;
|
|
|
|
*optptr++ = 1 + len;
|
|
|
|
*optptr++ = 0x1;
|
|
|
|
memcpy(optptr, clientid, len);
|
|
|
|
return optptr + len;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addreqoptions(FAR uint8_t *optptr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_REQ_LIST;
|
|
|
|
*optptr++ = 3;
|
|
|
|
*optptr++ = DHCP_OPTION_SUBNET_MASK;
|
|
|
|
*optptr++ = DHCP_OPTION_ROUTER;
|
|
|
|
*optptr++ = DHCP_OPTION_DNS_SERVER;
|
|
|
|
return optptr;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static FAR uint8_t *dhcpc_addend(FAR uint8_t *optptr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*optptr++ = DHCP_OPTION_END;
|
|
|
|
return optptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_sendmsg
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static int dhcpc_sendmsg(FAR struct dhcpc_state_s *pdhcpc,
|
|
|
|
FAR struct dhcpc_state *presult, int msgtype)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2023-01-10 13:10:52 +01:00
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
2011-03-19 22:04:13 +01:00
|
|
|
struct sockaddr_in addr;
|
2017-05-19 23:41:28 +02:00
|
|
|
FAR uint8_t *pend;
|
2011-03-19 22:04:13 +01:00
|
|
|
in_addr_t serverid = INADDR_BROADCAST;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Create the common message header settings */
|
|
|
|
|
|
|
|
memset(&pdhcpc->packet, 0, sizeof(struct dhcp_msg));
|
|
|
|
pdhcpc->packet.op = DHCP_REQUEST;
|
|
|
|
pdhcpc->packet.htype = DHCP_HTYPE_ETHERNET;
|
2021-10-11 09:43:26 +02:00
|
|
|
pdhcpc->packet.hlen = pdhcpc->maclen;
|
2022-09-05 16:18:20 +02:00
|
|
|
memcpy(pdhcpc->packet.xid, pdhcpc->xid, 4);
|
2021-10-11 09:43:26 +02:00
|
|
|
memcpy(pdhcpc->packet.chaddr, pdhcpc->macaddr, pdhcpc->maclen);
|
|
|
|
memset(&pdhcpc->packet.chaddr[pdhcpc->maclen], 0, 16 - pdhcpc->maclen);
|
2011-03-19 22:04:13 +01:00
|
|
|
memcpy(pdhcpc->packet.options, magic_cookie, sizeof(magic_cookie));
|
|
|
|
|
|
|
|
/* Add the common header options */
|
|
|
|
|
|
|
|
pend = &pdhcpc->packet.options[4];
|
|
|
|
pend = dhcpc_addmsgtype(pend, msgtype);
|
|
|
|
|
2020-08-04 08:34:18 +02:00
|
|
|
/* Get the current host name */
|
|
|
|
|
2020-09-20 16:08:47 +02:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) || (0 == strlen(hostname)))
|
2020-08-04 08:34:18 +02:00
|
|
|
{
|
2023-01-10 13:10:52 +01:00
|
|
|
strlcpy(hostname, CONFIG_NETUTILS_DHCPC_HOST_NAME, sizeof(hostname));
|
2020-08-04 08:34:18 +02:00
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Handle the message specific settings */
|
|
|
|
|
|
|
|
switch (msgtype)
|
|
|
|
{
|
|
|
|
/* Broadcast DISCOVER message to all servers */
|
|
|
|
|
|
|
|
case DHCPDISCOVER:
|
2018-12-27 15:46:13 +01:00
|
|
|
/* REVISIT: We don't need the broadcast flag since we can receive
|
|
|
|
* unicast traffic before being fully configured.
|
|
|
|
*/
|
|
|
|
|
2021-01-18 10:57:33 +01:00
|
|
|
/* Broadcast bit. */
|
|
|
|
|
|
|
|
pdhcpc->packet.flags = HTONS(CONFIG_NETUTILS_DHCPC_BOOTP_FLAGS);
|
2019-08-14 13:29:18 +02:00
|
|
|
|
2020-08-04 08:34:18 +02:00
|
|
|
pend = dhcpc_addhostname(hostname, pend);
|
2011-03-19 22:04:13 +01:00
|
|
|
pend = dhcpc_addreqoptions(pend);
|
2022-09-26 05:28:28 +02:00
|
|
|
pend = dhcpc_addclientid(pdhcpc->macaddr, pdhcpc->maclen, pend);
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Send REQUEST message to the server that sent the *first* OFFER */
|
|
|
|
|
|
|
|
case DHCPREQUEST:
|
2018-12-27 15:46:13 +01:00
|
|
|
/* REVISIT: We don't need the broadcast flag since we can receive
|
|
|
|
* unicast traffic before being fully configured.
|
|
|
|
*/
|
|
|
|
|
2021-01-18 10:57:33 +01:00
|
|
|
/* Broadcast bit. */
|
|
|
|
|
|
|
|
pdhcpc->packet.flags = HTONS(CONFIG_NETUTILS_DHCPC_BOOTP_FLAGS);
|
2019-08-14 13:29:18 +02:00
|
|
|
|
2020-08-04 08:34:18 +02:00
|
|
|
pend = dhcpc_addhostname(hostname, pend);
|
2011-03-19 22:04:13 +01:00
|
|
|
pend = dhcpc_addserverid(&pdhcpc->serverid, pend);
|
|
|
|
pend = dhcpc_addreqipaddr(&pdhcpc->ipaddr, pend);
|
2022-09-26 05:28:28 +02:00
|
|
|
pend = dhcpc_addclientid(pdhcpc->macaddr, pdhcpc->maclen, pend);
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Send DECLINE message to the server that sent the *last* OFFER */
|
|
|
|
|
|
|
|
case DHCPDECLINE:
|
|
|
|
memcpy(pdhcpc->packet.ciaddr, &presult->ipaddr.s_addr, 4);
|
|
|
|
pend = dhcpc_addserverid(&presult->serverid, pend);
|
|
|
|
serverid = presult->serverid.s_addr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pend = dhcpc_addend(pend);
|
2020-04-03 08:45:11 +02:00
|
|
|
len = pend - (uint8_t *)&pdhcpc->packet;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Send the request */
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = HTONS(DHCPC_SERVER_PORT);
|
|
|
|
addr.sin_addr.s_addr = serverid;
|
|
|
|
|
|
|
|
return sendto(pdhcpc->sockfd, &pdhcpc->packet, len, 0,
|
2020-04-03 08:45:11 +02:00
|
|
|
(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_parseoptions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,
|
|
|
|
FAR uint8_t *optptr, int len)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2017-05-19 23:41:28 +02:00
|
|
|
FAR uint8_t *end = optptr + len;
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t type = 0;
|
|
|
|
|
|
|
|
while (optptr < end)
|
|
|
|
{
|
2020-04-03 08:45:11 +02:00
|
|
|
switch (*optptr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
case DHCP_OPTION_SUBNET_MASK:
|
2020-04-03 08:45:11 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get subnet mask in network order */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 6 <= end)
|
|
|
|
{
|
|
|
|
memcpy(&presult->netmask.s_addr, optptr + 2, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (netmask missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_ROUTER:
|
2020-04-03 08:45:11 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get the default router address in network order */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 6 <= end)
|
|
|
|
{
|
|
|
|
memcpy(&presult->default_router.s_addr, optptr + 2, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (router address missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_DNS_SERVER:
|
2020-04-03 08:45:11 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get the DNS server address in network order */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 6 <= end)
|
|
|
|
{
|
|
|
|
memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (DNS address missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_MSG_TYPE:
|
2020-04-03 08:45:11 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get message type */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 3 <= end)
|
|
|
|
{
|
|
|
|
type = *(optptr + 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (type missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_SERVER_ID:
|
2020-04-03 08:45:11 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get server address in network order */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 6 <= end)
|
|
|
|
{
|
|
|
|
memcpy(&presult->serverid.s_addr, optptr + 2, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (server address missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_LEASE_TIME:
|
2021-06-29 13:39:16 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Get lease time (in seconds) in host order */
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 6 <= end)
|
|
|
|
{
|
|
|
|
uint16_t tmp[2];
|
|
|
|
memcpy(tmp, optptr + 2, 4);
|
|
|
|
presult->lease_time = ((uint32_t)ntohs(tmp[0])) << 16 |
|
|
|
|
(uint32_t)ntohs(tmp[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("Packet too short (lease time missing)\n");
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_END:
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:39:16 +02:00
|
|
|
if (optptr + 1 >= end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
optptr += optptr[1] + 2;
|
|
|
|
}
|
2017-05-19 22:34:00 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_parsemsg
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
static uint8_t dhcpc_parsemsg(FAR struct dhcpc_state_s *pdhcpc, int buflen,
|
|
|
|
FAR struct dhcpc_state *presult)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-06-29 13:39:16 +02:00
|
|
|
if (buflen >= 44 && pdhcpc->packet.op == DHCP_REPLY &&
|
2022-09-05 16:18:20 +02:00
|
|
|
memcmp(pdhcpc->packet.xid, pdhcpc->xid, 4) == 0 &&
|
2020-04-03 08:45:11 +02:00
|
|
|
memcmp(pdhcpc->packet.chaddr,
|
2021-10-11 09:43:26 +02:00
|
|
|
pdhcpc->macaddr, pdhcpc->maclen) == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
|
2021-06-29 13:39:16 +02:00
|
|
|
return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4],
|
|
|
|
buflen -
|
|
|
|
(offsetof(struct dhcp_msg, options) + 4));
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2017-05-19 22:34:00 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:43:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_run
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *dhcpc_run(void *args)
|
|
|
|
{
|
|
|
|
FAR struct dhcpc_state_s *pdhcpc = (FAR struct dhcpc_state_s *)args;
|
|
|
|
struct dhcpc_state result;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ret = dhcpc_request(pdhcpc, &result);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
pdhcpc->callback(&result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pdhcpc->callback(NULL);
|
2022-01-05 08:48:32 +01:00
|
|
|
memset(&result, 0, sizeof(result));
|
2021-10-11 09:43:26 +02:00
|
|
|
nerr("dhcpc_request error\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdhcpc->cancel)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:48:32 +01:00
|
|
|
result.lease_time /= 2;
|
2021-10-11 09:43:26 +02:00
|
|
|
while (result.lease_time)
|
|
|
|
{
|
|
|
|
result.lease_time = sleep(result.lease_time);
|
|
|
|
if (pdhcpc->cancel)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2015-10-03 00:20:33 +02:00
|
|
|
* Public Functions
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
|
|
|
|
int maclen)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2017-05-19 22:34:00 +02:00
|
|
|
FAR struct dhcpc_state_s *pdhcpc;
|
2011-03-19 22:04:13 +01:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct timeval tv;
|
2012-11-27 17:26:54 +01:00
|
|
|
int ret;
|
2022-09-05 16:18:20 +02:00
|
|
|
const uint8_t default_xid[4] =
|
|
|
|
{
|
|
|
|
0xad, 0xde, 0x12, 0x23
|
|
|
|
};
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
2020-04-03 08:45:11 +02:00
|
|
|
((uint8_t *)macaddr)[0], ((uint8_t *)macaddr)[1],
|
|
|
|
((uint8_t *)macaddr)[2], ((uint8_t *)macaddr)[3],
|
|
|
|
((uint8_t *)macaddr)[4], ((uint8_t *)macaddr)[5]);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Allocate an internal DHCP structure */
|
|
|
|
|
2021-10-11 09:43:26 +02:00
|
|
|
pdhcpc = malloc(sizeof(struct dhcpc_state_s) + maclen - 1);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (pdhcpc)
|
|
|
|
{
|
2022-10-20 13:26:37 +02:00
|
|
|
/* Initialize the allocated structure */
|
|
|
|
|
|
|
|
memset(pdhcpc, 0, sizeof(struct dhcpc_state_s));
|
|
|
|
|
2022-09-05 16:18:20 +02:00
|
|
|
/* RFC2131: A DHCP client MUST choose 'xid's in such a
|
|
|
|
* way as to minimize the chance of using an 'xid' identical to one
|
|
|
|
* used by another client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = getrandom(pdhcpc->xid, 4, 0);
|
|
|
|
if (ret != 4)
|
|
|
|
{
|
|
|
|
ret = getrandom(pdhcpc->xid, 4, GRND_RANDOM);
|
|
|
|
if (ret != 4)
|
|
|
|
{
|
|
|
|
memcpy(pdhcpc->xid, default_xid, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:43:26 +02:00
|
|
|
pdhcpc->interface = interface;
|
|
|
|
pdhcpc->maclen = maclen;
|
|
|
|
memcpy(pdhcpc->macaddr, macaddr, pdhcpc->maclen);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Create a UDP socket */
|
|
|
|
|
2022-09-26 20:56:21 +02:00
|
|
|
pdhcpc->sockfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (pdhcpc->sockfd < 0)
|
|
|
|
{
|
2020-05-25 16:18:09 +02:00
|
|
|
ninfo("socket handle %d\n", pdhcpc->sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
free(pdhcpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-27 17:26:54 +01:00
|
|
|
/* Bind the socket */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = HTONS(DHCPC_CLIENT_PORT);
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2020-04-03 08:45:11 +02:00
|
|
|
ret = bind(pdhcpc->sockfd, (struct sockaddr *)&addr,
|
2018-06-25 23:18:46 +02:00
|
|
|
sizeof(struct sockaddr_in));
|
2012-11-27 17:26:54 +01:00
|
|
|
if (ret < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-03 08:45:11 +02:00
|
|
|
ninfo("bind status %d\n", ret);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(pdhcpc->sockfd);
|
|
|
|
free(pdhcpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure for read timeouts */
|
|
|
|
|
2022-07-07 10:08:50 +02:00
|
|
|
tv.tv_sec = CONFIG_NETUTILS_DHCPC_RECV_TIMEOUT_MS / 1000;
|
|
|
|
tv.tv_usec = (CONFIG_NETUTILS_DHCPC_RECV_TIMEOUT_MS % 1000) * 1000;
|
2012-11-27 17:26:54 +01:00
|
|
|
|
2018-06-25 23:18:46 +02:00
|
|
|
ret = setsockopt(pdhcpc->sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
|
|
|
|
sizeof(struct timeval));
|
2012-11-27 17:26:54 +01:00
|
|
|
if (ret < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-03 08:45:11 +02:00
|
|
|
ninfo("setsockopt(RCVTIMEO) status %d\n", ret);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(pdhcpc->sockfd);
|
|
|
|
free(pdhcpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-06-25 23:18:46 +02:00
|
|
|
|
2022-08-24 04:37:49 +02:00
|
|
|
#ifdef CONFIG_NET_BINDTODEVICE
|
2018-06-25 23:18:46 +02:00
|
|
|
/* Bind socket to interface, because UDP packets have to be sent to the
|
|
|
|
* broadcast address at a moment when it is not possible to decide the
|
|
|
|
* target network device using the local or remote address (which is,
|
|
|
|
* by definition and purpose of DHCP, undefined yet).
|
|
|
|
*/
|
|
|
|
|
2022-08-24 04:37:49 +02:00
|
|
|
ret = setsockopt(pdhcpc->sockfd, SOL_SOCKET, SO_BINDTODEVICE,
|
2018-06-25 23:18:46 +02:00
|
|
|
pdhcpc->interface, strlen(pdhcpc->interface));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-04-03 08:45:11 +02:00
|
|
|
ninfo("setsockopt(BINDTODEVICE) status %d\n", ret);
|
2018-06-25 23:18:46 +02:00
|
|
|
close(pdhcpc->sockfd);
|
|
|
|
free(pdhcpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
return (FAR void *)pdhcpc;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
void dhcpc_close(FAR void *handle)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-11-27 17:26:54 +01:00
|
|
|
struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle;
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
if (pdhcpc)
|
|
|
|
{
|
2021-10-11 09:43:26 +02:00
|
|
|
if (pdhcpc->thread)
|
|
|
|
{
|
|
|
|
dhcpc_cancel(pdhcpc);
|
|
|
|
}
|
|
|
|
|
2012-11-27 17:26:54 +01:00
|
|
|
if (pdhcpc->sockfd)
|
|
|
|
{
|
|
|
|
close(pdhcpc->sockfd);
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
free(pdhcpc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:43:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_cancel
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void dhcpc_cancel(FAR void *handle)
|
|
|
|
{
|
|
|
|
struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle;
|
|
|
|
sighandler_t old;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (pdhcpc)
|
|
|
|
{
|
|
|
|
pdhcpc->cancel = true;
|
|
|
|
|
|
|
|
if (pdhcpc->thread)
|
|
|
|
{
|
|
|
|
old = signal(SIGQUIT, SIG_IGN);
|
|
|
|
|
|
|
|
/* Signal the dhcpc_run */
|
|
|
|
|
|
|
|
ret = pthread_kill(pdhcpc->thread, SIGQUIT);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: pthread_kill DHCPC thread\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the end of dhcpc_run */
|
|
|
|
|
|
|
|
ret = pthread_join(pdhcpc->thread, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: pthread_join DHCPC thread\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pdhcpc->thread = 0;
|
|
|
|
signal(SIGQUIT, old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_request
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 23:41:28 +02:00
|
|
|
int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2017-05-19 23:41:28 +02:00
|
|
|
FAR struct dhcpc_state_s *pdhcpc = (FAR struct dhcpc_state_s *)handle;
|
2011-03-19 22:04:13 +01:00
|
|
|
struct in_addr oldaddr;
|
|
|
|
ssize_t result;
|
|
|
|
uint8_t msgtype;
|
|
|
|
int retries;
|
|
|
|
int state;
|
2022-09-07 09:29:26 +02:00
|
|
|
clock_t start;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-10-21 05:04:52 +02:00
|
|
|
memset(presult, 0, sizeof(*presult));
|
|
|
|
|
2022-09-05 16:18:20 +02:00
|
|
|
/* RFC2131: For example, a client may choose a different,
|
|
|
|
* random initial 'xid' each time the client is rebooted, and
|
|
|
|
* subsequently use sequential 'xid's until the next reboot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pdhcpc->xid[3]++;
|
|
|
|
|
2022-10-13 11:44:45 +02:00
|
|
|
/* Save the currently assigned IP address. It should be INADDR_ANY
|
|
|
|
* if this is the initial request, or a valid IP if this is a renewal.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
oldaddr.s_addr = 0;
|
2017-05-19 22:34:00 +02:00
|
|
|
netlib_get_ipv4addr(pdhcpc->interface, &oldaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Loop sending the DISCOVER up to CONFIG_NETUTILS_DHCPC_RETRIES
|
|
|
|
* times
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
retries = 0;
|
2020-03-30 12:50:47 +02:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2020-03-30 12:50:47 +02:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
state = STATE_INITIAL;
|
|
|
|
do
|
|
|
|
{
|
2021-10-11 09:43:26 +02:00
|
|
|
if (pdhcpc->cancel)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Send the DISCOVER command */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
ninfo("Broadcast DISCOVER\n");
|
|
|
|
if (dhcpc_sendmsg(pdhcpc, presult, DHCPDISCOVER) < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-09-28 08:41:29 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
retries++;
|
2020-03-30 12:50:47 +02:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Get the DHCPOFFER response */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
start = clock();
|
|
|
|
do
|
2021-09-28 08:41:29 +02:00
|
|
|
{
|
2022-09-07 09:29:26 +02:00
|
|
|
result = recv(pdhcpc->sockfd, &pdhcpc->packet,
|
|
|
|
sizeof(struct dhcp_msg), 0);
|
|
|
|
if (result >= 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2022-09-07 09:29:26 +02:00
|
|
|
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;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-26 21:12:15 +02:00
|
|
|
else if (errno != EAGAIN && errno != EINTR)
|
2022-09-07 09:29:26 +02:00
|
|
|
{
|
|
|
|
/* An error other than a timeout was received -- error out */
|
2021-09-28 08:41:29 +02:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2020-03-30 12:50:47 +02:00
|
|
|
}
|
2022-09-07 09:29:26 +02:00
|
|
|
while (state == STATE_INITIAL && TICK2MSEC(clock() - start) <
|
|
|
|
CONFIG_NETUTILS_DHCPC_RECV_TIMEOUT_MS);
|
2021-09-28 08:41:29 +02:00
|
|
|
}
|
|
|
|
while (state == STATE_INITIAL &&
|
|
|
|
retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
2020-03-30 12:50:47 +02:00
|
|
|
|
2022-10-13 11:44:45 +02:00
|
|
|
/* If no DHCPOFFER received here, error out */
|
2017-05-21 18:51:26 +02:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
if (state == STATE_INITIAL)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Loop sending the REQUEST up to CONFIG_NETUTILS_DHCPC_RETRIES times
|
|
|
|
* (if there is no response)
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
retries = 0;
|
|
|
|
do
|
|
|
|
{
|
2021-10-11 09:43:26 +02:00
|
|
|
if (pdhcpc->cancel)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Send the REQUEST message to obtain the lease that was offered to
|
|
|
|
* us.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
ninfo("Send REQUEST\n");
|
|
|
|
if (dhcpc_sendmsg(pdhcpc, presult, DHCPREQUEST) < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
retries++;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-09-28 08:41:29 +02:00
|
|
|
/* Get the ACK/NAK response to the REQUEST (or timeout) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
start = clock();
|
|
|
|
do
|
2021-09-28 08:41:29 +02:00
|
|
|
{
|
2022-09-07 09:29:26 +02:00
|
|
|
result = recv(pdhcpc->sockfd, &pdhcpc->packet,
|
|
|
|
sizeof(struct dhcp_msg), 0);
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
/* Parse the response */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
msgtype = dhcpc_parsemsg(pdhcpc, result, presult);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* The ACK response means that the server has accepted
|
|
|
|
* our request and we have the lease.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
if (msgtype == DHCPACK)
|
|
|
|
{
|
|
|
|
ninfo("Received ACK\n");
|
|
|
|
state = STATE_HAVE_LEASE;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* NAK means the server has refused our request */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
else if (msgtype == DHCPNAK)
|
|
|
|
{
|
|
|
|
ninfo("Received NAK\n");
|
2022-10-13 11:44:45 +02:00
|
|
|
oldaddr.s_addr = INADDR_ANY;
|
|
|
|
netlib_set_ipv4addr(pdhcpc->interface, &oldaddr);
|
2022-09-07 09:29:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2023-02-10 03:52:40 +01:00
|
|
|
else if (msgtype == DHCPOFFER &&
|
|
|
|
pdhcpc->serverid.s_addr != presult->serverid.s_addr)
|
2022-09-07 09:29:26 +02:00
|
|
|
{
|
|
|
|
ninfo("Received another OFFER, send DECLINE\n");
|
|
|
|
dhcpc_sendmsg(pdhcpc, presult, DHCPDECLINE);
|
|
|
|
}
|
2021-09-28 08:41:29 +02:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* Otherwise, it is something that we do not recognize */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ninfo("Ignoring msgtype=%d\n", msgtype);
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2021-09-28 08:41:29 +02:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
/* 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).
|
|
|
|
*/
|
2021-09-28 08:41:29 +02:00
|
|
|
|
2022-09-26 21:12:15 +02:00
|
|
|
else if (errno != EAGAIN && errno != EINTR)
|
2022-09-07 09:29:26 +02:00
|
|
|
{
|
|
|
|
/* An error other than a timeout was received */
|
2021-09-28 08:41:29 +02:00
|
|
|
|
2022-09-07 09:29:26 +02:00
|
|
|
netlib_set_ipv4addr(pdhcpc->interface, &oldaddr);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2021-09-28 08:41:29 +02:00
|
|
|
}
|
2022-09-07 09:29:26 +02:00
|
|
|
while (state == STATE_HAVE_OFFER && TICK2MSEC(clock() - start) <
|
|
|
|
CONFIG_NETUTILS_DHCPC_RECV_TIMEOUT_MS);
|
2021-09-28 08:41:29 +02:00
|
|
|
}
|
|
|
|
while (state == STATE_HAVE_OFFER &&
|
|
|
|
retries < CONFIG_NETUTILS_DHCPC_RETRIES);
|
|
|
|
|
2022-10-13 11:44:45 +02:00
|
|
|
/* If no DHCPLEASE received here, error out */
|
2021-09-28 08:41:29 +02:00
|
|
|
|
|
|
|
if (state != STATE_HAVE_LEASE)
|
|
|
|
{
|
|
|
|
return ERROR;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("Got IP address %d.%d.%d.%d\n",
|
2020-11-23 12:25:33 +01:00
|
|
|
(int)((presult->ipaddr.s_addr) & 0xff),
|
|
|
|
(int)((presult->ipaddr.s_addr >> 8) & 0xff),
|
|
|
|
(int)((presult->ipaddr.s_addr >> 16) & 0xff),
|
|
|
|
(int)((presult->ipaddr.s_addr >> 24) & 0xff));
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("Got netmask %d.%d.%d.%d\n",
|
2020-11-23 12:25:33 +01:00
|
|
|
(int)((presult->netmask.s_addr) & 0xff),
|
|
|
|
(int)((presult->netmask.s_addr >> 8) & 0xff),
|
|
|
|
(int)((presult->netmask.s_addr >> 16) & 0xff),
|
|
|
|
(int)((presult->netmask.s_addr >> 24) & 0xff));
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("Got DNS server %d.%d.%d.%d\n",
|
2020-11-23 12:25:33 +01:00
|
|
|
(int)((presult->dnsaddr.s_addr) & 0xff),
|
|
|
|
(int)((presult->dnsaddr.s_addr >> 8) & 0xff),
|
|
|
|
(int)((presult->dnsaddr.s_addr >> 16) & 0xff),
|
|
|
|
(int)((presult->dnsaddr.s_addr >> 24) & 0xff));
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("Got default router %d.%d.%d.%d\n",
|
2020-11-23 12:25:33 +01:00
|
|
|
(int)((presult->default_router.s_addr) & 0xff),
|
|
|
|
(int)((presult->default_router.s_addr >> 8) & 0xff),
|
|
|
|
(int)((presult->default_router.s_addr >> 16) & 0xff),
|
|
|
|
(int)((presult->default_router.s_addr >> 24) & 0xff));
|
|
|
|
ninfo("Lease expires in %" PRId32 " seconds\n", presult->lease_time);
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2021-10-11 09:43:26 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpc_request_async
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dhcpc_request_async(FAR void *handle, dhcpc_callback_t callback)
|
|
|
|
{
|
|
|
|
FAR struct dhcpc_state_s *pdhcpc = (FAR struct dhcpc_state_s *)handle;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!handle || !callback)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdhcpc->thread)
|
|
|
|
{
|
|
|
|
nerr("ERROR: DHCPC thread already running\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdhcpc->callback = callback;
|
|
|
|
ret = pthread_create(&pdhcpc->thread, NULL, dhcpc_run, pdhcpc);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to start the DHCPC thread\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|