batctl: Add generic JSON interface
PATCH v2 1/2 batctl: Add generic JSON interface
To print JSON strings in a generic way a second nla_policy is used, with a name (dict key) and a callback function for each entry corresponding to its formats.
There are multiple callback functions created, most of them are straight forward. The nljson_print_ttflags() needed more logic behind it, because of its dependency with the last_seen_msecs entry and whether local or global table is requested.
Signed-off-by Alexander Sarmanow <[email protected]>
--- backbonetable.c | 2 +- claimtable.c | 2 +- dat_cache.c | 2 +- gateways.c | 2 +- main.h | 2 + mcast_flags.c | 2 +- neighbors.c | 2 +- netlink.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++- netlink.h | 37 ++++++- originators.c | 2 +- transglobal.c | 2 +- translocal.c | 2 +- 12 files changed, 328 insertions(+), 11 deletions(-)
backbonetable.c +1 -1
@@ -98,7 +98,7 @@ static int netlink_print_bla_backbone(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Originator VID last seen (CRC )\n" ,BATADV_CMD_GET_BLA_BACKBONE , - bla_backbone_callback); + bla_backbone_callback, 0); }static struct debug_table_data batctl_debug_table_backbonetable = {
claimtable.c +1 -1
@@ -103,7 +103,7 @@ static int netlink_print_bla_claim(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Client VID Originator [o] (CRC )\n" ,BATADV_CMD_GET_BLA_CLAIM , - bla_claim_callback); + bla_claim_callback, 0); }static struct debug_table_data batctl_debug_table_claimtable = {
dat_cache.c +1 -1
@@ -116,7 +116,7 @@ static int netlink_print_dat_cache(struct state *state, char *orig_iface, ret =netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_DAT_CACHE , - dat_cache_callback); + dat_cache_callback,0 );free (header);return ret;
gateways.c +1 -1
@@ -141,7 +141,7 @@ static int netlink_print_gateways(struct state *state, char *orig_iface, orig_timeout, watch_interval, header,BATADV_CMD_GET_GATEWAYS , - gateways_callback); + gateways_callback, 0); }static struct debug_table_data batctl_debug_table_gateways = {
main.h +2 -0
@@ -44,6 +44,8 @@ extern char module_ver_path[];#define ARRAY_SIZE (x) (sizeof(x) / sizeof(*(x))) +#define BOOL_STRING (x) (x ? "true" : "false") +#ifndef container_of#define container_of (ptr, type, member) __extension__ ({ \ const __typeof__(((type *)0)->member) *__pmember = (ptr); \
mcast_flags.c +1 -1
@@ -147,7 +147,7 @@ static int netlink_print_mcast_flags(struct state *state, char *orig_iface, ret =netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_MCAST_FLAGS , - mcast_flags_callback); + mcast_flags_callback,0 );free (header);return ret;
neighbors.c +1 -1
@@ -114,7 +114,7 @@ static int netlink_print_neighbors(struct state *state, char *orig_iface, orig_timeout, watch_interval,"IF Neighbor last-seen\n" ,BATADV_CMD_GET_NEIGHBORS , - neighbors_callback); + neighbors_callback, 0); }static struct debug_table_data batctl_debug_table_neighbors = {
netlink.c +281 -1
312 lines not on this page · raw message
netlink.h +36 -1
@@ -21,8 +21,22 @@ struct print_opts {float watch_interval;nl_recvmsg_msg_cb_t callback;char *remaining_header; +char *remaining_entry;const char *static_header;uint8_t nl_cmd; +uint8_t is_json; +uint8_t is_first; +}; + +struct json_opts { +uint8_t use_alt_int_val; +int alt_int_val; +}; + +struct nla_policy_json { +const char *name; +void (*cb)(struct json_opts *json_opts,struct print_opts *opts, +struct nlattr *attrs[],int idx); };struct ether_addr ;
@@ -40,13 +54,14 @@ int get_algoname_netlink(const char *mesh_iface, char *algoname, size_t algoname_len);extern struct nla_policy batadv_netlink_policy[]; +extern struct nla_policy_json batadv_netlink_policy_json[];int missing_mandatory_attrs (struct nlattr *attrs[],const int mandatory[],int num);int netlink_print_common (struct state *state,char *orig_iface,int read_opt,float orig_timeout,float watch_interval,const char *header,uint8_t nl_cmd, -nl_recvmsg_msg_cb_t callback); +nl_recvmsg_msg_cb_t callback,uint8_t is_json);int netlink_print_common_cb (struct nl_msg *msg,void *arg);int netlink_stop_callback (struct nl_msg *msg,void *arg);
@@ -54,6 +69,26 @@ int netlink_print_error(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg);void netlink_print_remaining_header (struct print_opts *opts); +void netlink_print_json_entries (struct nlattr *attrs[],int selected_attrs[], +int arr_len,struct print_opts *opts); +void netlink_print_json_entry (struct nlattr *attrs[],int idx); +void nljson_print_str (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_ifname_by_ifindex (struct json_opts *json_opts,struct print_opts *opts, +struct nlattr *attrs[],int idx); +void nljson_print_int (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_unsigned (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx); +void nljson_print_uint8_t (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx); +void nljson_print_vid (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_crc32 (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_mac (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_bool (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_ttflags (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx); +void create_json_print_string (const char *str,const char **str_ret); +void sanitize_string (const char *str,int str_len); +extern char algo_name_buf[256 ];extern int last_err;extern int64_t mcast_flags;
originators.c +1 -1
@@ -192,7 +192,7 @@ static int netlink_print_originators(struct state *state, char *orig_iface,return netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_ORIGINATORS , - originators_callback); + originators_callback,0 ); }static struct debug_table_data batctl_debug_table_originators = {
transglobal.c +1 -1
@@ -132,7 +132,7 @@ static int netlink_print_transglobal(struct state *state, char *orig_iface, orig_timeout, watch_interval," Client VID Flags Last ttvn Via ttvn (CRC )\n" ,BATADV_CMD_GET_TRANSTABLE_GLOBAL , - transglobal_callback); + transglobal_callback, 0); }static struct debug_table_data batctl_debug_table_transglobal = {
translocal.c +1 -1
@@ -128,7 +128,7 @@ static int netlink_print_translocal(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Client VID Flags Last seen (CRC )\n" ,BATADV_CMD_GET_TRANSTABLE_LOCAL , - translocal_callback); + translocal_callback, 0); }static struct debug_table_data batctl_debug_table_translocal = {
Signature · 1 line
2.25.1
Quoted 9 lines · On Friday, 7 May 2021 19:14:14 CEST Alexander Sarmanow wrote:
netlink.h +9
-0+void netlink_print_json_entries (struct nlattr *attrs[],int selected_attrs[], +int arr_len,struct print_opts *opts); +void netlink_print_json_entry (struct nlattr *attrs[],int idx); +void nljson_print_str (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_ifname_by_ifindex (struct json_opts *json_opts,struct print_opts *opts, +struct nlattr *attrs[],int idx); +void nljson_print_int (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_unsigned (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx);
netlink.h int netlink_print_error(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, · line 90
Quoted 10 lines
netlink.h +10
-0+void nljson_print_uint8_t (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx); +void nljson_print_vid (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_crc32 (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_mac (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_bool (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[],int idx); +void nljson_print_ttflags (struct json_opts *json_opts,struct print_opts *opts,struct nlattr *attrs[], +int idx); +void create_json_print_string (const char *str,const char **str_ret); +void sanitize_string (const char *str,int str_len);
Why are you exporting all of these functions? Most should be static inside the c file. I would recommend a new genl_json.c file.
Kind regards,
Sven