lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Tue,  6 Mar 2018 13:07:18 -0800
From:   Stephen Hemminger <stephen@...workplumber.org>
To:     dsahern@...il.com
Cc:     netdev@...r.kernel.org, Stephen Hemminger <sthemmin@...rosoft.com>,
        Stephen Hemminger <stephen@...workplumber.org>
Subject: [PATCH iproute2-next v2 10/12] tuntap: support JSON output

From: Stephen Hemminger <sthemmin@...rosoft.com>

Signed-off-by: Stephen Hemminger <stephen@...workplumber.org>
---
 ip/iptuntap.c | 57 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 43 insertions(+), 14 deletions(-)

diff --git a/ip/iptuntap.c b/ip/iptuntap.c
index 2f49ff6d9281..6c5a7259d155 100644
--- a/ip/iptuntap.c
+++ b/ip/iptuntap.c
@@ -228,24 +228,35 @@ static int do_del(int argc, char **argv)
 
 static void print_flags(long flags)
 {
+	open_json_array(PRINT_JSON, "flags");
+
 	if (flags & IFF_TUN)
-		printf(" tun");
+		print_string(PRINT_ANY, NULL, " %s", "tun");
 
 	if (flags & IFF_TAP)
-		printf(" tap");
+		print_string(PRINT_ANY, NULL, " %s", "tap");
 
 	if (!(flags & IFF_NO_PI))
-		printf(" pi");
+		print_string(PRINT_ANY, NULL, " %s", "pi");
 
 	if (flags & IFF_ONE_QUEUE)
-		printf(" one_queue");
+		print_string(PRINT_ANY, NULL, " %s", "one_queue");
 
 	if (flags & IFF_VNET_HDR)
-		printf(" vnet_hdr");
+		print_string(PRINT_ANY, NULL, " %s", "vnet_hdr");
+
+	if (flags & IFF_PERSIST)
+		print_string(PRINT_ANY, NULL, " %s", "persist");
+
+	if (!(flags & IFF_NOFILTER))
+		print_string(PRINT_ANY, NULL, " %s", "filter");
 
-	flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR);
+	flags &= ~(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
+		   IFF_VNET_HDR | IFF_PERSIST | IFF_NOFILTER);
 	if (flags)
-		printf(" UNKNOWN_FLAGS:%lx", flags);
+		print_0xhex(PRINT_ANY, NULL, "%#x", flags);
+
+	close_json_array(PRINT_JSON, NULL);
 }
 
 static char *pid_name(pid_t pid)
@@ -288,6 +299,8 @@ static void show_processes(const char *name)
 	if (err)
 		return;
 
+	open_json_array(PRINT_JSON, "processes");
+
 	fd_path = globbuf.gl_pathv;
 	while (*fd_path) {
 		const char *dev_net_tun = "/dev/net/tun";
@@ -334,7 +347,11 @@ static void show_processes(const char *name)
 				   !strcmp(name, value)) {
 				char *pname = pid_name(pid);
 
-				printf(" %s(%d)", pname ? : "<NULL>", pid);
+				print_string(PRINT_ANY, "name",
+					     "%s", pname ? : "<NULL>");
+
+				print_uint(PRINT_ANY, "pid",
+					   "(%d)", pid);
 				free(pname);
 			}
 
@@ -347,6 +364,7 @@ static void show_processes(const char *name)
 next:
 		++fd_path;
 	}
+	close_json_array(PRINT_JSON, NULL);
 
 	globfree(&globbuf);
 }
@@ -417,18 +435,24 @@ static int print_tuntap(const struct sockaddr_nl *who,
 	if (read_prop(name, "group", &group))
 		return 0;
 
-	printf("%s:", name);
+	open_json_object(NULL);
+	print_color_string(PRINT_ANY, COLOR_IFNAME,
+			   "ifname", "%s:", name);
 	print_flags(flags);
 	if (owner != -1)
-		printf(" user %ld", owner);
+		print_uint(PRINT_ANY, "user",
+			   " user %ld", owner);
 	if (group != -1)
-		printf(" group %ld", group);
-	fputc('\n', stdout);
+		print_uint(PRINT_ANY, "group",
+			   " group %ld", group);
+
 	if (show_details) {
-		printf("\tAttached to processes:");
+		print_string(PRINT_FP, NULL,
+			     "%s\tAttached to processes:", _SL_);
 		show_processes(name);
-		fputc('\n', stdout);
 	}
+	close_json_object();
+	print_string(PRINT_FP, NULL, "%s", "\n");
 
 	return 0;
 }
@@ -441,11 +465,16 @@ static int do_show(int argc, char **argv)
 		return -1;
 	}
 
+	new_json_obj(json);
+
 	if (rtnl_dump_filter(&rth, print_tuntap, NULL) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		return -1;
 	}
 
+	delete_json_obj();
+	fflush(stdout);
+
 	return 0;
 }
 
-- 
2.16.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ