[lxc-devel] [lxd/master] netns_getifaddrs: simplify

brauner on Github lxc-bot at linuxcontainers.org
Sat Oct 20 10:25:30 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181020/04e70630/attachment.bin>
-------------- next part --------------
From bb502aa06cc47a43e3cadfde6c7f57ee19df8bff Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 20 Oct 2018 12:24:49 +0200
Subject: [PATCH] netns_getifaddrs: simplify

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/netns_getifaddrs.c | 46 +++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 26 deletions(-)

diff --git a/shared/netns_getifaddrs.c b/shared/netns_getifaddrs.c
index d6f368baf3..80ab3303da 100644
--- a/shared/netns_getifaddrs.c
+++ b/shared/netns_getifaddrs.c
@@ -330,7 +330,8 @@ static int nl_msg_to_ifaddr(void *pctx, bool *netnsid_aware, struct nlmsghdr *h)
 
 static int __netlink_recv(int fd, unsigned int seq, int type, int af,
 			  __s32 netns_id, bool *netnsid_aware,
-			  int (*cb)(void *ctx, bool *netnsid_aware, struct nlmsghdr *h),
+			  int (*cb)(void *ctx, bool *netnsid_aware,
+				    struct nlmsghdr *h),
 			  void *ctx)
 {
 	char getlink_buf[__NETLINK_ALIGN(sizeof(struct nlmsghdr)) +
@@ -353,41 +354,34 @@ static int __netlink_recv(int fd, unsigned int seq, int type, int af,
 	} u;
 	int r, property, ret;
 
-	if (type == RTM_GETLINK)
+	if (type == RTM_GETLINK) {
+		ifi_msg = (struct ifinfomsg *)__NLMSG_DATA(hdr);
+		ifi_msg->ifi_family = af;
+
 		buf = getlink_buf;
-	else if (type == RTM_GETADDR)
-		buf = getaddr_buf;
-	else
-		return -1;
+		hdr = (struct nlmsghdr *)buf;
+		hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi_msg));
 
-	memset(buf, 0, sizeof(*buf));
-	hdr = (struct nlmsghdr *)buf;
-	if (type == RTM_GETLINK)
-		ifi_msg = (struct ifinfomsg *)__NLMSG_DATA(hdr);
-	else
+		property = IFLA_TARGET_NETNSID;
+	} else if (type == RTM_GETADDR) {
 		ifa_msg = (struct ifaddrmsg *)__NLMSG_DATA(hdr);
+		ifa_msg->ifa_family = af;
 
-	if (type == RTM_GETLINK)
-		hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi_msg));
-	else
+		buf = getaddr_buf;
+		hdr = (struct nlmsghdr *)buf;
 		hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa_msg));
 
+		property = IFA_TARGET_NETNSID;
+	} else {
+		errno = EINVAL;
+		return -1;
+	}
+	memset(buf, 0, sizeof(*buf));
+
 	hdr->nlmsg_type = type;
 	hdr->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
 	hdr->nlmsg_pid = 0;
 	hdr->nlmsg_seq = seq;
-	if (type == RTM_GETLINK)
-		ifi_msg->ifi_family = af;
-	else
-		ifa_msg->ifa_family = af;
-
-	errno = EINVAL;
-	if (type == RTM_GETLINK)
-		property = IFLA_TARGET_NETNSID;
-	else if (type == RTM_GETADDR)
-		property = IFA_TARGET_NETNSID;
-	else
-		return -1;
 
 	if (netns_id >= 0)
 		addattr(hdr, 1024, property, &netns_id, sizeof(netns_id));


More information about the lxc-devel mailing list