Move nl_sendgetlink() to nl.c.

This commit is contained in:
Nicholas J. Kain 2014-03-14 23:54:21 -04:00
parent 180ee36626
commit e5d9f4acf2
3 changed files with 33 additions and 32 deletions

View File

@ -33,7 +33,6 @@
#include <net/if.h> #include <net/if.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <errno.h> #include <errno.h>
#include <poll.h> #include <poll.h>
@ -138,39 +137,9 @@ void handle_nl_message(struct client_state_t *cs)
} while (ret > 0); } while (ret > 0);
} }
static int nl_sendgetlink(struct client_state_t *cs)
{
char nlbuf[8192];
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_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
nlh->nlmsg_seq = time(NULL);
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
};
retry_sendto:
r = sendto(cs->nlFd, nlbuf, nlh->nlmsg_len, 0,
(struct sockaddr *)&addr, sizeof addr);
if (r < 0) {
if (errno == EINTR)
goto retry_sendto;
else {
log_warning("%s: (%s) netlink sendto socket failed: %s",
client_config.interface, __func__, strerror(errno));
return -1;
}
}
return 0;
}
int nl_getifdata(struct client_state_t *cs) int nl_getifdata(struct client_state_t *cs)
{ {
if (nl_sendgetlink(cs)) if (nl_sendgetlink(cs->nlFd))
return -1; return -1;
for (int pr = 0; !pr;) { for (int pr = 0; !pr;) {

View File

@ -34,6 +34,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h>
#include <errno.h> #include <errno.h>
#include "log.h" #include "log.h"
#include "nl.h" #include "nl.h"
@ -142,6 +143,36 @@ int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid,
return 0; return 0;
} }
int nl_sendgetlink(int fd)
{
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_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
nlh->nlmsg_seq = time(NULL);
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_open(int nltype, int nlgroup, int *nlportid) int nl_open(int nltype, int nlgroup, int *nlportid)
{ {
int fd; int fd;

View File

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