rtnl: update rtgenmsg to ifinfomsg when request link info

The previous function use general message and will dump all interfaces'
information. Now update with ifinfomsg so we could get specific interface's
information.

We still could get all interfaces' info if set device to NULL.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
master
Hangbin Liu 2017-10-09 22:31:41 +08:00 committed by Richard Cochran
parent 7e294a4d04
commit 05bba46198
3 changed files with 12 additions and 9 deletions

2
port.c
View File

@ -1512,7 +1512,7 @@ static int port_initialize(struct port *p)
if (p->fda.fd[FD_RTNL] == -1) if (p->fda.fd[FD_RTNL] == -1)
p->fda.fd[FD_RTNL] = rtnl_open(); p->fda.fd[FD_RTNL] = rtnl_open();
if (p->fda.fd[FD_RTNL] >= 0) if (p->fda.fd[FD_RTNL] >= 0)
rtnl_link_query(p->fda.fd[FD_RTNL]); rtnl_link_query(p->fda.fd[FD_RTNL], p->iface->name);
} }
port_nrate_initialize(p); port_nrate_initialize(p);

12
rtnl.c
View File

@ -42,7 +42,7 @@ int rtnl_close(int fd)
return close(fd); return close(fd);
} }
int rtnl_link_query(int fd) int rtnl_link_query(int fd, char *device)
{ {
struct sockaddr_nl sa; struct sockaddr_nl sa;
struct msghdr msg; struct msghdr msg;
@ -51,19 +51,21 @@ int rtnl_link_query(int fd)
struct { struct {
struct nlmsghdr hdr; struct nlmsghdr hdr;
struct rtgenmsg gen; struct ifinfomsg ifm;
} __attribute__((packed)) request; } __attribute__((packed)) request;
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK; sa.nl_family = AF_NETLINK;
memset(&request, 0, sizeof(request)); memset(&request, 0, sizeof(request));
request.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(request.gen)); request.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(request.ifm));
request.hdr.nlmsg_type = RTM_GETLINK; request.hdr.nlmsg_type = RTM_GETLINK;
request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; request.hdr.nlmsg_flags = NLM_F_REQUEST;
request.hdr.nlmsg_seq = 1; request.hdr.nlmsg_seq = 1;
request.hdr.nlmsg_pid = 0; request.hdr.nlmsg_pid = 0;
request.gen.rtgen_family = AF_UNSPEC; request.ifm.ifi_family = AF_UNSPEC;
request.ifm.ifi_index = if_nametoindex(device ? device : "");
request.ifm.ifi_change = 0xffffffff;
iov.iov_base = &request; iov.iov_base = &request;
iov.iov_len = sizeof(request); iov.iov_len = sizeof(request);

3
rtnl.h
View File

@ -32,9 +32,10 @@ int rtnl_close(int fd);
/** /**
* Request the link status from the kernel. * Request the link status from the kernel.
* @param fd A socket obtained via rtnl_open(). * @param fd A socket obtained via rtnl_open().
* @param device Interface name. Request all iface's status if set NULL.
* @return Zero on success, non-zero otherwise. * @return Zero on success, non-zero otherwise.
*/ */
int rtnl_link_query(int fd); int rtnl_link_query(int fd, char *device);
/** /**
* Read kernel messages looking for a link up/down events. * Read kernel messages looking for a link up/down events.