Home | History | Annotate | Line # | Download | only in libaltq
quip_server.c revision 1.3
      1  1.3   itojun /*	$NetBSD: quip_server.c,v 1.3 2001/08/16 07:48:15 itojun Exp $	*/
      2  1.2   itojun /*	$KAME: quip_server.c,v 1.4 2001/08/15 12:51:58 kjc Exp $	*/
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (C) 1999-2000
      5  1.1  thorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     20  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  thorpej  * SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/param.h>
     30  1.1  thorpej #include <sys/socket.h>
     31  1.1  thorpej #include <sys/queue.h>
     32  1.1  thorpej 
     33  1.1  thorpej #include <net/if.h>
     34  1.1  thorpej #include <netinet/in.h>
     35  1.1  thorpej #include <arpa/inet.h>
     36  1.1  thorpej 
     37  1.1  thorpej #include <stdio.h>
     38  1.1  thorpej #include <stdlib.h>
     39  1.1  thorpej #include <unistd.h>
     40  1.1  thorpej #include <stddef.h>
     41  1.1  thorpej #include <string.h>
     42  1.1  thorpej #include <errno.h>
     43  1.1  thorpej #include <err.h>
     44  1.1  thorpej 
     45  1.1  thorpej #include <altq/altq.h>
     46  1.1  thorpej #include <altq/altq_red.h>
     47  1.1  thorpej #include <altq/altq_rio.h>
     48  1.1  thorpej 
     49  1.1  thorpej #include "altq_qop.h"
     50  1.1  thorpej #include "quip_server.h"
     51  1.1  thorpej 
     52  1.1  thorpej extern LIST_HEAD(qop_iflist, ifinfo)	qop_iflist;
     53  1.1  thorpej 
     54  1.1  thorpej #define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)
     55  1.1  thorpej 
     56  1.2   itojun static int next_word(char **, char *);
     57  1.1  thorpej 
     58  1.2   itojun static int query_list(const char *, const char *, char *, size_t);
     59  1.2   itojun static int query_handle2name(const char *, const char *, char *, size_t);
     60  1.2   itojun static int query_qdisc(const char *, const char *, char *, size_t);
     61  1.2   itojun static int query_filterspec(const char *, const char *, char *, size_t);
     62  1.1  thorpej 
     63  1.1  thorpej int
     64  1.1  thorpej quip_input(FILE *fp)
     65  1.1  thorpej {
     66  1.2   itojun 	char request[REQ_MAXSIZE], result[RES_MAXSIZE], body[BODY_MAXSIZE],
     67  1.2   itojun 	    w[REQ_MAXSIZE], *cp, *query;
     68  1.1  thorpej 	int n = 0;
     69  1.1  thorpej 
     70  1.1  thorpej 	while (1) {
     71  1.2   itojun 		if (fgets(request, REQ_MAXSIZE, fp) == NULL)  /* EOF */
     72  1.1  thorpej 			return (-1);
     73  1.1  thorpej 		/* skip preceding blank lines */
     74  1.1  thorpej 		if (request[0] == '\n')
     75  1.1  thorpej 			continue;
     76  1.1  thorpej 		break;
     77  1.1  thorpej 	}
     78  1.1  thorpej 
     79  1.1  thorpej 	/* remove trailing newline and white space */
     80  1.1  thorpej 	if ((cp = strrchr(request, '\n')) != NULL) {
     81  1.1  thorpej 		*cp-- = '\0';
     82  1.1  thorpej 		while (*cp == ' ' || *cp == '\t')
     83  1.1  thorpej 			*cp-- = '\0';
     84  1.1  thorpej 	}
     85  1.1  thorpej 
     86  1.1  thorpej 	body[0] = '\0';
     87  1.1  thorpej 	cp = request;
     88  1.1  thorpej 	if (!next_word(&cp, w)) {
     89  1.2   itojun 		snprintf(result, sizeof(result), "400 Bad request\n");
     90  1.1  thorpej 		goto done;
     91  1.1  thorpej 	}
     92  1.1  thorpej 	if (EQUAL(w, "GET")) {
     93  1.1  thorpej 		if (!next_word(&cp, w)) {
     94  1.2   itojun 			snprintf(result, sizeof(result), "400 Bad request\n");
     95  1.1  thorpej 			goto done;
     96  1.1  thorpej 		}
     97  1.1  thorpej 		if ((query = strchr(w, '?')) != NULL) {
     98  1.1  thorpej 			/* request has a query string */
     99  1.1  thorpej 			*query = '\0';
    100  1.1  thorpej 			query++;
    101  1.1  thorpej 		}
    102  1.1  thorpej 
    103  1.1  thorpej 		if (EQUAL(w, "list")) {
    104  1.2   itojun 			n = query_list(w, query, body, BODY_MAXSIZE);
    105  1.1  thorpej 		} else if (EQUAL(w, "handle-to-name")) {
    106  1.2   itojun 			n = query_handle2name(w, query, body, BODY_MAXSIZE);
    107  1.1  thorpej 		} else if (EQUAL(w, "qdisc")) {
    108  1.2   itojun 			n = query_qdisc(w, query, body, BODY_MAXSIZE);
    109  1.1  thorpej 		} else if (EQUAL(w, "filter")) {
    110  1.2   itojun 			n = query_filterspec(w, query, body, BODY_MAXSIZE);
    111  1.1  thorpej 		} else {
    112  1.2   itojun 			snprintf(result, sizeof(result), "400 Bad request\n");
    113  1.1  thorpej 			goto done;
    114  1.1  thorpej 		}
    115  1.1  thorpej 	} else {
    116  1.2   itojun 		snprintf(result, sizeof(result), "400 Bad request\n");
    117  1.1  thorpej 		goto done;
    118  1.1  thorpej 	}
    119  1.1  thorpej 
    120  1.1  thorpej 	if (n == 0) {
    121  1.2   itojun 		snprintf(result, sizeof(result), "204 No content\n");
    122  1.1  thorpej 	} else if (n < 0) {
    123  1.2   itojun 		snprintf(result, sizeof(result), "400 Bad request\n");
    124  1.1  thorpej 	} else {
    125  1.2   itojun 		snprintf(result, sizeof(result), "200 OK\nContent-Length:%d\n", n);
    126  1.1  thorpej 	}
    127  1.2   itojun 
    128  1.1  thorpej   done:
    129  1.1  thorpej 	/* send a result line and a blank line */
    130  1.1  thorpej 	if (fputs ("QUIP/1.0 ", fp) != 0 ||
    131  1.1  thorpej 	    fputs(result, fp) != 0 || fputs("\n", fp) != 0)
    132  1.1  thorpej 		return (-1);
    133  1.1  thorpej 
    134  1.1  thorpej 	/* send message body */
    135  1.1  thorpej 	if (fputs(body, fp) != 0)
    136  1.1  thorpej 		return (-1);
    137  1.1  thorpej 	return (0);
    138  1.1  thorpej }
    139  1.1  thorpej 
    140  1.1  thorpej /*
    141  1.1  thorpej  * Skip leading blanks, then copy next word (delimited by blank or zero, but
    142  1.1  thorpej  * no longer than 63 bytes) into buffer b, set scan pointer to following
    143  1.1  thorpej  * non-blank (or end of string), and return 1.  If there is no non-blank text,
    144  1.1  thorpej  * set scan ptr to point to 0 byte and return 0.
    145  1.1  thorpej  */
    146  1.1  thorpej static int
    147  1.1  thorpej next_word(char **cpp, char *b)
    148  1.1  thorpej {
    149  1.1  thorpej 	char           *tp;
    150  1.1  thorpej 	int		L;
    151  1.1  thorpej 
    152  1.1  thorpej 	*cpp += strspn(*cpp, " \t");
    153  1.1  thorpej 	if (**cpp == '\0' || **cpp == '\n' || **cpp == '#')
    154  1.1  thorpej 		return(0);
    155  1.1  thorpej 
    156  1.1  thorpej 	tp = strpbrk(*cpp, " \t\n#");
    157  1.1  thorpej 	L = MIN((tp)?(tp-*cpp):strlen(*cpp), 63);
    158  1.1  thorpej 	strncpy(b, *cpp, L);
    159  1.1  thorpej 	*(b + L) = '\0';
    160  1.1  thorpej 	*cpp += L;
    161  1.1  thorpej 	*cpp += strspn(*cpp, " \t");
    162  1.1  thorpej 	return (1);
    163  1.1  thorpej }
    164  1.1  thorpej 
    165  1.1  thorpej 
    166  1.1  thorpej /*
    167  1.1  thorpej  * expand_classname creates a long class name.
    168  1.1  thorpej  *   <ifname>:/<root_name>/../<parent_name>/<class_name>
    169  1.1  thorpej  */
    170  1.1  thorpej static int
    171  1.2   itojun expand_classname(struct classinfo *clinfo, char *name, size_t maxname)
    172  1.1  thorpej {
    173  1.1  thorpej 	struct classinfo *ci = clinfo;
    174  1.2   itojun #define CLASSNAMEMAX	256
    175  1.2   itojun 	char buf[2][CLASSNAMEMAX], *b0, *b1, *tmp;
    176  1.1  thorpej 
    177  1.1  thorpej 	b0 = buf[0]; b1 = buf[1];
    178  1.1  thorpej 	b1[0] = '\0';
    179  1.1  thorpej 	while (ci != NULL) {
    180  1.2   itojun 		strlcpy(b0, "/", CLASSNAMEMAX);
    181  1.2   itojun 		strlcat(b0, ci->clname, CLASSNAMEMAX);
    182  1.2   itojun 		strlcat(b0, b1, CLASSNAMEMAX);
    183  1.1  thorpej 
    184  1.1  thorpej 		ci = ci->parent;
    185  1.1  thorpej 		tmp = b0; b0 = b1; b1 = tmp;
    186  1.1  thorpej 	}
    187  1.2   itojun 	snprintf(b0, CLASSNAMEMAX, "%s:", clinfo->ifinfo->ifname);
    188  1.2   itojun 	strlcat(b0, b1, CLASSNAMEMAX);
    189  1.2   itojun 	strlcpy(name, b0, CLASSNAMEMAX);
    190  1.1  thorpej 	return (strlen(name));
    191  1.2   itojun #undef CLASSNAMEMAX
    192  1.1  thorpej }
    193  1.1  thorpej 
    194  1.1  thorpej /*
    195  1.1  thorpej  * expand_filtername creates a long filter name.
    196  1.1  thorpej  *   <ifname>:/<root_name>/../<parent_name>/<class_name>:<fltr_name>
    197  1.1  thorpej  */
    198  1.1  thorpej static int
    199  1.2   itojun expand_filtername(struct fltrinfo *fltrinfo, char *name, size_t maxname)
    200  1.1  thorpej {
    201  1.1  thorpej 	int len;
    202  1.1  thorpej 
    203  1.2   itojun 	len = expand_classname(fltrinfo->clinfo, name, maxname);
    204  1.2   itojun 	len += snprintf(name + len, maxname - len, ":%s", fltrinfo->flname);
    205  1.2   itojun 	return (len);
    206  1.1  thorpej }
    207  1.1  thorpej 
    208  1.1  thorpej static int
    209  1.2   itojun query_handle2name(const char *cmd, const char *arg, char *msg, size_t maxmsg)
    210  1.1  thorpej {
    211  1.1  thorpej 	struct ifinfo *ifinfo;
    212  1.1  thorpej 	struct classinfo *clinfo;
    213  1.1  thorpej 	struct fltrinfo *fltrinfo;
    214  1.1  thorpej 	char *ifname, *class_field, *fltr_field, buf[256], *cp;
    215  1.1  thorpej 	u_long handle;
    216  1.1  thorpej 	int len, size;
    217  1.1  thorpej 
    218  1.2   itojun 	strlcpy(buf, arg, sizeof(buf));
    219  1.1  thorpej 	cp = buf;
    220  1.1  thorpej 	ifname = strsep(&cp, ":");
    221  1.1  thorpej 	class_field = strsep(&cp, ":");
    222  1.1  thorpej 	fltr_field = cp;
    223  1.1  thorpej 
    224  1.1  thorpej 	if (fltr_field != NULL) {
    225  1.1  thorpej 		if (sscanf(fltr_field, "%lx", &handle) != 1)
    226  1.1  thorpej 			return (-1);
    227  1.1  thorpej 		if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    228  1.1  thorpej 			return (-1);
    229  1.1  thorpej 		if ((fltrinfo = flhandle2fltrinfo(ifinfo, handle)) == NULL)
    230  1.1  thorpej 			return (-1);
    231  1.1  thorpej 
    232  1.2   itojun 		len = expand_filtername(fltrinfo, msg, maxmsg);
    233  1.1  thorpej 	} else {
    234  1.1  thorpej 		if (sscanf(class_field, "%lx", &handle) != 1)
    235  1.1  thorpej 			return (-1);
    236  1.1  thorpej 		if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    237  1.1  thorpej 			return (-1);
    238  1.1  thorpej 		if ((clinfo = clhandle2clinfo(ifinfo, handle)) == NULL)
    239  1.1  thorpej 				return (-1);
    240  1.1  thorpej 
    241  1.2   itojun 		len = expand_classname(clinfo, msg, maxmsg);
    242  1.1  thorpej 	}
    243  1.2   itojun 	size = len + snprintf(msg + len, maxmsg - len, "\n");
    244  1.1  thorpej 	return (size);
    245  1.1  thorpej }
    246  1.1  thorpej 
    247  1.1  thorpej static int
    248  1.2   itojun query_qdisc(const char *cmd, const char *arg, char *msg, size_t maxmsg)
    249  1.1  thorpej {
    250  1.1  thorpej 	struct ifinfo *ifinfo;
    251  1.1  thorpej 	int size;
    252  1.1  thorpej 
    253  1.1  thorpej 	if ((ifinfo = ifname2ifinfo(arg)) == NULL)
    254  1.1  thorpej 		return (-1);
    255  1.1  thorpej 
    256  1.2   itojun 	size = snprintf(msg, maxmsg, "%s\nbandwidth:%.2fMbps\nstatus:%s\n",
    257  1.2   itojun 			ifinfo->qdisc->qname, (double)ifinfo->bandwidth/1000000,
    258  1.2   itojun 			(ifinfo->enabled ? "enabled" : "disabled"));
    259  1.1  thorpej 	return (size);
    260  1.1  thorpej }
    261  1.1  thorpej 
    262  1.1  thorpej static int
    263  1.2   itojun query_filterspec(const char *cmd, const char *arg, char *msg, size_t maxmsg)
    264  1.1  thorpej {
    265  1.1  thorpej 	struct ifinfo *ifinfo;
    266  1.1  thorpej 	struct fltrinfo *fltrinfo;
    267  1.1  thorpej 	struct flow_filter *filt;
    268  1.1  thorpej 	char *ifname, *class_field, *fltr_field, buf[256], *cp;
    269  1.1  thorpej 	u_long handle;
    270  1.1  thorpej 	int size;
    271  1.1  thorpej 
    272  1.2   itojun 	strlcpy(buf, arg, sizeof(buf));
    273  1.1  thorpej 	cp = buf;
    274  1.1  thorpej 	ifname = strsep(&cp, ":");
    275  1.1  thorpej 	class_field = strsep(&cp, ":");
    276  1.1  thorpej 	fltr_field = cp;
    277  1.1  thorpej 
    278  1.1  thorpej 	if (fltr_field == NULL)
    279  1.1  thorpej 		return (-1);
    280  1.1  thorpej 	if (sscanf(fltr_field, "%lx", &handle) != 1)
    281  1.1  thorpej 		return (-1);
    282  1.1  thorpej 
    283  1.1  thorpej 	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
    284  1.1  thorpej 		return (-1);
    285  1.1  thorpej 	if ((fltrinfo = flhandle2fltrinfo(ifinfo, handle)) == NULL)
    286  1.1  thorpej 		return (-1);
    287  1.1  thorpej 
    288  1.1  thorpej 	filt = &fltrinfo->fltr;
    289  1.1  thorpej 
    290  1.1  thorpej 	if (filt->ff_flow.fi_family == AF_INET) {
    291  1.1  thorpej 		char src[128], dst[128], smask[128], dmask[128], tos[128];
    292  1.1  thorpej 
    293  1.1  thorpej 		if (filt->ff_flow.fi_dst.s_addr == 0) {
    294  1.2   itojun 			snprintf(dst, sizeof(dst), "0");
    295  1.1  thorpej 			dmask[0] = '\0';
    296  1.1  thorpej 		} else {
    297  1.2   itojun 			snprintf(dst, sizeof(dst), "%s",
    298  1.2   itojun 				 inet_ntoa(filt->ff_flow.fi_dst));
    299  1.1  thorpej 			if (filt->ff_mask.mask_dst.s_addr == 0xffffffff)
    300  1.1  thorpej 				dmask[0] = '\0';
    301  1.1  thorpej 			else
    302  1.2   itojun 				snprintf(dmask, sizeof(dmask), " mask %#x",
    303  1.2   itojun 					 ntoh32(filt->ff_mask.mask_dst.s_addr));
    304  1.1  thorpej 		}
    305  1.1  thorpej 		if (filt->ff_flow.fi_src.s_addr == 0) {
    306  1.2   itojun 			snprintf(src, sizeof(src), "0");
    307  1.1  thorpej 			smask[0] = '\0';
    308  1.1  thorpej 		} else {
    309  1.2   itojun 			snprintf(src, sizeof(src), "%s",
    310  1.2   itojun 				 inet_ntoa(filt->ff_flow.fi_src));
    311  1.1  thorpej 			if (filt->ff_mask.mask_src.s_addr == 0xffffffff)
    312  1.1  thorpej 				smask[0] = '\0';
    313  1.1  thorpej 			else
    314  1.2   itojun 				snprintf(smask, sizeof(smask), " mask %#x",
    315  1.2   itojun 					 ntoh32(filt->ff_mask.mask_src.s_addr));
    316  1.1  thorpej 		}
    317  1.1  thorpej 		if (filt->ff_flow.fi_tos == 0)
    318  1.1  thorpej 			tos[0] = '\0';
    319  1.1  thorpej 		else
    320  1.2   itojun 			snprintf(tos, sizeof(tos), " tos %#x tosmask %#x",
    321  1.2   itojun 				 filt->ff_flow.fi_tos,
    322  1.2   itojun 				 filt->ff_mask.mask_tos);
    323  1.2   itojun 
    324  1.2   itojun 		size = snprintf(msg, maxmsg, "inet %s%s %d %s%s %d %d%s\n",
    325  1.2   itojun 				dst, dmask,
    326  1.2   itojun 				ntoh16(filt->ff_flow.fi_dport),
    327  1.2   itojun 				src, smask,
    328  1.2   itojun 				ntoh16(filt->ff_flow.fi_sport),
    329  1.2   itojun 				filt->ff_flow.fi_proto, tos);
    330  1.1  thorpej 	}
    331  1.1  thorpej #ifdef INET6
    332  1.1  thorpej 	else if (filt->ff_flow.fi_family == AF_INET6) {
    333  1.1  thorpej 		struct flow_filter6 *filt6;
    334  1.1  thorpej 		char dst6[INET6_ADDRSTRLEN], dmask6[INET6_ADDRSTRLEN];
    335  1.1  thorpej 		char src6[INET6_ADDRSTRLEN], smask6[INET6_ADDRSTRLEN];
    336  1.1  thorpej 		char tclass6[128];
    337  1.1  thorpej 		const struct in6_addr mask128 =
    338  1.1  thorpej 		{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    339  1.1  thorpej 		  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}};
    340  1.1  thorpej 
    341  1.1  thorpej 		filt6 = (struct flow_filter6 *)&fltrinfo->fltr;
    342  1.1  thorpej 		if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst)) {
    343  1.2   itojun 			snprintf(dst6, sizeof(dst6), "0");
    344  1.1  thorpej 			dmask6[0] = '\0';
    345  1.1  thorpej 		} else {
    346  1.1  thorpej 			inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_dst,
    347  1.1  thorpej 				  dst6, sizeof(dst6));
    348  1.1  thorpej 			if (IN6_ARE_ADDR_EQUAL(&mask128,
    349  1.1  thorpej 					       &filt6->ff_mask6.mask6_dst))
    350  1.1  thorpej 				dmask6[0] = '\0';
    351  1.1  thorpej 			else {
    352  1.2   itojun 				snprintf(dmask6, sizeof(dmask6), " mask ");
    353  1.1  thorpej 				inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_dst,
    354  1.1  thorpej 					  dmask6 + 6, sizeof(dmask6) -6);
    355  1.1  thorpej 			}
    356  1.1  thorpej 		}
    357  1.1  thorpej 
    358  1.1  thorpej 		if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src)) {
    359  1.2   itojun 			snprintf(src6, sizeof(src6), "0");
    360  1.1  thorpej 			smask6[0] = '\0';
    361  1.1  thorpej 		} else {
    362  1.1  thorpej 			inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_src,
    363  1.1  thorpej 				  src6, sizeof(src6));
    364  1.1  thorpej 			if (IN6_ARE_ADDR_EQUAL(&mask128,
    365  1.1  thorpej 					       &filt6->ff_mask6.mask6_src))
    366  1.1  thorpej 				smask6[0] = '\0';
    367  1.1  thorpej 			else {
    368  1.2   itojun 				snprintf(smask6, sizeof(smask6), " mask ");
    369  1.1  thorpej 				inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_src,
    370  1.1  thorpej 					  smask6 + 6, sizeof(smask6) -6);
    371  1.1  thorpej 			}
    372  1.1  thorpej 		}
    373  1.1  thorpej 		if (filt6->ff_flow6.fi6_tclass == 0)
    374  1.1  thorpej 			tclass6[0] = '\0';
    375  1.1  thorpej 		else
    376  1.2   itojun 			snprintf(tclass6, sizeof(tclass6),
    377  1.2   itojun 				 " tclass %#x tclassmask %#x",
    378  1.2   itojun 				 filt6->ff_flow6.fi6_tclass,
    379  1.2   itojun 				 filt6->ff_mask6.mask6_tclass);
    380  1.2   itojun 
    381  1.2   itojun 		size = snprintf(msg, maxmsg, "inet6 %s%s %d %s%s %d %d%s\n",
    382  1.2   itojun 				dst6, dmask6,
    383  1.2   itojun 				ntoh16(filt6->ff_flow6.fi6_dport),
    384  1.2   itojun 				src6, smask6,
    385  1.2   itojun 				ntoh16(filt6->ff_flow6.fi6_sport),
    386  1.2   itojun 				filt6->ff_flow6.fi6_proto, tclass6);
    387  1.1  thorpej 	}
    388  1.1  thorpej #endif /* INET6 */
    389  1.1  thorpej 
    390  1.1  thorpej 	return (size);
    391  1.1  thorpej }
    392  1.1  thorpej 
    393  1.1  thorpej 
    394  1.1  thorpej /*
    395  1.1  thorpej  * string_match compares 2 strings and returns 1 when s1 matches s2.
    396  1.1  thorpej  *	s1: possibly includes wildcards, "*".
    397  1.1  thorpej  *	s2: must be a full string (should not include "*").
    398  1.1  thorpej  */
    399  1.1  thorpej static int
    400  1.1  thorpej string_match(const char *s1, const char *s2)
    401  1.1  thorpej {
    402  1.1  thorpej 	char *ap, *next, sub[256];
    403  1.1  thorpej 	int prefixlen, sublen;
    404  1.1  thorpej 
    405  1.1  thorpej 	/* if there's no wild card, compare full string */
    406  1.1  thorpej 	if ((ap = strchr(s1, '*')) == NULL)
    407  1.1  thorpej 		return (strcmp(s1, s2) == 0);
    408  1.1  thorpej 
    409  1.1  thorpej 	/* compare string prefix */
    410  1.1  thorpej 	prefixlen = ap - s1;
    411  1.1  thorpej 	if (strncmp(s1, s2, prefixlen) != 0)
    412  1.1  thorpej 		return (0);
    413  1.1  thorpej 	s2 += prefixlen;
    414  1.1  thorpej 
    415  1.1  thorpej 	/*
    416  1.1  thorpej 	 * if there is another wildcard in the rest of the string,
    417  1.1  thorpej 	 * compare the substring between the 2 wildcards.
    418  1.1  thorpej 	 */
    419  1.1  thorpej 	while ((next = strchr(ap + 1, '*')) != NULL) {
    420  1.1  thorpej 		sublen = next - ap - 1;
    421  1.1  thorpej 		strncpy(sub, ap+1, sublen);
    422  1.1  thorpej 		sub[sublen] = '\0';
    423  1.1  thorpej 		if ((s2 = strstr(s2, sub)) == NULL)
    424  1.1  thorpej 			return (0);
    425  1.1  thorpej 
    426  1.1  thorpej 		s2 += sublen;
    427  1.1  thorpej 		ap = next;
    428  1.1  thorpej 	}
    429  1.1  thorpej 
    430  1.1  thorpej 	/* no more wildcard, compare the rest of the string */
    431  1.1  thorpej 	return (strcmp(ap+1, s2+strlen(s2)-strlen(ap+1)) == 0);
    432  1.1  thorpej }
    433  1.1  thorpej 
    434  1.1  thorpej static int
    435  1.2   itojun query_list(const char *cmd, const char *arg, char *msg, size_t maxmsg)
    436  1.1  thorpej {
    437  1.2   itojun 	char tmp[256], *cp, *ep;
    438  1.1  thorpej 	struct ifinfo *ifinfo;
    439  1.1  thorpej 	struct classinfo *clinfo;
    440  1.1  thorpej 	struct fltrinfo *fltrinfo;
    441  1.1  thorpej 	int print_if, print_class, print_fltr, size = 0;
    442  1.1  thorpej 
    443  1.1  thorpej 	if (arg == NULL) {
    444  1.1  thorpej 		/* no arg, print all */
    445  1.1  thorpej 		print_if = print_class = print_fltr = 1;
    446  1.1  thorpej 	} else {
    447  1.1  thorpej 		print_if = print_class = print_fltr = 0;
    448  1.1  thorpej 		if ((cp = strchr(arg, ':')) == NULL)
    449  1.1  thorpej 			print_if = 1;
    450  1.1  thorpej 		else if (strchr(cp+1, ':') == NULL)
    451  1.1  thorpej 			print_class = 1;
    452  1.1  thorpej 		else
    453  1.1  thorpej 			print_fltr = 1;
    454  1.1  thorpej 	}
    455  1.1  thorpej 
    456  1.1  thorpej 	cp = msg;
    457  1.2   itojun 	ep = msg + maxmsg;
    458  1.1  thorpej 	LIST_FOREACH(ifinfo, &qop_iflist, next) {
    459  1.1  thorpej 		if (print_if) {
    460  1.2   itojun 			strlcpy(tmp, ifinfo->ifname, sizeof(tmp));
    461  1.1  thorpej 			if (arg == NULL || string_match(arg, tmp))
    462  1.2   itojun 				cp += snprintf(cp, ep - cp, "%#010x\t%s\n",
    463  1.2   itojun 					       ifinfo->ifindex, tmp);
    464  1.1  thorpej 		}
    465  1.1  thorpej 		if (!print_class && !print_fltr)
    466  1.1  thorpej 			continue;
    467  1.1  thorpej 		for (clinfo = get_rootclass(ifinfo);
    468  1.1  thorpej 			  clinfo != NULL; clinfo = get_nextclass(clinfo)) {
    469  1.1  thorpej 			if (print_class) {
    470  1.2   itojun 				expand_classname(clinfo, tmp, sizeof(tmp));
    471  1.1  thorpej 				if (arg == NULL || string_match(arg, tmp))
    472  1.2   itojun 					cp += snprintf(cp, ep - cp,
    473  1.2   itojun 						       "%#010lx\t%s\n",
    474  1.2   itojun 						       clinfo->handle, tmp);
    475  1.1  thorpej 			}
    476  1.1  thorpej 			if (!print_fltr)
    477  1.1  thorpej 				continue;
    478  1.1  thorpej 			LIST_FOREACH(fltrinfo, &clinfo->fltrlist, next) {
    479  1.2   itojun 				expand_filtername(fltrinfo, tmp, sizeof(tmp));
    480  1.1  thorpej 				if (arg == NULL || string_match(arg, tmp))
    481  1.2   itojun 					cp += snprintf(cp, ep - cp, "%#010lx\t%s\n",
    482  1.1  thorpej 						      fltrinfo->handle, tmp);
    483  1.1  thorpej 			}
    484  1.1  thorpej 		}
    485  1.1  thorpej 	}
    486  1.1  thorpej 	size = cp - msg;
    487  1.1  thorpej 	return (size);
    488  1.1  thorpej }
    489  1.1  thorpej 
    490