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 <net/ethernet.h>
53 #include <netinet/in.h>
55 #include <netgraph/ng_message.h>
56 #include <netgraph/netgraph.h>
57 #include <netgraph/ng_parse.h>
59 #ifdef NG_SEPARATE_MALLOC
60 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
62 #define M_NETGRAPH_PARSE M_NETGRAPH
65 /* Compute alignment for primitive integral types */
81 #define INT8_ALIGNMENT 1
82 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
83 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
84 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
86 /* Output format for integral types */
87 #define INT_UNSIGNED 0
91 /* Type of composite object: struct, array, or fixedarray */
98 /* Composite types helper functions */
99 static int ng_parse_composite(const struct ng_parse_type *type,
100 const char *s, int *off, const u_char *start,
101 u_char *const buf, int *buflen, enum comptype ctype);
102 static int ng_unparse_composite(const struct ng_parse_type *type,
103 const u_char *data, int *off, char *cbuf, int cbuflen,
104 enum comptype ctype);
105 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
106 int index, const u_char *start, u_char *buf,
107 int *buflen, enum comptype ctype);
108 static int ng_get_composite_len(const struct ng_parse_type *type,
109 const u_char *start, const u_char *buf,
110 enum comptype ctype);
111 static const struct ng_parse_type *ng_get_composite_etype(const struct
112 ng_parse_type *type, int index, enum comptype ctype);
113 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
114 int index, enum comptype ctype, int posn);
116 /* Parsing helper functions */
117 static int ng_parse_skip_value(const char *s, int off, int *lenp);
119 /* Poor man's virtual method calls */
120 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
121 #define INVOKE(t,m) (*METHOD(t,m))
123 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
124 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
125 static ng_getDefault_t *ng_get_getDefault_method(const
126 struct ng_parse_type *t);
127 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
129 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
130 0 : INVOKE(t, getAlign)(t))
132 /* For converting binary to string */
133 #define NG_PARSE_APPEND(fmt, args...) \
137 len = snprintf((cbuf), (cbuflen), \
139 if (len >= (cbuflen)) \
145 /************************************************************************
147 ************************************************************************/
150 * Convert an ASCII string to binary according to the supplied type descriptor
153 ng_parse(const struct ng_parse_type *type,
154 const char *string, int *off, u_char *buf, int *buflen)
156 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
160 * Convert binary to an ASCII string according to the supplied type descriptor
163 ng_unparse(const struct ng_parse_type *type,
164 const u_char *data, char *cbuf, int cbuflen)
168 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
172 * Fill in the default value according to the supplied type descriptor
175 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
177 ng_getDefault_t *const func = METHOD(type, getDefault);
181 return (*func)(type, buf, buf, buflen);
185 /************************************************************************
187 ************************************************************************/
190 ng_struct_parse(const struct ng_parse_type *type,
191 const char *s, int *off, const u_char *const start,
192 u_char *const buf, int *buflen)
194 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
198 ng_struct_unparse(const struct ng_parse_type *type,
199 const u_char *data, int *off, char *cbuf, int cbuflen)
201 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
205 ng_struct_getDefault(const struct ng_parse_type *type,
206 const u_char *const start, u_char *buf, int *buflen)
210 return ng_parse_composite(type,
211 "{}", &off, start, buf, buflen, CT_STRUCT);
215 ng_struct_getAlign(const struct ng_parse_type *type)
217 const struct ng_parse_struct_field *field;
220 for (field = type->info; field->name != NULL; field++) {
221 int falign = ALIGNMENT(field->type);
229 const struct ng_parse_type ng_parse_struct_type = {
235 ng_struct_getDefault,
239 /************************************************************************
240 FIXED LENGTH ARRAY TYPE
241 ************************************************************************/
244 ng_fixedarray_parse(const struct ng_parse_type *type,
245 const char *s, int *off, const u_char *const start,
246 u_char *const buf, int *buflen)
248 return ng_parse_composite(type,
249 s, off, start, buf, buflen, CT_FIXEDARRAY);
253 ng_fixedarray_unparse(const struct ng_parse_type *type,
254 const u_char *data, int *off, char *cbuf, int cbuflen)
256 return ng_unparse_composite(type,
257 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
261 ng_fixedarray_getDefault(const struct ng_parse_type *type,
262 const u_char *const start, u_char *buf, int *buflen)
266 return ng_parse_composite(type,
267 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
271 ng_fixedarray_getAlign(const struct ng_parse_type *type)
273 const struct ng_parse_fixedarray_info *fi = type->info;
275 return ALIGNMENT(fi->elementType);
278 const struct ng_parse_type ng_parse_fixedarray_type = {
283 ng_fixedarray_unparse,
284 ng_fixedarray_getDefault,
285 ng_fixedarray_getAlign
288 /************************************************************************
289 VARIABLE LENGTH ARRAY TYPE
290 ************************************************************************/
293 ng_array_parse(const struct ng_parse_type *type,
294 const char *s, int *off, const u_char *const start,
295 u_char *const buf, int *buflen)
297 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
301 ng_array_unparse(const struct ng_parse_type *type,
302 const u_char *data, int *off, char *cbuf, int cbuflen)
304 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
308 ng_array_getDefault(const struct ng_parse_type *type,
309 const u_char *const start, u_char *buf, int *buflen)
313 return ng_parse_composite(type,
314 "[]", &off, start, buf, buflen, CT_ARRAY);
318 ng_array_getAlign(const struct ng_parse_type *type)
320 const struct ng_parse_array_info *ai = type->info;
322 return ALIGNMENT(ai->elementType);
325 const struct ng_parse_type ng_parse_array_type = {
335 /************************************************************************
337 ************************************************************************/
340 ng_int8_parse(const struct ng_parse_type *type,
341 const char *s, int *off, const u_char *const start,
342 u_char *const buf, int *buflen)
348 val = strtol(s + *off, &eptr, 0);
349 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
353 bcopy(&val8, buf, sizeof(int8_t));
354 *buflen = sizeof(int8_t);
359 ng_int8_unparse(const struct ng_parse_type *type,
360 const u_char *data, int *off, char *cbuf, int cbuflen)
366 bcopy(data + *off, &val, sizeof(int8_t));
367 switch ((intptr_t)type->info) {
374 fval = (u_int8_t)val;
378 fval = (u_int8_t)val;
381 panic("%s: unknown type", __func__);
382 #ifdef RESTARTABLE_PANICS
386 NG_PARSE_APPEND(fmt, fval);
387 *off += sizeof(int8_t);
392 ng_int8_getDefault(const struct ng_parse_type *type,
393 const u_char *const start, u_char *buf, int *buflen)
397 if (*buflen < sizeof(int8_t))
400 bcopy(&val, buf, sizeof(int8_t));
401 *buflen = sizeof(int8_t);
406 ng_int8_getAlign(const struct ng_parse_type *type)
408 return INT8_ALIGNMENT;
411 const struct ng_parse_type ng_parse_int8_type = {
421 const struct ng_parse_type ng_parse_uint8_type = {
426 const struct ng_parse_type ng_parse_hint8_type = {
431 /************************************************************************
433 ************************************************************************/
436 ng_int16_parse(const struct ng_parse_type *type,
437 const char *s, int *off, const u_char *const start,
438 u_char *const buf, int *buflen)
444 val = strtol(s + *off, &eptr, 0);
445 if (val < (int16_t)0x8000
446 || val > (u_int16_t)0xffff || eptr == s + *off)
449 val16 = (int16_t)val;
450 bcopy(&val16, buf, sizeof(int16_t));
451 *buflen = sizeof(int16_t);
456 ng_int16_unparse(const struct ng_parse_type *type,
457 const u_char *data, int *off, char *cbuf, int cbuflen)
463 bcopy(data + *off, &val, sizeof(int16_t));
464 switch ((intptr_t)type->info) {
471 fval = (u_int16_t)val;
475 fval = (u_int16_t)val;
478 panic("%s: unknown type", __func__);
479 #ifdef RESTARTABLE_PANICS
483 NG_PARSE_APPEND(fmt, fval);
484 *off += sizeof(int16_t);
489 ng_int16_getDefault(const struct ng_parse_type *type,
490 const u_char *const start, u_char *buf, int *buflen)
494 if (*buflen < sizeof(int16_t))
497 bcopy(&val, buf, sizeof(int16_t));
498 *buflen = sizeof(int16_t);
503 ng_int16_getAlign(const struct ng_parse_type *type)
505 return INT16_ALIGNMENT;
508 const struct ng_parse_type ng_parse_int16_type = {
518 const struct ng_parse_type ng_parse_uint16_type = {
519 &ng_parse_int16_type,
523 const struct ng_parse_type ng_parse_hint16_type = {
524 &ng_parse_int16_type,
528 /************************************************************************
530 ************************************************************************/
533 ng_int32_parse(const struct ng_parse_type *type,
534 const char *s, int *off, const u_char *const start,
535 u_char *const buf, int *buflen)
537 long val; /* assumes long is at least 32 bits */
541 val = strtol(s + *off, &eptr, 0);
542 if (val < (int32_t)0x80000000
543 || val > (u_int32_t)0xffffffff || eptr == s + *off)
546 val32 = (int32_t)val;
547 bcopy(&val32, buf, sizeof(int32_t));
548 *buflen = sizeof(int32_t);
553 ng_int32_unparse(const struct ng_parse_type *type,
554 const u_char *data, int *off, char *cbuf, int cbuflen)
560 bcopy(data + *off, &val, sizeof(int32_t));
561 switch ((intptr_t)type->info) {
568 fval = (u_int32_t)val;
572 fval = (u_int32_t)val;
575 panic("%s: unknown type", __func__);
576 #ifdef RESTARTABLE_PANICS
580 NG_PARSE_APPEND(fmt, fval);
581 *off += sizeof(int32_t);
586 ng_int32_getDefault(const struct ng_parse_type *type,
587 const u_char *const start, u_char *buf, int *buflen)
591 if (*buflen < sizeof(int32_t))
594 bcopy(&val, buf, sizeof(int32_t));
595 *buflen = sizeof(int32_t);
600 ng_int32_getAlign(const struct ng_parse_type *type)
602 return INT32_ALIGNMENT;
605 const struct ng_parse_type ng_parse_int32_type = {
615 const struct ng_parse_type ng_parse_uint32_type = {
616 &ng_parse_int32_type,
620 const struct ng_parse_type ng_parse_hint32_type = {
621 &ng_parse_int32_type,
625 /************************************************************************
627 ************************************************************************/
630 ng_int64_parse(const struct ng_parse_type *type,
631 const char *s, int *off, const u_char *const start,
632 u_char *const buf, int *buflen)
638 val = strtoq(s + *off, &eptr, 0);
639 if (eptr == s + *off)
642 val64 = (int64_t)val;
643 bcopy(&val64, buf, sizeof(int64_t));
644 *buflen = sizeof(int64_t);
649 ng_int64_unparse(const struct ng_parse_type *type,
650 const u_char *data, int *off, char *cbuf, int cbuflen)
656 bcopy(data + *off, &val, sizeof(int64_t));
657 switch ((intptr_t)type->info) {
664 fval = (u_int64_t)val;
668 fval = (u_int64_t)val;
671 panic("%s: unknown type", __func__);
672 #ifdef RESTARTABLE_PANICS
676 NG_PARSE_APPEND(fmt, fval);
677 *off += sizeof(int64_t);
682 ng_int64_getDefault(const struct ng_parse_type *type,
683 const u_char *const start, u_char *buf, int *buflen)
687 if (*buflen < sizeof(int64_t))
690 bcopy(&val, buf, sizeof(int64_t));
691 *buflen = sizeof(int64_t);
696 ng_int64_getAlign(const struct ng_parse_type *type)
698 return INT64_ALIGNMENT;
701 const struct ng_parse_type ng_parse_int64_type = {
711 const struct ng_parse_type ng_parse_uint64_type = {
712 &ng_parse_int64_type,
716 const struct ng_parse_type ng_parse_hint64_type = {
717 &ng_parse_int64_type,
721 /************************************************************************
723 ************************************************************************/
726 ng_string_parse(const struct ng_parse_type *type,
727 const char *s, int *off, const u_char *const start,
728 u_char *const buf, int *buflen)
734 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
737 bcopy(sval, buf, slen + 1);
738 FREE(sval, M_NETGRAPH_PARSE);
744 ng_string_unparse(const struct ng_parse_type *type,
745 const u_char *data, int *off, char *cbuf, int cbuflen)
747 const char *const raw = (const char *)data + *off;
748 char *const s = ng_encode_string(raw, strlen(raw));
752 NG_PARSE_APPEND("%s", s);
753 *off += strlen(raw) + 1;
754 FREE(s, M_NETGRAPH_PARSE);
759 ng_string_getDefault(const struct ng_parse_type *type,
760 const u_char *const start, u_char *buf, int *buflen)
765 buf[0] = (u_char)'\0';
770 const struct ng_parse_type ng_parse_string_type = {
776 ng_string_getDefault,
780 /************************************************************************
781 FIXED BUFFER STRING TYPE
782 ************************************************************************/
785 ng_fixedstring_parse(const struct ng_parse_type *type,
786 const char *s, int *off, const u_char *const start,
787 u_char *const buf, int *buflen)
789 const struct ng_parse_fixedstring_info *const fi = type->info;
794 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
796 if (slen + 1 > fi->bufSize)
799 bcopy(sval, buf, slen);
800 FREE(sval, M_NETGRAPH_PARSE);
801 bzero(buf + slen, fi->bufSize - slen);
802 *buflen = fi->bufSize;
807 ng_fixedstring_unparse(const struct ng_parse_type *type,
808 const u_char *data, int *off, char *cbuf, int cbuflen)
810 const struct ng_parse_fixedstring_info *const fi = type->info;
811 int error, temp = *off;
813 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
820 ng_fixedstring_getDefault(const struct ng_parse_type *type,
821 const u_char *const start, u_char *buf, int *buflen)
823 const struct ng_parse_fixedstring_info *const fi = type->info;
825 if (*buflen < fi->bufSize)
827 bzero(buf, fi->bufSize);
828 *buflen = fi->bufSize;
832 const struct ng_parse_type ng_parse_fixedstring_type = {
836 ng_fixedstring_parse,
837 ng_fixedstring_unparse,
838 ng_fixedstring_getDefault,
842 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
845 const struct ng_parse_type ng_parse_nodebuf_type = {
846 &ng_parse_fixedstring_type,
847 &ng_parse_nodebuf_info
850 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
853 const struct ng_parse_type ng_parse_hookbuf_type = {
854 &ng_parse_fixedstring_type,
855 &ng_parse_hookbuf_info
858 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
861 const struct ng_parse_type ng_parse_pathbuf_type = {
862 &ng_parse_fixedstring_type,
863 &ng_parse_pathbuf_info
866 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
869 const struct ng_parse_type ng_parse_typebuf_type = {
870 &ng_parse_fixedstring_type,
871 &ng_parse_typebuf_info
874 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
877 const struct ng_parse_type ng_parse_cmdbuf_type = {
878 &ng_parse_fixedstring_type,
879 &ng_parse_cmdbuf_info
882 /************************************************************************
883 EXPLICITLY SIZED STRING TYPE
884 ************************************************************************/
887 ng_sizedstring_parse(const struct ng_parse_type *type,
888 const char *s, int *off, const u_char *const start,
889 u_char *const buf, int *buflen)
895 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
900 *((u_int16_t *)buf) = (u_int16_t)slen;
901 bcopy(sval, buf + 2, slen);
902 FREE(sval, M_NETGRAPH_PARSE);
908 ng_sizedstring_unparse(const struct ng_parse_type *type,
909 const u_char *data, int *off, char *cbuf, int cbuflen)
911 const char *const raw = (const char *)data + *off + 2;
912 const int slen = *((const u_int16_t *)(data + *off));
913 char *const s = ng_encode_string(raw, slen);
917 NG_PARSE_APPEND("%s", s);
918 FREE(s, M_NETGRAPH_PARSE);
924 ng_sizedstring_getDefault(const struct ng_parse_type *type,
925 const u_char *const start, u_char *buf, int *buflen)
934 const struct ng_parse_type ng_parse_sizedstring_type = {
938 ng_sizedstring_parse,
939 ng_sizedstring_unparse,
940 ng_sizedstring_getDefault,
944 /************************************************************************
946 ************************************************************************/
949 ng_ipaddr_parse(const struct ng_parse_type *type,
950 const char *s, int *off, const u_char *const start,
951 u_char *const buf, int *buflen)
955 for (i = 0; i < 4; i++) {
956 if ((error = ng_int8_parse(&ng_parse_int8_type,
957 s, off, start, buf + i, buflen)) != 0)
959 if (i < 3 && s[*off] != '.')
968 ng_ipaddr_unparse(const struct ng_parse_type *type,
969 const u_char *data, int *off, char *cbuf, int cbuflen)
973 bcopy(data + *off, &ip, sizeof(ip));
974 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
975 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
981 ng_ipaddr_getDefault(const struct ng_parse_type *type,
982 const u_char *const start, u_char *buf, int *buflen)
984 struct in_addr ip = { 0 };
986 if (*buflen < sizeof(ip))
988 bcopy(&ip, buf, sizeof(ip));
989 *buflen = sizeof(ip);
993 const struct ng_parse_type ng_parse_ipaddr_type = {
999 ng_ipaddr_getDefault,
1003 /************************************************************************
1004 ETHERNET ADDRESS TYPE
1005 ************************************************************************/
1008 ng_enaddr_parse(const struct ng_parse_type *type,
1009 const char *s, int *const off, const u_char *const start,
1010 u_char *const buf, int *const buflen)
1016 if (*buflen < ETHER_ADDR_LEN)
1018 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1019 val = strtoul(s + *off, &eptr, 16);
1020 if (val > 0xff || eptr == s + *off)
1022 buf[i] = (u_char)val;
1024 if (i < ETHER_ADDR_LEN - 1) {
1030 *buflen = ETHER_ADDR_LEN;
1035 ng_enaddr_unparse(const struct ng_parse_type *type,
1036 const u_char *data, int *off, char *cbuf, int cbuflen)
1040 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1041 data[*off], data[*off + 1], data[*off + 2],
1042 data[*off + 3], data[*off + 4], data[*off + 5]);
1045 *off += ETHER_ADDR_LEN;
1049 const struct ng_parse_type ng_parse_enaddr_type = {
1059 /************************************************************************
1061 ************************************************************************/
1063 /* Get the length of a byte array */
1065 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1066 const u_char *start, const u_char *buf)
1068 ng_parse_array_getLength_t *const getLength = type->private;
1070 return (*getLength)(type, start, buf);
1073 /* Byte array element type is hex int8 */
1074 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1075 &ng_parse_hint8_type,
1076 &ng_parse_bytearray_subtype_getLength,
1079 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1080 &ng_parse_array_type,
1081 &ng_parse_bytearray_subtype_info
1085 ng_bytearray_parse(const struct ng_parse_type *type,
1086 const char *s, int *off, const u_char *const start,
1087 u_char *const buf, int *buflen)
1093 /* We accept either an array of bytes or a string constant */
1094 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1095 ng_parse_array_getLength_t *const getLength = type->info;
1098 arraylen = (*getLength)(type, start, buf);
1099 if (arraylen > *buflen) {
1100 FREE(str, M_NETGRAPH_PARSE);
1103 if (slen > arraylen) {
1104 FREE(str, M_NETGRAPH_PARSE);
1107 bcopy(str, buf, slen);
1108 bzero(buf + slen, arraylen - slen);
1109 FREE(str, M_NETGRAPH_PARSE);
1114 struct ng_parse_type subtype;
1116 subtype = ng_parse_bytearray_subtype;
1117 (const void *)subtype.private = type->info;
1118 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1123 ng_bytearray_unparse(const struct ng_parse_type *type,
1124 const u_char *data, int *off, char *cbuf, int cbuflen)
1126 struct ng_parse_type subtype;
1128 subtype = ng_parse_bytearray_subtype;
1129 (const void *)subtype.private = type->info;
1130 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1134 ng_bytearray_getDefault(const struct ng_parse_type *type,
1135 const u_char *const start, u_char *buf, int *buflen)
1137 struct ng_parse_type subtype;
1139 subtype = ng_parse_bytearray_subtype;
1140 (const void *)subtype.private = type->info;
1141 return ng_array_getDefault(&subtype, start, buf, buflen);
1144 const struct ng_parse_type ng_parse_bytearray_type = {
1149 ng_bytearray_unparse,
1150 ng_bytearray_getDefault,
1154 /************************************************************************
1156 ************************************************************************/
1158 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1160 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1161 const u_char *start, const u_char *buf)
1163 const struct ng_mesg *msg;
1165 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1166 return msg->header.arglen;
1169 /* Type for the variable length data portion of a struct ng_mesg */
1170 static const struct ng_parse_type ng_msg_data_type = {
1171 &ng_parse_bytearray_type,
1172 &ng_parse_ng_mesg_getLength
1175 /* Type for the entire struct ng_mesg header with data section */
1176 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1177 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1178 const struct ng_parse_type ng_parse_ng_mesg_type = {
1179 &ng_parse_struct_type,
1180 &ng_parse_ng_mesg_type_fields,
1183 /************************************************************************
1184 COMPOSITE HELPER ROUTINES
1185 ************************************************************************/
1188 * Convert a structure or array from ASCII to binary
1191 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1192 int *off, const u_char *const start, u_char *const buf, int *buflen,
1193 const enum comptype ctype)
1195 const int num = ng_get_composite_len(type, start, buf, ctype);
1196 int nextIndex = 0; /* next implicit array index */
1197 u_int index; /* field or element index */
1198 int *foff; /* field value offsets in string */
1199 int align, len, blen, error = 0;
1202 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1208 /* Get opening brace/bracket */
1209 if (ng_parse_get_token(s, off, &len)
1210 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1216 /* Get individual element value positions in the string */
1218 enum ng_parse_token tok;
1220 /* Check for closing brace/bracket */
1221 tok = ng_parse_get_token(s, off, &len);
1222 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1227 /* For arrays, the 'name' (ie, index) is optional, so
1228 distinguish name from values by seeing if the next
1229 token is an equals sign */
1230 if (ctype != CT_STRUCT) {
1234 /* If an opening brace/bracket, index is implied */
1235 if (tok == T_LBRACE || tok == T_LBRACKET) {
1236 index = nextIndex++;
1240 /* Might be an index, might be a value, either way... */
1241 if (tok != T_WORD) {
1246 /* If no equals sign follows, index is implied */
1248 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1249 index = nextIndex++;
1253 /* Index was specified explicitly; parse it */
1254 index = (u_int)strtoul(s + *off, &eptr, 0);
1255 if (index < 0 || eptr - (s + *off) != len) {
1259 nextIndex = index + 1;
1261 } else { /* a structure field */
1262 const struct ng_parse_struct_field *const
1263 fields = type->info;
1265 /* Find the field by name (required) in field list */
1266 if (tok != T_WORD) {
1270 for (index = 0; index < num; index++) {
1271 const struct ng_parse_struct_field *const
1272 field = &fields[index];
1274 if (strncmp(&s[*off], field->name, len) == 0
1275 && field->name[len] == '\0')
1284 /* Get equals sign */
1285 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1293 /* Check array index */
1299 /* Save value's position and skip over it for now */
1300 if (foff[index] != 0) {
1301 error = EALREADY; /* duplicate */
1304 while (isspace(s[*off]))
1307 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1312 /* Now build binary structure from supplied values and defaults */
1313 for (blen = index = 0; index < num; index++) {
1314 const struct ng_parse_type *const
1315 etype = ng_get_composite_etype(type, index, ctype);
1318 /* Zero-pad any alignment bytes */
1319 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1320 for (k = 0; k < pad; k++) {
1321 if (blen >= *buflen) {
1329 vlen = *buflen - blen;
1330 if (foff[index] == 0) { /* use default value */
1331 error = ng_get_composite_elem_default(type, index,
1332 start, buf + blen, &vlen, ctype);
1333 } else { /* parse given value */
1335 error = INVOKE(etype, parse)(etype,
1336 s, off, start, buf + blen, &vlen);
1343 /* Make total composite structure size a multiple of its alignment */
1344 if ((align = ALIGNMENT(type)) != 0) {
1345 while (blen % align != 0) {
1346 if (blen >= *buflen) {
1358 FREE(foff, M_NETGRAPH_PARSE);
1363 * Convert an array or structure from binary to ASCII
1366 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1367 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1369 const struct ng_mesg *const hdr
1370 = (const struct ng_mesg *)(data - sizeof(*hdr));
1371 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1372 const int workSize = 20 * 1024; /* XXX hard coded constant */
1373 int nextIndex = 0, didOne = 0;
1377 /* Get workspace for checking default values */
1378 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1379 if (workBuf == NULL)
1382 /* Opening brace/bracket */
1383 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1386 for (index = 0; index < num; index++) {
1387 const struct ng_parse_type *const
1388 etype = ng_get_composite_etype(type, index, ctype);
1390 /* Skip any alignment pad bytes */
1391 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1394 * See if element is equal to its default value; skip if so.
1395 * Copy struct ng_mesg header for types that peek into it.
1397 if (sizeof(*hdr) + *off < workSize) {
1398 int tempsize = workSize - sizeof(*hdr) - *off;
1400 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1401 if (ng_get_composite_elem_default(type, index, workBuf
1402 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1403 &tempsize, ctype) == 0
1404 && bcmp(workBuf + sizeof(*hdr) + *off,
1405 data + *off, tempsize) == 0) {
1412 NG_PARSE_APPEND(" ");
1413 if (ctype != CT_STRUCT) {
1414 if (index != nextIndex) {
1416 NG_PARSE_APPEND("%d=", index);
1420 const struct ng_parse_struct_field *const
1421 fields = type->info;
1423 NG_PARSE_APPEND("%s=", fields[index].name);
1427 if ((error = INVOKE(etype, unparse)
1428 (etype, data, off, cbuf, cbuflen)) != 0) {
1429 FREE(workBuf, M_NETGRAPH_PARSE);
1432 cbuflen -= strlen(cbuf);
1433 cbuf += strlen(cbuf);
1436 FREE(workBuf, M_NETGRAPH_PARSE);
1438 /* Closing brace/bracket */
1439 NG_PARSE_APPEND("%s%c",
1440 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1445 * Generate the default value for an element of an array or structure
1446 * Returns EOPNOTSUPP if default value is unspecified.
1449 ng_get_composite_elem_default(const struct ng_parse_type *type,
1450 int index, const u_char *const start, u_char *buf, int *buflen,
1451 const enum comptype ctype)
1453 const struct ng_parse_type *etype;
1454 ng_getDefault_t *func;
1461 const struct ng_parse_array_info *const ai = type->info;
1463 if (ai->getDefault != NULL) {
1464 return (*ai->getDefault)(type,
1465 index, start, buf, buflen);
1471 const struct ng_parse_fixedarray_info *const fi = type->info;
1473 if (*fi->getDefault != NULL) {
1474 return (*fi->getDefault)(type,
1475 index, start, buf, buflen);
1480 panic("%s", __func__);
1483 /* Default to element type default */
1484 etype = ng_get_composite_etype(type, index, ctype);
1485 func = METHOD(etype, getDefault);
1487 return (EOPNOTSUPP);
1488 return (*func)(etype, start, buf, buflen);
1492 * Get the number of elements in a struct, variable or fixed array.
1495 ng_get_composite_len(const struct ng_parse_type *type,
1496 const u_char *const start, const u_char *buf,
1497 const enum comptype ctype)
1502 const struct ng_parse_struct_field *const fields = type->info;
1505 for (numFields = 0; ; numFields++) {
1506 const struct ng_parse_struct_field *const
1507 fi = &fields[numFields];
1509 if (fi->name == NULL)
1516 const struct ng_parse_array_info *const ai = type->info;
1518 return (*ai->getLength)(type, start, buf);
1522 const struct ng_parse_fixedarray_info *const fi = type->info;
1527 panic("%s", __func__);
1533 * Return the type of the index'th element of a composite structure
1535 static const struct ng_parse_type *
1536 ng_get_composite_etype(const struct ng_parse_type *type,
1537 int index, const enum comptype ctype)
1539 const struct ng_parse_type *etype = NULL;
1544 const struct ng_parse_struct_field *const fields = type->info;
1546 etype = fields[index].type;
1551 const struct ng_parse_array_info *const ai = type->info;
1553 etype = ai->elementType;
1558 const struct ng_parse_fixedarray_info *const fi = type->info;
1560 etype = fi->elementType;
1564 panic("%s", __func__);
1570 * Get the number of bytes to skip to align for the next
1571 * element in a composite structure.
1574 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1575 int index, enum comptype ctype, int posn)
1577 const struct ng_parse_type *const
1578 etype = ng_get_composite_etype(type, index, ctype);
1581 /* Get element's alignment, and possibly override */
1582 align = ALIGNMENT(etype);
1583 if (ctype == CT_STRUCT) {
1584 const struct ng_parse_struct_field *const fields = type->info;
1586 if (fields[index].alignment != 0)
1587 align = fields[index].alignment;
1590 /* Return number of bytes to skip to align */
1591 return (align ? (align - (posn % align)) % align : 0);
1594 /************************************************************************
1595 PARSING HELPER ROUTINES
1596 ************************************************************************/
1602 ng_parse_skip_value(const char *s, int off0, int *lenp)
1604 int len, nbracket, nbrace;
1607 len = nbracket = nbrace = 0;
1609 switch (ng_parse_get_token(s, &off, &len)) {
1617 if (nbracket-- == 0)
1630 } while (nbracket > 0 || nbrace > 0);
1636 * Find the next token in the string, starting at offset *startp.
1637 * Returns the token type, with *startp pointing to the first char
1638 * and *lenp the length.
1641 ng_parse_get_token(const char *s, int *startp, int *lenp)
1646 while (isspace(s[*startp]))
1648 switch (s[*startp]) {
1668 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1670 FREE(t, M_NETGRAPH_PARSE);
1673 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1674 && s[i] != '{' && s[i] != '}' && s[i] != '['
1675 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1677 *lenp = i - *startp;
1683 * Get a string token, which must be enclosed in double quotes.
1684 * The normal C backslash escapes are recognized.
1687 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1693 while (isspace(s[*startp]))
1696 if (s[*startp] != '"')
1698 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1701 strcpy(cbuf, s + start + 1);
1702 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1709 } else if (p[0] == '\\' && p[1] != '\0') {
1740 case '0': case '1': case '2': case '3':
1741 case '4': case '5': case '6': case '7':
1743 k < 3 && *v >= '0' && *v <= '7'; v++) {
1744 x = (x << 3) + (*v - '0');
1750 for (v++, x = k = 0;
1751 k < 2 && isxdigit(*v); v++) {
1752 x = (x << 4) + (isdigit(*v) ?
1754 (tolower(*v) - 'a' + 10));
1765 return (NULL); /* no closing quote */
1769 * Encode a string so it can be safely put in double quotes.
1770 * Caller must free the result. Exactly "slen" characters
1774 ng_encode_string(const char *raw, int slen)
1780 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1784 for (i = 0; i < slen; i++, raw++) {
1812 if (*raw < 0x20 || *raw > 0x7e) {
1813 off += sprintf(cbuf + off,
1814 "\\x%02x", (u_char)*raw);
1826 /************************************************************************
1827 VIRTUAL METHOD LOOKUP
1828 ************************************************************************/
1831 ng_get_parse_method(const struct ng_parse_type *t)
1833 while (t != NULL && t->parse == NULL)
1835 return (t ? t->parse : NULL);
1838 static ng_unparse_t *
1839 ng_get_unparse_method(const struct ng_parse_type *t)
1841 while (t != NULL && t->unparse == NULL)
1843 return (t ? t->unparse : NULL);
1846 static ng_getDefault_t *
1847 ng_get_getDefault_method(const struct ng_parse_type *t)
1849 while (t != NULL && t->getDefault == NULL)
1851 return (t ? t->getDefault : NULL);
1854 static ng_getAlign_t *
1855 ng_get_getAlign_method(const struct ng_parse_type *t)
1857 while (t != NULL && t->getAlign == NULL)
1859 return (t ? t->getAlign : NULL);