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