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