apps/netutils/netlib/netlib_getnbtab.c: Add a utility function that will return a list of all network devices in the UP state.
This commit is contained in:
parent
6bdc814836
commit
e260ea779c
@ -55,6 +55,7 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <nuttx/net/netconfig.h>
|
||||
|
||||
@ -77,7 +78,7 @@
|
||||
# define IPv6_ROUTE_PATH CONFIG_NETLIB_PROCFS_MOUNTPT "/net/route/ipv6"
|
||||
#endif
|
||||
|
||||
/* Using the following defintions, the following socket() arguments should
|
||||
/* Using the following definitions, the following socket() arguments should
|
||||
* provide a valid socket in all configurations:
|
||||
*
|
||||
* ret = socket(NETLIB_SOCK_FAMILY, NETLIB_SOCK_TYPE, NETLIB_SOCK_PROTOCOL);
|
||||
@ -185,6 +186,18 @@ struct netlib_ipv6_route_s
|
||||
#endif
|
||||
#endif /* HAVE_ROUTE_PROCFS */
|
||||
|
||||
#ifdef CONFIG_NETLINK_ROUTE
|
||||
/* Describes one device returned by netlib_get_devices() */
|
||||
|
||||
struct netlib_device_s
|
||||
{
|
||||
#ifdef CONFIG_NETDEV_IFINDEX
|
||||
uint8_t ifindex; /* Interface index */
|
||||
#endif
|
||||
char ifname[IFNAMSIZ]; /* Interface name */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NETUTILS_NETLIB_GENERICURLPARSER
|
||||
struct url_s
|
||||
{
|
||||
@ -206,7 +219,6 @@ struct url_s
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
@ -224,6 +236,13 @@ extern "C"
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NETLINK_ROUTE
|
||||
/* Return a list of all devices */
|
||||
|
||||
ssize_t netlib_get_devices(FAR struct netlib_device_s *devlist,
|
||||
unsigned int nentries, sa_family_t family);
|
||||
#endif
|
||||
|
||||
/* Convert a textual representation of an IP address to a numerical representation.
|
||||
*
|
||||
* This function takes a textual representation of an IP address in
|
||||
|
@ -84,6 +84,12 @@ CSRCS += netlib_getnbtab.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# Device support
|
||||
|
||||
ifeq ($(CONFIG_NETLINK_ROUTE),y)
|
||||
CSRCS += netlib_getdevs.c
|
||||
endif
|
||||
|
||||
# These require TCP support
|
||||
|
||||
ifeq ($(CONFIG_NET_TCP),y)
|
||||
|
@ -109,6 +109,7 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
|
||||
ssize_t nrecvd;
|
||||
ssize_t paysize;
|
||||
ssize_t maxsize;
|
||||
pid_t pid;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
@ -137,9 +138,10 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
|
||||
|
||||
/* Bind the socket so that we can use send() and receive() */
|
||||
|
||||
pid = getpid();
|
||||
addr.nl_family = AF_NETLINK;
|
||||
addr.nl_pad = 0;
|
||||
addr.nl_pid = getpid();
|
||||
addr.nl_pid = pid;
|
||||
addr.nl_groups = RTM_GETNEIGH;
|
||||
|
||||
ret = bind(fd, (FAR const struct sockaddr *)&addr,
|
||||
@ -161,6 +163,7 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
|
||||
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_REQUEST;
|
||||
req.hdr.nlmsg_seq = thiseq;
|
||||
req.hdr.nlmsg_type = RTM_GETNEIGH;
|
||||
req.hdr.nlmsg_pid = pid;
|
||||
req.msg.ndm_family = AF_INET;
|
||||
|
||||
nsent = send(fd, &req, req.hdr.nlmsg_len, 0);
|
||||
|
261
netutils/netlib/netlib_getdevs.c
Normal file
261
netutils/netlib/netlib_getdevs.c
Normal file
@ -0,0 +1,261 @@
|
||||
/****************************************************************************
|
||||
* netutils/netlib/netlib_getdevs.c
|
||||
*
|
||||
* Copyright (C) 2019 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>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <netpacket/netlink.h>
|
||||
|
||||
#include <nuttx/net/arp.h>
|
||||
|
||||
#include "netutils/netlib.h"
|
||||
|
||||
#ifdef CONFIG_NETLINK_ROUTE
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct netlib_sendto_request_s
|
||||
{
|
||||
struct nlmsghdr hdr;
|
||||
struct rtgenmsg gen;
|
||||
};
|
||||
|
||||
struct netlib_recvfrom_response_s
|
||||
{
|
||||
struct nlmsghdr hdr;
|
||||
struct ifinfomsg iface;
|
||||
struct rtattr attr;
|
||||
uint8_t data[IFNAMSIZ]; /* IFLA_IFNAME is the only attribute
|
||||
* supported */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netlib_get_devices
|
||||
*
|
||||
* Description:
|
||||
* Return a list of all active network devices (devices in the DOWN state
|
||||
* are not reported).
|
||||
*
|
||||
* Parameters:
|
||||
* devlist - The location to store the list of devices.
|
||||
* nentries - The size of the provided 'devlist' in number of entries each
|
||||
* of size sizeof(struct netlib_device_s)
|
||||
* family - Address family. See AF_* definitions in sys/socket.h. Use
|
||||
* AF_PACKET to return devices for all address families.
|
||||
*
|
||||
* Return:
|
||||
* The number of device entries read is returned on success; a negated
|
||||
* errno value is returned on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t netlib_get_devices(FAR struct netlib_device_s *devlist,
|
||||
unsigned int nentries, sa_family_t family)
|
||||
{
|
||||
struct netlib_sendto_request_s req;
|
||||
struct netlib_recvfrom_response_s resp;
|
||||
struct sockaddr_nl addr;
|
||||
static unsigned int seqno = 0;
|
||||
unsigned int thiseq;
|
||||
ssize_t nsent;
|
||||
ssize_t nrecvd;
|
||||
size_t ncopied;
|
||||
pid_t pid;
|
||||
bool enddump;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
/* Create a NetLink socket with NETLINK_ROUTE protocol */
|
||||
|
||||
fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
|
||||
if (fd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: socket() failed: %d\n", errcode);
|
||||
return -errcode;
|
||||
}
|
||||
|
||||
/* Bind the socket so that we can use send() and receive() */
|
||||
|
||||
pid = getpid();
|
||||
addr.nl_family = AF_NETLINK;
|
||||
addr.nl_pad = 0;
|
||||
addr.nl_pid = pid;
|
||||
addr.nl_groups = RTM_GETLINK;
|
||||
|
||||
ret = bind(fd, (FAR const struct sockaddr *)&addr,
|
||||
sizeof( struct sockaddr_nl));
|
||||
if (fd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: bind() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Initialize the request */
|
||||
|
||||
thiseq = ++seqno;
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
||||
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
|
||||
req.hdr.nlmsg_seq = thiseq;
|
||||
req.hdr.nlmsg_type = RTM_GETLINK;
|
||||
req.hdr.nlmsg_pid = pid;
|
||||
req.gen.rtgen_family = family;
|
||||
|
||||
nsent = send(fd, &req, req.hdr.nlmsg_len, 0);
|
||||
if (nsent < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: send() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Read the response(s) */
|
||||
|
||||
for (enddump = false; !enddump; )
|
||||
{
|
||||
/* Receive the next device response */
|
||||
|
||||
nrecvd = recv(fd, &resp, sizeof(struct netlib_recvfrom_response_s), 0);
|
||||
if (nrecvd < 0)
|
||||
{
|
||||
int errcode = errno;
|
||||
fprintf(stderr, "ERROR: recv() failed: %d\n", errcode);
|
||||
ret = -errcode;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Verify the data and transfer the device list to the caller */
|
||||
|
||||
if (nrecvd != sizeof(struct netlib_recvfrom_response_s) ||
|
||||
resp.hdr.nlmsg_len < sizeof(struct nlmsghdr) ||
|
||||
resp.hdr.nlmsg_len != nrecvd)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Bad message\n");
|
||||
ret = -EIO;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* The sequence number in the response should match the sequence
|
||||
* number in the request (since we created the socket, this should
|
||||
* always be true).
|
||||
*/
|
||||
|
||||
if (resp.hdr.nlmsg_seq != thiseq)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Bad sequence number in response\n");
|
||||
ret = -EIO;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
/* Copy the device list to the caller's buffer */
|
||||
|
||||
switch (resp.hdr.nlmsg_type)
|
||||
{
|
||||
case NLMSG_DONE:
|
||||
enddump = true;
|
||||
break;
|
||||
|
||||
case RTM_NEWLINK:
|
||||
{
|
||||
FAR struct ifinfomsg *iface;
|
||||
FAR struct rtattr *attr;
|
||||
int len;
|
||||
|
||||
/* Decode the response */
|
||||
|
||||
iface = NLMSG_DATA(&resp.hdr);
|
||||
len = resp.hdr.nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
|
||||
|
||||
for (attr = IFLA_RTA(iface);
|
||||
RTA_OK(attr, len);
|
||||
attr = RTA_NEXT(attr, len))
|
||||
{
|
||||
switch (attr->rta_type)
|
||||
{
|
||||
case IFLA_IFNAME:
|
||||
if (ncopied < nentries)
|
||||
{
|
||||
#ifdef CONFIG_NETDEV_IFINDEX
|
||||
devlist[ncopied].ifindex = iface->ifi_index;
|
||||
#endif
|
||||
strncpy(devlist[ncopied].ifname,
|
||||
(FAR char *)RTA_DATA(attr), IFNAMSIZ);
|
||||
ncopied++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "ERROR: Message type %u, length %lu\n",
|
||||
resp.hdr.nlmsg_type, (unsigned long)resp.hdr.nlmsg_len);
|
||||
ret = -EIO;
|
||||
goto errout_with_socket;
|
||||
}
|
||||
}
|
||||
|
||||
errout_with_socket:
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NETLINK_ROUTE */
|
@ -109,6 +109,7 @@ ssize_t netlib_get_nbtable(FAR struct neighbor_entry_s *nbtab,
|
||||
ssize_t nrecvd;
|
||||
ssize_t paysize;
|
||||
ssize_t maxsize;
|
||||
pid_t pid;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
@ -138,9 +139,10 @@ ssize_t netlib_get_nbtable(FAR struct neighbor_entry_s *nbtab,
|
||||
|
||||
/* Bind the socket so that we can use send() and receive() */
|
||||
|
||||
pid = getpid();
|
||||
addr.nl_family = AF_NETLINK;
|
||||
addr.nl_pad = 0;
|
||||
addr.nl_pid = getpid();
|
||||
addr.nl_pid = pid;
|
||||
addr.nl_groups = RTM_GETNEIGH;
|
||||
|
||||
ret = bind(fd, (FAR const struct sockaddr *)&addr,
|
||||
@ -162,6 +164,7 @@ ssize_t netlib_get_nbtable(FAR struct neighbor_entry_s *nbtab,
|
||||
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_REQUEST;
|
||||
req.hdr.nlmsg_seq = thiseq;
|
||||
req.hdr.nlmsg_type = RTM_GETNEIGH;
|
||||
req.hdr.nlmsg_pid = pid;
|
||||
req.msg.ndm_family = AF_INET6;
|
||||
|
||||
nsent = send(fd, &req, req.hdr.nlmsg_len, 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user