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