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
  19#include "cmdl.h"
  20#include "msg.h"
  21#include "nametable.h"
  22#include "misc.h"
  23#include "utils.h"
  24
  25#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
  26
  27static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
  28{
  29        int *iteration = data;
  30        struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
  31        struct nlattr *info[TIPC_NLA_MAX + 1] = {};
  32        struct nlattr *attrs[TIPC_NLA_NAME_TABLE_MAX + 1] = {};
  33        struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1] = {};
  34        const char *scope[] = { "", "zone", "cluster", "node" };
  35        char str[33] = {0,};
  36
  37        mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
  38        if (!info[TIPC_NLA_NAME_TABLE])
  39                return MNL_CB_ERROR;
  40
  41        mnl_attr_parse_nested(info[TIPC_NLA_NAME_TABLE], parse_attrs, attrs);
  42        if (!attrs[TIPC_NLA_NAME_TABLE_PUBL])
  43                return MNL_CB_ERROR;
  44
  45        mnl_attr_parse_nested(attrs[TIPC_NLA_NAME_TABLE_PUBL], parse_attrs, publ);
  46        if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
  47                return MNL_CB_ERROR;
  48
  49        if (!*iteration && !is_json_context())
  50                printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
  51                       "Type", "Lower", "Upper", "Scope", "Port",
  52                       "Node");
  53        (*iteration)++;
  54
  55        hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
  56
  57        open_json_object(NULL);
  58        print_uint(PRINT_ANY, "type", "%-10u",
  59                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
  60        print_string(PRINT_FP, NULL, " ", "");
  61        print_uint(PRINT_ANY, "lower", "%-10u",
  62                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]));
  63        print_string(PRINT_FP, NULL, " ", "");
  64        print_uint(PRINT_ANY, "upper", "%-10u",
  65                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
  66        print_string(PRINT_FP, NULL, " ", "");
  67        print_string(PRINT_ANY, "scope", "%-8s",
  68                             scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
  69        print_string(PRINT_FP, NULL, " ", "");
  70        print_uint(PRINT_ANY, "port", "%-10u",
  71                           mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]));
  72        print_string(PRINT_FP, NULL, " ", "");
  73        print_string(PRINT_ANY, "node", "%s", str);
  74        print_string(PRINT_FP, NULL, "\n", "");
  75        close_json_object();
  76
  77        return MNL_CB_OK;
  78}
  79
  80static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
  81                              struct cmdl *cmdl, void *data)
  82{
  83        int iteration = 0;
  84        int rc = 0;
  85
  86        if (help_flag) {
  87                fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
  88                return -EINVAL;
  89        }
  90
  91        nlh = msg_init(TIPC_NL_NAME_TABLE_GET);
  92        if (!nlh) {
  93                fprintf(stderr, "error, message initialisation failed\n");
  94                return -1;
  95        }
  96
  97        new_json_obj(json);
  98        rc = msg_dumpit(nlh, nametable_show_cb, &iteration);
  99        delete_json_obj();
 100
 101        return rc;
 102}
 103
 104void cmd_nametable_help(struct cmdl *cmdl)
 105{
 106        fprintf(stderr,
 107                "Usage: %s nametable COMMAND\n\n"
 108                "COMMANDS\n"
 109                " show                  - Show nametable\n",
 110                cmdl->argv[0]);
 111}
 112
 113int cmd_nametable(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
 114                  void *data)
 115{
 116        const struct cmd cmds[] = {
 117                { "show",       cmd_nametable_show,     NULL },
 118                { NULL }
 119        };
 120
 121        return run_cmd(nlh, cmd, cmds, cmdl, NULL);
 122}
 123