NetLib: Add utility functions to support access to the ARP table

This commit is contained in:
Gregory Nutt 2016-02-08 11:44:59 -06:00
parent c8e21ddcd6
commit 3f95e05bc2
7 changed files with 337 additions and 0 deletions

View File

@ -1543,4 +1543,6 @@
* apps/system/i2c: The I2C tool now obeys it OS interfacing: * apps/system/i2c: The I2C tool now obeys it OS interfacing:
it now uses an I2C character driver to access the I2C bus it now uses an I2C character driver to access the I2C bus
(2016-02-02). (2016-02-02).
* apps/netutils/netlib: Add utility functions to support accesses
to the ARP table (2016-02-08).

View File

@ -135,6 +135,16 @@ uint8_t netlib_ipv6netmask2prefix(FAR const uint16_t *mask);
void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct in6_addr *netmask); void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct in6_addr *netmask);
#endif #endif
#ifdef CONFIG_NET_ARP
/* ARP Table Support */
int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr);
int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR uint8_t *macaddr);
int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const uint8_t *macaddr);
#endif
#ifdef CONFIG_NET_ICMPv6_AUTOCONF #ifdef CONFIG_NET_ICMPv6_AUTOCONF
/* ICMPv6 Autoconfiguration */ /* ICMPv6 Autoconfiguration */

View File

@ -49,6 +49,9 @@ ifeq ($(CONFIG_NET_IPv4),y)
CSRCS += netlib_setipv4addr.c netlib_getipv4addr.c CSRCS += netlib_setipv4addr.c netlib_getipv4addr.c
CSRCS += netlib_setdripv4addr.c netlib_setipv4netmask.c CSRCS += netlib_setdripv4addr.c netlib_setipv4netmask.c
CSRCS += netlib_getdripv4addr.c netlib_getipv4netmask.c CSRCS += netlib_getdripv4addr.c netlib_getipv4netmask.c
ifeq ($(CONFIG_NET_ARP),y)
CSRCS += netlib_getarp.c netlib_setarp.c netlib_delarp.c
endif
ifeq ($(CONFIG_NETDB_DNSCLIENT),y) ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += netlib_setipv4dnsaddr.c CSRCS += netlib_setipv4dnsaddr.c
endif endif

View File

@ -0,0 +1,103 @@
/****************************************************************************
* netutils/netlib/netlib_delarp.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <nuttx/config.h>
#if defined(CONFIG_NET_ARP) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <net/if.h>
#include <nuttx/net/arp.h>
#include <apps/netutils/netlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netlib_del_arpmapping
*
* Description:
* Delete the hardware address mapping for the provided protocol address.
*
* Parameters:
* inaddr The IPv4 address to use in the query
*
* Return:
* 0 on success; a negated errno value on failure.
*
****************************************************************************/
int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr)
{
int ret = -EINVAL;
if (inaddr != NULL)
{
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0);
if (sockfd >= 0)
{
struct arpreq req;
memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in));
memset(&req.arp_ha, 0, sizeof(struct sockaddr_in));
ret = ioctl(sockfd, SIOCDARP, (unsigned long)((uintptr_t)&req));
if (ret < 0)
{
ret = -errno;
DEBUGASSERT(ret < 0);
}
close(sockfd);
}
}
return ret;
}
#endif /* CONFIG_NET_ARP && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -0,0 +1,110 @@
/****************************************************************************
* netutils/netlib/netlib_getarp.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <nuttx/config.h>
#if defined(CONFIG_NET_ARP) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <net/if.h>
#include <nuttx/net/arp.h>
#include <apps/netutils/netlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netlib_get_arpmapping
*
* Description:
* Get the current hardware address mapping for the provided protocol
* address from the ARP table.
*
* Parameters:
* inaddr The IPv4 address to use in the query
* macaddr The location to return the mapped Ethernet MAC address
*
* Return:
* 0 on success; a negated errno value on failure.
*
****************************************************************************/
int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR uint8_t *macaddr)
{
int ret = -EINVAL;
if (inaddr != NULL && macaddr != NULL)
{
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0);
if (sockfd >= 0)
{
struct arpreq req;
memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in));
ret = ioctl(sockfd, SIOCGARP, (unsigned long)((uintptr_t)&req));
if (ret < 0)
{
ret = -errno;
DEBUGASSERT(ret < 0);
}
else
{
DEBUGASSERT(req.arp_ha.sa_family == ARPHRD_ETHER);
memcpy(macaddr, req.arp_ha.sa_data, ETHER_ADDR_LEN);
ret = OK;
}
close(sockfd);
}
}
return ret;
}
#endif /* CONFIG_NET_ARP && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -0,0 +1,108 @@
/****************************************************************************
* netutils/netlib/netlib_setarp.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 <nuttx/config.h>
#if defined(CONFIG_NET_ARP) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <net/if.h>
#include <nuttx/net/arp.h>
#include <apps/netutils/netlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netlib_set_arpmapping
*
* Description:
* Add the current hardware address mapping for the provided protocol
* address to the ARP table.
*
* Parameters:
* inaddr The IPv4 address to use in the mapping
* macaddr The Ethernet MAC address to use in the mapping
*
* Return:
* 0 on success; a negated errno value on failure.
*
****************************************************************************/
int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const uint8_t *macaddr)
{
int ret = -EINVAL;
if (inaddr != NULL && macaddr != NULL)
{
int sockfd = socket(PF_INET, NETLIB_SOCK_IOCTL, 0);
if (sockfd >= 0)
{
struct arpreq req;
memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in));
req.arp_ha.sa_family = ARPHRD_ETHER;
memcpy(&req.arp_ha, macaddr,ETHER_ADDR_LEN);
ret = ioctl(sockfd, SIOCSARP, (unsigned long)((uintptr_t)&req));
if (ret < 0)
{
ret = -errno;
DEBUGASSERT(ret < 0);
}
close(sockfd);
}
}
return ret;
}
#endif /* CONFIG_NET_ARP && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -374,6 +374,7 @@ static int dumptrace(void)
{ {
printf("dumptrace: usbtrace_enumerate failed: %d\n", -ret); printf("dumptrace: usbtrace_enumerate failed: %d\n", -ret);
} }
return ret; return ret;
} }
#else #else