6 * Copyright (c) 1999 Whistle Communications, Inc.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
52 #include <sys/ctype.h>
54 #include <machine/stdarg.h>
56 #include <net/ethernet.h>
58 #include <netinet/in.h>
60 #include <netgraph/ng_message.h>
61 #include <netgraph/netgraph.h>
62 #include <netgraph/ng_parse.h>
64 #ifdef NG_SEPARATE_MALLOC
65 static MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
67 #define M_NETGRAPH_PARSE M_NETGRAPH
70 /* Compute alignment for primitive integral types */
86 #define INT8_ALIGNMENT 1
87 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
88 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
89 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
91 /* Output format for integral types */
92 #define INT_UNSIGNED 0
96 /* Type of composite object: struct, array, or fixedarray */
103 /* Composite types helper functions */
104 static int ng_parse_composite(const struct ng_parse_type *type,
105 const char *s, int *off, const u_char *start,
106 u_char *const buf, int *buflen, enum comptype ctype);
107 static int ng_unparse_composite(const struct ng_parse_type *type,
108 const u_char *data, int *off, char *cbuf, int cbuflen,
109 enum comptype ctype);
110 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
111 int index, const u_char *start, u_char *buf,
112 int *buflen, enum comptype ctype);
113 static int ng_get_composite_len(const struct ng_parse_type *type,
114 const u_char *start, const u_char *buf,
115 enum comptype ctype);
116 static const struct ng_parse_type *ng_get_composite_etype(const struct
117 ng_parse_type *type, int index, enum comptype ctype);
118 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
119 int index, enum comptype ctype, int posn);
121 /* Parsing helper functions */
122 static int ng_parse_skip_value(const char *s, int off, int *lenp);
123 static int ng_parse_append(char **cbufp, int *cbuflenp,
124 const char *fmt, ...);
126 /* Poor man's virtual method calls */
127 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
128 #define INVOKE(t,m) (*METHOD(t,m))
130 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
131 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
132 static ng_getDefault_t *ng_get_getDefault_method(const
133 struct ng_parse_type *t);
134 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
136 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
137 0 : INVOKE(t, getAlign)(t))
139 /************************************************************************
141 ************************************************************************/
144 * Convert an ASCII string to binary according to the supplied type descriptor
147 ng_parse(const struct ng_parse_type *type,
148 const char *string, int *off, u_char *buf, int *buflen)
150 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
154 * Convert binary to an ASCII string according to the supplied type descriptor
157 ng_unparse(const struct ng_parse_type *type,
158 const u_char *data, char *cbuf, int cbuflen)
162 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
166 * Fill in the default value according to the supplied type descriptor
169 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
171 ng_getDefault_t *const func = METHOD(type, getDefault);
175 return (*func)(type, buf, buf, buflen);
178 /************************************************************************
180 ************************************************************************/
183 ng_struct_parse(const struct ng_parse_type *type,
184 const char *s, int *off, const u_char *const start,
185 u_char *const buf, int *buflen)
187 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
191 ng_struct_unparse(const struct ng_parse_type *type,
192 const u_char *data, int *off, char *cbuf, int cbuflen)
194 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
198 ng_struct_getDefault(const struct ng_parse_type *type,
199 const u_char *const start, u_char *buf, int *buflen)
203 return ng_parse_composite(type,
204 "{}", &off, start, buf, buflen, CT_STRUCT);
208 ng_struct_getAlign(const struct ng_parse_type *type)
210 const struct ng_parse_struct_field *field;
213 for (field = type->info; field->name != NULL; field++) {
214 int falign = ALIGNMENT(field->type);
222 const struct ng_parse_type ng_parse_struct_type = {
228 ng_struct_getDefault,
232 /************************************************************************
233 FIXED LENGTH ARRAY TYPE
234 ************************************************************************/
237 ng_fixedarray_parse(const struct ng_parse_type *type,
238 const char *s, int *off, const u_char *const start,
239 u_char *const buf, int *buflen)
241 return ng_parse_composite(type,
242 s, off, start, buf, buflen, CT_FIXEDARRAY);
246 ng_fixedarray_unparse(const struct ng_parse_type *type,
247 const u_char *data, int *off, char *cbuf, int cbuflen)
249 return ng_unparse_composite(type,
250 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
254 ng_fixedarray_getDefault(const struct ng_parse_type *type,
255 const u_char *const start, u_char *buf, int *buflen)
259 return ng_parse_composite(type,
260 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
264 ng_fixedarray_getAlign(const struct ng_parse_type *type)
266 const struct ng_parse_fixedarray_info *fi = type->info;
268 return ALIGNMENT(fi->elementType);
271 const struct ng_parse_type ng_parse_fixedarray_type = {
276 ng_fixedarray_unparse,
277 ng_fixedarray_getDefault,
278 ng_fixedarray_getAlign
281 /************************************************************************
282 VARIABLE LENGTH ARRAY TYPE
283 ************************************************************************/
286 ng_array_parse(const struct ng_parse_type *type,
287 const char *s, int *off, const u_char *const start,
288 u_char *const buf, int *buflen)
290 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
294 ng_array_unparse(const struct ng_parse_type *type,
295 const u_char *data, int *off, char *cbuf, int cbuflen)
297 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
301 ng_array_getDefault(const struct ng_parse_type *type,
302 const u_char *const start, u_char *buf, int *buflen)
306 return ng_parse_composite(type,
307 "[]", &off, start, buf, buflen, CT_ARRAY);
311 ng_array_getAlign(const struct ng_parse_type *type)
313 const struct ng_parse_array_info *ai = type->info;
315 return ALIGNMENT(ai->elementType);
318 const struct ng_parse_type ng_parse_array_type = {
328 /************************************************************************
330 ************************************************************************/
333 ng_int8_parse(const struct ng_parse_type *type,
334 const char *s, int *off, const u_char *const start,
335 u_char *const buf, int *buflen)
341 val = strtol(s + *off, &eptr, 0);
342 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
346 bcopy(&val8, buf, sizeof(int8_t));
347 *buflen = sizeof(int8_t);
352 ng_int8_unparse(const struct ng_parse_type *type,
353 const u_char *data, int *off, char *cbuf, int cbuflen)
360 bcopy(data + *off, &val, sizeof(int8_t));
361 switch ((intptr_t)type->info) {
368 fval = (u_int8_t)val;
372 fval = (u_int8_t)val;
375 panic("%s: unknown type", __func__);
377 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
379 *off += sizeof(int8_t);
384 ng_int8_getDefault(const struct ng_parse_type *type,
385 const u_char *const start, u_char *buf, int *buflen)
389 if (*buflen < sizeof(int8_t))
392 bcopy(&val, buf, sizeof(int8_t));
393 *buflen = sizeof(int8_t);
398 ng_int8_getAlign(const struct ng_parse_type *type)
400 return INT8_ALIGNMENT;
403 const struct ng_parse_type ng_parse_int8_type = {
413 const struct ng_parse_type ng_parse_uint8_type = {
418 const struct ng_parse_type ng_parse_hint8_type = {
423 /************************************************************************
425 ************************************************************************/
428 ng_int16_parse(const struct ng_parse_type *type,
429 const char *s, int *off, const u_char *const start,
430 u_char *const buf, int *buflen)
436 val = strtol(s + *off, &eptr, 0);
437 if (val < (int16_t)0x8000
438 || val > (u_int16_t)0xffff || eptr == s + *off)
441 val16 = (int16_t)val;
442 bcopy(&val16, buf, sizeof(int16_t));
443 *buflen = sizeof(int16_t);
448 ng_int16_unparse(const struct ng_parse_type *type,
449 const u_char *data, int *off, char *cbuf, int cbuflen)
456 bcopy(data + *off, &val, sizeof(int16_t));
457 switch ((intptr_t)type->info) {
464 fval = (u_int16_t)val;
468 fval = (u_int16_t)val;
471 panic("%s: unknown type", __func__);
473 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
475 *off += sizeof(int16_t);
480 ng_int16_getDefault(const struct ng_parse_type *type,
481 const u_char *const start, u_char *buf, int *buflen)
485 if (*buflen < sizeof(int16_t))
488 bcopy(&val, buf, sizeof(int16_t));
489 *buflen = sizeof(int16_t);
494 ng_int16_getAlign(const struct ng_parse_type *type)
496 return INT16_ALIGNMENT;
499 const struct ng_parse_type ng_parse_int16_type = {
509 const struct ng_parse_type ng_parse_uint16_type = {
510 &ng_parse_int16_type,
514 const struct ng_parse_type ng_parse_hint16_type = {
515 &ng_parse_int16_type,
519 /************************************************************************
521 ************************************************************************/
524 ng_int32_parse(const struct ng_parse_type *type,
525 const char *s, int *off, const u_char *const start,
526 u_char *const buf, int *buflen)
528 long val; /* assumes long is at least 32 bits */
532 if ((intptr_t)type->info == INT_SIGNED)
533 val = strtol(s + *off, &eptr, 0);
535 val = strtoul(s + *off, &eptr, 0);
536 if (val < (int32_t)0x80000000
537 || val > (u_int32_t)0xffffffff || eptr == s + *off)
540 val32 = (int32_t)val;
541 bcopy(&val32, buf, sizeof(int32_t));
542 *buflen = sizeof(int32_t);
547 ng_int32_unparse(const struct ng_parse_type *type,
548 const u_char *data, int *off, char *cbuf, int cbuflen)
555 bcopy(data + *off, &val, sizeof(int32_t));
556 switch ((intptr_t)type->info) {
563 fval = (u_int32_t)val;
567 fval = (u_int32_t)val;
570 panic("%s: unknown type", __func__);
572 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
574 *off += sizeof(int32_t);
579 ng_int32_getDefault(const struct ng_parse_type *type,
580 const u_char *const start, u_char *buf, int *buflen)
584 if (*buflen < sizeof(int32_t))
587 bcopy(&val, buf, sizeof(int32_t));
588 *buflen = sizeof(int32_t);
593 ng_int32_getAlign(const struct ng_parse_type *type)
595 return INT32_ALIGNMENT;
598 const struct ng_parse_type ng_parse_int32_type = {
608 const struct ng_parse_type ng_parse_uint32_type = {
609 &ng_parse_int32_type,
613 const struct ng_parse_type ng_parse_hint32_type = {
614 &ng_parse_int32_type,
618 /************************************************************************
620 ************************************************************************/
623 ng_int64_parse(const struct ng_parse_type *type,
624 const char *s, int *off, const u_char *const start,
625 u_char *const buf, int *buflen)
631 val = strtoq(s + *off, &eptr, 0);
632 if (eptr == s + *off)
635 val64 = (int64_t)val;
636 bcopy(&val64, buf, sizeof(int64_t));
637 *buflen = sizeof(int64_t);
642 ng_int64_unparse(const struct ng_parse_type *type,
643 const u_char *data, int *off, char *cbuf, int cbuflen)
650 bcopy(data + *off, &val, sizeof(int64_t));
651 switch ((intptr_t)type->info) {
658 fval = (u_int64_t)val;
662 fval = (u_int64_t)val;
665 panic("%s: unknown type", __func__);
667 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
669 *off += sizeof(int64_t);
674 ng_int64_getDefault(const struct ng_parse_type *type,
675 const u_char *const start, u_char *buf, int *buflen)
679 if (*buflen < sizeof(int64_t))
682 bcopy(&val, buf, sizeof(int64_t));
683 *buflen = sizeof(int64_t);
688 ng_int64_getAlign(const struct ng_parse_type *type)
690 return INT64_ALIGNMENT;
693 const struct ng_parse_type ng_parse_int64_type = {
703 const struct ng_parse_type ng_parse_uint64_type = {
704 &ng_parse_int64_type,
708 const struct ng_parse_type ng_parse_hint64_type = {
709 &ng_parse_int64_type,
713 /************************************************************************
715 ************************************************************************/
718 ng_string_parse(const struct ng_parse_type *type,
719 const char *s, int *off, const u_char *const start,
720 u_char *const buf, int *buflen)
726 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
729 bcopy(sval, buf, slen + 1);
730 free(sval, M_NETGRAPH_PARSE);
736 ng_string_unparse(const struct ng_parse_type *type,
737 const u_char *data, int *off, char *cbuf, int cbuflen)
739 const char *const raw = (const char *)data + *off;
740 char *const s = ng_encode_string(raw, strlen(raw));
745 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
746 free(s, M_NETGRAPH_PARSE);
749 *off += strlen(raw) + 1;
750 free(s, M_NETGRAPH_PARSE);
755 ng_string_getDefault(const struct ng_parse_type *type,
756 const u_char *const start, u_char *buf, int *buflen)
761 buf[0] = (u_char)'\0';
766 const struct ng_parse_type ng_parse_string_type = {
772 ng_string_getDefault,
776 /************************************************************************
777 FIXED BUFFER STRING TYPE
778 ************************************************************************/
781 ng_fixedstring_parse(const struct ng_parse_type *type,
782 const char *s, int *off, const u_char *const start,
783 u_char *const buf, int *buflen)
785 const struct ng_parse_fixedstring_info *const fi = type->info;
790 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
792 if (slen + 1 > fi->bufSize) {
793 free(sval, M_NETGRAPH_PARSE);
797 bcopy(sval, buf, slen);
798 free(sval, M_NETGRAPH_PARSE);
799 bzero(buf + slen, fi->bufSize - slen);
800 *buflen = fi->bufSize;
805 ng_fixedstring_unparse(const struct ng_parse_type *type,
806 const u_char *data, int *off, char *cbuf, int cbuflen)
808 const struct ng_parse_fixedstring_info *const fi = type->info;
809 int error, temp = *off;
811 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
818 ng_fixedstring_getDefault(const struct ng_parse_type *type,
819 const u_char *const start, u_char *buf, int *buflen)
821 const struct ng_parse_fixedstring_info *const fi = type->info;
823 if (*buflen < fi->bufSize)
825 bzero(buf, fi->bufSize);
826 *buflen = fi->bufSize;
830 const struct ng_parse_type ng_parse_fixedstring_type = {
834 ng_fixedstring_parse,
835 ng_fixedstring_unparse,
836 ng_fixedstring_getDefault,
840 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
843 const struct ng_parse_type ng_parse_nodebuf_type = {
844 &ng_parse_fixedstring_type,
845 &ng_parse_nodebuf_info
848 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
851 const struct ng_parse_type ng_parse_hookbuf_type = {
852 &ng_parse_fixedstring_type,
853 &ng_parse_hookbuf_info
856 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
859 const struct ng_parse_type ng_parse_pathbuf_type = {
860 &ng_parse_fixedstring_type,
861 &ng_parse_pathbuf_info
864 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
867 const struct ng_parse_type ng_parse_typebuf_type = {
868 &ng_parse_fixedstring_type,
869 &ng_parse_typebuf_info
872 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
875 const struct ng_parse_type ng_parse_cmdbuf_type = {
876 &ng_parse_fixedstring_type,
877 &ng_parse_cmdbuf_info
880 /************************************************************************
881 EXPLICITLY SIZED STRING TYPE
882 ************************************************************************/
885 ng_sizedstring_parse(const struct ng_parse_type *type,
886 const char *s, int *off, const u_char *const start,
887 u_char *const buf, int *buflen)
893 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
895 if (slen > USHRT_MAX) {
896 free(sval, M_NETGRAPH_PARSE);
900 *((u_int16_t *)buf) = (u_int16_t)slen;
901 bcopy(sval, buf + 2, slen);
902 free(sval, M_NETGRAPH_PARSE);
908 ng_sizedstring_unparse(const struct ng_parse_type *type,
909 const u_char *data, int *off, char *cbuf, int cbuflen)
911 const char *const raw = (const char *)data + *off + 2;
912 const int slen = *((const u_int16_t *)(data + *off));
913 char *const s = ng_encode_string(raw, slen);
918 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
919 free(s, M_NETGRAPH_PARSE);
922 free(s, M_NETGRAPH_PARSE);
928 ng_sizedstring_getDefault(const struct ng_parse_type *type,
929 const u_char *const start, u_char *buf, int *buflen)
938 const struct ng_parse_type ng_parse_sizedstring_type = {
942 ng_sizedstring_parse,
943 ng_sizedstring_unparse,
944 ng_sizedstring_getDefault,
948 /************************************************************************
950 ************************************************************************/
953 ng_ipaddr_parse(const struct ng_parse_type *type,
954 const char *s, int *off, const u_char *const start,
955 u_char *const buf, int *buflen)
959 for (i = 0; i < 4; i++) {
960 if ((error = ng_int8_parse(&ng_parse_int8_type,
961 s, off, start, buf + i, buflen)) != 0)
974 ng_ipaddr_unparse(const struct ng_parse_type *type,
975 const u_char *data, int *off, char *cbuf, int cbuflen)
980 bcopy(data + *off, &ip, sizeof(ip));
981 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
982 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
983 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
990 ng_ipaddr_getDefault(const struct ng_parse_type *type,
991 const u_char *const start, u_char *buf, int *buflen)
993 struct in_addr ip = { 0 };
995 if (*buflen < sizeof(ip))
997 bcopy(&ip, buf, sizeof(ip));
998 *buflen = sizeof(ip);
1002 const struct ng_parse_type ng_parse_ipaddr_type = {
1008 ng_ipaddr_getDefault,
1012 /************************************************************************
1013 ETHERNET ADDRESS TYPE
1014 ************************************************************************/
1017 ng_enaddr_parse(const struct ng_parse_type *type,
1018 const char *s, int *const off, const u_char *const start,
1019 u_char *const buf, int *const buflen)
1025 if (*buflen < ETHER_ADDR_LEN)
1027 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1028 val = strtoul(s + *off, &eptr, 16);
1029 if (val > 0xff || eptr == s + *off)
1031 buf[i] = (u_char)val;
1033 if (i < ETHER_ADDR_LEN - 1) {
1039 *buflen = ETHER_ADDR_LEN;
1044 ng_enaddr_unparse(const struct ng_parse_type *type,
1045 const u_char *data, int *off, char *cbuf, int cbuflen)
1049 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1050 data[*off], data[*off + 1], data[*off + 2],
1051 data[*off + 3], data[*off + 4], data[*off + 5]);
1054 *off += ETHER_ADDR_LEN;
1058 const struct ng_parse_type ng_parse_enaddr_type = {
1068 /************************************************************************
1070 ************************************************************************/
1072 /* Get the length of a byte array */
1074 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1075 const u_char *start, const u_char *buf)
1077 ng_parse_array_getLength_t *const getLength = type->private;
1079 return (*getLength)(type, start, buf);
1082 /* Byte array element type is hex int8 */
1083 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1084 &ng_parse_hint8_type,
1085 &ng_parse_bytearray_subtype_getLength,
1088 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1089 &ng_parse_array_type,
1090 &ng_parse_bytearray_subtype_info
1094 ng_bytearray_parse(const struct ng_parse_type *type,
1095 const char *s, int *off, const u_char *const start,
1096 u_char *const buf, int *buflen)
1102 /* We accept either an array of bytes or a string constant */
1103 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1104 ng_parse_array_getLength_t *const getLength = type->info;
1107 arraylen = (*getLength)(type, start, buf);
1108 if (arraylen > *buflen) {
1109 free(str, M_NETGRAPH_PARSE);
1112 if (slen > arraylen) {
1113 free(str, M_NETGRAPH_PARSE);
1116 bcopy(str, buf, slen);
1117 bzero(buf + slen, arraylen - slen);
1118 free(str, M_NETGRAPH_PARSE);
1123 struct ng_parse_type subtype;
1125 subtype = ng_parse_bytearray_subtype;
1126 subtype.private = __DECONST(void *, type->info);
1127 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1132 ng_bytearray_unparse(const struct ng_parse_type *type,
1133 const u_char *data, int *off, char *cbuf, int cbuflen)
1135 struct ng_parse_type subtype;
1137 subtype = ng_parse_bytearray_subtype;
1138 subtype.private = __DECONST(void *, type->info);
1139 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1143 ng_bytearray_getDefault(const struct ng_parse_type *type,
1144 const u_char *const start, u_char *buf, int *buflen)
1146 struct ng_parse_type subtype;
1148 subtype = ng_parse_bytearray_subtype;
1149 subtype.private = __DECONST(void *, type->info);
1150 return ng_array_getDefault(&subtype, start, buf, buflen);
1153 const struct ng_parse_type ng_parse_bytearray_type = {
1158 ng_bytearray_unparse,
1159 ng_bytearray_getDefault,
1163 /************************************************************************
1165 ************************************************************************/
1167 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1169 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1170 const u_char *start, const u_char *buf)
1172 const struct ng_mesg *msg;
1174 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1175 return msg->header.arglen;
1178 /* Type for the variable length data portion of a struct ng_mesg */
1179 static const struct ng_parse_type ng_msg_data_type = {
1180 &ng_parse_bytearray_type,
1181 &ng_parse_ng_mesg_getLength
1184 /* Type for the entire struct ng_mesg header with data section */
1185 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1186 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1187 const struct ng_parse_type ng_parse_ng_mesg_type = {
1188 &ng_parse_struct_type,
1189 &ng_parse_ng_mesg_type_fields,
1192 /************************************************************************
1193 COMPOSITE HELPER ROUTINES
1194 ************************************************************************/
1197 * Convert a structure or array from ASCII to binary
1200 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1201 int *off, const u_char *const start, u_char *const buf, int *buflen,
1202 const enum comptype ctype)
1204 const int num = ng_get_composite_len(type, start, buf, ctype);
1205 int nextIndex = 0; /* next implicit array index */
1206 u_int index; /* field or element index */
1207 int *foff; /* field value offsets in string */
1208 int align, len, blen, error = 0;
1211 foff = malloc(num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1217 /* Get opening brace/bracket */
1218 if (ng_parse_get_token(s, off, &len)
1219 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1225 /* Get individual element value positions in the string */
1227 enum ng_parse_token tok;
1229 /* Check for closing brace/bracket */
1230 tok = ng_parse_get_token(s, off, &len);
1231 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1236 /* For arrays, the 'name' (ie, index) is optional, so
1237 distinguish name from values by seeing if the next
1238 token is an equals sign */
1239 if (ctype != CT_STRUCT) {
1244 /* If an opening brace/bracket, index is implied */
1245 if (tok == T_LBRACE || tok == T_LBRACKET) {
1246 index = nextIndex++;
1250 /* Might be an index, might be a value, either way... */
1251 if (tok != T_WORD) {
1256 /* If no equals sign follows, index is implied */
1258 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1259 index = nextIndex++;
1263 /* Index was specified explicitly; parse it */
1264 ul = strtoul(s + *off, &eptr, 0);
1265 if (ul == ULONG_MAX || eptr - (s + *off) != len) {
1270 nextIndex = index + 1;
1272 } else { /* a structure field */
1273 const struct ng_parse_struct_field *const
1274 fields = type->info;
1276 /* Find the field by name (required) in field list */
1277 if (tok != T_WORD) {
1281 for (index = 0; index < num; index++) {
1282 const struct ng_parse_struct_field *const
1283 field = &fields[index];
1285 if (strncmp(&s[*off], field->name, len) == 0
1286 && field->name[len] == '\0')
1295 /* Get equals sign */
1296 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1304 /* Check array index */
1310 /* Save value's position and skip over it for now */
1311 if (foff[index] != 0) {
1312 error = EALREADY; /* duplicate */
1315 while (isspace(s[*off]))
1318 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1323 /* Now build binary structure from supplied values and defaults */
1324 for (blen = index = 0; index < num; index++) {
1325 const struct ng_parse_type *const
1326 etype = ng_get_composite_etype(type, index, ctype);
1329 /* Zero-pad any alignment bytes */
1330 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1331 for (k = 0; k < pad; k++) {
1332 if (blen >= *buflen) {
1340 vlen = *buflen - blen;
1341 if (foff[index] == 0) { /* use default value */
1342 error = ng_get_composite_elem_default(type, index,
1343 start, buf + blen, &vlen, ctype);
1344 } else { /* parse given value */
1346 error = INVOKE(etype, parse)(etype,
1347 s, off, start, buf + blen, &vlen);
1354 /* Make total composite structure size a multiple of its alignment */
1355 if ((align = ALIGNMENT(type)) != 0) {
1356 while (blen % align != 0) {
1357 if (blen >= *buflen) {
1369 free(foff, M_NETGRAPH_PARSE);
1374 * Convert an array or structure from binary to ASCII
1377 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1378 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1380 const struct ng_mesg *const hdr
1381 = (const struct ng_mesg *)(data - sizeof(*hdr));
1382 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1383 const int workSize = 20 * 1024; /* XXX hard coded constant */
1384 int nextIndex = 0, didOne = 0;
1388 /* Get workspace for checking default values */
1389 workBuf = malloc(workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1390 if (workBuf == NULL)
1393 /* Opening brace/bracket */
1394 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1395 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1399 for (index = 0; index < num; index++) {
1400 const struct ng_parse_type *const
1401 etype = ng_get_composite_etype(type, index, ctype);
1403 /* Skip any alignment pad bytes */
1404 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1407 * See if element is equal to its default value; skip if so.
1408 * Copy struct ng_mesg header for types that peek into it.
1410 if (sizeof(*hdr) + *off < workSize) {
1411 int tempsize = workSize - sizeof(*hdr) - *off;
1413 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1414 if (ng_get_composite_elem_default(type, index, workBuf
1415 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1416 &tempsize, ctype) == 0
1417 && bcmp(workBuf + sizeof(*hdr) + *off,
1418 data + *off, tempsize) == 0) {
1425 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1427 if (ctype != CT_STRUCT) {
1428 if (index != nextIndex) {
1430 if ((error = ng_parse_append(&cbuf,
1431 &cbuflen, "%d=", index)) != 0)
1436 const struct ng_parse_struct_field *const
1437 fields = type->info;
1439 if ((error = ng_parse_append(&cbuf,
1440 &cbuflen, "%s=", fields[index].name)) != 0)
1445 if ((error = INVOKE(etype, unparse)
1446 (etype, data, off, cbuf, cbuflen)) != 0) {
1447 free(workBuf, M_NETGRAPH_PARSE);
1450 cbuflen -= strlen(cbuf);
1451 cbuf += strlen(cbuf);
1455 /* Closing brace/bracket */
1456 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1457 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1460 /* Clean up after failure */
1461 free(workBuf, M_NETGRAPH_PARSE);
1466 * Generate the default value for an element of an array or structure
1467 * Returns EOPNOTSUPP if default value is unspecified.
1470 ng_get_composite_elem_default(const struct ng_parse_type *type,
1471 int index, const u_char *const start, u_char *buf, int *buflen,
1472 const enum comptype ctype)
1474 const struct ng_parse_type *etype;
1475 ng_getDefault_t *func;
1482 const struct ng_parse_array_info *const ai = type->info;
1484 if (ai->getDefault != NULL) {
1485 return (*ai->getDefault)(type,
1486 index, start, buf, buflen);
1492 const struct ng_parse_fixedarray_info *const fi = type->info;
1494 if (*fi->getDefault != NULL) {
1495 return (*fi->getDefault)(type,
1496 index, start, buf, buflen);
1501 panic("%s", __func__);
1504 /* Default to element type default */
1505 etype = ng_get_composite_etype(type, index, ctype);
1506 func = METHOD(etype, getDefault);
1508 return (EOPNOTSUPP);
1509 return (*func)(etype, start, buf, buflen);
1513 * Get the number of elements in a struct, variable or fixed array.
1516 ng_get_composite_len(const struct ng_parse_type *type,
1517 const u_char *const start, const u_char *buf,
1518 const enum comptype ctype)
1523 const struct ng_parse_struct_field *const fields = type->info;
1526 for (numFields = 0; ; numFields++) {
1527 const struct ng_parse_struct_field *const
1528 fi = &fields[numFields];
1530 if (fi->name == NULL)
1537 const struct ng_parse_array_info *const ai = type->info;
1539 return (*ai->getLength)(type, start, buf);
1543 const struct ng_parse_fixedarray_info *const fi = type->info;
1548 panic("%s", __func__);
1554 * Return the type of the index'th element of a composite structure
1556 static const struct ng_parse_type *
1557 ng_get_composite_etype(const struct ng_parse_type *type,
1558 int index, const enum comptype ctype)
1560 const struct ng_parse_type *etype = NULL;
1565 const struct ng_parse_struct_field *const fields = type->info;
1567 etype = fields[index].type;
1572 const struct ng_parse_array_info *const ai = type->info;
1574 etype = ai->elementType;
1579 const struct ng_parse_fixedarray_info *const fi = type->info;
1581 etype = fi->elementType;
1585 panic("%s", __func__);
1591 * Get the number of bytes to skip to align for the next
1592 * element in a composite structure.
1595 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1596 int index, enum comptype ctype, int posn)
1598 const struct ng_parse_type *const
1599 etype = ng_get_composite_etype(type, index, ctype);
1602 /* Get element's alignment, and possibly override */
1603 align = ALIGNMENT(etype);
1604 if (ctype == CT_STRUCT) {
1605 const struct ng_parse_struct_field *const fields = type->info;
1607 if (fields[index].alignment != 0)
1608 align = fields[index].alignment;
1611 /* Return number of bytes to skip to align */
1612 return (align ? (align - (posn % align)) % align : 0);
1615 /************************************************************************
1616 PARSING HELPER ROUTINES
1617 ************************************************************************/
1620 * Append to a fixed length string buffer.
1623 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1628 va_start(args, fmt);
1629 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1631 if (len >= *cbuflenp)
1643 ng_parse_skip_value(const char *s, int off0, int *lenp)
1645 int len, nbracket, nbrace;
1648 len = nbracket = nbrace = 0;
1650 switch (ng_parse_get_token(s, &off, &len)) {
1658 if (nbracket-- == 0)
1671 } while (nbracket > 0 || nbrace > 0);
1677 * Find the next token in the string, starting at offset *startp.
1678 * Returns the token type, with *startp pointing to the first char
1679 * and *lenp the length.
1682 ng_parse_get_token(const char *s, int *startp, int *lenp)
1687 while (isspace(s[*startp]))
1689 switch (s[*startp]) {
1709 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1711 free(t, M_NETGRAPH_PARSE);
1714 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1715 && s[i] != '{' && s[i] != '}' && s[i] != '['
1716 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1718 *lenp = i - *startp;
1724 * Get a string token, which must be enclosed in double quotes.
1725 * The normal C backslash escapes are recognized.
1728 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1734 while (isspace(s[*startp]))
1737 if (s[*startp] != '"')
1739 cbuf = malloc(strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1742 strcpy(cbuf, s + start + 1);
1743 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1750 } else if (p[0] == '\\' && p[1] != '\0') {
1781 case '0': case '1': case '2': case '3':
1782 case '4': case '5': case '6': case '7':
1784 k < 3 && *v >= '0' && *v <= '7'; v++) {
1785 x = (x << 3) + (*v - '0');
1791 for (v++, x = k = 0;
1792 k < 2 && isxdigit(*v); v++) {
1793 x = (x << 4) + (isdigit(*v) ?
1795 (tolower(*v) - 'a' + 10));
1806 free(cbuf, M_NETGRAPH_PARSE);
1807 return (NULL); /* no closing quote */
1811 * Encode a string so it can be safely put in double quotes.
1812 * Caller must free the result. Exactly "slen" characters
1816 ng_encode_string(const char *raw, int slen)
1822 cbuf = malloc(strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1826 for (i = 0; i < slen; i++, raw++) {
1854 if (*raw < 0x20 || *raw > 0x7e) {
1855 off += sprintf(cbuf + off,
1856 "\\x%02x", (u_char)*raw);
1868 /************************************************************************
1869 VIRTUAL METHOD LOOKUP
1870 ************************************************************************/
1873 ng_get_parse_method(const struct ng_parse_type *t)
1875 while (t != NULL && t->parse == NULL)
1877 return (t ? t->parse : NULL);
1880 static ng_unparse_t *
1881 ng_get_unparse_method(const struct ng_parse_type *t)
1883 while (t != NULL && t->unparse == NULL)
1885 return (t ? t->unparse : NULL);
1888 static ng_getDefault_t *
1889 ng_get_getDefault_method(const struct ng_parse_type *t)
1891 while (t != NULL && t->getDefault == NULL)
1893 return (t ? t->getDefault : NULL);
1896 static ng_getAlign_t *
1897 ng_get_getAlign_method(const struct ng_parse_type *t)
1899 while (t != NULL && t->getAlign == NULL)
1901 return (t ? t->getAlign : NULL);