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_info *si = type->info;
216 const struct ng_parse_struct_field *field;
219 for (field = si->fields; field->name != NULL; field++) {
220 int falign = ALIGNMENT(field->type);
228 const struct ng_parse_type ng_parse_struct_type = {
234 ng_struct_getDefault,
238 /************************************************************************
239 FIXED LENGTH ARRAY TYPE
240 ************************************************************************/
243 ng_fixedarray_parse(const struct ng_parse_type *type,
244 const char *s, int *off, const u_char *const start,
245 u_char *const buf, int *buflen)
247 return ng_parse_composite(type,
248 s, off, start, buf, buflen, CT_FIXEDARRAY);
252 ng_fixedarray_unparse(const struct ng_parse_type *type,
253 const u_char *data, int *off, char *cbuf, int cbuflen)
255 return ng_unparse_composite(type,
256 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
260 ng_fixedarray_getDefault(const struct ng_parse_type *type,
261 const u_char *const start, u_char *buf, int *buflen)
265 return ng_parse_composite(type,
266 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
270 ng_fixedarray_getAlign(const struct ng_parse_type *type)
272 const struct ng_parse_fixedarray_info *fi = type->info;
274 return ALIGNMENT(fi->elementType);
277 const struct ng_parse_type ng_parse_fixedarray_type = {
282 ng_fixedarray_unparse,
283 ng_fixedarray_getDefault,
284 ng_fixedarray_getAlign
287 /************************************************************************
288 VARIABLE LENGTH ARRAY TYPE
289 ************************************************************************/
292 ng_array_parse(const struct ng_parse_type *type,
293 const char *s, int *off, const u_char *const start,
294 u_char *const buf, int *buflen)
296 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
300 ng_array_unparse(const struct ng_parse_type *type,
301 const u_char *data, int *off, char *cbuf, int cbuflen)
303 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
307 ng_array_getDefault(const struct ng_parse_type *type,
308 const u_char *const start, u_char *buf, int *buflen)
312 return ng_parse_composite(type,
313 "[]", &off, start, buf, buflen, CT_ARRAY);
317 ng_array_getAlign(const struct ng_parse_type *type)
319 const struct ng_parse_array_info *ai = type->info;
321 return ALIGNMENT(ai->elementType);
324 const struct ng_parse_type ng_parse_array_type = {
334 /************************************************************************
336 ************************************************************************/
339 ng_int8_parse(const struct ng_parse_type *type,
340 const char *s, int *off, const u_char *const start,
341 u_char *const buf, int *buflen)
347 val = strtol(s + *off, &eptr, 0);
348 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
352 bcopy(&val8, buf, sizeof(int8_t));
353 *buflen = sizeof(int8_t);
358 ng_int8_unparse(const struct ng_parse_type *type,
359 const u_char *data, int *off, char *cbuf, int cbuflen)
365 bcopy(data + *off, &val, sizeof(int8_t));
366 switch ((int)type->info) {
373 fval = (u_int8_t)val;
377 fval = (u_int8_t)val;
380 panic("%s: unknown type", __func__);
381 #ifdef RESTARTABLE_PANICS
385 NG_PARSE_APPEND(fmt, fval);
386 *off += sizeof(int8_t);
391 ng_int8_getDefault(const struct ng_parse_type *type,
392 const u_char *const start, u_char *buf, int *buflen)
396 if (*buflen < sizeof(int8_t))
399 bcopy(&val, buf, sizeof(int8_t));
400 *buflen = sizeof(int8_t);
405 ng_int8_getAlign(const struct ng_parse_type *type)
407 return INT8_ALIGNMENT;
410 const struct ng_parse_type ng_parse_int8_type = {
420 const struct ng_parse_type ng_parse_uint8_type = {
425 const struct ng_parse_type ng_parse_hint8_type = {
430 /************************************************************************
432 ************************************************************************/
435 ng_int16_parse(const struct ng_parse_type *type,
436 const char *s, int *off, const u_char *const start,
437 u_char *const buf, int *buflen)
443 val = strtol(s + *off, &eptr, 0);
444 if (val < (int16_t)0x8000
445 || val > (u_int16_t)0xffff || eptr == s + *off)
448 val16 = (int16_t)val;
449 bcopy(&val16, buf, sizeof(int16_t));
450 *buflen = sizeof(int16_t);
455 ng_int16_unparse(const struct ng_parse_type *type,
456 const u_char *data, int *off, char *cbuf, int cbuflen)
462 bcopy(data + *off, &val, sizeof(int16_t));
463 switch ((int)type->info) {
470 fval = (u_int16_t)val;
474 fval = (u_int16_t)val;
477 panic("%s: unknown type", __func__);
478 #ifdef RESTARTABLE_PANICS
482 NG_PARSE_APPEND(fmt, fval);
483 *off += sizeof(int16_t);
488 ng_int16_getDefault(const struct ng_parse_type *type,
489 const u_char *const start, u_char *buf, int *buflen)
493 if (*buflen < sizeof(int16_t))
496 bcopy(&val, buf, sizeof(int16_t));
497 *buflen = sizeof(int16_t);
502 ng_int16_getAlign(const struct ng_parse_type *type)
504 return INT16_ALIGNMENT;
507 const struct ng_parse_type ng_parse_int16_type = {
517 const struct ng_parse_type ng_parse_uint16_type = {
518 &ng_parse_int16_type,
522 const struct ng_parse_type ng_parse_hint16_type = {
523 &ng_parse_int16_type,
527 /************************************************************************
529 ************************************************************************/
532 ng_int32_parse(const struct ng_parse_type *type,
533 const char *s, int *off, const u_char *const start,
534 u_char *const buf, int *buflen)
536 long val; /* assumes long is at least 32 bits */
540 val = strtol(s + *off, &eptr, 0);
541 if (val < (int32_t)0x80000000
542 || val > (u_int32_t)0xffffffff || eptr == s + *off)
545 val32 = (int32_t)val;
546 bcopy(&val32, buf, sizeof(int32_t));
547 *buflen = sizeof(int32_t);
552 ng_int32_unparse(const struct ng_parse_type *type,
553 const u_char *data, int *off, char *cbuf, int cbuflen)
559 bcopy(data + *off, &val, sizeof(int32_t));
560 switch ((int)type->info) {
567 fval = (u_int32_t)val;
571 fval = (u_int32_t)val;
574 panic("%s: unknown type", __func__);
575 #ifdef RESTARTABLE_PANICS
579 NG_PARSE_APPEND(fmt, fval);
580 *off += sizeof(int32_t);
585 ng_int32_getDefault(const struct ng_parse_type *type,
586 const u_char *const start, u_char *buf, int *buflen)
590 if (*buflen < sizeof(int32_t))
593 bcopy(&val, buf, sizeof(int32_t));
594 *buflen = sizeof(int32_t);
599 ng_int32_getAlign(const struct ng_parse_type *type)
601 return INT32_ALIGNMENT;
604 const struct ng_parse_type ng_parse_int32_type = {
614 const struct ng_parse_type ng_parse_uint32_type = {
615 &ng_parse_int32_type,
619 const struct ng_parse_type ng_parse_hint32_type = {
620 &ng_parse_int32_type,
624 /************************************************************************
626 ************************************************************************/
629 ng_int64_parse(const struct ng_parse_type *type,
630 const char *s, int *off, const u_char *const start,
631 u_char *const buf, int *buflen)
637 val = strtoq(s + *off, &eptr, 0);
638 if (eptr == s + *off)
641 val64 = (int64_t)val;
642 bcopy(&val64, buf, sizeof(int64_t));
643 *buflen = sizeof(int64_t);
648 ng_int64_unparse(const struct ng_parse_type *type,
649 const u_char *data, int *off, char *cbuf, int cbuflen)
655 bcopy(data + *off, &val, sizeof(int64_t));
656 switch ((int)type->info) {
663 fval = (u_int64_t)val;
667 fval = (u_int64_t)val;
670 panic("%s: unknown type", __func__);
671 #ifdef RESTARTABLE_PANICS
675 NG_PARSE_APPEND(fmt, fval);
676 *off += sizeof(int64_t);
681 ng_int64_getDefault(const struct ng_parse_type *type,
682 const u_char *const start, u_char *buf, int *buflen)
686 if (*buflen < sizeof(int64_t))
689 bcopy(&val, buf, sizeof(int64_t));
690 *buflen = sizeof(int64_t);
695 ng_int64_getAlign(const struct ng_parse_type *type)
697 return INT64_ALIGNMENT;
700 const struct ng_parse_type ng_parse_int64_type = {
710 const struct ng_parse_type ng_parse_uint64_type = {
711 &ng_parse_int64_type,
715 const struct ng_parse_type ng_parse_hint64_type = {
716 &ng_parse_int64_type,
720 /************************************************************************
722 ************************************************************************/
725 ng_string_parse(const struct ng_parse_type *type,
726 const char *s, int *off, const u_char *const start,
727 u_char *const buf, int *buflen)
733 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
736 bcopy(sval, buf, slen + 1);
737 FREE(sval, M_NETGRAPH_PARSE);
743 ng_string_unparse(const struct ng_parse_type *type,
744 const u_char *data, int *off, char *cbuf, int cbuflen)
746 const char *const raw = (const char *)data + *off;
747 char *const s = ng_encode_string(raw, strlen(raw));
751 NG_PARSE_APPEND("%s", s);
752 *off += strlen(raw) + 1;
753 FREE(s, M_NETGRAPH_PARSE);
758 ng_string_getDefault(const struct ng_parse_type *type,
759 const u_char *const start, u_char *buf, int *buflen)
764 buf[0] = (u_char)'\0';
769 const struct ng_parse_type ng_parse_string_type = {
775 ng_string_getDefault,
779 /************************************************************************
780 FIXED BUFFER STRING TYPE
781 ************************************************************************/
784 ng_fixedstring_parse(const struct ng_parse_type *type,
785 const char *s, int *off, const u_char *const start,
786 u_char *const buf, int *buflen)
788 const struct ng_parse_fixedstring_info *const fi = type->info;
793 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
795 if (slen + 1 > fi->bufSize)
798 bcopy(sval, buf, slen);
799 FREE(sval, M_NETGRAPH_PARSE);
800 bzero(buf + slen, fi->bufSize - slen);
801 *buflen = fi->bufSize;
806 ng_fixedstring_unparse(const struct ng_parse_type *type,
807 const u_char *data, int *off, char *cbuf, int cbuflen)
809 const struct ng_parse_fixedstring_info *const fi = type->info;
810 int error, temp = *off;
812 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
819 ng_fixedstring_getDefault(const struct ng_parse_type *type,
820 const u_char *const start, u_char *buf, int *buflen)
822 const struct ng_parse_fixedstring_info *const fi = type->info;
824 if (*buflen < fi->bufSize)
826 bzero(buf, fi->bufSize);
827 *buflen = fi->bufSize;
831 const struct ng_parse_type ng_parse_fixedstring_type = {
835 ng_fixedstring_parse,
836 ng_fixedstring_unparse,
837 ng_fixedstring_getDefault,
841 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
844 const struct ng_parse_type ng_parse_nodebuf_type = {
845 &ng_parse_fixedstring_type,
846 &ng_parse_nodebuf_info
849 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
852 const struct ng_parse_type ng_parse_hookbuf_type = {
853 &ng_parse_fixedstring_type,
854 &ng_parse_hookbuf_info
857 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
860 const struct ng_parse_type ng_parse_pathbuf_type = {
861 &ng_parse_fixedstring_type,
862 &ng_parse_pathbuf_info
865 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
868 const struct ng_parse_type ng_parse_typebuf_type = {
869 &ng_parse_fixedstring_type,
870 &ng_parse_typebuf_info
873 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
876 const struct ng_parse_type ng_parse_cmdbuf_type = {
877 &ng_parse_fixedstring_type,
878 &ng_parse_cmdbuf_info
881 /************************************************************************
882 EXPLICITLY SIZED STRING TYPE
883 ************************************************************************/
886 ng_sizedstring_parse(const struct ng_parse_type *type,
887 const char *s, int *off, const u_char *const start,
888 u_char *const buf, int *buflen)
894 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
899 *((u_int16_t *)buf) = (u_int16_t)slen;
900 bcopy(sval, buf + 2, slen);
901 FREE(sval, M_NETGRAPH_PARSE);
907 ng_sizedstring_unparse(const struct ng_parse_type *type,
908 const u_char *data, int *off, char *cbuf, int cbuflen)
910 const char *const raw = (const char *)data + *off + 2;
911 const int slen = *((const u_int16_t *)(data + *off));
912 char *const s = ng_encode_string(raw, slen);
916 NG_PARSE_APPEND("%s", s);
917 FREE(s, M_NETGRAPH_PARSE);
923 ng_sizedstring_getDefault(const struct ng_parse_type *type,
924 const u_char *const start, u_char *buf, int *buflen)
933 const struct ng_parse_type ng_parse_sizedstring_type = {
937 ng_sizedstring_parse,
938 ng_sizedstring_unparse,
939 ng_sizedstring_getDefault,
943 /************************************************************************
945 ************************************************************************/
948 ng_ipaddr_parse(const struct ng_parse_type *type,
949 const char *s, int *off, const u_char *const start,
950 u_char *const buf, int *buflen)
954 for (i = 0; i < 4; i++) {
955 if ((error = ng_int8_parse(&ng_parse_int8_type,
956 s, off, start, buf + i, buflen)) != 0)
958 if (i < 3 && s[*off] != '.')
967 ng_ipaddr_unparse(const struct ng_parse_type *type,
968 const u_char *data, int *off, char *cbuf, int cbuflen)
972 bcopy(data + *off, &ip, sizeof(ip));
973 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
974 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
980 ng_ipaddr_getDefault(const struct ng_parse_type *type,
981 const u_char *const start, u_char *buf, int *buflen)
983 struct in_addr ip = { 0 };
985 if (*buflen < sizeof(ip))
987 bcopy(&ip, buf, sizeof(ip));
988 *buflen = sizeof(ip);
992 const struct ng_parse_type ng_parse_ipaddr_type = {
998 ng_ipaddr_getDefault,
1002 /************************************************************************
1004 ************************************************************************/
1006 /* Get the length of a byte array */
1008 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1009 const u_char *start, const u_char *buf)
1011 ng_parse_array_getLength_t *const getLength = type->private;
1013 return (*getLength)(type, start, buf);
1016 /* Byte array element type is hex int8 */
1017 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1018 &ng_parse_hint8_type,
1019 &ng_parse_bytearray_subtype_getLength,
1022 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1023 &ng_parse_array_type,
1024 &ng_parse_bytearray_subtype_info
1028 ng_bytearray_parse(const struct ng_parse_type *type,
1029 const char *s, int *off, const u_char *const start,
1030 u_char *const buf, int *buflen)
1036 /* We accept either an array of bytes or a string constant */
1037 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1038 ng_parse_array_getLength_t *const getLength = type->info;
1041 arraylen = (*getLength)(type, start, buf);
1042 if (arraylen > *buflen) {
1043 FREE(str, M_NETGRAPH_PARSE);
1046 if (slen > arraylen) {
1047 FREE(str, M_NETGRAPH_PARSE);
1050 bcopy(str, buf, slen);
1051 bzero(buf + slen, arraylen - slen);
1052 FREE(str, M_NETGRAPH_PARSE);
1057 struct ng_parse_type subtype;
1059 subtype = ng_parse_bytearray_subtype;
1060 (const void *)subtype.private = type->info;
1061 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1066 ng_bytearray_unparse(const struct ng_parse_type *type,
1067 const u_char *data, int *off, char *cbuf, int cbuflen)
1069 struct ng_parse_type subtype;
1071 subtype = ng_parse_bytearray_subtype;
1072 (const void *)subtype.private = type->info;
1073 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1077 ng_bytearray_getDefault(const struct ng_parse_type *type,
1078 const u_char *const start, u_char *buf, int *buflen)
1080 struct ng_parse_type subtype;
1082 subtype = ng_parse_bytearray_subtype;
1083 (const void *)subtype.private = type->info;
1084 return ng_array_getDefault(&subtype, start, buf, buflen);
1087 const struct ng_parse_type ng_parse_bytearray_type = {
1092 ng_bytearray_unparse,
1093 ng_bytearray_getDefault,
1097 /************************************************************************
1099 ************************************************************************/
1101 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1103 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1104 const u_char *start, const u_char *buf)
1106 const struct ng_mesg *msg;
1108 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1109 return msg->header.arglen;
1112 /* Type for the variable length data portion of a struct ng_mesg */
1113 static const struct ng_parse_type ng_msg_data_type = {
1114 &ng_parse_bytearray_type,
1115 &ng_parse_ng_mesg_getLength
1118 /* Type for the entire struct ng_mesg header with data section */
1119 static const struct ng_parse_struct_info
1120 ng_parse_ng_mesg_type_info = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1121 const struct ng_parse_type ng_parse_ng_mesg_type = {
1122 &ng_parse_struct_type,
1123 &ng_parse_ng_mesg_type_info,
1126 /************************************************************************
1127 COMPOSITE HELPER ROUTINES
1128 ************************************************************************/
1131 * Convert a structure or array from ASCII to binary
1134 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1135 int *off, const u_char *const start, u_char *const buf, int *buflen,
1136 const enum comptype ctype)
1138 const int num = ng_get_composite_len(type, start, buf, ctype);
1139 int nextIndex = 0; /* next implicit array index */
1140 u_int index; /* field or element index */
1141 int *foff; /* field value offsets in string */
1142 int align, len, blen, error = 0;
1145 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1151 /* Get opening brace/bracket */
1152 if (ng_parse_get_token(s, off, &len)
1153 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1159 /* Get individual element value positions in the string */
1161 enum ng_parse_token tok;
1163 /* Check for closing brace/bracket */
1164 tok = ng_parse_get_token(s, off, &len);
1165 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1170 /* For arrays, the 'name' (ie, index) is optional, so
1171 distinguish name from values by seeing if the next
1172 token is an equals sign */
1173 if (ctype != CT_STRUCT) {
1177 /* If an opening brace/bracket, index is implied */
1178 if (tok == T_LBRACE || tok == T_LBRACKET) {
1179 index = nextIndex++;
1183 /* Might be an index, might be a value, either way... */
1184 if (tok != T_WORD) {
1189 /* If no equals sign follows, index is implied */
1191 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1192 index = nextIndex++;
1196 /* Index was specified explicitly; parse it */
1197 index = (u_int)strtoul(s + *off, &eptr, 0);
1198 if (index < 0 || eptr - (s + *off) != len) {
1202 nextIndex = index + 1;
1205 } else { /* a structure field */
1206 const struct ng_parse_struct_field *field = NULL;
1207 const struct ng_parse_struct_info *si = type->info;
1209 /* Find the field by name (required) in field list */
1210 if (tok != T_WORD) {
1214 for (index = 0; index < num; index++) {
1215 field = &si->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) {
1234 /* Check array index */
1240 /* Save value's position and skip over it for now */
1241 if (foff[index] != 0) {
1242 error = EALREADY; /* duplicate */
1245 while (isspace(s[*off]))
1248 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1253 /* Now build binary structure from supplied values and defaults */
1254 for (blen = index = 0; index < num; index++) {
1255 const struct ng_parse_type *const
1256 etype = ng_get_composite_etype(type, index, ctype);
1259 /* Zero-pad any alignment bytes */
1260 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1261 for (k = 0; k < pad; k++) {
1262 if (blen >= *buflen) {
1270 vlen = *buflen - blen;
1271 if (foff[index] == 0) { /* use default value */
1272 error = ng_get_composite_elem_default(type, index,
1273 start, buf + blen, &vlen, ctype);
1274 } else { /* parse given value */
1276 error = INVOKE(etype, parse)(etype,
1277 s, off, start, buf + blen, &vlen);
1284 /* Make total composite structure size a multiple of its alignment */
1285 if ((align = ALIGNMENT(type)) != 0) {
1286 while (blen % align != 0) {
1287 if (blen >= *buflen) {
1299 FREE(foff, M_NETGRAPH_PARSE);
1304 * Convert an array or structure from binary to ASCII
1307 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1308 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1310 const struct ng_mesg *const hdr
1311 = (const struct ng_mesg *)(data - sizeof(*hdr));
1312 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1313 const int workSize = 20 * 1024; /* XXX hard coded constant */
1314 int nextIndex = 0, didOne = 0;
1318 /* Get workspace for checking default values */
1319 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1320 if (workBuf == NULL)
1323 /* Opening brace/bracket */
1324 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1327 for (index = 0; index < num; index++) {
1328 const struct ng_parse_type *const
1329 etype = ng_get_composite_etype(type, index, ctype);
1331 /* Skip any alignment pad bytes */
1332 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1335 * See if element is equal to its default value; skip if so.
1336 * Copy struct ng_mesg header for types that peek into it.
1338 if (sizeof(*hdr) + *off < workSize) {
1339 int tempsize = workSize - sizeof(*hdr) - *off;
1341 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1342 if (ng_get_composite_elem_default(type, index, workBuf
1343 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1344 &tempsize, ctype) == 0
1345 && bcmp(workBuf + sizeof(*hdr) + *off,
1346 data + *off, tempsize) == 0) {
1353 NG_PARSE_APPEND(" ");
1354 if (ctype != CT_STRUCT) {
1355 if (index != nextIndex) {
1357 NG_PARSE_APPEND("%d=", index);
1361 const struct ng_parse_struct_info *si = type->info;
1363 NG_PARSE_APPEND("%s=", si->fields[index].name);
1367 if ((error = INVOKE(etype, unparse)
1368 (etype, data, off, cbuf, cbuflen)) != 0) {
1369 FREE(workBuf, M_NETGRAPH_PARSE);
1372 cbuflen -= strlen(cbuf);
1373 cbuf += strlen(cbuf);
1376 FREE(workBuf, M_NETGRAPH_PARSE);
1378 /* Closing brace/bracket */
1379 NG_PARSE_APPEND("%s%c",
1380 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1385 * Generate the default value for an element of an array or structure
1386 * Returns EOPNOTSUPP if default value is unspecified.
1389 ng_get_composite_elem_default(const struct ng_parse_type *type,
1390 int index, const u_char *const start, u_char *buf, int *buflen,
1391 const enum comptype ctype)
1393 const struct ng_parse_type *etype;
1394 ng_getDefault_t *func;
1401 const struct ng_parse_array_info *const ai = type->info;
1403 if (ai->getDefault != NULL) {
1404 return (*ai->getDefault)(type,
1405 index, start, buf, buflen);
1411 const struct ng_parse_fixedarray_info *const fi = type->info;
1413 if (*fi->getDefault != NULL) {
1414 return (*fi->getDefault)(type,
1415 index, start, buf, buflen);
1420 panic("%s", __func__);
1423 /* Default to element type default */
1424 etype = ng_get_composite_etype(type, index, ctype);
1425 func = METHOD(etype, getDefault);
1427 return (EOPNOTSUPP);
1428 return (*func)(etype, start, buf, buflen);
1432 * Get the number of elements in a struct, variable or fixed array.
1435 ng_get_composite_len(const struct ng_parse_type *type,
1436 const u_char *const start, const u_char *buf,
1437 const enum comptype ctype)
1442 const struct ng_parse_struct_info *const si = type->info;
1445 for (numFields = 0; ; numFields++) {
1446 const struct ng_parse_struct_field *const
1447 fi = &si->fields[numFields];
1449 if (fi->name == NULL)
1456 const struct ng_parse_array_info *const ai = type->info;
1458 return (*ai->getLength)(type, start, buf);
1462 const struct ng_parse_fixedarray_info *const fi = type->info;
1467 panic("%s", __func__);
1473 * Return the type of the index'th element of a composite structure
1475 static const struct ng_parse_type *
1476 ng_get_composite_etype(const struct ng_parse_type *type,
1477 int index, const enum comptype ctype)
1479 const struct ng_parse_type *etype = NULL;
1484 const struct ng_parse_struct_info *const si = type->info;
1486 etype = si->fields[index].type;
1491 const struct ng_parse_array_info *const ai = type->info;
1493 etype = ai->elementType;
1498 const struct ng_parse_fixedarray_info *const fi = type->info;
1500 etype = fi->elementType;
1504 panic("%s", __func__);
1510 * Get the number of bytes to skip to align for the next
1511 * element in a composite structure.
1514 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1515 int index, enum comptype ctype, int posn)
1517 const struct ng_parse_type *const
1518 etype = ng_get_composite_etype(type, index, ctype);
1521 /* Get element's alignment, and possibly override */
1522 align = ALIGNMENT(etype);
1523 if (ctype == CT_STRUCT) {
1524 const struct ng_parse_struct_info *si = type->info;
1526 if (si->fields[index].alignment != 0)
1527 align = si->fields[index].alignment;
1530 /* Return number of bytes to skip to align */
1531 return (align ? (align - (posn % align)) % align : 0);
1534 /************************************************************************
1535 PARSING HELPER ROUTINES
1536 ************************************************************************/
1542 ng_parse_skip_value(const char *s, int off0, int *lenp)
1544 int len, nbracket, nbrace;
1547 len = nbracket = nbrace = 0;
1549 switch (ng_parse_get_token(s, &off, &len)) {
1557 if (nbracket-- == 0)
1570 } while (nbracket > 0 || nbrace > 0);
1576 * Find the next token in the string, starting at offset *startp.
1577 * Returns the token type, with *startp pointing to the first char
1578 * and *lenp the length.
1581 ng_parse_get_token(const char *s, int *startp, int *lenp)
1586 while (isspace(s[*startp]))
1588 switch (s[*startp]) {
1608 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1610 FREE(t, M_NETGRAPH_PARSE);
1613 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1614 && s[i] != '{' && s[i] != '}' && s[i] != '['
1615 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1617 *lenp = i - *startp;
1623 * Get a string token, which must be enclosed in double quotes.
1624 * The normal C backslash escapes are recognized.
1627 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1633 while (isspace(s[*startp]))
1636 if (s[*startp] != '"')
1638 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1641 strcpy(cbuf, s + start + 1);
1642 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1649 } else if (p[0] == '\\' && p[1] != '\0') {
1680 case '0': case '1': case '2': case '3':
1681 case '4': case '5': case '6': case '7':
1683 k < 3 && *v >= '0' && *v <= '7'; v++) {
1684 x = (x << 3) + (*v - '0');
1690 for (v++, x = k = 0;
1691 k < 2 && isxdigit(*v); v++) {
1692 x = (x << 4) + (isdigit(*v) ?
1694 (tolower(*v) - 'a' + 10));
1705 return (NULL); /* no closing quote */
1709 * Encode a string so it can be safely put in double quotes.
1710 * Caller must free the result. Exactly "slen" characters
1714 ng_encode_string(const char *raw, int slen)
1720 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1724 for (i = 0; i < slen; i++, raw++) {
1752 if (*raw < 0x20 || *raw > 0x7e) {
1753 off += sprintf(cbuf + off,
1754 "\\x%02x", (u_char)*raw);
1766 /************************************************************************
1767 VIRTUAL METHOD LOOKUP
1768 ************************************************************************/
1771 ng_get_parse_method(const struct ng_parse_type *t)
1773 while (t != NULL && t->parse == NULL)
1775 return (t ? t->parse : NULL);
1778 static ng_unparse_t *
1779 ng_get_unparse_method(const struct ng_parse_type *t)
1781 while (t != NULL && t->unparse == NULL)
1783 return (t ? t->unparse : NULL);
1786 static ng_getDefault_t *
1787 ng_get_getDefault_method(const struct ng_parse_type *t)
1789 while (t != NULL && t->getDefault == NULL)
1791 return (t ? t->getDefault : NULL);
1794 static ng_getAlign_t *
1795 ng_get_getAlign_method(const struct ng_parse_type *t)
1797 while (t != NULL && t->getAlign == NULL)
1799 return (t ? t->getAlign : NULL);