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