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 $
41 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.30 2007/06/23 00:02:20 mjacob Exp $
42 * $DragonFly: src/sys/netgraph7/ng_parse.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/limits.h>
51 #include <sys/malloc.h>
53 #include <sys/ctype.h>
55 #include <machine/stdarg.h>
57 #include <net/ethernet.h>
59 #include <netinet/in.h>
61 #include "ng_message.h"
65 #ifdef NG_SEPARATE_MALLOC
66 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
68 #define M_NETGRAPH_PARSE M_NETGRAPH
71 /* Compute alignment for primitive integral types */
87 #define INT8_ALIGNMENT 1
88 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
89 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
90 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
92 /* Output format for integral types */
93 #define INT_UNSIGNED 0
97 /* Type of composite object: struct, array, or fixedarray */
104 /* Composite types helper functions */
105 static int ng_parse_composite(const struct ng_parse_type *type,
106 const char *s, int *off, const u_char *start,
107 u_char *const buf, int *buflen, enum comptype ctype);
108 static int ng_unparse_composite(const struct ng_parse_type *type,
109 const u_char *data, int *off, char *cbuf, int cbuflen,
110 enum comptype ctype);
111 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
112 int index, const u_char *start, u_char *buf,
113 int *buflen, enum comptype ctype);
114 static int ng_get_composite_len(const struct ng_parse_type *type,
115 const u_char *start, const u_char *buf,
116 enum comptype ctype);
117 static const struct ng_parse_type *ng_get_composite_etype(const struct
118 ng_parse_type *type, int index, enum comptype ctype);
119 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
120 int index, enum comptype ctype, int posn);
122 /* Parsing helper functions */
123 static int ng_parse_skip_value(const char *s, int off, int *lenp);
124 static int ng_parse_append(char **cbufp, int *cbuflenp,
125 const char *fmt, ...);
127 /* Poor man's virtual method calls */
128 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
129 #define INVOKE(t,m) (*METHOD(t,m))
131 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
132 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
133 static ng_getDefault_t *ng_get_getDefault_method(const
134 struct ng_parse_type *t);
135 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
137 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
138 0 : INVOKE(t, getAlign)(t))
140 /************************************************************************
142 ************************************************************************/
145 * Convert an ASCII string to binary according to the supplied type descriptor
148 ng_parse(const struct ng_parse_type *type,
149 const char *string, int *off, u_char *buf, int *buflen)
151 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
155 * Convert binary to an ASCII string according to the supplied type descriptor
158 ng_unparse(const struct ng_parse_type *type,
159 const u_char *data, char *cbuf, int cbuflen)
163 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
167 * Fill in the default value according to the supplied type descriptor
170 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
172 ng_getDefault_t *const func = METHOD(type, getDefault);
176 return (*func)(type, buf, buf, buflen);
180 /************************************************************************
182 ************************************************************************/
185 ng_struct_parse(const struct ng_parse_type *type,
186 const char *s, int *off, const u_char *const start,
187 u_char *const buf, int *buflen)
189 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
193 ng_struct_unparse(const struct ng_parse_type *type,
194 const u_char *data, int *off, char *cbuf, int cbuflen)
196 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
200 ng_struct_getDefault(const struct ng_parse_type *type,
201 const u_char *const start, u_char *buf, int *buflen)
205 return ng_parse_composite(type,
206 "{}", &off, start, buf, buflen, CT_STRUCT);
210 ng_struct_getAlign(const struct ng_parse_type *type)
212 const struct ng_parse_struct_field *field;
215 for (field = type->info; field->name != NULL; field++) {
216 int falign = ALIGNMENT(field->type);
224 const struct ng_parse_type ng_parse_struct_type = {
230 ng_struct_getDefault,
234 /************************************************************************
235 FIXED LENGTH ARRAY TYPE
236 ************************************************************************/
239 ng_fixedarray_parse(const struct ng_parse_type *type,
240 const char *s, int *off, const u_char *const start,
241 u_char *const buf, int *buflen)
243 return ng_parse_composite(type,
244 s, off, start, buf, buflen, CT_FIXEDARRAY);
248 ng_fixedarray_unparse(const struct ng_parse_type *type,
249 const u_char *data, int *off, char *cbuf, int cbuflen)
251 return ng_unparse_composite(type,
252 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
256 ng_fixedarray_getDefault(const struct ng_parse_type *type,
257 const u_char *const start, u_char *buf, int *buflen)
261 return ng_parse_composite(type,
262 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
266 ng_fixedarray_getAlign(const struct ng_parse_type *type)
268 const struct ng_parse_fixedarray_info *fi = type->info;
270 return ALIGNMENT(fi->elementType);
273 const struct ng_parse_type ng_parse_fixedarray_type = {
278 ng_fixedarray_unparse,
279 ng_fixedarray_getDefault,
280 ng_fixedarray_getAlign
283 /************************************************************************
284 VARIABLE LENGTH ARRAY TYPE
285 ************************************************************************/
288 ng_array_parse(const struct ng_parse_type *type,
289 const char *s, int *off, const u_char *const start,
290 u_char *const buf, int *buflen)
292 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
296 ng_array_unparse(const struct ng_parse_type *type,
297 const u_char *data, int *off, char *cbuf, int cbuflen)
299 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
303 ng_array_getDefault(const struct ng_parse_type *type,
304 const u_char *const start, u_char *buf, int *buflen)
308 return ng_parse_composite(type,
309 "[]", &off, start, buf, buflen, CT_ARRAY);
313 ng_array_getAlign(const struct ng_parse_type *type)
315 const struct ng_parse_array_info *ai = type->info;
317 return ALIGNMENT(ai->elementType);
320 const struct ng_parse_type ng_parse_array_type = {
330 /************************************************************************
332 ************************************************************************/
335 ng_int8_parse(const struct ng_parse_type *type,
336 const char *s, int *off, const u_char *const start,
337 u_char *const buf, int *buflen)
343 val = strtol(s + *off, &eptr, 0);
344 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
348 bcopy(&val8, buf, sizeof(int8_t));
349 *buflen = sizeof(int8_t);
354 ng_int8_unparse(const struct ng_parse_type *type,
355 const u_char *data, int *off, char *cbuf, int cbuflen)
362 bcopy(data + *off, &val, sizeof(int8_t));
363 switch ((intptr_t)type->info) {
370 fval = (u_int8_t)val;
374 fval = (u_int8_t)val;
377 panic("%s: unknown type", __func__);
378 #ifdef RESTARTABLE_PANICS
382 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
384 *off += sizeof(int8_t);
389 ng_int8_getDefault(const struct ng_parse_type *type,
390 const u_char *const start, u_char *buf, int *buflen)
394 if (*buflen < sizeof(int8_t))
397 bcopy(&val, buf, sizeof(int8_t));
398 *buflen = sizeof(int8_t);
403 ng_int8_getAlign(const struct ng_parse_type *type)
405 return INT8_ALIGNMENT;
408 const struct ng_parse_type ng_parse_int8_type = {
418 const struct ng_parse_type ng_parse_uint8_type = {
423 const struct ng_parse_type ng_parse_hint8_type = {
428 /************************************************************************
430 ************************************************************************/
433 ng_int16_parse(const struct ng_parse_type *type,
434 const char *s, int *off, const u_char *const start,
435 u_char *const buf, int *buflen)
441 val = strtol(s + *off, &eptr, 0);
442 if (val < (int16_t)0x8000
443 || val > (u_int16_t)0xffff || eptr == s + *off)
446 val16 = (int16_t)val;
447 bcopy(&val16, buf, sizeof(int16_t));
448 *buflen = sizeof(int16_t);
453 ng_int16_unparse(const struct ng_parse_type *type,
454 const u_char *data, int *off, char *cbuf, int cbuflen)
461 bcopy(data + *off, &val, sizeof(int16_t));
462 switch ((intptr_t)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 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
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 if ((intptr_t)type->info == INT_SIGNED)
541 val = strtol(s + *off, &eptr, 0);
543 val = strtoul(s + *off, &eptr, 0);
544 if (val < (int32_t)0x80000000
545 || val > (u_int32_t)0xffffffff || eptr == s + *off)
548 val32 = (int32_t)val;
549 bcopy(&val32, buf, sizeof(int32_t));
550 *buflen = sizeof(int32_t);
555 ng_int32_unparse(const struct ng_parse_type *type,
556 const u_char *data, int *off, char *cbuf, int cbuflen)
563 bcopy(data + *off, &val, sizeof(int32_t));
564 switch ((intptr_t)type->info) {
571 fval = (u_int32_t)val;
575 fval = (u_int32_t)val;
578 panic("%s: unknown type", __func__);
579 #ifdef RESTARTABLE_PANICS
583 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
585 *off += sizeof(int32_t);
590 ng_int32_getDefault(const struct ng_parse_type *type,
591 const u_char *const start, u_char *buf, int *buflen)
595 if (*buflen < sizeof(int32_t))
598 bcopy(&val, buf, sizeof(int32_t));
599 *buflen = sizeof(int32_t);
604 ng_int32_getAlign(const struct ng_parse_type *type)
606 return INT32_ALIGNMENT;
609 const struct ng_parse_type ng_parse_int32_type = {
619 const struct ng_parse_type ng_parse_uint32_type = {
620 &ng_parse_int32_type,
624 const struct ng_parse_type ng_parse_hint32_type = {
625 &ng_parse_int32_type,
629 /************************************************************************
631 ************************************************************************/
634 ng_int64_parse(const struct ng_parse_type *type,
635 const char *s, int *off, const u_char *const start,
636 u_char *const buf, int *buflen)
642 val = strtoq(s + *off, &eptr, 0);
643 if (eptr == s + *off)
646 val64 = (int64_t)val;
647 bcopy(&val64, buf, sizeof(int64_t));
648 *buflen = sizeof(int64_t);
653 ng_int64_unparse(const struct ng_parse_type *type,
654 const u_char *data, int *off, char *cbuf, int cbuflen)
661 bcopy(data + *off, &val, sizeof(int64_t));
662 switch ((intptr_t)type->info) {
669 fval = (u_int64_t)val;
673 fval = (u_int64_t)val;
676 panic("%s: unknown type", __func__);
677 #ifdef RESTARTABLE_PANICS
681 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
683 *off += sizeof(int64_t);
688 ng_int64_getDefault(const struct ng_parse_type *type,
689 const u_char *const start, u_char *buf, int *buflen)
693 if (*buflen < sizeof(int64_t))
696 bcopy(&val, buf, sizeof(int64_t));
697 *buflen = sizeof(int64_t);
702 ng_int64_getAlign(const struct ng_parse_type *type)
704 return INT64_ALIGNMENT;
707 const struct ng_parse_type ng_parse_int64_type = {
717 const struct ng_parse_type ng_parse_uint64_type = {
718 &ng_parse_int64_type,
722 const struct ng_parse_type ng_parse_hint64_type = {
723 &ng_parse_int64_type,
727 /************************************************************************
729 ************************************************************************/
732 ng_string_parse(const struct ng_parse_type *type,
733 const char *s, int *off, const u_char *const start,
734 u_char *const buf, int *buflen)
740 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
743 bcopy(sval, buf, slen + 1);
744 FREE(sval, M_NETGRAPH_PARSE);
750 ng_string_unparse(const struct ng_parse_type *type,
751 const u_char *data, int *off, char *cbuf, int cbuflen)
753 const char *const raw = (const char *)data + *off;
754 char *const s = ng_encode_string(raw, strlen(raw));
759 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
760 FREE(s, M_NETGRAPH_PARSE);
763 *off += strlen(raw) + 1;
764 FREE(s, M_NETGRAPH_PARSE);
769 ng_string_getDefault(const struct ng_parse_type *type,
770 const u_char *const start, u_char *buf, int *buflen)
775 buf[0] = (u_char)'\0';
780 const struct ng_parse_type ng_parse_string_type = {
786 ng_string_getDefault,
790 /************************************************************************
791 FIXED BUFFER STRING TYPE
792 ************************************************************************/
795 ng_fixedstring_parse(const struct ng_parse_type *type,
796 const char *s, int *off, const u_char *const start,
797 u_char *const buf, int *buflen)
799 const struct ng_parse_fixedstring_info *const fi = type->info;
804 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
806 if (slen + 1 > fi->bufSize) {
807 FREE(sval, M_NETGRAPH_PARSE);
811 bcopy(sval, buf, slen);
812 FREE(sval, M_NETGRAPH_PARSE);
813 bzero(buf + slen, fi->bufSize - slen);
814 *buflen = fi->bufSize;
819 ng_fixedstring_unparse(const struct ng_parse_type *type,
820 const u_char *data, int *off, char *cbuf, int cbuflen)
822 const struct ng_parse_fixedstring_info *const fi = type->info;
823 int error, temp = *off;
825 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
832 ng_fixedstring_getDefault(const struct ng_parse_type *type,
833 const u_char *const start, u_char *buf, int *buflen)
835 const struct ng_parse_fixedstring_info *const fi = type->info;
837 if (*buflen < fi->bufSize)
839 bzero(buf, fi->bufSize);
840 *buflen = fi->bufSize;
844 const struct ng_parse_type ng_parse_fixedstring_type = {
848 ng_fixedstring_parse,
849 ng_fixedstring_unparse,
850 ng_fixedstring_getDefault,
854 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
857 const struct ng_parse_type ng_parse_nodebuf_type = {
858 &ng_parse_fixedstring_type,
859 &ng_parse_nodebuf_info
862 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
865 const struct ng_parse_type ng_parse_hookbuf_type = {
866 &ng_parse_fixedstring_type,
867 &ng_parse_hookbuf_info
870 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
873 const struct ng_parse_type ng_parse_pathbuf_type = {
874 &ng_parse_fixedstring_type,
875 &ng_parse_pathbuf_info
878 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
881 const struct ng_parse_type ng_parse_typebuf_type = {
882 &ng_parse_fixedstring_type,
883 &ng_parse_typebuf_info
886 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
889 const struct ng_parse_type ng_parse_cmdbuf_type = {
890 &ng_parse_fixedstring_type,
891 &ng_parse_cmdbuf_info
894 /************************************************************************
895 EXPLICITLY SIZED STRING TYPE
896 ************************************************************************/
899 ng_sizedstring_parse(const struct ng_parse_type *type,
900 const char *s, int *off, const u_char *const start,
901 u_char *const buf, int *buflen)
907 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
909 if (slen > USHRT_MAX) {
910 FREE(sval, M_NETGRAPH_PARSE);
914 *((u_int16_t *)buf) = (u_int16_t)slen;
915 bcopy(sval, buf + 2, slen);
916 FREE(sval, M_NETGRAPH_PARSE);
922 ng_sizedstring_unparse(const struct ng_parse_type *type,
923 const u_char *data, int *off, char *cbuf, int cbuflen)
925 const char *const raw = (const char *)data + *off + 2;
926 const int slen = *((const u_int16_t *)(data + *off));
927 char *const s = ng_encode_string(raw, slen);
932 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
933 FREE(s, M_NETGRAPH_PARSE);
936 FREE(s, M_NETGRAPH_PARSE);
942 ng_sizedstring_getDefault(const struct ng_parse_type *type,
943 const u_char *const start, u_char *buf, int *buflen)
952 const struct ng_parse_type ng_parse_sizedstring_type = {
956 ng_sizedstring_parse,
957 ng_sizedstring_unparse,
958 ng_sizedstring_getDefault,
962 /************************************************************************
964 ************************************************************************/
967 ng_ipaddr_parse(const struct ng_parse_type *type,
968 const char *s, int *off, const u_char *const start,
969 u_char *const buf, int *buflen)
973 for (i = 0; i < 4; i++) {
974 if ((error = ng_int8_parse(&ng_parse_int8_type,
975 s, off, start, buf + i, buflen)) != 0)
977 if (i < 3 && s[*off] != '.')
986 ng_ipaddr_unparse(const struct ng_parse_type *type,
987 const u_char *data, int *off, char *cbuf, int cbuflen)
992 bcopy(data + *off, &ip, sizeof(ip));
993 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
994 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
995 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
1002 ng_ipaddr_getDefault(const struct ng_parse_type *type,
1003 const u_char *const start, u_char *buf, int *buflen)
1005 struct in_addr ip = { 0 };
1007 if (*buflen < sizeof(ip))
1009 bcopy(&ip, buf, sizeof(ip));
1010 *buflen = sizeof(ip);
1014 const struct ng_parse_type ng_parse_ipaddr_type = {
1020 ng_ipaddr_getDefault,
1024 /************************************************************************
1025 ETHERNET ADDRESS TYPE
1026 ************************************************************************/
1029 ng_enaddr_parse(const struct ng_parse_type *type,
1030 const char *s, int *const off, const u_char *const start,
1031 u_char *const buf, int *const buflen)
1037 if (*buflen < ETHER_ADDR_LEN)
1039 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1040 val = strtoul(s + *off, &eptr, 16);
1041 if (val > 0xff || eptr == s + *off)
1043 buf[i] = (u_char)val;
1045 if (i < ETHER_ADDR_LEN - 1) {
1051 *buflen = ETHER_ADDR_LEN;
1056 ng_enaddr_unparse(const struct ng_parse_type *type,
1057 const u_char *data, int *off, char *cbuf, int cbuflen)
1061 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1062 data[*off], data[*off + 1], data[*off + 2],
1063 data[*off + 3], data[*off + 4], data[*off + 5]);
1066 *off += ETHER_ADDR_LEN;
1070 const struct ng_parse_type ng_parse_enaddr_type = {
1080 /************************************************************************
1082 ************************************************************************/
1084 /* Get the length of a byte array */
1086 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1087 const u_char *start, const u_char *buf)
1089 ng_parse_array_getLength_t *const getLength = type->private;
1091 return (*getLength)(type, start, buf);
1094 /* Byte array element type is hex int8 */
1095 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1096 &ng_parse_hint8_type,
1097 &ng_parse_bytearray_subtype_getLength,
1100 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1101 &ng_parse_array_type,
1102 &ng_parse_bytearray_subtype_info
1106 ng_bytearray_parse(const struct ng_parse_type *type,
1107 const char *s, int *off, const u_char *const start,
1108 u_char *const buf, int *buflen)
1114 /* We accept either an array of bytes or a string constant */
1115 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1116 ng_parse_array_getLength_t *const getLength = type->info;
1119 arraylen = (*getLength)(type, start, buf);
1120 if (arraylen > *buflen) {
1121 FREE(str, M_NETGRAPH_PARSE);
1124 if (slen > arraylen) {
1125 FREE(str, M_NETGRAPH_PARSE);
1128 bcopy(str, buf, slen);
1129 bzero(buf + slen, arraylen - slen);
1130 FREE(str, M_NETGRAPH_PARSE);
1135 struct ng_parse_type subtype;
1137 subtype = ng_parse_bytearray_subtype;
1138 *(const void **)&subtype.private = type->info;
1139 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1144 ng_bytearray_unparse(const struct ng_parse_type *type,
1145 const u_char *data, int *off, char *cbuf, int cbuflen)
1147 struct ng_parse_type subtype;
1149 subtype = ng_parse_bytearray_subtype;
1150 *(const void **)&subtype.private = type->info;
1151 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1155 ng_bytearray_getDefault(const struct ng_parse_type *type,
1156 const u_char *const start, u_char *buf, int *buflen)
1158 struct ng_parse_type subtype;
1160 subtype = ng_parse_bytearray_subtype;
1161 *(const void **)&subtype.private = type->info;
1162 return ng_array_getDefault(&subtype, start, buf, buflen);
1165 const struct ng_parse_type ng_parse_bytearray_type = {
1170 ng_bytearray_unparse,
1171 ng_bytearray_getDefault,
1175 /************************************************************************
1177 ************************************************************************/
1179 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1181 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1182 const u_char *start, const u_char *buf)
1184 const struct ng_mesg *msg;
1186 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1187 return msg->header.arglen;
1190 /* Type for the variable length data portion of a struct ng_mesg */
1191 static const struct ng_parse_type ng_msg_data_type = {
1192 &ng_parse_bytearray_type,
1193 &ng_parse_ng_mesg_getLength
1196 /* Type for the entire struct ng_mesg header with data section */
1197 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1198 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1199 const struct ng_parse_type ng_parse_ng_mesg_type = {
1200 &ng_parse_struct_type,
1201 &ng_parse_ng_mesg_type_fields,
1204 /************************************************************************
1205 COMPOSITE HELPER ROUTINES
1206 ************************************************************************/
1209 * Convert a structure or array from ASCII to binary
1212 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1213 int *off, const u_char *const start, u_char *const buf, int *buflen,
1214 const enum comptype ctype)
1216 const int num = ng_get_composite_len(type, start, buf, ctype);
1217 int nextIndex = 0; /* next implicit array index */
1218 u_int index; /* field or element index */
1219 int *foff; /* field value offsets in string */
1220 int align, len, blen, error = 0;
1223 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK | M_ZERO);
1229 /* Get opening brace/bracket */
1230 if (ng_parse_get_token(s, off, &len)
1231 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1237 /* Get individual element value positions in the string */
1239 enum ng_parse_token tok;
1241 /* Check for closing brace/bracket */
1242 tok = ng_parse_get_token(s, off, &len);
1243 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1248 /* For arrays, the 'name' (ie, index) is optional, so
1249 distinguish name from values by seeing if the next
1250 token is an equals sign */
1251 if (ctype != CT_STRUCT) {
1255 /* If an opening brace/bracket, index is implied */
1256 if (tok == T_LBRACE || tok == T_LBRACKET) {
1257 index = nextIndex++;
1261 /* Might be an index, might be a value, either way... */
1262 if (tok != T_WORD) {
1267 /* If no equals sign follows, index is implied */
1269 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1270 index = nextIndex++;
1274 /* Index was specified explicitly; parse it */
1275 index = (u_int)strtoul(s + *off, &eptr, 0);
1276 if (index < 0 || eptr - (s + *off) != len) {
1280 nextIndex = index + 1;
1282 } else { /* a structure field */
1283 const struct ng_parse_struct_field *const
1284 fields = type->info;
1286 /* Find the field by name (required) in field list */
1287 if (tok != T_WORD) {
1291 for (index = 0; index < num; index++) {
1292 const struct ng_parse_struct_field *const
1293 field = &fields[index];
1295 if (strncmp(&s[*off], field->name, len) == 0
1296 && field->name[len] == '\0')
1305 /* Get equals sign */
1306 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1314 /* Check array index */
1320 /* Save value's position and skip over it for now */
1321 if (foff[index] != 0) {
1322 error = EALREADY; /* duplicate */
1325 while (isspace(s[*off]))
1328 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1333 /* Now build binary structure from supplied values and defaults */
1334 for (blen = index = 0; index < num; index++) {
1335 const struct ng_parse_type *const
1336 etype = ng_get_composite_etype(type, index, ctype);
1339 /* Zero-pad any alignment bytes */
1340 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1341 for (k = 0; k < pad; k++) {
1342 if (blen >= *buflen) {
1350 vlen = *buflen - blen;
1351 if (foff[index] == 0) { /* use default value */
1352 error = ng_get_composite_elem_default(type, index,
1353 start, buf + blen, &vlen, ctype);
1354 } else { /* parse given value */
1356 error = INVOKE(etype, parse)(etype,
1357 s, off, start, buf + blen, &vlen);
1364 /* Make total composite structure size a multiple of its alignment */
1365 if ((align = ALIGNMENT(type)) != 0) {
1366 while (blen % align != 0) {
1367 if (blen >= *buflen) {
1379 FREE(foff, M_NETGRAPH_PARSE);
1384 * Convert an array or structure from binary to ASCII
1387 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1388 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1390 const struct ng_mesg *const hdr
1391 = (const struct ng_mesg *)(data - sizeof(*hdr));
1392 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1393 const int workSize = 20 * 1024; /* XXX hard coded constant */
1394 int nextIndex = 0, didOne = 0;
1398 /* Get workspace for checking default values */
1399 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1400 if (workBuf == NULL)
1403 /* Opening brace/bracket */
1404 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1405 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1409 for (index = 0; index < num; index++) {
1410 const struct ng_parse_type *const
1411 etype = ng_get_composite_etype(type, index, ctype);
1413 /* Skip any alignment pad bytes */
1414 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1417 * See if element is equal to its default value; skip if so.
1418 * Copy struct ng_mesg header for types that peek into it.
1420 if (sizeof(*hdr) + *off < workSize) {
1421 int tempsize = workSize - sizeof(*hdr) - *off;
1423 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1424 if (ng_get_composite_elem_default(type, index, workBuf
1425 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1426 &tempsize, ctype) == 0
1427 && bcmp(workBuf + sizeof(*hdr) + *off,
1428 data + *off, tempsize) == 0) {
1435 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1437 if (ctype != CT_STRUCT) {
1438 if (index != nextIndex) {
1440 if ((error = ng_parse_append(&cbuf,
1441 &cbuflen, "%d=", index)) != 0)
1446 const struct ng_parse_struct_field *const
1447 fields = type->info;
1449 if ((error = ng_parse_append(&cbuf,
1450 &cbuflen, "%s=", fields[index].name)) != 0)
1455 if ((error = INVOKE(etype, unparse)
1456 (etype, data, off, cbuf, cbuflen)) != 0) {
1457 FREE(workBuf, M_NETGRAPH_PARSE);
1460 cbuflen -= strlen(cbuf);
1461 cbuf += strlen(cbuf);
1465 /* Closing brace/bracket */
1466 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1467 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1470 /* Clean up after failure */
1471 FREE(workBuf, M_NETGRAPH_PARSE);
1476 * Generate the default value for an element of an array or structure
1477 * Returns EOPNOTSUPP if default value is unspecified.
1480 ng_get_composite_elem_default(const struct ng_parse_type *type,
1481 int index, const u_char *const start, u_char *buf, int *buflen,
1482 const enum comptype ctype)
1484 const struct ng_parse_type *etype;
1485 ng_getDefault_t *func;
1492 const struct ng_parse_array_info *const ai = type->info;
1494 if (ai->getDefault != NULL) {
1495 return (*ai->getDefault)(type,
1496 index, start, buf, buflen);
1502 const struct ng_parse_fixedarray_info *const fi = type->info;
1504 if (*fi->getDefault != NULL) {
1505 return (*fi->getDefault)(type,
1506 index, start, buf, buflen);
1511 panic("%s", __func__);
1514 /* Default to element type default */
1515 etype = ng_get_composite_etype(type, index, ctype);
1516 func = METHOD(etype, getDefault);
1518 return (EOPNOTSUPP);
1519 return (*func)(etype, start, buf, buflen);
1523 * Get the number of elements in a struct, variable or fixed array.
1526 ng_get_composite_len(const struct ng_parse_type *type,
1527 const u_char *const start, const u_char *buf,
1528 const enum comptype ctype)
1533 const struct ng_parse_struct_field *const fields = type->info;
1536 for (numFields = 0; ; numFields++) {
1537 const struct ng_parse_struct_field *const
1538 fi = &fields[numFields];
1540 if (fi->name == NULL)
1547 const struct ng_parse_array_info *const ai = type->info;
1549 return (*ai->getLength)(type, start, buf);
1553 const struct ng_parse_fixedarray_info *const fi = type->info;
1558 panic("%s", __func__);
1564 * Return the type of the index'th element of a composite structure
1566 static const struct ng_parse_type *
1567 ng_get_composite_etype(const struct ng_parse_type *type,
1568 int index, const enum comptype ctype)
1570 const struct ng_parse_type *etype = NULL;
1575 const struct ng_parse_struct_field *const fields = type->info;
1577 etype = fields[index].type;
1582 const struct ng_parse_array_info *const ai = type->info;
1584 etype = ai->elementType;
1589 const struct ng_parse_fixedarray_info *const fi = type->info;
1591 etype = fi->elementType;
1595 panic("%s", __func__);
1601 * Get the number of bytes to skip to align for the next
1602 * element in a composite structure.
1605 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1606 int index, enum comptype ctype, int posn)
1608 const struct ng_parse_type *const
1609 etype = ng_get_composite_etype(type, index, ctype);
1612 /* Get element's alignment, and possibly override */
1613 align = ALIGNMENT(etype);
1614 if (ctype == CT_STRUCT) {
1615 const struct ng_parse_struct_field *const fields = type->info;
1617 if (fields[index].alignment != 0)
1618 align = fields[index].alignment;
1621 /* Return number of bytes to skip to align */
1622 return (align ? (align - (posn % align)) % align : 0);
1625 /************************************************************************
1626 PARSING HELPER ROUTINES
1627 ************************************************************************/
1630 * Append to a fixed length string buffer.
1633 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1638 va_start(args, fmt);
1639 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1641 if (len >= *cbuflenp)
1653 ng_parse_skip_value(const char *s, int off0, int *lenp)
1655 int len, nbracket, nbrace;
1658 len = nbracket = nbrace = 0;
1660 switch (ng_parse_get_token(s, &off, &len)) {
1668 if (nbracket-- == 0)
1681 } while (nbracket > 0 || nbrace > 0);
1687 * Find the next token in the string, starting at offset *startp.
1688 * Returns the token type, with *startp pointing to the first char
1689 * and *lenp the length.
1692 ng_parse_get_token(const char *s, int *startp, int *lenp)
1697 while (isspace(s[*startp]))
1699 switch (s[*startp]) {
1719 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1721 FREE(t, M_NETGRAPH_PARSE);
1724 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1725 && s[i] != '{' && s[i] != '}' && s[i] != '['
1726 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1728 *lenp = i - *startp;
1734 * Get a string token, which must be enclosed in double quotes.
1735 * The normal C backslash escapes are recognized.
1738 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1744 while (isspace(s[*startp]))
1747 if (s[*startp] != '"')
1749 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1752 strcpy(cbuf, s + start + 1);
1753 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1760 } else if (p[0] == '\\' && p[1] != '\0') {
1791 case '0': case '1': case '2': case '3':
1792 case '4': case '5': case '6': case '7':
1794 k < 3 && *v >= '0' && *v <= '7'; v++) {
1795 x = (x << 3) + (*v - '0');
1801 for (v++, x = k = 0;
1802 k < 2 && isxdigit(*v); v++) {
1803 x = (x << 4) + (isdigit(*v) ?
1805 (tolower(*v) - 'a' + 10));
1816 FREE(cbuf, M_NETGRAPH_PARSE);
1817 return (NULL); /* no closing quote */
1821 * Encode a string so it can be safely put in double quotes.
1822 * Caller must free the result. Exactly "slen" characters
1826 ng_encode_string(const char *raw, int slen)
1832 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1836 for (i = 0; i < slen; i++, raw++) {
1864 if (*raw < 0x20 || *raw > 0x7e) {
1865 off += sprintf(cbuf + off,
1866 "\\x%02x", (u_char)*raw);
1878 /************************************************************************
1879 VIRTUAL METHOD LOOKUP
1880 ************************************************************************/
1883 ng_get_parse_method(const struct ng_parse_type *t)
1885 while (t != NULL && t->parse == NULL)
1887 return (t ? t->parse : NULL);
1890 static ng_unparse_t *
1891 ng_get_unparse_method(const struct ng_parse_type *t)
1893 while (t != NULL && t->unparse == NULL)
1895 return (t ? t->unparse : NULL);
1898 static ng_getDefault_t *
1899 ng_get_getDefault_method(const struct ng_parse_type *t)
1901 while (t != NULL && t->getDefault == NULL)
1903 return (t ? t->getDefault : NULL);
1906 static ng_getAlign_t *
1907 ng_get_getAlign_method(const struct ng_parse_type *t)
1909 while (t != NULL && t->getAlign == NULL)
1911 return (t ? t->getAlign : NULL);