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>
50 #include <sys/ctype.h>
52 #include <net/ethernet.h>
54 #include <netinet/in.h>
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/ng_parse.h>
60 #ifdef NG_SEPARATE_MALLOC
61 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
63 #define M_NETGRAPH_PARSE M_NETGRAPH
66 /* Compute alignment for primitive integral types */
82 #define INT8_ALIGNMENT 1
83 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
84 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
85 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
87 /* Output format for integral types */
88 #define INT_UNSIGNED 0
92 /* Type of composite object: struct, array, or fixedarray */
99 /* Composite types helper functions */
100 static int ng_parse_composite(const struct ng_parse_type *type,
101 const char *s, int *off, const u_char *start,
102 u_char *const buf, int *buflen, enum comptype ctype);
103 static int ng_unparse_composite(const struct ng_parse_type *type,
104 const u_char *data, int *off, char *cbuf, int cbuflen,
105 enum comptype ctype);
106 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
107 int index, const u_char *start, u_char *buf,
108 int *buflen, enum comptype ctype);
109 static int ng_get_composite_len(const struct ng_parse_type *type,
110 const u_char *start, const u_char *buf,
111 enum comptype ctype);
112 static const struct ng_parse_type *ng_get_composite_etype(const struct
113 ng_parse_type *type, int index, enum comptype ctype);
114 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
115 int index, enum comptype ctype, int posn);
117 /* Parsing helper functions */
118 static int ng_parse_skip_value(const char *s, int off, int *lenp);
120 /* Poor man's virtual method calls */
121 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
122 #define INVOKE(t,m) (*METHOD(t,m))
124 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
125 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
126 static ng_getDefault_t *ng_get_getDefault_method(const
127 struct ng_parse_type *t);
128 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
130 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
131 0 : INVOKE(t, getAlign)(t))
133 /* For converting binary to string */
134 #define NG_PARSE_APPEND(fmt, args...) \
138 len = snprintf((cbuf), (cbuflen), \
140 if (len >= (cbuflen)) \
146 /************************************************************************
148 ************************************************************************/
151 * Convert an ASCII string to binary according to the supplied type descriptor
154 ng_parse(const struct ng_parse_type *type,
155 const char *string, int *off, u_char *buf, int *buflen)
157 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
161 * Convert binary to an ASCII string according to the supplied type descriptor
164 ng_unparse(const struct ng_parse_type *type,
165 const u_char *data, char *cbuf, int cbuflen)
169 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
173 * Fill in the default value according to the supplied type descriptor
176 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
178 ng_getDefault_t *const func = METHOD(type, getDefault);
182 return (*func)(type, buf, buf, buflen);
186 /************************************************************************
188 ************************************************************************/
191 ng_struct_parse(const struct ng_parse_type *type,
192 const char *s, int *off, const u_char *const start,
193 u_char *const buf, int *buflen)
195 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
199 ng_struct_unparse(const struct ng_parse_type *type,
200 const u_char *data, int *off, char *cbuf, int cbuflen)
202 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
206 ng_struct_getDefault(const struct ng_parse_type *type,
207 const u_char *const start, u_char *buf, int *buflen)
211 return ng_parse_composite(type,
212 "{}", &off, start, buf, buflen, CT_STRUCT);
216 ng_struct_getAlign(const struct ng_parse_type *type)
218 const struct ng_parse_struct_field *field;
221 for (field = type->info; field->name != NULL; field++) {
222 int falign = ALIGNMENT(field->type);
230 const struct ng_parse_type ng_parse_struct_type = {
236 ng_struct_getDefault,
240 /************************************************************************
241 FIXED LENGTH ARRAY TYPE
242 ************************************************************************/
245 ng_fixedarray_parse(const struct ng_parse_type *type,
246 const char *s, int *off, const u_char *const start,
247 u_char *const buf, int *buflen)
249 return ng_parse_composite(type,
250 s, off, start, buf, buflen, CT_FIXEDARRAY);
254 ng_fixedarray_unparse(const struct ng_parse_type *type,
255 const u_char *data, int *off, char *cbuf, int cbuflen)
257 return ng_unparse_composite(type,
258 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
262 ng_fixedarray_getDefault(const struct ng_parse_type *type,
263 const u_char *const start, u_char *buf, int *buflen)
267 return ng_parse_composite(type,
268 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
272 ng_fixedarray_getAlign(const struct ng_parse_type *type)
274 const struct ng_parse_fixedarray_info *fi = type->info;
276 return ALIGNMENT(fi->elementType);
279 const struct ng_parse_type ng_parse_fixedarray_type = {
284 ng_fixedarray_unparse,
285 ng_fixedarray_getDefault,
286 ng_fixedarray_getAlign
289 /************************************************************************
290 VARIABLE LENGTH ARRAY TYPE
291 ************************************************************************/
294 ng_array_parse(const struct ng_parse_type *type,
295 const char *s, int *off, const u_char *const start,
296 u_char *const buf, int *buflen)
298 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
302 ng_array_unparse(const struct ng_parse_type *type,
303 const u_char *data, int *off, char *cbuf, int cbuflen)
305 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
309 ng_array_getDefault(const struct ng_parse_type *type,
310 const u_char *const start, u_char *buf, int *buflen)
314 return ng_parse_composite(type,
315 "[]", &off, start, buf, buflen, CT_ARRAY);
319 ng_array_getAlign(const struct ng_parse_type *type)
321 const struct ng_parse_array_info *ai = type->info;
323 return ALIGNMENT(ai->elementType);
326 const struct ng_parse_type ng_parse_array_type = {
336 /************************************************************************
338 ************************************************************************/
341 ng_int8_parse(const struct ng_parse_type *type,
342 const char *s, int *off, const u_char *const start,
343 u_char *const buf, int *buflen)
349 val = strtol(s + *off, &eptr, 0);
350 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
354 bcopy(&val8, buf, sizeof(int8_t));
355 *buflen = sizeof(int8_t);
360 ng_int8_unparse(const struct ng_parse_type *type,
361 const u_char *data, int *off, char *cbuf, int cbuflen)
367 bcopy(data + *off, &val, sizeof(int8_t));
368 switch ((intptr_t)type->info) {
375 fval = (u_int8_t)val;
379 fval = (u_int8_t)val;
382 panic("%s: unknown type", __func__);
383 #ifdef RESTARTABLE_PANICS
387 NG_PARSE_APPEND(fmt, fval);
388 *off += sizeof(int8_t);
393 ng_int8_getDefault(const struct ng_parse_type *type,
394 const u_char *const start, u_char *buf, int *buflen)
398 if (*buflen < sizeof(int8_t))
401 bcopy(&val, buf, sizeof(int8_t));
402 *buflen = sizeof(int8_t);
407 ng_int8_getAlign(const struct ng_parse_type *type)
409 return INT8_ALIGNMENT;
412 const struct ng_parse_type ng_parse_int8_type = {
422 const struct ng_parse_type ng_parse_uint8_type = {
427 const struct ng_parse_type ng_parse_hint8_type = {
432 /************************************************************************
434 ************************************************************************/
437 ng_int16_parse(const struct ng_parse_type *type,
438 const char *s, int *off, const u_char *const start,
439 u_char *const buf, int *buflen)
445 val = strtol(s + *off, &eptr, 0);
446 if (val < (int16_t)0x8000
447 || val > (u_int16_t)0xffff || eptr == s + *off)
450 val16 = (int16_t)val;
451 bcopy(&val16, buf, sizeof(int16_t));
452 *buflen = sizeof(int16_t);
457 ng_int16_unparse(const struct ng_parse_type *type,
458 const u_char *data, int *off, char *cbuf, int cbuflen)
464 bcopy(data + *off, &val, sizeof(int16_t));
465 switch ((intptr_t)type->info) {
472 fval = (u_int16_t)val;
476 fval = (u_int16_t)val;
479 panic("%s: unknown type", __func__);
480 #ifdef RESTARTABLE_PANICS
484 NG_PARSE_APPEND(fmt, fval);
485 *off += sizeof(int16_t);
490 ng_int16_getDefault(const struct ng_parse_type *type,
491 const u_char *const start, u_char *buf, int *buflen)
495 if (*buflen < sizeof(int16_t))
498 bcopy(&val, buf, sizeof(int16_t));
499 *buflen = sizeof(int16_t);
504 ng_int16_getAlign(const struct ng_parse_type *type)
506 return INT16_ALIGNMENT;
509 const struct ng_parse_type ng_parse_int16_type = {
519 const struct ng_parse_type ng_parse_uint16_type = {
520 &ng_parse_int16_type,
524 const struct ng_parse_type ng_parse_hint16_type = {
525 &ng_parse_int16_type,
529 /************************************************************************
531 ************************************************************************/
534 ng_int32_parse(const struct ng_parse_type *type,
535 const char *s, int *off, const u_char *const start,
536 u_char *const buf, int *buflen)
538 long val; /* assumes long is at least 32 bits */
542 val = strtol(s + *off, &eptr, 0);
543 if (val < (int32_t)0x80000000
544 || val > (u_int32_t)0xffffffff || eptr == s + *off)
547 val32 = (int32_t)val;
548 bcopy(&val32, buf, sizeof(int32_t));
549 *buflen = sizeof(int32_t);
554 ng_int32_unparse(const struct ng_parse_type *type,
555 const u_char *data, int *off, char *cbuf, int cbuflen)
561 bcopy(data + *off, &val, sizeof(int32_t));
562 switch ((intptr_t)type->info) {
569 fval = (u_int32_t)val;
573 fval = (u_int32_t)val;
576 panic("%s: unknown type", __func__);
577 #ifdef RESTARTABLE_PANICS
581 NG_PARSE_APPEND(fmt, fval);
582 *off += sizeof(int32_t);
587 ng_int32_getDefault(const struct ng_parse_type *type,
588 const u_char *const start, u_char *buf, int *buflen)
592 if (*buflen < sizeof(int32_t))
595 bcopy(&val, buf, sizeof(int32_t));
596 *buflen = sizeof(int32_t);
601 ng_int32_getAlign(const struct ng_parse_type *type)
603 return INT32_ALIGNMENT;
606 const struct ng_parse_type ng_parse_int32_type = {
616 const struct ng_parse_type ng_parse_uint32_type = {
617 &ng_parse_int32_type,
621 const struct ng_parse_type ng_parse_hint32_type = {
622 &ng_parse_int32_type,
626 /************************************************************************
628 ************************************************************************/
631 ng_int64_parse(const struct ng_parse_type *type,
632 const char *s, int *off, const u_char *const start,
633 u_char *const buf, int *buflen)
639 val = strtoq(s + *off, &eptr, 0);
640 if (eptr == s + *off)
643 val64 = (int64_t)val;
644 bcopy(&val64, buf, sizeof(int64_t));
645 *buflen = sizeof(int64_t);
650 ng_int64_unparse(const struct ng_parse_type *type,
651 const u_char *data, int *off, char *cbuf, int cbuflen)
657 bcopy(data + *off, &val, sizeof(int64_t));
658 switch ((intptr_t)type->info) {
665 fval = (u_int64_t)val;
669 fval = (u_int64_t)val;
672 panic("%s: unknown type", __func__);
673 #ifdef RESTARTABLE_PANICS
677 NG_PARSE_APPEND(fmt, fval);
678 *off += sizeof(int64_t);
683 ng_int64_getDefault(const struct ng_parse_type *type,
684 const u_char *const start, u_char *buf, int *buflen)
688 if (*buflen < sizeof(int64_t))
691 bcopy(&val, buf, sizeof(int64_t));
692 *buflen = sizeof(int64_t);
697 ng_int64_getAlign(const struct ng_parse_type *type)
699 return INT64_ALIGNMENT;
702 const struct ng_parse_type ng_parse_int64_type = {
712 const struct ng_parse_type ng_parse_uint64_type = {
713 &ng_parse_int64_type,
717 const struct ng_parse_type ng_parse_hint64_type = {
718 &ng_parse_int64_type,
722 /************************************************************************
724 ************************************************************************/
727 ng_string_parse(const struct ng_parse_type *type,
728 const char *s, int *off, const u_char *const start,
729 u_char *const buf, int *buflen)
735 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
738 bcopy(sval, buf, slen + 1);
739 FREE(sval, M_NETGRAPH_PARSE);
745 ng_string_unparse(const struct ng_parse_type *type,
746 const u_char *data, int *off, char *cbuf, int cbuflen)
748 const char *const raw = (const char *)data + *off;
749 char *const s = ng_encode_string(raw, strlen(raw));
753 NG_PARSE_APPEND("%s", s);
754 *off += strlen(raw) + 1;
755 FREE(s, M_NETGRAPH_PARSE);
760 ng_string_getDefault(const struct ng_parse_type *type,
761 const u_char *const start, u_char *buf, int *buflen)
766 buf[0] = (u_char)'\0';
771 const struct ng_parse_type ng_parse_string_type = {
777 ng_string_getDefault,
781 /************************************************************************
782 FIXED BUFFER STRING TYPE
783 ************************************************************************/
786 ng_fixedstring_parse(const struct ng_parse_type *type,
787 const char *s, int *off, const u_char *const start,
788 u_char *const buf, int *buflen)
790 const struct ng_parse_fixedstring_info *const fi = type->info;
795 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
797 if (slen + 1 > fi->bufSize)
800 bcopy(sval, buf, slen);
801 FREE(sval, M_NETGRAPH_PARSE);
802 bzero(buf + slen, fi->bufSize - slen);
803 *buflen = fi->bufSize;
808 ng_fixedstring_unparse(const struct ng_parse_type *type,
809 const u_char *data, int *off, char *cbuf, int cbuflen)
811 const struct ng_parse_fixedstring_info *const fi = type->info;
812 int error, temp = *off;
814 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
821 ng_fixedstring_getDefault(const struct ng_parse_type *type,
822 const u_char *const start, u_char *buf, int *buflen)
824 const struct ng_parse_fixedstring_info *const fi = type->info;
826 if (*buflen < fi->bufSize)
828 bzero(buf, fi->bufSize);
829 *buflen = fi->bufSize;
833 const struct ng_parse_type ng_parse_fixedstring_type = {
837 ng_fixedstring_parse,
838 ng_fixedstring_unparse,
839 ng_fixedstring_getDefault,
843 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
846 const struct ng_parse_type ng_parse_nodebuf_type = {
847 &ng_parse_fixedstring_type,
848 &ng_parse_nodebuf_info
851 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
854 const struct ng_parse_type ng_parse_hookbuf_type = {
855 &ng_parse_fixedstring_type,
856 &ng_parse_hookbuf_info
859 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
862 const struct ng_parse_type ng_parse_pathbuf_type = {
863 &ng_parse_fixedstring_type,
864 &ng_parse_pathbuf_info
867 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
870 const struct ng_parse_type ng_parse_typebuf_type = {
871 &ng_parse_fixedstring_type,
872 &ng_parse_typebuf_info
875 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
878 const struct ng_parse_type ng_parse_cmdbuf_type = {
879 &ng_parse_fixedstring_type,
880 &ng_parse_cmdbuf_info
883 /************************************************************************
884 EXPLICITLY SIZED STRING TYPE
885 ************************************************************************/
888 ng_sizedstring_parse(const struct ng_parse_type *type,
889 const char *s, int *off, const u_char *const start,
890 u_char *const buf, int *buflen)
896 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
901 *((u_int16_t *)buf) = (u_int16_t)slen;
902 bcopy(sval, buf + 2, slen);
903 FREE(sval, M_NETGRAPH_PARSE);
909 ng_sizedstring_unparse(const struct ng_parse_type *type,
910 const u_char *data, int *off, char *cbuf, int cbuflen)
912 const char *const raw = (const char *)data + *off + 2;
913 const int slen = *((const u_int16_t *)(data + *off));
914 char *const s = ng_encode_string(raw, slen);
918 NG_PARSE_APPEND("%s", s);
919 FREE(s, M_NETGRAPH_PARSE);
925 ng_sizedstring_getDefault(const struct ng_parse_type *type,
926 const u_char *const start, u_char *buf, int *buflen)
935 const struct ng_parse_type ng_parse_sizedstring_type = {
939 ng_sizedstring_parse,
940 ng_sizedstring_unparse,
941 ng_sizedstring_getDefault,
945 /************************************************************************
947 ************************************************************************/
950 ng_ipaddr_parse(const struct ng_parse_type *type,
951 const char *s, int *off, const u_char *const start,
952 u_char *const buf, int *buflen)
956 for (i = 0; i < 4; i++) {
957 if ((error = ng_int8_parse(&ng_parse_int8_type,
958 s, off, start, buf + i, buflen)) != 0)
960 if (i < 3 && s[*off] != '.')
969 ng_ipaddr_unparse(const struct ng_parse_type *type,
970 const u_char *data, int *off, char *cbuf, int cbuflen)
974 bcopy(data + *off, &ip, sizeof(ip));
975 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
976 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
982 ng_ipaddr_getDefault(const struct ng_parse_type *type,
983 const u_char *const start, u_char *buf, int *buflen)
985 struct in_addr ip = { 0 };
987 if (*buflen < sizeof(ip))
989 bcopy(&ip, buf, sizeof(ip));
990 *buflen = sizeof(ip);
994 const struct ng_parse_type ng_parse_ipaddr_type = {
1000 ng_ipaddr_getDefault,
1004 /************************************************************************
1005 ETHERNET ADDRESS TYPE
1006 ************************************************************************/
1009 ng_enaddr_parse(const struct ng_parse_type *type,
1010 const char *s, int *const off, const u_char *const start,
1011 u_char *const buf, int *const buflen)
1017 if (*buflen < ETHER_ADDR_LEN)
1019 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1020 val = strtoul(s + *off, &eptr, 16);
1021 if (val > 0xff || eptr == s + *off)
1023 buf[i] = (u_char)val;
1025 if (i < ETHER_ADDR_LEN - 1) {
1031 *buflen = ETHER_ADDR_LEN;
1036 ng_enaddr_unparse(const struct ng_parse_type *type,
1037 const u_char *data, int *off, char *cbuf, int cbuflen)
1041 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1042 data[*off], data[*off + 1], data[*off + 2],
1043 data[*off + 3], data[*off + 4], data[*off + 5]);
1046 *off += ETHER_ADDR_LEN;
1050 const struct ng_parse_type ng_parse_enaddr_type = {
1060 /************************************************************************
1062 ************************************************************************/
1064 /* Get the length of a byte array */
1066 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1067 const u_char *start, const u_char *buf)
1069 ng_parse_array_getLength_t *const getLength = type->private;
1071 return (*getLength)(type, start, buf);
1074 /* Byte array element type is hex int8 */
1075 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1076 &ng_parse_hint8_type,
1077 &ng_parse_bytearray_subtype_getLength,
1080 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1081 &ng_parse_array_type,
1082 &ng_parse_bytearray_subtype_info
1086 ng_bytearray_parse(const struct ng_parse_type *type,
1087 const char *s, int *off, const u_char *const start,
1088 u_char *const buf, int *buflen)
1094 /* We accept either an array of bytes or a string constant */
1095 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1096 ng_parse_array_getLength_t *const getLength = type->info;
1099 arraylen = (*getLength)(type, start, buf);
1100 if (arraylen > *buflen) {
1101 FREE(str, M_NETGRAPH_PARSE);
1104 if (slen > arraylen) {
1105 FREE(str, M_NETGRAPH_PARSE);
1108 bcopy(str, buf, slen);
1109 bzero(buf + slen, arraylen - slen);
1110 FREE(str, M_NETGRAPH_PARSE);
1115 struct ng_parse_type subtype;
1117 subtype = ng_parse_bytearray_subtype;
1118 (const void *)subtype.private = type->info;
1119 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1124 ng_bytearray_unparse(const struct ng_parse_type *type,
1125 const u_char *data, int *off, char *cbuf, int cbuflen)
1127 struct ng_parse_type subtype;
1129 subtype = ng_parse_bytearray_subtype;
1130 (const void *)subtype.private = type->info;
1131 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1135 ng_bytearray_getDefault(const struct ng_parse_type *type,
1136 const u_char *const start, u_char *buf, int *buflen)
1138 struct ng_parse_type subtype;
1140 subtype = ng_parse_bytearray_subtype;
1141 (const void *)subtype.private = type->info;
1142 return ng_array_getDefault(&subtype, start, buf, buflen);
1145 const struct ng_parse_type ng_parse_bytearray_type = {
1150 ng_bytearray_unparse,
1151 ng_bytearray_getDefault,
1155 /************************************************************************
1157 ************************************************************************/
1159 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1161 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1162 const u_char *start, const u_char *buf)
1164 const struct ng_mesg *msg;
1166 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1167 return msg->header.arglen;
1170 /* Type for the variable length data portion of a struct ng_mesg */
1171 static const struct ng_parse_type ng_msg_data_type = {
1172 &ng_parse_bytearray_type,
1173 &ng_parse_ng_mesg_getLength
1176 /* Type for the entire struct ng_mesg header with data section */
1177 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1178 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1179 const struct ng_parse_type ng_parse_ng_mesg_type = {
1180 &ng_parse_struct_type,
1181 &ng_parse_ng_mesg_type_fields,
1184 /************************************************************************
1185 COMPOSITE HELPER ROUTINES
1186 ************************************************************************/
1189 * Convert a structure or array from ASCII to binary
1192 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1193 int *off, const u_char *const start, u_char *const buf, int *buflen,
1194 const enum comptype ctype)
1196 const int num = ng_get_composite_len(type, start, buf, ctype);
1197 int nextIndex = 0; /* next implicit array index */
1198 u_int index; /* field or element index */
1199 int *foff; /* field value offsets in string */
1200 int align, len, blen, error = 0;
1203 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1209 /* Get opening brace/bracket */
1210 if (ng_parse_get_token(s, off, &len)
1211 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1217 /* Get individual element value positions in the string */
1219 enum ng_parse_token tok;
1221 /* Check for closing brace/bracket */
1222 tok = ng_parse_get_token(s, off, &len);
1223 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1228 /* For arrays, the 'name' (ie, index) is optional, so
1229 distinguish name from values by seeing if the next
1230 token is an equals sign */
1231 if (ctype != CT_STRUCT) {
1235 /* If an opening brace/bracket, index is implied */
1236 if (tok == T_LBRACE || tok == T_LBRACKET) {
1237 index = nextIndex++;
1241 /* Might be an index, might be a value, either way... */
1242 if (tok != T_WORD) {
1247 /* If no equals sign follows, index is implied */
1249 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1250 index = nextIndex++;
1254 /* Index was specified explicitly; parse it */
1255 index = (u_int)strtoul(s + *off, &eptr, 0);
1256 if (index < 0 || eptr - (s + *off) != len) {
1260 nextIndex = index + 1;
1262 } else { /* a structure field */
1263 const struct ng_parse_struct_field *const
1264 fields = type->info;
1266 /* Find the field by name (required) in field list */
1267 if (tok != T_WORD) {
1271 for (index = 0; index < num; index++) {
1272 const struct ng_parse_struct_field *const
1273 field = &fields[index];
1275 if (strncmp(&s[*off], field->name, len) == 0
1276 && field->name[len] == '\0')
1285 /* Get equals sign */
1286 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1294 /* Check array index */
1300 /* Save value's position and skip over it for now */
1301 if (foff[index] != 0) {
1302 error = EALREADY; /* duplicate */
1305 while (isspace(s[*off]))
1308 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1313 /* Now build binary structure from supplied values and defaults */
1314 for (blen = index = 0; index < num; index++) {
1315 const struct ng_parse_type *const
1316 etype = ng_get_composite_etype(type, index, ctype);
1319 /* Zero-pad any alignment bytes */
1320 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1321 for (k = 0; k < pad; k++) {
1322 if (blen >= *buflen) {
1330 vlen = *buflen - blen;
1331 if (foff[index] == 0) { /* use default value */
1332 error = ng_get_composite_elem_default(type, index,
1333 start, buf + blen, &vlen, ctype);
1334 } else { /* parse given value */
1336 error = INVOKE(etype, parse)(etype,
1337 s, off, start, buf + blen, &vlen);
1344 /* Make total composite structure size a multiple of its alignment */
1345 if ((align = ALIGNMENT(type)) != 0) {
1346 while (blen % align != 0) {
1347 if (blen >= *buflen) {
1359 FREE(foff, M_NETGRAPH_PARSE);
1364 * Convert an array or structure from binary to ASCII
1367 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1368 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1370 const struct ng_mesg *const hdr
1371 = (const struct ng_mesg *)(data - sizeof(*hdr));
1372 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1373 const int workSize = 20 * 1024; /* XXX hard coded constant */
1374 int nextIndex = 0, didOne = 0;
1378 /* Get workspace for checking default values */
1379 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1380 if (workBuf == NULL)
1383 /* Opening brace/bracket */
1384 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1387 for (index = 0; index < num; index++) {
1388 const struct ng_parse_type *const
1389 etype = ng_get_composite_etype(type, index, ctype);
1391 /* Skip any alignment pad bytes */
1392 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1395 * See if element is equal to its default value; skip if so.
1396 * Copy struct ng_mesg header for types that peek into it.
1398 if (sizeof(*hdr) + *off < workSize) {
1399 int tempsize = workSize - sizeof(*hdr) - *off;
1401 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1402 if (ng_get_composite_elem_default(type, index, workBuf
1403 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1404 &tempsize, ctype) == 0
1405 && bcmp(workBuf + sizeof(*hdr) + *off,
1406 data + *off, tempsize) == 0) {
1413 NG_PARSE_APPEND(" ");
1414 if (ctype != CT_STRUCT) {
1415 if (index != nextIndex) {
1417 NG_PARSE_APPEND("%d=", index);
1421 const struct ng_parse_struct_field *const
1422 fields = type->info;
1424 NG_PARSE_APPEND("%s=", fields[index].name);
1428 if ((error = INVOKE(etype, unparse)
1429 (etype, data, off, cbuf, cbuflen)) != 0) {
1430 FREE(workBuf, M_NETGRAPH_PARSE);
1433 cbuflen -= strlen(cbuf);
1434 cbuf += strlen(cbuf);
1437 FREE(workBuf, M_NETGRAPH_PARSE);
1439 /* Closing brace/bracket */
1440 NG_PARSE_APPEND("%s%c",
1441 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1446 * Generate the default value for an element of an array or structure
1447 * Returns EOPNOTSUPP if default value is unspecified.
1450 ng_get_composite_elem_default(const struct ng_parse_type *type,
1451 int index, const u_char *const start, u_char *buf, int *buflen,
1452 const enum comptype ctype)
1454 const struct ng_parse_type *etype;
1455 ng_getDefault_t *func;
1462 const struct ng_parse_array_info *const ai = type->info;
1464 if (ai->getDefault != NULL) {
1465 return (*ai->getDefault)(type,
1466 index, start, buf, buflen);
1472 const struct ng_parse_fixedarray_info *const fi = type->info;
1474 if (*fi->getDefault != NULL) {
1475 return (*fi->getDefault)(type,
1476 index, start, buf, buflen);
1481 panic("%s", __func__);
1484 /* Default to element type default */
1485 etype = ng_get_composite_etype(type, index, ctype);
1486 func = METHOD(etype, getDefault);
1488 return (EOPNOTSUPP);
1489 return (*func)(etype, start, buf, buflen);
1493 * Get the number of elements in a struct, variable or fixed array.
1496 ng_get_composite_len(const struct ng_parse_type *type,
1497 const u_char *const start, const u_char *buf,
1498 const enum comptype ctype)
1503 const struct ng_parse_struct_field *const fields = type->info;
1506 for (numFields = 0; ; numFields++) {
1507 const struct ng_parse_struct_field *const
1508 fi = &fields[numFields];
1510 if (fi->name == NULL)
1517 const struct ng_parse_array_info *const ai = type->info;
1519 return (*ai->getLength)(type, start, buf);
1523 const struct ng_parse_fixedarray_info *const fi = type->info;
1528 panic("%s", __func__);
1534 * Return the type of the index'th element of a composite structure
1536 static const struct ng_parse_type *
1537 ng_get_composite_etype(const struct ng_parse_type *type,
1538 int index, const enum comptype ctype)
1540 const struct ng_parse_type *etype = NULL;
1545 const struct ng_parse_struct_field *const fields = type->info;
1547 etype = fields[index].type;
1552 const struct ng_parse_array_info *const ai = type->info;
1554 etype = ai->elementType;
1559 const struct ng_parse_fixedarray_info *const fi = type->info;
1561 etype = fi->elementType;
1565 panic("%s", __func__);
1571 * Get the number of bytes to skip to align for the next
1572 * element in a composite structure.
1575 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1576 int index, enum comptype ctype, int posn)
1578 const struct ng_parse_type *const
1579 etype = ng_get_composite_etype(type, index, ctype);
1582 /* Get element's alignment, and possibly override */
1583 align = ALIGNMENT(etype);
1584 if (ctype == CT_STRUCT) {
1585 const struct ng_parse_struct_field *const fields = type->info;
1587 if (fields[index].alignment != 0)
1588 align = fields[index].alignment;
1591 /* Return number of bytes to skip to align */
1592 return (align ? (align - (posn % align)) % align : 0);
1595 /************************************************************************
1596 PARSING HELPER ROUTINES
1597 ************************************************************************/
1603 ng_parse_skip_value(const char *s, int off0, int *lenp)
1605 int len, nbracket, nbrace;
1608 len = nbracket = nbrace = 0;
1610 switch (ng_parse_get_token(s, &off, &len)) {
1618 if (nbracket-- == 0)
1631 } while (nbracket > 0 || nbrace > 0);
1637 * Find the next token in the string, starting at offset *startp.
1638 * Returns the token type, with *startp pointing to the first char
1639 * and *lenp the length.
1642 ng_parse_get_token(const char *s, int *startp, int *lenp)
1647 while (isspace(s[*startp]))
1649 switch (s[*startp]) {
1669 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1671 FREE(t, M_NETGRAPH_PARSE);
1674 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1675 && s[i] != '{' && s[i] != '}' && s[i] != '['
1676 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1678 *lenp = i - *startp;
1684 * Get a string token, which must be enclosed in double quotes.
1685 * The normal C backslash escapes are recognized.
1688 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1694 while (isspace(s[*startp]))
1697 if (s[*startp] != '"')
1699 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1702 strcpy(cbuf, s + start + 1);
1703 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1710 } else if (p[0] == '\\' && p[1] != '\0') {
1741 case '0': case '1': case '2': case '3':
1742 case '4': case '5': case '6': case '7':
1744 k < 3 && *v >= '0' && *v <= '7'; v++) {
1745 x = (x << 3) + (*v - '0');
1751 for (v++, x = k = 0;
1752 k < 2 && isxdigit(*v); v++) {
1753 x = (x << 4) + (isdigit(*v) ?
1755 (tolower(*v) - 'a' + 10));
1766 FREE(cbuf, M_NETGRAPH_PARSE);
1767 return (NULL); /* no closing quote */
1771 * Encode a string so it can be safely put in double quotes.
1772 * Caller must free the result. Exactly "slen" characters
1776 ng_encode_string(const char *raw, int slen)
1782 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1786 for (i = 0; i < slen; i++, raw++) {
1814 if (*raw < 0x20 || *raw > 0x7e) {
1815 off += sprintf(cbuf + off,
1816 "\\x%02x", (u_char)*raw);
1828 /************************************************************************
1829 VIRTUAL METHOD LOOKUP
1830 ************************************************************************/
1833 ng_get_parse_method(const struct ng_parse_type *t)
1835 while (t != NULL && t->parse == NULL)
1837 return (t ? t->parse : NULL);
1840 static ng_unparse_t *
1841 ng_get_unparse_method(const struct ng_parse_type *t)
1843 while (t != NULL && t->unparse == NULL)
1845 return (t ? t->unparse : NULL);
1848 static ng_getDefault_t *
1849 ng_get_getDefault_method(const struct ng_parse_type *t)
1851 while (t != NULL && t->getDefault == NULL)
1853 return (t ? t->getDefault : NULL);
1856 static ng_getAlign_t *
1857 ng_get_getAlign_method(const struct ng_parse_type *t)
1859 while (t != NULL && t->getAlign == NULL)
1861 return (t ? t->getAlign : NULL);