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 $
40 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.3.2.8 2002/07/02 23:44:02 archie Exp $
41 * $DragonFly: src/sys/netgraph/netgraph/ng_parse.c,v 1.7 2006/09/30 20:23:05 swildner Exp $
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
51 #include <netinet/in.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_parse.h>
57 /* Compute alignment for primitive integral types */
73 #define INT8_ALIGNMENT 1
74 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
75 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
76 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
78 /* Output format for integral types */
79 #define INT_UNSIGNED 0
83 /* Type of composite object: struct, array, or fixedarray */
90 /* Composite types helper functions */
91 static int ng_parse_composite(const struct ng_parse_type *type,
92 const char *s, int *off, const u_char *start,
93 u_char *const buf, int *buflen, enum comptype ctype);
94 static int ng_unparse_composite(const struct ng_parse_type *type,
95 const u_char *data, int *off, char *cbuf, int cbuflen,
97 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
98 int index, const u_char *start, u_char *buf,
99 int *buflen, enum comptype ctype);
100 static int ng_get_composite_len(const struct ng_parse_type *type,
101 const u_char *start, const u_char *buf,
102 enum comptype ctype);
103 static const struct ng_parse_type *ng_get_composite_etype(const struct
104 ng_parse_type *type, int index, enum comptype ctype);
105 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
106 int index, enum comptype ctype, int posn);
108 /* Parsing helper functions */
109 static int ng_parse_skip_value(const char *s, int off, int *lenp);
111 /* Poor man's virtual method calls */
112 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
113 #define INVOKE(t,m) (*METHOD(t,m))
115 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
116 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
117 static ng_getDefault_t *ng_get_getDefault_method(const
118 struct ng_parse_type *t);
119 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
121 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
122 0 : INVOKE(t, getAlign)(t))
124 /* For converting binary to string */
125 #define NG_PARSE_APPEND(fmt, args...) \
129 len = snprintf((cbuf), (cbuflen), \
131 if (len >= (cbuflen)) \
137 /************************************************************************
139 ************************************************************************/
142 * Convert an ASCII string to binary according to the supplied type descriptor
145 ng_parse(const struct ng_parse_type *type,
146 const char *string, int *off, u_char *buf, int *buflen)
148 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
152 * Convert binary to an ASCII string according to the supplied type descriptor
155 ng_unparse(const struct ng_parse_type *type,
156 const u_char *data, char *cbuf, int cbuflen)
160 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
164 * Fill in the default value according to the supplied type descriptor
167 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
169 ng_getDefault_t *const func = METHOD(type, getDefault);
173 return (*func)(type, buf, buf, buflen);
177 /************************************************************************
179 ************************************************************************/
182 ng_struct_parse(const struct ng_parse_type *type,
183 const char *s, int *off, const u_char *const start,
184 u_char *const buf, int *buflen)
186 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
190 ng_struct_unparse(const struct ng_parse_type *type,
191 const u_char *data, int *off, char *cbuf, int cbuflen)
193 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
197 ng_struct_getDefault(const struct ng_parse_type *type,
198 const u_char *const start, u_char *buf, int *buflen)
202 return ng_parse_composite(type,
203 "{}", &off, start, buf, buflen, CT_STRUCT);
207 ng_struct_getAlign(const struct ng_parse_type *type)
209 const struct ng_parse_struct_field *field;
212 for (field = type->info; field->name != NULL; field++) {
213 int falign = ALIGNMENT(field->type);
221 const struct ng_parse_type ng_parse_struct_type = {
227 ng_struct_getDefault,
231 /************************************************************************
232 FIXED LENGTH ARRAY TYPE
233 ************************************************************************/
236 ng_fixedarray_parse(const struct ng_parse_type *type,
237 const char *s, int *off, const u_char *const start,
238 u_char *const buf, int *buflen)
240 return ng_parse_composite(type,
241 s, off, start, buf, buflen, CT_FIXEDARRAY);
245 ng_fixedarray_unparse(const struct ng_parse_type *type,
246 const u_char *data, int *off, char *cbuf, int cbuflen)
248 return ng_unparse_composite(type,
249 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
253 ng_fixedarray_getDefault(const struct ng_parse_type *type,
254 const u_char *const start, u_char *buf, int *buflen)
258 return ng_parse_composite(type,
259 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
263 ng_fixedarray_getAlign(const struct ng_parse_type *type)
265 const struct ng_parse_fixedarray_info *fi = type->info;
267 return ALIGNMENT(fi->elementType);
270 const struct ng_parse_type ng_parse_fixedarray_type = {
275 ng_fixedarray_unparse,
276 ng_fixedarray_getDefault,
277 ng_fixedarray_getAlign
280 /************************************************************************
281 VARIABLE LENGTH ARRAY TYPE
282 ************************************************************************/
285 ng_array_parse(const struct ng_parse_type *type,
286 const char *s, int *off, const u_char *const start,
287 u_char *const buf, int *buflen)
289 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
293 ng_array_unparse(const struct ng_parse_type *type,
294 const u_char *data, int *off, char *cbuf, int cbuflen)
296 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
300 ng_array_getDefault(const struct ng_parse_type *type,
301 const u_char *const start, u_char *buf, int *buflen)
305 return ng_parse_composite(type,
306 "[]", &off, start, buf, buflen, CT_ARRAY);
310 ng_array_getAlign(const struct ng_parse_type *type)
312 const struct ng_parse_array_info *ai = type->info;
314 return ALIGNMENT(ai->elementType);
317 const struct ng_parse_type ng_parse_array_type = {
327 /************************************************************************
329 ************************************************************************/
332 ng_int8_parse(const struct ng_parse_type *type,
333 const char *s, int *off, const u_char *const start,
334 u_char *const buf, int *buflen)
340 val = strtol(s + *off, &eptr, 0);
341 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
345 bcopy(&val8, buf, sizeof(int8_t));
346 *buflen = sizeof(int8_t);
351 ng_int8_unparse(const struct ng_parse_type *type,
352 const u_char *data, int *off, char *cbuf, int cbuflen)
358 bcopy(data + *off, &val, sizeof(int8_t));
359 switch ((int)type->info) {
366 fval = (u_int8_t)val;
370 fval = (u_int8_t)val;
373 panic("%s: unknown type", __func__);
375 NG_PARSE_APPEND(fmt, fval);
376 *off += sizeof(int8_t);
381 ng_int8_getDefault(const struct ng_parse_type *type,
382 const u_char *const start, u_char *buf, int *buflen)
386 if (*buflen < sizeof(int8_t))
389 bcopy(&val, buf, sizeof(int8_t));
390 *buflen = sizeof(int8_t);
395 ng_int8_getAlign(const struct ng_parse_type *type)
397 return INT8_ALIGNMENT;
400 const struct ng_parse_type ng_parse_int8_type = {
410 const struct ng_parse_type ng_parse_uint8_type = {
415 const struct ng_parse_type ng_parse_hint8_type = {
420 /************************************************************************
422 ************************************************************************/
425 ng_int16_parse(const struct ng_parse_type *type,
426 const char *s, int *off, const u_char *const start,
427 u_char *const buf, int *buflen)
433 val = strtol(s + *off, &eptr, 0);
434 if (val < (int16_t)0x8000
435 || val > (u_int16_t)0xffff || eptr == s + *off)
438 val16 = (int16_t)val;
439 bcopy(&val16, buf, sizeof(int16_t));
440 *buflen = sizeof(int16_t);
445 ng_int16_unparse(const struct ng_parse_type *type,
446 const u_char *data, int *off, char *cbuf, int cbuflen)
452 bcopy(data + *off, &val, sizeof(int16_t));
453 switch ((int)type->info) {
460 fval = (u_int16_t)val;
464 fval = (u_int16_t)val;
467 panic("%s: unknown type", __func__);
469 NG_PARSE_APPEND(fmt, fval);
470 *off += sizeof(int16_t);
475 ng_int16_getDefault(const struct ng_parse_type *type,
476 const u_char *const start, u_char *buf, int *buflen)
480 if (*buflen < sizeof(int16_t))
483 bcopy(&val, buf, sizeof(int16_t));
484 *buflen = sizeof(int16_t);
489 ng_int16_getAlign(const struct ng_parse_type *type)
491 return INT16_ALIGNMENT;
494 const struct ng_parse_type ng_parse_int16_type = {
504 const struct ng_parse_type ng_parse_uint16_type = {
505 &ng_parse_int16_type,
509 const struct ng_parse_type ng_parse_hint16_type = {
510 &ng_parse_int16_type,
514 /************************************************************************
516 ************************************************************************/
519 ng_int32_parse(const struct ng_parse_type *type,
520 const char *s, int *off, const u_char *const start,
521 u_char *const buf, int *buflen)
523 long val; /* assumes long is at least 32 bits */
527 val = strtol(s + *off, &eptr, 0);
528 if (val < (int32_t)0x80000000
529 || val > (u_int32_t)0xffffffff || eptr == s + *off)
532 val32 = (int32_t)val;
533 bcopy(&val32, buf, sizeof(int32_t));
534 *buflen = sizeof(int32_t);
539 ng_int32_unparse(const struct ng_parse_type *type,
540 const u_char *data, int *off, char *cbuf, int cbuflen)
546 bcopy(data + *off, &val, sizeof(int32_t));
547 switch ((int)type->info) {
554 fval = (u_int32_t)val;
558 fval = (u_int32_t)val;
561 panic("%s: unknown type", __func__);
563 NG_PARSE_APPEND(fmt, fval);
564 *off += sizeof(int32_t);
569 ng_int32_getDefault(const struct ng_parse_type *type,
570 const u_char *const start, u_char *buf, int *buflen)
574 if (*buflen < sizeof(int32_t))
577 bcopy(&val, buf, sizeof(int32_t));
578 *buflen = sizeof(int32_t);
583 ng_int32_getAlign(const struct ng_parse_type *type)
585 return INT32_ALIGNMENT;
588 const struct ng_parse_type ng_parse_int32_type = {
598 const struct ng_parse_type ng_parse_uint32_type = {
599 &ng_parse_int32_type,
603 const struct ng_parse_type ng_parse_hint32_type = {
604 &ng_parse_int32_type,
608 /************************************************************************
610 ************************************************************************/
613 ng_int64_parse(const struct ng_parse_type *type,
614 const char *s, int *off, const u_char *const start,
615 u_char *const buf, int *buflen)
621 val = strtoq(s + *off, &eptr, 0);
622 if (eptr == s + *off)
625 val64 = (int64_t)val;
626 bcopy(&val64, buf, sizeof(int64_t));
627 *buflen = sizeof(int64_t);
632 ng_int64_unparse(const struct ng_parse_type *type,
633 const u_char *data, int *off, char *cbuf, int cbuflen)
639 bcopy(data + *off, &val, sizeof(int64_t));
640 switch ((int)type->info) {
647 fval = (u_int64_t)val;
651 fval = (u_int64_t)val;
654 panic("%s: unknown type", __func__);
656 NG_PARSE_APPEND(fmt, fval);
657 *off += sizeof(int64_t);
662 ng_int64_getDefault(const struct ng_parse_type *type,
663 const u_char *const start, u_char *buf, int *buflen)
667 if (*buflen < sizeof(int64_t))
670 bcopy(&val, buf, sizeof(int64_t));
671 *buflen = sizeof(int64_t);
676 ng_int64_getAlign(const struct ng_parse_type *type)
678 return INT64_ALIGNMENT;
681 const struct ng_parse_type ng_parse_int64_type = {
691 const struct ng_parse_type ng_parse_uint64_type = {
692 &ng_parse_int64_type,
696 const struct ng_parse_type ng_parse_hint64_type = {
697 &ng_parse_int64_type,
701 /************************************************************************
703 ************************************************************************/
706 ng_string_parse(const struct ng_parse_type *type,
707 const char *s, int *off, const u_char *const start,
708 u_char *const buf, int *buflen)
713 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
716 len = strlen(sval) + 1;
717 bcopy(sval, buf, len);
718 FREE(sval, M_NETGRAPH);
724 ng_string_unparse(const struct ng_parse_type *type,
725 const u_char *data, int *off, char *cbuf, int cbuflen)
727 const char *const raw = (const char *)data + *off;
728 char *const s = ng_encode_string(raw);
732 NG_PARSE_APPEND("%s", s);
733 *off += strlen(raw) + 1;
739 ng_string_getDefault(const struct ng_parse_type *type,
740 const u_char *const start, u_char *buf, int *buflen)
745 buf[0] = (u_char)'\0';
750 const struct ng_parse_type ng_parse_string_type = {
756 ng_string_getDefault,
760 /************************************************************************
761 FIXED BUFFER STRING TYPE
762 ************************************************************************/
765 ng_fixedstring_parse(const struct ng_parse_type *type,
766 const char *s, int *off, const u_char *const start,
767 u_char *const buf, int *buflen)
769 const struct ng_parse_fixedstring_info *const fi = type->info;
773 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
775 if (strlen(sval) + 1 > fi->bufSize)
778 len = strlen(sval) + 1;
779 bcopy(sval, buf, len);
780 FREE(sval, M_NETGRAPH);
781 bzero(buf + len, fi->bufSize - len);
782 *buflen = fi->bufSize;
787 ng_fixedstring_unparse(const struct ng_parse_type *type,
788 const u_char *data, int *off, char *cbuf, int cbuflen)
790 const struct ng_parse_fixedstring_info *const fi = type->info;
791 int error, temp = *off;
793 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
800 ng_fixedstring_getDefault(const struct ng_parse_type *type,
801 const u_char *const start, u_char *buf, int *buflen)
803 const struct ng_parse_fixedstring_info *const fi = type->info;
805 if (*buflen < fi->bufSize)
807 bzero(buf, fi->bufSize);
808 *buflen = fi->bufSize;
812 const struct ng_parse_type ng_parse_fixedstring_type = {
816 ng_fixedstring_parse,
817 ng_fixedstring_unparse,
818 ng_fixedstring_getDefault,
822 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
825 const struct ng_parse_type ng_parse_nodebuf_type = {
826 &ng_parse_fixedstring_type,
827 &ng_parse_nodebuf_info
830 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
833 const struct ng_parse_type ng_parse_hookbuf_type = {
834 &ng_parse_fixedstring_type,
835 &ng_parse_hookbuf_info
838 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
841 const struct ng_parse_type ng_parse_pathbuf_type = {
842 &ng_parse_fixedstring_type,
843 &ng_parse_pathbuf_info
846 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
849 const struct ng_parse_type ng_parse_typebuf_type = {
850 &ng_parse_fixedstring_type,
851 &ng_parse_typebuf_info
854 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
857 const struct ng_parse_type ng_parse_cmdbuf_type = {
858 &ng_parse_fixedstring_type,
859 &ng_parse_cmdbuf_info
862 /************************************************************************
864 ************************************************************************/
867 ng_ipaddr_parse(const struct ng_parse_type *type,
868 const char *s, int *off, const u_char *const start,
869 u_char *const buf, int *buflen)
873 for (i = 0; i < 4; i++) {
874 if ((error = ng_int8_parse(&ng_parse_int8_type,
875 s, off, start, buf + i, buflen)) != 0)
877 if (i < 3 && s[*off] != '.')
886 ng_ipaddr_unparse(const struct ng_parse_type *type,
887 const u_char *data, int *off, char *cbuf, int cbuflen)
891 bcopy(data + *off, &ip, sizeof(ip));
892 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
893 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
899 ng_ipaddr_getDefault(const struct ng_parse_type *type,
900 const u_char *const start, u_char *buf, int *buflen)
902 struct in_addr ip = { 0 };
904 if (*buflen < sizeof(ip))
906 bcopy(&ip, buf, sizeof(ip));
907 *buflen = sizeof(ip);
911 const struct ng_parse_type ng_parse_ipaddr_type = {
917 ng_ipaddr_getDefault,
921 /************************************************************************
923 ************************************************************************/
925 /* Get the length of a byte array */
927 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
928 const u_char *start, const u_char *buf)
930 ng_parse_array_getLength_t *const getLength = type->private;
932 return (*getLength)(type, start, buf);
935 /* Byte array element type is hex int8 */
936 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
937 &ng_parse_hint8_type,
938 &ng_parse_bytearray_subtype_getLength,
941 static const struct ng_parse_type ng_parse_bytearray_subtype = {
942 &ng_parse_array_type,
943 &ng_parse_bytearray_subtype_info
947 ng_bytearray_parse(const struct ng_parse_type *type,
948 const char *s, int *off, const u_char *const start,
949 u_char *const buf, int *buflen)
954 /* We accept either an array of bytes or a string constant */
955 if ((str = ng_get_string_token(s, off, &toklen)) != NULL) {
956 ng_parse_array_getLength_t *const getLength = type->info;
959 arraylen = (*getLength)(type, start, buf);
960 if (arraylen > *buflen) {
961 FREE(str, M_NETGRAPH);
964 slen = strlen(str) + 1;
965 if (slen > arraylen) {
966 FREE(str, M_NETGRAPH);
969 bcopy(str, buf, slen);
970 bzero(buf + slen, arraylen - slen);
971 FREE(str, M_NETGRAPH);
976 struct ng_parse_type subtype;
978 subtype = ng_parse_bytearray_subtype;
979 *(const void **)&subtype.private = type->info;
980 return ng_array_parse(&subtype, s, off, start, buf, buflen);
985 ng_bytearray_unparse(const struct ng_parse_type *type,
986 const u_char *data, int *off, char *cbuf, int cbuflen)
988 struct ng_parse_type subtype;
990 subtype = ng_parse_bytearray_subtype;
991 *(const void **)&subtype.private = type->info;
992 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
996 ng_bytearray_getDefault(const struct ng_parse_type *type,
997 const u_char *const start, u_char *buf, int *buflen)
999 struct ng_parse_type subtype;
1001 subtype = ng_parse_bytearray_subtype;
1002 *(const void **)&subtype.private = type->info;
1003 return ng_array_getDefault(&subtype, start, buf, buflen);
1006 const struct ng_parse_type ng_parse_bytearray_type = {
1011 ng_bytearray_unparse,
1012 ng_bytearray_getDefault,
1016 /************************************************************************
1018 ************************************************************************/
1020 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1022 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1023 const u_char *start, const u_char *buf)
1025 const struct ng_mesg *msg;
1027 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1028 return msg->header.arglen;
1031 /* Type for the variable length data portion of a struct ng_mesg */
1032 static const struct ng_parse_type ng_msg_data_type = {
1033 &ng_parse_bytearray_type,
1034 &ng_parse_ng_mesg_getLength
1037 /* Type for the entire struct ng_mesg header with data section */
1038 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1039 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1040 const struct ng_parse_type ng_parse_ng_mesg_type = {
1041 &ng_parse_struct_type,
1042 &ng_parse_ng_mesg_type_fields,
1045 /************************************************************************
1046 COMPOSITE HELPER ROUTINES
1047 ************************************************************************/
1050 * Convert a structure or array from ASCII to binary
1053 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1054 int *off, const u_char *const start, u_char *const buf, int *buflen,
1055 const enum comptype ctype)
1057 const int num = ng_get_composite_len(type, start, buf, ctype);
1058 int nextIndex = 0; /* next implicit array index */
1059 u_int index; /* field or element index */
1060 int *foff; /* field value offsets in string */
1061 int align, len, blen, error = 0;
1064 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT);
1069 bzero(foff, num * sizeof(*foff));
1071 /* Get opening brace/bracket */
1072 if (ng_parse_get_token(s, off, &len)
1073 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1079 /* Get individual element value positions in the string */
1081 enum ng_parse_token tok;
1083 /* Check for closing brace/bracket */
1084 tok = ng_parse_get_token(s, off, &len);
1085 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1090 /* For arrays, the 'name' (ie, index) is optional, so
1091 distinguish name from values by seeing if the next
1092 token is an equals sign */
1093 if (ctype != CT_STRUCT) {
1097 /* If an opening brace/bracket, index is implied */
1098 if (tok == T_LBRACE || tok == T_LBRACKET) {
1099 index = nextIndex++;
1103 /* Might be an index, might be a value, either way... */
1104 if (tok != T_WORD) {
1109 /* If no equals sign follows, index is implied */
1111 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1112 index = nextIndex++;
1116 /* Index was specified explicitly; parse it */
1117 index = (u_int)strtoul(s + *off, &eptr, 0);
1118 if (index < 0 || eptr - (s + *off) != len) {
1122 nextIndex = index + 1;
1125 ; /* avoid gcc3.x warning */
1126 } else { /* a structure field */
1127 const struct ng_parse_struct_field *const
1128 fields = type->info;
1130 /* Find the field by name (required) in field list */
1131 if (tok != T_WORD) {
1135 for (index = 0; index < num; index++) {
1136 const struct ng_parse_struct_field *const
1137 field = &fields[index];
1139 if (strncmp(&s[*off], field->name, len) == 0
1140 && field->name[len] == '\0')
1149 /* Get equals sign */
1150 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1157 /* Check array index */
1163 /* Save value's position and skip over it for now */
1164 if (foff[index] != 0) {
1165 error = EALREADY; /* duplicate */
1168 while (isspace(s[*off]))
1171 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1176 /* Now build binary structure from supplied values and defaults */
1177 for (blen = index = 0; index < num; index++) {
1178 const struct ng_parse_type *const
1179 etype = ng_get_composite_etype(type, index, ctype);
1182 /* Zero-pad any alignment bytes */
1183 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1184 for (k = 0; k < pad; k++) {
1185 if (blen >= *buflen) {
1193 vlen = *buflen - blen;
1194 if (foff[index] == 0) { /* use default value */
1195 error = ng_get_composite_elem_default(type, index,
1196 start, buf + blen, &vlen, ctype);
1197 } else { /* parse given value */
1199 error = INVOKE(etype, parse)(etype,
1200 s, off, start, buf + blen, &vlen);
1207 /* Make total composite structure size a multiple of its alignment */
1208 if ((align = ALIGNMENT(type)) != 0) {
1209 while (blen % align != 0) {
1210 if (blen >= *buflen) {
1222 FREE(foff, M_NETGRAPH);
1227 * Convert an array or structure from binary to ASCII
1230 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1231 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1233 const struct ng_mesg *const hdr
1234 = (const struct ng_mesg *)(data - sizeof(*hdr));
1235 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1236 const int workSize = 20 * 1024; /* XXX hard coded constant */
1237 int nextIndex = 0, didOne = 0;
1241 /* Get workspace for checking default values */
1242 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH, M_NOWAIT);
1243 if (workBuf == NULL)
1246 /* Opening brace/bracket */
1247 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1250 for (index = 0; index < num; index++) {
1251 const struct ng_parse_type *const
1252 etype = ng_get_composite_etype(type, index, ctype);
1254 /* Skip any alignment pad bytes */
1255 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1258 * See if element is equal to its default value; skip if so.
1259 * Copy struct ng_mesg header for types that peek into it.
1261 if (sizeof(*hdr) + *off < workSize) {
1262 int tempsize = workSize - sizeof(*hdr) - *off;
1264 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1265 if (ng_get_composite_elem_default(type, index, workBuf
1266 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1267 &tempsize, ctype) == 0
1268 && bcmp(workBuf + sizeof(*hdr) + *off,
1269 data + *off, tempsize) == 0) {
1276 NG_PARSE_APPEND(" ");
1277 if (ctype != CT_STRUCT) {
1278 if (index != nextIndex) {
1280 NG_PARSE_APPEND("%d=", index);
1284 const struct ng_parse_struct_field *const
1285 fields = type->info;
1287 NG_PARSE_APPEND("%s=", fields[index].name);
1291 if ((error = INVOKE(etype, unparse)
1292 (etype, data, off, cbuf, cbuflen)) != 0) {
1293 FREE(workBuf, M_NETGRAPH);
1296 cbuflen -= strlen(cbuf);
1297 cbuf += strlen(cbuf);
1300 FREE(workBuf, M_NETGRAPH);
1302 /* Closing brace/bracket */
1303 NG_PARSE_APPEND("%s%c",
1304 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1309 * Generate the default value for an element of an array or structure
1310 * Returns EOPNOTSUPP if default value is unspecified.
1313 ng_get_composite_elem_default(const struct ng_parse_type *type,
1314 int index, const u_char *const start, u_char *buf, int *buflen,
1315 const enum comptype ctype)
1317 const struct ng_parse_type *etype;
1318 ng_getDefault_t *func;
1325 const struct ng_parse_array_info *const ai = type->info;
1327 if (ai->getDefault != NULL) {
1328 return (*ai->getDefault)(type,
1329 index, start, buf, buflen);
1335 const struct ng_parse_fixedarray_info *const fi = type->info;
1337 if (*fi->getDefault != NULL) {
1338 return (*fi->getDefault)(type,
1339 index, start, buf, buflen);
1344 panic("%s", __func__);
1347 /* Default to element type default */
1348 etype = ng_get_composite_etype(type, index, ctype);
1349 func = METHOD(etype, getDefault);
1351 return (EOPNOTSUPP);
1352 return (*func)(etype, start, buf, buflen);
1356 * Get the number of elements in a struct, variable or fixed array.
1359 ng_get_composite_len(const struct ng_parse_type *type,
1360 const u_char *const start, const u_char *buf,
1361 const enum comptype ctype)
1366 const struct ng_parse_struct_field *const fields = type->info;
1369 for (numFields = 0; ; numFields++) {
1370 const struct ng_parse_struct_field *const
1371 fi = &fields[numFields];
1373 if (fi->name == NULL)
1380 const struct ng_parse_array_info *const ai = type->info;
1382 return (*ai->getLength)(type, start, buf);
1386 const struct ng_parse_fixedarray_info *const fi = type->info;
1391 panic("%s", __func__);
1397 * Return the type of the index'th element of a composite structure
1399 static const struct ng_parse_type *
1400 ng_get_composite_etype(const struct ng_parse_type *type,
1401 int index, const enum comptype ctype)
1403 const struct ng_parse_type *etype = NULL;
1408 const struct ng_parse_struct_field *const fields = type->info;
1410 etype = fields[index].type;
1415 const struct ng_parse_array_info *const ai = type->info;
1417 etype = ai->elementType;
1422 const struct ng_parse_fixedarray_info *const fi = type->info;
1424 etype = fi->elementType;
1428 panic("%s", __func__);
1434 * Get the number of bytes to skip to align for the next
1435 * element in a composite structure.
1438 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1439 int index, enum comptype ctype, int posn)
1441 const struct ng_parse_type *const
1442 etype = ng_get_composite_etype(type, index, ctype);
1445 /* Get element's alignment, and possibly override */
1446 align = ALIGNMENT(etype);
1447 if (ctype == CT_STRUCT) {
1448 const struct ng_parse_struct_field *const fields = type->info;
1450 if (fields[index].alignment != 0)
1451 align = fields[index].alignment;
1454 /* Return number of bytes to skip to align */
1455 return (align ? (align - (posn % align)) % align : 0);
1458 /************************************************************************
1459 PARSING HELPER ROUTINES
1460 ************************************************************************/
1466 ng_parse_skip_value(const char *s, int off0, int *lenp)
1468 int len, nbracket, nbrace;
1471 len = nbracket = nbrace = 0;
1473 switch (ng_parse_get_token(s, &off, &len)) {
1481 if (nbracket-- == 0)
1494 } while (nbracket > 0 || nbrace > 0);
1500 * Find the next token in the string, starting at offset *startp.
1501 * Returns the token type, with *startp pointing to the first char
1502 * and *lenp the length.
1505 ng_parse_get_token(const char *s, int *startp, int *lenp)
1510 while (isspace(s[*startp]))
1512 switch (s[*startp]) {
1532 if ((t = ng_get_string_token(s, startp, lenp)) == NULL)
1534 FREE(t, M_NETGRAPH);
1537 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1538 && s[i] != '{' && s[i] != '}' && s[i] != '['
1539 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1541 *lenp = i - *startp;
1547 * Get a string token, which must be enclosed in double quotes.
1548 * The normal C backslash escapes are recognized.
1551 ng_get_string_token(const char *s, int *startp, int *lenp)
1556 while (isspace(s[*startp]))
1559 if (s[*startp] != '"')
1561 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT);
1564 strcpy(cbuf, s + start + 1);
1565 for (off = 1, p = cbuf; *p != '\0'; off++, p++) {
1570 } else if (p[0] == '\\' && p[1] != '\0') {
1601 case '0': case '1': case '2': case '3':
1602 case '4': case '5': case '6': case '7':
1604 k < 3 && *v >= '0' && *v <= '7'; v++) {
1605 x = (x << 3) + (*v - '0');
1611 for (v++, x = k = 0;
1612 k < 2 && isxdigit(*v); v++) {
1613 x = (x << 4) + (isdigit(*v) ?
1615 (tolower(*v) - 'a' + 10));
1626 return (NULL); /* no closing quote */
1630 * Encode a string so it can be safely put in double quotes.
1631 * Caller must free the result.
1634 ng_encode_string(const char *raw)
1639 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1643 for ( ; *raw != '\0'; raw++) {
1671 if (*raw < 0x20 || *raw > 0x7e) {
1672 off += sprintf(cbuf + off,
1673 "\\x%02x", (u_char)*raw);
1685 /************************************************************************
1686 VIRTUAL METHOD LOOKUP
1687 ************************************************************************/
1690 ng_get_parse_method(const struct ng_parse_type *t)
1692 while (t != NULL && t->parse == NULL)
1694 return (t ? t->parse : NULL);
1697 static ng_unparse_t *
1698 ng_get_unparse_method(const struct ng_parse_type *t)
1700 while (t != NULL && t->unparse == NULL)
1702 return (t ? t->unparse : NULL);
1705 static ng_getDefault_t *
1706 ng_get_getDefault_method(const struct ng_parse_type *t)
1708 while (t != NULL && t->getDefault == NULL)
1710 return (t ? t->getDefault : NULL);
1713 static ng_getAlign_t *
1714 ng_get_getAlign_method(const struct ng_parse_type *t)
1716 while (t != NULL && t->getAlign == NULL)
1718 return (t ? t->getAlign : NULL);