Debian Codegraph @

tipc/peer.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * peer.c TIPC peer functionality.
4 *
5 * Authors: Richard Alpe <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include <linux/tipc_netlink.h>
14 #include <linux/tipc.h>
15 #include <linux/genetlink.h>
16
17 #include "cmdl.h"
18 #include "msg.h"
19 #include "misc.h"
20 #include "peer.h"
21
22 static int cmd_peer_rm_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
23 struct cmdl *cmdl, void *data)
24 {
25 char *str;
26 uint32_t addr;
27 struct nlattr *nest;
28
29 if ((cmdl->argc != cmdl->optind + 1) || help_flag) {
30 fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
31 cmdl->argv[0]);
32 return -EINVAL;
33 }
34
35 str = shift_cmdl(cmdl);
36
37 /* First try legacy Z.C.N format, then integer format */
38 addr = str2addr(str);
39 if (!addr)
40 addr = atoi(str);
41 if (!addr)
42 return -1;
43
44 nlh = msg_init(TIPC_NL_PEER_REMOVE);
45 if (!nlh) {
46 fprintf(stderr, "error, message initialisation failed\n");
47 return -1;
48 }
49
50 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
51 mnl_attr_put_u32(nlh, TIPC_NLA_NET_ADDR, addr);
52 mnl_attr_nest_end(nlh, nest);
53
54 return msg_doit(nlh, NULL, NULL);
55 }
56
57 static int cmd_peer_rm_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
58 struct cmdl *cmdl, void *data)
59 {
60 __u8 id[16] = {0,};
61 __u64 *w0 = (__u64 *)&id[0];
62 __u64 *w1 = (__u64 *)&id[8];
63 struct nlattr *nest;
64 char *str;
65
66 if (cmdl->argc != cmdl->optind + 1) {
67 fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
68 cmdl->argv[0]);
69 return -EINVAL;
70 }
71
72 str = shift_cmdl(cmdl);
73 if (str2nodeid(str, id)) {
74 fprintf(stderr, "Invalid node identity\n");
75 return -EINVAL;
76 }
77
78 nlh = msg_init(TIPC_NL_PEER_REMOVE);
79 if (!nlh) {
80 fprintf(stderr, "error, message initialisation failed\n");
81 return -1;
82 }
83
84 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
85 mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0);
86 mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1);
87 mnl_attr_nest_end(nlh, nest);
88
89 return msg_doit(nlh, NULL, NULL);
90 }
91
92 static void cmd_peer_rm_help(struct cmdl *cmdl)
93 {
94 fprintf(stderr, "Usage: %s peer remove PROPERTY\n\n"
95 "PROPERTIES\n"
96 " identity NODEID - Remove peer node identity\n",
97 cmdl->argv[0]);
98 }
99
100 static void cmd_peer_rm_addr_help(struct cmdl *cmdl)
101 {
102 fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
103 cmdl->argv[0]);
104 }
105
106 static void cmd_peer_rm_nodeid_help(struct cmdl *cmdl)
107 {
108 fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
109 cmdl->argv[0]);
110 }
111
112 static int cmd_peer_rm(struct nlmsghdr *nlh, const struct cmd *cmd,
113 struct cmdl *cmdl, void *data)
114 {
115 const struct cmd cmds[] = {
116 { "address", cmd_peer_rm_addr, cmd_peer_rm_addr_help },
117 { "identity", cmd_peer_rm_nodeid, cmd_peer_rm_nodeid_help },
118 { NULL }
119 };
120
121 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
122 }
123
124 void cmd_peer_help(struct cmdl *cmdl)
125 {
126 fprintf(stderr,
127 "Usage: %s peer COMMAND [ARGS] ...\n\n"
128 "COMMANDS\n"
129 " remove - Remove an offline peer node\n",
130 cmdl->argv[0]);
131 }
132
133 int cmd_peer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
134 void *data)
135 {
136 const struct cmd cmds[] = {
137 { "remove", cmd_peer_rm, cmd_peer_rm_help },
138 { NULL }
139 };
140
141 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
142 }