From e5d9f4acf29cf7c44248cf3f913a2a470286596b Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Fri, 14 Mar 2014 23:54:21 -0400 Subject: [PATCH] Move nl_sendgetlink() to nl.c. --- ndhc/netlink.c | 33 +-------------------------------- ndhc/nl.c | 31 +++++++++++++++++++++++++++++++ ndhc/nl.h | 1 + 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/ndhc/netlink.c b/ndhc/netlink.c index c93b890..79cea95 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include @@ -138,39 +137,9 @@ void handle_nl_message(struct client_state_t *cs) } 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) { - if (nl_sendgetlink(cs)) + if (nl_sendgetlink(cs->nlFd)) return -1; for (int pr = 0; !pr;) { diff --git a/ndhc/nl.c b/ndhc/nl.c index 4c9473e..996bd1f 100644 --- a/ndhc/nl.c +++ b/ndhc/nl.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include "log.h" #include "nl.h" @@ -142,6 +143,36 @@ int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid, 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 fd; diff --git a/ndhc/nl.h b/ndhc/nl.h index 2c5de75..463b7e6 100644 --- a/ndhc/nl.h +++ b/ndhc/nl.h @@ -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 *); 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_open(int nltype, int nlgroup, int *nlportid);