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