Skip to contentSkip to conversation

Add JSON output options to 'iw' for scan results

PATCH Superseded Isaev Ruslan 1 participant 1 message Mar 6, 2024 Kbuild

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 Add JSON output options to 'iw' for scan results

v6 changes:
- add universal print func
iw_printf(const char *key, const char *fmt, ...)
- rewrite print_ssid_escaped() as a demo with new concept

void print_ssid_escaped(const uint8_t len, const uint8_t *data) {
	char *escaped_ssid = ssid_escape(len, data);
	iw_printf("SSID", "%s", escaped_ssid);
}

How it works:
text output:
Tabs for text output is based on json depth. For example: ssid is depth 3 or something so 3 tabs will be printed before "SSID: %s"

json output:
iw_printf will print number (without "") or text based on passed format.

For all number formats. Formats: %d %i %p %hu etc will print without quotes If else format is passed iw_print will print it with quotes. To make it work some new functions added.

branch: v5.19

This commit introduces the ability to output scan results from 'iw' in JSON format, similar to the 'ip' utility from the iproute2 package. The addition aims to enhance the tool's usability and scriptability by providing a structured data format option.

Two new command-line options are added:
- '-j': Outputs scan results in compact JSON format.
- '-jj': Outputs scan results in pretty-printed JSON format.

The implementation focuses on modifying the printing functions to format the output as JSON without altering the existing netlink response parsing logic. This patch demonstrates the concept with scan results, showcasing the potential for expanding JSON support to other 'iw' functionalities.

If these changes prove valuable, I am prepared to continue working on extending JSON output support throughout the tool. Feedback is welcome and appreciated.

Thank you.

---
 Makefile           |    2 +-
 iw.c               |   29 +-
 json/a             |  Bin 0 -> 23512 bytes
 json/a.out         |  Bin 0 -> 23512 bytes
 json/json_print.c  |  284 ++++++
 json/json_print.h  |   63 ++
 json/json_writer.c |  298 +++++++
 json/json_writer.h |   76 ++
 json/main.c        |   28 +
 scan.c             | 2111 ++++++++++++++++++++++++++++----------------
 util.c             |  488 +++++++---
 11 files changed, 2492 insertions(+), 887 deletions(-)
 create mode 100755 json/a
 create mode 100755 json/a.out
 create mode 100644 json/json_print.c
 create mode 100644 json/json_print.h
 create mode 100644 json/json_writer.c
 create mode 100644 json/json_writer.h
 create mode 100644 json/main.c
Makefile +1 -1
index 2fb8db8..6cda648 100644
@@ -19,7 +19,7 @@ CFLAGS += -Werror-implicit-function-declaration -Wsign-compare -Wno-unused-param
 CFLAGS += -Wdeclaration-after-statement
 CFLAGS += $(CFLAGS_EVAL)
 
-_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c)))
+_OBJS := $(sort $(patsubst %.c,%.o,$(wildcard *.c) json/json_writer.c json/json_print.c))
 VERSION_OBJS := $(filter-out version.o, $(_OBJS))
 OBJS := $(VERSION_OBJS) version.o
 
iw.c +28 -1
index 35308ba..624aaf2 100644
@@ -24,6 +24,8 @@
 #include "nl80211.h"
 #include "iw.h"
 
+#include "json/json_print.h"
+
 /* libnl 1.x compatibility code */
 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
 static inline struct nl_handle *nl_socket_alloc(void)
@@ -181,7 +183,11 @@ static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
 static void usage_options(void)
 {
 	printf("Options:\n");
-	printf("\t--debug\t\tenable netlink debugging\n");
+	printf(""
+	"\t--debug\t\tenable netlink debugging\n"
+	"\t-j     \t\tjson output\n"
+	"\t-jj    \t\tjson pretty output\n"
+	"");
 }
 
 static const char *argv0;
@@ -592,6 +598,23 @@ int main(int argc, char **argv)
 		argv++;
 	}
 
+	if (argc > 0 && strcmp(*argv, "-j") == 0) {
+		iw_json = 1;
+		new_json_obj(1, 0); //pretty print disabled
+		argc--;
+		argv++;
+	}
+
+	if (argc > 0 && strcmp(*argv, "-jj") == 0) {
+		iw_json = 1;
+		new_json_obj(1, 1); //pretty print enabled
+		argc--;
+		argv++;
+	}
+
+	//init json anyway to track depth
+	if (!iw_json) new_json_obj(1, 0);
+
 	if (argc > 0 && strcmp(*argv, "--version") == 0) {
 		version();
 		return 0;
@@ -648,5 +671,9 @@ int main(int argc, char **argv)
 
 	nl80211_cleanup(&nlstate);
 
+	//close root json object and deinit jsonw
+	if(iw_json){
+		delete_json_obj();
+	}
 	return err;
 }
json/a new file binary +0 -0
new file mode 100755
index 0000000000000000000000000000000000000000..49a62723867de656ae8da6bd7c0c3287986e320a
GIT binary patch
json/a.out new file binary +0 -0
new file mode 100755
index 0000000000000000000000000000000000000000..49a62723867de656ae8da6bd7c0c3287986e320a
GIT binary patch
json/json_print.c new file +284 -0

285 lines not on this page · raw message

json/json_print.h new file +63 -0

63 lines not on this page · raw message

json/json_writer.c new file +298 -0

298 lines not on this page · raw message

json/json_writer.h new file +76 -0

76 lines not on this page · raw message

json/main.c new file +28 -0
new file mode 100644
index 0000000..21d7cf4
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#include "json_print.h"
+
+
+int main() {
+    // init json print without pretty print
+	new_json_obj(1, 0);
+	open_json_object("tx");
+	print_u64(PRINT_JSON, "bytes", NULL, 1);
+	print_u64(PRINT_JSON, "packets", NULL, 1);
+	print_u64(PRINT_JSON, "errors", NULL, 1);
+	print_u64(PRINT_JSON, "dropped", NULL, 1);
+    open_json_array("hobbies", NULL); // create object entry hobbies with array
+    print_string(PRINT_JSON, NULL, "%s", "Reading");
+    print_string(PRINT_JSON, NULL, "%s", "Hiking");
+    print_string(PRINT_JSON, NULL, "%s", "Coding");
+	close_json_array(NULL);
+	close_json_object();
+
+
+    // finish
+    delete_json_obj();
+
+    return 0;
+}
+
scan.c +1362 -749

2,190 lines not on this page · raw message

util.c +352 -136

488 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.42.0

Part of the diff is not shown here. View the raw message.