diff --git a/ndhc/netlink.c b/ndhc/netlink.c index 79cea95..9d32a1d 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -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;) { diff --git a/ndhc/nl.c b/ndhc/nl.c index 996bd1f..2cb55df 100644 --- a/ndhc/nl.c +++ b/ndhc/nl.c @@ -34,7 +34,6 @@ #include #include #include -#include #include #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, diff --git a/ndhc/nl.h b/ndhc/nl.h index 463b7e6..653caac 100644 --- a/ndhc/nl.h +++ b/ndhc/nl.h @@ -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);