iproute2/tipc/tipc.c
<<
>>
Prefs
   1/*
   2 * tipc.        TIPC utility frontend.
   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 <getopt.h>
  15#include <unistd.h>
  16#include <linux/tipc_netlink.h>
  17#include <libmnl/libmnl.h>
  18#include <errno.h>
  19
  20#include "mnl_utils.h"
  21#include "bearer.h"
  22#include "link.h"
  23#include "nametable.h"
  24#include "socket.h"
  25#include "media.h"
  26#include "node.h"
  27#include "peer.h"
  28#include "cmdl.h"
  29#include "utils.h"
  30
  31int help_flag;
  32int json;
  33struct mnlu_gen_socket tipc_nlg;
  34
  35static void about(struct cmdl *cmdl)
  36{
  37        fprintf(stderr,
  38                "Transparent Inter-Process Communication Protocol\n"
  39                "Usage: %s [OPTIONS] COMMAND [ARGS] ...\n"
  40                "\n"
  41                "Options:\n"
  42                " -h, --help \t\tPrint help for last given command\n"
  43                " -j, --json \t\tJson format printouts\n"
  44                " -p, --pretty \t\tpretty print\n"
  45                "\n"
  46                "Commands:\n"
  47                " bearer                - Show or modify bearers\n"
  48                " link                  - Show or modify links\n"
  49                " media                 - Show or modify media\n"
  50                " nametable             - Show nametable\n"
  51                " node                  - Show or modify node related parameters\n"
  52                " peer                  - Peer related operations\n"
  53                " socket                - Show sockets\n",
  54                cmdl->argv[0]);
  55}
  56
  57int main(int argc, char *argv[])
  58{
  59        int i;
  60        int res;
  61        struct cmdl cmdl;
  62        const struct cmd cmd = {"tipc", NULL, about};
  63        struct option long_options[] = {
  64                {"help", no_argument, 0, 'h'},
  65                {"json", no_argument, 0, 'j'},
  66                {"pretty", no_argument, 0, 'p'},
  67                {0, 0, 0, 0}
  68        };
  69        const struct cmd cmds[] = {
  70                { "bearer",     cmd_bearer,     cmd_bearer_help},
  71                { "link",       cmd_link,       cmd_link_help},
  72                { "media",      cmd_media,      cmd_media_help},
  73                { "nametable",  cmd_nametable,  cmd_nametable_help},
  74                { "node",       cmd_node,       cmd_node_help},
  75                { "peer",       cmd_peer,       cmd_peer_help},
  76                { "socket",     cmd_socket,     cmd_socket_help},
  77                { NULL }
  78        };
  79
  80        do {
  81                int option_index = 0;
  82
  83                i = getopt_long(argc, argv, "hjp", long_options, &option_index);
  84
  85                switch (i) {
  86                case 'h':
  87                        /*
  88                         * We want the help for the last command, so we flag
  89                         * here in order to print later.
  90                         */
  91                        help_flag = 1;
  92                        break;
  93                case 'j':
  94                        /*
  95                         * Enable json format printouts
  96                         */
  97                        json = 1;
  98                        break;
  99                case 'p':
 100                        /*
 101                         * Enable json pretty output
 102                         */
 103                        pretty = 1;
 104                        break;
 105                case -1:
 106                        /* End of options */
 107                        break;
 108                default:
 109                        /* Invalid option, error msg is printed by getopts */
 110                        return 1;
 111                }
 112        } while (i != -1);
 113
 114        cmdl.optind = optind;
 115        cmdl.argc = argc;
 116        cmdl.argv = argv;
 117
 118        res = mnlu_gen_socket_open(&tipc_nlg, TIPC_GENL_V2_NAME,
 119                                   TIPC_GENL_V2_VERSION);
 120        if (res) {
 121                fprintf(stderr,
 122                        "Unable to get TIPC nl family id (module loaded?)\n");
 123                return -1;
 124        }
 125
 126        res = run_cmd(NULL, &cmd, cmds, &cmdl, &tipc_nlg);
 127        if (res != 0) {
 128                mnlu_gen_socket_close(&tipc_nlg);
 129                return -1;
 130        }
 131
 132        mnlu_gen_socket_close(&tipc_nlg);
 133        return 0;
 134}
 135