quip_client.c revision 1.12 1 /* $NetBSD: quip_client.c,v 1.12 2016/02/17 19:51:29 christos Exp $ */
2 /* $KAME: quip_client.c,v 1.9 2003/05/17 05:59:00 itojun Exp $ */
3 /*
4 * Copyright (C) 1999-2000
5 * Sony Computer Science Laboratories, Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <sys/un.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <err.h>
40
41 #include "quip_client.h"
42 #include "altqstat.h"
43
44 /*
45 * quip (queue information protocol) is a http-like protocol
46 * in order to retrieve information from the server.
47 * a unix domain TCP socket "/var/run/altq_quip" is used for
48 * client-server style communication.
49 *
50 * there are 2 quip message types: request and response.
51 * request format: (only single-line request message is used at this moment)
52 * request-line
53 *
54 * request-line = <method> <operation>[?<query>] <quip-version>
55 * <method> = GET (only GET is defined at this moment)
56 * <operation> = list | handle-to-name | qdisc | filter
57 * query format is operation dependent but most query takes
58 * <interface> or <class> or <filter>.
59 * <interface> = <if_name>
60 * <class> = <if_name>:<class_path>/<class_name>
61 * <filter> = <if_name>:<class_path>/<class_name>:<filter_name>
62 * "list" operation accepts "*" as a wildcard.
63 *
64 * response format:
65 * status-line
66 * response-headers (0 or more)
67 * <blank line>
68 * body
69 *
70 * status-line = <quip-version> <status-code> <reason phrase>
71 * response-header = Content-Length:<value>
72 *
73 * "Content-Length" specifies the length of the message body.
74 *
75 * example:
76 * to retrieve a list of classes (handle and name) on interface "fxp0":
77 * a request message looks like,
78 * GET list?fxp0:* QUIP/1.0<cr>
79 * a response message looks like,
80 * QUIP/1.0 200 OK<cr>
81 * Content-Length:86<cr>
82 * <cr>
83 * 0000000000 fxp0:/root<cr>
84 * 0xc0d1be00 fxp0:/root/parent<cr>
85 * 0xc0d1ba00 fxp0:/root/parent/child<cr>
86 *
87 * other examples:
88 * list all interfaces, classes, and filters:
89 * GET list QUIP/1.0<cr>
90 * list all interfaces:
91 * GET list?* QUIP/1.0<cr>
92 * list all classes:
93 * GET list?*:* QUIP/1.0<cr>
94 * list all filters:
95 * GET list?*:*:* QUIP/1.0<cr>
96 * convert class handle to class name:
97 * GET handle-to-name?fxp0:0xc0d1be00 QUIP/1.0<cr>
98 * convert filter handle to filter name:
99 * GET handle-to-name?fxp0::0x1000000a QUIP/1.0<cr>
100 */
101
102 #define MAXLINESIZE 1024
103
104 enum nametype { INTERFACE, CLASS, FILTER, CONDITIONER };
105
106 static FILE *server = NULL;
107 int quip_echo = 0;
108
109 static char *extract_ifname(const char *);
110
111 int
112 quip_openserver(void)
113 {
114 struct sockaddr_un addr;
115 int fd;
116
117 if ((fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
118 err(1, "can't open socket");
119
120 bzero(&addr, sizeof(addr));
121 addr.sun_family = AF_LOCAL;
122 strlcpy(addr.sun_path, QUIP_PATH,sizeof(addr.sun_path));
123
124 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
125 warnx("can't talk to altqd;probably, altqd is not running");
126 return (-1);
127 }
128
129 if ((server = fdopen(fd, "r+")) == NULL) {
130 warn("fdopen: can't open stream to the quip server");
131 return (-1);
132 }
133 return (0);
134 }
135
136 int
137 quip_closeserver(void)
138 {
139 if (server != NULL)
140 return fclose(server);
141 return (0);
142 }
143
144 void
145 quip_sendrequest(FILE *fp, const char *request)
146 {
147 char buf[QUIPMSG_MAXSIZE], *cp;
148 int n;
149
150 if ((cp = strstr(request, "QUIP")) == NULL) {
151 cp = strchr(request, '\n');
152 n = cp - request;
153 if (cp == NULL || n > REQ_MAXSIZE - 10)
154 return;
155 strncpy(buf, request, n);
156 snprintf(buf + n, REQ_MAXSIZE - n, " QUIP/1.0");
157 strlcat(buf, cp, REQ_MAXSIZE);
158 }
159 else
160 strlcpy(buf, request, REQ_MAXSIZE);
161
162 if (fputs(buf, fp) != 0)
163 err(1, "fputs");
164 if (fflush(fp) != 0)
165 err(1, "fflush");
166 if (quip_echo) {
167 fputs("<< ", stdout);
168 fputs(buf, stdout);
169 }
170 }
171
172 /*
173 * recv_response receives a response message from the server
174 * and returns status_code.
175 */
176 int
177 quip_recvresponse(FILE *fp, char *header, char *body, int *blen)
178 {
179 char buf[MAXLINESIZE], version[MAXLINESIZE];
180 int code, resid, len, buflen;
181 int end_of_header = 0;
182
183 if (blen != NULL)
184 *blen = 0;
185 code = 0;
186 resid = 0;
187 buflen = RES_MAXSIZE;
188 while (fgets(buf, sizeof(buf), fp) != 0) {
189 if (quip_echo) {
190 fputs("> ", stdout);
191 fputs(buf, stdout);
192 }
193
194 if (!end_of_header) {
195 /* process message header */
196 if (header != NULL) {
197 len = strlcpy(header, buf, buflen);
198 if (len >= buflen) {
199 /* header too long */
200 fpurge(fp);
201 return (-1);
202 }
203 header += len;
204 buflen -= len;
205 }
206
207 if (code == 0) {
208 /* status line expected */
209 if (buf[0] == '\n') {
210 /* ignore blank lines */
211 }
212 /* XXX sizeof(version) == 1024 */
213 else if (sscanf(buf, "%1023s %d",
214 version, &code) != 2) {
215 /* can't get result code */
216 fpurge(fp);
217 return (-1);
218 }
219 }
220 else {
221 /* entity header expected */
222 char *field, *cp;
223
224 if (buf[0] == '\n') {
225 /* end of header */
226 end_of_header = 1;
227 buflen = BODY_MAXSIZE;
228 if (resid == 0)
229 /* no message body */
230 return (code);
231 }
232
233 cp = buf;
234 field = strsep(&cp, ":");
235 if (strcmp(field, "Content-Length") == 0) {
236 if (sscanf(cp, "%d", &resid) != 1) {
237 fpurge(fp);
238 return (-1);
239 }
240 if (blen != NULL)
241 *blen = resid;
242 }
243 }
244 }
245 else {
246 /* process message body */
247 if (body != NULL) {
248 len = strlcpy(body, buf, buflen);
249 if (len >= buflen) {
250 /* body too long */
251 fpurge(fp);
252 return (-1);
253 }
254 body += len;
255 buflen -= len;
256 }
257 else
258 len = strlen(buf);
259 resid -= len;
260 if (resid <= 0)
261 return (code);
262 }
263 }
264 return (-1);
265 }
266
267 void
268 quip_rawmode(void)
269 {
270 char line[MAXLINESIZE];
271
272 printf(">>>Entering the raw interactive mode to the server:\n\n");
273 if (server == NULL) {
274 printf("No server available!\n");
275 return;
276 }
277
278 while (1) {
279 printf("%% "); fflush(stdout);
280 /* read a line from stdin */
281 if (fgets(line, sizeof(line), stdin) == NULL)
282 break;
283
284 if (line[0] == '\n') {
285 /* if a blank line, echo locally */
286 fputs(line, stdout);
287 continue;
288 }
289 if (line[0] == 'q') {
290 printf("Exit\n");
291 break;
292 }
293
294 /* send the input line to the server */
295 quip_sendrequest(server, line);
296
297 /* get a response message from the server */
298 (void)quip_recvresponse(server, NULL, NULL, NULL);
299 }
300 }
301
302 char *
303 quip_selectinterface(char *ifname)
304 {
305 char buf[BODY_MAXSIZE], *cp;
306 int result_code, len;
307 u_int if_index;
308 static char interface[64];
309
310 if (server == NULL)
311 return (ifname);
312
313 /* get an inferface list from the server */
314 quip_sendrequest(server, "GET list?*\n");
315
316 result_code = quip_recvresponse(server, NULL, buf, &len);
317 if (result_code != 200)
318 errx(1, "can't get interface list");
319
320 cp = buf;
321 while (1) {
322 if (sscanf(cp, "%x %63s", &if_index, interface) != 2)
323 break;
324 if (ifname == NULL) {
325 /* if name isn't specified, return the 1st entry */
326 return (interface);
327 }
328 if (strcmp(ifname, interface) == 0)
329 /* found the matching entry */
330 return (interface);
331 if ((cp = strchr(cp+1, '\n')) == NULL)
332 break;
333 }
334 errx(1, "can't get interface");
335 return (NULL);
336 }
337
338 char *
339 quip_selectqdisc(char *ifname, char *qdisc_name)
340 {
341 char buf[BODY_MAXSIZE], req[REQ_MAXSIZE];
342 int result_code, len;
343 static char qdisc[64];
344
345 if (server == NULL) {
346 if (ifname == NULL || qdisc_name == NULL)
347 errx(1, "when disabling server communication,\n"
348 "specify both interface (-i) and qdisc (-q)!");
349 return (qdisc_name);
350 }
351
352 /* get qdisc info from the server */
353 snprintf(req, sizeof(req), "GET qdisc?%s\n", ifname);
354 quip_sendrequest(server, req);
355
356 result_code = quip_recvresponse(server, NULL, buf, &len);
357 if (result_code != 200)
358 errx(1, "can't get qdisc info");
359
360 if (sscanf(buf, "%63s", qdisc) != 1)
361 errx(1, "can't get qdisc name");
362
363 if (qdisc_name != NULL && strcmp(qdisc, qdisc_name) != 0)
364 errx(1, "qdisc %s on %s doesn't match specified qdisc %s",
365 qdisc, ifname, qdisc_name);
366
367 return (qdisc);
368 }
369
370 void
371 quip_chandle2name(const char *ifname, u_long handle, char *name, size_t size)
372 {
373 char buf[BODY_MAXSIZE], req[REQ_MAXSIZE], *cp;
374 int result_code, len;
375
376 name[0] = '\0';
377 if (server == NULL)
378 return;
379
380 /* get class name from the server */
381 snprintf(req, sizeof(req), "GET handle-to-name?%s:%#lx\n", ifname, handle);
382 quip_sendrequest(server, req);
383
384 result_code = quip_recvresponse(server, NULL, buf, &len);
385 if (result_code != 200)
386 errx(1, "can't get class name");
387
388 if ((cp = strchr(buf, '\n')) != NULL)
389 *cp = '\0';
390 if ((cp = strrchr(buf, '/')) != NULL)
391 strlcpy(name, cp+1, size);
392 }
393
394 void
395 quip_printqdisc(const char *ifname)
396 {
397 char buf[BODY_MAXSIZE], req[REQ_MAXSIZE], *cp;
398 int result_code, len;
399
400 if (server == NULL) {
401 printf("No server available!\n");
402 return;
403 }
404
405 /* get qdisc info from the server */
406 snprintf(req, sizeof(req), "GET qdisc?%s\n", ifname);
407 quip_sendrequest(server, req);
408
409 result_code = quip_recvresponse(server, NULL, buf, &len);
410 if (result_code != 200)
411 errx(1, "can't get qdisc info");
412
413 /* replace newline by space */
414 cp = buf;
415 while ((cp = strchr(cp, '\n')) != NULL)
416 *cp = ' ';
417
418 printf(" qdisc:%s\n", buf);
419 }
420
421 void
422 quip_printfilter(const char *ifname, const u_long handle)
423 {
424 char buf[BODY_MAXSIZE], req[REQ_MAXSIZE], *cp;
425 int result_code, len;
426
427 if (server == NULL) {
428 printf("No server available!\n");
429 return;
430 }
431
432 /* get qdisc info from the server */
433 snprintf(req, sizeof(req), "GET filter?%s::%#lx\n", ifname, handle);
434 quip_sendrequest(server, req);
435
436 result_code = quip_recvresponse(server, NULL, buf, &len);
437 if (result_code != 200)
438 errx(1, "can't get filter info");
439
440 if ((cp = strchr(buf, '\n')) != NULL)
441 *cp = '\0';
442 printf("%s", buf);
443 }
444
445 static char *
446 extract_ifname(const char *name)
447 {
448 char *cp;
449 int len;
450 static char ifname[64];
451
452 if ((cp = strchr(name, ':')) != NULL)
453 len = cp - name;
454 else
455 len = strlen(name);
456 len = MIN(len, 63);
457 strncpy(ifname, name, len);
458 ifname[len] = '\0';
459 return (ifname);
460 }
461
462 void
463 quip_printconfig(void)
464 {
465 char buf[BODY_MAXSIZE], name[256], *cp, *p, *flname;
466 int result_code, len;
467 enum nametype type;
468 u_long handle;
469
470 if (server == NULL) {
471 printf("No server available!\n");
472 return;
473 }
474
475 /* get a total list from the server */
476 quip_sendrequest(server, "GET list\n");
477
478 result_code = quip_recvresponse(server, NULL, buf, &len);
479 if (result_code != 200)
480 errx(1, "can't get total list");
481
482 printf("------------ current configuration ------------");
483
484 cp = buf;
485 while (1) {
486 if (sscanf(cp, "%lx %255s", &handle, name) != 2)
487 break;
488
489 if ((p = strchr(name, ':')) == NULL)
490 type = INTERFACE;
491 else if (strchr(p+1, ':') == NULL)
492 type = CLASS;
493 else
494 type = FILTER;
495
496 switch (type) {
497 case INTERFACE:
498 printf("\ninterface: %s (index:%lu)\n",
499 name, handle);
500 quip_printqdisc(name);
501 break;
502 case CLASS:
503 printf("class: %s (handle:%#lx)\n",
504 name, handle);
505 break;
506 case FILTER:
507 flname = strrchr(name, ':') + 1;
508 printf(" filter: name:%s [", flname);
509 quip_printfilter(extract_ifname(name), handle);
510 printf("] (handle:%#lx)\n", handle);
511 break;
512 case CONDITIONER:
513 break;
514 }
515
516 if ((cp = strchr(cp+1, '\n')) == NULL)
517 break;
518 }
519 printf("-----------------------------------------------\n\n");
520 }
521
522