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
27static 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	addr = str2addr(str);
43	if (!addr)
44		return -1;
45
46	if (!(nlh = msg_init(buf, TIPC_NL_PEER_REMOVE))) {
47		fprintf(stderr, "error, message initialisation failed\n");
48		return -1;
49	}
50
51	nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
52	mnl_attr_put_u32(nlh, TIPC_NLA_NET_ADDR, addr);
53	mnl_attr_nest_end(nlh, nest);
54
55	return msg_doit(nlh, NULL, NULL);
56}
57
58static void cmd_peer_rm_help(struct cmdl *cmdl)
59{
60	fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
61		cmdl->argv[0]);
62}
63
64static int cmd_peer_rm(struct nlmsghdr *nlh, const struct cmd *cmd,
65			struct cmdl *cmdl, void *data)
66{
67	const struct cmd cmds[] = {
68		{ "address",	cmd_peer_rm_addr,	cmd_peer_rm_help },
69		{ NULL }
70	};
71
72	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
73}
74
75void cmd_peer_help(struct cmdl *cmdl)
76{
77	fprintf(stderr,
78		"Usage: %s peer COMMAND [ARGS] ...\n\n"
79		"COMMANDS\n"
80		" remove                - Remove an offline peer node\n",
81		cmdl->argv[0]);
82}
83
84int cmd_peer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
85	     void *data)
86{
87	const struct cmd cmds[] = {
88		{ "remove",	cmd_peer_rm,	cmd_peer_rm_help },
89		{ NULL }
90	};
91
92	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
93}
94