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 $
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/limits.h>
49 #include <sys/malloc.h>
51 #include <sys/ctype.h>
53 #include <machine/stdarg.h>
55 #include <net/ethernet.h>
57 #include <netinet/in.h>
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
63 #ifdef NG_SEPARATE_MALLOC
64 static MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
66 #define M_NETGRAPH_PARSE M_NETGRAPH
69 /* Compute alignment for primitive integral types */
85 #define INT8_ALIGNMENT 1
86 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
87 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
88 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
90 /* Output format for integral types */
91 #define INT_UNSIGNED 0
95 /* Type of composite object: struct, array, or fixedarray */
102 /* Composite types helper functions */
103 static int ng_parse_composite(const struct ng_parse_type *type,
104 const char *s, int *off, const u_char *start,
105 u_char *const buf, int *buflen, enum comptype ctype);
106 static int ng_unparse_composite(const struct ng_parse_type *type,
107 const u_char *data, int *off, char *cbuf, int cbuflen,
108 enum comptype ctype);
109 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
110 int index, const u_char *start, u_char *buf,
111 int *buflen, enum comptype ctype);
112 static int ng_get_composite_len(const struct ng_parse_type *type,
113 const u_char *start, const u_char *buf,
114 enum comptype ctype);
115 static const struct ng_parse_type *ng_get_composite_etype(const struct
116 ng_parse_type *type, int index, enum comptype ctype);
117 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
118 int index, enum comptype ctype, int posn);
120 /* Parsing helper functions */
121 static int ng_parse_skip_value(const char *s, int off, int *lenp);
122 static int ng_parse_append(char **cbufp, int *cbuflenp,
123 const char *fmt, ...);
125 /* Poor man's virtual method calls */
126 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
127 #define INVOKE(t,m) (*METHOD(t,m))
129 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
130 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
131 static ng_getDefault_t *ng_get_getDefault_method(const
132 struct ng_parse_type *t);
133 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
135 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
136 0 : INVOKE(t, getAlign)(t))
138 /************************************************************************
140 ************************************************************************/
143 * Convert an ASCII string to binary according to the supplied type descriptor
146 ng_parse(const struct ng_parse_type *type,
147 const char *string, int *off, u_char *buf, int *buflen)
149 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
153 * Convert binary to an ASCII string according to the supplied type descriptor
156 ng_unparse(const struct ng_parse_type *type,
157 const u_char *data, char *cbuf, int cbuflen)
161 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
165 * Fill in the default value according to the supplied type descriptor
168 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
170 ng_getDefault_t *const func = METHOD(type, getDefault);
174 return (*func)(type, buf, buf, buflen);
177 /************************************************************************
179 ************************************************************************/
182 ng_struct_parse(const struct ng_parse_type *type,
183 const char *s, int *off, const u_char *const start,
184 u_char *const buf, int *buflen)
186 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
190 ng_struct_unparse(const struct ng_parse_type *type,
191 const u_char *data, int *off, char *cbuf, int cbuflen)
193 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
197 ng_struct_getDefault(const struct ng_parse_type *type,
198 const u_char *const start, u_char *buf, int *buflen)
202 return ng_parse_composite(type,
203 "{}", &off, start, buf, buflen, CT_STRUCT);
207 ng_struct_getAlign(const struct ng_parse_type *type)
209 const struct ng_parse_struct_field *field;
212 for (field = type->info; field->name != NULL; field++) {
213 int falign = ALIGNMENT(field->type);
221 const struct ng_parse_type ng_parse_struct_type = {
227 ng_struct_getDefault,
231 /************************************************************************
232 FIXED LENGTH ARRAY TYPE
233 ************************************************************************/
236 ng_fixedarray_parse(const struct ng_parse_type *type,
237 const char *s, int *off, const u_char *const start,
238 u_char *const buf, int *buflen)
240 return ng_parse_composite(type,
241 s, off, start, buf, buflen, CT_FIXEDARRAY);
245 ng_fixedarray_unparse(const struct ng_parse_type *type,
246 const u_char *data, int *off, char *cbuf, int cbuflen)
248 return ng_unparse_composite(type,
249 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
253 ng_fixedarray_getDefault(const struct ng_parse_type *type,
254 const u_char *const start, u_char *buf, int *buflen)
258 return ng_parse_composite(type,
259 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
263 ng_fixedarray_getAlign(const struct ng_parse_type *type)
265 const struct ng_parse_fixedarray_info *fi = type->info;
267 return ALIGNMENT(fi->elementType);
270 const struct ng_parse_type ng_parse_fixedarray_type = {
275 ng_fixedarray_unparse,
276 ng_fixedarray_getDefault,
277 ng_fixedarray_getAlign
280 /************************************************************************
281 VARIABLE LENGTH ARRAY TYPE
282 ************************************************************************/
285 ng_array_parse(const struct ng_parse_type *type,
286 const char *s, int *off, const u_char *const start,
287 u_char *const buf, int *buflen)
289 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
293 ng_array_unparse(const struct ng_parse_type *type,
294 const u_char *data, int *off, char *cbuf, int cbuflen)
296 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
300 ng_array_getDefault(const struct ng_parse_type *type,
301 const u_char *const start, u_char *buf, int *buflen)
305 return ng_parse_composite(type,
306 "[]", &off, start, buf, buflen, CT_ARRAY);
310 ng_array_getAlign(const struct ng_parse_type *type)
312 const struct ng_parse_array_info *ai = type->info;
314 return ALIGNMENT(ai->elementType);
317 const struct ng_parse_type ng_parse_array_type = {
327 /************************************************************************
329 ************************************************************************/
332 ng_int8_parse(const struct ng_parse_type *type,
333 const char *s, int *off, const u_char *const start,
334 u_char *const buf, int *buflen)
340 val = strtol(s + *off, &eptr, 0);
341 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
345 bcopy(&val8, buf, sizeof(int8_t));
346 *buflen = sizeof(int8_t);
351 ng_int8_unparse(const struct ng_parse_type *type,
352 const u_char *data, int *off, char *cbuf, int cbuflen)
359 bcopy(data + *off, &val, sizeof(int8_t));
360 switch ((intptr_t)type->info) {
367 fval = (u_int8_t)val;
371 fval = (u_int8_t)val;
374 panic("%s: unknown type", __func__);
376 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
378 *off += sizeof(int8_t);
383 ng_int8_getDefault(const struct ng_parse_type *type,
384 const u_char *const start, u_char *buf, int *buflen)
388 if (*buflen < sizeof(int8_t))
391 bcopy(&val, buf, sizeof(int8_t));
392 *buflen = sizeof(int8_t);
397 ng_int8_getAlign(const struct ng_parse_type *type)
399 return INT8_ALIGNMENT;
402 const struct ng_parse_type ng_parse_int8_type = {
412 const struct ng_parse_type ng_parse_uint8_type = {
417 const struct ng_parse_type ng_parse_hint8_type = {
422 /************************************************************************
424 ************************************************************************/
427 ng_int16_parse(const struct ng_parse_type *type,
428 const char *s, int *off, const u_char *const start,
429 u_char *const buf, int *buflen)
435 val = strtol(s + *off, &eptr, 0);
436 if (val < (int16_t)0x8000
437 || val > (u_int16_t)0xffff || eptr == s + *off)
440 val16 = (int16_t)val;
441 bcopy(&val16, buf, sizeof(int16_t));
442 *buflen = sizeof(int16_t);
447 ng_int16_unparse(const struct ng_parse_type *type,
448 const u_char *data, int *off, char *cbuf, int cbuflen)
455 bcopy(data + *off, &val, sizeof(int16_t));
456 switch ((intptr_t)type->info) {
463 fval = (u_int16_t)val;
467 fval = (u_int16_t)val;
470 panic("%s: unknown type", __func__);
472 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
474 *off += sizeof(int16_t);
479 ng_int16_getDefault(const struct ng_parse_type *type,
480 const u_char *const start, u_char *buf, int *buflen)
484 if (*buflen < sizeof(int16_t))
487 bcopy(&val, buf, sizeof(int16_t));
488 *buflen = sizeof(int16_t);
493 ng_int16_getAlign(const struct ng_parse_type *type)
495 return INT16_ALIGNMENT;
498 const struct ng_parse_type ng_parse_int16_type = {
508 const struct ng_parse_type ng_parse_uint16_type = {
509 &ng_parse_int16_type,
513 const struct ng_parse_type ng_parse_hint16_type = {
514 &ng_parse_int16_type,
518 /************************************************************************
520 ************************************************************************/
523 ng_int32_parse(const struct ng_parse_type *type,
524 const char *s, int *off, const u_char *const start,
525 u_char *const buf, int *buflen)
527 long val; /* assumes long is at least 32 bits */
531 if ((intptr_t)type->info == INT_SIGNED)
532 val = strtol(s + *off, &eptr, 0);
534 val = strtoul(s + *off, &eptr, 0);
535 if (val < (int32_t)0x80000000
536 || val > (u_int32_t)0xffffffff || eptr == s + *off)
539 val32 = (int32_t)val;
540 bcopy(&val32, buf, sizeof(int32_t));
541 *buflen = sizeof(int32_t);
546 ng_int32_unparse(const struct ng_parse_type *type,
547 const u_char *data, int *off, char *cbuf, int cbuflen)
554 bcopy(data + *off, &val, sizeof(int32_t));
555 switch ((intptr_t)type->info) {
562 fval = (u_int32_t)val;
566 fval = (u_int32_t)val;
569 panic("%s: unknown type", __func__);
571 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
573 *off += sizeof(int32_t);
578 ng_int32_getDefault(const struct ng_parse_type *type,
579 const u_char *const start, u_char *buf, int *buflen)
583 if (*buflen < sizeof(int32_t))
586 bcopy(&val, buf, sizeof(int32_t));
587 *buflen = sizeof(int32_t);
592 ng_int32_getAlign(const struct ng_parse_type *type)
594 return INT32_ALIGNMENT;
597 const struct ng_parse_type ng_parse_int32_type = {
607 const struct ng_parse_type ng_parse_uint32_type = {
608 &ng_parse_int32_type,
612 const struct ng_parse_type ng_parse_hint32_type = {
613 &ng_parse_int32_type,
617 /************************************************************************
619 ************************************************************************/
622 ng_int64_parse(const struct ng_parse_type *type,
623 const char *s, int *off, const u_char *const start,
624 u_char *const buf, int *buflen)
630 val = strtoq(s + *off, &eptr, 0);
631 if (eptr == s + *off)
634 val64 = (int64_t)val;
635 bcopy(&val64, buf, sizeof(int64_t));
636 *buflen = sizeof(int64_t);
641 ng_int64_unparse(const struct ng_parse_type *type,
642 const u_char *data, int *off, char *cbuf, int cbuflen)
649 bcopy(data + *off, &val, sizeof(int64_t));
650 switch ((intptr_t)type->info) {
657 fval = (u_int64_t)val;
661 fval = (u_int64_t)val;
664 panic("%s: unknown type", __func__);
666 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
668 *off += sizeof(int64_t);
673 ng_int64_getDefault(const struct ng_parse_type *type,
674 const u_char *const start, u_char *buf, int *buflen)
678 if (*buflen < sizeof(int64_t))
681 bcopy(&val, buf, sizeof(int64_t));
682 *buflen = sizeof(int64_t);
687 ng_int64_getAlign(const struct ng_parse_type *type)
689 return INT64_ALIGNMENT;
692 const struct ng_parse_type ng_parse_int64_type = {
702 const struct ng_parse_type ng_parse_uint64_type = {
703 &ng_parse_int64_type,
707 const struct ng_parse_type ng_parse_hint64_type = {
708 &ng_parse_int64_type,
712 /************************************************************************
714 ************************************************************************/
717 ng_string_parse(const struct ng_parse_type *type,
718 const char *s, int *off, const u_char *const start,
719 u_char *const buf, int *buflen)
725 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
728 bcopy(sval, buf, slen + 1);
729 free(sval, M_NETGRAPH_PARSE);
735 ng_string_unparse(const struct ng_parse_type *type,
736 const u_char *data, int *off, char *cbuf, int cbuflen)
738 const char *const raw = (const char *)data + *off;
739 char *const s = ng_encode_string(raw, strlen(raw));
744 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
745 free(s, M_NETGRAPH_PARSE);
748 *off += strlen(raw) + 1;
749 free(s, M_NETGRAPH_PARSE);
754 ng_string_getDefault(const struct ng_parse_type *type,
755 const u_char *const start, u_char *buf, int *buflen)
760 buf[0] = (u_char)'\0';
765 const struct ng_parse_type ng_parse_string_type = {
771 ng_string_getDefault,
775 /************************************************************************
776 FIXED BUFFER STRING TYPE
777 ************************************************************************/
780 ng_fixedstring_parse(const struct ng_parse_type *type,
781 const char *s, int *off, const u_char *const start,
782 u_char *const buf, int *buflen)
784 const struct ng_parse_fixedstring_info *const fi = type->info;
789 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
791 if (slen + 1 > fi->bufSize) {
792 free(sval, M_NETGRAPH_PARSE);
796 bcopy(sval, buf, slen);
797 free(sval, M_NETGRAPH_PARSE);
798 bzero(buf + slen, fi->bufSize - slen);
799 *buflen = fi->bufSize;
804 ng_fixedstring_unparse(const struct ng_parse_type *type,
805 const u_char *data, int *off, char *cbuf, int cbuflen)
807 const struct ng_parse_fixedstring_info *const fi = type->info;
808 int error, temp = *off;
810 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
817 ng_fixedstring_getDefault(const struct ng_parse_type *type,
818 const u_char *const start, u_char *buf, int *buflen)
820 const struct ng_parse_fixedstring_info *const fi = type->info;
822 if (*buflen < fi->bufSize)
824 bzero(buf, fi->bufSize);
825 *buflen = fi->bufSize;
829 const struct ng_parse_type ng_parse_fixedstring_type = {
833 ng_fixedstring_parse,
834 ng_fixedstring_unparse,
835 ng_fixedstring_getDefault,
839 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
842 const struct ng_parse_type ng_parse_nodebuf_type = {
843 &ng_parse_fixedstring_type,
844 &ng_parse_nodebuf_info
847 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
850 const struct ng_parse_type ng_parse_hookbuf_type = {
851 &ng_parse_fixedstring_type,
852 &ng_parse_hookbuf_info
855 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
858 const struct ng_parse_type ng_parse_pathbuf_type = {
859 &ng_parse_fixedstring_type,
860 &ng_parse_pathbuf_info
863 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
866 const struct ng_parse_type ng_parse_typebuf_type = {
867 &ng_parse_fixedstring_type,
868 &ng_parse_typebuf_info
871 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
874 const struct ng_parse_type ng_parse_cmdbuf_type = {
875 &ng_parse_fixedstring_type,
876 &ng_parse_cmdbuf_info
879 /************************************************************************
880 EXPLICITLY SIZED STRING TYPE
881 ************************************************************************/
884 ng_sizedstring_parse(const struct ng_parse_type *type,
885 const char *s, int *off, const u_char *const start,
886 u_char *const buf, int *buflen)
892 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
894 if (slen > USHRT_MAX) {
895 free(sval, M_NETGRAPH_PARSE);
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);
917 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
918 free(s, M_NETGRAPH_PARSE);
921 free(s, M_NETGRAPH_PARSE);
927 ng_sizedstring_getDefault(const struct ng_parse_type *type,
928 const u_char *const start, u_char *buf, int *buflen)
937 const struct ng_parse_type ng_parse_sizedstring_type = {
941 ng_sizedstring_parse,
942 ng_sizedstring_unparse,
943 ng_sizedstring_getDefault,
947 /************************************************************************
949 ************************************************************************/
952 ng_ipaddr_parse(const struct ng_parse_type *type,
953 const char *s, int *off, const u_char *const start,
954 u_char *const buf, int *buflen)
958 for (i = 0; i < 4; i++) {
959 if ((error = ng_int8_parse(&ng_parse_int8_type,
960 s, off, start, buf + i, buflen)) != 0)
973 ng_ipaddr_unparse(const struct ng_parse_type *type,
974 const u_char *data, int *off, char *cbuf, int cbuflen)
979 bcopy(data + *off, &ip, sizeof(ip));
980 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
981 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
982 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
989 ng_ipaddr_getDefault(const struct ng_parse_type *type,
990 const u_char *const start, u_char *buf, int *buflen)
992 struct in_addr ip = { 0 };
994 if (*buflen < sizeof(ip))
996 bcopy(&ip, buf, sizeof(ip));
997 *buflen = sizeof(ip);
1001 const struct ng_parse_type ng_parse_ipaddr_type = {
1007 ng_ipaddr_getDefault,
1011 /************************************************************************
1012 ETHERNET ADDRESS TYPE
1013 ************************************************************************/
1016 ng_enaddr_parse(const struct ng_parse_type *type,
1017 const char *s, int *const off, const u_char *const start,
1018 u_char *const buf, int *const buflen)
1024 if (*buflen < ETHER_ADDR_LEN)
1026 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1027 val = strtoul(s + *off, &eptr, 16);
1028 if (val > 0xff || eptr == s + *off)
1030 buf[i] = (u_char)val;
1032 if (i < ETHER_ADDR_LEN - 1) {
1038 *buflen = ETHER_ADDR_LEN;
1043 ng_enaddr_unparse(const struct ng_parse_type *type,
1044 const u_char *data, int *off, char *cbuf, int cbuflen)
1048 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1049 data[*off], data[*off + 1], data[*off + 2],
1050 data[*off + 3], data[*off + 4], data[*off + 5]);
1053 *off += ETHER_ADDR_LEN;
1057 const struct ng_parse_type ng_parse_enaddr_type = {
1067 /************************************************************************
1069 ************************************************************************/
1071 /* Get the length of a byte array */
1073 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1074 const u_char *start, const u_char *buf)
1076 ng_parse_array_getLength_t *const getLength = type->private;
1078 return (*getLength)(type, start, buf);
1081 /* Byte array element type is hex int8 */
1082 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1083 &ng_parse_hint8_type,
1084 &ng_parse_bytearray_subtype_getLength,
1087 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1088 &ng_parse_array_type,
1089 &ng_parse_bytearray_subtype_info
1093 ng_bytearray_parse(const struct ng_parse_type *type,
1094 const char *s, int *off, const u_char *const start,
1095 u_char *const buf, int *buflen)
1101 /* We accept either an array of bytes or a string constant */
1102 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1103 ng_parse_array_getLength_t *const getLength = type->info;
1106 arraylen = (*getLength)(type, start, buf);
1107 if (arraylen > *buflen) {
1108 free(str, M_NETGRAPH_PARSE);
1111 if (slen > arraylen) {
1112 free(str, M_NETGRAPH_PARSE);
1115 bcopy(str, buf, slen);
1116 bzero(buf + slen, arraylen - slen);
1117 free(str, M_NETGRAPH_PARSE);
1122 struct ng_parse_type subtype;
1124 subtype = ng_parse_bytearray_subtype;
1125 subtype.private = __DECONST(void *, type->info);
1126 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1131 ng_bytearray_unparse(const struct ng_parse_type *type,
1132 const u_char *data, int *off, char *cbuf, int cbuflen)
1134 struct ng_parse_type subtype;
1136 subtype = ng_parse_bytearray_subtype;
1137 subtype.private = __DECONST(void *, type->info);
1138 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1142 ng_bytearray_getDefault(const struct ng_parse_type *type,
1143 const u_char *const start, u_char *buf, int *buflen)
1145 struct ng_parse_type subtype;
1147 subtype = ng_parse_bytearray_subtype;
1148 subtype.private = __DECONST(void *, type->info);
1149 return ng_array_getDefault(&subtype, start, buf, buflen);
1152 const struct ng_parse_type ng_parse_bytearray_type = {
1157 ng_bytearray_unparse,
1158 ng_bytearray_getDefault,
1162 /************************************************************************
1164 ************************************************************************/
1166 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1168 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1169 const u_char *start, const u_char *buf)
1171 const struct ng_mesg *msg;
1173 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1174 return msg->header.arglen;
1177 /* Type for the variable length data portion of a struct ng_mesg */
1178 static const struct ng_parse_type ng_msg_data_type = {
1179 &ng_parse_bytearray_type,
1180 &ng_parse_ng_mesg_getLength
1183 /* Type for the entire struct ng_mesg header with data section */
1184 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1185 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1186 const struct ng_parse_type ng_parse_ng_mesg_type = {
1187 &ng_parse_struct_type,
1188 &ng_parse_ng_mesg_type_fields,
1191 /************************************************************************
1192 COMPOSITE HELPER ROUTINES
1193 ************************************************************************/
1196 * Convert a structure or array from ASCII to binary
1199 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1200 int *off, const u_char *const start, u_char *const buf, int *buflen,
1201 const enum comptype ctype)
1203 const int num = ng_get_composite_len(type, start, buf, ctype);
1204 int nextIndex = 0; /* next implicit array index */
1205 u_int index; /* field or element index */
1206 int *foff; /* field value offsets in string */
1207 int align, len, blen, error = 0;
1210 foff = malloc(num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1216 /* Get opening brace/bracket */
1217 if (ng_parse_get_token(s, off, &len)
1218 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1224 /* Get individual element value positions in the string */
1226 enum ng_parse_token tok;
1228 /* Check for closing brace/bracket */
1229 tok = ng_parse_get_token(s, off, &len);
1230 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1235 /* For arrays, the 'name' (ie, index) is optional, so
1236 distinguish name from values by seeing if the next
1237 token is an equals sign */
1238 if (ctype != CT_STRUCT) {
1243 /* If an opening brace/bracket, index is implied */
1244 if (tok == T_LBRACE || tok == T_LBRACKET) {
1245 index = nextIndex++;
1249 /* Might be an index, might be a value, either way... */
1250 if (tok != T_WORD) {
1255 /* If no equals sign follows, index is implied */
1257 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1258 index = nextIndex++;
1262 /* Index was specified explicitly; parse it */
1263 ul = strtoul(s + *off, &eptr, 0);
1264 if (ul == ULONG_MAX || eptr - (s + *off) != len) {
1269 nextIndex = index + 1;
1271 } else { /* a structure field */
1272 const struct ng_parse_struct_field *const
1273 fields = type->info;
1275 /* Find the field by name (required) in field list */
1276 if (tok != T_WORD) {
1280 for (index = 0; index < num; index++) {
1281 const struct ng_parse_struct_field *const
1282 field = &fields[index];
1284 if (strncmp(&s[*off], field->name, len) == 0
1285 && field->name[len] == '\0')
1294 /* Get equals sign */
1295 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1303 /* Check array index */
1309 /* Save value's position and skip over it for now */
1310 if (foff[index] != 0) {
1311 error = EALREADY; /* duplicate */
1314 while (isspace(s[*off]))
1317 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1322 /* Now build binary structure from supplied values and defaults */
1323 for (blen = index = 0; index < num; index++) {
1324 const struct ng_parse_type *const
1325 etype = ng_get_composite_etype(type, index, ctype);
1328 /* Zero-pad any alignment bytes */
1329 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1330 for (k = 0; k < pad; k++) {
1331 if (blen >= *buflen) {
1339 vlen = *buflen - blen;
1340 if (foff[index] == 0) { /* use default value */
1341 error = ng_get_composite_elem_default(type, index,
1342 start, buf + blen, &vlen, ctype);
1343 } else { /* parse given value */
1345 error = INVOKE(etype, parse)(etype,
1346 s, off, start, buf + blen, &vlen);
1353 /* Make total composite structure size a multiple of its alignment */
1354 if ((align = ALIGNMENT(type)) != 0) {
1355 while (blen % align != 0) {
1356 if (blen >= *buflen) {
1368 free(foff, M_NETGRAPH_PARSE);
1373 * Convert an array or structure from binary to ASCII
1376 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1377 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1379 const struct ng_mesg *const hdr
1380 = (const struct ng_mesg *)(data - sizeof(*hdr));
1381 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1382 const int workSize = 20 * 1024; /* XXX hard coded constant */
1383 int nextIndex = 0, didOne = 0;
1387 /* Get workspace for checking default values */
1388 workBuf = malloc(workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1389 if (workBuf == NULL)
1392 /* Opening brace/bracket */
1393 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1394 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1398 for (index = 0; index < num; index++) {
1399 const struct ng_parse_type *const
1400 etype = ng_get_composite_etype(type, index, ctype);
1402 /* Skip any alignment pad bytes */
1403 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1406 * See if element is equal to its default value; skip if so.
1407 * Copy struct ng_mesg header for types that peek into it.
1409 if (sizeof(*hdr) + *off < workSize) {
1410 int tempsize = workSize - sizeof(*hdr) - *off;
1412 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1413 if (ng_get_composite_elem_default(type, index, workBuf
1414 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1415 &tempsize, ctype) == 0
1416 && bcmp(workBuf + sizeof(*hdr) + *off,
1417 data + *off, tempsize) == 0) {
1424 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1426 if (ctype != CT_STRUCT) {
1427 if (index != nextIndex) {
1429 if ((error = ng_parse_append(&cbuf,
1430 &cbuflen, "%d=", index)) != 0)
1435 const struct ng_parse_struct_field *const
1436 fields = type->info;
1438 if ((error = ng_parse_append(&cbuf,
1439 &cbuflen, "%s=", fields[index].name)) != 0)
1444 if ((error = INVOKE(etype, unparse)
1445 (etype, data, off, cbuf, cbuflen)) != 0) {
1446 free(workBuf, M_NETGRAPH_PARSE);
1449 cbuflen -= strlen(cbuf);
1450 cbuf += strlen(cbuf);
1454 /* Closing brace/bracket */
1455 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1456 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1459 /* Clean up after failure */
1460 free(workBuf, M_NETGRAPH_PARSE);
1465 * Generate the default value for an element of an array or structure
1466 * Returns EOPNOTSUPP if default value is unspecified.
1469 ng_get_composite_elem_default(const struct ng_parse_type *type,
1470 int index, const u_char *const start, u_char *buf, int *buflen,
1471 const enum comptype ctype)
1473 const struct ng_parse_type *etype;
1474 ng_getDefault_t *func;
1481 const struct ng_parse_array_info *const ai = type->info;
1483 if (ai->getDefault != NULL) {
1484 return (*ai->getDefault)(type,
1485 index, start, buf, buflen);
1491 const struct ng_parse_fixedarray_info *const fi = type->info;
1493 if (*fi->getDefault != NULL) {
1494 return (*fi->getDefault)(type,
1495 index, start, buf, buflen);
1500 panic("%s", __func__);
1503 /* Default to element type default */
1504 etype = ng_get_composite_etype(type, index, ctype);
1505 func = METHOD(etype, getDefault);
1507 return (EOPNOTSUPP);
1508 return (*func)(etype, start, buf, buflen);
1512 * Get the number of elements in a struct, variable or fixed array.
1515 ng_get_composite_len(const struct ng_parse_type *type,
1516 const u_char *const start, const u_char *buf,
1517 const enum comptype ctype)
1522 const struct ng_parse_struct_field *const fields = type->info;
1525 for (numFields = 0; ; numFields++) {
1526 const struct ng_parse_struct_field *const
1527 fi = &fields[numFields];
1529 if (fi->name == NULL)
1536 const struct ng_parse_array_info *const ai = type->info;
1538 return (*ai->getLength)(type, start, buf);
1542 const struct ng_parse_fixedarray_info *const fi = type->info;
1547 panic("%s", __func__);
1553 * Return the type of the index'th element of a composite structure
1555 static const struct ng_parse_type *
1556 ng_get_composite_etype(const struct ng_parse_type *type,
1557 int index, const enum comptype ctype)
1559 const struct ng_parse_type *etype = NULL;
1564 const struct ng_parse_struct_field *const fields = type->info;
1566 etype = fields[index].type;
1571 const struct ng_parse_array_info *const ai = type->info;
1573 etype = ai->elementType;
1578 const struct ng_parse_fixedarray_info *const fi = type->info;
1580 etype = fi->elementType;
1584 panic("%s", __func__);
1590 * Get the number of bytes to skip to align for the next
1591 * element in a composite structure.
1594 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1595 int index, enum comptype ctype, int posn)
1597 const struct ng_parse_type *const
1598 etype = ng_get_composite_etype(type, index, ctype);
1601 /* Get element's alignment, and possibly override */
1602 align = ALIGNMENT(etype);
1603 if (ctype == CT_STRUCT) {
1604 const struct ng_parse_struct_field *const fields = type->info;
1606 if (fields[index].alignment != 0)
1607 align = fields[index].alignment;
1610 /* Return number of bytes to skip to align */
1611 return (align ? (align - (posn % align)) % align : 0);
1614 /************************************************************************
1615 PARSING HELPER ROUTINES
1616 ************************************************************************/
1619 * Append to a fixed length string buffer.
1622 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1627 va_start(args, fmt);
1628 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1630 if (len >= *cbuflenp)
1642 ng_parse_skip_value(const char *s, int off0, int *lenp)
1644 int len, nbracket, nbrace;
1647 len = nbracket = nbrace = 0;
1649 switch (ng_parse_get_token(s, &off, &len)) {
1657 if (nbracket-- == 0)
1670 } while (nbracket > 0 || nbrace > 0);
1676 * Find the next token in the string, starting at offset *startp.
1677 * Returns the token type, with *startp pointing to the first char
1678 * and *lenp the length.
1681 ng_parse_get_token(const char *s, int *startp, int *lenp)
1686 while (isspace(s[*startp]))
1688 switch (s[*startp]) {
1708 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1710 free(t, M_NETGRAPH_PARSE);
1713 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1714 && s[i] != '{' && s[i] != '}' && s[i] != '['
1715 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1717 *lenp = i - *startp;
1723 * Get a string token, which must be enclosed in double quotes.
1724 * The normal C backslash escapes are recognized.
1727 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1733 while (isspace(s[*startp]))
1736 if (s[*startp] != '"')
1738 cbuf = malloc(strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1741 strcpy(cbuf, s + start + 1);
1742 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1749 } else if (p[0] == '\\' && p[1] != '\0') {
1780 case '0': case '1': case '2': case '3':
1781 case '4': case '5': case '6': case '7':
1783 k < 3 && *v >= '0' && *v <= '7'; v++) {
1784 x = (x << 3) + (*v - '0');
1790 for (v++, x = k = 0;
1791 k < 2 && isxdigit(*v); v++) {
1792 x = (x << 4) + (isdigit(*v) ?
1794 (tolower(*v) - 'a' + 10));
1805 free(cbuf, M_NETGRAPH_PARSE);
1806 return (NULL); /* no closing quote */
1810 * Encode a string so it can be safely put in double quotes.
1811 * Caller must free the result. Exactly "slen" characters
1815 ng_encode_string(const char *raw, int slen)
1821 cbuf = malloc(strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1825 for (i = 0; i < slen; i++, raw++) {
1853 if (*raw < 0x20 || *raw > 0x7e) {
1854 off += sprintf(cbuf + off,
1855 "\\x%02x", (u_char)*raw);
1867 /************************************************************************
1868 VIRTUAL METHOD LOOKUP
1869 ************************************************************************/
1872 ng_get_parse_method(const struct ng_parse_type *t)
1874 while (t != NULL && t->parse == NULL)
1876 return (t ? t->parse : NULL);
1879 static ng_unparse_t *
1880 ng_get_unparse_method(const struct ng_parse_type *t)
1882 while (t != NULL && t->unparse == NULL)
1884 return (t ? t->unparse : NULL);
1887 static ng_getDefault_t *
1888 ng_get_getDefault_method(const struct ng_parse_type *t)
1890 while (t != NULL && t->getDefault == NULL)
1892 return (t ? t->getDefault : NULL);
1895 static ng_getAlign_t *
1896 ng_get_getAlign_method(const struct ng_parse_type *t)
1898 while (t != NULL && t->getAlign == NULL)
1900 return (t ? t->getAlign : NULL);