static void print_flags(long flags)
{
+ open_json_array(PRINT_JSON, "flags");
+
if (flags & IFF_TUN)
- printf(" tun");
+ print_string(PRINT_ANY, NULL, " %s", "tun");
if (flags & IFF_TAP)
- printf(" tap");
+ print_string(PRINT_ANY, NULL, " %s", "tap");
if (!(flags & IFF_NO_PI))
- printf(" pi");
+ print_string(PRINT_ANY, NULL, " %s", "pi");
if (flags & IFF_ONE_QUEUE)
- printf(" one_queue");
+ print_string(PRINT_ANY, NULL, " %s", "one_queue");
if (flags & IFF_VNET_HDR)
- printf(" vnet_hdr");
+ print_string(PRINT_ANY, NULL, " %s", "vnet_hdr");
+
+ if (flags & IFF_PERSIST)
+ print_string(PRINT_ANY, NULL, " %s", "persist");
+
+ if (!(flags & IFF_NOFILTER))
+ print_string(PRINT_ANY, NULL, " %s", "filter");
- flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR);
+ flags &= ~(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
+ IFF_VNET_HDR | IFF_PERSIST | IFF_NOFILTER);
if (flags)
- printf(" UNKNOWN_FLAGS:%lx", flags);
+ print_0xhex(PRINT_ANY, NULL, "%#x", flags);
+
+ close_json_array(PRINT_JSON, NULL);
}
static char *pid_name(pid_t pid)
if (err)
return;
+ open_json_array(PRINT_JSON, "processes");
+
fd_path = globbuf.gl_pathv;
while (*fd_path) {
const char *dev_net_tun = "/dev/net/tun";
!strcmp(name, value)) {
char *pname = pid_name(pid);
- printf(" %s(%d)", pname ? : "<NULL>", pid);
+ print_string(PRINT_ANY, "name",
+ "%s", pname ? : "<NULL>");
+
+ print_uint(PRINT_ANY, "pid",
+ "(%d)", pid);
free(pname);
}
next:
++fd_path;
}
+ close_json_array(PRINT_JSON, NULL);
globfree(&globbuf);
}
return 0;
}
-static int print_tuntap(const struct sockaddr_nl *who,
- struct nlmsghdr *n, void *arg)
+static int print_tuntap(struct nlmsghdr *n, void *arg)
{
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr *tb[IFLA_MAX+1];
if (read_prop(name, "group", &group))
return 0;
- printf("%s:", name);
+ open_json_object(NULL);
+ print_color_string(PRINT_ANY, COLOR_IFNAME,
+ "ifname", "%s:", name);
print_flags(flags);
if (owner != -1)
- printf(" user %ld", owner);
+ print_u64(PRINT_ANY, "user",
+ " user %ld", owner);
if (group != -1)
- printf(" group %ld", group);
- fputc('\n', stdout);
+ print_u64(PRINT_ANY, "group",
+ " group %ld", group);
+
if (show_details) {
- printf("\tAttached to processes:");
+ print_string(PRINT_FP, NULL,
+ "%s\tAttached to processes:", _SL_);
show_processes(name);
- fputc('\n', stdout);
}
+ close_json_object();
+ print_string(PRINT_FP, NULL, "%s", "\n");
return 0;
}
static int do_show(int argc, char **argv)
{
- if (rtnl_wilddump_req_filter_fn(&rth, AF_UNSPEC, RTM_GETLINK,
+ if (rtnl_linkdump_req_filter_fn(&rth, AF_UNSPEC,
tuntap_filter_req) < 0) {
perror("Cannot send dump request\n");
return -1;
}
+ new_json_obj(json);
+
if (rtnl_dump_filter(&rth, print_tuntap, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
return -1;
}
+ delete_json_obj();
+ fflush(stdout);
+
return 0;
}
struct passwd *pw = getpwuid(uid);
if (pw)
- fprintf(f, "user %s ", pw->pw_name);
+ print_string(PRINT_ANY, "user", "user %s ", pw->pw_name);
else
- fprintf(f, "user %u ", uid);
+ print_uint(PRINT_ANY, "user", "user %u ", uid);
}
static void print_group(FILE *f, gid_t gid)
struct group *group = getgrgid(gid);
if (group)
- fprintf(f, "group %s ", group->gr_name);
+ print_string(PRINT_ANY, "group", "group %s ", group->gr_name);
else
- fprintf(f, "group %u ", gid);
+ print_uint(PRINT_ANY, "group", "group %u ", gid);
}
static void print_mq(FILE *f, struct rtattr *tb[])
{
if (!tb[IFLA_TUN_MULTI_QUEUE] ||
- !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE]))
+ !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE])) {
+ if (is_json_context())
+ print_bool(PRINT_JSON, "multi_queue", NULL, false);
return;
+ }
- fprintf(f, "multi_queue ");
+ print_bool(PRINT_ANY, "multi_queue", "multi_queue ", true);
if (tb[IFLA_TUN_NUM_QUEUES]) {
- fprintf(f, "numqueues %u ",
- rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
+ print_uint(PRINT_ANY, "numqueues", "numqueues %u ",
+ rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
}
if (tb[IFLA_TUN_NUM_DISABLED_QUEUES]) {
- fprintf(f, "numdisabled %u ",
- rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
+ print_uint(PRINT_ANY, "numdisabled", "numdisabled %u ",
+ rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
}
}
static void print_onoff(FILE *f, const char *flag, __u8 val)
{
- fprintf(f, "%s %s ", flag, val ? "on" : "off");
+ if (is_json_context())
+ print_bool(PRINT_JSON, flag, NULL, !!val);
+ else
+ fprintf(f, "%s %s ", flag, val ? "on" : "off");
+}
+
+static void print_type(FILE *f, __u8 type)
+{
+ SPRINT_BUF(buf);
+ const char *str = buf;
+
+ if (type == IFF_TUN)
+ str = "tun";
+ else if (type == IFF_TAP)
+ str = "tap";
+ else
+ snprintf(buf, sizeof(buf), "UNKNOWN:%hhu", type);
+
+ print_string(PRINT_ANY, "type", "type %s ", str);
}
static void tun_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
if (!tb)
return;
- if (tb[IFLA_TUN_TYPE]) {
- __u8 type = rta_getattr_u8(tb[IFLA_TUN_TYPE]);
-
- if (type == IFF_TUN)
- fprintf(f, "type tun ");
- else if (type == IFF_TAP)
- fprintf(f, "type tap ");
- else
- fprintf(f, "type UNKNOWN:%hhu ", type);
- }
+ if (tb[IFLA_TUN_TYPE])
+ print_type(f, rta_getattr_u8(tb[IFLA_TUN_TYPE]));
if (tb[IFLA_TUN_PI])
print_onoff(f, "pi", rta_getattr_u8(tb[IFLA_TUN_PI]));