]>
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>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
22 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #include "ip_common.h"
31 int preferred_family
= AF_UNSPEC
;
33 int resolve_hosts
= 0;
37 char *batch_file
= NULL
;
39 struct rtnl_handle rth
= { .fd
= -1 };
41 static void usage(void) __attribute__((noreturn
));
43 static void usage(void)
46 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
47 " ip [ -force ] [-batch filename\n"
48 "where OBJECT := { link | addr | route | rule | neigh | ntable | 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");
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 { "maddress", do_multiaddr
},
67 { "route", do_iproute
},
68 { "rule", do_iprule
},
69 { "neighbor", do_ipneigh
},
70 { "neighbour", do_ipneigh
},
71 { "ntable", do_ipntable
},
72 { "ntbl", do_ipntable
},
73 { "link", do_iplink
},
74 { "tunnel", do_iptunnel
},
75 { "tunl", do_iptunnel
},
76 { "monitor", do_ipmonitor
},
78 { "mroute", do_multiroute
},
83 static int do_cmd(const char *argv0
, int argc
, char **argv
)
87 for (c
= cmds
; c
->cmd
; ++c
) {
88 if (matches(argv0
, c
->cmd
) == 0)
89 return c
->func(argc
-1, argv
+1);
92 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
96 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");
116 while (getcmdline(&line
, &len
, stdin
) != -1) {
120 largc
= makeargs(line
, largv
, 100);
122 continue; /* blank line */
124 if (do_cmd(largv
[0], largc
, largv
)) {
125 fprintf(stderr
, "Command failed %s:%d\n", name
, lineno
);
139 int main(int argc
, char **argv
)
143 basename
= strrchr(argv
[0], '/');
144 if (basename
== NULL
)
151 if (strcmp(opt
,"--") == 0) {
159 if (matches(opt
, "-family") == 0) {
164 if (strcmp(argv
[1], "inet") == 0)
165 preferred_family
= AF_INET
;
166 else if (strcmp(argv
[1], "inet6") == 0)
167 preferred_family
= AF_INET6
;
168 else if (strcmp(argv
[1], "dnet") == 0)
169 preferred_family
= AF_DECnet
;
170 else if (strcmp(argv
[1], "link") == 0)
171 preferred_family
= AF_PACKET
;
172 else if (strcmp(argv
[1], "ipx") == 0)
173 preferred_family
= AF_IPX
;
174 else if (strcmp(argv
[1], "help") == 0)
177 invarg(argv
[1], "invalid protocol family");
178 } else if (strcmp(opt
, "-4") == 0) {
179 preferred_family
= AF_INET
;
180 } else if (strcmp(opt
, "-6") == 0) {
181 preferred_family
= AF_INET6
;
182 } else if (strcmp(opt
, "-0") == 0) {
183 preferred_family
= AF_PACKET
;
184 } else if (strcmp(opt
, "-I") == 0) {
185 preferred_family
= AF_IPX
;
186 } else if (strcmp(opt
, "-D") == 0) {
187 preferred_family
= AF_DECnet
;
188 } else if (matches(opt
, "-stats") == 0 ||
189 matches(opt
, "-statistics") == 0) {
191 } else if (matches(opt
, "-resolve") == 0) {
193 } else if (matches(opt
, "-oneline") == 0) {
195 } else if (matches(opt
, "-timestamp") == 0) {
198 } else if (matches(opt
, "-numeric") == 0) {
199 rtnl_names_numeric
++;
201 } else if (matches(opt
, "-Version") == 0) {
202 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
204 } else if (matches(opt
, "-force") == 0) {
206 } else if (matches(opt
, "-batch") == 0) {
211 batch_file
= argv
[1];
212 } else if (matches(opt
, "-help") == 0) {
215 fprintf(stderr
, "Option \"%s\" is unknown, try \"ip -help\".\n", opt
);
221 _SL_
= oneline
? "\\" : "\n" ;
224 return batch(batch_file
);
226 if (rtnl_open(&rth
, 0) < 0)
229 if (strlen(basename
) > 2)
230 return do_cmd(basename
+2, argc
, argv
);
233 return do_cmd(argv
[1], argc
-1, argv
+1);