]>
Commit | Line | Data |
---|---|---|
f043759d RA |
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" | |
5947046d | 23 | #include "misc.h" |
1304f50a | 24 | #include "utils.h" |
f043759d RA |
25 | |
26 | #define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */ | |
27 | ||
28 | static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) | |
29 | { | |
30 | int *iteration = data; | |
f043759d RA |
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" }; | |
5947046d | 36 | char str[33] = {0,}; |
f043759d RA |
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 | ||
1304f50a | 50 | if (!*iteration && !is_json_context()) |
5947046d JM |
51 | printf("%-10s %-10s %-10s %-8s %-10s %-33s\n", |
52 | "Type", "Lower", "Upper", "Scope", "Port", | |
53 | "Node"); | |
f043759d RA |
54 | (*iteration)++; |
55 | ||
5947046d JM |
56 | hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str); |
57 | ||
1304f50a HL |
58 | open_json_object(NULL); |
59 | print_uint(PRINT_ANY, "type", "%-10u", | |
60 | mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE])); | |
853adffe | 61 | print_string(PRINT_FP, NULL, " ", ""); |
1304f50a HL |
62 | print_uint(PRINT_ANY, "lower", "%-10u", |
63 | mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER])); | |
853adffe | 64 | print_string(PRINT_FP, NULL, " ", ""); |
1304f50a HL |
65 | print_uint(PRINT_ANY, "upper", "%-10u", |
66 | mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER])); | |
853adffe | 67 | print_string(PRINT_FP, NULL, " ", ""); |
1304f50a HL |
68 | print_string(PRINT_ANY, "scope", "%-8s", |
69 | scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); | |
853adffe | 70 | print_string(PRINT_FP, NULL, " ", ""); |
1304f50a HL |
71 | print_uint(PRINT_ANY, "port", "%-10u", |
72 | mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF])); | |
853adffe | 73 | print_string(PRINT_FP, NULL, " ", ""); |
1304f50a HL |
74 | print_string(PRINT_ANY, "node", "%s", str); |
75 | print_string(PRINT_FP, NULL, "\n", ""); | |
76 | close_json_object(); | |
f043759d RA |
77 | |
78 | return MNL_CB_OK; | |
79 | } | |
80 | ||
81 | static 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]; | |
1304f50a | 86 | int rc = 0; |
f043759d RA |
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 | ||
1304f50a HL |
98 | new_json_obj(json); |
99 | rc = msg_dumpit(nlh, nametable_show_cb, &iteration); | |
100 | delete_json_obj(); | |
101 | ||
102 | return rc; | |
f043759d RA |
103 | } |
104 | ||
105 | void 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 | ||
114 | int 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 | } |