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