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