]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/ipmaddr.c
Merge branch 'hdrs-for-dump-req' into iproute2-next
[mirror_iproute2.git] / ip / ipmaddr.c
index 46b86a3a77232d62b11c666d886f0cd1a8a593a5..105b23a8f075a871c31a129a0e44046bd921893a 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
@@ -29,6 +28,7 @@
 #include "rt_names.h"
 #include "utils.h"
 #include "ip_common.h"
+#include "json_print.h"
 
 static struct {
        char *dev;
@@ -194,50 +194,66 @@ static void read_igmp6(struct ma_info **result_p)
 
 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) {
                SPRINT_BUF(b1);
-               fprintf(fp, "link  %s", ll_addr_n2a((unsigned char *)list->addr.data,
-                                                   list->addr.bytelen, 0,
-                                                   b1, sizeof(b1)));
+
+               print_string(PRINT_FP, NULL, "link  ", NULL);
+               print_color_string(PRINT_ANY, COLOR_MAC, "link", "%s",
+                                  ll_addr_n2a((void *)list->addr.data, list->addr.bytelen,
+                                              0, b1, sizeof(b1)));
        } else {
-               switch (list->addr.family) {
-               case AF_INET:
-                       fprintf(fp, "inet  ");
-                       break;
-               case AF_INET6:
-                       fprintf(fp, "inet6 ");
-                       break;
-               default:
-                       fprintf(fp, "family %d ", list->addr.family);
-                       break;
-               }
-               fprintf(fp, "%s",
-                       format_host(list->addr.family,
-                                   -1, list->addr.data));
+               print_string(PRINT_ANY, "family", "%-5s ",
+                            family_name(list->addr.family));
+               print_color_string(PRINT_ANY, ifa_family_color(list->addr.family),
+                                  "address", "%s",
+                                  format_host(list->addr.family,
+                                              -1, list->addr.data));
        }
+
        if (list->users != 1)
-               fprintf(fp, " users %d", list->users);
+               print_uint(PRINT_ANY, "users", " users %u", list->users);
+
        if (list->features)
-               fprintf(fp, " %s", list->features);
-       fprintf(fp, "\n");
+               print_string(PRINT_ANY, "features", " %s", list->features);
+
+       print_string(PRINT_FP, NULL, "\n", NULL);
+       close_json_object();
 }
 
 static void print_mlist(FILE *fp, struct ma_info *list)
 {
        int cur_index = 0;
 
+       new_json_obj(json);
        for (; list; list = list->next) {
-               if (oneline) {
-                       cur_index = list->index;
-                       fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_);
-               } else if (cur_index != list->index) {
+
+               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_nl();
                        cur_index = list->index;
-                       fprintf(fp, "%d:\t%s\n", cur_index, list->name);
+
+                       open_json_array(PRINT_JSON, "maddr");
                }
+
                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)
@@ -273,6 +289,7 @@ static int multiaddr_list(int argc, char **argv)
 static int multiaddr_modify(int cmd, int argc, char **argv)
 {
        struct ifreq ifr = {};
+       int family;
        int fd;
 
        if (cmd == RTM_NEWADDR)
@@ -308,7 +325,17 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
                exit(-1);
        }
 
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       switch (preferred_family) {
+       case AF_INET6:
+       case AF_PACKET:
+       case AF_INET:
+               family = preferred_family;
+               break;
+       default:
+               family = AF_INET;
+       }
+
+       fd = socket(family, SOCK_DGRAM, 0);
        if (fd < 0) {
                perror("Cannot create socket");
                exit(1);