]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip.c
2 * ip.c "ip" utility frontend.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
24 #include "ip_common.h"
26 int preferred_family
= AF_UNSPEC
;
29 int resolve_hosts
= 0;
33 char *batch_file
= NULL
;
35 int max_flush_loops
= 10;
37 struct rtnl_handle rth
= { .fd
= -1 };
39 static void usage(void) __attribute__((noreturn
));
41 static void usage(void)
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 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
49 " -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
50 " -l[oops] { maximum-addr-flush-attempts } |\n"
51 " -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
52 " -rc[vbuf] [size]}\n");
56 static int do_help(int argc
, char **argv
)
61 static const struct cmd
{
63 int (*func
)(int argc
, char **argv
);
65 { "address", do_ipaddr
},
66 { "addrlabel", do_ipaddrlabel
},
67 { "maddress", do_multiaddr
},
68 { "route", do_iproute
},
69 { "rule", do_iprule
},
70 { "neighbor", do_ipneigh
},
71 { "neighbour", do_ipneigh
},
72 { "ntable", do_ipntable
},
73 { "ntbl", do_ipntable
},
74 { "link", do_iplink
},
75 { "tunnel", do_iptunnel
},
76 { "tunl", do_iptunnel
},
77 { "tuntap", do_iptuntap
},
78 { "tap", do_iptuntap
},
79 { "monitor", do_ipmonitor
},
81 { "mroute", do_multiroute
},
82 { "mrule", do_multirule
},
87 static int do_cmd(const char *argv0
, int argc
, char **argv
)
91 for (c
= cmds
; c
->cmd
; ++c
) {
92 if (matches(argv0
, c
->cmd
) == 0)
93 return c
->func(argc
-1, argv
+1);
96 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
100 static int batch(const char *name
)
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
));
114 if (rtnl_open(&rth
, 0) < 0) {
115 fprintf(stderr
, "Cannot open rtnetlink\n");
120 while (getcmdline(&line
, &len
, stdin
) != -1) {
124 largc
= makeargs(line
, largv
, 100);
126 continue; /* blank line */
128 if (do_cmd(largv
[0], largc
, largv
)) {
129 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
143 int main(int argc
, char **argv
)
147 basename
= strrchr(argv
[0], '/');
148 if (basename
== NULL
)
155 if (strcmp(opt
,"--") == 0) {
163 if (matches(opt
, "-loops") == 0) {
168 max_flush_loops
= atoi(argv
[1]);
169 } else if (matches(opt
, "-family") == 0) {
174 if (strcmp(argv
[1], "inet") == 0)
175 preferred_family
= AF_INET
;
176 else if (strcmp(argv
[1], "inet6") == 0)
177 preferred_family
= AF_INET6
;
178 else if (strcmp(argv
[1], "dnet") == 0)
179 preferred_family
= AF_DECnet
;
180 else if (strcmp(argv
[1], "link") == 0)
181 preferred_family
= AF_PACKET
;
182 else if (strcmp(argv
[1], "ipx") == 0)
183 preferred_family
= AF_IPX
;
184 else if (strcmp(argv
[1], "help") == 0)
187 invarg(argv
[1], "invalid protocol family");
188 } else if (strcmp(opt
, "-4") == 0) {
189 preferred_family
= AF_INET
;
190 } else if (strcmp(opt
, "-6") == 0) {
191 preferred_family
= AF_INET6
;
192 } else if (strcmp(opt
, "-0") == 0) {
193 preferred_family
= AF_PACKET
;
194 } else if (strcmp(opt
, "-I") == 0) {
195 preferred_family
= AF_IPX
;
196 } else if (strcmp(opt
, "-D") == 0) {
197 preferred_family
= AF_DECnet
;
198 } else if (matches(opt
, "-stats") == 0 ||
199 matches(opt
, "-statistics") == 0) {
201 } else if (matches(opt
, "-details") == 0) {
203 } else if (matches(opt
, "-resolve") == 0) {
205 } else if (matches(opt
, "-oneline") == 0) {
207 } else if (matches(opt
, "-timestamp") == 0) {
210 } else if (matches(opt
, "-numeric") == 0) {
211 rtnl_names_numeric
++;
213 } else if (matches(opt
, "-Version") == 0) {
214 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
216 } else if (matches(opt
, "-force") == 0) {
218 } else if (matches(opt
, "-batch") == 0) {
223 batch_file
= argv
[1];
224 } else if (matches(opt
, "-rcvbuf") == 0) {
231 if (get_unsigned(&size
, argv
[1], 0)) {
232 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
237 } else if (matches(opt
, "-help") == 0) {
240 fprintf(stderr
, "Option \"%s\" is unknown, try \"ip -help\".\n", opt
);
246 _SL_
= oneline
? "\\" : "\n" ;
249 return batch(batch_file
);
251 if (rtnl_open(&rth
, 0) < 0)
254 if (strlen(basename
) > 2)
255 return do_cmd(basename
+2, argc
, argv
);
258 return do_cmd(argv
[1], argc
-1, argv
+1);