ipmaddr: json and color support

Support printing mulitcast addresses in json and color mode.
Output format is unchanged for normal use.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Stephen Hemminger 2018-03-08 18:02:17 -08:00 committed by David Ahern
parent bea42e6c24
commit 311dca0aa0
1 changed files with 43 additions and 26 deletions

View File

@ -28,6 +28,7 @@
#include "rt_names.h" #include "rt_names.h"
#include "utils.h" #include "utils.h"
#include "ip_common.h" #include "ip_common.h"
#include "json_print.h"
static struct { static struct {
char *dev; char *dev;
@ -193,50 +194,66 @@ static void read_igmp6(struct ma_info **result_p)
static void print_maddr(FILE *fp, struct ma_info *list) static void print_maddr(FILE *fp, struct ma_info *list)
{ {
fprintf(fp, "\t"); print_string(PRINT_FP, NULL, "\t", NULL);
open_json_object(NULL);
if (list->addr.family == AF_PACKET) { if (list->addr.family == AF_PACKET) {
SPRINT_BUF(b1); SPRINT_BUF(b1);
fprintf(fp, "link %s", ll_addr_n2a((unsigned char *)list->addr.data,
list->addr.bytelen, 0, print_string(PRINT_FP, NULL, "link ", NULL);
b1, sizeof(b1))); print_color_string(PRINT_ANY, COLOR_MAC, "link", "%s",
ll_addr_n2a((void *)list->addr.data, list->addr.bytelen,
0, b1, sizeof(b1)));
} else { } else {
switch (list->addr.family) { print_string(PRINT_ANY, "family", "%-5s ",
case AF_INET: family_name(list->addr.family));
fprintf(fp, "inet "); print_color_string(PRINT_ANY, ifa_family_color(list->addr.family),
break; "address", "%s",
case AF_INET6: format_host(list->addr.family,
fprintf(fp, "inet6 "); -1, list->addr.data));
break;
default:
fprintf(fp, "family %d ", list->addr.family);
break;
}
fprintf(fp, "%s",
format_host(list->addr.family,
-1, list->addr.data));
} }
if (list->users != 1) if (list->users != 1)
fprintf(fp, " users %d", list->users); print_uint(PRINT_ANY, "users", " users %u", list->users);
if (list->features) if (list->features)
fprintf(fp, " %s", list->features); print_string(PRINT_ANY, "features", " %s", list->features);
fprintf(fp, "\n");
print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
} }
static void print_mlist(FILE *fp, struct ma_info *list) static void print_mlist(FILE *fp, struct ma_info *list)
{ {
int cur_index = 0; int cur_index = 0;
new_json_obj(json);
for (; list; list = list->next) { for (; list; list = list->next) {
if (oneline) {
if (list->index != cur_index || oneline) {
if (cur_index) {
close_json_array(PRINT_JSON, NULL);
close_json_object();
}
open_json_object(NULL);
print_uint(PRINT_ANY, "ifindex", "%d:", list->index);
print_color_string(PRINT_ANY, COLOR_IFNAME,
"ifname", "\t%s", list->name);
print_string(PRINT_FP, NULL, "%s", _SL_);
cur_index = list->index; cur_index = list->index;
fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_);
} else if (cur_index != list->index) { open_json_array(PRINT_JSON, "maddr");
cur_index = list->index;
fprintf(fp, "%d:\t%s\n", cur_index, list->name);
} }
print_maddr(fp, list); print_maddr(fp, list);
} }
if (cur_index) {
close_json_array(PRINT_JSON, NULL);
close_json_object();
}
delete_json_obj();
} }
static int multiaddr_list(int argc, char **argv) static int multiaddr_list(int argc, char **argv)