]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
Fix build warnings on x86_64
[mirror_iproute2.git] / tc / tc.c
1 /*
2 * tc.c "tc" 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 * Fixes:
12 *
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <fcntl.h>
21 #include <dlfcn.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "SNAPSHOT.h"
29 #include "utils.h"
30 #include "tc_util.h"
31 #include "tc_common.h"
32
33 int show_stats = 0;
34 int show_details = 0;
35 int show_raw = 0;
36 int resolve_hosts = 0;
37 int use_iec = 0;
38
39 static void *BODY; /* cached handle dlopen(NULL) */
40 static struct qdisc_util * qdisc_list;
41 static struct filter_util * filter_list;
42
43 static int print_noqopt(struct qdisc_util *qu, FILE *f,
44 struct rtattr *opt)
45 {
46 if (opt && RTA_PAYLOAD(opt))
47 fprintf(f, "[Unknown qdisc, optlen=%u] ",
48 (unsigned) RTA_PAYLOAD(opt));
49 return 0;
50 }
51
52 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
53 {
54 if (argc) {
55 fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
56 return -1;
57 }
58 return 0;
59 }
60
61 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
62 {
63 if (opt && RTA_PAYLOAD(opt))
64 fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
65 fhandle, (unsigned) RTA_PAYLOAD(opt));
66 else if (fhandle)
67 fprintf(f, "fh %08x ", fhandle);
68 return 0;
69 }
70
71 static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
72 {
73 __u32 handle;
74
75 if (argc) {
76 fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
77 return -1;
78 }
79 if (fhandle) {
80 struct tcmsg *t = NLMSG_DATA(n);
81 if (get_u32(&handle, fhandle, 16)) {
82 fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
83 return -1;
84 }
85 t->tcm_handle = handle;
86 }
87 return 0;
88 }
89
90 struct qdisc_util *get_qdisc_kind(const char *str)
91 {
92 void *dlh;
93 char buf[256];
94 struct qdisc_util *q;
95
96 for (q = qdisc_list; q; q = q->next)
97 if (strcmp(q->id, str) == 0)
98 return q;
99
100 snprintf(buf, sizeof(buf), "/usr/lib/tc/q_%s.so", str);
101 dlh = dlopen(buf, RTLD_LAZY);
102 if (!dlh) {
103 /* look in current binary, only open once */
104 dlh = BODY;
105 if (dlh == NULL) {
106 dlh = BODY = dlopen(NULL, RTLD_LAZY);
107 if (dlh == NULL)
108 goto noexist;
109 }
110 }
111
112 snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
113 q = dlsym(dlh, buf);
114 if (q == NULL)
115 goto noexist;
116
117 reg:
118 q->next = qdisc_list;
119 qdisc_list = q;
120 return q;
121
122 noexist:
123 q = malloc(sizeof(*q));
124 if (q) {
125
126 memset(q, 0, sizeof(*q));
127 q->id = strcpy(malloc(strlen(str)+1), str);
128 q->parse_qopt = parse_noqopt;
129 q->print_qopt = print_noqopt;
130 goto reg;
131 }
132 return q;
133 }
134
135
136 struct filter_util *get_filter_kind(const char *str)
137 {
138 void *dlh;
139 char buf[256];
140 struct filter_util *q;
141
142 for (q = filter_list; q; q = q->next)
143 if (strcmp(q->id, str) == 0)
144 return q;
145
146 snprintf(buf, sizeof(buf), "/usr/lib/tc/f_%s.so", str);
147 dlh = dlopen(buf, RTLD_LAZY);
148 if (dlh == NULL) {
149 dlh = BODY;
150 if (dlh == NULL) {
151 dlh = BODY = dlopen(NULL, RTLD_LAZY);
152 if (dlh == NULL)
153 goto noexist;
154 }
155 }
156
157 snprintf(buf, sizeof(buf), "%s_filter_util", str);
158 q = dlsym(dlh, buf);
159 if (q == NULL)
160 goto noexist;
161
162 reg:
163 q->next = filter_list;
164 filter_list = q;
165 return q;
166 noexist:
167 q = malloc(sizeof(*q));
168 if (q) {
169 memset(q, 0, sizeof(*q));
170 strncpy(q->id, str, 15);
171 q->parse_fopt = parse_nofopt;
172 q->print_fopt = print_nofopt;
173 goto reg;
174 }
175 return q;
176 }
177
178 static void usage(void) __attribute__((noreturn));
179
180 static void usage(void)
181 {
182 fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
183 "where OBJECT := { qdisc | class | filter | action }\n"
184 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -b[atch] file }\n");
185 exit(-1);
186 }
187
188
189
190 int main(int argc, char **argv)
191 {
192 char *basename;
193
194 basename = strrchr(argv[0], '/');
195 if (basename == NULL)
196 basename = argv[0];
197 else
198 basename++;
199
200
201 /* batch mode */
202 if (argc > 1 && matches(argv[1], "-batch") == 0) {
203 FILE *batch;
204 char line[400];
205 char *largv[100];
206 int largc, ret=0;
207 #define BMAXARG (sizeof(largv)/sizeof(char *)-2)
208
209 if (argc != 3) {
210 fprintf(stderr, "Wrong number of arguments in batch mode\n");
211 exit(-1);
212 }
213 if (matches(argv[2], "-") != 0) {
214 if ((batch = fopen(argv[2], "r")) == NULL) {
215 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n", argv[2], strerror(errno));
216 exit(-1);
217 }
218 } else {
219 if ((batch = fdopen(0, "r")) == NULL) {
220 fprintf(stderr, "Cannot open stdin for reading: %s=n", strerror(errno));
221 exit(-1);
222 }
223 }
224
225 tc_core_init();
226
227 while (fgets(line, sizeof(line)-1, batch)) {
228 if (line[strlen(line)-1]=='\n') {
229 line[strlen(line)-1] = '\0';
230 } else {
231 fprintf(stderr, "No newline at the end of line, looks like to long (%d chars or more)\n",
232 (int) strlen(line));
233 exit(-1);
234 }
235 largc = 0;
236 largv[largc]=strtok(line, " ");
237 while ((largv[++largc]=strtok(NULL, " ")) != NULL) {
238 if (largc > BMAXARG) {
239 fprintf(stderr, "Over %d arguments in batch mode, enough!\n",
240 (int) BMAXARG);
241 exit(-1);
242 }
243 }
244
245 if (matches(largv[0], "qdisc") == 0) {
246 ret += do_qdisc(largc-1, largv+1);
247 } else if (matches(largv[0], "class") == 0) {
248 ret += do_class(largc-1, largv+1);
249 } else if (matches(largv[0], "filter") == 0) {
250 ret += do_filter(largc-1, largv+1);
251 } else if (matches(largv[0], "action") == 0) {
252 ret += do_action(largc-1, largv+1);
253 } else if (matches(largv[0], "help") == 0) {
254 usage(); /* note that usage() doesn't return */
255 } else {
256 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", largv[1]);
257 exit(-1);
258 }
259 }
260 fclose(batch);
261 exit(0); /* end of batch, that's all */
262 }
263
264 while (argc > 1) {
265 if (argv[1][0] != '-')
266 break;
267 if (matches(argv[1], "-stats") == 0 ||
268 matches(argv[1], "-statistics") == 0) {
269 ++show_stats;
270 } else if (matches(argv[1], "-details") == 0) {
271 ++show_details;
272 } else if (matches(argv[1], "-raw") == 0) {
273 ++show_raw;
274 } else if (matches(argv[1], "-Version") == 0) {
275 printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
276 exit(0);
277 } else if (matches(argv[1], "-iec") == 0) {
278 ++use_iec;
279 } else if (matches(argv[1], "-help") == 0) {
280 usage();
281 } else {
282 fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
283 exit(-1);
284 }
285 argc--; argv++;
286 }
287
288 tc_core_init();
289
290 if (argc > 1) {
291 if (matches(argv[1], "qdisc") == 0)
292 return do_qdisc(argc-2, argv+2);
293 if (matches(argv[1], "class") == 0)
294 return do_class(argc-2, argv+2);
295 if (matches(argv[1], "filter") == 0)
296 return do_filter(argc-2, argv+2);
297 if (matches(argv[1], "actions") == 0)
298 return do_action(argc-2, argv+2);
299 if (matches(argv[1], "help") == 0)
300 usage();
301 fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", argv[1]);
302 exit(-1);
303 }
304
305 usage();
306 }