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 $
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/ctype.h>
50 #include <netinet/in.h>
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include <netgraph/ng_parse.h>
56 /* Compute alignment for primitive integral types */
72 #define INT8_ALIGNMENT 1
73 #define INT16_ALIGNMENT ((uintptr_t)&((struct int16_temp *)0)->y)
74 #define INT32_ALIGNMENT ((uintptr_t)&((struct int32_temp *)0)->y)
75 #define INT64_ALIGNMENT ((uintptr_t)&((struct int64_temp *)0)->y)
77 /* Output format for integral types */
78 #define INT_UNSIGNED 0
82 /* Type of composite object: struct, array, or fixedarray */
89 /* Composite types helper functions */
90 static int ng_parse_composite(const struct ng_parse_type *type,
91 const char *s, int *off, const u_char *start,
92 u_char *const buf, int *buflen, enum comptype ctype);
93 static int ng_unparse_composite(const struct ng_parse_type *type,
94 const u_char *data, int *off, char *cbuf, int cbuflen,
96 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
97 int index, const u_char *start, u_char *buf,
98 int *buflen, enum comptype ctype);
99 static int ng_get_composite_len(const struct ng_parse_type *type,
100 const u_char *start, const u_char *buf,
101 enum comptype ctype);
102 static const struct ng_parse_type *ng_get_composite_etype(const struct
103 ng_parse_type *type, int index, enum comptype ctype);
104 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
105 int index, enum comptype ctype, int posn);
107 /* Parsing helper functions */
108 static int ng_parse_skip_value(const char *s, int off, int *lenp);
110 /* Poor man's virtual method calls */
111 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
112 #define INVOKE(t,m) (*METHOD(t,m))
114 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
115 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
116 static ng_getDefault_t *ng_get_getDefault_method(const
117 struct ng_parse_type *t);
118 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
120 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
121 0 : INVOKE(t, getAlign)(t))
123 /* For converting binary to string */
124 #define NG_PARSE_APPEND(fmt, args...) \
128 len = ksnprintf((cbuf), (cbuflen), \
130 if (len >= (cbuflen)) \
136 /************************************************************************
138 ************************************************************************/
141 * Convert an ASCII string to binary according to the supplied type descriptor
144 ng_parse(const struct ng_parse_type *type,
145 const char *string, int *off, u_char *buf, int *buflen)
147 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
151 * Convert binary to an ASCII string according to the supplied type descriptor
154 ng_unparse(const struct ng_parse_type *type,
155 const u_char *data, char *cbuf, int cbuflen)
159 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
163 * Fill in the default value according to the supplied type descriptor
166 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
168 ng_getDefault_t *const func = METHOD(type, getDefault);
172 return (*func)(type, buf, buf, buflen);
176 /************************************************************************
178 ************************************************************************/
181 ng_struct_parse(const struct ng_parse_type *type,
182 const char *s, int *off, const u_char *const start,
183 u_char *const buf, int *buflen)
185 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
189 ng_struct_unparse(const struct ng_parse_type *type,
190 const u_char *data, int *off, char *cbuf, int cbuflen)
192 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
196 ng_struct_getDefault(const struct ng_parse_type *type,
197 const u_char *const start, u_char *buf, int *buflen)
201 return ng_parse_composite(type,
202 "{}", &off, start, buf, buflen, CT_STRUCT);
206 ng_struct_getAlign(const struct ng_parse_type *type)
208 const struct ng_parse_struct_field *field;
211 for (field = type->info; field->name != NULL; field++) {
212 int falign = ALIGNMENT(field->type);
220 const struct ng_parse_type ng_parse_struct_type = {
226 ng_struct_getDefault,
230 /************************************************************************
231 FIXED LENGTH ARRAY TYPE
232 ************************************************************************/
235 ng_fixedarray_parse(const struct ng_parse_type *type,
236 const char *s, int *off, const u_char *const start,
237 u_char *const buf, int *buflen)
239 return ng_parse_composite(type,
240 s, off, start, buf, buflen, CT_FIXEDARRAY);
244 ng_fixedarray_unparse(const struct ng_parse_type *type,
245 const u_char *data, int *off, char *cbuf, int cbuflen)
247 return ng_unparse_composite(type,
248 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
252 ng_fixedarray_getDefault(const struct ng_parse_type *type,
253 const u_char *const start, u_char *buf, int *buflen)
257 return ng_parse_composite(type,
258 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
262 ng_fixedarray_getAlign(const struct ng_parse_type *type)
264 const struct ng_parse_fixedarray_info *fi = type->info;
266 return ALIGNMENT(fi->elementType);
269 const struct ng_parse_type ng_parse_fixedarray_type = {
274 ng_fixedarray_unparse,
275 ng_fixedarray_getDefault,
276 ng_fixedarray_getAlign
279 /************************************************************************
280 VARIABLE LENGTH ARRAY TYPE
281 ************************************************************************/
284 ng_array_parse(const struct ng_parse_type *type,
285 const char *s, int *off, const u_char *const start,
286 u_char *const buf, int *buflen)
288 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
292 ng_array_unparse(const struct ng_parse_type *type,
293 const u_char *data, int *off, char *cbuf, int cbuflen)
295 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
299 ng_array_getDefault(const struct ng_parse_type *type,
300 const u_char *const start, u_char *buf, int *buflen)
304 return ng_parse_composite(type,
305 "[]", &off, start, buf, buflen, CT_ARRAY);
309 ng_array_getAlign(const struct ng_parse_type *type)
311 const struct ng_parse_array_info *ai = type->info;
313 return ALIGNMENT(ai->elementType);
316 const struct ng_parse_type ng_parse_array_type = {
326 /************************************************************************
328 ************************************************************************/
331 ng_int8_parse(const struct ng_parse_type *type,
332 const char *s, int *off, const u_char *const start,
333 u_char *const buf, int *buflen)
339 val = strtol(s + *off, &eptr, 0);
340 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
344 bcopy(&val8, buf, sizeof(int8_t));
345 *buflen = sizeof(int8_t);
350 ng_int8_unparse(const struct ng_parse_type *type,
351 const u_char *data, int *off, char *cbuf, int cbuflen)
357 bcopy(data + *off, &val, sizeof(int8_t));
358 switch ((int)(intptr_t)type->info) {
365 fval = (u_int8_t)val;
369 fval = (u_int8_t)val;
372 panic("%s: unknown type", __func__);
374 NG_PARSE_APPEND(fmt, fval);
375 *off += sizeof(int8_t);
380 ng_int8_getDefault(const struct ng_parse_type *type,
381 const u_char *const start, u_char *buf, int *buflen)
385 if (*buflen < sizeof(int8_t))
388 bcopy(&val, buf, sizeof(int8_t));
389 *buflen = sizeof(int8_t);
394 ng_int8_getAlign(const struct ng_parse_type *type)
396 return INT8_ALIGNMENT;
399 const struct ng_parse_type ng_parse_int8_type = {
409 const struct ng_parse_type ng_parse_uint8_type = {
414 const struct ng_parse_type ng_parse_hint8_type = {
419 /************************************************************************
421 ************************************************************************/
424 ng_int16_parse(const struct ng_parse_type *type,
425 const char *s, int *off, const u_char *const start,
426 u_char *const buf, int *buflen)
432 val = strtol(s + *off, &eptr, 0);
433 if (val < (int16_t)0x8000
434 || val > (u_int16_t)0xffff || eptr == s + *off)
437 val16 = (int16_t)val;
438 bcopy(&val16, buf, sizeof(int16_t));
439 *buflen = sizeof(int16_t);
444 ng_int16_unparse(const struct ng_parse_type *type,
445 const u_char *data, int *off, char *cbuf, int cbuflen)
451 bcopy(data + *off, &val, sizeof(int16_t));
452 switch ((int)(intptr_t)type->info) {
459 fval = (u_int16_t)val;
463 fval = (u_int16_t)val;
466 panic("%s: unknown type", __func__);
468 NG_PARSE_APPEND(fmt, fval);
469 *off += sizeof(int16_t);
474 ng_int16_getDefault(const struct ng_parse_type *type,
475 const u_char *const start, u_char *buf, int *buflen)
479 if (*buflen < sizeof(int16_t))
482 bcopy(&val, buf, sizeof(int16_t));
483 *buflen = sizeof(int16_t);
488 ng_int16_getAlign(const struct ng_parse_type *type)
490 return INT16_ALIGNMENT;
493 const struct ng_parse_type ng_parse_int16_type = {
503 const struct ng_parse_type ng_parse_uint16_type = {
504 &ng_parse_int16_type,
508 const struct ng_parse_type ng_parse_hint16_type = {
509 &ng_parse_int16_type,
513 /************************************************************************
515 ************************************************************************/
518 ng_int32_parse(const struct ng_parse_type *type,
519 const char *s, int *off, const u_char *const start,
520 u_char *const buf, int *buflen)
522 long val; /* assumes long is at least 32 bits */
526 val = strtol(s + *off, &eptr, 0);
527 if (val < (int32_t)0x80000000
528 || val > (u_int32_t)0xffffffff || eptr == s + *off)
531 val32 = (int32_t)val;
532 bcopy(&val32, buf, sizeof(int32_t));
533 *buflen = sizeof(int32_t);
538 ng_int32_unparse(const struct ng_parse_type *type,
539 const u_char *data, int *off, char *cbuf, int cbuflen)
545 bcopy(data + *off, &val, sizeof(int32_t));
546 switch ((int)(intptr_t)type->info) {
553 fval = (u_int32_t)val;
557 fval = (u_int32_t)val;
560 panic("%s: unknown type", __func__);
562 NG_PARSE_APPEND(fmt, fval);
563 *off += sizeof(int32_t);
568 ng_int32_getDefault(const struct ng_parse_type *type,
569 const u_char *const start, u_char *buf, int *buflen)
573 if (*buflen < sizeof(int32_t))
576 bcopy(&val, buf, sizeof(int32_t));
577 *buflen = sizeof(int32_t);
582 ng_int32_getAlign(const struct ng_parse_type *type)
584 return INT32_ALIGNMENT;
587 const struct ng_parse_type ng_parse_int32_type = {
597 const struct ng_parse_type ng_parse_uint32_type = {
598 &ng_parse_int32_type,
602 const struct ng_parse_type ng_parse_hint32_type = {
603 &ng_parse_int32_type,
607 /************************************************************************
609 ************************************************************************/
612 ng_int64_parse(const struct ng_parse_type *type,
613 const char *s, int *off, const u_char *const start,
614 u_char *const buf, int *buflen)
620 val = strtoq(s + *off, &eptr, 0);
621 if (eptr == s + *off)
624 val64 = (int64_t)val;
625 bcopy(&val64, buf, sizeof(int64_t));
626 *buflen = sizeof(int64_t);
631 ng_int64_unparse(const struct ng_parse_type *type,
632 const u_char *data, int *off, char *cbuf, int cbuflen)
638 bcopy(data + *off, &val, sizeof(int64_t));
639 switch ((int)(intptr_t)type->info) {
646 fval = (u_int64_t)val;
650 fval = (u_int64_t)val;
653 panic("%s: unknown type", __func__);
655 NG_PARSE_APPEND(fmt, fval);
656 *off += sizeof(int64_t);
661 ng_int64_getDefault(const struct ng_parse_type *type,
662 const u_char *const start, u_char *buf, int *buflen)
666 if (*buflen < sizeof(int64_t))
669 bcopy(&val, buf, sizeof(int64_t));
670 *buflen = sizeof(int64_t);
675 ng_int64_getAlign(const struct ng_parse_type *type)
677 return INT64_ALIGNMENT;
680 const struct ng_parse_type ng_parse_int64_type = {
690 const struct ng_parse_type ng_parse_uint64_type = {
691 &ng_parse_int64_type,
695 const struct ng_parse_type ng_parse_hint64_type = {
696 &ng_parse_int64_type,
700 /************************************************************************
702 ************************************************************************/
705 ng_string_parse(const struct ng_parse_type *type,
706 const char *s, int *off, const u_char *const start,
707 u_char *const buf, int *buflen)
712 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
715 len = strlen(sval) + 1;
716 bcopy(sval, buf, len);
717 kfree(sval, M_NETGRAPH);
723 ng_string_unparse(const struct ng_parse_type *type,
724 const u_char *data, int *off, char *cbuf, int cbuflen)
726 const char *const raw = (const char *)data + *off;
727 char *const s = ng_encode_string(raw);
731 NG_PARSE_APPEND("%s", s);
732 *off += strlen(raw) + 1;
733 kfree(s, M_NETGRAPH);
738 ng_string_getDefault(const struct ng_parse_type *type,
739 const u_char *const start, u_char *buf, int *buflen)
744 buf[0] = (u_char)'\0';
749 const struct ng_parse_type ng_parse_string_type = {
755 ng_string_getDefault,
759 /************************************************************************
760 FIXED BUFFER STRING TYPE
761 ************************************************************************/
764 ng_fixedstring_parse(const struct ng_parse_type *type,
765 const char *s, int *off, const u_char *const start,
766 u_char *const buf, int *buflen)
768 const struct ng_parse_fixedstring_info *const fi = type->info;
772 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
774 if (strlen(sval) + 1 > fi->bufSize)
777 len = strlen(sval) + 1;
778 bcopy(sval, buf, len);
779 kfree(sval, M_NETGRAPH);
780 bzero(buf + len, fi->bufSize - len);
781 *buflen = fi->bufSize;
786 ng_fixedstring_unparse(const struct ng_parse_type *type,
787 const u_char *data, int *off, char *cbuf, int cbuflen)
789 const struct ng_parse_fixedstring_info *const fi = type->info;
790 int error, temp = *off;
792 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
799 ng_fixedstring_getDefault(const struct ng_parse_type *type,
800 const u_char *const start, u_char *buf, int *buflen)
802 const struct ng_parse_fixedstring_info *const fi = type->info;
804 if (*buflen < fi->bufSize)
806 bzero(buf, fi->bufSize);
807 *buflen = fi->bufSize;
811 const struct ng_parse_type ng_parse_fixedstring_type = {
815 ng_fixedstring_parse,
816 ng_fixedstring_unparse,
817 ng_fixedstring_getDefault,
821 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
824 const struct ng_parse_type ng_parse_nodebuf_type = {
825 &ng_parse_fixedstring_type,
826 &ng_parse_nodebuf_info
829 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
832 const struct ng_parse_type ng_parse_hookbuf_type = {
833 &ng_parse_fixedstring_type,
834 &ng_parse_hookbuf_info
837 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
840 const struct ng_parse_type ng_parse_pathbuf_type = {
841 &ng_parse_fixedstring_type,
842 &ng_parse_pathbuf_info
845 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
848 const struct ng_parse_type ng_parse_typebuf_type = {
849 &ng_parse_fixedstring_type,
850 &ng_parse_typebuf_info
853 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
856 const struct ng_parse_type ng_parse_cmdbuf_type = {
857 &ng_parse_fixedstring_type,
858 &ng_parse_cmdbuf_info
861 /************************************************************************
863 ************************************************************************/
866 ng_ipaddr_parse(const struct ng_parse_type *type,
867 const char *s, int *off, const u_char *const start,
868 u_char *const buf, int *buflen)
872 for (i = 0; i < 4; i++) {
873 if ((error = ng_int8_parse(&ng_parse_int8_type,
874 s, off, start, buf + i, buflen)) != 0)
876 if (i < 3 && s[*off] != '.')
885 ng_ipaddr_unparse(const struct ng_parse_type *type,
886 const u_char *data, int *off, char *cbuf, int cbuflen)
890 bcopy(data + *off, &ip, sizeof(ip));
891 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
892 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
898 ng_ipaddr_getDefault(const struct ng_parse_type *type,
899 const u_char *const start, u_char *buf, int *buflen)
901 struct in_addr ip = { 0 };
903 if (*buflen < sizeof(ip))
905 bcopy(&ip, buf, sizeof(ip));
906 *buflen = sizeof(ip);
910 const struct ng_parse_type ng_parse_ipaddr_type = {
916 ng_ipaddr_getDefault,
920 /************************************************************************
922 ************************************************************************/
924 /* Get the length of a byte array */
926 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
927 const u_char *start, const u_char *buf)
929 ng_parse_array_getLength_t *const getLength = type->private;
931 return (*getLength)(type, start, buf);
934 /* Byte array element type is hex int8 */
935 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
936 &ng_parse_hint8_type,
937 &ng_parse_bytearray_subtype_getLength,
940 static const struct ng_parse_type ng_parse_bytearray_subtype = {
941 &ng_parse_array_type,
942 &ng_parse_bytearray_subtype_info
946 ng_bytearray_parse(const struct ng_parse_type *type,
947 const char *s, int *off, const u_char *const start,
948 u_char *const buf, int *buflen)
953 /* We accept either an array of bytes or a string constant */
954 if ((str = ng_get_string_token(s, off, &toklen)) != NULL) {
955 ng_parse_array_getLength_t *const getLength = type->info;
958 arraylen = (*getLength)(type, start, buf);
959 if (arraylen > *buflen) {
960 kfree(str, M_NETGRAPH);
963 slen = strlen(str) + 1;
964 if (slen > arraylen) {
965 kfree(str, M_NETGRAPH);
968 bcopy(str, buf, slen);
969 bzero(buf + slen, arraylen - slen);
970 kfree(str, M_NETGRAPH);
975 struct ng_parse_type subtype;
977 subtype = ng_parse_bytearray_subtype;
978 *(const void **)(void *)&subtype.private = type->info;
979 return ng_array_parse(&subtype, s, off, start, buf, buflen);
984 ng_bytearray_unparse(const struct ng_parse_type *type,
985 const u_char *data, int *off, char *cbuf, int cbuflen)
987 struct ng_parse_type subtype;
989 subtype = ng_parse_bytearray_subtype;
990 *(const void **)(void *)&subtype.private = type->info;
991 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
995 ng_bytearray_getDefault(const struct ng_parse_type *type,
996 const u_char *const start, u_char *buf, int *buflen)
998 struct ng_parse_type subtype;
1000 subtype = ng_parse_bytearray_subtype;
1001 *(const void **)(void *)&subtype.private = type->info;
1002 return ng_array_getDefault(&subtype, start, buf, buflen);
1005 const struct ng_parse_type ng_parse_bytearray_type = {
1010 ng_bytearray_unparse,
1011 ng_bytearray_getDefault,
1015 /************************************************************************
1017 ************************************************************************/
1019 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1021 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1022 const u_char *start, const u_char *buf)
1024 const struct ng_mesg *msg;
1026 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1027 return msg->header.arglen;
1030 /* Type for the variable length data portion of a struct ng_mesg */
1031 static const struct ng_parse_type ng_msg_data_type = {
1032 &ng_parse_bytearray_type,
1033 &ng_parse_ng_mesg_getLength
1036 /* Type for the entire struct ng_mesg header with data section */
1037 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1038 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1039 const struct ng_parse_type ng_parse_ng_mesg_type = {
1040 &ng_parse_struct_type,
1041 &ng_parse_ng_mesg_type_fields,
1044 /************************************************************************
1045 COMPOSITE HELPER ROUTINES
1046 ************************************************************************/
1049 * Convert a structure or array from ASCII to binary
1052 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1053 int *off, const u_char *const start, u_char *const buf, int *buflen,
1054 const enum comptype ctype)
1056 const int num = ng_get_composite_len(type, start, buf, ctype);
1057 int nextIndex = 0; /* next implicit array index */
1058 u_int index; /* field or element index */
1059 int *foff; /* field value offsets in string */
1060 int align, len, blen, error = 0;
1063 foff = kmalloc(num * sizeof(*foff), M_NETGRAPH, M_NOWAIT | M_ZERO);
1069 /* Get opening brace/bracket */
1070 if (ng_parse_get_token(s, off, &len)
1071 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1077 /* Get individual element value positions in the string */
1079 enum ng_parse_token tok;
1081 /* Check for closing brace/bracket */
1082 tok = ng_parse_get_token(s, off, &len);
1083 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1088 /* For arrays, the 'name' (ie, index) is optional, so
1089 distinguish name from values by seeing if the next
1090 token is an equals sign */
1091 if (ctype != CT_STRUCT) {
1095 /* If an opening brace/bracket, index is implied */
1096 if (tok == T_LBRACE || tok == T_LBRACKET) {
1097 index = nextIndex++;
1101 /* Might be an index, might be a value, either way... */
1102 if (tok != T_WORD) {
1107 /* If no equals sign follows, index is implied */
1109 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1110 index = nextIndex++;
1114 /* Index was specified explicitly; parse it */
1115 index = (u_int)strtoul(s + *off, &eptr, 0);
1116 if (index < 0 || eptr - (s + *off) != len) {
1120 nextIndex = index + 1;
1123 ; /* avoid gcc3.x warning */
1124 } else { /* a structure field */
1125 const struct ng_parse_struct_field *const
1126 fields = type->info;
1128 /* Find the field by name (required) in field list */
1129 if (tok != T_WORD) {
1133 for (index = 0; index < num; index++) {
1134 const struct ng_parse_struct_field *const
1135 field = &fields[index];
1137 if (strncmp(&s[*off], field->name, len) == 0
1138 && field->name[len] == '\0')
1147 /* Get equals sign */
1148 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1155 /* Check array index */
1161 /* Save value's position and skip over it for now */
1162 if (foff[index] != 0) {
1163 error = EALREADY; /* duplicate */
1166 while (isspace(s[*off]))
1169 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1174 /* Now build binary structure from supplied values and defaults */
1175 for (blen = index = 0; index < num; index++) {
1176 const struct ng_parse_type *const
1177 etype = ng_get_composite_etype(type, index, ctype);
1180 /* Zero-pad any alignment bytes */
1181 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1182 for (k = 0; k < pad; k++) {
1183 if (blen >= *buflen) {
1191 vlen = *buflen - blen;
1192 if (foff[index] == 0) { /* use default value */
1193 error = ng_get_composite_elem_default(type, index,
1194 start, buf + blen, &vlen, ctype);
1195 } else { /* parse given value */
1197 error = INVOKE(etype, parse)(etype,
1198 s, off, start, buf + blen, &vlen);
1205 /* Make total composite structure size a multiple of its alignment */
1206 if ((align = ALIGNMENT(type)) != 0) {
1207 while (blen % align != 0) {
1208 if (blen >= *buflen) {
1220 kfree(foff, M_NETGRAPH);
1225 * Convert an array or structure from binary to ASCII
1228 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1229 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1231 const struct ng_mesg *const hdr
1232 = (const struct ng_mesg *)(data - sizeof(*hdr));
1233 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1234 const int workSize = 20 * 1024; /* XXX hard coded constant */
1235 int nextIndex = 0, didOne = 0;
1239 /* Get workspace for checking default values */
1240 workBuf = kmalloc(workSize, M_NETGRAPH, M_NOWAIT);
1241 if (workBuf == NULL)
1244 /* Opening brace/bracket */
1245 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1248 for (index = 0; index < num; index++) {
1249 const struct ng_parse_type *const
1250 etype = ng_get_composite_etype(type, index, ctype);
1252 /* Skip any alignment pad bytes */
1253 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1256 * See if element is equal to its default value; skip if so.
1257 * Copy struct ng_mesg header for types that peek into it.
1259 if (sizeof(*hdr) + *off < workSize) {
1260 int tempsize = workSize - sizeof(*hdr) - *off;
1262 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1263 if (ng_get_composite_elem_default(type, index, workBuf
1264 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1265 &tempsize, ctype) == 0
1266 && bcmp(workBuf + sizeof(*hdr) + *off,
1267 data + *off, tempsize) == 0) {
1274 NG_PARSE_APPEND(" ");
1275 if (ctype != CT_STRUCT) {
1276 if (index != nextIndex) {
1278 NG_PARSE_APPEND("%d=", index);
1282 const struct ng_parse_struct_field *const
1283 fields = type->info;
1285 NG_PARSE_APPEND("%s=", fields[index].name);
1289 if ((error = INVOKE(etype, unparse)
1290 (etype, data, off, cbuf, cbuflen)) != 0) {
1291 kfree(workBuf, M_NETGRAPH);
1294 cbuflen -= strlen(cbuf);
1295 cbuf += strlen(cbuf);
1298 kfree(workBuf, M_NETGRAPH);
1300 /* Closing brace/bracket */
1301 NG_PARSE_APPEND("%s%c",
1302 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1307 * Generate the default value for an element of an array or structure
1308 * Returns EOPNOTSUPP if default value is unspecified.
1311 ng_get_composite_elem_default(const struct ng_parse_type *type,
1312 int index, const u_char *const start, u_char *buf, int *buflen,
1313 const enum comptype ctype)
1315 const struct ng_parse_type *etype;
1316 ng_getDefault_t *func;
1323 const struct ng_parse_array_info *const ai = type->info;
1325 if (ai->getDefault != NULL) {
1326 return (*ai->getDefault)(type,
1327 index, start, buf, buflen);
1333 const struct ng_parse_fixedarray_info *const fi = type->info;
1335 if (*fi->getDefault != NULL) {
1336 return (*fi->getDefault)(type,
1337 index, start, buf, buflen);
1342 panic("%s", __func__);
1345 /* Default to element type default */
1346 etype = ng_get_composite_etype(type, index, ctype);
1347 func = METHOD(etype, getDefault);
1349 return (EOPNOTSUPP);
1350 return (*func)(etype, start, buf, buflen);
1354 * Get the number of elements in a struct, variable or fixed array.
1357 ng_get_composite_len(const struct ng_parse_type *type,
1358 const u_char *const start, const u_char *buf,
1359 const enum comptype ctype)
1364 const struct ng_parse_struct_field *const fields = type->info;
1367 for (numFields = 0; ; numFields++) {
1368 const struct ng_parse_struct_field *const
1369 fi = &fields[numFields];
1371 if (fi->name == NULL)
1378 const struct ng_parse_array_info *const ai = type->info;
1380 return (*ai->getLength)(type, start, buf);
1384 const struct ng_parse_fixedarray_info *const fi = type->info;
1389 panic("%s", __func__);
1395 * Return the type of the index'th element of a composite structure
1397 static const struct ng_parse_type *
1398 ng_get_composite_etype(const struct ng_parse_type *type,
1399 int index, const enum comptype ctype)
1401 const struct ng_parse_type *etype = NULL;
1406 const struct ng_parse_struct_field *const fields = type->info;
1408 etype = fields[index].type;
1413 const struct ng_parse_array_info *const ai = type->info;
1415 etype = ai->elementType;
1420 const struct ng_parse_fixedarray_info *const fi = type->info;
1422 etype = fi->elementType;
1426 panic("%s", __func__);
1432 * Get the number of bytes to skip to align for the next
1433 * element in a composite structure.
1436 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1437 int index, enum comptype ctype, int posn)
1439 const struct ng_parse_type *const
1440 etype = ng_get_composite_etype(type, index, ctype);
1443 /* Get element's alignment, and possibly override */
1444 align = ALIGNMENT(etype);
1445 if (ctype == CT_STRUCT) {
1446 const struct ng_parse_struct_field *const fields = type->info;
1448 if (fields[index].alignment != 0)
1449 align = fields[index].alignment;
1452 /* Return number of bytes to skip to align */
1453 return (align ? (align - (posn % align)) % align : 0);
1456 /************************************************************************
1457 PARSING HELPER ROUTINES
1458 ************************************************************************/
1464 ng_parse_skip_value(const char *s, int off0, int *lenp)
1466 int len, nbracket, nbrace;
1469 len = nbracket = nbrace = 0;
1471 switch (ng_parse_get_token(s, &off, &len)) {
1479 if (nbracket-- == 0)
1492 } while (nbracket > 0 || nbrace > 0);
1498 * Find the next token in the string, starting at offset *startp.
1499 * Returns the token type, with *startp pointing to the first char
1500 * and *lenp the length.
1503 ng_parse_get_token(const char *s, int *startp, int *lenp)
1508 while (isspace(s[*startp]))
1510 switch (s[*startp]) {
1530 if ((t = ng_get_string_token(s, startp, lenp)) == NULL)
1532 kfree(t, M_NETGRAPH);
1535 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1536 && s[i] != '{' && s[i] != '}' && s[i] != '['
1537 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1539 *lenp = i - *startp;
1545 * Get a string token, which must be enclosed in double quotes.
1546 * The normal C backslash escapes are recognized.
1549 ng_get_string_token(const char *s, int *startp, int *lenp)
1554 while (isspace(s[*startp]))
1557 if (s[*startp] != '"')
1559 cbuf = kmalloc(strlen(s + start), M_NETGRAPH, M_NOWAIT);
1562 strcpy(cbuf, s + start + 1);
1563 for (off = 1, p = cbuf; *p != '\0'; off++, p++) {
1568 } else if (p[0] == '\\' && p[1] != '\0') {
1599 case '0': case '1': case '2': case '3':
1600 case '4': case '5': case '6': case '7':
1602 k < 3 && *v >= '0' && *v <= '7'; v++) {
1603 x = (x << 3) + (*v - '0');
1609 for (v++, x = k = 0;
1610 k < 2 && isxdigit(*v); v++) {
1611 x = (x << 4) + (isdigit(*v) ?
1613 (tolower(*v) - 'a' + 10));
1624 return (NULL); /* no closing quote */
1628 * Encode a string so it can be safely put in double quotes.
1629 * Caller must free the result.
1632 ng_encode_string(const char *raw)
1637 cbuf = kmalloc(strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1641 for ( ; *raw != '\0'; raw++) {
1669 if (*raw < 0x20 || *raw > 0x7e) {
1670 off += ksprintf(cbuf + off,
1671 "\\x%02x", (u_char)*raw);
1683 /************************************************************************
1684 VIRTUAL METHOD LOOKUP
1685 ************************************************************************/
1688 ng_get_parse_method(const struct ng_parse_type *t)
1690 while (t != NULL && t->parse == NULL)
1692 return (t ? t->parse : NULL);
1695 static ng_unparse_t *
1696 ng_get_unparse_method(const struct ng_parse_type *t)
1698 while (t != NULL && t->unparse == NULL)
1700 return (t ? t->unparse : NULL);
1703 static ng_getDefault_t *
1704 ng_get_getDefault_method(const struct ng_parse_type *t)
1706 while (t != NULL && t->getDefault == NULL)
1708 return (t ? t->getDefault : NULL);
1711 static ng_getAlign_t *
1712 ng_get_getAlign_method(const struct ng_parse_type *t)
1714 while (t != NULL && t->getAlign == NULL)
1716 return (t ? t->getAlign : NULL);