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