DHCPC: Remove hard-coded interface device. Now passed as a parameter to dhcpc_open().

This commit is contained in:
Sebastien Lorquet 2017-05-19 14:34:00 -06:00 committed by Gregory Nutt
parent 395c191b43
commit ea1025ead2
10 changed files with 26 additions and 33 deletions

View File

@ -142,7 +142,7 @@ printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address in this
* example. The address should be renewed in ds.lease_time/2 seconds.

View File

@ -147,7 +147,7 @@ int discover_main(int argc, char *argv[])
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address in this
* example. The address should be renewed in ds.lease_time/2 seconds.

View File

@ -156,7 +156,7 @@ static int tcpecho_netsetup()
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address in this
* example. The address should be renewed in ds.lease_time/2 seconds.

View File

@ -153,7 +153,7 @@ int webserver_main(int argc, char *argv[])
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address in this
* example. The address should be renewed in ds.lease_time/2 seconds.

View File

@ -321,7 +321,7 @@ static int xmlrpc_netinit(void)
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note: there is no logic here for renewing the address
* in this example. The address should be renewed in ds.lease_time/2

View File

@ -75,8 +75,9 @@ extern "C"
#define EXTERN extern
#endif
FAR void *dhcpc_open(FAR const void *mac_addr, int mac_len);
int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult);
FAR void *dhcpc_open(FAR const char *interface,
FAR const void *mac_addr, int mac_len);
int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult);
void dhcpc_close(FAR void *handle);
#undef EXTERN

View File

@ -12,12 +12,4 @@ config NETUTILS_DHCPC
if NETUTILS_DHCPC
config NETUTILS_DHCPC_DEVNAME
string "DHCP device name"
default "eth0" if !DRIVERS_IEEE80211
default "wlan0" if DRIVERS_IEEE80211
depends on NET_UDP && NET_BROADCAST && NET_IPv4
---help---
Specify the Ethernet or IEEE 802.15.4 device to use.
endif

View File

@ -63,11 +63,6 @@
/* Configuration */
#ifdef CONFIG_NETUTILS_DHCPC_DEVNAME
# define DEVNAME CONFIG_NETUTILS_DHCPC_DEVNAME
#else
# define DEVNAME "eth0"
#endif
/* DHCP Definitions */
@ -133,7 +128,8 @@ struct dhcp_msg
struct dhcpc_state_s
{
const void *ds_macaddr;
FAR const char *interface;
FAR const void *ds_macaddr;
int ds_maclen;
int sockfd;
struct in_addr ipaddr;
@ -328,6 +324,7 @@ static uint8_t dhcpc_parseoptions(struct dhcpc_state *presult, uint8_t *optptr,
optptr += optptr[1] + 2;
}
return type;
}
@ -335,8 +332,8 @@ static uint8_t dhcpc_parseoptions(struct dhcpc_state *presult, uint8_t *optptr,
* Name: dhcpc_parsemsg
****************************************************************************/
static uint8_t dhcpc_parsemsg(struct dhcpc_state_s *pdhcpc, int buflen,
struct dhcpc_state *presult)
static uint8_t dhcpc_parsemsg(FAR struct dhcpc_state_s *pdhcpc, int buflen,
FAR struct dhcpc_state *presult)
{
if (pdhcpc->packet.op == DHCP_REPLY &&
memcmp(pdhcpc->packet.xid, xid, sizeof(xid)) == 0 &&
@ -345,6 +342,7 @@ static uint8_t dhcpc_parsemsg(struct dhcpc_state_s *pdhcpc, int buflen,
memcpy(&presult->ipaddr.s_addr, pdhcpc->packet.yiaddr, 4);
return dhcpc_parseoptions(presult, &pdhcpc->packet.options[4], buflen);
}
return 0;
}
@ -356,9 +354,10 @@ static uint8_t dhcpc_parsemsg(struct dhcpc_state_s *pdhcpc, int buflen,
* Name: dhcpc_open
****************************************************************************/
void *dhcpc_open(const void *macaddr, int maclen)
FAR void *dhcpc_open(FAR const char *interface, FAR const void *macaddr,
int maclen)
{
struct dhcpc_state_s *pdhcpc;
FAR struct dhcpc_state_s *pdhcpc;
struct sockaddr_in addr;
struct timeval tv;
int ret;
@ -375,6 +374,7 @@ void *dhcpc_open(const void *macaddr, int maclen)
/* Initialize the allocated structure */
memset(pdhcpc, 0, sizeof(struct dhcpc_state_s));
pdhcpc->interface = interface;
pdhcpc->ds_macaddr = macaddr;
pdhcpc->ds_maclen = maclen;
@ -418,14 +418,14 @@ void *dhcpc_open(const void *macaddr, int maclen)
}
}
return (void*)pdhcpc;
return (FAR void *)pdhcpc;
}
/****************************************************************************
* Name: dhcpc_close
****************************************************************************/
void dhcpc_close(void *handle)
void dhcpc_close(FAR void *handle)
{
struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle;
@ -457,7 +457,7 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
/* Save the currently assigned IP address (should be INADDR_ANY) */
oldaddr.s_addr = 0;
netlib_get_ipv4addr(DEVNAME, &oldaddr);
netlib_get_ipv4addr(pdhcpc->interface, &oldaddr);
/* Loop until we receive the lease (or an error occurs) */
@ -466,7 +466,7 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
/* Set the IP address to INADDR_ANY. */
newaddr.s_addr = INADDR_ANY;
(void)netlib_set_ipv4addr(DEVNAME, &newaddr);
(void)netlib_set_ipv4addr(pdhcpc->interface, &newaddr);
/* Loop sending DISCOVER until we receive an OFFER from a DHCP
* server. We will lock on to the first OFFER and decline any
@ -506,7 +506,7 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
* out of the loop.
*/
(void)netlib_set_ipv4addr(DEVNAME, &presult->ipaddr);
(void)netlib_set_ipv4addr(pdhcpc->interface, &presult->ipaddr);
state = STATE_HAVE_OFFER;
}
}
@ -598,7 +598,7 @@ int dhcpc_request(void *handle, struct dhcpc_state *presult)
{
/* An error other than a timeout was received */
(void)netlib_set_ipv4addr(DEVNAME, &oldaddr);
(void)netlib_set_ipv4addr(pdhcpc->interface, &oldaddr);
return ERROR;
}
}

View File

@ -1063,7 +1063,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open("eth0", &mac, IFHWADDRLEN);
/* Get an IP address. Note that there is no logic for renewing the IP
* address in this example. The address should be renewed in

View File

@ -377,7 +377,7 @@ static void nsh_netinit_configure(void)
/* Set up the DHCPC modules */
handle = dhcpc_open(&mac, IFHWADDRLEN);
handle = dhcpc_open(NET_DEVNAME, &mac, IFHWADDRLEN);
/* Get an IP address. Note that there is no logic for renewing the IP address in this
* example. The address should be renewed in ds.lease_time/2 seconds.