Blame tipc/peer.c

Packit Service 3880ab
/*
Packit Service 3880ab
 * peer.c	TIPC peer functionality.
Packit Service 3880ab
 *
Packit Service 3880ab
 *		This program is free software; you can redistribute it and/or
Packit Service 3880ab
 *		modify it under the terms of the GNU General Public License
Packit Service 3880ab
 *		as published by the Free Software Foundation; either version
Packit Service 3880ab
 *		2 of the License, or (at your option) any later version.
Packit Service 3880ab
 *
Packit Service 3880ab
 * Authors:	Richard Alpe <richard.alpe@ericsson.com>
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
#include <stdio.h>
Packit Service 3880ab
#include <stdlib.h>
Packit Service 3880ab
#include <string.h>
Packit Service 3880ab
#include <errno.h>
Packit Service 3880ab
Packit Service 3880ab
#include <linux/tipc_netlink.h>
Packit Service 3880ab
#include <linux/tipc.h>
Packit Service 3880ab
#include <linux/genetlink.h>
Packit Service 3880ab
#include <libmnl/libmnl.h>
Packit Service 3880ab
Packit Service 3880ab
#include "cmdl.h"
Packit Service 3880ab
#include "msg.h"
Packit Service 3880ab
#include "misc.h"
Packit Service 3880ab
#include "peer.h"
Packit Service 3880ab
Packit Service 3880ab
static int cmd_peer_rm_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
Packit Service 3880ab
			    struct cmdl *cmdl, void *data)
Packit Service 3880ab
{
Packit Service 3880ab
	char *str;
Packit Service 3880ab
	uint32_t addr;
Packit Service 3880ab
	struct nlattr *nest;
Packit Service 3880ab
	char buf[MNL_SOCKET_BUFFER_SIZE];
Packit Service 3880ab
Packit Service 3880ab
	if ((cmdl->argc != cmdl->optind + 1) || help_flag) {
Packit Service 3880ab
		fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
Packit Service 3880ab
			cmdl->argv[0]);
Packit Service 3880ab
		return -EINVAL;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	str = shift_cmdl(cmdl);
Packit Service 3880ab
Packit Service 3880ab
	/* First try legacy Z.C.N format, then integer format */
Packit Service 3880ab
	addr = str2addr(str);
Packit Service 3880ab
	if (!addr)
Packit Service 3880ab
		addr = atoi(str);
Packit Service 3880ab
	if (!addr)
Packit Service 3880ab
		return -1;
Packit Service 3880ab
Packit Service 3880ab
	if (!(nlh = msg_init(buf, TIPC_NL_PEER_REMOVE))) {
Packit Service 3880ab
		fprintf(stderr, "error, message initialisation failed\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
Packit Service 3880ab
	mnl_attr_put_u32(nlh, TIPC_NLA_NET_ADDR, addr);
Packit Service 3880ab
	mnl_attr_nest_end(nlh, nest);
Packit Service 3880ab
Packit Service 3880ab
	return msg_doit(nlh, NULL, NULL);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Bot 867fae
static int cmd_peer_rm_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
Packit Bot 867fae
			      struct cmdl *cmdl, void *data)
Packit Bot 867fae
{
Packit Bot 867fae
	char buf[MNL_SOCKET_BUFFER_SIZE];
Packit Bot 867fae
	__u8 id[16] = {0,};
Packit Bot 867fae
	__u64 *w0 = (__u64 *)&id[0];
Packit Bot 867fae
	__u64 *w1 = (__u64 *)&id[8];
Packit Bot 867fae
	struct nlattr *nest;
Packit Bot 867fae
	char *str;
Packit Bot 867fae
Packit Bot 867fae
	if (cmdl->argc != cmdl->optind + 1) {
Packit Bot 867fae
		fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
Packit Bot 867fae
			cmdl->argv[0]);
Packit Bot 867fae
		return -EINVAL;
Packit Bot 867fae
	}
Packit Bot 867fae
Packit Bot 867fae
	str = shift_cmdl(cmdl);
Packit Bot 867fae
	if (str2nodeid(str, id)) {
Packit Bot 867fae
		fprintf(stderr, "Invalid node identity\n");
Packit Bot 867fae
		return -EINVAL;
Packit Bot 867fae
	}
Packit Bot 867fae
Packit Bot 867fae
	nlh = msg_init(buf, TIPC_NL_PEER_REMOVE);
Packit Bot 867fae
	if (!nlh) {
Packit Bot 867fae
		fprintf(stderr, "error, message initialisation failed\n");
Packit Bot 867fae
		return -1;
Packit Bot 867fae
	}
Packit Bot 867fae
Packit Bot 867fae
	nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
Packit Bot 867fae
	mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0);
Packit Bot 867fae
	mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1);
Packit Bot 867fae
	mnl_attr_nest_end(nlh, nest);
Packit Bot 867fae
Packit Bot 867fae
	return msg_doit(nlh, NULL, NULL);
Packit Bot 867fae
}
Packit Bot 867fae
Packit Service 3880ab
static void cmd_peer_rm_help(struct cmdl *cmdl)
Packit Service 3880ab
{
Packit Bot 867fae
	fprintf(stderr, "Usage: %s peer remove PROPERTY\n\n"
Packit Bot 867fae
		"PROPERTIES\n"
Packit Bot 867fae
		" identity NODEID         - Remove peer node identity\n",
Packit Bot 867fae
		cmdl->argv[0]);
Packit Bot 867fae
}
Packit Bot 867fae
Packit Bot 867fae
static void cmd_peer_rm_addr_help(struct cmdl *cmdl)
Packit Bot 867fae
{
Packit Service 3880ab
	fprintf(stderr, "Usage: %s peer remove address ADDRESS\n",
Packit Service 3880ab
		cmdl->argv[0]);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Bot 867fae
static void cmd_peer_rm_nodeid_help(struct cmdl *cmdl)
Packit Bot 867fae
{
Packit Bot 867fae
	fprintf(stderr, "Usage: %s peer remove identity NODEID\n",
Packit Bot 867fae
		cmdl->argv[0]);
Packit Bot 867fae
}
Packit Bot 867fae
Packit Service 3880ab
static int cmd_peer_rm(struct nlmsghdr *nlh, const struct cmd *cmd,
Packit Service 3880ab
			struct cmdl *cmdl, void *data)
Packit Service 3880ab
{
Packit Service 3880ab
	const struct cmd cmds[] = {
Packit Bot 867fae
		{ "address",  cmd_peer_rm_addr,   cmd_peer_rm_addr_help },
Packit Bot 867fae
		{ "identity", cmd_peer_rm_nodeid, cmd_peer_rm_nodeid_help },
Packit Service 3880ab
		{ NULL }
Packit Service 3880ab
	};
Packit Service 3880ab
Packit Service 3880ab
	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
void cmd_peer_help(struct cmdl *cmdl)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Usage: %s peer COMMAND [ARGS] ...\n\n"
Packit Service 3880ab
		"COMMANDS\n"
Packit Service 3880ab
		" remove                - Remove an offline peer node\n",
Packit Service 3880ab
		cmdl->argv[0]);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
int cmd_peer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
Packit Service 3880ab
	     void *data)
Packit Service 3880ab
{
Packit Service 3880ab
	const struct cmd cmds[] = {
Packit Service 3880ab
		{ "remove",	cmd_peer_rm,	cmd_peer_rm_help },
Packit Service 3880ab
		{ NULL }
Packit Service 3880ab
	};
Packit Service 3880ab
Packit Service 3880ab
	return run_cmd(nlh, cmd, cmds, cmdl, NULL);
Packit Service 3880ab
}