netutils/netlib/netlib_getroute.c: Add function to retrieve routing tables using netlink.

This commit is contained in:
Gregory Nutt
2019-11-10 12:36:34 -06:00
parent e260ea779c
commit 438777cd38
7 changed files with 411 additions and 77 deletions

View File

@@ -40,11 +40,11 @@
#include <nuttx/config.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <errno.h>
#include <netpacket/netlink.h>
@@ -168,91 +168,93 @@ ssize_t netlib_get_devices(FAR struct netlib_device_s *devlist,
{
/* 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;
}
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 */
/* 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;
}
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).
*/
/* 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;
}
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 */
/* Copy the device list to the caller's buffer */
switch (resp.hdr.nlmsg_type)
{
case NLMSG_DONE:
enddump = true;
break;
switch (resp.hdr.nlmsg_type)
{
case NLMSG_DONE:
enddump = true;
break;
case RTM_NEWLINK:
{
FAR struct ifinfomsg *iface;
FAR struct rtattr *attr;
int len;
case RTM_NEWLINK:
{
FAR struct rtattr *attr;
int len;
/* Decode the response */
/* Decode the response */
iface = NLMSG_DATA(&resp.hdr);
len = resp.hdr.nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
attr = &resp.attr;
len = RTA_PAYLOAD(attr);
for (attr = IFLA_RTA(iface);
RTA_OK(attr, len);
attr = RTA_NEXT(attr, len))
{
switch (attr->rta_type)
{
case IFLA_IFNAME:
if (ncopied < nentries)
{
for (; 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;
FAR struct ifinfomsg *iface = &resp.iface;
devlist[ncopied].ifindex = iface->ifi_index;
#endif
strncpy(devlist[ncopied].ifname,
(FAR char *)RTA_DATA(attr), IFNAMSIZ);
ncopied++;
}
strncpy(devlist[ncopied].ifname,
(FAR char *)RTA_DATA(attr), IFNAMSIZ);
ncopied++;
}
break;
break;
default:
break;
}
}
}
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;
}
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;
}
}
close(fd);
return ncopied;
errout_with_socket:
close(fd);
return ret;