netutils/netlib: Encapsulate the dhcp configuration network code and modify at all calling locations
The DHCP configuration network code is called multiple times, so it is encapsulated Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
This commit is contained in:
parent
054edfc653
commit
6d52a0fbc1
@ -375,6 +375,12 @@ ssize_t netlib_get_route(FAR struct rtentry *rtelist,
|
|||||||
unsigned int nentries, sa_family_t family);
|
unsigned int nentries, sa_family_t family);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETUTILS_DHCPC)
|
||||||
|
/* DHCP */
|
||||||
|
|
||||||
|
int netlib_obtain_ipv4addr(FAR const char *ifname);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
|
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
|
||||||
/* ICMPv6 Autoconfiguration */
|
/* ICMPv6 Autoconfiguration */
|
||||||
|
|
||||||
|
@ -34,15 +34,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
#include <signal.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -51,9 +48,6 @@
|
|||||||
#include <sys/boardctl.h>
|
#include <sys/boardctl.h>
|
||||||
|
|
||||||
#include "netutils/netlib.h"
|
#include "netutils/netlib.h"
|
||||||
#if defined(CONFIG_NETUTILS_DHCPC) || defined(CONFIG_NETINIT_DNS)
|
|
||||||
# include "netutils/dhcpc.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CONFIG_NET_6LOWPAN
|
#ifdef CONFIG_NET_6LOWPAN
|
||||||
# include <nuttx/net/sixlowpan.h>
|
# include <nuttx/net/sixlowpan.h>
|
||||||
@ -627,12 +621,6 @@ static void netinit_set_ipaddrs(void)
|
|||||||
#if defined(NETINIT_HAVE_NETDEV) && !defined(CONFIG_NETINIT_NETLOCAL)
|
#if defined(NETINIT_HAVE_NETDEV) && !defined(CONFIG_NETINIT_NETLOCAL)
|
||||||
static void netinit_net_bringup(void)
|
static void netinit_net_bringup(void)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_NETUTILS_DHCPC
|
|
||||||
uint8_t mac[IFHWADDRLEN];
|
|
||||||
struct dhcpc_state ds;
|
|
||||||
FAR void *handle;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Bring the network up. */
|
/* Bring the network up. */
|
||||||
|
|
||||||
if (netlib_ifup(NET_DEVNAME) < 0)
|
if (netlib_ifup(NET_DEVNAME) < 0)
|
||||||
@ -658,46 +646,10 @@ static void netinit_net_bringup(void)
|
|||||||
#ifdef CONFIG_NETUTILS_DHCPC
|
#ifdef CONFIG_NETUTILS_DHCPC
|
||||||
if (g_use_dhcpc)
|
if (g_use_dhcpc)
|
||||||
{
|
{
|
||||||
/* Get the MAC address of the NIC */
|
if (netlib_obtain_ipv4addr(NET_DEVNAME) < 0)
|
||||||
|
|
||||||
netlib_getmacaddr(NET_DEVNAME, mac);
|
|
||||||
|
|
||||||
/* Set up the DHCPC modules */
|
|
||||||
|
|
||||||
handle = dhcpc_open(NET_DEVNAME, &mac, IFHWADDRLEN);
|
|
||||||
if (handle == NULL)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (dhcpc_request(handle, &ds) == OK)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4addr(NET_DEVNAME, &ds.ipaddr);
|
|
||||||
|
|
||||||
if (ds.netmask.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4netmask(NET_DEVNAME, &ds.netmask);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ds.default_router.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_dripv4addr(NET_DEVNAME, &ds.default_router);
|
|
||||||
}
|
|
||||||
|
|
||||||
# ifdef CONFIG_NETINIT_DNS
|
|
||||||
if (ds.dnsaddr.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
|
|
||||||
dhcpc_close(handle);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -53,6 +53,9 @@ if(CONFIG_NETUTILS_NETLIB)
|
|||||||
if(CONFIG_NET_IPTABLES)
|
if(CONFIG_NET_IPTABLES)
|
||||||
list(APPEND SRCS netlib_iptables.c)
|
list(APPEND SRCS netlib_iptables.c)
|
||||||
endif()
|
endif()
|
||||||
|
if(CONFIG_NETUTILS_DHCPC)
|
||||||
|
list(APPEND SRCS netlib_obtainipv4addr.c)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(CONFIG_NET_IPv6)
|
if(CONFIG_NET_IPv6)
|
||||||
|
@ -53,6 +53,9 @@ endif
|
|||||||
ifeq ($(CONFIG_NET_IPTABLES),y)
|
ifeq ($(CONFIG_NET_IPTABLES),y)
|
||||||
CSRCS += netlib_iptables.c
|
CSRCS += netlib_iptables.c
|
||||||
endif
|
endif
|
||||||
|
ifeq ($(CONFIG_NETUTILS_DHCPC),y)
|
||||||
|
CSRCS += netlib_obtainipv4addr.c
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_NET_IPv6),y)
|
ifeq ($(CONFIG_NET_IPv6),y)
|
||||||
|
181
netutils/netlib/netlib_obtainipv4addr.c
Normal file
181
netutils/netlib/netlib_obtainipv4addr.c
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/netutils/netlib/netlib_obtainipv4addr.c
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership. The
|
||||||
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the
|
||||||
|
* License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <debug.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "netutils/dhcpc.h"
|
||||||
|
#include "netutils/netlib.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: dhcp_setup_result
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Set network connection parameters
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* ifname The name of the interface to use
|
||||||
|
* ds The configure network parameter values
|
||||||
|
*
|
||||||
|
* Return:
|
||||||
|
* 0 on success
|
||||||
|
* Negative integer on failure
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int dhcp_setup_result(FAR const char *ifname,
|
||||||
|
FAR struct dhcpc_state *ds)
|
||||||
|
{
|
||||||
|
int ret = netlib_set_ipv4addr(ifname, &ds->ipaddr);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
nerr("ERROR: set IPv4 address failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ds->netmask.s_addr != 0)
|
||||||
|
{
|
||||||
|
ret = netlib_set_ipv4netmask(ifname, &ds->netmask);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
nerr("ERROR: set IPv4 netmask failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ds->default_router.s_addr != 0)
|
||||||
|
{
|
||||||
|
ret = netlib_set_dripv4addr(ifname, &ds->default_router);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
nerr("ERROR: set default router address failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_NETDB_DNSCLIENT
|
||||||
|
if (ds->dnsaddr.s_addr != 0)
|
||||||
|
{
|
||||||
|
ret = netlib_set_ipv4dnsaddr(&ds->dnsaddr);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
nerr("ERROR: set the DNS server address failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: dhcp_obtain_statefuladdr
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Configure network by dhcp
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* ifname The name of the interface to use
|
||||||
|
*
|
||||||
|
* Return:
|
||||||
|
* 0 on success
|
||||||
|
* Negative integer on failure
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int dhcp_obtain_statefuladdr(FAR const char *ifname)
|
||||||
|
{
|
||||||
|
struct dhcpc_state ds;
|
||||||
|
FAR void *handle;
|
||||||
|
uint8_t mac[IFHWADDRLEN];
|
||||||
|
|
||||||
|
int ret = netlib_getmacaddr(ifname, mac);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
nerr("ERROR: get MAC address failed for '%s' : %d\n", ifname, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set up the DHCPC modules */
|
||||||
|
|
||||||
|
handle = dhcpc_open(ifname, &mac, IFHWADDRLEN);
|
||||||
|
if (handle == NULL)
|
||||||
|
{
|
||||||
|
nerr("ERROR: dhcpc open failed for '%s'\n", ifname);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ret = dhcpc_request(handle, &ds);
|
||||||
|
if (ret == OK)
|
||||||
|
{
|
||||||
|
ret = dhcp_setup_result(ifname, &ds);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nerr("ERROR: dhcpc request failed: %d\n", ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
dhcpc_close(handle);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: netlib_obtain_ipv4addr
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Perform DHCP in an attempt to get the IP
|
||||||
|
* address of the specified network device.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* ifname The name of the interface to use
|
||||||
|
*
|
||||||
|
* Return:
|
||||||
|
* 0 on success
|
||||||
|
* Negative integer on failure
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int netlib_obtain_ipv4addr(FAR const char *ifname)
|
||||||
|
{
|
||||||
|
if (ifname == NULL)
|
||||||
|
{
|
||||||
|
nerr("ERROR: Invalid network card name\n");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dhcp_obtain_statefuladdr(ifname);
|
||||||
|
}
|
@ -90,10 +90,6 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_NETINIT_DHCPC
|
|
||||||
# include "netutils/dhcpc.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "nsh.h"
|
#include "nsh.h"
|
||||||
#include "nsh_console.h"
|
#include "nsh_console.h"
|
||||||
|
|
||||||
@ -579,9 +575,6 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||||||
bool badarg = false;
|
bool badarg = false;
|
||||||
#ifdef HAVE_HWADDR
|
#ifdef HAVE_HWADDR
|
||||||
mac_addr_t macaddr;
|
mac_addr_t macaddr;
|
||||||
#endif
|
|
||||||
#if defined(CONFIG_NETINIT_DHCPC)
|
|
||||||
FAR void *handle;
|
|
||||||
#endif
|
#endif
|
||||||
int ret;
|
int ret;
|
||||||
int mtu = 0;
|
int mtu = 0;
|
||||||
@ -833,7 +826,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||||||
{
|
{
|
||||||
if (hostip != NULL)
|
if (hostip != NULL)
|
||||||
{
|
{
|
||||||
#if defined(CONFIG_NETINIT_DHCPC)
|
#if defined(CONFIG_NETUTILS_DHCPC)
|
||||||
if (strcmp(hostip, "dhcp") == 0)
|
if (strcmp(hostip, "dhcp") == 0)
|
||||||
{
|
{
|
||||||
/* Set DHCP addr */
|
/* Set DHCP addr */
|
||||||
@ -1041,48 +1034,11 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||||||
#endif /* CONFIG_NET_IPv4 */
|
#endif /* CONFIG_NET_IPv4 */
|
||||||
#endif /* CONFIG_NETDB_DNSCLIENT */
|
#endif /* CONFIG_NETDB_DNSCLIENT */
|
||||||
|
|
||||||
#if defined(CONFIG_NETINIT_DHCPC)
|
#if defined(CONFIG_NETUTILS_DHCPC)
|
||||||
/* Get the MAC address of the NIC */
|
|
||||||
|
|
||||||
if (!gip)
|
if (!gip)
|
||||||
{
|
{
|
||||||
netlib_getmacaddr("eth0", macaddr);
|
netlib_obtain_ipv4addr(ifname);
|
||||||
|
|
||||||
/* Set up the DHCPC modules */
|
|
||||||
|
|
||||||
handle = dhcpc_open("eth0", &macaddr, 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (handle != NULL)
|
|
||||||
{
|
|
||||||
struct dhcpc_state ds;
|
|
||||||
|
|
||||||
dhcpc_request(handle, &ds);
|
|
||||||
netlib_set_ipv4addr("eth0", &ds.ipaddr);
|
|
||||||
|
|
||||||
if (ds.netmask.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4netmask("eth0", &ds.netmask);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ds.default_router.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_dripv4addr("eth0", &ds.default_router);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_NETDB_DNSCLIENT
|
|
||||||
if (ds.dnsaddr.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
dhcpc_close(handle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@
|
|||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
|
||||||
#include "netutils/netlib.h"
|
#include "netutils/netlib.h"
|
||||||
#include "netutils/dhcpc.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
@ -56,10 +55,6 @@ static void dhcpc_showusage(FAR const char *progname, int exitcode)
|
|||||||
|
|
||||||
int main(int argc, FAR char *argv[])
|
int main(int argc, FAR char *argv[])
|
||||||
{
|
{
|
||||||
FAR const char *devname;
|
|
||||||
FAR void *handle;
|
|
||||||
uint8_t mac[IFHWADDRLEN];
|
|
||||||
struct dhcpc_state ds;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* One and only one argument is expected: The network device name. */
|
/* One and only one argument is expected: The network device name. */
|
||||||
@ -70,50 +65,12 @@ int main(int argc, FAR char *argv[])
|
|||||||
dhcpc_showusage(argv[0], EXIT_FAILURE);
|
dhcpc_showusage(argv[0], EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
devname = argv[1];
|
ret = netlib_obtain_ipv4addr(argv[1]);
|
||||||
|
|
||||||
/* Get the MAC address of the NIC */
|
|
||||||
|
|
||||||
netlib_getmacaddr(devname, mac);
|
|
||||||
|
|
||||||
/* Set up the DHCPC modules */
|
|
||||||
|
|
||||||
handle = dhcpc_open(devname, &mac, IFHWADDRLEN);
|
|
||||||
if (handle == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "ERROR: dhcpc_open() for '%s' failed\n", devname);
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get an IP address. */
|
|
||||||
|
|
||||||
ret = dhcpc_request(handle, &ds);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
dhcpc_close(handle);
|
fprintf(stderr, "ERROR: netlib_obtain_ipv4addr() failed\n");
|
||||||
fprintf(stderr, "ERROR: dhcpc_request() failed\n");
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Save the addresses that we obtained. */
|
|
||||||
|
|
||||||
netlib_set_ipv4addr(devname, &ds.ipaddr);
|
|
||||||
|
|
||||||
if (ds.netmask.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4netmask(devname, &ds.netmask);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ds.default_router.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_dripv4addr(devname, &ds.default_router);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ds.dnsaddr.s_addr != 0)
|
|
||||||
{
|
|
||||||
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
|
||||||
}
|
|
||||||
|
|
||||||
dhcpc_close(handle);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user