]>
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 struct rtnl_handle rth
= { .fd
= -1 };
37 static void usage(void) __attribute__((noreturn
));
39 static void usage(void)
42 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
43 " ip [ -force ] -batch filename\n"
44 "where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
45 " tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm }\n"
46 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
47 " -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
48 " -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
49 " -rc[vbuf] [size]}\n");
53 static int do_help(int argc
, char **argv
)
58 static const struct cmd
{
60 int (*func
)(int argc
, char **argv
);
62 { "address", do_ipaddr
},
63 { "addrlabel", do_ipaddrlabel
},
64 { "maddress", do_multiaddr
},
65 { "route", do_iproute
},
66 { "rule", do_iprule
},
67 { "neighbor", do_ipneigh
},
68 { "neighbour", do_ipneigh
},
69 { "ntable", do_ipntable
},
70 { "ntbl", do_ipntable
},
71 { "link", do_iplink
},
72 { "tunnel", do_iptunnel
},
73 { "tunl", do_iptunnel
},
74 { "tuntap", do_iptuntap
},
75 { "tap", do_iptuntap
},
76 { "monitor", do_ipmonitor
},
78 { "mroute", do_multiroute
},
79 { "mrule", do_multirule
},
84 static int do_cmd(const char *argv0
, int argc
, char **argv
)
88 for (c
= cmds
; c
->cmd
; ++c
) {
89 if (matches(argv0
, c
->cmd
) == 0)
90 return c
->func(argc
-1, argv
+1);
93 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
97 static int batch(const char *name
)
103 if (name
&& strcmp(name
, "-") != 0) {
104 if (freopen(name
, "r", stdin
) == NULL
) {
105 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
106 name
, strerror(errno
));
111 if (rtnl_open(&rth
, 0) < 0) {
112 fprintf(stderr
, "Cannot open rtnetlink\n");
117 while (getcmdline(&line
, &len
, stdin
) != -1) {
121 largc
= makeargs(line
, largv
, 100);
123 continue; /* blank line */
125 if (do_cmd(largv
[0], largc
, largv
)) {
126 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
140 int main(int argc
, char **argv
)
144 basename
= strrchr(argv
[0], '/');
145 if (basename
== NULL
)
152 if (strcmp(opt
,"--") == 0) {
160 if (matches(opt
, "-family") == 0) {
165 if (strcmp(argv
[1], "inet") == 0)
166 preferred_family
= AF_INET
;
167 else if (strcmp(argv
[1], "inet6") == 0)
168 preferred_family
= AF_INET6
;
169 else if (strcmp(argv
[1], "dnet") == 0)
170 preferred_family
= AF_DECnet
;
171 else if (strcmp(argv
[1], "link") == 0)
172 preferred_family
= AF_PACKET
;
173 else if (strcmp(argv
[1], "ipx") == 0)
174 preferred_family
= AF_IPX
;
175 else if (strcmp(argv
[1], "help") == 0)
178 invarg(argv
[1], "invalid protocol family");
179 } else if (strcmp(opt
, "-4") == 0) {
180 preferred_family
= AF_INET
;
181 } else if (strcmp(opt
, "-6") == 0) {
182 preferred_family
= AF_INET6
;
183 } else if (strcmp(opt
, "-0") == 0) {
184 preferred_family
= AF_PACKET
;
185 } else if (strcmp(opt
, "-I") == 0) {
186 preferred_family
= AF_IPX
;
187 } else if (strcmp(opt
, "-D") == 0) {
188 preferred_family
= AF_DECnet
;
189 } else if (matches(opt
, "-stats") == 0 ||
190 matches(opt
, "-statistics") == 0) {
192 } else if (matches(opt
, "-details") == 0) {
194 } else if (matches(opt
, "-resolve") == 0) {
196 } else if (matches(opt
, "-oneline") == 0) {
198 } else if (matches(opt
, "-timestamp") == 0) {
201 } else if (matches(opt
, "-numeric") == 0) {
202 rtnl_names_numeric
++;
204 } else if (matches(opt
, "-Version") == 0) {
205 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
207 } else if (matches(opt
, "-force") == 0) {
209 } else if (matches(opt
, "-batch") == 0) {
214 batch_file
= argv
[1];
215 } else if (matches(opt
, "-rcvbuf") == 0) {
222 if (get_unsigned(&size
, argv
[1], 0)) {
223 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
228 } else if (matches(opt
, "-help") == 0) {
231 fprintf(stderr
, "Option \"%s\" is unknown, try \"ip -help\".\n", opt
);
237 _SL_
= oneline
? "\\" : "\n" ;
240 return batch(batch_file
);
242 if (rtnl_open(&rth
, 0) < 0)
245 if (strlen(basename
) > 2)
246 return do_cmd(basename
+2, argc
, argv
);
249 return do_cmd(argv
[1], argc
-1, argv
+1);