]>
git.proxmox.com Git - mirror_iproute2.git/blob - tipc/node.c
2 * node.c TIPC node functionality.
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.
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
17 #include <linux/tipc_netlink.h>
18 #include <linux/tipc.h>
19 #include <linux/genetlink.h>
20 #include <libmnl/libmnl.h>
27 static int node_list_cb(const struct nlmsghdr
*nlh
, void *data
)
30 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
31 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
32 struct nlattr
*attrs
[TIPC_NLA_NODE_MAX
+ 1] = {};
34 mnl_attr_parse(nlh
, sizeof(*genl
), parse_attrs
, info
);
35 if (!info
[TIPC_NLA_NODE
])
38 mnl_attr_parse_nested(info
[TIPC_NLA_NODE
], parse_attrs
, attrs
);
39 if (!attrs
[TIPC_NLA_NODE_ADDR
])
42 addr
= mnl_attr_get_u32(attrs
[TIPC_NLA_NODE_ADDR
]);
43 printf("<%u.%u.%u>: ",
48 if (attrs
[TIPC_NLA_NODE_UP
])
56 static int cmd_node_list(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
57 struct cmdl
*cmdl
, void *data
)
59 char buf
[MNL_SOCKET_BUFFER_SIZE
];
62 fprintf(stderr
, "Usage: %s node list\n", cmdl
->argv
[0]);
66 if (!(nlh
= msg_init(buf
, TIPC_NL_NODE_GET
))) {
67 fprintf(stderr
, "error, message initialisation failed\n");
71 return msg_dumpit(nlh
, node_list_cb
, NULL
);
74 static int cmd_node_set_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
75 struct cmdl
*cmdl
, void *data
)
80 char buf
[MNL_SOCKET_BUFFER_SIZE
];
82 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
83 fprintf(stderr
, "Usage: %s node set address ADDRESS\n",
88 str
= shift_cmdl(cmdl
);
93 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
94 fprintf(stderr
, "error, message initialisation failed\n");
98 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
99 mnl_attr_put_u32(nlh
, TIPC_NLA_NET_ADDR
, addr
);
100 mnl_attr_nest_end(nlh
, nest
);
102 return msg_doit(nlh
, NULL
, NULL
);
105 static int cmd_node_get_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
106 struct cmdl
*cmdl
, void *data
)
109 socklen_t sz
= sizeof(struct sockaddr_tipc
);
110 struct sockaddr_tipc addr
;
112 sk
= socket(AF_TIPC
, SOCK_RDM
, 0);
114 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
118 if (getsockname(sk
, (struct sockaddr
*)&addr
, &sz
) < 0) {
119 fprintf(stderr
, "getting TIPC socket address: %s\n",
126 printf("<%u.%u.%u>\n",
127 tipc_zone(addr
.addr
.id
.node
),
128 tipc_cluster(addr
.addr
.id
.node
),
129 tipc_node(addr
.addr
.id
.node
));
134 static int netid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
136 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
137 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
138 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
140 mnl_attr_parse(nlh
, sizeof(*genl
), parse_attrs
, info
);
141 if (!info
[TIPC_NLA_NET
])
144 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
145 if (!attrs
[TIPC_NLA_NET_ID
])
148 printf("%u\n", mnl_attr_get_u32(attrs
[TIPC_NLA_NET_ID
]));
153 static int cmd_node_get_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
154 struct cmdl
*cmdl
, void *data
)
156 char buf
[MNL_SOCKET_BUFFER_SIZE
];
163 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_GET
))) {
164 fprintf(stderr
, "error, message initialisation failed\n");
168 return msg_dumpit(nlh
, netid_get_cb
, NULL
);
171 static int cmd_node_set_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
172 struct cmdl
*cmdl
, void *data
)
175 char buf
[MNL_SOCKET_BUFFER_SIZE
];
183 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
184 fprintf(stderr
, "error, message initialisation failed\n");
188 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
189 fprintf(stderr
, "Usage: %s node set netid NETID\n",
193 netid
= atoi(shift_cmdl(cmdl
));
195 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
196 mnl_attr_put_u32(nlh
, TIPC_NLA_NET_ID
, netid
);
197 mnl_attr_nest_end(nlh
, nest
);
199 return msg_doit(nlh
, NULL
, NULL
);
202 static void cmd_node_set_help(struct cmdl
*cmdl
)
205 "Usage: %s node set PROPERTY\n\n"
207 " address ADDRESS - Set local address\n"
208 " netid NETID - Set local netid\n",
212 static int cmd_node_set(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
213 struct cmdl
*cmdl
, void *data
)
215 const struct cmd cmds
[] = {
216 { "address", cmd_node_set_addr
, NULL
},
217 { "netid", cmd_node_set_netid
, NULL
},
221 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
224 static void cmd_node_get_help(struct cmdl
*cmdl
)
227 "Usage: %s node get PROPERTY\n\n"
229 " address - Get local address\n"
230 " netid - Get local netid\n",
234 static int cmd_node_get(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
235 struct cmdl
*cmdl
, void *data
)
237 const struct cmd cmds
[] = {
238 { "address", cmd_node_get_addr
, NULL
},
239 { "netid", cmd_node_get_netid
, NULL
},
243 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
246 void cmd_node_help(struct cmdl
*cmdl
)
249 "Usage: %s node COMMAND [ARGS] ...\n\n"
251 " list - List remote nodes\n"
252 " get - Get local node parameters\n"
253 " set - Set local node parameters\n",
257 int cmd_node(struct nlmsghdr
*nlh
, const struct cmd
*cmd
, struct cmdl
*cmdl
,
260 const struct cmd cmds
[] = {
261 { "list", cmd_node_list
, NULL
},
262 { "get", cmd_node_get
, cmd_node_get_help
},
263 { "set", cmd_node_set
, cmd_node_set_help
},
267 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);