Remove empty DragonFly CVS IDs.
[dragonfly.git] / sys / netgraph7 / netgraph / ng_parse.c
blobf862f3910ba1d63a1c6985e38ef2c4783d19d2cb
1 /*
2 * ng_parse.c
3 */
5 /*-
6 * Copyright (c) 1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.30 2007/06/23 00:02:20 mjacob Exp $
42 * $DragonFly: src/sys/netgraph7/ng_parse.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/limits.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/ctype.h>
55 #include <machine/stdarg.h>
57 #include <net/ethernet.h>
59 #include <netinet/in.h>
61 #include <netgraph7/ng_message.h>
62 #include <netgraph7/netgraph.h>
63 #include <netgraph7/ng_parse.h>
65 #ifdef NG_SEPARATE_MALLOC
66 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
67 #else
68 #define M_NETGRAPH_PARSE M_NETGRAPH
69 #endif
71 /* Compute alignment for primitive integral types */
72 struct int16_temp {
73 char x;
74 int16_t y;
77 struct int32_temp {
78 char x;
79 int32_t y;
82 struct int64_temp {
83 char x;
84 int64_t y;
87 #define INT8_ALIGNMENT 1
88 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
89 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
90 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
92 /* Output format for integral types */
93 #define INT_UNSIGNED 0
94 #define INT_SIGNED 1
95 #define INT_HEX 2
97 /* Type of composite object: struct, array, or fixedarray */
98 enum comptype {
99 CT_STRUCT,
100 CT_ARRAY,
101 CT_FIXEDARRAY,
104 /* Composite types helper functions */
105 static int ng_parse_composite(const struct ng_parse_type *type,
106 const char *s, int *off, const u_char *start,
107 u_char *const buf, int *buflen, enum comptype ctype);
108 static int ng_unparse_composite(const struct ng_parse_type *type,
109 const u_char *data, int *off, char *cbuf, int cbuflen,
110 enum comptype ctype);
111 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
112 int index, const u_char *start, u_char *buf,
113 int *buflen, enum comptype ctype);
114 static int ng_get_composite_len(const struct ng_parse_type *type,
115 const u_char *start, const u_char *buf,
116 enum comptype ctype);
117 static const struct ng_parse_type *ng_get_composite_etype(const struct
118 ng_parse_type *type, int index, enum comptype ctype);
119 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
120 int index, enum comptype ctype, int posn);
122 /* Parsing helper functions */
123 static int ng_parse_skip_value(const char *s, int off, int *lenp);
124 static int ng_parse_append(char **cbufp, int *cbuflenp,
125 const char *fmt, ...);
127 /* Poor man's virtual method calls */
128 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
129 #define INVOKE(t,m) (*METHOD(t,m))
131 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
132 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
133 static ng_getDefault_t *ng_get_getDefault_method(const
134 struct ng_parse_type *t);
135 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
137 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
138 0 : INVOKE(t, getAlign)(t))
140 /************************************************************************
141 PUBLIC FUNCTIONS
142 ************************************************************************/
145 * Convert an ASCII string to binary according to the supplied type descriptor
148 ng_parse(const struct ng_parse_type *type,
149 const char *string, int *off, u_char *buf, int *buflen)
151 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
155 * Convert binary to an ASCII string according to the supplied type descriptor
158 ng_unparse(const struct ng_parse_type *type,
159 const u_char *data, char *cbuf, int cbuflen)
161 int off = 0;
163 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
167 * Fill in the default value according to the supplied type descriptor
170 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
172 ng_getDefault_t *const func = METHOD(type, getDefault);
174 if (func == NULL)
175 return (EOPNOTSUPP);
176 return (*func)(type, buf, buf, buflen);
180 /************************************************************************
181 STRUCTURE TYPE
182 ************************************************************************/
184 static int
185 ng_struct_parse(const struct ng_parse_type *type,
186 const char *s, int *off, const u_char *const start,
187 u_char *const buf, int *buflen)
189 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
192 static int
193 ng_struct_unparse(const struct ng_parse_type *type,
194 const u_char *data, int *off, char *cbuf, int cbuflen)
196 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
199 static int
200 ng_struct_getDefault(const struct ng_parse_type *type,
201 const u_char *const start, u_char *buf, int *buflen)
203 int off = 0;
205 return ng_parse_composite(type,
206 "{}", &off, start, buf, buflen, CT_STRUCT);
209 static int
210 ng_struct_getAlign(const struct ng_parse_type *type)
212 const struct ng_parse_struct_field *field;
213 int align = 0;
215 for (field = type->info; field->name != NULL; field++) {
216 int falign = ALIGNMENT(field->type);
218 if (falign > align)
219 align = falign;
221 return align;
224 const struct ng_parse_type ng_parse_struct_type = {
225 NULL,
226 NULL,
227 NULL,
228 ng_struct_parse,
229 ng_struct_unparse,
230 ng_struct_getDefault,
231 ng_struct_getAlign
234 /************************************************************************
235 FIXED LENGTH ARRAY TYPE
236 ************************************************************************/
238 static int
239 ng_fixedarray_parse(const struct ng_parse_type *type,
240 const char *s, int *off, const u_char *const start,
241 u_char *const buf, int *buflen)
243 return ng_parse_composite(type,
244 s, off, start, buf, buflen, CT_FIXEDARRAY);
247 static int
248 ng_fixedarray_unparse(const struct ng_parse_type *type,
249 const u_char *data, int *off, char *cbuf, int cbuflen)
251 return ng_unparse_composite(type,
252 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
255 static int
256 ng_fixedarray_getDefault(const struct ng_parse_type *type,
257 const u_char *const start, u_char *buf, int *buflen)
259 int off = 0;
261 return ng_parse_composite(type,
262 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
265 static int
266 ng_fixedarray_getAlign(const struct ng_parse_type *type)
268 const struct ng_parse_fixedarray_info *fi = type->info;
270 return ALIGNMENT(fi->elementType);
273 const struct ng_parse_type ng_parse_fixedarray_type = {
274 NULL,
275 NULL,
276 NULL,
277 ng_fixedarray_parse,
278 ng_fixedarray_unparse,
279 ng_fixedarray_getDefault,
280 ng_fixedarray_getAlign
283 /************************************************************************
284 VARIABLE LENGTH ARRAY TYPE
285 ************************************************************************/
287 static int
288 ng_array_parse(const struct ng_parse_type *type,
289 const char *s, int *off, const u_char *const start,
290 u_char *const buf, int *buflen)
292 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
295 static int
296 ng_array_unparse(const struct ng_parse_type *type,
297 const u_char *data, int *off, char *cbuf, int cbuflen)
299 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
302 static int
303 ng_array_getDefault(const struct ng_parse_type *type,
304 const u_char *const start, u_char *buf, int *buflen)
306 int off = 0;
308 return ng_parse_composite(type,
309 "[]", &off, start, buf, buflen, CT_ARRAY);
312 static int
313 ng_array_getAlign(const struct ng_parse_type *type)
315 const struct ng_parse_array_info *ai = type->info;
317 return ALIGNMENT(ai->elementType);
320 const struct ng_parse_type ng_parse_array_type = {
321 NULL,
322 NULL,
323 NULL,
324 ng_array_parse,
325 ng_array_unparse,
326 ng_array_getDefault,
327 ng_array_getAlign
330 /************************************************************************
331 INT8 TYPE
332 ************************************************************************/
334 static int
335 ng_int8_parse(const struct ng_parse_type *type,
336 const char *s, int *off, const u_char *const start,
337 u_char *const buf, int *buflen)
339 long val;
340 int8_t val8;
341 char *eptr;
343 val = strtol(s + *off, &eptr, 0);
344 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
345 return (EINVAL);
346 *off = eptr - s;
347 val8 = (int8_t)val;
348 bcopy(&val8, buf, sizeof(int8_t));
349 *buflen = sizeof(int8_t);
350 return (0);
353 static int
354 ng_int8_unparse(const struct ng_parse_type *type,
355 const u_char *data, int *off, char *cbuf, int cbuflen)
357 const char *fmt;
358 int fval;
359 int error;
360 int8_t val;
362 bcopy(data + *off, &val, sizeof(int8_t));
363 switch ((intptr_t)type->info) {
364 case INT_SIGNED:
365 fmt = "%d";
366 fval = val;
367 break;
368 case INT_UNSIGNED:
369 fmt = "%u";
370 fval = (u_int8_t)val;
371 break;
372 case INT_HEX:
373 fmt = "0x%x";
374 fval = (u_int8_t)val;
375 break;
376 default:
377 panic("%s: unknown type", __func__);
378 #ifdef RESTARTABLE_PANICS
379 return(0);
380 #endif
382 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
383 return (error);
384 *off += sizeof(int8_t);
385 return (0);
388 static int
389 ng_int8_getDefault(const struct ng_parse_type *type,
390 const u_char *const start, u_char *buf, int *buflen)
392 int8_t val;
394 if (*buflen < sizeof(int8_t))
395 return (ERANGE);
396 val = 0;
397 bcopy(&val, buf, sizeof(int8_t));
398 *buflen = sizeof(int8_t);
399 return (0);
402 static int
403 ng_int8_getAlign(const struct ng_parse_type *type)
405 return INT8_ALIGNMENT;
408 const struct ng_parse_type ng_parse_int8_type = {
409 NULL,
410 (void *)INT_SIGNED,
411 NULL,
412 ng_int8_parse,
413 ng_int8_unparse,
414 ng_int8_getDefault,
415 ng_int8_getAlign
418 const struct ng_parse_type ng_parse_uint8_type = {
419 &ng_parse_int8_type,
420 (void *)INT_UNSIGNED
423 const struct ng_parse_type ng_parse_hint8_type = {
424 &ng_parse_int8_type,
425 (void *)INT_HEX
428 /************************************************************************
429 INT16 TYPE
430 ************************************************************************/
432 static int
433 ng_int16_parse(const struct ng_parse_type *type,
434 const char *s, int *off, const u_char *const start,
435 u_char *const buf, int *buflen)
437 long val;
438 int16_t val16;
439 char *eptr;
441 val = strtol(s + *off, &eptr, 0);
442 if (val < (int16_t)0x8000
443 || val > (u_int16_t)0xffff || eptr == s + *off)
444 return (EINVAL);
445 *off = eptr - s;
446 val16 = (int16_t)val;
447 bcopy(&val16, buf, sizeof(int16_t));
448 *buflen = sizeof(int16_t);
449 return (0);
452 static int
453 ng_int16_unparse(const struct ng_parse_type *type,
454 const u_char *data, int *off, char *cbuf, int cbuflen)
456 const char *fmt;
457 int fval;
458 int error;
459 int16_t val;
461 bcopy(data + *off, &val, sizeof(int16_t));
462 switch ((intptr_t)type->info) {
463 case INT_SIGNED:
464 fmt = "%d";
465 fval = val;
466 break;
467 case INT_UNSIGNED:
468 fmt = "%u";
469 fval = (u_int16_t)val;
470 break;
471 case INT_HEX:
472 fmt = "0x%x";
473 fval = (u_int16_t)val;
474 break;
475 default:
476 panic("%s: unknown type", __func__);
477 #ifdef RESTARTABLE_PANICS
478 return(0);
479 #endif
481 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
482 return (error);
483 *off += sizeof(int16_t);
484 return (0);
487 static int
488 ng_int16_getDefault(const struct ng_parse_type *type,
489 const u_char *const start, u_char *buf, int *buflen)
491 int16_t val;
493 if (*buflen < sizeof(int16_t))
494 return (ERANGE);
495 val = 0;
496 bcopy(&val, buf, sizeof(int16_t));
497 *buflen = sizeof(int16_t);
498 return (0);
501 static int
502 ng_int16_getAlign(const struct ng_parse_type *type)
504 return INT16_ALIGNMENT;
507 const struct ng_parse_type ng_parse_int16_type = {
508 NULL,
509 (void *)INT_SIGNED,
510 NULL,
511 ng_int16_parse,
512 ng_int16_unparse,
513 ng_int16_getDefault,
514 ng_int16_getAlign
517 const struct ng_parse_type ng_parse_uint16_type = {
518 &ng_parse_int16_type,
519 (void *)INT_UNSIGNED
522 const struct ng_parse_type ng_parse_hint16_type = {
523 &ng_parse_int16_type,
524 (void *)INT_HEX
527 /************************************************************************
528 INT32 TYPE
529 ************************************************************************/
531 static int
532 ng_int32_parse(const struct ng_parse_type *type,
533 const char *s, int *off, const u_char *const start,
534 u_char *const buf, int *buflen)
536 long val; /* assumes long is at least 32 bits */
537 int32_t val32;
538 char *eptr;
540 if ((intptr_t)type->info == INT_SIGNED)
541 val = strtol(s + *off, &eptr, 0);
542 else
543 val = strtoul(s + *off, &eptr, 0);
544 if (val < (int32_t)0x80000000
545 || val > (u_int32_t)0xffffffff || eptr == s + *off)
546 return (EINVAL);
547 *off = eptr - s;
548 val32 = (int32_t)val;
549 bcopy(&val32, buf, sizeof(int32_t));
550 *buflen = sizeof(int32_t);
551 return (0);
554 static int
555 ng_int32_unparse(const struct ng_parse_type *type,
556 const u_char *data, int *off, char *cbuf, int cbuflen)
558 const char *fmt;
559 long fval;
560 int error;
561 int32_t val;
563 bcopy(data + *off, &val, sizeof(int32_t));
564 switch ((intptr_t)type->info) {
565 case INT_SIGNED:
566 fmt = "%ld";
567 fval = val;
568 break;
569 case INT_UNSIGNED:
570 fmt = "%lu";
571 fval = (u_int32_t)val;
572 break;
573 case INT_HEX:
574 fmt = "0x%lx";
575 fval = (u_int32_t)val;
576 break;
577 default:
578 panic("%s: unknown type", __func__);
579 #ifdef RESTARTABLE_PANICS
580 return(0);
581 #endif
583 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
584 return (error);
585 *off += sizeof(int32_t);
586 return (0);
589 static int
590 ng_int32_getDefault(const struct ng_parse_type *type,
591 const u_char *const start, u_char *buf, int *buflen)
593 int32_t val;
595 if (*buflen < sizeof(int32_t))
596 return (ERANGE);
597 val = 0;
598 bcopy(&val, buf, sizeof(int32_t));
599 *buflen = sizeof(int32_t);
600 return (0);
603 static int
604 ng_int32_getAlign(const struct ng_parse_type *type)
606 return INT32_ALIGNMENT;
609 const struct ng_parse_type ng_parse_int32_type = {
610 NULL,
611 (void *)INT_SIGNED,
612 NULL,
613 ng_int32_parse,
614 ng_int32_unparse,
615 ng_int32_getDefault,
616 ng_int32_getAlign
619 const struct ng_parse_type ng_parse_uint32_type = {
620 &ng_parse_int32_type,
621 (void *)INT_UNSIGNED
624 const struct ng_parse_type ng_parse_hint32_type = {
625 &ng_parse_int32_type,
626 (void *)INT_HEX
629 /************************************************************************
630 INT64 TYPE
631 ************************************************************************/
633 static int
634 ng_int64_parse(const struct ng_parse_type *type,
635 const char *s, int *off, const u_char *const start,
636 u_char *const buf, int *buflen)
638 quad_t val;
639 int64_t val64;
640 char *eptr;
642 val = strtoq(s + *off, &eptr, 0);
643 if (eptr == s + *off)
644 return (EINVAL);
645 *off = eptr - s;
646 val64 = (int64_t)val;
647 bcopy(&val64, buf, sizeof(int64_t));
648 *buflen = sizeof(int64_t);
649 return (0);
652 static int
653 ng_int64_unparse(const struct ng_parse_type *type,
654 const u_char *data, int *off, char *cbuf, int cbuflen)
656 const char *fmt;
657 long long fval;
658 int64_t val;
659 int error;
661 bcopy(data + *off, &val, sizeof(int64_t));
662 switch ((intptr_t)type->info) {
663 case INT_SIGNED:
664 fmt = "%lld";
665 fval = val;
666 break;
667 case INT_UNSIGNED:
668 fmt = "%llu";
669 fval = (u_int64_t)val;
670 break;
671 case INT_HEX:
672 fmt = "0x%llx";
673 fval = (u_int64_t)val;
674 break;
675 default:
676 panic("%s: unknown type", __func__);
677 #ifdef RESTARTABLE_PANICS
678 return(0);
679 #endif
681 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
682 return (error);
683 *off += sizeof(int64_t);
684 return (0);
687 static int
688 ng_int64_getDefault(const struct ng_parse_type *type,
689 const u_char *const start, u_char *buf, int *buflen)
691 int64_t val;
693 if (*buflen < sizeof(int64_t))
694 return (ERANGE);
695 val = 0;
696 bcopy(&val, buf, sizeof(int64_t));
697 *buflen = sizeof(int64_t);
698 return (0);
701 static int
702 ng_int64_getAlign(const struct ng_parse_type *type)
704 return INT64_ALIGNMENT;
707 const struct ng_parse_type ng_parse_int64_type = {
708 NULL,
709 (void *)INT_SIGNED,
710 NULL,
711 ng_int64_parse,
712 ng_int64_unparse,
713 ng_int64_getDefault,
714 ng_int64_getAlign
717 const struct ng_parse_type ng_parse_uint64_type = {
718 &ng_parse_int64_type,
719 (void *)INT_UNSIGNED
722 const struct ng_parse_type ng_parse_hint64_type = {
723 &ng_parse_int64_type,
724 (void *)INT_HEX
727 /************************************************************************
728 STRING TYPE
729 ************************************************************************/
731 static int
732 ng_string_parse(const struct ng_parse_type *type,
733 const char *s, int *off, const u_char *const start,
734 u_char *const buf, int *buflen)
736 char *sval;
737 int len;
738 int slen;
740 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
741 return (EINVAL);
742 *off += len;
743 bcopy(sval, buf, slen + 1);
744 kfree(sval, M_NETGRAPH_PARSE);
745 *buflen = slen + 1;
746 return (0);
749 static int
750 ng_string_unparse(const struct ng_parse_type *type,
751 const u_char *data, int *off, char *cbuf, int cbuflen)
753 const char *const raw = (const char *)data + *off;
754 char *const s = ng_encode_string(raw, strlen(raw));
755 int error;
757 if (s == NULL)
758 return (ENOMEM);
759 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
760 kfree(s, M_NETGRAPH_PARSE);
761 return (error);
763 *off += strlen(raw) + 1;
764 kfree(s, M_NETGRAPH_PARSE);
765 return (0);
768 static int
769 ng_string_getDefault(const struct ng_parse_type *type,
770 const u_char *const start, u_char *buf, int *buflen)
773 if (*buflen < 1)
774 return (ERANGE);
775 buf[0] = (u_char)'\0';
776 *buflen = 1;
777 return (0);
780 const struct ng_parse_type ng_parse_string_type = {
781 NULL,
782 NULL,
783 NULL,
784 ng_string_parse,
785 ng_string_unparse,
786 ng_string_getDefault,
787 NULL
790 /************************************************************************
791 FIXED BUFFER STRING TYPE
792 ************************************************************************/
794 static int
795 ng_fixedstring_parse(const struct ng_parse_type *type,
796 const char *s, int *off, const u_char *const start,
797 u_char *const buf, int *buflen)
799 const struct ng_parse_fixedstring_info *const fi = type->info;
800 char *sval;
801 int len;
802 int slen;
804 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
805 return (EINVAL);
806 if (slen + 1 > fi->bufSize) {
807 kfree(sval, M_NETGRAPH_PARSE);
808 return (E2BIG);
810 *off += len;
811 bcopy(sval, buf, slen);
812 kfree(sval, M_NETGRAPH_PARSE);
813 bzero(buf + slen, fi->bufSize - slen);
814 *buflen = fi->bufSize;
815 return (0);
818 static int
819 ng_fixedstring_unparse(const struct ng_parse_type *type,
820 const u_char *data, int *off, char *cbuf, int cbuflen)
822 const struct ng_parse_fixedstring_info *const fi = type->info;
823 int error, temp = *off;
825 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
826 return (error);
827 *off += fi->bufSize;
828 return (0);
831 static int
832 ng_fixedstring_getDefault(const struct ng_parse_type *type,
833 const u_char *const start, u_char *buf, int *buflen)
835 const struct ng_parse_fixedstring_info *const fi = type->info;
837 if (*buflen < fi->bufSize)
838 return (ERANGE);
839 bzero(buf, fi->bufSize);
840 *buflen = fi->bufSize;
841 return (0);
844 const struct ng_parse_type ng_parse_fixedstring_type = {
845 NULL,
846 NULL,
847 NULL,
848 ng_fixedstring_parse,
849 ng_fixedstring_unparse,
850 ng_fixedstring_getDefault,
851 NULL
854 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
855 NG_NODESIZ
857 const struct ng_parse_type ng_parse_nodebuf_type = {
858 &ng_parse_fixedstring_type,
859 &ng_parse_nodebuf_info
862 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
863 NG_HOOKSIZ
865 const struct ng_parse_type ng_parse_hookbuf_type = {
866 &ng_parse_fixedstring_type,
867 &ng_parse_hookbuf_info
870 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
871 NG_PATHSIZ
873 const struct ng_parse_type ng_parse_pathbuf_type = {
874 &ng_parse_fixedstring_type,
875 &ng_parse_pathbuf_info
878 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
879 NG_TYPESIZ
881 const struct ng_parse_type ng_parse_typebuf_type = {
882 &ng_parse_fixedstring_type,
883 &ng_parse_typebuf_info
886 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
887 NG_CMDSTRSIZ
889 const struct ng_parse_type ng_parse_cmdbuf_type = {
890 &ng_parse_fixedstring_type,
891 &ng_parse_cmdbuf_info
894 /************************************************************************
895 EXPLICITLY SIZED STRING TYPE
896 ************************************************************************/
898 static int
899 ng_sizedstring_parse(const struct ng_parse_type *type,
900 const char *s, int *off, const u_char *const start,
901 u_char *const buf, int *buflen)
903 char *sval;
904 int len;
905 int slen;
907 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
908 return (EINVAL);
909 if (slen > USHRT_MAX) {
910 kfree(sval, M_NETGRAPH_PARSE);
911 return (EINVAL);
913 *off += len;
914 *((u_int16_t *)buf) = (u_int16_t)slen;
915 bcopy(sval, buf + 2, slen);
916 kfree(sval, M_NETGRAPH_PARSE);
917 *buflen = 2 + slen;
918 return (0);
921 static int
922 ng_sizedstring_unparse(const struct ng_parse_type *type,
923 const u_char *data, int *off, char *cbuf, int cbuflen)
925 const char *const raw = (const char *)data + *off + 2;
926 const int slen = *((const u_int16_t *)(data + *off));
927 char *const s = ng_encode_string(raw, slen);
928 int error;
930 if (s == NULL)
931 return (ENOMEM);
932 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
933 kfree(s, M_NETGRAPH_PARSE);
934 return (error);
936 kfree(s, M_NETGRAPH_PARSE);
937 *off += slen + 2;
938 return (0);
941 static int
942 ng_sizedstring_getDefault(const struct ng_parse_type *type,
943 const u_char *const start, u_char *buf, int *buflen)
945 if (*buflen < 2)
946 return (ERANGE);
947 bzero(buf, 2);
948 *buflen = 2;
949 return (0);
952 const struct ng_parse_type ng_parse_sizedstring_type = {
953 NULL,
954 NULL,
955 NULL,
956 ng_sizedstring_parse,
957 ng_sizedstring_unparse,
958 ng_sizedstring_getDefault,
959 NULL
962 /************************************************************************
963 IP ADDRESS TYPE
964 ************************************************************************/
966 static int
967 ng_ipaddr_parse(const struct ng_parse_type *type,
968 const char *s, int *off, const u_char *const start,
969 u_char *const buf, int *buflen)
971 int i, error;
973 for (i = 0; i < 4; i++) {
974 if ((error = ng_int8_parse(&ng_parse_int8_type,
975 s, off, start, buf + i, buflen)) != 0)
976 return (error);
977 if (i < 3 && s[*off] != '.')
978 return (EINVAL);
979 (*off)++;
981 *buflen = 4;
982 return (0);
985 static int
986 ng_ipaddr_unparse(const struct ng_parse_type *type,
987 const u_char *data, int *off, char *cbuf, int cbuflen)
989 struct in_addr ip;
990 int error;
992 bcopy(data + *off, &ip, sizeof(ip));
993 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
994 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
995 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
996 return (error);
997 *off += sizeof(ip);
998 return (0);
1001 static int
1002 ng_ipaddr_getDefault(const struct ng_parse_type *type,
1003 const u_char *const start, u_char *buf, int *buflen)
1005 struct in_addr ip = { 0 };
1007 if (*buflen < sizeof(ip))
1008 return (ERANGE);
1009 bcopy(&ip, buf, sizeof(ip));
1010 *buflen = sizeof(ip);
1011 return (0);
1014 const struct ng_parse_type ng_parse_ipaddr_type = {
1015 NULL,
1016 NULL,
1017 NULL,
1018 ng_ipaddr_parse,
1019 ng_ipaddr_unparse,
1020 ng_ipaddr_getDefault,
1021 ng_int32_getAlign
1024 /************************************************************************
1025 ETHERNET ADDRESS TYPE
1026 ************************************************************************/
1028 static int
1029 ng_enaddr_parse(const struct ng_parse_type *type,
1030 const char *s, int *const off, const u_char *const start,
1031 u_char *const buf, int *const buflen)
1033 char *eptr;
1034 u_long val;
1035 int i;
1037 if (*buflen < ETHER_ADDR_LEN)
1038 return (ERANGE);
1039 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1040 val = strtoul(s + *off, &eptr, 16);
1041 if (val > 0xff || eptr == s + *off)
1042 return (EINVAL);
1043 buf[i] = (u_char)val;
1044 *off = (eptr - s);
1045 if (i < ETHER_ADDR_LEN - 1) {
1046 if (*eptr != ':')
1047 return (EINVAL);
1048 (*off)++;
1051 *buflen = ETHER_ADDR_LEN;
1052 return (0);
1055 static int
1056 ng_enaddr_unparse(const struct ng_parse_type *type,
1057 const u_char *data, int *off, char *cbuf, int cbuflen)
1059 int len;
1061 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1062 data[*off], data[*off + 1], data[*off + 2],
1063 data[*off + 3], data[*off + 4], data[*off + 5]);
1064 if (len >= cbuflen)
1065 return (ERANGE);
1066 *off += ETHER_ADDR_LEN;
1067 return (0);
1070 const struct ng_parse_type ng_parse_enaddr_type = {
1071 NULL,
1072 NULL,
1073 NULL,
1074 ng_enaddr_parse,
1075 ng_enaddr_unparse,
1076 NULL,
1080 /************************************************************************
1081 BYTE ARRAY TYPE
1082 ************************************************************************/
1084 /* Get the length of a byte array */
1085 static int
1086 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1087 const u_char *start, const u_char *buf)
1089 ng_parse_array_getLength_t *const getLength = type->private;
1091 return (*getLength)(type, start, buf);
1094 /* Byte array element type is hex int8 */
1095 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1096 &ng_parse_hint8_type,
1097 &ng_parse_bytearray_subtype_getLength,
1098 NULL
1100 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1101 &ng_parse_array_type,
1102 &ng_parse_bytearray_subtype_info
1105 static int
1106 ng_bytearray_parse(const struct ng_parse_type *type,
1107 const char *s, int *off, const u_char *const start,
1108 u_char *const buf, int *buflen)
1110 char *str;
1111 int toklen;
1112 int slen;
1114 /* We accept either an array of bytes or a string constant */
1115 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1116 ng_parse_array_getLength_t *const getLength = type->info;
1117 int arraylen;
1119 arraylen = (*getLength)(type, start, buf);
1120 if (arraylen > *buflen) {
1121 kfree(str, M_NETGRAPH_PARSE);
1122 return (ERANGE);
1124 if (slen > arraylen) {
1125 kfree(str, M_NETGRAPH_PARSE);
1126 return (E2BIG);
1128 bcopy(str, buf, slen);
1129 bzero(buf + slen, arraylen - slen);
1130 kfree(str, M_NETGRAPH_PARSE);
1131 *off += toklen;
1132 *buflen = arraylen;
1133 return (0);
1134 } else {
1135 struct ng_parse_type subtype;
1137 subtype = ng_parse_bytearray_subtype;
1138 *(const void **)&subtype.private = type->info;
1139 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1143 static int
1144 ng_bytearray_unparse(const struct ng_parse_type *type,
1145 const u_char *data, int *off, char *cbuf, int cbuflen)
1147 struct ng_parse_type subtype;
1149 subtype = ng_parse_bytearray_subtype;
1150 *(const void **)&subtype.private = type->info;
1151 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1154 static int
1155 ng_bytearray_getDefault(const struct ng_parse_type *type,
1156 const u_char *const start, u_char *buf, int *buflen)
1158 struct ng_parse_type subtype;
1160 subtype = ng_parse_bytearray_subtype;
1161 *(const void **)&subtype.private = type->info;
1162 return ng_array_getDefault(&subtype, start, buf, buflen);
1165 const struct ng_parse_type ng_parse_bytearray_type = {
1166 NULL,
1167 NULL,
1168 NULL,
1169 ng_bytearray_parse,
1170 ng_bytearray_unparse,
1171 ng_bytearray_getDefault,
1172 NULL
1175 /************************************************************************
1176 STRUCT NG_MESG TYPE
1177 ************************************************************************/
1179 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1180 static int
1181 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1182 const u_char *start, const u_char *buf)
1184 const struct ng_mesg *msg;
1186 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1187 return msg->header.arglen;
1190 /* Type for the variable length data portion of a struct ng_mesg */
1191 static const struct ng_parse_type ng_msg_data_type = {
1192 &ng_parse_bytearray_type,
1193 &ng_parse_ng_mesg_getLength
1196 /* Type for the entire struct ng_mesg header with data section */
1197 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1198 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1199 const struct ng_parse_type ng_parse_ng_mesg_type = {
1200 &ng_parse_struct_type,
1201 &ng_parse_ng_mesg_type_fields,
1204 /************************************************************************
1205 COMPOSITE HELPER ROUTINES
1206 ************************************************************************/
1209 * Convert a structure or array from ASCII to binary
1211 static int
1212 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1213 int *off, const u_char *const start, u_char *const buf, int *buflen,
1214 const enum comptype ctype)
1216 const int num = ng_get_composite_len(type, start, buf, ctype);
1217 int nextIndex = 0; /* next implicit array index */
1218 u_int index; /* field or element index */
1219 int *foff; /* field value offsets in string */
1220 int align, len, blen, error = 0;
1222 /* Initialize */
1223 foff = kmalloc(num * sizeof(*foff), M_NETGRAPH_PARSE,
1224 M_WAITOK | M_NULLOK | M_ZERO);
1225 if (foff == NULL) {
1226 error = ENOMEM;
1227 goto done;
1230 /* Get opening brace/bracket */
1231 if (ng_parse_get_token(s, off, &len)
1232 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1233 error = EINVAL;
1234 goto done;
1236 *off += len;
1238 /* Get individual element value positions in the string */
1239 for (;;) {
1240 enum ng_parse_token tok;
1242 /* Check for closing brace/bracket */
1243 tok = ng_parse_get_token(s, off, &len);
1244 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1245 *off += len;
1246 break;
1249 /* For arrays, the 'name' (ie, index) is optional, so
1250 distinguish name from values by seeing if the next
1251 token is an equals sign */
1252 if (ctype != CT_STRUCT) {
1253 int len2, off2;
1254 char *eptr;
1256 /* If an opening brace/bracket, index is implied */
1257 if (tok == T_LBRACE || tok == T_LBRACKET) {
1258 index = nextIndex++;
1259 goto gotIndex;
1262 /* Might be an index, might be a value, either way... */
1263 if (tok != T_WORD) {
1264 error = EINVAL;
1265 goto done;
1268 /* If no equals sign follows, index is implied */
1269 off2 = *off + len;
1270 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1271 index = nextIndex++;
1272 goto gotIndex;
1275 /* Index was specified explicitly; parse it */
1276 index = (u_int)strtoul(s + *off, &eptr, 0);
1277 if (index < 0 || eptr - (s + *off) != len) {
1278 error = EINVAL;
1279 goto done;
1281 nextIndex = index + 1;
1282 *off += len + len2;
1283 } else { /* a structure field */
1284 const struct ng_parse_struct_field *const
1285 fields = type->info;
1287 /* Find the field by name (required) in field list */
1288 if (tok != T_WORD) {
1289 error = EINVAL;
1290 goto done;
1292 for (index = 0; index < num; index++) {
1293 const struct ng_parse_struct_field *const
1294 field = &fields[index];
1296 if (strncmp(&s[*off], field->name, len) == 0
1297 && field->name[len] == '\0')
1298 break;
1300 if (index == num) {
1301 error = ENOENT;
1302 goto done;
1304 *off += len;
1306 /* Get equals sign */
1307 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1308 error = EINVAL;
1309 goto done;
1311 *off += len;
1313 gotIndex:
1315 /* Check array index */
1316 if (index >= num) {
1317 error = E2BIG;
1318 goto done;
1321 /* Save value's position and skip over it for now */
1322 if (foff[index] != 0) {
1323 error = EALREADY; /* duplicate */
1324 goto done;
1326 while (isspace(s[*off]))
1327 (*off)++;
1328 foff[index] = *off;
1329 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1330 goto done;
1331 *off += len;
1334 /* Now build binary structure from supplied values and defaults */
1335 for (blen = index = 0; index < num; index++) {
1336 const struct ng_parse_type *const
1337 etype = ng_get_composite_etype(type, index, ctype);
1338 int k, pad, vlen;
1340 /* Zero-pad any alignment bytes */
1341 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1342 for (k = 0; k < pad; k++) {
1343 if (blen >= *buflen) {
1344 error = ERANGE;
1345 goto done;
1347 buf[blen++] = 0;
1350 /* Get value */
1351 vlen = *buflen - blen;
1352 if (foff[index] == 0) { /* use default value */
1353 error = ng_get_composite_elem_default(type, index,
1354 start, buf + blen, &vlen, ctype);
1355 } else { /* parse given value */
1356 *off = foff[index];
1357 error = INVOKE(etype, parse)(etype,
1358 s, off, start, buf + blen, &vlen);
1360 if (error != 0)
1361 goto done;
1362 blen += vlen;
1365 /* Make total composite structure size a multiple of its alignment */
1366 if ((align = ALIGNMENT(type)) != 0) {
1367 while (blen % align != 0) {
1368 if (blen >= *buflen) {
1369 error = ERANGE;
1370 goto done;
1372 buf[blen++] = 0;
1376 /* Done */
1377 *buflen = blen;
1378 done:
1379 if (foff != NULL)
1380 kfree(foff, M_NETGRAPH_PARSE);
1381 return (error);
1385 * Convert an array or structure from binary to ASCII
1387 static int
1388 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1389 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1391 const struct ng_mesg *const hdr
1392 = (const struct ng_mesg *)(data - sizeof(*hdr));
1393 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1394 const int workSize = 20 * 1024; /* XXX hard coded constant */
1395 int nextIndex = 0, didOne = 0;
1396 int error, index;
1397 u_char *workBuf;
1399 /* Get workspace for checking default values */
1400 workBuf = kmalloc(workSize, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1401 if (workBuf == NULL)
1402 return (ENOMEM);
1404 /* Opening brace/bracket */
1405 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1406 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1407 goto fail;
1409 /* Do each item */
1410 for (index = 0; index < num; index++) {
1411 const struct ng_parse_type *const
1412 etype = ng_get_composite_etype(type, index, ctype);
1414 /* Skip any alignment pad bytes */
1415 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1418 * See if element is equal to its default value; skip if so.
1419 * Copy struct ng_mesg header for types that peek into it.
1421 if (sizeof(*hdr) + *off < workSize) {
1422 int tempsize = workSize - sizeof(*hdr) - *off;
1424 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1425 if (ng_get_composite_elem_default(type, index, workBuf
1426 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1427 &tempsize, ctype) == 0
1428 && bcmp(workBuf + sizeof(*hdr) + *off,
1429 data + *off, tempsize) == 0) {
1430 *off += tempsize;
1431 continue;
1435 /* Print name= */
1436 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1437 goto fail;
1438 if (ctype != CT_STRUCT) {
1439 if (index != nextIndex) {
1440 nextIndex = index;
1441 if ((error = ng_parse_append(&cbuf,
1442 &cbuflen, "%d=", index)) != 0)
1443 goto fail;
1445 nextIndex++;
1446 } else {
1447 const struct ng_parse_struct_field *const
1448 fields = type->info;
1450 if ((error = ng_parse_append(&cbuf,
1451 &cbuflen, "%s=", fields[index].name)) != 0)
1452 goto fail;
1455 /* Print value */
1456 if ((error = INVOKE(etype, unparse)
1457 (etype, data, off, cbuf, cbuflen)) != 0) {
1458 kfree(workBuf, M_NETGRAPH_PARSE);
1459 return (error);
1461 cbuflen -= strlen(cbuf);
1462 cbuf += strlen(cbuf);
1463 didOne = 1;
1466 /* Closing brace/bracket */
1467 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1468 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1470 fail:
1471 /* Clean up after failure */
1472 kfree(workBuf, M_NETGRAPH_PARSE);
1473 return (error);
1477 * Generate the default value for an element of an array or structure
1478 * Returns EOPNOTSUPP if default value is unspecified.
1480 static int
1481 ng_get_composite_elem_default(const struct ng_parse_type *type,
1482 int index, const u_char *const start, u_char *buf, int *buflen,
1483 const enum comptype ctype)
1485 const struct ng_parse_type *etype;
1486 ng_getDefault_t *func;
1488 switch (ctype) {
1489 case CT_STRUCT:
1490 break;
1491 case CT_ARRAY:
1493 const struct ng_parse_array_info *const ai = type->info;
1495 if (ai->getDefault != NULL) {
1496 return (*ai->getDefault)(type,
1497 index, start, buf, buflen);
1499 break;
1501 case CT_FIXEDARRAY:
1503 const struct ng_parse_fixedarray_info *const fi = type->info;
1505 if (*fi->getDefault != NULL) {
1506 return (*fi->getDefault)(type,
1507 index, start, buf, buflen);
1509 break;
1511 default:
1512 panic("%s", __func__);
1515 /* Default to element type default */
1516 etype = ng_get_composite_etype(type, index, ctype);
1517 func = METHOD(etype, getDefault);
1518 if (func == NULL)
1519 return (EOPNOTSUPP);
1520 return (*func)(etype, start, buf, buflen);
1524 * Get the number of elements in a struct, variable or fixed array.
1526 static int
1527 ng_get_composite_len(const struct ng_parse_type *type,
1528 const u_char *const start, const u_char *buf,
1529 const enum comptype ctype)
1531 switch (ctype) {
1532 case CT_STRUCT:
1534 const struct ng_parse_struct_field *const fields = type->info;
1535 int numFields = 0;
1537 for (numFields = 0; ; numFields++) {
1538 const struct ng_parse_struct_field *const
1539 fi = &fields[numFields];
1541 if (fi->name == NULL)
1542 break;
1544 return (numFields);
1546 case CT_ARRAY:
1548 const struct ng_parse_array_info *const ai = type->info;
1550 return (*ai->getLength)(type, start, buf);
1552 case CT_FIXEDARRAY:
1554 const struct ng_parse_fixedarray_info *const fi = type->info;
1556 return fi->length;
1558 default:
1559 panic("%s", __func__);
1561 return (0);
1565 * Return the type of the index'th element of a composite structure
1567 static const struct ng_parse_type *
1568 ng_get_composite_etype(const struct ng_parse_type *type,
1569 int index, const enum comptype ctype)
1571 const struct ng_parse_type *etype = NULL;
1573 switch (ctype) {
1574 case CT_STRUCT:
1576 const struct ng_parse_struct_field *const fields = type->info;
1578 etype = fields[index].type;
1579 break;
1581 case CT_ARRAY:
1583 const struct ng_parse_array_info *const ai = type->info;
1585 etype = ai->elementType;
1586 break;
1588 case CT_FIXEDARRAY:
1590 const struct ng_parse_fixedarray_info *const fi = type->info;
1592 etype = fi->elementType;
1593 break;
1595 default:
1596 panic("%s", __func__);
1598 return (etype);
1602 * Get the number of bytes to skip to align for the next
1603 * element in a composite structure.
1605 static int
1606 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1607 int index, enum comptype ctype, int posn)
1609 const struct ng_parse_type *const
1610 etype = ng_get_composite_etype(type, index, ctype);
1611 int align;
1613 /* Get element's alignment, and possibly override */
1614 align = ALIGNMENT(etype);
1615 if (ctype == CT_STRUCT) {
1616 const struct ng_parse_struct_field *const fields = type->info;
1618 if (fields[index].alignment != 0)
1619 align = fields[index].alignment;
1622 /* Return number of bytes to skip to align */
1623 return (align ? (align - (posn % align)) % align : 0);
1626 /************************************************************************
1627 PARSING HELPER ROUTINES
1628 ************************************************************************/
1631 * Append to a fixed length string buffer.
1633 static int
1634 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1636 va_list args;
1637 int len;
1639 va_start(args, fmt);
1640 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1641 va_end(args);
1642 if (len >= *cbuflenp)
1643 return ERANGE;
1644 *cbufp += len;
1645 *cbuflenp -= len;
1647 return (0);
1651 * Skip over a value
1653 static int
1654 ng_parse_skip_value(const char *s, int off0, int *lenp)
1656 int len, nbracket, nbrace;
1657 int off = off0;
1659 len = nbracket = nbrace = 0;
1660 do {
1661 switch (ng_parse_get_token(s, &off, &len)) {
1662 case T_LBRACKET:
1663 nbracket++;
1664 break;
1665 case T_LBRACE:
1666 nbrace++;
1667 break;
1668 case T_RBRACKET:
1669 if (nbracket-- == 0)
1670 return (EINVAL);
1671 break;
1672 case T_RBRACE:
1673 if (nbrace-- == 0)
1674 return (EINVAL);
1675 break;
1676 case T_EOF:
1677 return (EINVAL);
1678 default:
1679 break;
1681 off += len;
1682 } while (nbracket > 0 || nbrace > 0);
1683 *lenp = off - off0;
1684 return (0);
1688 * Find the next token in the string, starting at offset *startp.
1689 * Returns the token type, with *startp pointing to the first char
1690 * and *lenp the length.
1692 enum ng_parse_token
1693 ng_parse_get_token(const char *s, int *startp, int *lenp)
1695 char *t;
1696 int i;
1698 while (isspace(s[*startp]))
1699 (*startp)++;
1700 switch (s[*startp]) {
1701 case '\0':
1702 *lenp = 0;
1703 return T_EOF;
1704 case '{':
1705 *lenp = 1;
1706 return T_LBRACE;
1707 case '}':
1708 *lenp = 1;
1709 return T_RBRACE;
1710 case '[':
1711 *lenp = 1;
1712 return T_LBRACKET;
1713 case ']':
1714 *lenp = 1;
1715 return T_RBRACKET;
1716 case '=':
1717 *lenp = 1;
1718 return T_EQUALS;
1719 case '"':
1720 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1721 return T_ERROR;
1722 kfree(t, M_NETGRAPH_PARSE);
1723 return T_STRING;
1724 default:
1725 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1726 && s[i] != '{' && s[i] != '}' && s[i] != '['
1727 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1729 *lenp = i - *startp;
1730 return T_WORD;
1735 * Get a string token, which must be enclosed in double quotes.
1736 * The normal C backslash escapes are recognized.
1738 char *
1739 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1741 char *cbuf, *p;
1742 int start, off;
1743 int slen;
1745 while (isspace(s[*startp]))
1746 (*startp)++;
1747 start = *startp;
1748 if (s[*startp] != '"')
1749 return (NULL);
1750 cbuf = kmalloc(strlen(s + start), M_NETGRAPH_PARSE,
1751 M_WAITOK | M_NULLOK);
1752 if (cbuf == NULL)
1753 return (NULL);
1754 strcpy(cbuf, s + start + 1);
1755 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1756 if (*p == '"') {
1757 *p = '\0';
1758 *lenp = off + 1;
1759 if (slenp != NULL)
1760 *slenp = slen;
1761 return (cbuf);
1762 } else if (p[0] == '\\' && p[1] != '\0') {
1763 int x, k;
1764 char *v;
1766 strcpy(p, p + 1);
1767 v = p;
1768 switch (*p) {
1769 case 't':
1770 *v = '\t';
1771 off++;
1772 continue;
1773 case 'n':
1774 *v = '\n';
1775 off++;
1776 continue;
1777 case 'r':
1778 *v = '\r';
1779 off++;
1780 continue;
1781 case 'v':
1782 *v = '\v';
1783 off++;
1784 continue;
1785 case 'f':
1786 *v = '\f';
1787 off++;
1788 continue;
1789 case '"':
1790 *v = '"';
1791 off++;
1792 continue;
1793 case '0': case '1': case '2': case '3':
1794 case '4': case '5': case '6': case '7':
1795 for (x = k = 0;
1796 k < 3 && *v >= '0' && *v <= '7'; v++) {
1797 x = (x << 3) + (*v - '0');
1798 off++;
1800 *--v = (char)x;
1801 break;
1802 case 'x':
1803 for (v++, x = k = 0;
1804 k < 2 && isxdigit(*v); v++) {
1805 x = (x << 4) + (isdigit(*v) ?
1806 (*v - '0') :
1807 (tolower(*v) - 'a' + 10));
1808 off++;
1810 *--v = (char)x;
1811 break;
1812 default:
1813 continue;
1815 strcpy(p, v);
1818 kfree(cbuf, M_NETGRAPH_PARSE);
1819 return (NULL); /* no closing quote */
1823 * Encode a string so it can be safely put in double quotes.
1824 * Caller must free the result. Exactly "slen" characters
1825 * are encoded.
1827 char *
1828 ng_encode_string(const char *raw, int slen)
1830 char *cbuf;
1831 int off = 0;
1832 int i;
1834 cbuf = kmalloc(strlen(raw) * 4 + 3, M_NETGRAPH_PARSE,
1835 M_WAITOK | M_NULLOK);
1836 if (cbuf == NULL)
1837 return (NULL);
1838 cbuf[off++] = '"';
1839 for (i = 0; i < slen; i++, raw++) {
1840 switch (*raw) {
1841 case '\t':
1842 cbuf[off++] = '\\';
1843 cbuf[off++] = 't';
1844 break;
1845 case '\f':
1846 cbuf[off++] = '\\';
1847 cbuf[off++] = 'f';
1848 break;
1849 case '\n':
1850 cbuf[off++] = '\\';
1851 cbuf[off++] = 'n';
1852 break;
1853 case '\r':
1854 cbuf[off++] = '\\';
1855 cbuf[off++] = 'r';
1856 break;
1857 case '\v':
1858 cbuf[off++] = '\\';
1859 cbuf[off++] = 'v';
1860 break;
1861 case '"':
1862 case '\\':
1863 cbuf[off++] = '\\';
1864 cbuf[off++] = *raw;
1865 break;
1866 default:
1867 if (*raw < 0x20 || *raw > 0x7e) {
1868 off += sprintf(cbuf + off,
1869 "\\x%02x", (u_char)*raw);
1870 break;
1872 cbuf[off++] = *raw;
1873 break;
1876 cbuf[off++] = '"';
1877 cbuf[off] = '\0';
1878 return (cbuf);
1881 /************************************************************************
1882 VIRTUAL METHOD LOOKUP
1883 ************************************************************************/
1885 static ng_parse_t *
1886 ng_get_parse_method(const struct ng_parse_type *t)
1888 while (t != NULL && t->parse == NULL)
1889 t = t->supertype;
1890 return (t ? t->parse : NULL);
1893 static ng_unparse_t *
1894 ng_get_unparse_method(const struct ng_parse_type *t)
1896 while (t != NULL && t->unparse == NULL)
1897 t = t->supertype;
1898 return (t ? t->unparse : NULL);
1901 static ng_getDefault_t *
1902 ng_get_getDefault_method(const struct ng_parse_type *t)
1904 while (t != NULL && t->getDefault == NULL)
1905 t = t->supertype;
1906 return (t ? t->getDefault : NULL);
1909 static ng_getAlign_t *
1910 ng_get_getAlign_method(const struct ng_parse_type *t)
1912 while (t != NULL && t->getAlign == NULL)
1913 t = t->supertype;
1914 return (t ? t->getAlign : NULL);