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