/* $NetBSD: quip_client.c,v 1.16 2024/10/04 15:37:00 rillig Exp $ */
/* $KAME: quip_client.c,v 1.9 2003/05/17 05:59:00 itojun Exp $ */
/*
* Copyright (C) 1999-2000
* Sony Computer Science Laboratories, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* quip (queue information protocol) is a http-like protocol
* in order to retrieve information from the server.
* a unix domain TCP socket "/var/run/altq_quip" is used for
* client-server style communication.
*
* there are 2 quip message types: request and response.
* request format: (only single-line request message is used at this moment)
* request-line
*
* request-line = <method> <operation>[?<query>] <quip-version>
* <method> = GET (only GET is defined at this moment)
* <operation> = list | handle-to-name | qdisc | filter
* query format is operation dependent but most query takes
* <interface> or <class> or <filter>.
* <interface> = <if_name>
* <class> = <if_name>:<class_path>/<class_name>
* <filter> = <if_name>:<class_path>/<class_name>:<filter_name>
* "list" operation accepts "*" as a wildcard.
*
* response format:
* status-line
* response-headers (0 or more)
* <blank line>
* body
*
* status-line = <quip-version> <status-code> <reason phrase>
* response-header = Content-Length:<value>
*
* "Content-Length" specifies the length of the message body.
*
* example:
* to retrieve a list of classes (handle and name) on interface "fxp0":
* a request message looks like,
* GET list?fxp0:* QUIP/1.0<cr>
* a response message looks like,
* QUIP/1.0 200 OK<cr>
* Content-Length:86<cr>
* <cr>
* 0000000000 fxp0:/root<cr>
* 0xc0d1be00 fxp0:/root/parent<cr>
* 0xc0d1ba00 fxp0:/root/parent/child<cr>
*
* other examples:
* list all interfaces, classes, and filters:
* GET list QUIP/1.0<cr>
* list all interfaces:
* GET list?* QUIP/1.0<cr>
* list all classes:
* GET list?*:* QUIP/1.0<cr>
* list all filters:
* GET list?*:*:* QUIP/1.0<cr>
* convert class handle to class name:
* GET handle-to-name?fxp0:0xc0d1be00 QUIP/1.0<cr>
* convert filter handle to filter name:
* GET handle-to-name?fxp0::0x1000000a QUIP/1.0<cr>
*/
if ((cp = strstr(request, "QUIP")) == NULL) {
cp = strchr(request, '\n');
n = cp - request;
if (cp == NULL || n > REQ_MAXSIZE - 10)
return;
strncpy(buf, request, n);
snprintf(buf + n, REQ_MAXSIZE - n, " QUIP/1.0");
strlcat(buf, cp, REQ_MAXSIZE);
}
else
strlcpy(buf, request, REQ_MAXSIZE);
if (fputs(buf, fp) != 0)
err(1, "fputs");
if (fflush(fp) != 0)
err(1, "fflush");
if (quip_echo) {
fputs("<< ", stdout);
fputs(buf, stdout);
}
}
/*
* recv_response receives a response message from the server
* and returns status_code.
*/
int
quip_recvresponse(FILE *fp, char *header, char *body, int *blen)
{
char buf[MAXLINESIZE], version[MAXLINESIZE];
int code, resid, len, buflen;
int end_of_header = 0;