summaryrefslogtreecommitdiff
path: root/iface.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2017-02-08 09:36:09 +0100
committerTobias Klauser <tklauser@distanz.ch>2017-02-08 09:36:09 +0100
commit884cb12992c823b7b7d57f2b4887117afc7899bc (patch)
tree0984a9cea5f97bb54ae8eaf159ff533a4a4b7412 /iface.c
parent66c552de33469f3b2619ed398436addfe843adea (diff)
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 <tklauser@distanz.ch>
Diffstat (limited to 'iface.c')
-rw-r--r--iface.c28
1 files changed, 9 insertions, 19 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);
}