summaryrefslogtreecommitdiffstats
path: root/tipc/tipc.c
blob: 56af052cbbcd58b411825bd32334bdeb3df31c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * tipc.	TIPC utility frontend.
 *
 * Authors:	Richard Alpe <richard.alpe@ericsson.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <linux/tipc_netlink.h>
#include <libmnl/libmnl.h>
#include <errno.h>

#include "mnl_utils.h"
#include "bearer.h"
#include "link.h"
#include "nametable.h"
#include "socket.h"
#include "media.h"
#include "node.h"
#include "peer.h"
#include "cmdl.h"
#include "utils.h"

int help_flag;
int json;
struct mnlu_gen_socket tipc_nlg;

static void about(struct cmdl *cmdl)
{
	fprintf(stderr,
		"Transparent Inter-Process Communication Protocol\n"
		"Usage: %s [OPTIONS] COMMAND [ARGS] ...\n"
		"\n"
		"Options:\n"
		" -h, --help \t\tPrint help for last given command\n"
		" -j, --json \t\tJson format printouts\n"
		" -p, --pretty \t\tpretty print\n"
		"\n"
		"Commands:\n"
		" bearer                - Show or modify bearers\n"
		" link                  - Show or modify links\n"
		" media                 - Show or modify media\n"
		" nametable             - Show nametable\n"
		" node                  - Show or modify node related parameters\n"
		" peer                  - Peer related operations\n"
		" socket                - Show sockets\n",
		cmdl->argv[0]);
}

int main(int argc, char *argv[])
{
	int i;
	int res;
	struct cmdl cmdl;
	const struct cmd cmd = {"tipc", NULL, about};
	struct option long_options[] = {
		{"help", no_argument, 0, 'h'},
		{"json", no_argument, 0, 'j'},
		{"pretty", no_argument, 0, 'p'},
		{0, 0, 0, 0}
	};
	const struct cmd cmds[] = {
		{ "bearer",	cmd_bearer,	cmd_bearer_help},
		{ "link",	cmd_link,	cmd_link_help},
		{ "media",	cmd_media,	cmd_media_help},
		{ "nametable",	cmd_nametable,	cmd_nametable_help},
		{ "node",	cmd_node,	cmd_node_help},
		{ "peer",	cmd_peer,	cmd_peer_help},
		{ "socket",	cmd_socket,	cmd_socket_help},
		{ NULL }
	};

	do {
		int option_index = 0;

		i = getopt_long(argc, argv, "hjp", long_options, &option_index);

		switch (i) {
		case 'h':
			/*
			 * We want the help for the last command, so we flag
			 * here in order to print later.
			 */
			help_flag = 1;
			break;
		case 'j':
			/*
			 * Enable json format printouts
			 */
			json = 1;
			break;
		case 'p':
			/*
			 * Enable json pretty output
			 */
			pretty = 1;
			break;
		case -1:
			/* End of options */
			break;
		default:
			/* Invalid option, error msg is printed by getopts */
			return 1;
		}
	} while (i != -1);

	cmdl.optind = optind;
	cmdl.argc = argc;
	cmdl.argv = argv;

	res = mnlu_gen_socket_open(&tipc_nlg, TIPC_GENL_V2_NAME,
				   TIPC_GENL_V2_VERSION);
	if (res) {
		fprintf(stderr,
			"Unable to get TIPC nl family id (module loaded?)\n");
		return -1;
	}

	res = run_cmd(NULL, &cmd, cmds, &cmdl, &tipc_nlg);
	if (res != 0) {
		mnlu_gen_socket_close(&tipc_nlg);
		return -1;
	}

	mnlu_gen_socket_close(&tipc_nlg);
	return 0;
}