iproute2/tipc/nametable.c
<<
>>
Prefs
   1/*
   2 * nametable.c  TIPC nametable functionality.
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Richard Alpe <richard.alpe@ericsson.com>
  10 */
  11
  12#include <stdio.h>
  13#include <errno.h>
  14
  15#include <linux/tipc_netlink.h>
  16#include <linux/tipc.h>
  17#include <linux/genetlink.h>
  18#include <libmnl/libmnl.h>
  19
  20#include "cmdl.h"
  21#include "msg.h"
  22#include "nametable.h"
  23#include "misc.h"
  24#include "utils.h"
  25
  26#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
  27
  28static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
  29{
  30        int *iteration = data;
  31        struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
  32        struct nlattr *info[TIPC_NLA_MAX + 1] = {};
  33        struct nlattr *attrs[TIPC_NLA_NAME_TABLE_MAX + 1] = {};
  34        struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1] = {};
  35        const char *scope[] = { "", "zone", "cluster", "node" };
  36        char str[33] = {0,};
  37
  38        mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
  39        if (!info[TIPC_NLA_NAME_TABLE])
  40                return MNL_CB_ERROR;
  41
  42        mnl_attr_parse_nested(info[TIPC_NLA_NAME_TABLE], parse_attrs, attrs);
  43        if (!attrs[TIPC_NLA_NAME_TABLE_PUBL])
  44                return MNL_CB_ERROR;
  45
  46        mnl_attr_parse_nested(attrs[TIPC_NLA_NAME_TABLE_PUBL], parse_attrs, publ);
  47        if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
  48                return MNL_CB_ERROR;
  49
  50        if (!*iteration && !is_json_context())
  51                printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
  52                       "Type", "Lower", "Upper", "Scope", "Port",
  53                       "Node");
  54        (*iteration)++;
  55
  56        hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
  57
  58        open_json_object(NULL);
  59        print_uint(PRINT_ANY, "type", "%-10u",
  60                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
  61        print_string(PRINT_FP, NULL, " ", "");
  62        print_uint(PRINT_ANY, "lower", "%-10u",
  63                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]));
  64        print_string(PRINT_FP, NULL, " ", "");
  65        print_uint(PRINT_ANY, "upper", "%-10u",
  66                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
  67        print_string(PRINT_FP, NULL, " ", "");
  68        print_string(PRINT_ANY, "scope", "%-8s",
  69                             scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
  70        print_string(PRINT_FP, NULL, " ", "");
  71        print_uint(PRINT_ANY, "port", "%-10u",
  72                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]));
  73        print_string(PRINT_FP, NULL, " ", "");
  74        print_string(PRINT_ANY, "node", "%s", str);
  75        print_string(PRINT_FP, NULL, "\n", "");
  76        close_json_object();
  77
  78        return MNL_CB_OK;
  79}
  80
  81static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
  82                              struct cmdl *cmdl, void *data)
  83{
  84        int iteration = 0;
  85        char buf[MNL_SOCKET_BUFFER_SIZE];
  86        int rc = 0;
  87
  88        if (help_flag) {
  89                fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
  90                return -EINVAL;
  91        }
  92
  93        if (!(nlh = msg_init(buf, TIPC_NL_NAME_TABLE_GET))) {
  94                fprintf(stderr, "error, message initialisation failed\n");
  95                return -1;
  96        }
  97
  98        new_json_obj(json);
  99        rc = msg_dumpit(nlh, nametable_show_cb, &iteration);
 100        delete_json_obj();
 101
 102        return rc;
 103}
 104
 105void cmd_nametable_help(struct cmdl *cmdl)
 106{
 107        fprintf(stderr,
 108                "Usage: %s nametable COMMAND\n\n"
 109                "COMMANDS\n"
 110                " show                  - Show nametable\n",
 111                cmdl->argv[0]);
 112}
 113
 114int cmd_nametable(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 115                  void *data)
 116{
 117        const struct cmd cmds[] = {
 118                { "show",       cmd_nametable_show,     NULL },
 119                { NULL }
 120        };
 121
 122        return run_cmd(nlh, cmd, cmds, cmdl, NULL);
 123}
 124