Skip to contentSkip to conversation

Add JSON output options to 'iw' for scan results

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

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

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

v8 changes:
- json_print.c rewritten.
- all printf() calls to print iw scan info replaced with new iw_printf()

example text output:

root@ru-fap650-a3:/tmp# ./iw phy0-ap0 scan | head -n 30
00:0c:43:26:60:18:
        ifname: phy0-ap0
        status:
        TSF:
                usec: 278491388966
                dd: 3
                hh: 5
                mm: 21
                ss: 31
        freq: 5180
        beacon_interval_tus: 100
        capability:
                ESS
                Privacy
                ShortPreamble
        capability_raw: 0x0031
        signal: -78.00 dBm
        last_seen_ms_ago: 2540
        bss_probe_resp_data_frame:
                SSID:
                        SSID: tobe52
                Supported rates:
                        rates:
                                6.0*
                                9.0
                                12.0*
                                18.0
                                24.0*
                                36.0
                                48.0
root@ru-fap650-a3:/tmp#

example pretty json output:

root@ru-fap650-a3:/tmp# ./iw -jj phy0-ap0 scan | head -n 30
{
    "00:0c:43:26:60:18": {
        "ifname": "phy0-ap0",
        "status": "",
        "TSF": {
            "usec": 278643858296,
            "dd": 3,
            "hh": 5,
            "mm": 24,
            "ss": 3
        },
        "freq": 5180,
        "beacon_interval_tus": 100,
        "capability": [ "ESS","Privacy","ShortPreamble" ],
        "capability_raw": "0x0031",
        "signal": "-77.00 dBm",
        "last_seen_ms_ago": 2740,
        "bss_probe_resp_data_frame": {
            "SSID": {
                "SSID": "tobe52"
            },
            "Supported rates": {
                "rates": [ "6.0*","9.0","12.0*","18.0","24.0*","36.0","48.0","54.0" ]
            },

v7 changes:
- binaries json/a json/a.out removed

v6 changes:
- add 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                 |   27 +-
 json/Makefile        |   19 +
 json/iw_json_print.c |  221 ++++++++
 json/iw_json_print.h |   35 ++
 json/iw_json_printer |  Bin 0 -> 28112 bytes
 json/json_writer.c   |  298 +++++++++++
 json/json_writer.h   |   76 +++
 json/main.c          |   49 ++
 scan.c               | 1187 ++++++++++++++++++++----------------------
 util.c               |  333 +++++++-----
 11 files changed, 1508 insertions(+), 739 deletions(-)
 create mode 100644 json/Makefile
 create mode 100644 json/iw_json_print.c
 create mode 100644 json/iw_json_print.h
 create mode 100755 json/iw_json_printer
 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..9b73f4b 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/iw_json_print.c))
 VERSION_OBJS := $(filter-out version.o, $(_OBJS))
 OBJS := $(VERSION_OBJS) version.o
 
iw.c +26 -1
index 35308ba..98541fd 100644
@@ -24,6 +24,8 @@
 #include "nl80211.h"
 #include "iw.h"
 
+#include "json/iw_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,21 @@ int main(int argc, char **argv)
 		argv++;
 	}
 
+	if (argc > 0 && strcmp(*argv, "-j") == 0) {
+		json_obj_new(1, 0); //pretty print disabled
+		argc--;
+		argv++;
+	}
+
+	if (argc > 0 && strcmp(*argv, "-jj") == 0) {
+		json_obj_new(1, 1); //pretty print enabled
+		argc--;
+		argv++;
+	}
+
+	//call json_obj_new() anyway to track depth
+	if (!iw_json) json_obj_new(0, 0);
+
 	if (argc > 0 && strcmp(*argv, "--version") == 0) {
 		version();
 		return 0;
@@ -648,5 +669,9 @@ int main(int argc, char **argv)
 
 	nl80211_cleanup(&nlstate);
 
+	//close root json object and deinit jsonw
+	if(iw_json){
+		json_obj_del();
+	}
 	return err;
 }
json/Makefile new file +19 -0
new file mode 100644
index 0000000..d7b0858
@@ -0,0 +1,19 @@
+CC = gcc
+CFLAGS = -Wall -Wextra -Iinclude
+
+SRC_DIR = .
+OBJ_DIR = .
+
+SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
+OBJ_FILES = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
+
+all: iw_json_printer
+
+$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
+	$(CC) $(CFLAGS) -c $< -o $@
+
+iw_json_printer: $(OBJ_FILES)
+	$(CC) $(CFLAGS) $^ -o $@
+
+clean:
+	rm -f $(OBJ_DIR)/*.o iw_json_printer
json/iw_json_print.c new file +221 -0

222 lines not on this page · raw message

json/iw_json_print.h new file +35 -0
new file mode 100644
index 0000000..7542624
@@ -0,0 +1,35 @@
+#ifndef _JSON_PRINT_H_
+#define _JSON_PRINT_H_
+#include "json_writer.h"
+
+extern int iw_json;
+
+json_writer_t *get_json_writer(void);
+
+void json_obj_new(int json, bool is_pretty);
+void json_obj_del(void);
+void json_obj_open(const char *fmt, ...);
+void json_obj_close(void);
+void json_arr_open(const char *fmt, ...);
+void json_arr_close(void);
+
+#define _PRINT_FUNC(type_name, type)         \
+	void json_print_##type_name(const char *key, type value)
+_PRINT_FUNC(u8, uint8_t);
+_PRINT_FUNC(u16, uint16_t);
+_PRINT_FUNC(u32, uint32_t);
+_PRINT_FUNC(u64, uint64_t);
+_PRINT_FUNC(s8, int8_t);
+_PRINT_FUNC(s16, int16_t);
+_PRINT_FUNC(s32, int32_t);
+_PRINT_FUNC(s64, int64_t);
+_PRINT_FUNC(int, int);
+_PRINT_FUNC(float, double);
+_PRINT_FUNC(bool, bool);
+#undef _PRINT_FUNC
+
+void json_print_string(const char *key, const char *fmt, ...);
+void json_print_num(const char *key, const char *fmt, ...);
+void iw_printf(const char *key, const char *fmt, ...);
+
+#endif /* _JSON_PRINT_H_ */
json/iw_json_printer new file binary +0 -0
new file mode 100755
index 0000000000000000000000000000000000000000..9511de64fe0289ce88af5fd74f307d131d04d424
GIT binary patch
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 +49 -0

49 lines not on this page · raw message

scan.c +566 -621

2,022 lines not on this page · raw message

util.c +217 -116

221 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.