5 * Copyright (c) 1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@whistle.com>
39 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/ctype.h>
50 #include <netinet/in.h>
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include <netgraph/ng_parse.h>
56 /* Compute alignment for primitive integral types */
72 #define INT8_ALIGNMENT 1
73 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
74 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
75 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
77 /* Type of composite object: struct, array, or fixedarray */
84 /* Composite types helper functions */
85 static int ng_parse_composite(const struct ng_parse_type *type,
86 const char *s, int *off, const u_char *start,
87 u_char *const buf, int *buflen, enum comptype ctype);
88 static int ng_unparse_composite(const struct ng_parse_type *type,
89 const u_char *data, int *off, char *cbuf, int cbuflen,
91 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
92 int index, const u_char *start, u_char *buf,
93 int *buflen, enum comptype ctype);
94 static int ng_get_composite_len(const struct ng_parse_type *type,
95 const u_char *start, const u_char *buf,
97 static const struct ng_parse_type *ng_get_composite_etype(const struct
98 ng_parse_type *type, int index, enum comptype ctype);
99 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
100 int index, enum comptype ctype, int posn);
102 /* Parsing helper functions */
103 static int ng_parse_skip_value(const char *s, int off, int *lenp);
105 /* Poor man's virtual method calls */
106 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
107 #define INVOKE(t,m) (*METHOD(t,m))
109 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
110 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
111 static ng_getDefault_t *ng_get_getDefault_method(const
112 struct ng_parse_type *t);
113 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
115 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
116 0 : INVOKE(t, getAlign)(t))
118 /* For converting binary to string */
119 #define NG_PARSE_APPEND(fmt, args...) \
123 len = snprintf((cbuf), (cbuflen), \
125 if (len >= (cbuflen)) \
131 /************************************************************************
133 ************************************************************************/
136 * Convert an ASCII string to binary according to the supplied type descriptor
139 ng_parse(const struct ng_parse_type *type,
140 const char *string, int *off, u_char *buf, int *buflen)
142 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
146 * Convert binary to an ASCII string according to the supplied type descriptor
149 ng_unparse(const struct ng_parse_type *type,
150 const u_char *data, char *cbuf, int cbuflen)
154 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
158 * Fill in the default value according to the supplied type descriptor
161 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
163 ng_getDefault_t *const func = METHOD(type, getDefault);
167 return (*func)(type, buf, buf, buflen);
171 /************************************************************************
173 ************************************************************************/
176 ng_struct_parse(const struct ng_parse_type *type,
177 const char *s, int *off, const u_char *const start,
178 u_char *const buf, int *buflen)
180 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
184 ng_struct_unparse(const struct ng_parse_type *type,
185 const u_char *data, int *off, char *cbuf, int cbuflen)
187 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
191 ng_struct_getDefault(const struct ng_parse_type *type,
192 const u_char *const start, u_char *buf, int *buflen)
196 return ng_parse_composite(type,
197 "{}", &off, start, buf, buflen, CT_STRUCT);
201 ng_struct_getAlign(const struct ng_parse_type *type)
203 const struct ng_parse_struct_info *si = type->info;
204 const struct ng_parse_struct_field *field;
207 for (field = si->fields; field->name != NULL; field++) {
208 int falign = ALIGNMENT(field->type);
216 const struct ng_parse_type ng_parse_struct_type = {
222 ng_struct_getDefault,
226 /************************************************************************
227 FIXED LENGTH ARRAY TYPE
228 ************************************************************************/
231 ng_fixedarray_parse(const struct ng_parse_type *type,
232 const char *s, int *off, const u_char *const start,
233 u_char *const buf, int *buflen)
235 return ng_parse_composite(type,
236 s, off, start, buf, buflen, CT_FIXEDARRAY);
240 ng_fixedarray_unparse(const struct ng_parse_type *type,
241 const u_char *data, int *off, char *cbuf, int cbuflen)
243 return ng_unparse_composite(type,
244 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
248 ng_fixedarray_getDefault(const struct ng_parse_type *type,
249 const u_char *const start, u_char *buf, int *buflen)
253 return ng_parse_composite(type,
254 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
258 ng_fixedarray_getAlign(const struct ng_parse_type *type)
260 const struct ng_parse_fixedarray_info *fi = type->info;
262 return ALIGNMENT(fi->elementType);
265 const struct ng_parse_type ng_parse_fixedarray_type = {
270 ng_fixedarray_unparse,
271 ng_fixedarray_getDefault,
272 ng_fixedarray_getAlign
275 /************************************************************************
276 VARIABLE LENGTH ARRAY TYPE
277 ************************************************************************/
280 ng_array_parse(const struct ng_parse_type *type,
281 const char *s, int *off, const u_char *const start,
282 u_char *const buf, int *buflen)
284 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
288 ng_array_unparse(const struct ng_parse_type *type,
289 const u_char *data, int *off, char *cbuf, int cbuflen)
291 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
295 ng_array_getDefault(const struct ng_parse_type *type,
296 const u_char *const start, u_char *buf, int *buflen)
300 return ng_parse_composite(type,
301 "[]", &off, start, buf, buflen, CT_ARRAY);
305 ng_array_getAlign(const struct ng_parse_type *type)
307 const struct ng_parse_array_info *ai = type->info;
309 return ALIGNMENT(ai->elementType);
312 const struct ng_parse_type ng_parse_array_type = {
322 /************************************************************************
324 ************************************************************************/
327 ng_int8_parse(const struct ng_parse_type *type,
328 const char *s, int *off, const u_char *const start,
329 u_char *const buf, int *buflen)
335 val = strtol(s + *off, &eptr, 0);
336 if (val < -0x80 || val > 0xff || eptr == s + *off)
340 bcopy(&val8, buf, sizeof(int8_t));
341 *buflen = sizeof(int8_t);
346 ng_int8_unparse(const struct ng_parse_type *type,
347 const u_char *data, int *off, char *cbuf, int cbuflen)
351 bcopy(data + *off, &val, sizeof(int8_t));
352 NG_PARSE_APPEND("%d", (int)val);
353 *off += sizeof(int8_t);
358 ng_int8_getDefault(const struct ng_parse_type *type,
359 const u_char *const start, u_char *buf, int *buflen)
363 if (*buflen < sizeof(int8_t))
366 bcopy(&val, buf, sizeof(int8_t));
367 *buflen = sizeof(int8_t);
372 ng_int8_getAlign(const struct ng_parse_type *type)
374 return INT8_ALIGNMENT;
377 const struct ng_parse_type ng_parse_int8_type = {
387 /************************************************************************
389 ************************************************************************/
392 ng_int16_parse(const struct ng_parse_type *type,
393 const char *s, int *off, const u_char *const start,
394 u_char *const buf, int *buflen)
400 val = strtol(s + *off, &eptr, 0);
401 if (val < -0x8000 || val > 0xffff || eptr == s + *off)
404 val16 = (int16_t)val;
405 bcopy(&val16, buf, sizeof(int16_t));
406 *buflen = sizeof(int16_t);
411 ng_int16_unparse(const struct ng_parse_type *type,
412 const u_char *data, int *off, char *cbuf, int cbuflen)
416 bcopy(data + *off, &val, sizeof(int16_t));
417 NG_PARSE_APPEND("%d", (int)val);
418 *off += sizeof(int16_t);
423 ng_int16_getDefault(const struct ng_parse_type *type,
424 const u_char *const start, u_char *buf, int *buflen)
428 if (*buflen < sizeof(int16_t))
431 bcopy(&val, buf, sizeof(int16_t));
432 *buflen = sizeof(int16_t);
437 ng_int16_getAlign(const struct ng_parse_type *type)
439 return INT16_ALIGNMENT;
442 const struct ng_parse_type ng_parse_int16_type = {
452 /************************************************************************
454 ************************************************************************/
457 ng_int32_parse(const struct ng_parse_type *type,
458 const char *s, int *off, const u_char *const start,
459 u_char *const buf, int *buflen)
461 long val; /* assumes long is at least 32 bits */
465 val = strtol(s + *off, &eptr, 0);
466 if (val < (long)-0x80000000
467 || val > (u_long)0xffffffff || eptr == s + *off)
470 val32 = (int32_t)val;
471 bcopy(&val32, buf, sizeof(int32_t));
472 *buflen = sizeof(int32_t);
477 ng_int32_unparse(const struct ng_parse_type *type,
478 const u_char *data, int *off, char *cbuf, int cbuflen)
482 bcopy(data + *off, &val, sizeof(int32_t));
483 NG_PARSE_APPEND("%ld", (long)val);
484 *off += sizeof(int32_t);
489 ng_int32_getDefault(const struct ng_parse_type *type,
490 const u_char *const start, u_char *buf, int *buflen)
494 if (*buflen < sizeof(int32_t))
497 bcopy(&val, buf, sizeof(int32_t));
498 *buflen = sizeof(int32_t);
503 ng_int32_getAlign(const struct ng_parse_type *type)
505 return INT32_ALIGNMENT;
508 const struct ng_parse_type ng_parse_int32_type = {
518 /************************************************************************
520 ************************************************************************/
523 ng_int64_parse(const struct ng_parse_type *type,
524 const char *s, int *off, const u_char *const start,
525 u_char *const buf, int *buflen)
531 val = strtoq(s + *off, &eptr, 0);
532 if (eptr == s + *off)
535 val64 = (int64_t)val;
536 bcopy(&val64, buf, sizeof(int64_t));
537 *buflen = sizeof(int64_t);
542 ng_int64_unparse(const struct ng_parse_type *type,
543 const u_char *data, int *off, char *cbuf, int cbuflen)
547 bcopy(data + *off, &val, sizeof(int64_t));
548 NG_PARSE_APPEND("%lld", (long long)val);
549 *off += sizeof(int64_t);
554 ng_int64_getDefault(const struct ng_parse_type *type,
555 const u_char *const start, u_char *buf, int *buflen)
559 if (*buflen < sizeof(int64_t))
562 bcopy(&val, buf, sizeof(int64_t));
563 *buflen = sizeof(int64_t);
568 ng_int64_getAlign(const struct ng_parse_type *type)
570 return INT64_ALIGNMENT;
573 const struct ng_parse_type ng_parse_int64_type = {
583 /************************************************************************
585 ************************************************************************/
588 ng_string_parse(const struct ng_parse_type *type,
589 const char *s, int *off, const u_char *const start,
590 u_char *const buf, int *buflen)
595 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
598 len = strlen(sval) + 1;
599 bcopy(sval, buf, len);
600 FREE(sval, M_NETGRAPH);
606 ng_string_unparse(const struct ng_parse_type *type,
607 const u_char *data, int *off, char *cbuf, int cbuflen)
609 const char *const raw = (const char *)data + *off;
610 char *const s = ng_encode_string(raw);
614 NG_PARSE_APPEND("%s", s);
615 *off += strlen(raw) + 1;
621 ng_string_getDefault(const struct ng_parse_type *type,
622 const u_char *const start, u_char *buf, int *buflen)
627 buf[0] = (u_char)'\0';
632 const struct ng_parse_type ng_parse_string_type = {
638 ng_string_getDefault,
642 /************************************************************************
643 FIXED BUFFER STRING TYPE
644 ************************************************************************/
647 ng_fixedstring_parse(const struct ng_parse_type *type,
648 const char *s, int *off, const u_char *const start,
649 u_char *const buf, int *buflen)
651 const struct ng_parse_fixedstring_info *const fi = type->info;
655 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
657 if (strlen(sval) + 1 > fi->bufSize)
660 len = strlen(sval) + 1;
661 bcopy(sval, buf, len);
662 FREE(sval, M_NETGRAPH);
663 bzero(buf + len, fi->bufSize - len);
664 *buflen = fi->bufSize;
669 ng_fixedstring_unparse(const struct ng_parse_type *type,
670 const u_char *data, int *off, char *cbuf, int cbuflen)
672 const struct ng_parse_fixedstring_info *const fi = type->info;
673 int error, temp = *off;
675 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
682 ng_fixedstring_getDefault(const struct ng_parse_type *type,
683 const u_char *const start, u_char *buf, int *buflen)
685 const struct ng_parse_fixedstring_info *const fi = type->info;
687 if (*buflen < fi->bufSize)
689 bzero(buf, fi->bufSize);
690 *buflen = fi->bufSize;
694 const struct ng_parse_type ng_parse_fixedstring_type = {
698 ng_fixedstring_parse,
699 ng_fixedstring_unparse,
700 ng_fixedstring_getDefault,
704 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
707 const struct ng_parse_type ng_parse_nodebuf_type = {
708 &ng_parse_fixedstring_type,
709 &ng_parse_nodebuf_info
712 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
715 const struct ng_parse_type ng_parse_hookbuf_type = {
716 &ng_parse_fixedstring_type,
717 &ng_parse_hookbuf_info
720 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
723 const struct ng_parse_type ng_parse_pathbuf_type = {
724 &ng_parse_fixedstring_type,
725 &ng_parse_pathbuf_info
728 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
731 const struct ng_parse_type ng_parse_typebuf_type = {
732 &ng_parse_fixedstring_type,
733 &ng_parse_typebuf_info
736 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
739 const struct ng_parse_type ng_parse_cmdbuf_type = {
740 &ng_parse_fixedstring_type,
741 &ng_parse_cmdbuf_info
744 /************************************************************************
746 ************************************************************************/
749 ng_ipaddr_parse(const struct ng_parse_type *type,
750 const char *s, int *off, const u_char *const start,
751 u_char *const buf, int *buflen)
755 for (i = 0; i < 4; i++) {
756 if ((error = ng_int8_parse(&ng_parse_int8_type,
757 s, off, start, buf + i, buflen)) != 0)
759 if (i < 3 && s[*off] != '.')
768 ng_ipaddr_unparse(const struct ng_parse_type *type,
769 const u_char *data, int *off, char *cbuf, int cbuflen)
773 bcopy(data + *off, &ip, sizeof(ip));
774 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
775 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
781 ng_ipaddr_getDefault(const struct ng_parse_type *type,
782 const u_char *const start, u_char *buf, int *buflen)
784 struct in_addr ip = { 0 };
786 if (*buflen < sizeof(ip))
788 bcopy(&ip, buf, sizeof(ip));
789 *buflen = sizeof(ip);
793 const struct ng_parse_type ng_parse_ipaddr_type = {
799 ng_ipaddr_getDefault,
803 /************************************************************************
805 ************************************************************************/
807 /* Get the length of a byte array */
809 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
810 const u_char *start, const u_char *buf)
812 ng_parse_array_getLength_t *const getLength = type->private;
814 return (*getLength)(type, start, buf);
818 ng_bytearray_elem_unparse(const struct ng_parse_type *type,
819 const u_char *data, int *off, char *cbuf, int cbuflen)
823 bcopy(data + *off, &val, sizeof(int8_t));
824 NG_PARSE_APPEND("0x%02x", (int)val & 0xff); /* always hex format */
825 *off += sizeof(int8_t);
829 /* Byte array element type is int8, but always output in hex format */
830 const struct ng_parse_type ng_parse_bytearray_elem_type = {
835 ng_bytearray_elem_unparse,
840 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
841 &ng_parse_bytearray_elem_type,
842 &ng_parse_bytearray_subtype_getLength,
845 static const struct ng_parse_type ng_parse_bytearray_subtype = {
846 &ng_parse_array_type,
847 &ng_parse_bytearray_subtype_info
851 ng_bytearray_parse(const struct ng_parse_type *type,
852 const char *s, int *off, const u_char *const start,
853 u_char *const buf, int *buflen)
858 /* We accept either an array of bytes or a string constant */
859 if ((str = ng_get_string_token(s, off, &toklen)) != NULL) {
860 ng_parse_array_getLength_t *const getLength = type->info;
863 arraylen = (*getLength)(type, start, buf);
864 if (arraylen > *buflen) {
865 FREE(str, M_NETGRAPH);
868 slen = strlen(str) + 1;
869 if (slen > arraylen) {
870 FREE(str, M_NETGRAPH);
873 bcopy(str, buf, slen);
874 bzero(buf + slen, arraylen - slen);
875 FREE(str, M_NETGRAPH);
880 struct ng_parse_type subtype;
882 subtype = ng_parse_bytearray_subtype;
883 (const void *)subtype.private = type->info;
884 return ng_array_parse(&subtype, s, off, start, buf, buflen);
889 ng_bytearray_unparse(const struct ng_parse_type *type,
890 const u_char *data, int *off, char *cbuf, int cbuflen)
892 struct ng_parse_type subtype;
894 subtype = ng_parse_bytearray_subtype;
895 (const void *)subtype.private = type->info;
896 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
900 ng_bytearray_getDefault(const struct ng_parse_type *type,
901 const u_char *const start, u_char *buf, int *buflen)
903 struct ng_parse_type subtype;
905 subtype = ng_parse_bytearray_subtype;
906 (const void *)subtype.private = type->info;
907 return ng_array_getDefault(&subtype, start, buf, buflen);
910 const struct ng_parse_type ng_parse_bytearray_type = {
915 ng_bytearray_unparse,
916 ng_bytearray_getDefault,
920 /************************************************************************
922 ************************************************************************/
924 /* Get msg->header.arglen when "buf" is pointing to msg->data */
926 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
927 const u_char *start, const u_char *buf)
929 const struct ng_mesg *msg;
931 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
932 return msg->header.arglen;
935 /* Type for the variable length data portion of a struct ng_mesg */
936 static const struct ng_parse_type ng_msg_data_type = {
937 &ng_parse_bytearray_type,
938 &ng_parse_ng_mesg_getLength
941 /* Type for the entire struct ng_mesg header with data section */
942 static const struct ng_parse_struct_info
943 ng_parse_ng_mesg_type_info = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
944 const struct ng_parse_type ng_parse_ng_mesg_type = {
945 &ng_parse_struct_type,
946 &ng_parse_ng_mesg_type_info,
949 /************************************************************************
950 COMPOSITE HELPER ROUTINES
951 ************************************************************************/
954 * Convert a structure or array from ASCII to binary
957 ng_parse_composite(const struct ng_parse_type *type, const char *s,
958 int *off, const u_char *const start, u_char *const buf, int *buflen,
959 const enum comptype ctype)
961 const int num = ng_get_composite_len(type, start, buf, ctype);
962 int nextIndex = 0; /* next implicit array index */
963 u_int index; /* field or element index */
964 int *foff; /* field value offsets in string */
965 int align, len, blen, error = 0;
968 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT);
973 bzero(foff, num * sizeof(*foff));
975 /* Get opening brace/bracket */
976 if (ng_parse_get_token(s, off, &len)
977 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
983 /* Get individual element value positions in the string */
985 enum ng_parse_token tok;
987 /* Check for closing brace/bracket */
988 tok = ng_parse_get_token(s, off, &len);
989 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
994 /* For arrays, the 'name' (ie, index) is optional, so
995 distinguish name from values by seeing if the next
996 token is an equals sign */
997 if (ctype != CT_STRUCT) {
1001 /* If an opening brace/bracket, index is implied */
1002 if (tok == T_LBRACE || tok == T_LBRACKET) {
1003 index = nextIndex++;
1007 /* Might be an index, might be a value, either way... */
1008 if (tok != T_WORD) {
1013 /* If no equals sign follows, index is implied */
1015 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1016 index = nextIndex++;
1020 /* Index was specified explicitly; parse it */
1021 index = (u_int)strtoul(s + *off, &eptr, 0);
1022 if (index < 0 || eptr - (s + *off) != len) {
1026 nextIndex = index + 1;
1029 } else { /* a structure field */
1030 const struct ng_parse_struct_field *field = NULL;
1031 const struct ng_parse_struct_info *si = type->info;
1033 /* Find the field by name (required) in field list */
1034 if (tok != T_WORD) {
1038 for (index = 0; index < num; index++) {
1039 field = &si->fields[index];
1040 if (strncmp(&s[*off], field->name, len) == 0
1041 && field->name[len] == '\0')
1050 /* Get equals sign */
1051 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1058 /* Check array index */
1064 /* Save value's position and skip over it for now */
1065 if (foff[index] != 0) {
1066 error = EALREADY; /* duplicate */
1069 while (isspace(s[*off]))
1072 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1077 /* Now build binary structure from supplied values and defaults */
1078 for (blen = index = 0; index < num; index++) {
1079 const struct ng_parse_type *const
1080 etype = ng_get_composite_etype(type, index, ctype);
1083 /* Zero-pad any alignment bytes */
1084 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1085 for (k = 0; k < pad; k++) {
1086 if (blen >= *buflen) {
1094 vlen = *buflen - blen;
1095 if (foff[index] == 0) { /* use default value */
1096 error = ng_get_composite_elem_default(type, index,
1097 start, buf + blen, &vlen, ctype);
1098 } else { /* parse given value */
1100 error = INVOKE(etype, parse)(etype,
1101 s, off, start, buf + blen, &vlen);
1108 /* Make total composite structure size a multiple of its alignment */
1109 if ((align = ALIGNMENT(type)) != 0) {
1110 while (blen % align != 0) {
1111 if (blen >= *buflen) {
1122 FREE(foff, M_NETGRAPH);
1127 * Convert an array or structure from binary to ASCII
1130 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1131 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1133 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1134 int nextIndex = 0, didOne = 0;
1137 /* Opening brace/bracket */
1138 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1141 for (index = 0; index < num; index++) {
1142 const struct ng_parse_type *const
1143 etype = ng_get_composite_etype(type, index, ctype);
1146 /* Skip any alignment pad bytes */
1147 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1149 /* See if element is equal to its default value; skip if so */
1150 if (*off < sizeof(temp)) {
1151 int tempsize = sizeof(temp) - *off;
1153 bcopy(data, temp, *off);
1154 if (ng_get_composite_elem_default(type, index, temp,
1155 temp + *off, &tempsize, ctype) == 0
1156 && bcmp(temp + *off, data + *off, tempsize) == 0) {
1163 NG_PARSE_APPEND(" ");
1164 if (ctype != CT_STRUCT) {
1165 if (index != nextIndex) {
1167 NG_PARSE_APPEND("%d=", index);
1171 const struct ng_parse_struct_info *si = type->info;
1173 NG_PARSE_APPEND("%s=", si->fields[index].name);
1177 if ((error = INVOKE(etype, unparse)
1178 (etype, data, off, cbuf, cbuflen)) != 0)
1180 cbuflen -= strlen(cbuf);
1181 cbuf += strlen(cbuf);
1185 /* Closing brace/bracket */
1186 NG_PARSE_APPEND("%s%c",
1187 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1192 * Generate the default value for an element of an array or structure
1193 * Returns EOPNOTSUPP if default value is unspecified.
1196 ng_get_composite_elem_default(const struct ng_parse_type *type,
1197 int index, const u_char *const start, u_char *buf, int *buflen,
1198 const enum comptype ctype)
1200 const struct ng_parse_type *etype;
1201 ng_getDefault_t *func;
1208 const struct ng_parse_array_info *const ai = type->info;
1210 if (ai->getDefault != NULL) {
1211 return (*ai->getDefault)(type,
1212 index, start, buf, buflen);
1218 const struct ng_parse_fixedarray_info *const fi = type->info;
1220 if (*fi->getDefault != NULL) {
1221 return (*fi->getDefault)(type,
1222 index, start, buf, buflen);
1227 panic("%s", __FUNCTION__);
1230 /* Default to element type default */
1231 etype = ng_get_composite_etype(type, index, ctype);
1232 func = METHOD(etype, getDefault);
1234 return (EOPNOTSUPP);
1235 return (*func)(etype, start, buf, buflen);
1239 * Get the number of elements in a struct, variable or fixed array.
1242 ng_get_composite_len(const struct ng_parse_type *type,
1243 const u_char *const start, const u_char *buf,
1244 const enum comptype ctype)
1249 const struct ng_parse_struct_info *const si = type->info;
1252 for (numFields = 0; ; numFields++) {
1253 const struct ng_parse_struct_field *const
1254 fi = &si->fields[numFields];
1256 if (fi->name == NULL)
1263 const struct ng_parse_array_info *const ai = type->info;
1265 return (*ai->getLength)(type, start, buf);
1269 const struct ng_parse_fixedarray_info *const fi = type->info;
1274 panic("%s", __FUNCTION__);
1280 * Return the type of the index'th element of a composite structure
1282 static const struct ng_parse_type *
1283 ng_get_composite_etype(const struct ng_parse_type *type,
1284 int index, const enum comptype ctype)
1286 const struct ng_parse_type *etype = NULL;
1291 const struct ng_parse_struct_info *const si = type->info;
1293 etype = si->fields[index].type;
1298 const struct ng_parse_array_info *const ai = type->info;
1300 etype = ai->elementType;
1305 const struct ng_parse_fixedarray_info *const fi = type->info;
1307 etype = fi->elementType;
1311 panic("%s", __FUNCTION__);
1317 * Get the number of bytes to skip to align for the next
1318 * element in a composite structure.
1321 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1322 int index, enum comptype ctype, int posn)
1324 const struct ng_parse_type *const
1325 etype = ng_get_composite_etype(type, index, ctype);
1328 /* Get element's alignment, and possibly override */
1329 align = ALIGNMENT(etype);
1330 if (ctype == CT_STRUCT) {
1331 const struct ng_parse_struct_info *si = type->info;
1333 if (si->fields[index].alignment != 0)
1334 align = si->fields[index].alignment;
1337 /* Return number of bytes to skip to align */
1338 return (align ? (align - (posn % align)) % align : 0);
1341 /************************************************************************
1342 PARSING HELPER ROUTINES
1343 ************************************************************************/
1349 ng_parse_skip_value(const char *s, int off0, int *lenp)
1351 int len, nbracket, nbrace;
1354 len = nbracket = nbrace = 0;
1356 switch (ng_parse_get_token(s, &off, &len)) {
1364 if (nbracket-- == 0)
1377 } while (nbracket > 0 || nbrace > 0);
1383 * Find the next token in the string, starting at offset *startp.
1384 * Returns the token type, with *startp pointing to the first char
1385 * and *lenp the length.
1388 ng_parse_get_token(const char *s, int *startp, int *lenp)
1393 while (isspace(s[*startp]))
1395 switch (s[*startp]) {
1415 if ((t = ng_get_string_token(s, startp, lenp)) == NULL)
1417 FREE(t, M_NETGRAPH);
1420 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1421 && s[i] != '{' && s[i] != '}' && s[i] != '['
1422 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1424 *lenp = i - *startp;
1430 * Get a string token, which must be enclosed in double quotes.
1431 * The normal C backslash escapes are recognized.
1434 ng_get_string_token(const char *s, int *startp, int *lenp)
1439 while (isspace(s[*startp]))
1442 if (s[*startp] != '"')
1444 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT);
1447 strcpy(cbuf, s + start + 1);
1448 for (off = 1, p = cbuf; *p != '\0'; off++, p++) {
1453 } else if (p[0] == '\\' && p[1] != '\0') {
1484 case '0': case '1': case '2': case '3':
1485 case '4': case '5': case '6': case '7':
1487 k < 3 && *v >= '0' && *v <= '7'; v++) {
1488 x = (x << 3) + (*v - '0');
1494 for (v++, x = k = 0;
1495 k < 2 && isxdigit(*v); v++) {
1496 x = (x << 4) + (isdigit(*v) ?
1498 (tolower(*v) - 'a' + 10));
1509 return (NULL); /* no closing quote */
1513 * Encode a string so it can be safely put in double quotes.
1514 * Caller must free the result.
1517 ng_encode_string(const char *raw)
1522 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1526 for ( ; *raw != '\0'; raw++) {
1554 if (*raw < 0x20 || *raw > 0x7e) {
1555 off += sprintf(cbuf + off,
1556 "\\x%02x", (u_char)*raw);
1568 /************************************************************************
1569 VIRTUAL METHOD LOOKUP
1570 ************************************************************************/
1573 ng_get_parse_method(const struct ng_parse_type *t)
1575 while (t != NULL && t->parse == NULL)
1577 return (t ? t->parse : NULL);
1580 static ng_unparse_t *
1581 ng_get_unparse_method(const struct ng_parse_type *t)
1583 while (t != NULL && t->unparse == NULL)
1585 return (t ? t->unparse : NULL);
1588 static ng_getDefault_t *
1589 ng_get_getDefault_method(const struct ng_parse_type *t)
1591 while (t != NULL && t->getDefault == NULL)
1593 return (t ? t->getDefault : NULL);
1596 static ng_getAlign_t *
1597 ng_get_getAlign_method(const struct ng_parse_type *t)
1599 while (t != NULL && t->getAlign == NULL)
1601 return (t ? t->getAlign : NULL);