Add a nl_sendgetaddr() function.

This commit is contained in:
Nicholas J. Kain 2014-03-15 00:30:39 -04:00
parent e5d9f4acf2
commit 4d1cfe8586
3 changed files with 36 additions and 5 deletions

View File

@ -33,6 +33,7 @@
#include <net/if.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <poll.h>
@ -139,7 +140,7 @@ void handle_nl_message(struct client_state_t *cs)
int nl_getifdata(struct client_state_t *cs)
{
if (nl_sendgetlink(cs->nlFd))
if (nl_sendgetlink(cs->nlFd, time(NULL)))
return -1;
for (int pr = 0; !pr;) {

View File

@ -34,7 +34,6 @@
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include "log.h"
#include "nl.h"
@ -143,7 +142,7 @@ int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid,
return 0;
}
int nl_sendgetlink(int fd)
int nl_sendgetlink(int fd, int seq)
{
char nlbuf[512];
struct nlmsghdr *nlh = (struct nlmsghdr *)nlbuf;
@ -153,7 +152,37 @@ int nl_sendgetlink(int fd)
nlh->nlmsg_len = NLMSG_LENGTH(sizeof (struct rtattr));
nlh->nlmsg_type = RTM_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
nlh->nlmsg_seq = time(NULL);
nlh->nlmsg_seq = seq;
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
};
retry_sendto:
r = sendto(fd, nlbuf, nlh->nlmsg_len, 0,
(struct sockaddr *)&addr, sizeof addr);
if (r < 0) {
if (errno == EINTR)
goto retry_sendto;
else {
log_warning("%s: netlink sendto socket failed: %s",
__func__, strerror(errno));
return -1;
}
}
return 0;
}
int nl_sendgetaddr(int fd, int seq)
{
char nlbuf[512];
struct nlmsghdr *nlh = (struct nlmsghdr *)nlbuf;
ssize_t r;
memset(nlbuf, 0, sizeof nlbuf);
nlh->nlmsg_len = NLMSG_LENGTH(sizeof (struct rtattr));
nlh->nlmsg_type = RTM_GETADDR;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
nlh->nlmsg_seq = seq;
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,

View File

@ -77,7 +77,8 @@ extern ssize_t nl_recv_buf(int fd, char *buf, size_t blen);
typedef int (*nlmsg_foreach_fn)(const struct nlmsghdr *, void *);
extern int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid,
nlmsg_foreach_fn pfn, void *fnarg);
extern int nl_sendgetlink(int fd);
extern int nl_sendgetlink(int fd, int seq);
extern int nl_sendgetaddr(int fd, int seq);
extern int nl_open(int nltype, int nlgroup, int *nlportid);