]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/ip.c
Add usage for ip -batch
[lisovros/iproute2_canprio.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  * 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
31 int preferred_family = AF_UNSPEC;
32 int show_stats = 0;
33 int resolve_hosts = 0;
34 int oneline = 0;
35 int timestamp = 0;
36 char * _SL_ = NULL;
37 char *batch_file = NULL;
38 int force = 0;
39 struct rtnl_handle rth;
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 | 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
56 static const struct cmd {
57         const char *cmd;
58         int (*func)(int argc, char **argv);
59 } cmds[] = {
60         { "addr",       do_ipaddr },
61         { "maddr",      do_multiaddr },
62         { "route",      do_iproute },
63         { "rule",       do_iprule },
64         { "neigh",      do_ipneigh },
65         { "link",       do_iplink },
66         { "tunnel",     do_iptunnel },
67         { "monitor",    do_ipmonitor },
68         { "xfrm",       do_xfrm },
69         { 0 }
70 };
71
72 static int do_cmd(const char *argv0, int argc, char **argv)
73 {
74         const struct cmd *c;
75
76         for (c = cmds; c->cmd; ++c)
77                 if (strcmp(c->cmd, argv0) == 0)
78                         return c->func(argc-1, argv+1);
79
80         fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
81         exit(-1);
82 }
83
84 static int batch(const char *name)
85 {
86         char *line = NULL;
87         size_t len = 0;
88         int ret = 0;
89         int lineno = 0;
90
91         if (name && strcmp(name, "-") != 0) {
92                 if (freopen(name, "r", stdin) == NULL) {
93                         fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n",
94                                 name, strerror(errno));
95                         return -1;
96                 }
97         }
98
99         if (rtnl_open(&rth, 0) < 0) {
100                 fprintf(stderr, "Cannot open rtnetlink\n");
101                 return -1;
102         }
103
104         while (getcmdline(&line, &len, stdin) != -1) {
105                 char *largv[100];
106                 int largc;
107
108                 largc = makeargs(line, largv, 100);
109                 if (largc == 0)
110                         continue;       /* blank line */
111
112                 if (do_cmd(largv[0], largc, largv)) {
113                         fprintf(stderr, "Command failed %s:%d\n", name, lineno);
114                         ret = 1;
115                         if (!force)
116                                 break;
117                 }
118         }
119
120         rtnl_close(&rth);
121         return ret;
122 }
123
124
125 int main(int argc, char **argv)
126 {
127         char *basename;
128
129         basename = strrchr(argv[0], '/');
130         if (basename == NULL)
131                 basename = argv[0];
132         else
133                 basename++;
134         
135         while (argc > 1) {
136                 char *opt = argv[1];
137                 if (strcmp(opt,"--") == 0) {
138                         argc--; argv++;
139                         break;
140                 }
141                 if (opt[0] != '-')
142                         break;
143                 if (opt[1] == '-')
144                         opt++;
145                 if (matches(opt, "-family") == 0) {
146                         argc--;
147                         argv++;
148                         if (argc <= 1)
149                                 usage();
150                         if (strcmp(argv[1], "inet") == 0)
151                                 preferred_family = AF_INET;
152                         else if (strcmp(argv[1], "inet6") == 0)
153                                 preferred_family = AF_INET6;
154                         else if (strcmp(argv[1], "dnet") == 0)
155                                 preferred_family = AF_DECnet;
156                         else if (strcmp(argv[1], "link") == 0)
157                                 preferred_family = AF_PACKET;
158                         else if (strcmp(argv[1], "ipx") == 0)
159                                 preferred_family = AF_IPX;
160                         else if (strcmp(argv[1], "help") == 0)
161                                 usage();
162                         else
163                                 invarg(argv[1], "invalid protocol family");
164                 } else if (strcmp(opt, "-4") == 0) {
165                         preferred_family = AF_INET;
166                 } else if (strcmp(opt, "-6") == 0) {
167                         preferred_family = AF_INET6;
168                 } else if (strcmp(opt, "-0") == 0) {
169                         preferred_family = AF_PACKET;
170                 } else if (strcmp(opt, "-I") == 0) {
171                         preferred_family = AF_IPX;
172                 } else if (strcmp(opt, "-D") == 0) {
173                         preferred_family = AF_DECnet;
174                 } else if (matches(opt, "-stats") == 0 ||
175                            matches(opt, "-statistics") == 0) {
176                         ++show_stats;
177                 } else if (matches(opt, "-resolve") == 0) {
178                         ++resolve_hosts;
179                 } else if (matches(opt, "-oneline") == 0) {
180                         ++oneline;
181                 } else if (matches(opt, "-timestamp") == 0) {
182                         ++timestamp;
183 #if 0
184                 } else if (matches(opt, "-numeric") == 0) {
185                         rtnl_names_numeric++;
186 #endif
187                 } else if (matches(opt, "-Version") == 0) {
188                         printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
189                         exit(0);
190                 } else if (matches(opt, "-force") == 0) {
191                         ++force;
192                 } else if (matches(opt, "-batch") == 0) {
193                         argc--;
194                         argv++;
195                         if (argc <= 1)
196                                 usage();
197                         batch_file = argv[1];
198                 } else if (matches(opt, "-help") == 0) {
199                         usage();
200                 } else {
201                         fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
202                         exit(-1);
203                 }
204                 argc--; argv++;
205         }
206
207         _SL_ = oneline ? "\\" : "\n" ;
208
209         if (batch_file) 
210                 return batch(batch_file);
211                 
212         if (rtnl_open(&rth, 0) < 0)
213                 exit(1);
214
215         if (strlen(basename) > 2) 
216                 return do_cmd(basename+2, argc, argv);
217
218         if (argc > 1) 
219                 return do_cmd(argv[1], argc-1, argv+1);
220
221         rtnl_close(&rth);
222         usage();
223 }