From 884cb12992c823b7b7d57f2b4887117afc7899bc Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 8 Feb 2017 09:36:09 +0100 Subject: iface: Merge duplicate code Call iface_recv() from iface_rtnl_enumerate() instead of open-coding it. Reorder code a bit to avoid duplication. Signed-off-by: Tobias Klauser --- iface.c | 28 +++++++++------------------- iface.h | 2 +- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/iface.c b/iface.c index 83dc377..bcbca49 100644 --- a/iface.c +++ b/iface.c @@ -297,14 +297,12 @@ static int iface_nlmsg_process(const struct nlmsghdr *nlh, size_t len) return 0; } -static int iface_rtnl_enumerate(int sock, uint16_t type, unsigned char family) +static void iface_rtnl_enumerate(int sock, uint16_t type, unsigned char family) { struct { struct nlmsghdr n; struct rtgenmsg r; } req; - ssize_t recvlen; - uint8_t pktbuf[8192]; memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r)); @@ -314,16 +312,11 @@ static int iface_rtnl_enumerate(int sock, uint16_t type, unsigned char family) if (send(sock, &req, req.n.nlmsg_len, 0) < 0) { log_err("Failed to send netlink enumeration message: %s\n", strerror(errno)); - return -1; - } - - if ((recvlen = recv(sock, pktbuf, sizeof(pktbuf), 0)) < 0) { - if (errno != EINTR) - log_err("Failed to receive netlink message: %s\n", strerror(errno)); - return -1; + return; } - return iface_nlmsg_process((const struct nlmsghdr *)pktbuf, recvlen); + if (iface_recv(sock) < 0) + log_err("Failed to enumerate rtnl interfaces: %s\n", strerror(errno)); } void iface_init(int sock, const char *iface, bool ipv6, @@ -335,14 +328,12 @@ void iface_init(int sock, const char *iface, bool ipv6, iface_ifindex = if_nametoindex(iface); /* send RTM_GETADDR request to initially populate the interface list */ - if (iface_rtnl_enumerate(sock, RTM_GETADDR, AF_INET) < 0) - log_err("Failed to enumerate rtnl interfaces: %s\n", strerror(errno)); + iface_rtnl_enumerate(sock, RTM_GETADDR, AF_INET); if (ipv6) - if (iface_rtnl_enumerate(sock, RTM_GETADDR, AF_INET6) < 0) - log_err("Failed to enumerate rtnl interfaces: %s\n", strerror(errno)); + iface_rtnl_enumerate(sock, RTM_GETADDR, AF_INET6); } -void iface_recv(int sock) +int iface_recv(int sock) { ssize_t recvlen; uint8_t pktbuf[8192]; @@ -350,9 +341,8 @@ void iface_recv(int sock) if ((recvlen = recv(sock, pktbuf, sizeof(pktbuf), 0)) < 0) { if (errno != EINTR) log_err("Failed to receive netlink message: %s\n", strerror(errno)); - return; + return -1; } - if (iface_nlmsg_process((const struct nlmsghdr *)pktbuf, recvlen) < 0) - log_warn("Error processing netlink message\n"); + return iface_nlmsg_process((const struct nlmsghdr *)pktbuf, recvlen); } diff --git a/iface.h b/iface.h index 2203e92..a8eb92c 100644 --- a/iface.h +++ b/iface.h @@ -32,7 +32,7 @@ typedef void (*iface_event_handler_t)(enum iface_event_type, unsigned char af, void iface_init(int sock, const char *iface, bool ipv6, iface_event_handler_t event_handler); -void iface_recv(int sock); +int iface_recv(int sock); size_t iface_addr_lookup(unsigned int ifindex, unsigned char family, struct sockaddr_storage *addrs, size_t addrs_size); -- cgit v1.2.3-54-g00ecf