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.10 2008/01/05 14:02:39 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 = ksnprintf((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)(intptr_t)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)(intptr_t)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)(intptr_t)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)(intptr_t)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 | M_ZERO);
1070 /* Get opening brace/bracket */
1071 if (ng_parse_get_token(s, off, &len)
1072 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1078 /* Get individual element value positions in the string */
1080 enum ng_parse_token tok;
1082 /* Check for closing brace/bracket */
1083 tok = ng_parse_get_token(s, off, &len);
1084 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1089 /* For arrays, the 'name' (ie, index) is optional, so
1090 distinguish name from values by seeing if the next
1091 token is an equals sign */
1092 if (ctype != CT_STRUCT) {
1096 /* If an opening brace/bracket, index is implied */
1097 if (tok == T_LBRACE || tok == T_LBRACKET) {
1098 index = nextIndex++;
1102 /* Might be an index, might be a value, either way... */
1103 if (tok != T_WORD) {
1108 /* If no equals sign follows, index is implied */
1110 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1111 index = nextIndex++;
1115 /* Index was specified explicitly; parse it */
1116 index = (u_int)strtoul(s + *off, &eptr, 0);
1117 if (index < 0 || eptr - (s + *off) != len) {
1121 nextIndex = index + 1;
1124 ; /* avoid gcc3.x warning */
1125 } else { /* a structure field */
1126 const struct ng_parse_struct_field *const
1127 fields = type->info;
1129 /* Find the field by name (required) in field list */
1130 if (tok != T_WORD) {
1134 for (index = 0; index < num; index++) {
1135 const struct ng_parse_struct_field *const
1136 field = &fields[index];
1138 if (strncmp(&s[*off], field->name, len) == 0
1139 && field->name[len] == '\0')
1148 /* Get equals sign */
1149 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1156 /* Check array index */
1162 /* Save value's position and skip over it for now */
1163 if (foff[index] != 0) {
1164 error = EALREADY; /* duplicate */
1167 while (isspace(s[*off]))
1170 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1175 /* Now build binary structure from supplied values and defaults */
1176 for (blen = index = 0; index < num; index++) {
1177 const struct ng_parse_type *const
1178 etype = ng_get_composite_etype(type, index, ctype);
1181 /* Zero-pad any alignment bytes */
1182 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1183 for (k = 0; k < pad; k++) {
1184 if (blen >= *buflen) {
1192 vlen = *buflen - blen;
1193 if (foff[index] == 0) { /* use default value */
1194 error = ng_get_composite_elem_default(type, index,
1195 start, buf + blen, &vlen, ctype);
1196 } else { /* parse given value */
1198 error = INVOKE(etype, parse)(etype,
1199 s, off, start, buf + blen, &vlen);
1206 /* Make total composite structure size a multiple of its alignment */
1207 if ((align = ALIGNMENT(type)) != 0) {
1208 while (blen % align != 0) {
1209 if (blen >= *buflen) {
1221 FREE(foff, M_NETGRAPH);
1226 * Convert an array or structure from binary to ASCII
1229 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1230 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1232 const struct ng_mesg *const hdr
1233 = (const struct ng_mesg *)(data - sizeof(*hdr));
1234 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1235 const int workSize = 20 * 1024; /* XXX hard coded constant */
1236 int nextIndex = 0, didOne = 0;
1240 /* Get workspace for checking default values */
1241 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH, M_NOWAIT);
1242 if (workBuf == NULL)
1245 /* Opening brace/bracket */
1246 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1249 for (index = 0; index < num; index++) {
1250 const struct ng_parse_type *const
1251 etype = ng_get_composite_etype(type, index, ctype);
1253 /* Skip any alignment pad bytes */
1254 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1257 * See if element is equal to its default value; skip if so.
1258 * Copy struct ng_mesg header for types that peek into it.
1260 if (sizeof(*hdr) + *off < workSize) {
1261 int tempsize = workSize - sizeof(*hdr) - *off;
1263 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1264 if (ng_get_composite_elem_default(type, index, workBuf
1265 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1266 &tempsize, ctype) == 0
1267 && bcmp(workBuf + sizeof(*hdr) + *off,
1268 data + *off, tempsize) == 0) {
1275 NG_PARSE_APPEND(" ");
1276 if (ctype != CT_STRUCT) {
1277 if (index != nextIndex) {
1279 NG_PARSE_APPEND("%d=", index);
1283 const struct ng_parse_struct_field *const
1284 fields = type->info;
1286 NG_PARSE_APPEND("%s=", fields[index].name);
1290 if ((error = INVOKE(etype, unparse)
1291 (etype, data, off, cbuf, cbuflen)) != 0) {
1292 FREE(workBuf, M_NETGRAPH);
1295 cbuflen -= strlen(cbuf);
1296 cbuf += strlen(cbuf);
1299 FREE(workBuf, M_NETGRAPH);
1301 /* Closing brace/bracket */
1302 NG_PARSE_APPEND("%s%c",
1303 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1308 * Generate the default value for an element of an array or structure
1309 * Returns EOPNOTSUPP if default value is unspecified.
1312 ng_get_composite_elem_default(const struct ng_parse_type *type,
1313 int index, const u_char *const start, u_char *buf, int *buflen,
1314 const enum comptype ctype)
1316 const struct ng_parse_type *etype;
1317 ng_getDefault_t *func;
1324 const struct ng_parse_array_info *const ai = type->info;
1326 if (ai->getDefault != NULL) {
1327 return (*ai->getDefault)(type,
1328 index, start, buf, buflen);
1334 const struct ng_parse_fixedarray_info *const fi = type->info;
1336 if (*fi->getDefault != NULL) {
1337 return (*fi->getDefault)(type,
1338 index, start, buf, buflen);
1343 panic("%s", __func__);
1346 /* Default to element type default */
1347 etype = ng_get_composite_etype(type, index, ctype);
1348 func = METHOD(etype, getDefault);
1350 return (EOPNOTSUPP);
1351 return (*func)(etype, start, buf, buflen);
1355 * Get the number of elements in a struct, variable or fixed array.
1358 ng_get_composite_len(const struct ng_parse_type *type,
1359 const u_char *const start, const u_char *buf,
1360 const enum comptype ctype)
1365 const struct ng_parse_struct_field *const fields = type->info;
1368 for (numFields = 0; ; numFields++) {
1369 const struct ng_parse_struct_field *const
1370 fi = &fields[numFields];
1372 if (fi->name == NULL)
1379 const struct ng_parse_array_info *const ai = type->info;
1381 return (*ai->getLength)(type, start, buf);
1385 const struct ng_parse_fixedarray_info *const fi = type->info;
1390 panic("%s", __func__);
1396 * Return the type of the index'th element of a composite structure
1398 static const struct ng_parse_type *
1399 ng_get_composite_etype(const struct ng_parse_type *type,
1400 int index, const enum comptype ctype)
1402 const struct ng_parse_type *etype = NULL;
1407 const struct ng_parse_struct_field *const fields = type->info;
1409 etype = fields[index].type;
1414 const struct ng_parse_array_info *const ai = type->info;
1416 etype = ai->elementType;
1421 const struct ng_parse_fixedarray_info *const fi = type->info;
1423 etype = fi->elementType;
1427 panic("%s", __func__);
1433 * Get the number of bytes to skip to align for the next
1434 * element in a composite structure.
1437 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1438 int index, enum comptype ctype, int posn)
1440 const struct ng_parse_type *const
1441 etype = ng_get_composite_etype(type, index, ctype);
1444 /* Get element's alignment, and possibly override */
1445 align = ALIGNMENT(etype);
1446 if (ctype == CT_STRUCT) {
1447 const struct ng_parse_struct_field *const fields = type->info;
1449 if (fields[index].alignment != 0)
1450 align = fields[index].alignment;
1453 /* Return number of bytes to skip to align */
1454 return (align ? (align - (posn % align)) % align : 0);
1457 /************************************************************************
1458 PARSING HELPER ROUTINES
1459 ************************************************************************/
1465 ng_parse_skip_value(const char *s, int off0, int *lenp)
1467 int len, nbracket, nbrace;
1470 len = nbracket = nbrace = 0;
1472 switch (ng_parse_get_token(s, &off, &len)) {
1480 if (nbracket-- == 0)
1493 } while (nbracket > 0 || nbrace > 0);
1499 * Find the next token in the string, starting at offset *startp.
1500 * Returns the token type, with *startp pointing to the first char
1501 * and *lenp the length.
1504 ng_parse_get_token(const char *s, int *startp, int *lenp)
1509 while (isspace(s[*startp]))
1511 switch (s[*startp]) {
1531 if ((t = ng_get_string_token(s, startp, lenp)) == NULL)
1533 FREE(t, M_NETGRAPH);
1536 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1537 && s[i] != '{' && s[i] != '}' && s[i] != '['
1538 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1540 *lenp = i - *startp;
1546 * Get a string token, which must be enclosed in double quotes.
1547 * The normal C backslash escapes are recognized.
1550 ng_get_string_token(const char *s, int *startp, int *lenp)
1555 while (isspace(s[*startp]))
1558 if (s[*startp] != '"')
1560 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT);
1563 strcpy(cbuf, s + start + 1);
1564 for (off = 1, p = cbuf; *p != '\0'; off++, p++) {
1569 } else if (p[0] == '\\' && p[1] != '\0') {
1600 case '0': case '1': case '2': case '3':
1601 case '4': case '5': case '6': case '7':
1603 k < 3 && *v >= '0' && *v <= '7'; v++) {
1604 x = (x << 3) + (*v - '0');
1610 for (v++, x = k = 0;
1611 k < 2 && isxdigit(*v); v++) {
1612 x = (x << 4) + (isdigit(*v) ?
1614 (tolower(*v) - 'a' + 10));
1625 return (NULL); /* no closing quote */
1629 * Encode a string so it can be safely put in double quotes.
1630 * Caller must free the result.
1633 ng_encode_string(const char *raw)
1638 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1642 for ( ; *raw != '\0'; raw++) {
1670 if (*raw < 0x20 || *raw > 0x7e) {
1671 off += ksprintf(cbuf + off,
1672 "\\x%02x", (u_char)*raw);
1684 /************************************************************************
1685 VIRTUAL METHOD LOOKUP
1686 ************************************************************************/
1689 ng_get_parse_method(const struct ng_parse_type *t)
1691 while (t != NULL && t->parse == NULL)
1693 return (t ? t->parse : NULL);
1696 static ng_unparse_t *
1697 ng_get_unparse_method(const struct ng_parse_type *t)
1699 while (t != NULL && t->unparse == NULL)
1701 return (t ? t->unparse : NULL);
1704 static ng_getDefault_t *
1705 ng_get_getDefault_method(const struct ng_parse_type *t)
1707 while (t != NULL && t->getDefault == NULL)
1709 return (t ? t->getDefault : NULL);
1712 static ng_getAlign_t *
1713 ng_get_getAlign_method(const struct ng_parse_type *t)
1715 while (t != NULL && t->getAlign == NULL)
1717 return (t ? t->getAlign : NULL);