From ea1025ead20a7bdbd80672d37e67a7e7a0b4a836 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Fri, 19 May 2017 14:34:00 -0600 Subject: [PATCH] DHCPC: Remove hard-coded interface device. Now passed as a parameter to dhcpc_open(). --- examples/bridge/bridge_main.c | 2 +- examples/discover/discover_main.c | 2 +- examples/tcpecho/tcpecho_main.c | 2 +- examples/webserver/webserver_main.c | 2 +- examples/xmlrpc/xmlrpc_main.c | 2 +- include/netutils/dhcpc.h | 5 +++-- netutils/dhcpc/Kconfig | 8 -------- netutils/dhcpc/dhcpc.c | 32 ++++++++++++++--------------- nshlib/nsh_netcmds.c | 2 +- nshlib/nsh_netinit.c | 2 +- 10 files changed, 26 insertions(+), 33 deletions(-) diff --git a/examples/bridge/bridge_main.c b/examples/bridge/bridge_main.c index c06e70eed..18d05df0d 100644 --- a/examples/bridge/bridge_main.c +++ b/examples/bridge/bridge_main.c @@ -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. diff --git a/examples/discover/discover_main.c b/examples/discover/discover_main.c index 8f91cc304..ec0f87ab8 100644 --- a/examples/discover/discover_main.c +++ b/examples/discover/discover_main.c @@ -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. diff --git a/examples/tcpecho/tcpecho_main.c b/examples/tcpecho/tcpecho_main.c index 9daf2ac65..be65a2e5e 100644 --- a/examples/tcpecho/tcpecho_main.c +++ b/examples/tcpecho/tcpecho_main.c @@ -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. diff --git a/examples/webserver/webserver_main.c b/examples/webserver/webserver_main.c index 2278b83c5..e1d97a790 100644 --- a/examples/webserver/webserver_main.c +++ b/examples/webserver/webserver_main.c @@ -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. diff --git a/examples/xmlrpc/xmlrpc_main.c b/examples/xmlrpc/xmlrpc_main.c index db431163c..72149fe27 100644 --- a/examples/xmlrpc/xmlrpc_main.c +++ b/examples/xmlrpc/xmlrpc_main.c @@ -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 diff --git a/include/netutils/dhcpc.h b/include/netutils/dhcpc.h index d0c4f1634..b0fb5a1b2 100644 --- a/include/netutils/dhcpc.h +++ b/include/netutils/dhcpc.h @@ -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 diff --git a/netutils/dhcpc/Kconfig b/netutils/dhcpc/Kconfig index 8f7765692..348bbec97 100644 --- a/netutils/dhcpc/Kconfig +++ b/netutils/dhcpc/Kconfig @@ -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 diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c index 3215787c4..9f095192f 100644 --- a/netutils/dhcpc/dhcpc.c +++ b/netutils/dhcpc/dhcpc.c @@ -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; } } diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index 2607b6ede..d80ef1a10 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -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 diff --git a/nshlib/nsh_netinit.c b/nshlib/nsh_netinit.c index 8e98881b0..00a952877 100644 --- a/nshlib/nsh_netinit.c +++ b/nshlib/nsh_netinit.c @@ -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.