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@freebsd.org>
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/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
51 #include <netinet/in.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_parse.h>
57 #ifdef NG_SEPARATE_MALLOC
58 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
60 #define M_NETGRAPH_PARSE M_NETGRAPH
63 /* Compute alignment for primitive integral types */
79 #define INT8_ALIGNMENT 1
80 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
81 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
82 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
84 /* Output format for integral types */
85 #define INT_UNSIGNED 0
89 /* Type of composite object: struct, array, or fixedarray */
96 /* Composite types helper functions */
97 static int ng_parse_composite(const struct ng_parse_type *type,
98 const char *s, int *off, const u_char *start,
99 u_char *const buf, int *buflen, enum comptype ctype);
100 static int ng_unparse_composite(const struct ng_parse_type *type,
101 const u_char *data, int *off, char *cbuf, int cbuflen,
102 enum comptype ctype);
103 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
104 int index, const u_char *start, u_char *buf,
105 int *buflen, enum comptype ctype);
106 static int ng_get_composite_len(const struct ng_parse_type *type,
107 const u_char *start, const u_char *buf,
108 enum comptype ctype);
109 static const struct ng_parse_type *ng_get_composite_etype(const struct
110 ng_parse_type *type, int index, enum comptype ctype);
111 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
112 int index, enum comptype ctype, int posn);
114 /* Parsing helper functions */
115 static int ng_parse_skip_value(const char *s, int off, int *lenp);
117 /* Poor man's virtual method calls */
118 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
119 #define INVOKE(t,m) (*METHOD(t,m))
121 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
122 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
123 static ng_getDefault_t *ng_get_getDefault_method(const
124 struct ng_parse_type *t);
125 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
127 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
128 0 : INVOKE(t, getAlign)(t))
130 /* For converting binary to string */
131 #define NG_PARSE_APPEND(fmt, args...) \
135 len = snprintf((cbuf), (cbuflen), \
137 if (len >= (cbuflen)) \
143 /************************************************************************
145 ************************************************************************/
148 * Convert an ASCII string to binary according to the supplied type descriptor
151 ng_parse(const struct ng_parse_type *type,
152 const char *string, int *off, u_char *buf, int *buflen)
154 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
158 * Convert binary to an ASCII string according to the supplied type descriptor
161 ng_unparse(const struct ng_parse_type *type,
162 const u_char *data, char *cbuf, int cbuflen)
166 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
170 * Fill in the default value according to the supplied type descriptor
173 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
175 ng_getDefault_t *const func = METHOD(type, getDefault);
179 return (*func)(type, buf, buf, buflen);
183 /************************************************************************
185 ************************************************************************/
188 ng_struct_parse(const struct ng_parse_type *type,
189 const char *s, int *off, const u_char *const start,
190 u_char *const buf, int *buflen)
192 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
196 ng_struct_unparse(const struct ng_parse_type *type,
197 const u_char *data, int *off, char *cbuf, int cbuflen)
199 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
203 ng_struct_getDefault(const struct ng_parse_type *type,
204 const u_char *const start, u_char *buf, int *buflen)
208 return ng_parse_composite(type,
209 "{}", &off, start, buf, buflen, CT_STRUCT);
213 ng_struct_getAlign(const struct ng_parse_type *type)
215 const struct ng_parse_struct_field *field;
218 for (field = type->info; field->name != NULL; field++) {
219 int falign = ALIGNMENT(field->type);
227 const struct ng_parse_type ng_parse_struct_type = {
233 ng_struct_getDefault,
237 /************************************************************************
238 FIXED LENGTH ARRAY TYPE
239 ************************************************************************/
242 ng_fixedarray_parse(const struct ng_parse_type *type,
243 const char *s, int *off, const u_char *const start,
244 u_char *const buf, int *buflen)
246 return ng_parse_composite(type,
247 s, off, start, buf, buflen, CT_FIXEDARRAY);
251 ng_fixedarray_unparse(const struct ng_parse_type *type,
252 const u_char *data, int *off, char *cbuf, int cbuflen)
254 return ng_unparse_composite(type,
255 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
259 ng_fixedarray_getDefault(const struct ng_parse_type *type,
260 const u_char *const start, u_char *buf, int *buflen)
264 return ng_parse_composite(type,
265 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
269 ng_fixedarray_getAlign(const struct ng_parse_type *type)
271 const struct ng_parse_fixedarray_info *fi = type->info;
273 return ALIGNMENT(fi->elementType);
276 const struct ng_parse_type ng_parse_fixedarray_type = {
281 ng_fixedarray_unparse,
282 ng_fixedarray_getDefault,
283 ng_fixedarray_getAlign
286 /************************************************************************
287 VARIABLE LENGTH ARRAY TYPE
288 ************************************************************************/
291 ng_array_parse(const struct ng_parse_type *type,
292 const char *s, int *off, const u_char *const start,
293 u_char *const buf, int *buflen)
295 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
299 ng_array_unparse(const struct ng_parse_type *type,
300 const u_char *data, int *off, char *cbuf, int cbuflen)
302 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
306 ng_array_getDefault(const struct ng_parse_type *type,
307 const u_char *const start, u_char *buf, int *buflen)
311 return ng_parse_composite(type,
312 "[]", &off, start, buf, buflen, CT_ARRAY);
316 ng_array_getAlign(const struct ng_parse_type *type)
318 const struct ng_parse_array_info *ai = type->info;
320 return ALIGNMENT(ai->elementType);
323 const struct ng_parse_type ng_parse_array_type = {
333 /************************************************************************
335 ************************************************************************/
338 ng_int8_parse(const struct ng_parse_type *type,
339 const char *s, int *off, const u_char *const start,
340 u_char *const buf, int *buflen)
346 val = strtol(s + *off, &eptr, 0);
347 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
351 bcopy(&val8, buf, sizeof(int8_t));
352 *buflen = sizeof(int8_t);
357 ng_int8_unparse(const struct ng_parse_type *type,
358 const u_char *data, int *off, char *cbuf, int cbuflen)
364 bcopy(data + *off, &val, sizeof(int8_t));
365 switch ((int)type->info) {
372 fval = (u_int8_t)val;
376 fval = (u_int8_t)val;
379 panic("%s: unknown type", __func__);
380 #ifdef RESTARTABLE_PANICS
384 NG_PARSE_APPEND(fmt, fval);
385 *off += sizeof(int8_t);
390 ng_int8_getDefault(const struct ng_parse_type *type,
391 const u_char *const start, u_char *buf, int *buflen)
395 if (*buflen < sizeof(int8_t))
398 bcopy(&val, buf, sizeof(int8_t));
399 *buflen = sizeof(int8_t);
404 ng_int8_getAlign(const struct ng_parse_type *type)
406 return INT8_ALIGNMENT;
409 const struct ng_parse_type ng_parse_int8_type = {
419 const struct ng_parse_type ng_parse_uint8_type = {
424 const struct ng_parse_type ng_parse_hint8_type = {
429 /************************************************************************
431 ************************************************************************/
434 ng_int16_parse(const struct ng_parse_type *type,
435 const char *s, int *off, const u_char *const start,
436 u_char *const buf, int *buflen)
442 val = strtol(s + *off, &eptr, 0);
443 if (val < (int16_t)0x8000
444 || val > (u_int16_t)0xffff || eptr == s + *off)
447 val16 = (int16_t)val;
448 bcopy(&val16, buf, sizeof(int16_t));
449 *buflen = sizeof(int16_t);
454 ng_int16_unparse(const struct ng_parse_type *type,
455 const u_char *data, int *off, char *cbuf, int cbuflen)
461 bcopy(data + *off, &val, sizeof(int16_t));
462 switch ((int)type->info) {
469 fval = (u_int16_t)val;
473 fval = (u_int16_t)val;
476 panic("%s: unknown type", __func__);
477 #ifdef RESTARTABLE_PANICS
481 NG_PARSE_APPEND(fmt, fval);
482 *off += sizeof(int16_t);
487 ng_int16_getDefault(const struct ng_parse_type *type,
488 const u_char *const start, u_char *buf, int *buflen)
492 if (*buflen < sizeof(int16_t))
495 bcopy(&val, buf, sizeof(int16_t));
496 *buflen = sizeof(int16_t);
501 ng_int16_getAlign(const struct ng_parse_type *type)
503 return INT16_ALIGNMENT;
506 const struct ng_parse_type ng_parse_int16_type = {
516 const struct ng_parse_type ng_parse_uint16_type = {
517 &ng_parse_int16_type,
521 const struct ng_parse_type ng_parse_hint16_type = {
522 &ng_parse_int16_type,
526 /************************************************************************
528 ************************************************************************/
531 ng_int32_parse(const struct ng_parse_type *type,
532 const char *s, int *off, const u_char *const start,
533 u_char *const buf, int *buflen)
535 long val; /* assumes long is at least 32 bits */
539 val = strtol(s + *off, &eptr, 0);
540 if (val < (int32_t)0x80000000
541 || val > (u_int32_t)0xffffffff || eptr == s + *off)
544 val32 = (int32_t)val;
545 bcopy(&val32, buf, sizeof(int32_t));
546 *buflen = sizeof(int32_t);
551 ng_int32_unparse(const struct ng_parse_type *type,
552 const u_char *data, int *off, char *cbuf, int cbuflen)
558 bcopy(data + *off, &val, sizeof(int32_t));
559 switch ((int)type->info) {
566 fval = (u_int32_t)val;
570 fval = (u_int32_t)val;
573 panic("%s: unknown type", __func__);
574 #ifdef RESTARTABLE_PANICS
578 NG_PARSE_APPEND(fmt, fval);
579 *off += sizeof(int32_t);
584 ng_int32_getDefault(const struct ng_parse_type *type,
585 const u_char *const start, u_char *buf, int *buflen)
589 if (*buflen < sizeof(int32_t))
592 bcopy(&val, buf, sizeof(int32_t));
593 *buflen = sizeof(int32_t);
598 ng_int32_getAlign(const struct ng_parse_type *type)
600 return INT32_ALIGNMENT;
603 const struct ng_parse_type ng_parse_int32_type = {
613 const struct ng_parse_type ng_parse_uint32_type = {
614 &ng_parse_int32_type,
618 const struct ng_parse_type ng_parse_hint32_type = {
619 &ng_parse_int32_type,
623 /************************************************************************
625 ************************************************************************/
628 ng_int64_parse(const struct ng_parse_type *type,
629 const char *s, int *off, const u_char *const start,
630 u_char *const buf, int *buflen)
636 val = strtoq(s + *off, &eptr, 0);
637 if (eptr == s + *off)
640 val64 = (int64_t)val;
641 bcopy(&val64, buf, sizeof(int64_t));
642 *buflen = sizeof(int64_t);
647 ng_int64_unparse(const struct ng_parse_type *type,
648 const u_char *data, int *off, char *cbuf, int cbuflen)
654 bcopy(data + *off, &val, sizeof(int64_t));
655 switch ((int)type->info) {
662 fval = (u_int64_t)val;
666 fval = (u_int64_t)val;
669 panic("%s: unknown type", __func__);
670 #ifdef RESTARTABLE_PANICS
674 NG_PARSE_APPEND(fmt, fval);
675 *off += sizeof(int64_t);
680 ng_int64_getDefault(const struct ng_parse_type *type,
681 const u_char *const start, u_char *buf, int *buflen)
685 if (*buflen < sizeof(int64_t))
688 bcopy(&val, buf, sizeof(int64_t));
689 *buflen = sizeof(int64_t);
694 ng_int64_getAlign(const struct ng_parse_type *type)
696 return INT64_ALIGNMENT;
699 const struct ng_parse_type ng_parse_int64_type = {
709 const struct ng_parse_type ng_parse_uint64_type = {
710 &ng_parse_int64_type,
714 const struct ng_parse_type ng_parse_hint64_type = {
715 &ng_parse_int64_type,
719 /************************************************************************
721 ************************************************************************/
724 ng_string_parse(const struct ng_parse_type *type,
725 const char *s, int *off, const u_char *const start,
726 u_char *const buf, int *buflen)
732 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
735 bcopy(sval, buf, slen + 1);
736 FREE(sval, M_NETGRAPH_PARSE);
742 ng_string_unparse(const struct ng_parse_type *type,
743 const u_char *data, int *off, char *cbuf, int cbuflen)
745 const char *const raw = (const char *)data + *off;
746 char *const s = ng_encode_string(raw, strlen(raw));
750 NG_PARSE_APPEND("%s", s);
751 *off += strlen(raw) + 1;
752 FREE(s, M_NETGRAPH_PARSE);
757 ng_string_getDefault(const struct ng_parse_type *type,
758 const u_char *const start, u_char *buf, int *buflen)
763 buf[0] = (u_char)'\0';
768 const struct ng_parse_type ng_parse_string_type = {
774 ng_string_getDefault,
778 /************************************************************************
779 FIXED BUFFER STRING TYPE
780 ************************************************************************/
783 ng_fixedstring_parse(const struct ng_parse_type *type,
784 const char *s, int *off, const u_char *const start,
785 u_char *const buf, int *buflen)
787 const struct ng_parse_fixedstring_info *const fi = type->info;
792 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
794 if (slen + 1 > fi->bufSize)
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)
898 *((u_int16_t *)buf) = (u_int16_t)slen;
899 bcopy(sval, buf + 2, slen);
900 FREE(sval, M_NETGRAPH_PARSE);
906 ng_sizedstring_unparse(const struct ng_parse_type *type,
907 const u_char *data, int *off, char *cbuf, int cbuflen)
909 const char *const raw = (const char *)data + *off + 2;
910 const int slen = *((const u_int16_t *)(data + *off));
911 char *const s = ng_encode_string(raw, slen);
915 NG_PARSE_APPEND("%s", s);
916 FREE(s, M_NETGRAPH_PARSE);
922 ng_sizedstring_getDefault(const struct ng_parse_type *type,
923 const u_char *const start, u_char *buf, int *buflen)
932 const struct ng_parse_type ng_parse_sizedstring_type = {
936 ng_sizedstring_parse,
937 ng_sizedstring_unparse,
938 ng_sizedstring_getDefault,
942 /************************************************************************
944 ************************************************************************/
947 ng_ipaddr_parse(const struct ng_parse_type *type,
948 const char *s, int *off, const u_char *const start,
949 u_char *const buf, int *buflen)
953 for (i = 0; i < 4; i++) {
954 if ((error = ng_int8_parse(&ng_parse_int8_type,
955 s, off, start, buf + i, buflen)) != 0)
957 if (i < 3 && s[*off] != '.')
966 ng_ipaddr_unparse(const struct ng_parse_type *type,
967 const u_char *data, int *off, char *cbuf, int cbuflen)
971 bcopy(data + *off, &ip, sizeof(ip));
972 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
973 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
979 ng_ipaddr_getDefault(const struct ng_parse_type *type,
980 const u_char *const start, u_char *buf, int *buflen)
982 struct in_addr ip = { 0 };
984 if (*buflen < sizeof(ip))
986 bcopy(&ip, buf, sizeof(ip));
987 *buflen = sizeof(ip);
991 const struct ng_parse_type ng_parse_ipaddr_type = {
997 ng_ipaddr_getDefault,
1001 /************************************************************************
1003 ************************************************************************/
1005 /* Get the length of a byte array */
1007 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1008 const u_char *start, const u_char *buf)
1010 ng_parse_array_getLength_t *const getLength = type->private;
1012 return (*getLength)(type, start, buf);
1015 /* Byte array element type is hex int8 */
1016 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1017 &ng_parse_hint8_type,
1018 &ng_parse_bytearray_subtype_getLength,
1021 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1022 &ng_parse_array_type,
1023 &ng_parse_bytearray_subtype_info
1027 ng_bytearray_parse(const struct ng_parse_type *type,
1028 const char *s, int *off, const u_char *const start,
1029 u_char *const buf, int *buflen)
1035 /* We accept either an array of bytes or a string constant */
1036 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1037 ng_parse_array_getLength_t *const getLength = type->info;
1040 arraylen = (*getLength)(type, start, buf);
1041 if (arraylen > *buflen) {
1042 FREE(str, M_NETGRAPH_PARSE);
1045 if (slen > arraylen) {
1046 FREE(str, M_NETGRAPH_PARSE);
1049 bcopy(str, buf, slen);
1050 bzero(buf + slen, arraylen - slen);
1051 FREE(str, M_NETGRAPH_PARSE);
1056 struct ng_parse_type subtype;
1058 subtype = ng_parse_bytearray_subtype;
1059 (const void *)subtype.private = type->info;
1060 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1065 ng_bytearray_unparse(const struct ng_parse_type *type,
1066 const u_char *data, int *off, char *cbuf, int cbuflen)
1068 struct ng_parse_type subtype;
1070 subtype = ng_parse_bytearray_subtype;
1071 (const void *)subtype.private = type->info;
1072 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1076 ng_bytearray_getDefault(const struct ng_parse_type *type,
1077 const u_char *const start, u_char *buf, int *buflen)
1079 struct ng_parse_type subtype;
1081 subtype = ng_parse_bytearray_subtype;
1082 (const void *)subtype.private = type->info;
1083 return ng_array_getDefault(&subtype, start, buf, buflen);
1086 const struct ng_parse_type ng_parse_bytearray_type = {
1091 ng_bytearray_unparse,
1092 ng_bytearray_getDefault,
1096 /************************************************************************
1098 ************************************************************************/
1100 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1102 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1103 const u_char *start, const u_char *buf)
1105 const struct ng_mesg *msg;
1107 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1108 return msg->header.arglen;
1111 /* Type for the variable length data portion of a struct ng_mesg */
1112 static const struct ng_parse_type ng_msg_data_type = {
1113 &ng_parse_bytearray_type,
1114 &ng_parse_ng_mesg_getLength
1117 /* Type for the entire struct ng_mesg header with data section */
1118 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1119 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1120 const struct ng_parse_type ng_parse_ng_mesg_type = {
1121 &ng_parse_struct_type,
1122 &ng_parse_ng_mesg_type_fields,
1125 /************************************************************************
1126 COMPOSITE HELPER ROUTINES
1127 ************************************************************************/
1130 * Convert a structure or array from ASCII to binary
1133 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1134 int *off, const u_char *const start, u_char *const buf, int *buflen,
1135 const enum comptype ctype)
1137 const int num = ng_get_composite_len(type, start, buf, ctype);
1138 int nextIndex = 0; /* next implicit array index */
1139 u_int index; /* field or element index */
1140 int *foff; /* field value offsets in string */
1141 int align, len, blen, error = 0;
1144 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1150 /* Get opening brace/bracket */
1151 if (ng_parse_get_token(s, off, &len)
1152 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1158 /* Get individual element value positions in the string */
1160 enum ng_parse_token tok;
1162 /* Check for closing brace/bracket */
1163 tok = ng_parse_get_token(s, off, &len);
1164 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1169 /* For arrays, the 'name' (ie, index) is optional, so
1170 distinguish name from values by seeing if the next
1171 token is an equals sign */
1172 if (ctype != CT_STRUCT) {
1176 /* If an opening brace/bracket, index is implied */
1177 if (tok == T_LBRACE || tok == T_LBRACKET) {
1178 index = nextIndex++;
1182 /* Might be an index, might be a value, either way... */
1183 if (tok != T_WORD) {
1188 /* If no equals sign follows, index is implied */
1190 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1191 index = nextIndex++;
1195 /* Index was specified explicitly; parse it */
1196 index = (u_int)strtoul(s + *off, &eptr, 0);
1197 if (index < 0 || eptr - (s + *off) != len) {
1201 nextIndex = index + 1;
1203 } else { /* a structure field */
1204 const struct ng_parse_struct_field *const
1205 fields = type->info;
1207 /* Find the field by name (required) in field list */
1208 if (tok != T_WORD) {
1212 for (index = 0; index < num; index++) {
1213 const struct ng_parse_struct_field *const
1214 field = &fields[index];
1216 if (strncmp(&s[*off], field->name, len) == 0
1217 && field->name[len] == '\0')
1226 /* Get equals sign */
1227 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1235 /* Check array index */
1241 /* Save value's position and skip over it for now */
1242 if (foff[index] != 0) {
1243 error = EALREADY; /* duplicate */
1246 while (isspace(s[*off]))
1249 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1254 /* Now build binary structure from supplied values and defaults */
1255 for (blen = index = 0; index < num; index++) {
1256 const struct ng_parse_type *const
1257 etype = ng_get_composite_etype(type, index, ctype);
1260 /* Zero-pad any alignment bytes */
1261 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1262 for (k = 0; k < pad; k++) {
1263 if (blen >= *buflen) {
1271 vlen = *buflen - blen;
1272 if (foff[index] == 0) { /* use default value */
1273 error = ng_get_composite_elem_default(type, index,
1274 start, buf + blen, &vlen, ctype);
1275 } else { /* parse given value */
1277 error = INVOKE(etype, parse)(etype,
1278 s, off, start, buf + blen, &vlen);
1285 /* Make total composite structure size a multiple of its alignment */
1286 if ((align = ALIGNMENT(type)) != 0) {
1287 while (blen % align != 0) {
1288 if (blen >= *buflen) {
1300 FREE(foff, M_NETGRAPH_PARSE);
1305 * Convert an array or structure from binary to ASCII
1308 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1309 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1311 const struct ng_mesg *const hdr
1312 = (const struct ng_mesg *)(data - sizeof(*hdr));
1313 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1314 const int workSize = 20 * 1024; /* XXX hard coded constant */
1315 int nextIndex = 0, didOne = 0;
1319 /* Get workspace for checking default values */
1320 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1321 if (workBuf == NULL)
1324 /* Opening brace/bracket */
1325 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1328 for (index = 0; index < num; index++) {
1329 const struct ng_parse_type *const
1330 etype = ng_get_composite_etype(type, index, ctype);
1332 /* Skip any alignment pad bytes */
1333 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1336 * See if element is equal to its default value; skip if so.
1337 * Copy struct ng_mesg header for types that peek into it.
1339 if (sizeof(*hdr) + *off < workSize) {
1340 int tempsize = workSize - sizeof(*hdr) - *off;
1342 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1343 if (ng_get_composite_elem_default(type, index, workBuf
1344 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1345 &tempsize, ctype) == 0
1346 && bcmp(workBuf + sizeof(*hdr) + *off,
1347 data + *off, tempsize) == 0) {
1354 NG_PARSE_APPEND(" ");
1355 if (ctype != CT_STRUCT) {
1356 if (index != nextIndex) {
1358 NG_PARSE_APPEND("%d=", index);
1362 const struct ng_parse_struct_field *const
1363 fields = type->info;
1365 NG_PARSE_APPEND("%s=", fields[index].name);
1369 if ((error = INVOKE(etype, unparse)
1370 (etype, data, off, cbuf, cbuflen)) != 0) {
1371 FREE(workBuf, M_NETGRAPH_PARSE);
1374 cbuflen -= strlen(cbuf);
1375 cbuf += strlen(cbuf);
1378 FREE(workBuf, M_NETGRAPH_PARSE);
1380 /* Closing brace/bracket */
1381 NG_PARSE_APPEND("%s%c",
1382 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1387 * Generate the default value for an element of an array or structure
1388 * Returns EOPNOTSUPP if default value is unspecified.
1391 ng_get_composite_elem_default(const struct ng_parse_type *type,
1392 int index, const u_char *const start, u_char *buf, int *buflen,
1393 const enum comptype ctype)
1395 const struct ng_parse_type *etype;
1396 ng_getDefault_t *func;
1403 const struct ng_parse_array_info *const ai = type->info;
1405 if (ai->getDefault != NULL) {
1406 return (*ai->getDefault)(type,
1407 index, start, buf, buflen);
1413 const struct ng_parse_fixedarray_info *const fi = type->info;
1415 if (*fi->getDefault != NULL) {
1416 return (*fi->getDefault)(type,
1417 index, start, buf, buflen);
1422 panic("%s", __func__);
1425 /* Default to element type default */
1426 etype = ng_get_composite_etype(type, index, ctype);
1427 func = METHOD(etype, getDefault);
1429 return (EOPNOTSUPP);
1430 return (*func)(etype, start, buf, buflen);
1434 * Get the number of elements in a struct, variable or fixed array.
1437 ng_get_composite_len(const struct ng_parse_type *type,
1438 const u_char *const start, const u_char *buf,
1439 const enum comptype ctype)
1444 const struct ng_parse_struct_field *const fields = type->info;
1447 for (numFields = 0; ; numFields++) {
1448 const struct ng_parse_struct_field *const
1449 fi = &fields[numFields];
1451 if (fi->name == NULL)
1458 const struct ng_parse_array_info *const ai = type->info;
1460 return (*ai->getLength)(type, start, buf);
1464 const struct ng_parse_fixedarray_info *const fi = type->info;
1469 panic("%s", __func__);
1475 * Return the type of the index'th element of a composite structure
1477 static const struct ng_parse_type *
1478 ng_get_composite_etype(const struct ng_parse_type *type,
1479 int index, const enum comptype ctype)
1481 const struct ng_parse_type *etype = NULL;
1486 const struct ng_parse_struct_field *const fields = type->info;
1488 etype = fields[index].type;
1493 const struct ng_parse_array_info *const ai = type->info;
1495 etype = ai->elementType;
1500 const struct ng_parse_fixedarray_info *const fi = type->info;
1502 etype = fi->elementType;
1506 panic("%s", __func__);
1512 * Get the number of bytes to skip to align for the next
1513 * element in a composite structure.
1516 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1517 int index, enum comptype ctype, int posn)
1519 const struct ng_parse_type *const
1520 etype = ng_get_composite_etype(type, index, ctype);
1523 /* Get element's alignment, and possibly override */
1524 align = ALIGNMENT(etype);
1525 if (ctype == CT_STRUCT) {
1526 const struct ng_parse_struct_field *const fields = type->info;
1528 if (fields[index].alignment != 0)
1529 align = fields[index].alignment;
1532 /* Return number of bytes to skip to align */
1533 return (align ? (align - (posn % align)) % align : 0);
1536 /************************************************************************
1537 PARSING HELPER ROUTINES
1538 ************************************************************************/
1544 ng_parse_skip_value(const char *s, int off0, int *lenp)
1546 int len, nbracket, nbrace;
1549 len = nbracket = nbrace = 0;
1551 switch (ng_parse_get_token(s, &off, &len)) {
1559 if (nbracket-- == 0)
1572 } while (nbracket > 0 || nbrace > 0);
1578 * Find the next token in the string, starting at offset *startp.
1579 * Returns the token type, with *startp pointing to the first char
1580 * and *lenp the length.
1583 ng_parse_get_token(const char *s, int *startp, int *lenp)
1588 while (isspace(s[*startp]))
1590 switch (s[*startp]) {
1610 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1612 FREE(t, M_NETGRAPH_PARSE);
1615 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1616 && s[i] != '{' && s[i] != '}' && s[i] != '['
1617 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1619 *lenp = i - *startp;
1625 * Get a string token, which must be enclosed in double quotes.
1626 * The normal C backslash escapes are recognized.
1629 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1635 while (isspace(s[*startp]))
1638 if (s[*startp] != '"')
1640 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1643 strcpy(cbuf, s + start + 1);
1644 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1651 } else if (p[0] == '\\' && p[1] != '\0') {
1682 case '0': case '1': case '2': case '3':
1683 case '4': case '5': case '6': case '7':
1685 k < 3 && *v >= '0' && *v <= '7'; v++) {
1686 x = (x << 3) + (*v - '0');
1692 for (v++, x = k = 0;
1693 k < 2 && isxdigit(*v); v++) {
1694 x = (x << 4) + (isdigit(*v) ?
1696 (tolower(*v) - 'a' + 10));
1707 return (NULL); /* no closing quote */
1711 * Encode a string so it can be safely put in double quotes.
1712 * Caller must free the result. Exactly "slen" characters
1716 ng_encode_string(const char *raw, int slen)
1722 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1726 for (i = 0; i < slen; i++, raw++) {
1754 if (*raw < 0x20 || *raw > 0x7e) {
1755 off += sprintf(cbuf + off,
1756 "\\x%02x", (u_char)*raw);
1768 /************************************************************************
1769 VIRTUAL METHOD LOOKUP
1770 ************************************************************************/
1773 ng_get_parse_method(const struct ng_parse_type *t)
1775 while (t != NULL && t->parse == NULL)
1777 return (t ? t->parse : NULL);
1780 static ng_unparse_t *
1781 ng_get_unparse_method(const struct ng_parse_type *t)
1783 while (t != NULL && t->unparse == NULL)
1785 return (t ? t->unparse : NULL);
1788 static ng_getDefault_t *
1789 ng_get_getDefault_method(const struct ng_parse_type *t)
1791 while (t != NULL && t->getDefault == NULL)
1793 return (t ? t->getDefault : NULL);
1796 static ng_getAlign_t *
1797 ng_get_getAlign_method(const struct ng_parse_type *t)
1799 while (t != NULL && t->getAlign == NULL)
1801 return (t ? t->getAlign : NULL);