quip_server.c revision 1.4.44.1 1 1.4.44.1 jym /* $NetBSD: quip_server.c,v 1.4.44.1 2009/05/13 19:20:15 jym Exp $ */
2 1.4 itojun /* $KAME: quip_server.c,v 1.6 2001/08/20 06:41:32 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.4.44.1 jym L = MIN((tp)?(tp-*cpp):(int)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.4 itojun snprintf(name + len, maxname - len, ":%s", fltrinfo->flname);
205 1.4 itojun return (strlen(name));
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.4 itojun int len;
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.4 itojun strlcat(msg, "\n", maxmsg);
244 1.4 itojun return (strlen(msg));
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
252 1.1 thorpej if ((ifinfo = ifname2ifinfo(arg)) == NULL)
253 1.1 thorpej return (-1);
254 1.1 thorpej
255 1.4 itojun snprintf(msg, maxmsg, "%s\nbandwidth:%.2fMbps\nstatus:%s\n",
256 1.4 itojun ifinfo->qdisc->qname, (double)ifinfo->bandwidth/1000000,
257 1.2 itojun (ifinfo->enabled ? "enabled" : "disabled"));
258 1.4 itojun return (strlen(msg));
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej static int
262 1.2 itojun query_filterspec(const char *cmd, const char *arg, char *msg, size_t maxmsg)
263 1.1 thorpej {
264 1.1 thorpej struct ifinfo *ifinfo;
265 1.1 thorpej struct fltrinfo *fltrinfo;
266 1.1 thorpej struct flow_filter *filt;
267 1.1 thorpej char *ifname, *class_field, *fltr_field, buf[256], *cp;
268 1.1 thorpej u_long handle;
269 1.1 thorpej
270 1.2 itojun strlcpy(buf, arg, sizeof(buf));
271 1.1 thorpej cp = buf;
272 1.1 thorpej ifname = strsep(&cp, ":");
273 1.1 thorpej class_field = strsep(&cp, ":");
274 1.1 thorpej fltr_field = cp;
275 1.1 thorpej
276 1.1 thorpej if (fltr_field == NULL)
277 1.1 thorpej return (-1);
278 1.1 thorpej if (sscanf(fltr_field, "%lx", &handle) != 1)
279 1.1 thorpej return (-1);
280 1.1 thorpej
281 1.1 thorpej if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
282 1.1 thorpej return (-1);
283 1.1 thorpej if ((fltrinfo = flhandle2fltrinfo(ifinfo, handle)) == NULL)
284 1.1 thorpej return (-1);
285 1.1 thorpej
286 1.1 thorpej filt = &fltrinfo->fltr;
287 1.1 thorpej
288 1.1 thorpej if (filt->ff_flow.fi_family == AF_INET) {
289 1.1 thorpej char src[128], dst[128], smask[128], dmask[128], tos[128];
290 1.1 thorpej
291 1.1 thorpej if (filt->ff_flow.fi_dst.s_addr == 0) {
292 1.2 itojun snprintf(dst, sizeof(dst), "0");
293 1.1 thorpej dmask[0] = '\0';
294 1.1 thorpej } else {
295 1.2 itojun snprintf(dst, sizeof(dst), "%s",
296 1.2 itojun inet_ntoa(filt->ff_flow.fi_dst));
297 1.1 thorpej if (filt->ff_mask.mask_dst.s_addr == 0xffffffff)
298 1.1 thorpej dmask[0] = '\0';
299 1.1 thorpej else
300 1.2 itojun snprintf(dmask, sizeof(dmask), " mask %#x",
301 1.2 itojun ntoh32(filt->ff_mask.mask_dst.s_addr));
302 1.1 thorpej }
303 1.1 thorpej if (filt->ff_flow.fi_src.s_addr == 0) {
304 1.2 itojun snprintf(src, sizeof(src), "0");
305 1.1 thorpej smask[0] = '\0';
306 1.1 thorpej } else {
307 1.2 itojun snprintf(src, sizeof(src), "%s",
308 1.2 itojun inet_ntoa(filt->ff_flow.fi_src));
309 1.1 thorpej if (filt->ff_mask.mask_src.s_addr == 0xffffffff)
310 1.1 thorpej smask[0] = '\0';
311 1.1 thorpej else
312 1.2 itojun snprintf(smask, sizeof(smask), " mask %#x",
313 1.2 itojun ntoh32(filt->ff_mask.mask_src.s_addr));
314 1.1 thorpej }
315 1.1 thorpej if (filt->ff_flow.fi_tos == 0)
316 1.1 thorpej tos[0] = '\0';
317 1.1 thorpej else
318 1.2 itojun snprintf(tos, sizeof(tos), " tos %#x tosmask %#x",
319 1.2 itojun filt->ff_flow.fi_tos,
320 1.2 itojun filt->ff_mask.mask_tos);
321 1.2 itojun
322 1.4 itojun snprintf(msg, maxmsg, "inet %s%s %d %s%s %d %d%s\n",
323 1.4 itojun dst, dmask,
324 1.4 itojun ntoh16(filt->ff_flow.fi_dport),
325 1.4 itojun src, smask,
326 1.4 itojun ntoh16(filt->ff_flow.fi_sport),
327 1.4 itojun filt->ff_flow.fi_proto, tos);
328 1.1 thorpej }
329 1.1 thorpej #ifdef INET6
330 1.1 thorpej else if (filt->ff_flow.fi_family == AF_INET6) {
331 1.1 thorpej struct flow_filter6 *filt6;
332 1.1 thorpej char dst6[INET6_ADDRSTRLEN], dmask6[INET6_ADDRSTRLEN];
333 1.1 thorpej char src6[INET6_ADDRSTRLEN], smask6[INET6_ADDRSTRLEN];
334 1.1 thorpej char tclass6[128];
335 1.1 thorpej const struct in6_addr mask128 =
336 1.1 thorpej {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
337 1.1 thorpej 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}};
338 1.1 thorpej
339 1.1 thorpej filt6 = (struct flow_filter6 *)&fltrinfo->fltr;
340 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst)) {
341 1.2 itojun snprintf(dst6, sizeof(dst6), "0");
342 1.1 thorpej dmask6[0] = '\0';
343 1.1 thorpej } else {
344 1.1 thorpej inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_dst,
345 1.1 thorpej dst6, sizeof(dst6));
346 1.1 thorpej if (IN6_ARE_ADDR_EQUAL(&mask128,
347 1.1 thorpej &filt6->ff_mask6.mask6_dst))
348 1.1 thorpej dmask6[0] = '\0';
349 1.1 thorpej else {
350 1.2 itojun snprintf(dmask6, sizeof(dmask6), " mask ");
351 1.1 thorpej inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_dst,
352 1.1 thorpej dmask6 + 6, sizeof(dmask6) -6);
353 1.1 thorpej }
354 1.1 thorpej }
355 1.1 thorpej
356 1.1 thorpej if (IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src)) {
357 1.2 itojun snprintf(src6, sizeof(src6), "0");
358 1.1 thorpej smask6[0] = '\0';
359 1.1 thorpej } else {
360 1.1 thorpej inet_ntop(AF_INET6, &filt6->ff_flow6.fi6_src,
361 1.1 thorpej src6, sizeof(src6));
362 1.1 thorpej if (IN6_ARE_ADDR_EQUAL(&mask128,
363 1.1 thorpej &filt6->ff_mask6.mask6_src))
364 1.1 thorpej smask6[0] = '\0';
365 1.1 thorpej else {
366 1.2 itojun snprintf(smask6, sizeof(smask6), " mask ");
367 1.1 thorpej inet_ntop(AF_INET6, &filt6->ff_mask6.mask6_src,
368 1.1 thorpej smask6 + 6, sizeof(smask6) -6);
369 1.1 thorpej }
370 1.1 thorpej }
371 1.1 thorpej if (filt6->ff_flow6.fi6_tclass == 0)
372 1.1 thorpej tclass6[0] = '\0';
373 1.1 thorpej else
374 1.2 itojun snprintf(tclass6, sizeof(tclass6),
375 1.2 itojun " tclass %#x tclassmask %#x",
376 1.2 itojun filt6->ff_flow6.fi6_tclass,
377 1.2 itojun filt6->ff_mask6.mask6_tclass);
378 1.2 itojun
379 1.4 itojun snprintf(msg, maxmsg, "inet6 %s%s %d %s%s %d %d%s\n",
380 1.4 itojun dst6, dmask6,
381 1.4 itojun ntoh16(filt6->ff_flow6.fi6_dport),
382 1.4 itojun src6, smask6,
383 1.4 itojun ntoh16(filt6->ff_flow6.fi6_sport),
384 1.4 itojun filt6->ff_flow6.fi6_proto, tclass6);
385 1.1 thorpej }
386 1.1 thorpej #endif /* INET6 */
387 1.1 thorpej
388 1.4 itojun return (strlen(msg));
389 1.1 thorpej }
390 1.1 thorpej
391 1.1 thorpej
392 1.1 thorpej /*
393 1.1 thorpej * string_match compares 2 strings and returns 1 when s1 matches s2.
394 1.1 thorpej * s1: possibly includes wildcards, "*".
395 1.1 thorpej * s2: must be a full string (should not include "*").
396 1.1 thorpej */
397 1.1 thorpej static int
398 1.1 thorpej string_match(const char *s1, const char *s2)
399 1.1 thorpej {
400 1.1 thorpej char *ap, *next, sub[256];
401 1.1 thorpej int prefixlen, sublen;
402 1.1 thorpej
403 1.1 thorpej /* if there's no wild card, compare full string */
404 1.1 thorpej if ((ap = strchr(s1, '*')) == NULL)
405 1.1 thorpej return (strcmp(s1, s2) == 0);
406 1.1 thorpej
407 1.1 thorpej /* compare string prefix */
408 1.1 thorpej prefixlen = ap - s1;
409 1.1 thorpej if (strncmp(s1, s2, prefixlen) != 0)
410 1.1 thorpej return (0);
411 1.1 thorpej s2 += prefixlen;
412 1.1 thorpej
413 1.1 thorpej /*
414 1.1 thorpej * if there is another wildcard in the rest of the string,
415 1.1 thorpej * compare the substring between the 2 wildcards.
416 1.1 thorpej */
417 1.1 thorpej while ((next = strchr(ap + 1, '*')) != NULL) {
418 1.1 thorpej sublen = next - ap - 1;
419 1.1 thorpej strncpy(sub, ap+1, sublen);
420 1.1 thorpej sub[sublen] = '\0';
421 1.1 thorpej if ((s2 = strstr(s2, sub)) == NULL)
422 1.1 thorpej return (0);
423 1.1 thorpej
424 1.1 thorpej s2 += sublen;
425 1.1 thorpej ap = next;
426 1.1 thorpej }
427 1.1 thorpej
428 1.1 thorpej /* no more wildcard, compare the rest of the string */
429 1.1 thorpej return (strcmp(ap+1, s2+strlen(s2)-strlen(ap+1)) == 0);
430 1.1 thorpej }
431 1.1 thorpej
432 1.1 thorpej static int
433 1.2 itojun query_list(const char *cmd, const char *arg, char *msg, size_t maxmsg)
434 1.1 thorpej {
435 1.2 itojun char tmp[256], *cp, *ep;
436 1.1 thorpej struct ifinfo *ifinfo;
437 1.1 thorpej struct classinfo *clinfo;
438 1.1 thorpej struct fltrinfo *fltrinfo;
439 1.4 itojun int print_if, print_class, print_fltr, len;
440 1.1 thorpej
441 1.1 thorpej if (arg == NULL) {
442 1.1 thorpej /* no arg, print all */
443 1.1 thorpej print_if = print_class = print_fltr = 1;
444 1.1 thorpej } else {
445 1.1 thorpej print_if = print_class = print_fltr = 0;
446 1.1 thorpej if ((cp = strchr(arg, ':')) == NULL)
447 1.1 thorpej print_if = 1;
448 1.1 thorpej else if (strchr(cp+1, ':') == NULL)
449 1.1 thorpej print_class = 1;
450 1.1 thorpej else
451 1.1 thorpej print_fltr = 1;
452 1.1 thorpej }
453 1.1 thorpej
454 1.1 thorpej cp = msg;
455 1.2 itojun ep = msg + maxmsg;
456 1.1 thorpej LIST_FOREACH(ifinfo, &qop_iflist, next) {
457 1.1 thorpej if (print_if) {
458 1.2 itojun strlcpy(tmp, ifinfo->ifname, sizeof(tmp));
459 1.4 itojun if (arg == NULL || string_match(arg, tmp)) {
460 1.4 itojun len = snprintf(cp, ep - cp, "%#010x\t%s\n",
461 1.2 itojun ifinfo->ifindex, tmp);
462 1.4 itojun if (len < 0 || len >= ep - cp)
463 1.4 itojun break;
464 1.4 itojun cp += len;
465 1.4 itojun }
466 1.1 thorpej }
467 1.1 thorpej if (!print_class && !print_fltr)
468 1.1 thorpej continue;
469 1.1 thorpej for (clinfo = get_rootclass(ifinfo);
470 1.1 thorpej clinfo != NULL; clinfo = get_nextclass(clinfo)) {
471 1.1 thorpej if (print_class) {
472 1.2 itojun expand_classname(clinfo, tmp, sizeof(tmp));
473 1.4 itojun if (arg == NULL || string_match(arg, tmp)) {
474 1.4 itojun len = snprintf(cp, ep - cp,
475 1.2 itojun "%#010lx\t%s\n",
476 1.2 itojun clinfo->handle, tmp);
477 1.4 itojun if (len < 0 || len >= ep - cp)
478 1.4 itojun break;
479 1.4 itojun cp += len;
480 1.4 itojun }
481 1.1 thorpej }
482 1.1 thorpej if (!print_fltr)
483 1.1 thorpej continue;
484 1.1 thorpej LIST_FOREACH(fltrinfo, &clinfo->fltrlist, next) {
485 1.2 itojun expand_filtername(fltrinfo, tmp, sizeof(tmp));
486 1.4 itojun if (arg == NULL || string_match(arg, tmp)) {
487 1.4 itojun len = snprintf(cp, ep - cp, "%#010lx\t%s\n",
488 1.4 itojun fltrinfo->handle, tmp);
489 1.4 itojun if (len < 0 || len >= ep - cp)
490 1.4 itojun break;
491 1.4 itojun cp += len;
492 1.4 itojun }
493 1.1 thorpej }
494 1.1 thorpej }
495 1.1 thorpej }
496 1.4 itojun return (strlen(msg));
497 1.1 thorpej }
498 1.1 thorpej
499