1/*
2 * ip.c		"ip" 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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 *
12 * Changes:
13 *
14 * Rani Assaf <rani@magic.metawire.com> 980929:	resolve addresses
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <syslog.h>
21#include <fcntl.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <string.h>
25#include <errno.h>
26
27#include "SNAPSHOT.h"
28#include "utils.h"
29#include "ip_common.h"
30
31int preferred_family = AF_UNSPEC;
32int show_stats = 0;
33int resolve_hosts = 0;
34int oneline = 0;
35int timestamp = 0;
36char * _SL_ = NULL;
37char *batch_file = NULL;
38int force = 0;
39struct rtnl_handle rth;
40
41static void usage(void) __attribute__((noreturn));
42
43static void usage(void)
44{
45	fprintf(stderr,
46"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
47"       ip [ -force ] [-batch filename\n"
48"where  OBJECT := { link | addr | route | rule | neigh | tunnel |\n"
49"                   maddr | mroute | monitor | xfrm }\n"
50"       OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] |\n"
51"                    -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
52"                    -o[neline] | -t[imestamp] }\n");
53	exit(-1);
54}
55
56static int do_help(int argc, char **argv)
57{
58	usage();
59}
60
61static const struct cmd {
62	const char *cmd;
63	int (*func)(int argc, char **argv);
64} cmds[] = {
65	{ "addr", 	do_ipaddr },
66	{ "maddr",	do_multiaddr },
67	{ "route",	do_iproute },
68	{ "rule",	do_iprule },
69	{ "neigh",	do_ipneigh },
70	{ "link",	do_iplink },
71	{ "tunnel",	do_iptunnel },
72	{ "monitor",	do_ipmonitor },
73	{ "xfrm",	do_xfrm },
74	{ "mroute",	do_multiroute },
75	{ "help",	do_help },
76	{ 0 }
77};
78
79static int do_cmd(const char *argv0, int argc, char **argv)
80{
81	const struct cmd *c;
82
83	for (c = cmds; c->cmd; ++c)
84		if (strcmp(c->cmd, argv0) == 0)
85			return c->func(argc-1, argv+1);
86
87	fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
88	exit(-1);
89}
90
91static int batch(const char *name)
92{
93	char *line = NULL;
94	size_t len = 0;
95	int ret = 0;
96	int lineno = 0;
97
98	if (name && strcmp(name, "-") != 0) {
99		if (freopen(name, "r", stdin) == NULL) {
100			fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n",
101				name, strerror(errno));
102			return -1;
103		}
104	}
105
106	if (rtnl_open(&rth, 0) < 0) {
107		fprintf(stderr, "Cannot open rtnetlink\n");
108		return -1;
109	}
110
111	while (getcmdline(&line, &len, stdin) != -1) {
112		char *largv[100];
113		int largc;
114
115		largc = makeargs(line, largv, 100);
116		if (largc == 0)
117			continue;	/* blank line */
118
119		if (do_cmd(largv[0], largc, largv)) {
120			fprintf(stderr, "Command failed %s:%d\n", name, lineno);
121			ret = 1;
122			if (!force)
123				break;
124		}
125	}
126	if (line)
127		free(line);
128
129	rtnl_close(&rth);
130	return ret;
131}
132
133
134int main(int argc, char **argv)
135{
136	char *basename;
137
138	basename = strrchr(argv[0], '/');
139	if (basename == NULL)
140		basename = argv[0];
141	else
142		basename++;
143
144	while (argc > 1) {
145		char *opt = argv[1];
146		if (strcmp(opt,"--") == 0) {
147			argc--; argv++;
148			break;
149		}
150		if (opt[0] != '-')
151			break;
152		if (opt[1] == '-')
153			opt++;
154		if (matches(opt, "-family") == 0) {
155			argc--;
156			argv++;
157			if (argc <= 1)
158				usage();
159			if (strcmp(argv[1], "inet") == 0)
160				preferred_family = AF_INET;
161			else if (strcmp(argv[1], "inet6") == 0)
162				preferred_family = AF_INET6;
163			else if (strcmp(argv[1], "dnet") == 0)
164				preferred_family = AF_DECnet;
165			else if (strcmp(argv[1], "link") == 0)
166				preferred_family = AF_PACKET;
167			else if (strcmp(argv[1], "ipx") == 0)
168				preferred_family = AF_IPX;
169			else if (strcmp(argv[1], "help") == 0)
170				usage();
171			else
172				invarg(argv[1], "invalid protocol family");
173		} else if (strcmp(opt, "-4") == 0) {
174			preferred_family = AF_INET;
175		} else if (strcmp(opt, "-6") == 0) {
176			preferred_family = AF_INET6;
177		} else if (strcmp(opt, "-0") == 0) {
178			preferred_family = AF_PACKET;
179		} else if (strcmp(opt, "-I") == 0) {
180			preferred_family = AF_IPX;
181		} else if (strcmp(opt, "-D") == 0) {
182			preferred_family = AF_DECnet;
183		} else if (matches(opt, "-stats") == 0 ||
184			   matches(opt, "-statistics") == 0) {
185			++show_stats;
186		} else if (matches(opt, "-resolve") == 0) {
187			++resolve_hosts;
188		} else if (matches(opt, "-oneline") == 0) {
189			++oneline;
190		} else if (matches(opt, "-timestamp") == 0) {
191			++timestamp;
192#if 0
193		} else if (matches(opt, "-numeric") == 0) {
194			rtnl_names_numeric++;
195#endif
196		} else if (matches(opt, "-Version") == 0) {
197			printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
198			exit(0);
199		} else if (matches(opt, "-force") == 0) {
200			++force;
201		} else if (matches(opt, "-batch") == 0) {
202			argc--;
203			argv++;
204			if (argc <= 1)
205				usage();
206			batch_file = argv[1];
207		} else if (matches(opt, "-help") == 0) {
208			usage();
209		} else {
210			fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
211			exit(-1);
212		}
213		argc--;	argv++;
214	}
215
216	_SL_ = oneline ? "\\" : "\n" ;
217
218	if (batch_file)
219		return batch(batch_file);
220
221	if (rtnl_open(&rth, 0) < 0)
222		exit(1);
223
224	if (strlen(basename) > 2)
225		return do_cmd(basename+2, argc, argv);
226
227	if (argc > 1)
228		return do_cmd(argv[1], argc-1, argv+1);
229
230	rtnl_close(&rth);
231	usage();
232}
233