1 /* $NetBSD: quip_client.c,v 1.8.2.1 2006/03/18 12:16:52 peter Exp $ */
2 /* $KAME: quip_client.c,v 1.9 2003/05/17 05:59:00 itojun Exp $ */
4 * Copyright (C) 1999-2000
5 * Sony Computer Science Laboratories, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/socket.h>
41 #include "quip_client.h"
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.
50 * there are 2 quip message types: request and response.
51 * request format: (only single-line request message is used at this moment)
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.
66 * response-headers (0 or more)
70 * status-line = <quip-version> <status-code> <reason phrase>
71 * response-header = Content-Length:<value>
73 * "Content-Length" specifies the length of the message body.
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,
81 * Content-Length:86<cr>
83 * 0000000000 fxp0:/root<cr>
84 * 0xc0d1be00 fxp0:/root/parent<cr>
85 * 0xc0d1ba00 fxp0:/root/parent/child<cr>
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>
93 * GET list?*:* QUIP/1.0<cr>
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>
102 #define MAXLINESIZE 1024
104 enum nametype
{ INTERFACE
, CLASS
, FILTER
, CONDITIONER
};
106 static FILE *server
= NULL
;
109 static char *extract_ifname(const char *);
112 quip_openserver(void)
114 struct sockaddr_un addr
;
117 if ((fd
= socket(AF_LOCAL
, SOCK_STREAM
, 0)) < 0)
118 err(1, "can't open socket");
120 bzero(&addr
, sizeof(addr
));
121 addr
.sun_family
= AF_LOCAL
;
122 strlcpy(addr
.sun_path
, QUIP_PATH
,sizeof(addr
.sun_path
));
124 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
125 fprintf(stderr
, "can't talk to altqd!\n"
126 "probably, altqd is not running\n");
130 if ((server
= fdopen(fd
, "r+")) == NULL
) {
131 warn("fdopen: can't open stream to the quip server");
138 quip_closeserver(void)
141 return fclose(server
);
146 quip_sendrequest(FILE *fp
, const char *request
)
148 char buf
[QUIPMSG_MAXSIZE
], *cp
;
151 if ((cp
= strstr(request
, "QUIP")) == NULL
) {
152 cp
= strchr(request
, '\n');
154 if (cp
== NULL
|| n
> REQ_MAXSIZE
- 10)
156 strncpy(buf
, request
, n
);
157 snprintf(buf
+ n
, REQ_MAXSIZE
- n
, " QUIP/1.0");
158 strlcat(buf
, cp
, REQ_MAXSIZE
);
161 strlcpy(buf
, request
, REQ_MAXSIZE
);
163 if (fputs(buf
, fp
) != 0)
168 fputs("<< ", stdout
);
174 * recv_response receives a response message from the server
175 * and returns status_code.
178 quip_recvresponse(FILE *fp
, char *header
, char *body
, int *blen
)
180 char buf
[MAXLINESIZE
], version
[MAXLINESIZE
];
181 int code
, resid
, len
, buflen
;
182 int end_of_header
= 0;
188 buflen
= RES_MAXSIZE
;
189 while (fgets(buf
, sizeof(buf
), fp
) != 0) {
195 if (!end_of_header
) {
196 /* process message header */
197 if (header
!= NULL
) {
198 len
= strlcpy(header
, buf
, buflen
);
200 /* header too long */
209 /* status line expected */
210 if (buf
[0] == '\n') {
211 /* ignore blank lines */
213 /* XXX sizeof(version) == 1024 */
214 else if (sscanf(buf
, "%1023s %d",
215 version
, &code
) != 2) {
216 /* can't get result code */
222 /* entity header expected */
225 if (buf
[0] == '\n') {
228 buflen
= BODY_MAXSIZE
;
230 /* no message body */
235 field
= strsep(&cp
, ":");
236 if (strcmp(field
, "Content-Length") == 0) {
237 if (sscanf(cp
, "%d", &resid
) != 1) {
247 /* process message body */
249 len
= strlcpy(body
, buf
, buflen
);
271 char line
[MAXLINESIZE
];
274 printf(">>>Entering the raw interactive mode to the server:\n\n");
275 if (server
== NULL
) {
276 printf("No server available!\n");
281 printf("%% "); fflush(stdout
);
282 /* read a line from stdin */
283 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
286 if (line
[0] == '\n') {
287 /* if a blank line, echo locally */
291 if (line
[0] == 'q') {
296 /* send the input line to the server */
297 quip_sendrequest(server
, line
);
299 /* get a response message from the server */
300 result_code
= quip_recvresponse(server
, NULL
, NULL
, NULL
);
305 quip_selectinterface(char *ifname
)
307 char buf
[BODY_MAXSIZE
], *cp
;
308 int result_code
, len
;
310 static char interface
[64];
315 /* get an inferface list from the server */
316 quip_sendrequest(server
, "GET list?*\n");
318 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
319 if (result_code
!= 200)
320 errx(1, "can't get interface list");
324 if (sscanf(cp
, "%x %63s", &if_index
, interface
) != 2)
326 if (ifname
== NULL
) {
327 /* if name isn't specified, return the 1st entry */
330 if (strcmp(ifname
, interface
) == 0)
331 /* found the matching entry */
333 if ((cp
= strchr(cp
+1, '\n')) == NULL
)
336 errx(1, "can't get interface");
341 quip_selectqdisc(char *ifname
, char *qdisc_name
)
343 char buf
[BODY_MAXSIZE
], req
[REQ_MAXSIZE
];
344 int result_code
, len
;
345 static char qdisc
[64];
347 if (server
== NULL
) {
348 if (ifname
== NULL
|| qdisc_name
== NULL
)
349 errx(1, "when disabling server communication,\n"
350 "specify both interface (-i) and qdisc (-q)!");
354 /* get qdisc info from the server */
355 snprintf(req
, sizeof(req
), "GET qdisc?%s\n", ifname
);
356 quip_sendrequest(server
, req
);
358 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
359 if (result_code
!= 200)
360 errx(1, "can't get qdisc info");
362 if (sscanf(buf
, "%s", qdisc
) != 1)
363 errx(1, "can't get qdisc name");
365 if (qdisc_name
!= NULL
&& strcmp(qdisc
, qdisc_name
) != 0)
366 errx(1, "qdisc %s on %s doesn't match specified qdisc %s",
367 qdisc
, ifname
, qdisc_name
);
373 quip_chandle2name(const char *ifname
, u_long handle
, char *name
, size_t size
)
375 char buf
[BODY_MAXSIZE
], req
[REQ_MAXSIZE
], *cp
;
376 int result_code
, len
;
382 /* get class name from the server */
383 snprintf(req
, sizeof(req
), "GET handle-to-name?%s:%#lx\n", ifname
, handle
);
384 quip_sendrequest(server
, req
);
386 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
387 if (result_code
!= 200)
388 errx(1, "can't get class name");
390 if ((cp
= strchr(buf
, '\n')) != NULL
)
392 if ((cp
= strrchr(buf
, '/')) != NULL
)
393 strlcpy(name
, cp
+1, size
);
397 quip_printqdisc(const char *ifname
)
399 char buf
[BODY_MAXSIZE
], req
[REQ_MAXSIZE
], *cp
;
400 int result_code
, len
;
402 if (server
== NULL
) {
403 printf("No server available!\n");
407 /* get qdisc info from the server */
408 snprintf(req
, sizeof(req
), "GET qdisc?%s\n", ifname
);
409 quip_sendrequest(server
, req
);
411 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
412 if (result_code
!= 200)
413 errx(1, "can't get qdisc info");
415 /* replace newline by space */
417 while ((cp
= strchr(cp
, '\n')) != NULL
)
420 printf(" qdisc:%s\n", buf
);
424 quip_printfilter(const char *ifname
, const u_long handle
)
426 char buf
[BODY_MAXSIZE
], req
[REQ_MAXSIZE
], *cp
;
427 int result_code
, len
;
429 if (server
== NULL
) {
430 printf("No server available!\n");
434 /* get qdisc info from the server */
435 snprintf(req
, sizeof(req
), "GET filter?%s::%#lx\n", ifname
, handle
);
436 quip_sendrequest(server
, req
);
438 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
439 if (result_code
!= 200)
440 errx(1, "can't get filter info");
442 if ((cp
= strchr(buf
, '\n')) != NULL
)
448 extract_ifname(const char *name
)
452 static char ifname
[64];
454 if ((cp
= strchr(name
, ':')) != NULL
)
459 strncpy(ifname
, name
, len
);
465 quip_printconfig(void)
467 char buf
[BODY_MAXSIZE
], name
[256], *cp
, *p
, *flname
;
468 int result_code
, len
;
472 if (server
== NULL
) {
473 printf("No server available!\n");
477 /* get a total list from the server */
478 quip_sendrequest(server
, "GET list\n");
480 result_code
= quip_recvresponse(server
, NULL
, buf
, &len
);
481 if (result_code
!= 200)
482 errx(1, "can't get total list");
484 printf("------------ current configuration ------------");
488 if (sscanf(cp
, "%lx %255s", &handle
, name
) != 2)
491 if ((p
= strchr(name
, ':')) == NULL
)
493 else if (strchr(p
+1, ':') == NULL
)
500 printf("\ninterface: %s (index:%lu)\n",
502 quip_printqdisc(name
);
505 printf("class: %s (handle:%#lx)\n",
509 flname
= strrchr(name
, ':') + 1;
510 printf(" filter: name:%s [", flname
);
511 quip_printfilter(extract_ifname(name
), handle
);
512 printf("] (handle:%#lx)\n", handle
);
518 if ((cp
= strchr(cp
+1, '\n')) == NULL
)
521 printf("-----------------------------------------------\n\n");