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