]>
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 if (!(sk
= socket(AF_TIPC
, SOCK_RDM
, 0))) {
113 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
117 if (getsockname(sk
, (struct sockaddr
*)&addr
, &sz
) < 0) {
118 fprintf(stderr
, "getting TIPC socket address: %s\n",
125 printf("<%u.%u.%u>\n",
126 tipc_zone(addr
.addr
.id
.node
),
127 tipc_cluster(addr
.addr
.id
.node
),
128 tipc_node(addr
.addr
.id
.node
));
133 static int netid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
135 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
136 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
137 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
139 mnl_attr_parse(nlh
, sizeof(*genl
), parse_attrs
, info
);
140 if (!info
[TIPC_NLA_NET
])
143 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
144 if (!attrs
[TIPC_NLA_NET_ID
])
147 printf("%u\n", mnl_attr_get_u32(attrs
[TIPC_NLA_NET_ID
]));
152 static int cmd_node_get_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
153 struct cmdl
*cmdl
, void *data
)
155 char buf
[MNL_SOCKET_BUFFER_SIZE
];
162 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_GET
))) {
163 fprintf(stderr
, "error, message initialisation failed\n");
167 return msg_dumpit(nlh
, netid_get_cb
, NULL
);
170 static int cmd_node_set_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
171 struct cmdl
*cmdl
, void *data
)
174 char buf
[MNL_SOCKET_BUFFER_SIZE
];
182 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
183 fprintf(stderr
, "error, message initialisation failed\n");
187 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
188 fprintf(stderr
, "Usage: %s node set netid NETID\n",
192 netid
= atoi(shift_cmdl(cmdl
));
194 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
195 mnl_attr_put_u32(nlh
, TIPC_NLA_NET_ID
, netid
);
196 mnl_attr_nest_end(nlh
, nest
);
198 return msg_doit(nlh
, NULL
, NULL
);
201 static void cmd_node_set_help(struct cmdl
*cmdl
)
204 "Usage: %s node set PROPERTY\n\n"
206 " address ADDRESS - Set local address\n"
207 " netid NETID - Set local netid\n",
211 static int cmd_node_set(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
212 struct cmdl
*cmdl
, void *data
)
214 const struct cmd cmds
[] = {
215 { "address", cmd_node_set_addr
, NULL
},
216 { "netid", cmd_node_set_netid
, NULL
},
220 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
223 static void cmd_node_get_help(struct cmdl
*cmdl
)
226 "Usage: %s node get PROPERTY\n\n"
228 " address - Get local address\n"
229 " netid - Get local netid\n",
233 static int cmd_node_get(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
234 struct cmdl
*cmdl
, void *data
)
236 const struct cmd cmds
[] = {
237 { "address", cmd_node_get_addr
, NULL
},
238 { "netid", cmd_node_get_netid
, NULL
},
242 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
245 void cmd_node_help(struct cmdl
*cmdl
)
248 "Usage: %s media COMMAND [ARGS] ...\n\n"
250 " list - List remote nodes\n"
251 " get - Get local node parameters\n"
252 " set - Set local node parameters\n",
256 int cmd_node(struct nlmsghdr
*nlh
, const struct cmd
*cmd
, struct cmdl
*cmdl
,
259 const struct cmd cmds
[] = {
260 { "list", cmd_node_list
, NULL
},
261 { "get", cmd_node_get
, cmd_node_get_help
},
262 { "set", cmd_node_set
, cmd_node_set_help
},
266 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);