]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ip.c
ip: Small corrections of '-tshort' option in usage
[mirror_iproute2.git] / ip / ip.c
CommitLineData
aba5acdf
SH
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>
aba5acdf
SH
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <string.h>
351efcde 20#include <errno.h>
aba5acdf
SH
21
22#include "SNAPSHOT.h"
23#include "utils.h"
24#include "ip_common.h"
52700d40 25#include "namespace.h"
aba5acdf
SH
26
27int preferred_family = AF_UNSPEC;
b68d9837 28int human_readable = 0;
1e264abc 29int use_iec = 0;
aba5acdf 30int show_stats = 0;
1d934839 31int show_details = 0;
aba5acdf
SH
32int resolve_hosts = 0;
33int oneline = 0;
90f93024 34int timestamp = 0;
aba5acdf 35char * _SL_ = NULL;
351efcde 36int force = 0;
64c79560 37int max_flush_loops = 10;
a3aa47a5 38int batch_mode = 0;
64c79560 39
3bfa73ff 40struct rtnl_handle rth = { .fd = -1 };
aba5acdf
SH
41
42static void usage(void) __attribute__((noreturn));
43
44static void usage(void)
45{
46 fprintf(stderr,
47"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
10494d27 48" ip [ -force ] -batch filename\n"
4759758c 49"where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
0dc34c77 50" tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
6928747b 51" netns | l2tp | fou | tcp_metrics | token | netconf }\n"
1d934839 52" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
1e264abc 53" -h[uman-readable] | -iec |\n"
27bca615 54" -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
f4809174 55" -4 | -6 | -I | -D | -B | -0 |\n"
64c79560 56" -l[oops] { maximum-addr-flush-attempts } |\n"
a925535c 57" -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
52700d40 58" -rc[vbuf] [size] | -n[etns] name }\n");
aba5acdf
SH
59 exit(-1);
60}
61
fc57a9df
SH
62static int do_help(int argc, char **argv)
63{
64 usage();
ecf52428 65 return 0;
fc57a9df
SH
66}
67
351efcde
SH
68static const struct cmd {
69 const char *cmd;
70 int (*func)(int argc, char **argv);
71} cmds[] = {
4806867a 72 { "address", do_ipaddr },
4759758c 73 { "addrlabel", do_ipaddrlabel },
ede72396 74 { "maddress", do_multiaddr },
351efcde
SH
75 { "route", do_iproute },
76 { "rule", do_iprule },
ede72396
SH
77 { "neighbor", do_ipneigh },
78 { "neighbour", do_ipneigh },
09954dc6
SH
79 { "ntable", do_ipntable },
80 { "ntbl", do_ipntable },
351efcde 81 { "link", do_iplink },
5aa08f6b 82 { "l2tp", do_ipl2tp },
6928747b 83 { "fou", do_ipfou },
351efcde 84 { "tunnel", do_iptunnel },
ede72396 85 { "tunl", do_iptunnel },
580fbd88
DW
86 { "tuntap", do_iptuntap },
87 { "tap", do_iptuntap },
191b60bd 88 { "token", do_iptoken },
ea63a69b
JA
89 { "tcpmetrics", do_tcp_metrics },
90 { "tcp_metrics",do_tcp_metrics },
351efcde
SH
91 { "monitor", do_ipmonitor },
92 { "xfrm", do_xfrm },
84616f83 93 { "mroute", do_multiroute },
b6c8e808 94 { "mrule", do_multirule },
0dc34c77 95 { "netns", do_netns },
9d0efc10 96 { "netconf", do_ipnetconf },
fc57a9df 97 { "help", do_help },
351efcde
SH
98 { 0 }
99};
100
101static int do_cmd(const char *argv0, int argc, char **argv)
102{
e25d6970 103 const struct cmd *c;
351efcde 104
ede72396 105 for (c = cmds; c->cmd; ++c) {
7397944d
SH
106 if (matches(argv0, c->cmd) == 0) {
107 return -(c->func(argc-1, argv+1));
108 }
ede72396
SH
109 }
110
351efcde 111 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
7397944d 112 return EXIT_FAILURE;
351efcde
SH
113}
114
115static int batch(const char *name)
116{
117 char *line = NULL;
118 size_t len = 0;
7397944d 119 int ret = EXIT_SUCCESS;
351efcde 120
a3aa47a5
SH
121 batch_mode = 1;
122
351efcde
SH
123 if (name && strcmp(name, "-") != 0) {
124 if (freopen(name, "r", stdin) == NULL) {
692e0757 125 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
351efcde 126 name, strerror(errno));
7397944d 127 return EXIT_FAILURE;
351efcde
SH
128 }
129 }
130
131 if (rtnl_open(&rth, 0) < 0) {
132 fprintf(stderr, "Cannot open rtnetlink\n");
7397944d 133 return EXIT_FAILURE;
351efcde
SH
134 }
135
3e4f6a38 136 cmdlineno = 0;
351efcde
SH
137 while (getcmdline(&line, &len, stdin) != -1) {
138 char *largv[100];
139 int largc;
140
141 largc = makeargs(line, largv, 100);
142 if (largc == 0)
143 continue; /* blank line */
144
145 if (do_cmd(largv[0], largc, largv)) {
3e4f6a38 146 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
7397944d 147 ret = EXIT_FAILURE;
351efcde
SH
148 if (!force)
149 break;
150 }
151 }
8ed63ab1
SH
152 if (line)
153 free(line);
351efcde
SH
154
155 rtnl_close(&rth);
156 return ret;
157}
158
159
aba5acdf
SH
160int main(int argc, char **argv)
161{
162 char *basename;
a3aa47a5 163 char *batch_file = NULL;
aba5acdf
SH
164
165 basename = strrchr(argv[0], '/');
166 if (basename == NULL)
167 basename = argv[0];
168 else
169 basename++;
ae665a52 170
aba5acdf
SH
171 while (argc > 1) {
172 char *opt = argv[1];
173 if (strcmp(opt,"--") == 0) {
174 argc--; argv++;
175 break;
176 }
177 if (opt[0] != '-')
178 break;
179 if (opt[1] == '-')
180 opt++;
64c79560
BG
181 if (matches(opt, "-loops") == 0) {
182 argc--;
183 argv++;
184 if (argc <= 1)
185 usage();
186 max_flush_loops = atoi(argv[1]);
187 } else if (matches(opt, "-family") == 0) {
aba5acdf
SH
188 argc--;
189 argv++;
190 if (argc <= 1)
191 usage();
192 if (strcmp(argv[1], "inet") == 0)
193 preferred_family = AF_INET;
194 else if (strcmp(argv[1], "inet6") == 0)
195 preferred_family = AF_INET6;
196 else if (strcmp(argv[1], "dnet") == 0)
197 preferred_family = AF_DECnet;
198 else if (strcmp(argv[1], "link") == 0)
199 preferred_family = AF_PACKET;
200 else if (strcmp(argv[1], "ipx") == 0)
201 preferred_family = AF_IPX;
27bca615
SH
202 else if (strcmp(argv[1], "bridge") == 0)
203 preferred_family = AF_BRIDGE;
aba5acdf
SH
204 else if (strcmp(argv[1], "help") == 0)
205 usage();
206 else
f1675d61 207 invarg("invalid protocol family", argv[1]);
aba5acdf
SH
208 } else if (strcmp(opt, "-4") == 0) {
209 preferred_family = AF_INET;
210 } else if (strcmp(opt, "-6") == 0) {
211 preferred_family = AF_INET6;
212 } else if (strcmp(opt, "-0") == 0) {
213 preferred_family = AF_PACKET;
214 } else if (strcmp(opt, "-I") == 0) {
215 preferred_family = AF_IPX;
216 } else if (strcmp(opt, "-D") == 0) {
217 preferred_family = AF_DECnet;
27bca615
SH
218 } else if (strcmp(opt, "-B") == 0) {
219 preferred_family = AF_BRIDGE;
b68d9837
CH
220 } else if (matches(opt, "-human") == 0 ||
221 matches(opt, "-human-readable") == 0) {
222 ++human_readable;
1e264abc
SH
223 } else if (matches(opt, "-iec") == 0) {
224 ++use_iec;
aba5acdf
SH
225 } else if (matches(opt, "-stats") == 0 ||
226 matches(opt, "-statistics") == 0) {
227 ++show_stats;
1d934839
PM
228 } else if (matches(opt, "-details") == 0) {
229 ++show_details;
aba5acdf
SH
230 } else if (matches(opt, "-resolve") == 0) {
231 ++resolve_hosts;
232 } else if (matches(opt, "-oneline") == 0) {
233 ++oneline;
90f93024
SH
234 } else if (matches(opt, "-timestamp") == 0) {
235 ++timestamp;
79aa79d0
VK
236 } else if (matches(opt, "-tshort") == 0) {
237 ++timestamp;
238 ++timestamp_short;
aba5acdf
SH
239#if 0
240 } else if (matches(opt, "-numeric") == 0) {
241 rtnl_names_numeric++;
242#endif
243 } else if (matches(opt, "-Version") == 0) {
244 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
245 exit(0);
351efcde
SH
246 } else if (matches(opt, "-force") == 0) {
247 ++force;
248 } else if (matches(opt, "-batch") == 0) {
249 argc--;
250 argv++;
251 if (argc <= 1)
252 usage();
253 batch_file = argv[1];
7f03191f
PM
254 } else if (matches(opt, "-rcvbuf") == 0) {
255 unsigned int size;
256
257 argc--;
258 argv++;
259 if (argc <= 1)
260 usage();
261 if (get_unsigned(&size, argv[1], 0)) {
262 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
263 argv[1]);
264 exit(-1);
265 }
266 rcvbuf = size;
aba5acdf
SH
267 } else if (matches(opt, "-help") == 0) {
268 usage();
52700d40
VK
269 } else if (matches(opt, "-netns") == 0) {
270 NEXT_ARG();
271 if (netns_switch(argv[1]))
272 exit(-1);
aba5acdf
SH
273 } else {
274 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
275 exit(-1);
276 }
277 argc--; argv++;
278 }
279
280 _SL_ = oneline ? "\\" : "\n" ;
281
ae665a52 282 if (batch_file)
351efcde 283 return batch(batch_file);
ae665a52 284
351efcde
SH
285 if (rtnl_open(&rth, 0) < 0)
286 exit(1);
287
ae665a52 288 if (strlen(basename) > 2)
351efcde
SH
289 return do_cmd(basename+2, argc, argv);
290
ae665a52 291 if (argc > 1)
351efcde
SH
292 return do_cmd(argv[1], argc-1, argv+1);
293
294 rtnl_close(&rth);
aba5acdf
SH
295 usage();
296}