xref: /freebsd/sys/netgraph/ng_parse.c (revision 27121ab1a472e7240f114938aa6e35b314d6e714)
1f8307e12SArchie Cobbs 
2f8307e12SArchie Cobbs /*
3f8307e12SArchie Cobbs  * ng_parse.c
4f8307e12SArchie Cobbs  *
5f8307e12SArchie Cobbs  * Copyright (c) 1999 Whistle Communications, Inc.
6f8307e12SArchie Cobbs  * All rights reserved.
7f8307e12SArchie Cobbs  *
8f8307e12SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9f8307e12SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10f8307e12SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
11f8307e12SArchie Cobbs  * provided, however, that:
12f8307e12SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13f8307e12SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
14f8307e12SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
15f8307e12SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
16f8307e12SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17f8307e12SArchie Cobbs  *    such appears in the above copyright notice or in the software.
18f8307e12SArchie Cobbs  *
19f8307e12SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20f8307e12SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21f8307e12SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22f8307e12SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23f8307e12SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24f8307e12SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25f8307e12SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26f8307e12SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27f8307e12SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28f8307e12SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29f8307e12SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30f8307e12SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31f8307e12SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32f8307e12SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33f8307e12SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34f8307e12SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35f8307e12SArchie Cobbs  * OF SUCH DAMAGE.
36f8307e12SArchie Cobbs  *
37cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
38f8307e12SArchie Cobbs  *
39f8307e12SArchie Cobbs  * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
40f8307e12SArchie Cobbs  * $FreeBSD$
41f8307e12SArchie Cobbs  */
42f8307e12SArchie Cobbs 
43f8307e12SArchie Cobbs #include <sys/types.h>
44f8307e12SArchie Cobbs #include <sys/param.h>
45f8307e12SArchie Cobbs #include <sys/systm.h>
46f8307e12SArchie Cobbs #include <sys/errno.h>
47f8307e12SArchie Cobbs #include <sys/malloc.h>
48f8307e12SArchie Cobbs #include <sys/ctype.h>
49f8307e12SArchie Cobbs 
50f8307e12SArchie Cobbs #include <netinet/in.h>
51f8307e12SArchie Cobbs 
52f8307e12SArchie Cobbs #include <netgraph/ng_message.h>
53f8307e12SArchie Cobbs #include <netgraph/netgraph.h>
54f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
55f8307e12SArchie Cobbs 
56f8307e12SArchie Cobbs /* Compute alignment for primitive integral types */
57f8307e12SArchie Cobbs struct int16_temp {
58f8307e12SArchie Cobbs 	char	x;
59f8307e12SArchie Cobbs 	int16_t	y;
60f8307e12SArchie Cobbs };
61f8307e12SArchie Cobbs 
62f8307e12SArchie Cobbs struct int32_temp {
63f8307e12SArchie Cobbs 	char	x;
64f8307e12SArchie Cobbs 	int32_t	y;
65f8307e12SArchie Cobbs };
66f8307e12SArchie Cobbs 
67f8307e12SArchie Cobbs struct int64_temp {
68f8307e12SArchie Cobbs 	char	x;
69f8307e12SArchie Cobbs 	int64_t	y;
70f8307e12SArchie Cobbs };
71f8307e12SArchie Cobbs 
72f8307e12SArchie Cobbs #define INT8_ALIGNMENT		1
73f8307e12SArchie Cobbs #define INT16_ALIGNMENT		((int)&((struct int16_temp *)0)->y)
74f8307e12SArchie Cobbs #define INT32_ALIGNMENT		((int)&((struct int32_temp *)0)->y)
75f8307e12SArchie Cobbs #define INT64_ALIGNMENT		((int)&((struct int64_temp *)0)->y)
76f8307e12SArchie Cobbs 
778db3c6cdSArchie Cobbs /* Output format for integral types */
788db3c6cdSArchie Cobbs #define INT_UNSIGNED		0
798db3c6cdSArchie Cobbs #define INT_SIGNED		1
808db3c6cdSArchie Cobbs #define INT_HEX			2
818db3c6cdSArchie Cobbs 
82f8307e12SArchie Cobbs /* Type of composite object: struct, array, or fixedarray */
83f8307e12SArchie Cobbs enum comptype {
84f8307e12SArchie Cobbs 	CT_STRUCT,
85f8307e12SArchie Cobbs 	CT_ARRAY,
86f8307e12SArchie Cobbs 	CT_FIXEDARRAY,
87f8307e12SArchie Cobbs };
88f8307e12SArchie Cobbs 
89f8307e12SArchie Cobbs /* Composite types helper functions */
90f8307e12SArchie Cobbs static int	ng_parse_composite(const struct ng_parse_type *type,
91f8307e12SArchie Cobbs 			const char *s, int *off, const u_char *start,
92f8307e12SArchie Cobbs 			u_char *const buf, int *buflen, enum comptype ctype);
93f8307e12SArchie Cobbs static int	ng_unparse_composite(const struct ng_parse_type *type,
94f8307e12SArchie Cobbs 			const u_char *data, int *off, char *cbuf, int cbuflen,
95f8307e12SArchie Cobbs 			enum comptype ctype);
96f8307e12SArchie Cobbs static int	ng_get_composite_elem_default(const struct ng_parse_type *type,
97f8307e12SArchie Cobbs 			int index, const u_char *start, u_char *buf,
98f8307e12SArchie Cobbs 			int *buflen, enum comptype ctype);
99f8307e12SArchie Cobbs static int	ng_get_composite_len(const struct ng_parse_type *type,
100f8307e12SArchie Cobbs 			const u_char *start, const u_char *buf,
101f8307e12SArchie Cobbs 			enum comptype ctype);
102f8307e12SArchie Cobbs static const	struct ng_parse_type *ng_get_composite_etype(const struct
103f8307e12SArchie Cobbs 			ng_parse_type *type, int index, enum comptype ctype);
104f8307e12SArchie Cobbs static int	ng_parse_get_elem_pad(const struct ng_parse_type *type,
105f8307e12SArchie Cobbs 			int index, enum comptype ctype, int posn);
106f8307e12SArchie Cobbs 
107f8307e12SArchie Cobbs /* Parsing helper functions */
108f8307e12SArchie Cobbs static int	ng_parse_skip_value(const char *s, int off, int *lenp);
109f8307e12SArchie Cobbs 
110f8307e12SArchie Cobbs /* Poor man's virtual method calls */
111f8307e12SArchie Cobbs #define METHOD(t,m)	(ng_get_ ## m ## _method(t))
112f8307e12SArchie Cobbs #define INVOKE(t,m)	(*METHOD(t,m))
113f8307e12SArchie Cobbs 
114f8307e12SArchie Cobbs static ng_parse_t	*ng_get_parse_method(const struct ng_parse_type *t);
115f8307e12SArchie Cobbs static ng_unparse_t	*ng_get_unparse_method(const struct ng_parse_type *t);
116f8307e12SArchie Cobbs static ng_getDefault_t	*ng_get_getDefault_method(const
117f8307e12SArchie Cobbs 				struct ng_parse_type *t);
118f8307e12SArchie Cobbs static ng_getAlign_t	*ng_get_getAlign_method(const struct ng_parse_type *t);
119f8307e12SArchie Cobbs 
120f8307e12SArchie Cobbs #define ALIGNMENT(t)	(METHOD(t, getAlign) == NULL ? \
121f8307e12SArchie Cobbs 				0 : INVOKE(t, getAlign)(t))
122f8307e12SArchie Cobbs 
123f8307e12SArchie Cobbs /* For converting binary to string */
124f8307e12SArchie Cobbs #define NG_PARSE_APPEND(fmt, args...)				\
125f8307e12SArchie Cobbs 		do {						\
126f8307e12SArchie Cobbs 			int len;				\
127f8307e12SArchie Cobbs 								\
128f8307e12SArchie Cobbs 			len = snprintf((cbuf), (cbuflen),	\
129f8307e12SArchie Cobbs 				fmt , ## args);			\
130f8307e12SArchie Cobbs 			if (len >= (cbuflen))			\
131f8307e12SArchie Cobbs 				return (ERANGE);		\
132f8307e12SArchie Cobbs 			(cbuf) += len;				\
133f8307e12SArchie Cobbs 			(cbuflen) -= len;			\
134f8307e12SArchie Cobbs 		} while (0)
135f8307e12SArchie Cobbs 
136f8307e12SArchie Cobbs /************************************************************************
137f8307e12SArchie Cobbs 			PUBLIC FUNCTIONS
138f8307e12SArchie Cobbs  ************************************************************************/
139f8307e12SArchie Cobbs 
140f8307e12SArchie Cobbs /*
141f8307e12SArchie Cobbs  * Convert an ASCII string to binary according to the supplied type descriptor
142f8307e12SArchie Cobbs  */
143f8307e12SArchie Cobbs int
144f8307e12SArchie Cobbs ng_parse(const struct ng_parse_type *type,
145f8307e12SArchie Cobbs 	const char *string, int *off, u_char *buf, int *buflen)
146f8307e12SArchie Cobbs {
147f8307e12SArchie Cobbs 	return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
148f8307e12SArchie Cobbs }
149f8307e12SArchie Cobbs 
150f8307e12SArchie Cobbs /*
151f8307e12SArchie Cobbs  * Convert binary to an ASCII string according to the supplied type descriptor
152f8307e12SArchie Cobbs  */
153f8307e12SArchie Cobbs int
154f8307e12SArchie Cobbs ng_unparse(const struct ng_parse_type *type,
155f8307e12SArchie Cobbs 	const u_char *data, char *cbuf, int cbuflen)
156f8307e12SArchie Cobbs {
157f8307e12SArchie Cobbs 	int off = 0;
158f8307e12SArchie Cobbs 
159f8307e12SArchie Cobbs 	return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
160f8307e12SArchie Cobbs }
161f8307e12SArchie Cobbs 
162f8307e12SArchie Cobbs /*
163f8307e12SArchie Cobbs  * Fill in the default value according to the supplied type descriptor
164f8307e12SArchie Cobbs  */
165f8307e12SArchie Cobbs int
166f8307e12SArchie Cobbs ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
167f8307e12SArchie Cobbs {
168f8307e12SArchie Cobbs 	ng_getDefault_t *const func = METHOD(type, getDefault);
169f8307e12SArchie Cobbs 
170f8307e12SArchie Cobbs 	if (func == NULL)
171f8307e12SArchie Cobbs 		return (EOPNOTSUPP);
172f8307e12SArchie Cobbs 	return (*func)(type, buf, buf, buflen);
173f8307e12SArchie Cobbs }
174f8307e12SArchie Cobbs 
175f8307e12SArchie Cobbs 
176f8307e12SArchie Cobbs /************************************************************************
177f8307e12SArchie Cobbs 			STRUCTURE TYPE
178f8307e12SArchie Cobbs  ************************************************************************/
179f8307e12SArchie Cobbs 
180f8307e12SArchie Cobbs static int
181f8307e12SArchie Cobbs ng_struct_parse(const struct ng_parse_type *type,
182f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
183f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
184f8307e12SArchie Cobbs {
185f8307e12SArchie Cobbs 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
186f8307e12SArchie Cobbs }
187f8307e12SArchie Cobbs 
188f8307e12SArchie Cobbs static int
189f8307e12SArchie Cobbs ng_struct_unparse(const struct ng_parse_type *type,
190f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
191f8307e12SArchie Cobbs {
192f8307e12SArchie Cobbs 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
193f8307e12SArchie Cobbs }
194f8307e12SArchie Cobbs 
195f8307e12SArchie Cobbs static int
196f8307e12SArchie Cobbs ng_struct_getDefault(const struct ng_parse_type *type,
197f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
198f8307e12SArchie Cobbs {
199f8307e12SArchie Cobbs 	int off = 0;
200f8307e12SArchie Cobbs 
201f8307e12SArchie Cobbs 	return ng_parse_composite(type,
202f8307e12SArchie Cobbs 	    "{}", &off, start, buf, buflen, CT_STRUCT);
203f8307e12SArchie Cobbs }
204f8307e12SArchie Cobbs 
205f8307e12SArchie Cobbs static int
206f8307e12SArchie Cobbs ng_struct_getAlign(const struct ng_parse_type *type)
207f8307e12SArchie Cobbs {
208f8307e12SArchie Cobbs 	const struct ng_parse_struct_info *si = type->info;
209f8307e12SArchie Cobbs 	const struct ng_parse_struct_field *field;
210f8307e12SArchie Cobbs 	int align = 0;
211f8307e12SArchie Cobbs 
212f8307e12SArchie Cobbs 	for (field = si->fields; field->name != NULL; field++) {
213f8307e12SArchie Cobbs 		int falign = ALIGNMENT(field->type);
214f8307e12SArchie Cobbs 
215f8307e12SArchie Cobbs 		if (falign > align)
216f8307e12SArchie Cobbs 			align = falign;
217f8307e12SArchie Cobbs 	}
218f8307e12SArchie Cobbs 	return align;
219f8307e12SArchie Cobbs }
220f8307e12SArchie Cobbs 
221f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_struct_type = {
222f8307e12SArchie Cobbs 	NULL,
223f8307e12SArchie Cobbs 	NULL,
224f8307e12SArchie Cobbs 	NULL,
225f8307e12SArchie Cobbs 	ng_struct_parse,
226f8307e12SArchie Cobbs 	ng_struct_unparse,
227f8307e12SArchie Cobbs 	ng_struct_getDefault,
228f8307e12SArchie Cobbs 	ng_struct_getAlign
229f8307e12SArchie Cobbs };
230f8307e12SArchie Cobbs 
231f8307e12SArchie Cobbs /************************************************************************
232f8307e12SArchie Cobbs 			FIXED LENGTH ARRAY TYPE
233f8307e12SArchie Cobbs  ************************************************************************/
234f8307e12SArchie Cobbs 
235f8307e12SArchie Cobbs static int
236f8307e12SArchie Cobbs ng_fixedarray_parse(const struct ng_parse_type *type,
237f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
238f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
239f8307e12SArchie Cobbs {
240f8307e12SArchie Cobbs 	return ng_parse_composite(type,
241f8307e12SArchie Cobbs 	    s, off, start, buf, buflen, CT_FIXEDARRAY);
242f8307e12SArchie Cobbs }
243f8307e12SArchie Cobbs 
244f8307e12SArchie Cobbs static int
245f8307e12SArchie Cobbs ng_fixedarray_unparse(const struct ng_parse_type *type,
246f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
247f8307e12SArchie Cobbs {
248f8307e12SArchie Cobbs 	return ng_unparse_composite(type,
249f8307e12SArchie Cobbs 		data, off, cbuf, cbuflen, CT_FIXEDARRAY);
250f8307e12SArchie Cobbs }
251f8307e12SArchie Cobbs 
252f8307e12SArchie Cobbs static int
253f8307e12SArchie Cobbs ng_fixedarray_getDefault(const struct ng_parse_type *type,
254f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
255f8307e12SArchie Cobbs {
256f8307e12SArchie Cobbs 	int off = 0;
257f8307e12SArchie Cobbs 
258f8307e12SArchie Cobbs 	return ng_parse_composite(type,
259f8307e12SArchie Cobbs 	    "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
260f8307e12SArchie Cobbs }
261f8307e12SArchie Cobbs 
262f8307e12SArchie Cobbs static int
263f8307e12SArchie Cobbs ng_fixedarray_getAlign(const struct ng_parse_type *type)
264f8307e12SArchie Cobbs {
265f8307e12SArchie Cobbs 	const struct ng_parse_fixedarray_info *fi = type->info;
266f8307e12SArchie Cobbs 
267f8307e12SArchie Cobbs 	return ALIGNMENT(fi->elementType);
268f8307e12SArchie Cobbs }
269f8307e12SArchie Cobbs 
270f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedarray_type = {
271f8307e12SArchie Cobbs 	NULL,
272f8307e12SArchie Cobbs 	NULL,
273f8307e12SArchie Cobbs 	NULL,
274f8307e12SArchie Cobbs 	ng_fixedarray_parse,
275f8307e12SArchie Cobbs 	ng_fixedarray_unparse,
276f8307e12SArchie Cobbs 	ng_fixedarray_getDefault,
277f8307e12SArchie Cobbs 	ng_fixedarray_getAlign
278f8307e12SArchie Cobbs };
279f8307e12SArchie Cobbs 
280f8307e12SArchie Cobbs /************************************************************************
281f8307e12SArchie Cobbs 			VARIABLE LENGTH ARRAY TYPE
282f8307e12SArchie Cobbs  ************************************************************************/
283f8307e12SArchie Cobbs 
284f8307e12SArchie Cobbs static int
285f8307e12SArchie Cobbs ng_array_parse(const struct ng_parse_type *type,
286f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
287f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
288f8307e12SArchie Cobbs {
289f8307e12SArchie Cobbs 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
290f8307e12SArchie Cobbs }
291f8307e12SArchie Cobbs 
292f8307e12SArchie Cobbs static int
293f8307e12SArchie Cobbs ng_array_unparse(const struct ng_parse_type *type,
294f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
295f8307e12SArchie Cobbs {
296f8307e12SArchie Cobbs 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
297f8307e12SArchie Cobbs }
298f8307e12SArchie Cobbs 
299f8307e12SArchie Cobbs static int
300f8307e12SArchie Cobbs ng_array_getDefault(const struct ng_parse_type *type,
301f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
302f8307e12SArchie Cobbs {
303f8307e12SArchie Cobbs 	int off = 0;
304f8307e12SArchie Cobbs 
305f8307e12SArchie Cobbs 	return ng_parse_composite(type,
306f8307e12SArchie Cobbs 	    "[]", &off, start, buf, buflen, CT_ARRAY);
307f8307e12SArchie Cobbs }
308f8307e12SArchie Cobbs 
309f8307e12SArchie Cobbs static int
310f8307e12SArchie Cobbs ng_array_getAlign(const struct ng_parse_type *type)
311f8307e12SArchie Cobbs {
312f8307e12SArchie Cobbs 	const struct ng_parse_array_info *ai = type->info;
313f8307e12SArchie Cobbs 
314f8307e12SArchie Cobbs 	return ALIGNMENT(ai->elementType);
315f8307e12SArchie Cobbs }
316f8307e12SArchie Cobbs 
317f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_array_type = {
318f8307e12SArchie Cobbs 	NULL,
319f8307e12SArchie Cobbs 	NULL,
320f8307e12SArchie Cobbs 	NULL,
321f8307e12SArchie Cobbs 	ng_array_parse,
322f8307e12SArchie Cobbs 	ng_array_unparse,
323f8307e12SArchie Cobbs 	ng_array_getDefault,
324f8307e12SArchie Cobbs 	ng_array_getAlign
325f8307e12SArchie Cobbs };
326f8307e12SArchie Cobbs 
327f8307e12SArchie Cobbs /************************************************************************
328f8307e12SArchie Cobbs 				INT8 TYPE
329f8307e12SArchie Cobbs  ************************************************************************/
330f8307e12SArchie Cobbs 
331f8307e12SArchie Cobbs static int
332f8307e12SArchie Cobbs ng_int8_parse(const struct ng_parse_type *type,
333f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
334f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
335f8307e12SArchie Cobbs {
336f8307e12SArchie Cobbs 	long val;
337f8307e12SArchie Cobbs 	int8_t val8;
338f8307e12SArchie Cobbs 	char *eptr;
339f8307e12SArchie Cobbs 
340f8307e12SArchie Cobbs 	val = strtol(s + *off, &eptr, 0);
341f8307e12SArchie Cobbs 	if (val < -0x80 || val > 0xff || eptr == s + *off)
342f8307e12SArchie Cobbs 		return (EINVAL);
343f8307e12SArchie Cobbs 	*off = eptr - s;
344f8307e12SArchie Cobbs 	val8 = (int8_t)val;
345f8307e12SArchie Cobbs 	bcopy(&val8, buf, sizeof(int8_t));
346f8307e12SArchie Cobbs 	*buflen = sizeof(int8_t);
347f8307e12SArchie Cobbs 	return (0);
348f8307e12SArchie Cobbs }
349f8307e12SArchie Cobbs 
350f8307e12SArchie Cobbs static int
351f8307e12SArchie Cobbs ng_int8_unparse(const struct ng_parse_type *type,
352f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
353f8307e12SArchie Cobbs {
3548db3c6cdSArchie Cobbs 	const char *fmt;
3558db3c6cdSArchie Cobbs 	int fval;
356f8307e12SArchie Cobbs 	int8_t val;
357f8307e12SArchie Cobbs 
358f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int8_t));
3598db3c6cdSArchie Cobbs 	switch ((int)type->info) {
3608db3c6cdSArchie Cobbs 	case INT_SIGNED:
3618db3c6cdSArchie Cobbs 		fmt = "%d";
3628db3c6cdSArchie Cobbs 		fval = val;
3638db3c6cdSArchie Cobbs 		break;
3648db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
3658db3c6cdSArchie Cobbs 		fmt = "%u";
3668db3c6cdSArchie Cobbs 		fval = (u_int8_t)val;
3678db3c6cdSArchie Cobbs 		break;
3688db3c6cdSArchie Cobbs 	case INT_HEX:
3698db3c6cdSArchie Cobbs 		fmt = "0x%x";
3708db3c6cdSArchie Cobbs 		fval = (u_int8_t)val;
3718db3c6cdSArchie Cobbs 		break;
3728db3c6cdSArchie Cobbs 	default:
3738db3c6cdSArchie Cobbs 		panic("%s: unknown type", __FUNCTION__);
3748db3c6cdSArchie Cobbs 	}
3758db3c6cdSArchie Cobbs 	NG_PARSE_APPEND(fmt, fval);
376f8307e12SArchie Cobbs 	*off += sizeof(int8_t);
377f8307e12SArchie Cobbs 	return (0);
378f8307e12SArchie Cobbs }
379f8307e12SArchie Cobbs 
380f8307e12SArchie Cobbs static int
381f8307e12SArchie Cobbs ng_int8_getDefault(const struct ng_parse_type *type,
382f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
383f8307e12SArchie Cobbs {
384f8307e12SArchie Cobbs 	int8_t val;
385f8307e12SArchie Cobbs 
386f8307e12SArchie Cobbs 	if (*buflen < sizeof(int8_t))
387f8307e12SArchie Cobbs 		return (ERANGE);
388f8307e12SArchie Cobbs 	val = 0;
389f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int8_t));
390f8307e12SArchie Cobbs 	*buflen = sizeof(int8_t);
391f8307e12SArchie Cobbs 	return (0);
392f8307e12SArchie Cobbs }
393f8307e12SArchie Cobbs 
394f8307e12SArchie Cobbs static int
395f8307e12SArchie Cobbs ng_int8_getAlign(const struct ng_parse_type *type)
396f8307e12SArchie Cobbs {
397f8307e12SArchie Cobbs 	return INT8_ALIGNMENT;
398f8307e12SArchie Cobbs }
399f8307e12SArchie Cobbs 
400f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int8_type = {
401f8307e12SArchie Cobbs 	NULL,
4028db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
403f8307e12SArchie Cobbs 	NULL,
404f8307e12SArchie Cobbs 	ng_int8_parse,
405f8307e12SArchie Cobbs 	ng_int8_unparse,
406f8307e12SArchie Cobbs 	ng_int8_getDefault,
407f8307e12SArchie Cobbs 	ng_int8_getAlign
408f8307e12SArchie Cobbs };
409f8307e12SArchie Cobbs 
4108db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint8_type = {
4118db3c6cdSArchie Cobbs 	&ng_parse_int8_type,
4128db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
4138db3c6cdSArchie Cobbs };
4148db3c6cdSArchie Cobbs 
4158db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint8_type = {
4168db3c6cdSArchie Cobbs 	&ng_parse_int8_type,
4178db3c6cdSArchie Cobbs 	(void *)INT_HEX
4188db3c6cdSArchie Cobbs };
4198db3c6cdSArchie Cobbs 
420f8307e12SArchie Cobbs /************************************************************************
421f8307e12SArchie Cobbs 				INT16 TYPE
422f8307e12SArchie Cobbs  ************************************************************************/
423f8307e12SArchie Cobbs 
424f8307e12SArchie Cobbs static int
425f8307e12SArchie Cobbs ng_int16_parse(const struct ng_parse_type *type,
426f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
427f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
428f8307e12SArchie Cobbs {
429f8307e12SArchie Cobbs 	long val;
430f8307e12SArchie Cobbs 	int16_t val16;
431f8307e12SArchie Cobbs 	char *eptr;
432f8307e12SArchie Cobbs 
433f8307e12SArchie Cobbs 	val = strtol(s + *off, &eptr, 0);
434f8307e12SArchie Cobbs 	if (val < -0x8000 || val > 0xffff || eptr == s + *off)
435f8307e12SArchie Cobbs 		return (EINVAL);
436f8307e12SArchie Cobbs 	*off = eptr - s;
437f8307e12SArchie Cobbs 	val16 = (int16_t)val;
438f8307e12SArchie Cobbs 	bcopy(&val16, buf, sizeof(int16_t));
439f8307e12SArchie Cobbs 	*buflen = sizeof(int16_t);
440f8307e12SArchie Cobbs 	return (0);
441f8307e12SArchie Cobbs }
442f8307e12SArchie Cobbs 
443f8307e12SArchie Cobbs static int
444f8307e12SArchie Cobbs ng_int16_unparse(const struct ng_parse_type *type,
445f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
446f8307e12SArchie Cobbs {
4478db3c6cdSArchie Cobbs 	const char *fmt;
4488db3c6cdSArchie Cobbs 	int fval;
449f8307e12SArchie Cobbs 	int16_t val;
450f8307e12SArchie Cobbs 
451f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int16_t));
4528db3c6cdSArchie Cobbs 	switch ((int)type->info) {
4538db3c6cdSArchie Cobbs 	case INT_SIGNED:
4548db3c6cdSArchie Cobbs 		fmt = "%d";
4558db3c6cdSArchie Cobbs 		fval = val;
4568db3c6cdSArchie Cobbs 		break;
4578db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
4588db3c6cdSArchie Cobbs 		fmt = "%u";
4598db3c6cdSArchie Cobbs 		fval = (u_int16_t)val;
4608db3c6cdSArchie Cobbs 		break;
4618db3c6cdSArchie Cobbs 	case INT_HEX:
4628db3c6cdSArchie Cobbs 		fmt = "0x%x";
4638db3c6cdSArchie Cobbs 		fval = (u_int16_t)val;
4648db3c6cdSArchie Cobbs 		break;
4658db3c6cdSArchie Cobbs 	default:
4668db3c6cdSArchie Cobbs 		panic("%s: unknown type", __FUNCTION__);
4678db3c6cdSArchie Cobbs 	}
4688db3c6cdSArchie Cobbs 	NG_PARSE_APPEND(fmt, fval);
469f8307e12SArchie Cobbs 	*off += sizeof(int16_t);
470f8307e12SArchie Cobbs 	return (0);
471f8307e12SArchie Cobbs }
472f8307e12SArchie Cobbs 
473f8307e12SArchie Cobbs static int
474f8307e12SArchie Cobbs ng_int16_getDefault(const struct ng_parse_type *type,
475f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
476f8307e12SArchie Cobbs {
477f8307e12SArchie Cobbs 	int16_t val;
478f8307e12SArchie Cobbs 
479f8307e12SArchie Cobbs 	if (*buflen < sizeof(int16_t))
480f8307e12SArchie Cobbs 		return (ERANGE);
481f8307e12SArchie Cobbs 	val = 0;
482f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int16_t));
483f8307e12SArchie Cobbs 	*buflen = sizeof(int16_t);
484f8307e12SArchie Cobbs 	return (0);
485f8307e12SArchie Cobbs }
486f8307e12SArchie Cobbs 
487f8307e12SArchie Cobbs static int
488f8307e12SArchie Cobbs ng_int16_getAlign(const struct ng_parse_type *type)
489f8307e12SArchie Cobbs {
490f8307e12SArchie Cobbs 	return INT16_ALIGNMENT;
491f8307e12SArchie Cobbs }
492f8307e12SArchie Cobbs 
493f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int16_type = {
494f8307e12SArchie Cobbs 	NULL,
4958db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
496f8307e12SArchie Cobbs 	NULL,
497f8307e12SArchie Cobbs 	ng_int16_parse,
498f8307e12SArchie Cobbs 	ng_int16_unparse,
499f8307e12SArchie Cobbs 	ng_int16_getDefault,
500f8307e12SArchie Cobbs 	ng_int16_getAlign
501f8307e12SArchie Cobbs };
502f8307e12SArchie Cobbs 
5038db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint16_type = {
5048db3c6cdSArchie Cobbs 	&ng_parse_int16_type,
5058db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
5068db3c6cdSArchie Cobbs };
5078db3c6cdSArchie Cobbs 
5088db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint16_type = {
5098db3c6cdSArchie Cobbs 	&ng_parse_int16_type,
5108db3c6cdSArchie Cobbs 	(void *)INT_HEX
5118db3c6cdSArchie Cobbs };
5128db3c6cdSArchie Cobbs 
513f8307e12SArchie Cobbs /************************************************************************
514f8307e12SArchie Cobbs 				INT32 TYPE
515f8307e12SArchie Cobbs  ************************************************************************/
516f8307e12SArchie Cobbs 
517f8307e12SArchie Cobbs static int
518f8307e12SArchie Cobbs ng_int32_parse(const struct ng_parse_type *type,
519f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
520f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
521f8307e12SArchie Cobbs {
522f8307e12SArchie Cobbs 	long val;			/* assumes long is at least 32 bits */
523f8307e12SArchie Cobbs 	int32_t val32;
524f8307e12SArchie Cobbs 	char *eptr;
525f8307e12SArchie Cobbs 
526f8307e12SArchie Cobbs 	val = strtol(s + *off, &eptr, 0);
5272076235aSArchie Cobbs 	if (val < (long)-0x80000000
5282076235aSArchie Cobbs 	    || val > (u_long)0xffffffff || eptr == s + *off)
529f8307e12SArchie Cobbs 		return (EINVAL);
530f8307e12SArchie Cobbs 	*off = eptr - s;
531f8307e12SArchie Cobbs 	val32 = (int32_t)val;
532f8307e12SArchie Cobbs 	bcopy(&val32, buf, sizeof(int32_t));
533f8307e12SArchie Cobbs 	*buflen = sizeof(int32_t);
534f8307e12SArchie Cobbs 	return (0);
535f8307e12SArchie Cobbs }
536f8307e12SArchie Cobbs 
537f8307e12SArchie Cobbs static int
538f8307e12SArchie Cobbs ng_int32_unparse(const struct ng_parse_type *type,
539f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
540f8307e12SArchie Cobbs {
5418db3c6cdSArchie Cobbs 	const char *fmt;
5428db3c6cdSArchie Cobbs 	long fval;
543f8307e12SArchie Cobbs 	int32_t val;
544f8307e12SArchie Cobbs 
545f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int32_t));
5468db3c6cdSArchie Cobbs 	switch ((int)type->info) {
5478db3c6cdSArchie Cobbs 	case INT_SIGNED:
5488db3c6cdSArchie Cobbs 		fmt = "%ld";
5498db3c6cdSArchie Cobbs 		fval = val;
5508db3c6cdSArchie Cobbs 		break;
5518db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
5528db3c6cdSArchie Cobbs 		fmt = "%lu";
5538db3c6cdSArchie Cobbs 		fval = (u_int32_t)val;
5548db3c6cdSArchie Cobbs 		break;
5558db3c6cdSArchie Cobbs 	case INT_HEX:
5568db3c6cdSArchie Cobbs 		fmt = "0x%lx";
5578db3c6cdSArchie Cobbs 		fval = (u_int32_t)val;
5588db3c6cdSArchie Cobbs 		break;
5598db3c6cdSArchie Cobbs 	default:
5608db3c6cdSArchie Cobbs 		panic("%s: unknown type", __FUNCTION__);
5618db3c6cdSArchie Cobbs 	}
5628db3c6cdSArchie Cobbs 	NG_PARSE_APPEND(fmt, fval);
563f8307e12SArchie Cobbs 	*off += sizeof(int32_t);
564f8307e12SArchie Cobbs 	return (0);
565f8307e12SArchie Cobbs }
566f8307e12SArchie Cobbs 
567f8307e12SArchie Cobbs static int
568f8307e12SArchie Cobbs ng_int32_getDefault(const struct ng_parse_type *type,
569f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
570f8307e12SArchie Cobbs {
571f8307e12SArchie Cobbs 	int32_t val;
572f8307e12SArchie Cobbs 
573f8307e12SArchie Cobbs 	if (*buflen < sizeof(int32_t))
574f8307e12SArchie Cobbs 		return (ERANGE);
575f8307e12SArchie Cobbs 	val = 0;
576f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int32_t));
577f8307e12SArchie Cobbs 	*buflen = sizeof(int32_t);
578f8307e12SArchie Cobbs 	return (0);
579f8307e12SArchie Cobbs }
580f8307e12SArchie Cobbs 
581f8307e12SArchie Cobbs static int
582f8307e12SArchie Cobbs ng_int32_getAlign(const struct ng_parse_type *type)
583f8307e12SArchie Cobbs {
584f8307e12SArchie Cobbs 	return INT32_ALIGNMENT;
585f8307e12SArchie Cobbs }
586f8307e12SArchie Cobbs 
587f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int32_type = {
588f8307e12SArchie Cobbs 	NULL,
5898db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
590f8307e12SArchie Cobbs 	NULL,
591f8307e12SArchie Cobbs 	ng_int32_parse,
592f8307e12SArchie Cobbs 	ng_int32_unparse,
593f8307e12SArchie Cobbs 	ng_int32_getDefault,
594f8307e12SArchie Cobbs 	ng_int32_getAlign
595f8307e12SArchie Cobbs };
596f8307e12SArchie Cobbs 
5978db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint32_type = {
5988db3c6cdSArchie Cobbs 	&ng_parse_int32_type,
5998db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
6008db3c6cdSArchie Cobbs };
6018db3c6cdSArchie Cobbs 
6028db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint32_type = {
6038db3c6cdSArchie Cobbs 	&ng_parse_int32_type,
6048db3c6cdSArchie Cobbs 	(void *)INT_HEX
6058db3c6cdSArchie Cobbs };
6068db3c6cdSArchie Cobbs 
607f8307e12SArchie Cobbs /************************************************************************
608f8307e12SArchie Cobbs 				INT64 TYPE
609f8307e12SArchie Cobbs  ************************************************************************/
610f8307e12SArchie Cobbs 
611f8307e12SArchie Cobbs static int
612f8307e12SArchie Cobbs ng_int64_parse(const struct ng_parse_type *type,
613f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
614f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
615f8307e12SArchie Cobbs {
616f8307e12SArchie Cobbs 	quad_t val;
617f8307e12SArchie Cobbs 	int64_t val64;
618f8307e12SArchie Cobbs 	char *eptr;
619f8307e12SArchie Cobbs 
620f8307e12SArchie Cobbs 	val = strtoq(s + *off, &eptr, 0);
621f8307e12SArchie Cobbs 	if (eptr == s + *off)
622f8307e12SArchie Cobbs 		return (EINVAL);
623f8307e12SArchie Cobbs 	*off = eptr - s;
624f8307e12SArchie Cobbs 	val64 = (int64_t)val;
625f8307e12SArchie Cobbs 	bcopy(&val64, buf, sizeof(int64_t));
626f8307e12SArchie Cobbs 	*buflen = sizeof(int64_t);
627f8307e12SArchie Cobbs 	return (0);
628f8307e12SArchie Cobbs }
629f8307e12SArchie Cobbs 
630f8307e12SArchie Cobbs static int
631f8307e12SArchie Cobbs ng_int64_unparse(const struct ng_parse_type *type,
632f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
633f8307e12SArchie Cobbs {
6348db3c6cdSArchie Cobbs 	const char *fmt;
6358db3c6cdSArchie Cobbs 	long long fval;
636f8307e12SArchie Cobbs 	int64_t val;
637f8307e12SArchie Cobbs 
638f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int64_t));
6398db3c6cdSArchie Cobbs 	switch ((int)type->info) {
6408db3c6cdSArchie Cobbs 	case INT_SIGNED:
6418db3c6cdSArchie Cobbs 		fmt = "%lld";
6428db3c6cdSArchie Cobbs 		fval = val;
6438db3c6cdSArchie Cobbs 		break;
6448db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
6458db3c6cdSArchie Cobbs 		fmt = "%llu";
6468db3c6cdSArchie Cobbs 		fval = (u_int64_t)val;
6478db3c6cdSArchie Cobbs 		break;
6488db3c6cdSArchie Cobbs 	case INT_HEX:
6498db3c6cdSArchie Cobbs 		fmt = "0x%llx";
6508db3c6cdSArchie Cobbs 		fval = (u_int64_t)val;
6518db3c6cdSArchie Cobbs 		break;
6528db3c6cdSArchie Cobbs 	default:
6538db3c6cdSArchie Cobbs 		panic("%s: unknown type", __FUNCTION__);
6548db3c6cdSArchie Cobbs 	}
6558db3c6cdSArchie Cobbs 	NG_PARSE_APPEND(fmt, fval);
656f8307e12SArchie Cobbs 	*off += sizeof(int64_t);
657f8307e12SArchie Cobbs 	return (0);
658f8307e12SArchie Cobbs }
659f8307e12SArchie Cobbs 
660f8307e12SArchie Cobbs static int
661f8307e12SArchie Cobbs ng_int64_getDefault(const struct ng_parse_type *type,
662f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
663f8307e12SArchie Cobbs {
664f8307e12SArchie Cobbs 	int64_t val;
665f8307e12SArchie Cobbs 
666f8307e12SArchie Cobbs 	if (*buflen < sizeof(int64_t))
667f8307e12SArchie Cobbs 		return (ERANGE);
668f8307e12SArchie Cobbs 	val = 0;
669f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int64_t));
670f8307e12SArchie Cobbs 	*buflen = sizeof(int64_t);
671f8307e12SArchie Cobbs 	return (0);
672f8307e12SArchie Cobbs }
673f8307e12SArchie Cobbs 
674f8307e12SArchie Cobbs static int
675f8307e12SArchie Cobbs ng_int64_getAlign(const struct ng_parse_type *type)
676f8307e12SArchie Cobbs {
677f8307e12SArchie Cobbs 	return INT64_ALIGNMENT;
678f8307e12SArchie Cobbs }
679f8307e12SArchie Cobbs 
680f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int64_type = {
681f8307e12SArchie Cobbs 	NULL,
6828db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
683f8307e12SArchie Cobbs 	NULL,
684f8307e12SArchie Cobbs 	ng_int64_parse,
685f8307e12SArchie Cobbs 	ng_int64_unparse,
686f8307e12SArchie Cobbs 	ng_int64_getDefault,
687f8307e12SArchie Cobbs 	ng_int64_getAlign
688f8307e12SArchie Cobbs };
689f8307e12SArchie Cobbs 
6908db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint64_type = {
6918db3c6cdSArchie Cobbs 	&ng_parse_int64_type,
6928db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
6938db3c6cdSArchie Cobbs };
6948db3c6cdSArchie Cobbs 
6958db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint64_type = {
6968db3c6cdSArchie Cobbs 	&ng_parse_int64_type,
6978db3c6cdSArchie Cobbs 	(void *)INT_HEX
6988db3c6cdSArchie Cobbs };
6998db3c6cdSArchie Cobbs 
700f8307e12SArchie Cobbs /************************************************************************
701f8307e12SArchie Cobbs 				STRING TYPE
702f8307e12SArchie Cobbs  ************************************************************************/
703f8307e12SArchie Cobbs 
704f8307e12SArchie Cobbs static int
705f8307e12SArchie Cobbs ng_string_parse(const struct ng_parse_type *type,
706f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
707f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
708f8307e12SArchie Cobbs {
709f8307e12SArchie Cobbs 	char *sval;
710f8307e12SArchie Cobbs 	int len;
71127121ab1SBrian Somers 	int slen;
712f8307e12SArchie Cobbs 
71327121ab1SBrian Somers 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
714f8307e12SArchie Cobbs 		return (EINVAL);
715f8307e12SArchie Cobbs 	*off += len;
71627121ab1SBrian Somers 	bcopy(sval, buf, slen + 1);
717f8307e12SArchie Cobbs 	FREE(sval, M_NETGRAPH);
71827121ab1SBrian Somers 	*buflen = slen + 1;
719f8307e12SArchie Cobbs 	return (0);
720f8307e12SArchie Cobbs }
721f8307e12SArchie Cobbs 
722f8307e12SArchie Cobbs static int
723f8307e12SArchie Cobbs ng_string_unparse(const struct ng_parse_type *type,
724f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
725f8307e12SArchie Cobbs {
726f8307e12SArchie Cobbs 	const char *const raw = (const char *)data + *off;
72727121ab1SBrian Somers 	char *const s = ng_encode_string(raw, strlen(raw));
728f8307e12SArchie Cobbs 
729f8307e12SArchie Cobbs 	if (s == NULL)
730f8307e12SArchie Cobbs 		return (ENOMEM);
731f8307e12SArchie Cobbs 	NG_PARSE_APPEND("%s", s);
732f8307e12SArchie Cobbs 	*off += strlen(raw) + 1;
733f8307e12SArchie Cobbs 	FREE(s, M_NETGRAPH);
734f8307e12SArchie Cobbs 	return (0);
735f8307e12SArchie Cobbs }
736f8307e12SArchie Cobbs 
737f8307e12SArchie Cobbs static int
738f8307e12SArchie Cobbs ng_string_getDefault(const struct ng_parse_type *type,
739f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
740f8307e12SArchie Cobbs {
741f8307e12SArchie Cobbs 
742f8307e12SArchie Cobbs 	if (*buflen < 1)
743f8307e12SArchie Cobbs 		return (ERANGE);
744f8307e12SArchie Cobbs 	buf[0] = (u_char)'\0';
745f8307e12SArchie Cobbs 	*buflen = 1;
746f8307e12SArchie Cobbs 	return (0);
747f8307e12SArchie Cobbs }
748f8307e12SArchie Cobbs 
749f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_string_type = {
750f8307e12SArchie Cobbs 	NULL,
751f8307e12SArchie Cobbs 	NULL,
752f8307e12SArchie Cobbs 	NULL,
753f8307e12SArchie Cobbs 	ng_string_parse,
754f8307e12SArchie Cobbs 	ng_string_unparse,
755f8307e12SArchie Cobbs 	ng_string_getDefault,
756f8307e12SArchie Cobbs 	NULL
757f8307e12SArchie Cobbs };
758f8307e12SArchie Cobbs 
759f8307e12SArchie Cobbs /************************************************************************
760f8307e12SArchie Cobbs 			FIXED BUFFER STRING TYPE
761f8307e12SArchie Cobbs  ************************************************************************/
762f8307e12SArchie Cobbs 
763f8307e12SArchie Cobbs static int
764f8307e12SArchie Cobbs ng_fixedstring_parse(const struct ng_parse_type *type,
765f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
766f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
767f8307e12SArchie Cobbs {
768c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
769f8307e12SArchie Cobbs 	char *sval;
770f8307e12SArchie Cobbs 	int len;
77127121ab1SBrian Somers 	int slen;
772f8307e12SArchie Cobbs 
77327121ab1SBrian Somers 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
774f8307e12SArchie Cobbs 		return (EINVAL);
77527121ab1SBrian Somers 	if (slen + 1 > fi->bufSize)
776f8307e12SArchie Cobbs 		return (E2BIG);
777f8307e12SArchie Cobbs 	*off += len;
77827121ab1SBrian Somers 	bcopy(sval, buf, slen);
779f8307e12SArchie Cobbs 	FREE(sval, M_NETGRAPH);
78027121ab1SBrian Somers 	bzero(buf + slen, fi->bufSize - slen);
781f8307e12SArchie Cobbs 	*buflen = fi->bufSize;
782f8307e12SArchie Cobbs 	return (0);
783f8307e12SArchie Cobbs }
784f8307e12SArchie Cobbs 
785f8307e12SArchie Cobbs static int
786f8307e12SArchie Cobbs ng_fixedstring_unparse(const struct ng_parse_type *type,
787f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
788f8307e12SArchie Cobbs {
789c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
790f8307e12SArchie Cobbs 	int error, temp = *off;
791f8307e12SArchie Cobbs 
792f8307e12SArchie Cobbs 	if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
793f8307e12SArchie Cobbs 		return (error);
794f8307e12SArchie Cobbs 	*off += fi->bufSize;
795f8307e12SArchie Cobbs 	return (0);
796f8307e12SArchie Cobbs }
797f8307e12SArchie Cobbs 
798f8307e12SArchie Cobbs static int
799f8307e12SArchie Cobbs ng_fixedstring_getDefault(const struct ng_parse_type *type,
800f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
801f8307e12SArchie Cobbs {
802c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
803f8307e12SArchie Cobbs 
804f8307e12SArchie Cobbs 	if (*buflen < fi->bufSize)
805f8307e12SArchie Cobbs 		return (ERANGE);
806f8307e12SArchie Cobbs 	bzero(buf, fi->bufSize);
807f8307e12SArchie Cobbs 	*buflen = fi->bufSize;
808f8307e12SArchie Cobbs 	return (0);
809f8307e12SArchie Cobbs }
810f8307e12SArchie Cobbs 
811f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedstring_type = {
812f8307e12SArchie Cobbs 	NULL,
813f8307e12SArchie Cobbs 	NULL,
814f8307e12SArchie Cobbs 	NULL,
815f8307e12SArchie Cobbs 	ng_fixedstring_parse,
816f8307e12SArchie Cobbs 	ng_fixedstring_unparse,
817f8307e12SArchie Cobbs 	ng_fixedstring_getDefault,
818f8307e12SArchie Cobbs 	NULL
819f8307e12SArchie Cobbs };
820f8307e12SArchie Cobbs 
821c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
822f8307e12SArchie Cobbs 	NG_NODELEN + 1
823f8307e12SArchie Cobbs };
824f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_nodebuf_type = {
825f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
826f8307e12SArchie Cobbs 	&ng_parse_nodebuf_info
827f8307e12SArchie Cobbs };
828f8307e12SArchie Cobbs 
829c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
830f8307e12SArchie Cobbs 	NG_HOOKLEN + 1
831f8307e12SArchie Cobbs };
832f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_hookbuf_type = {
833f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
834f8307e12SArchie Cobbs 	&ng_parse_hookbuf_info
835f8307e12SArchie Cobbs };
836f8307e12SArchie Cobbs 
837c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
838f8307e12SArchie Cobbs 	NG_PATHLEN + 1
839f8307e12SArchie Cobbs };
840f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_pathbuf_type = {
841f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
842f8307e12SArchie Cobbs 	&ng_parse_pathbuf_info
843f8307e12SArchie Cobbs };
844f8307e12SArchie Cobbs 
845c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
846f8307e12SArchie Cobbs 	NG_TYPELEN + 1
847f8307e12SArchie Cobbs };
848f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_typebuf_type = {
849f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
850f8307e12SArchie Cobbs 	&ng_parse_typebuf_info
851f8307e12SArchie Cobbs };
852f8307e12SArchie Cobbs 
853c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
854f8307e12SArchie Cobbs 	NG_CMDSTRLEN + 1
855f8307e12SArchie Cobbs };
856f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_cmdbuf_type = {
857f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
858f8307e12SArchie Cobbs 	&ng_parse_cmdbuf_info
859f8307e12SArchie Cobbs };
860f8307e12SArchie Cobbs 
861f8307e12SArchie Cobbs /************************************************************************
86227121ab1SBrian Somers 			EXPLICITLY SIZED STRING TYPE
86327121ab1SBrian Somers  ************************************************************************/
86427121ab1SBrian Somers 
86527121ab1SBrian Somers static int
86627121ab1SBrian Somers ng_sizedstring_parse(const struct ng_parse_type *type,
86727121ab1SBrian Somers 	const char *s, int *off, const u_char *const start,
86827121ab1SBrian Somers 	u_char *const buf, int *buflen)
86927121ab1SBrian Somers {
87027121ab1SBrian Somers 	char *sval;
87127121ab1SBrian Somers 	int len;
87227121ab1SBrian Somers 	int slen;
87327121ab1SBrian Somers 
87427121ab1SBrian Somers 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
87527121ab1SBrian Somers 		return (EINVAL);
87627121ab1SBrian Somers 	if (slen > 0xffff)
87727121ab1SBrian Somers 		return (EINVAL);
87827121ab1SBrian Somers 	*off += len;
87927121ab1SBrian Somers 	*((u_int16_t *)buf) = (u_int16_t)slen;
88027121ab1SBrian Somers 	bcopy(sval, buf + 2, slen);
88127121ab1SBrian Somers 	FREE(sval, M_NETGRAPH);
88227121ab1SBrian Somers 	*buflen = 2 + slen;
88327121ab1SBrian Somers 	return (0);
88427121ab1SBrian Somers }
88527121ab1SBrian Somers 
88627121ab1SBrian Somers static int
88727121ab1SBrian Somers ng_sizedstring_unparse(const struct ng_parse_type *type,
88827121ab1SBrian Somers 	const u_char *data, int *off, char *cbuf, int cbuflen)
88927121ab1SBrian Somers {
89027121ab1SBrian Somers 	const char *const raw = (const char *)data + *off + 2;
89127121ab1SBrian Somers 	const int slen = *((const u_int16_t *)(data + *off));
89227121ab1SBrian Somers 	char *const s = ng_encode_string(raw, slen);
89327121ab1SBrian Somers 
89427121ab1SBrian Somers 	if (s == NULL)
89527121ab1SBrian Somers 		return (ENOMEM);
89627121ab1SBrian Somers 	NG_PARSE_APPEND("%s", s);
89727121ab1SBrian Somers 	FREE(s, M_NETGRAPH);
89827121ab1SBrian Somers 	*off += slen + 2;
89927121ab1SBrian Somers 	return (0);
90027121ab1SBrian Somers }
90127121ab1SBrian Somers 
90227121ab1SBrian Somers static int
90327121ab1SBrian Somers ng_sizedstring_getDefault(const struct ng_parse_type *type,
90427121ab1SBrian Somers 	const u_char *const start, u_char *buf, int *buflen)
90527121ab1SBrian Somers {
90627121ab1SBrian Somers 	if (*buflen < 2)
90727121ab1SBrian Somers 		return (ERANGE);
90827121ab1SBrian Somers 	bzero(buf, 2);
90927121ab1SBrian Somers 	*buflen = 2;
91027121ab1SBrian Somers 	return (0);
91127121ab1SBrian Somers }
91227121ab1SBrian Somers 
91327121ab1SBrian Somers const struct ng_parse_type ng_parse_sizedstring_type = {
91427121ab1SBrian Somers 	NULL,
91527121ab1SBrian Somers 	NULL,
91627121ab1SBrian Somers 	NULL,
91727121ab1SBrian Somers 	ng_sizedstring_parse,
91827121ab1SBrian Somers 	ng_sizedstring_unparse,
91927121ab1SBrian Somers 	ng_sizedstring_getDefault,
92027121ab1SBrian Somers 	NULL
92127121ab1SBrian Somers };
92227121ab1SBrian Somers 
92327121ab1SBrian Somers /************************************************************************
924f8307e12SArchie Cobbs 			IP ADDRESS TYPE
925f8307e12SArchie Cobbs  ************************************************************************/
926f8307e12SArchie Cobbs 
927f8307e12SArchie Cobbs static int
928f8307e12SArchie Cobbs ng_ipaddr_parse(const struct ng_parse_type *type,
929f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
930f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
931f8307e12SArchie Cobbs {
932f8307e12SArchie Cobbs 	int i, error;
933f8307e12SArchie Cobbs 
934f8307e12SArchie Cobbs 	for (i = 0; i < 4; i++) {
935f8307e12SArchie Cobbs 		if ((error = ng_int8_parse(&ng_parse_int8_type,
936f8307e12SArchie Cobbs 		    s, off, start, buf + i, buflen)) != 0)
937f8307e12SArchie Cobbs 			return (error);
938f8307e12SArchie Cobbs 		if (i < 3 && s[*off] != '.')
939f8307e12SArchie Cobbs 			return (EINVAL);
940f8307e12SArchie Cobbs 		(*off)++;
941f8307e12SArchie Cobbs 	}
942f8307e12SArchie Cobbs 	*buflen = 4;
943f8307e12SArchie Cobbs 	return (0);
944f8307e12SArchie Cobbs }
945f8307e12SArchie Cobbs 
946f8307e12SArchie Cobbs static int
947f8307e12SArchie Cobbs ng_ipaddr_unparse(const struct ng_parse_type *type,
948f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
949f8307e12SArchie Cobbs {
950f8307e12SArchie Cobbs 	struct in_addr ip;
951f8307e12SArchie Cobbs 
952f8307e12SArchie Cobbs 	bcopy(data + *off, &ip, sizeof(ip));
953f8307e12SArchie Cobbs 	NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
954f8307e12SArchie Cobbs 	    ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
955f8307e12SArchie Cobbs 	*off += sizeof(ip);
956f8307e12SArchie Cobbs 	return (0);
957f8307e12SArchie Cobbs }
958f8307e12SArchie Cobbs 
959f8307e12SArchie Cobbs static int
960f8307e12SArchie Cobbs ng_ipaddr_getDefault(const struct ng_parse_type *type,
961f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
962f8307e12SArchie Cobbs {
963f8307e12SArchie Cobbs 	struct in_addr ip = { 0 };
964f8307e12SArchie Cobbs 
965f8307e12SArchie Cobbs 	if (*buflen < sizeof(ip))
966f8307e12SArchie Cobbs 		return (ERANGE);
967f8307e12SArchie Cobbs 	bcopy(&ip, buf, sizeof(ip));
968f8307e12SArchie Cobbs 	*buflen = sizeof(ip);
969f8307e12SArchie Cobbs 	return (0);
970f8307e12SArchie Cobbs }
971f8307e12SArchie Cobbs 
972f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ipaddr_type = {
973f8307e12SArchie Cobbs 	NULL,
974f8307e12SArchie Cobbs 	NULL,
975f8307e12SArchie Cobbs 	NULL,
976f8307e12SArchie Cobbs 	ng_ipaddr_parse,
977f8307e12SArchie Cobbs 	ng_ipaddr_unparse,
978f8307e12SArchie Cobbs 	ng_ipaddr_getDefault,
979f8307e12SArchie Cobbs 	ng_int32_getAlign
980f8307e12SArchie Cobbs };
981f8307e12SArchie Cobbs 
982f8307e12SArchie Cobbs /************************************************************************
983f8307e12SArchie Cobbs 			BYTE ARRAY TYPE
984f8307e12SArchie Cobbs  ************************************************************************/
985f8307e12SArchie Cobbs 
986f8307e12SArchie Cobbs /* Get the length of a byte array */
987f8307e12SArchie Cobbs static int
988f8307e12SArchie Cobbs ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
989f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
990f8307e12SArchie Cobbs {
991f8307e12SArchie Cobbs 	ng_parse_array_getLength_t *const getLength = type->private;
992f8307e12SArchie Cobbs 
993f8307e12SArchie Cobbs 	return (*getLength)(type, start, buf);
994f8307e12SArchie Cobbs }
995f8307e12SArchie Cobbs 
9968db3c6cdSArchie Cobbs /* Byte array element type is hex int8 */
997f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
9988db3c6cdSArchie Cobbs 	&ng_parse_hint8_type,
999f8307e12SArchie Cobbs 	&ng_parse_bytearray_subtype_getLength,
1000f8307e12SArchie Cobbs 	NULL
1001f8307e12SArchie Cobbs };
1002f8307e12SArchie Cobbs static const struct ng_parse_type ng_parse_bytearray_subtype = {
1003f8307e12SArchie Cobbs 	&ng_parse_array_type,
1004f8307e12SArchie Cobbs 	&ng_parse_bytearray_subtype_info
1005f8307e12SArchie Cobbs };
1006f8307e12SArchie Cobbs 
1007f8307e12SArchie Cobbs static int
1008f8307e12SArchie Cobbs ng_bytearray_parse(const struct ng_parse_type *type,
1009f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
1010f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
1011f8307e12SArchie Cobbs {
1012f8307e12SArchie Cobbs 	char *str;
1013f8307e12SArchie Cobbs 	int toklen;
101427121ab1SBrian Somers 	int slen;
1015f8307e12SArchie Cobbs 
1016f8307e12SArchie Cobbs 	/* We accept either an array of bytes or a string constant */
101727121ab1SBrian Somers 	if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1018f8307e12SArchie Cobbs 		ng_parse_array_getLength_t *const getLength = type->info;
101927121ab1SBrian Somers 		int arraylen;
1020f8307e12SArchie Cobbs 
1021f8307e12SArchie Cobbs 		arraylen = (*getLength)(type, start, buf);
1022f8307e12SArchie Cobbs 		if (arraylen > *buflen) {
1023f8307e12SArchie Cobbs 			FREE(str, M_NETGRAPH);
1024f8307e12SArchie Cobbs 			return (ERANGE);
1025f8307e12SArchie Cobbs 		}
1026f8307e12SArchie Cobbs 		if (slen > arraylen) {
1027f8307e12SArchie Cobbs 			FREE(str, M_NETGRAPH);
1028f8307e12SArchie Cobbs 			return (E2BIG);
1029f8307e12SArchie Cobbs 		}
1030f8307e12SArchie Cobbs 		bcopy(str, buf, slen);
1031f8307e12SArchie Cobbs 		bzero(buf + slen, arraylen - slen);
1032f8307e12SArchie Cobbs 		FREE(str, M_NETGRAPH);
1033f8307e12SArchie Cobbs 		*off += toklen;
1034f8307e12SArchie Cobbs 		*buflen = arraylen;
1035f8307e12SArchie Cobbs 		return (0);
1036f8307e12SArchie Cobbs 	} else {
1037f8307e12SArchie Cobbs 		struct ng_parse_type subtype;
1038f8307e12SArchie Cobbs 
1039f8307e12SArchie Cobbs 		subtype = ng_parse_bytearray_subtype;
1040f8307e12SArchie Cobbs 		(const void *)subtype.private = type->info;
1041f8307e12SArchie Cobbs 		return ng_array_parse(&subtype, s, off, start, buf, buflen);
1042f8307e12SArchie Cobbs 	}
1043f8307e12SArchie Cobbs }
1044f8307e12SArchie Cobbs 
1045f8307e12SArchie Cobbs static int
1046f8307e12SArchie Cobbs ng_bytearray_unparse(const struct ng_parse_type *type,
1047f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
1048f8307e12SArchie Cobbs {
1049f8307e12SArchie Cobbs 	struct ng_parse_type subtype;
1050f8307e12SArchie Cobbs 
1051f8307e12SArchie Cobbs 	subtype = ng_parse_bytearray_subtype;
1052f8307e12SArchie Cobbs 	(const void *)subtype.private = type->info;
1053f8307e12SArchie Cobbs 	return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1054f8307e12SArchie Cobbs }
1055f8307e12SArchie Cobbs 
1056f8307e12SArchie Cobbs static int
1057f8307e12SArchie Cobbs ng_bytearray_getDefault(const struct ng_parse_type *type,
1058f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
1059f8307e12SArchie Cobbs {
1060f8307e12SArchie Cobbs 	struct ng_parse_type subtype;
1061f8307e12SArchie Cobbs 
1062f8307e12SArchie Cobbs 	subtype = ng_parse_bytearray_subtype;
1063f8307e12SArchie Cobbs 	(const void *)subtype.private = type->info;
1064f8307e12SArchie Cobbs 	return ng_array_getDefault(&subtype, start, buf, buflen);
1065f8307e12SArchie Cobbs }
1066f8307e12SArchie Cobbs 
1067f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_bytearray_type = {
1068f8307e12SArchie Cobbs 	NULL,
1069f8307e12SArchie Cobbs 	NULL,
1070f8307e12SArchie Cobbs 	NULL,
1071f8307e12SArchie Cobbs 	ng_bytearray_parse,
1072f8307e12SArchie Cobbs 	ng_bytearray_unparse,
1073f8307e12SArchie Cobbs 	ng_bytearray_getDefault,
1074f8307e12SArchie Cobbs 	NULL
1075f8307e12SArchie Cobbs };
1076f8307e12SArchie Cobbs 
1077f8307e12SArchie Cobbs /************************************************************************
1078f8307e12SArchie Cobbs 			STRUCT NG_MESG TYPE
1079f8307e12SArchie Cobbs  ************************************************************************/
1080f8307e12SArchie Cobbs 
1081f8307e12SArchie Cobbs /* Get msg->header.arglen when "buf" is pointing to msg->data */
1082f8307e12SArchie Cobbs static int
1083f8307e12SArchie Cobbs ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1084f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
1085f8307e12SArchie Cobbs {
1086f8307e12SArchie Cobbs 	const struct ng_mesg *msg;
1087f8307e12SArchie Cobbs 
1088f8307e12SArchie Cobbs 	msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1089f8307e12SArchie Cobbs 	return msg->header.arglen;
1090f8307e12SArchie Cobbs }
1091f8307e12SArchie Cobbs 
1092f8307e12SArchie Cobbs /* Type for the variable length data portion of a struct ng_mesg */
1093f8307e12SArchie Cobbs static const struct ng_parse_type ng_msg_data_type = {
1094f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
1095f8307e12SArchie Cobbs 	&ng_parse_ng_mesg_getLength
1096f8307e12SArchie Cobbs };
1097f8307e12SArchie Cobbs 
1098f8307e12SArchie Cobbs /* Type for the entire struct ng_mesg header with data section */
1099f8307e12SArchie Cobbs static const struct ng_parse_struct_info
1100f8307e12SArchie Cobbs 	ng_parse_ng_mesg_type_info = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1101f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ng_mesg_type = {
1102f8307e12SArchie Cobbs 	&ng_parse_struct_type,
1103f8307e12SArchie Cobbs 	&ng_parse_ng_mesg_type_info,
1104f8307e12SArchie Cobbs };
1105f8307e12SArchie Cobbs 
1106f8307e12SArchie Cobbs /************************************************************************
1107f8307e12SArchie Cobbs 			COMPOSITE HELPER ROUTINES
1108f8307e12SArchie Cobbs  ************************************************************************/
1109f8307e12SArchie Cobbs 
1110f8307e12SArchie Cobbs /*
1111f8307e12SArchie Cobbs  * Convert a structure or array from ASCII to binary
1112f8307e12SArchie Cobbs  */
1113f8307e12SArchie Cobbs static int
1114f8307e12SArchie Cobbs ng_parse_composite(const struct ng_parse_type *type, const char *s,
1115f8307e12SArchie Cobbs 	int *off, const u_char *const start, u_char *const buf, int *buflen,
1116f8307e12SArchie Cobbs 	const enum comptype ctype)
1117f8307e12SArchie Cobbs {
1118f8307e12SArchie Cobbs 	const int num = ng_get_composite_len(type, start, buf, ctype);
1119f8307e12SArchie Cobbs 	int nextIndex = 0;		/* next implicit array index */
1120f8307e12SArchie Cobbs 	u_int index;			/* field or element index */
1121f8307e12SArchie Cobbs 	int *foff;			/* field value offsets in string */
1122f8307e12SArchie Cobbs 	int align, len, blen, error = 0;
1123f8307e12SArchie Cobbs 
1124f8307e12SArchie Cobbs 	/* Initialize */
1125f8307e12SArchie Cobbs 	MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT);
1126f8307e12SArchie Cobbs 	if (foff == NULL) {
1127f8307e12SArchie Cobbs 		error = ENOMEM;
1128f8307e12SArchie Cobbs 		goto done;
1129f8307e12SArchie Cobbs 	}
1130f8307e12SArchie Cobbs 	bzero(foff, num * sizeof(*foff));
1131f8307e12SArchie Cobbs 
1132f8307e12SArchie Cobbs 	/* Get opening brace/bracket */
1133f8307e12SArchie Cobbs 	if (ng_parse_get_token(s, off, &len)
1134f8307e12SArchie Cobbs 	    != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1135f8307e12SArchie Cobbs 		error = EINVAL;
1136f8307e12SArchie Cobbs 		goto done;
1137f8307e12SArchie Cobbs 	}
1138f8307e12SArchie Cobbs 	*off += len;
1139f8307e12SArchie Cobbs 
1140f8307e12SArchie Cobbs 	/* Get individual element value positions in the string */
1141f8307e12SArchie Cobbs 	for (;;) {
1142f8307e12SArchie Cobbs 		enum ng_parse_token tok;
1143f8307e12SArchie Cobbs 
1144f8307e12SArchie Cobbs 		/* Check for closing brace/bracket */
1145f8307e12SArchie Cobbs 		tok = ng_parse_get_token(s, off, &len);
1146f8307e12SArchie Cobbs 		if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1147f8307e12SArchie Cobbs 			*off += len;
1148f8307e12SArchie Cobbs 			break;
1149f8307e12SArchie Cobbs 		}
1150f8307e12SArchie Cobbs 
1151f8307e12SArchie Cobbs 		/* For arrays, the 'name' (ie, index) is optional, so
1152f8307e12SArchie Cobbs 		   distinguish name from values by seeing if the next
1153f8307e12SArchie Cobbs 		   token is an equals sign */
1154f8307e12SArchie Cobbs 		if (ctype != CT_STRUCT) {
1155f8307e12SArchie Cobbs 			int len2, off2;
1156f8307e12SArchie Cobbs 			char *eptr;
1157f8307e12SArchie Cobbs 
1158f8307e12SArchie Cobbs 			/* If an opening brace/bracket, index is implied */
1159f8307e12SArchie Cobbs 			if (tok == T_LBRACE || tok == T_LBRACKET) {
1160f8307e12SArchie Cobbs 				index = nextIndex++;
1161f8307e12SArchie Cobbs 				goto gotIndex;
1162f8307e12SArchie Cobbs 			}
1163f8307e12SArchie Cobbs 
1164f8307e12SArchie Cobbs 			/* Might be an index, might be a value, either way... */
1165f8307e12SArchie Cobbs 			if (tok != T_WORD) {
1166f8307e12SArchie Cobbs 				error = EINVAL;
1167f8307e12SArchie Cobbs 				goto done;
1168f8307e12SArchie Cobbs 			}
1169f8307e12SArchie Cobbs 
1170f8307e12SArchie Cobbs 			/* If no equals sign follows, index is implied */
1171f8307e12SArchie Cobbs 			off2 = *off + len;
1172f8307e12SArchie Cobbs 			if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1173f8307e12SArchie Cobbs 				index = nextIndex++;
1174f8307e12SArchie Cobbs 				goto gotIndex;
1175f8307e12SArchie Cobbs 			}
1176f8307e12SArchie Cobbs 
1177f8307e12SArchie Cobbs 			/* Index was specified explicitly; parse it */
1178f8307e12SArchie Cobbs 			index = (u_int)strtoul(s + *off, &eptr, 0);
1179f8307e12SArchie Cobbs 			if (index < 0 || eptr - (s + *off) != len) {
1180f8307e12SArchie Cobbs 				error = EINVAL;
1181f8307e12SArchie Cobbs 				goto done;
1182f8307e12SArchie Cobbs 			}
1183f8307e12SArchie Cobbs 			nextIndex = index + 1;
1184f8307e12SArchie Cobbs 			*off += len + len2;
1185f8307e12SArchie Cobbs gotIndex:
1186f8307e12SArchie Cobbs 		} else {			/* a structure field */
1187f8307e12SArchie Cobbs 			const struct ng_parse_struct_field *field = NULL;
1188f8307e12SArchie Cobbs 			const struct ng_parse_struct_info *si = type->info;
1189f8307e12SArchie Cobbs 
1190f8307e12SArchie Cobbs 			/* Find the field by name (required) in field list */
1191f8307e12SArchie Cobbs 			if (tok != T_WORD) {
1192f8307e12SArchie Cobbs 				error = EINVAL;
1193f8307e12SArchie Cobbs 				goto done;
1194f8307e12SArchie Cobbs 			}
1195f8307e12SArchie Cobbs 			for (index = 0; index < num; index++) {
1196f8307e12SArchie Cobbs 				field = &si->fields[index];
1197f8307e12SArchie Cobbs 				if (strncmp(&s[*off], field->name, len) == 0
1198f8307e12SArchie Cobbs 				    && field->name[len] == '\0')
1199f8307e12SArchie Cobbs 					break;
1200f8307e12SArchie Cobbs 			}
1201f8307e12SArchie Cobbs 			if (index == num) {
1202f8307e12SArchie Cobbs 				error = ENOENT;
1203f8307e12SArchie Cobbs 				goto done;
1204f8307e12SArchie Cobbs 			}
1205f8307e12SArchie Cobbs 			*off += len;
1206f8307e12SArchie Cobbs 
1207f8307e12SArchie Cobbs 			/* Get equals sign */
1208f8307e12SArchie Cobbs 			if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1209f8307e12SArchie Cobbs 				error = EINVAL;
1210f8307e12SArchie Cobbs 				goto done;
1211f8307e12SArchie Cobbs 			}
1212f8307e12SArchie Cobbs 			*off += len;
1213f8307e12SArchie Cobbs 		}
1214f8307e12SArchie Cobbs 
1215f8307e12SArchie Cobbs 		/* Check array index */
1216f8307e12SArchie Cobbs 		if (index >= num) {
1217f8307e12SArchie Cobbs 			error = E2BIG;
1218f8307e12SArchie Cobbs 			goto done;
1219f8307e12SArchie Cobbs 		}
1220f8307e12SArchie Cobbs 
1221f8307e12SArchie Cobbs 		/* Save value's position and skip over it for now */
1222f8307e12SArchie Cobbs 		if (foff[index] != 0) {
1223f8307e12SArchie Cobbs 			error = EALREADY;		/* duplicate */
1224f8307e12SArchie Cobbs 			goto done;
1225f8307e12SArchie Cobbs 		}
1226f8307e12SArchie Cobbs 		while (isspace(s[*off]))
1227f8307e12SArchie Cobbs 			(*off)++;
1228f8307e12SArchie Cobbs 		foff[index] = *off;
1229f8307e12SArchie Cobbs 		if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1230f8307e12SArchie Cobbs 			goto done;
1231f8307e12SArchie Cobbs 		*off += len;
1232f8307e12SArchie Cobbs 	}
1233f8307e12SArchie Cobbs 
1234f8307e12SArchie Cobbs 	/* Now build binary structure from supplied values and defaults */
1235f8307e12SArchie Cobbs 	for (blen = index = 0; index < num; index++) {
1236f8307e12SArchie Cobbs 		const struct ng_parse_type *const
1237f8307e12SArchie Cobbs 		    etype = ng_get_composite_etype(type, index, ctype);
1238f8307e12SArchie Cobbs 		int k, pad, vlen;
1239f8307e12SArchie Cobbs 
1240f8307e12SArchie Cobbs 		/* Zero-pad any alignment bytes */
1241f8307e12SArchie Cobbs 		pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1242f8307e12SArchie Cobbs 		for (k = 0; k < pad; k++) {
1243f8307e12SArchie Cobbs 			if (blen >= *buflen) {
1244f8307e12SArchie Cobbs 				error = ERANGE;
1245f8307e12SArchie Cobbs 				goto done;
1246f8307e12SArchie Cobbs 			}
1247f8307e12SArchie Cobbs 			buf[blen++] = 0;
1248f8307e12SArchie Cobbs 		}
1249f8307e12SArchie Cobbs 
1250f8307e12SArchie Cobbs 		/* Get value */
1251f8307e12SArchie Cobbs 		vlen = *buflen - blen;
1252f8307e12SArchie Cobbs 		if (foff[index] == 0) {		/* use default value */
1253f8307e12SArchie Cobbs 			error = ng_get_composite_elem_default(type, index,
1254f8307e12SArchie Cobbs 			    start, buf + blen, &vlen, ctype);
1255f8307e12SArchie Cobbs 		} else {			/* parse given value */
1256f8307e12SArchie Cobbs 			*off = foff[index];
1257f8307e12SArchie Cobbs 			error = INVOKE(etype, parse)(etype,
1258f8307e12SArchie Cobbs 			    s, off, start, buf + blen, &vlen);
1259f8307e12SArchie Cobbs 		}
1260f8307e12SArchie Cobbs 		if (error != 0)
1261f8307e12SArchie Cobbs 			goto done;
1262f8307e12SArchie Cobbs 		blen += vlen;
1263f8307e12SArchie Cobbs 	}
1264f8307e12SArchie Cobbs 
1265f8307e12SArchie Cobbs 	/* Make total composite structure size a multiple of its alignment */
1266f8307e12SArchie Cobbs 	if ((align = ALIGNMENT(type)) != 0) {
1267f8307e12SArchie Cobbs 		while (blen % align != 0) {
1268f8307e12SArchie Cobbs 			if (blen >= *buflen) {
1269f8307e12SArchie Cobbs 				error = ERANGE;
1270f8307e12SArchie Cobbs 				goto done;
1271f8307e12SArchie Cobbs 			}
1272f8307e12SArchie Cobbs 			buf[blen++] = 0;
1273f8307e12SArchie Cobbs 		}
1274f8307e12SArchie Cobbs 	}
1275f8307e12SArchie Cobbs 
1276f8307e12SArchie Cobbs 	/* Done */
1277f8307e12SArchie Cobbs 	*buflen = blen;
1278f8307e12SArchie Cobbs done:
1279d99b0733SArchie Cobbs 	if (foff != NULL)
1280f8307e12SArchie Cobbs 		FREE(foff, M_NETGRAPH);
1281f8307e12SArchie Cobbs 	return (error);
1282f8307e12SArchie Cobbs }
1283f8307e12SArchie Cobbs 
1284f8307e12SArchie Cobbs /*
1285f8307e12SArchie Cobbs  * Convert an array or structure from binary to ASCII
1286f8307e12SArchie Cobbs  */
1287f8307e12SArchie Cobbs static int
1288f8307e12SArchie Cobbs ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1289f8307e12SArchie Cobbs 	int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1290f8307e12SArchie Cobbs {
1291f8307e12SArchie Cobbs 	const int num = ng_get_composite_len(type, data, data + *off, ctype);
12928db3c6cdSArchie Cobbs 	const int workSize = 20 * 1024;		/* XXX hard coded constant */
1293f8307e12SArchie Cobbs 	int nextIndex = 0, didOne = 0;
1294f8307e12SArchie Cobbs 	int error, index;
12958db3c6cdSArchie Cobbs 	u_char *workBuf;
12968db3c6cdSArchie Cobbs 
12978db3c6cdSArchie Cobbs 	/* Get workspace for checking default values */
12988db3c6cdSArchie Cobbs 	MALLOC(workBuf, u_char *, workSize, M_NETGRAPH, M_NOWAIT);
12998db3c6cdSArchie Cobbs 	if (workBuf == NULL)
13008db3c6cdSArchie Cobbs 		return (ENOMEM);
1301f8307e12SArchie Cobbs 
1302f8307e12SArchie Cobbs 	/* Opening brace/bracket */
1303f8307e12SArchie Cobbs 	NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1304f8307e12SArchie Cobbs 
1305f8307e12SArchie Cobbs 	/* Do each item */
1306f8307e12SArchie Cobbs 	for (index = 0; index < num; index++) {
1307f8307e12SArchie Cobbs 		const struct ng_parse_type *const
1308f8307e12SArchie Cobbs 		    etype = ng_get_composite_etype(type, index, ctype);
1309f8307e12SArchie Cobbs 
1310f8307e12SArchie Cobbs 		/* Skip any alignment pad bytes */
1311f8307e12SArchie Cobbs 		*off += ng_parse_get_elem_pad(type, index, ctype, *off);
1312f8307e12SArchie Cobbs 
1313f8307e12SArchie Cobbs 		/* See if element is equal to its default value; skip if so */
13148db3c6cdSArchie Cobbs 		if (*off < workSize) {
13158db3c6cdSArchie Cobbs 			int tempsize = workSize - *off;
1316f8307e12SArchie Cobbs 
13178db3c6cdSArchie Cobbs 			bcopy(data, workBuf, *off);
13188db3c6cdSArchie Cobbs 			if (ng_get_composite_elem_default(type, index, workBuf,
13198db3c6cdSArchie Cobbs 			      workBuf + *off, &tempsize, ctype) == 0
13208db3c6cdSArchie Cobbs 			    && bcmp(workBuf + *off,
13218db3c6cdSArchie Cobbs 			      data + *off, tempsize) == 0) {
1322f8307e12SArchie Cobbs 				*off += tempsize;
1323f8307e12SArchie Cobbs 				continue;
1324f8307e12SArchie Cobbs 			}
1325f8307e12SArchie Cobbs 		}
1326f8307e12SArchie Cobbs 
1327f8307e12SArchie Cobbs 		/* Print name= */
1328f8307e12SArchie Cobbs 		NG_PARSE_APPEND(" ");
1329f8307e12SArchie Cobbs 		if (ctype != CT_STRUCT) {
1330f8307e12SArchie Cobbs 			if (index != nextIndex) {
1331f8307e12SArchie Cobbs 				nextIndex = index;
1332f8307e12SArchie Cobbs 				NG_PARSE_APPEND("%d=", index);
1333f8307e12SArchie Cobbs 			}
1334f8307e12SArchie Cobbs 			nextIndex++;
1335f8307e12SArchie Cobbs 		} else {
1336f8307e12SArchie Cobbs 			const struct ng_parse_struct_info *si = type->info;
1337f8307e12SArchie Cobbs 
1338f8307e12SArchie Cobbs 			NG_PARSE_APPEND("%s=", si->fields[index].name);
1339f8307e12SArchie Cobbs 		}
1340f8307e12SArchie Cobbs 
1341f8307e12SArchie Cobbs 		/* Print value */
1342f8307e12SArchie Cobbs 		if ((error = INVOKE(etype, unparse)
13438db3c6cdSArchie Cobbs 		    (etype, data, off, cbuf, cbuflen)) != 0) {
13448db3c6cdSArchie Cobbs 			FREE(workBuf, M_NETGRAPH);
1345f8307e12SArchie Cobbs 			return (error);
13468db3c6cdSArchie Cobbs 		}
1347f8307e12SArchie Cobbs 		cbuflen -= strlen(cbuf);
1348f8307e12SArchie Cobbs 		cbuf += strlen(cbuf);
1349f8307e12SArchie Cobbs 		didOne = 1;
1350f8307e12SArchie Cobbs 	}
13518db3c6cdSArchie Cobbs 	FREE(workBuf, M_NETGRAPH);
1352f8307e12SArchie Cobbs 
1353f8307e12SArchie Cobbs 	/* Closing brace/bracket */
1354f8307e12SArchie Cobbs 	NG_PARSE_APPEND("%s%c",
1355f8307e12SArchie Cobbs 	    didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1356f8307e12SArchie Cobbs 	return (0);
1357f8307e12SArchie Cobbs }
1358f8307e12SArchie Cobbs 
1359f8307e12SArchie Cobbs /*
1360f8307e12SArchie Cobbs  * Generate the default value for an element of an array or structure
1361f8307e12SArchie Cobbs  * Returns EOPNOTSUPP if default value is unspecified.
1362f8307e12SArchie Cobbs  */
1363f8307e12SArchie Cobbs static int
1364f8307e12SArchie Cobbs ng_get_composite_elem_default(const struct ng_parse_type *type,
1365f8307e12SArchie Cobbs 	int index, const u_char *const start, u_char *buf, int *buflen,
1366f8307e12SArchie Cobbs 	const enum comptype ctype)
1367f8307e12SArchie Cobbs {
1368f8307e12SArchie Cobbs 	const struct ng_parse_type *etype;
1369f8307e12SArchie Cobbs 	ng_getDefault_t *func;
1370f8307e12SArchie Cobbs 
1371f8307e12SArchie Cobbs 	switch (ctype) {
1372f8307e12SArchie Cobbs 	case CT_STRUCT:
1373f8307e12SArchie Cobbs 		break;
1374f8307e12SArchie Cobbs 	case CT_ARRAY:
1375f8307e12SArchie Cobbs 	    {
1376f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1377f8307e12SArchie Cobbs 
1378f8307e12SArchie Cobbs 		if (ai->getDefault != NULL) {
1379f8307e12SArchie Cobbs 			return (*ai->getDefault)(type,
1380f8307e12SArchie Cobbs 			    index, start, buf, buflen);
1381f8307e12SArchie Cobbs 		}
1382f8307e12SArchie Cobbs 		break;
1383f8307e12SArchie Cobbs 	    }
1384f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1385f8307e12SArchie Cobbs 	    {
1386f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1387f8307e12SArchie Cobbs 
1388f8307e12SArchie Cobbs 		if (*fi->getDefault != NULL) {
1389f8307e12SArchie Cobbs 			return (*fi->getDefault)(type,
1390f8307e12SArchie Cobbs 			    index, start, buf, buflen);
1391f8307e12SArchie Cobbs 		}
1392f8307e12SArchie Cobbs 		break;
1393f8307e12SArchie Cobbs 	    }
1394f8307e12SArchie Cobbs 	default:
1395f8307e12SArchie Cobbs 	    panic("%s", __FUNCTION__);
1396f8307e12SArchie Cobbs 	}
1397f8307e12SArchie Cobbs 
1398f8307e12SArchie Cobbs 	/* Default to element type default */
1399f8307e12SArchie Cobbs 	etype = ng_get_composite_etype(type, index, ctype);
1400f8307e12SArchie Cobbs 	func = METHOD(etype, getDefault);
1401f8307e12SArchie Cobbs 	if (func == NULL)
1402f8307e12SArchie Cobbs 		return (EOPNOTSUPP);
1403f8307e12SArchie Cobbs 	return (*func)(etype, start, buf, buflen);
1404f8307e12SArchie Cobbs }
1405f8307e12SArchie Cobbs 
1406f8307e12SArchie Cobbs /*
1407f8307e12SArchie Cobbs  * Get the number of elements in a struct, variable or fixed array.
1408f8307e12SArchie Cobbs  */
1409f8307e12SArchie Cobbs static int
1410f8307e12SArchie Cobbs ng_get_composite_len(const struct ng_parse_type *type,
1411f8307e12SArchie Cobbs 	const u_char *const start, const u_char *buf,
1412f8307e12SArchie Cobbs 	const enum comptype ctype)
1413f8307e12SArchie Cobbs {
1414f8307e12SArchie Cobbs 	switch (ctype) {
1415f8307e12SArchie Cobbs 	case CT_STRUCT:
1416f8307e12SArchie Cobbs 	    {
1417f8307e12SArchie Cobbs 		const struct ng_parse_struct_info *const si = type->info;
1418f8307e12SArchie Cobbs 		int numFields = 0;
1419f8307e12SArchie Cobbs 
1420f8307e12SArchie Cobbs 		for (numFields = 0; ; numFields++) {
1421f8307e12SArchie Cobbs 			const struct ng_parse_struct_field *const
1422f8307e12SArchie Cobbs 				fi = &si->fields[numFields];
1423f8307e12SArchie Cobbs 
1424f8307e12SArchie Cobbs 			if (fi->name == NULL)
1425f8307e12SArchie Cobbs 				break;
1426f8307e12SArchie Cobbs 		}
1427f8307e12SArchie Cobbs 		return (numFields);
1428f8307e12SArchie Cobbs 	    }
1429f8307e12SArchie Cobbs 	case CT_ARRAY:
1430f8307e12SArchie Cobbs 	    {
1431f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1432f8307e12SArchie Cobbs 
1433f8307e12SArchie Cobbs 		return (*ai->getLength)(type, start, buf);
1434f8307e12SArchie Cobbs 	    }
1435f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1436f8307e12SArchie Cobbs 	    {
1437f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1438f8307e12SArchie Cobbs 
1439f8307e12SArchie Cobbs 		return fi->length;
1440f8307e12SArchie Cobbs 	    }
1441f8307e12SArchie Cobbs 	default:
1442f8307e12SArchie Cobbs 	    panic("%s", __FUNCTION__);
1443f8307e12SArchie Cobbs 	}
1444f8307e12SArchie Cobbs 	return (0);
1445f8307e12SArchie Cobbs }
1446f8307e12SArchie Cobbs 
1447f8307e12SArchie Cobbs /*
1448f8307e12SArchie Cobbs  * Return the type of the index'th element of a composite structure
1449f8307e12SArchie Cobbs  */
1450f8307e12SArchie Cobbs static const struct ng_parse_type *
1451f8307e12SArchie Cobbs ng_get_composite_etype(const struct ng_parse_type *type,
1452f8307e12SArchie Cobbs 	int index, const enum comptype ctype)
1453f8307e12SArchie Cobbs {
1454f8307e12SArchie Cobbs 	const struct ng_parse_type *etype = NULL;
1455f8307e12SArchie Cobbs 
1456f8307e12SArchie Cobbs 	switch (ctype) {
1457f8307e12SArchie Cobbs 	case CT_STRUCT:
1458f8307e12SArchie Cobbs 	    {
1459f8307e12SArchie Cobbs 		const struct ng_parse_struct_info *const si = type->info;
1460f8307e12SArchie Cobbs 
1461f8307e12SArchie Cobbs 		etype = si->fields[index].type;
1462f8307e12SArchie Cobbs 		break;
1463f8307e12SArchie Cobbs 	    }
1464f8307e12SArchie Cobbs 	case CT_ARRAY:
1465f8307e12SArchie Cobbs 	    {
1466f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1467f8307e12SArchie Cobbs 
1468f8307e12SArchie Cobbs 		etype = ai->elementType;
1469f8307e12SArchie Cobbs 		break;
1470f8307e12SArchie Cobbs 	    }
1471f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1472f8307e12SArchie Cobbs 	    {
1473f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1474f8307e12SArchie Cobbs 
1475f8307e12SArchie Cobbs 		etype = fi->elementType;
1476f8307e12SArchie Cobbs 		break;
1477f8307e12SArchie Cobbs 	    }
1478f8307e12SArchie Cobbs 	default:
1479f8307e12SArchie Cobbs 	    panic("%s", __FUNCTION__);
1480f8307e12SArchie Cobbs 	}
1481f8307e12SArchie Cobbs 	return (etype);
1482f8307e12SArchie Cobbs }
1483f8307e12SArchie Cobbs 
1484f8307e12SArchie Cobbs /*
1485f8307e12SArchie Cobbs  * Get the number of bytes to skip to align for the next
1486f8307e12SArchie Cobbs  * element in a composite structure.
1487f8307e12SArchie Cobbs  */
1488f8307e12SArchie Cobbs static int
1489f8307e12SArchie Cobbs ng_parse_get_elem_pad(const struct ng_parse_type *type,
1490f8307e12SArchie Cobbs 	int index, enum comptype ctype, int posn)
1491f8307e12SArchie Cobbs {
1492f8307e12SArchie Cobbs 	const struct ng_parse_type *const
1493f8307e12SArchie Cobbs 	    etype = ng_get_composite_etype(type, index, ctype);
1494f8307e12SArchie Cobbs 	int align;
1495f8307e12SArchie Cobbs 
1496f8307e12SArchie Cobbs 	/* Get element's alignment, and possibly override */
1497f8307e12SArchie Cobbs 	align = ALIGNMENT(etype);
1498f8307e12SArchie Cobbs 	if (ctype == CT_STRUCT) {
1499f8307e12SArchie Cobbs 		const struct ng_parse_struct_info *si = type->info;
1500f8307e12SArchie Cobbs 
1501f8307e12SArchie Cobbs 		if (si->fields[index].alignment != 0)
1502f8307e12SArchie Cobbs 			align = si->fields[index].alignment;
1503f8307e12SArchie Cobbs 	}
1504f8307e12SArchie Cobbs 
1505f8307e12SArchie Cobbs 	/* Return number of bytes to skip to align */
1506f8307e12SArchie Cobbs 	return (align ? (align - (posn % align)) % align : 0);
1507f8307e12SArchie Cobbs }
1508f8307e12SArchie Cobbs 
1509f8307e12SArchie Cobbs /************************************************************************
1510f8307e12SArchie Cobbs 			PARSING HELPER ROUTINES
1511f8307e12SArchie Cobbs  ************************************************************************/
1512f8307e12SArchie Cobbs 
1513f8307e12SArchie Cobbs /*
1514f8307e12SArchie Cobbs  * Skip over a value
1515f8307e12SArchie Cobbs  */
1516f8307e12SArchie Cobbs static int
1517f8307e12SArchie Cobbs ng_parse_skip_value(const char *s, int off0, int *lenp)
1518f8307e12SArchie Cobbs {
1519f8307e12SArchie Cobbs 	int len, nbracket, nbrace;
1520f8307e12SArchie Cobbs 	int off = off0;
1521f8307e12SArchie Cobbs 
1522f8307e12SArchie Cobbs 	len = nbracket = nbrace = 0;
1523f8307e12SArchie Cobbs 	do {
1524f8307e12SArchie Cobbs 		switch (ng_parse_get_token(s, &off, &len)) {
1525f8307e12SArchie Cobbs 		case T_LBRACKET:
1526f8307e12SArchie Cobbs 			nbracket++;
1527f8307e12SArchie Cobbs 			break;
1528f8307e12SArchie Cobbs 		case T_LBRACE:
1529f8307e12SArchie Cobbs 			nbrace++;
1530f8307e12SArchie Cobbs 			break;
1531f8307e12SArchie Cobbs 		case T_RBRACKET:
1532f8307e12SArchie Cobbs 			if (nbracket-- == 0)
1533f8307e12SArchie Cobbs 				return (EINVAL);
1534f8307e12SArchie Cobbs 			break;
1535f8307e12SArchie Cobbs 		case T_RBRACE:
1536f8307e12SArchie Cobbs 			if (nbrace-- == 0)
1537f8307e12SArchie Cobbs 				return (EINVAL);
1538f8307e12SArchie Cobbs 			break;
1539f8307e12SArchie Cobbs 		case T_EOF:
1540f8307e12SArchie Cobbs 			return (EINVAL);
1541f8307e12SArchie Cobbs 		default:
1542f8307e12SArchie Cobbs 			break;
1543f8307e12SArchie Cobbs 		}
1544f8307e12SArchie Cobbs 		off += len;
1545f8307e12SArchie Cobbs 	} while (nbracket > 0 || nbrace > 0);
1546f8307e12SArchie Cobbs 	*lenp = off - off0;
1547f8307e12SArchie Cobbs 	return (0);
1548f8307e12SArchie Cobbs }
1549f8307e12SArchie Cobbs 
1550f8307e12SArchie Cobbs /*
1551f8307e12SArchie Cobbs  * Find the next token in the string, starting at offset *startp.
1552f8307e12SArchie Cobbs  * Returns the token type, with *startp pointing to the first char
1553f8307e12SArchie Cobbs  * and *lenp the length.
1554f8307e12SArchie Cobbs  */
1555f8307e12SArchie Cobbs enum ng_parse_token
1556f8307e12SArchie Cobbs ng_parse_get_token(const char *s, int *startp, int *lenp)
1557f8307e12SArchie Cobbs {
1558f8307e12SArchie Cobbs 	char *t;
1559f8307e12SArchie Cobbs 	int i;
1560f8307e12SArchie Cobbs 
1561f8307e12SArchie Cobbs 	while (isspace(s[*startp]))
1562f8307e12SArchie Cobbs 		(*startp)++;
1563f8307e12SArchie Cobbs 	switch (s[*startp]) {
1564f8307e12SArchie Cobbs 	case '\0':
1565f8307e12SArchie Cobbs 		*lenp = 0;
1566f8307e12SArchie Cobbs 		return T_EOF;
1567f8307e12SArchie Cobbs 	case '{':
1568f8307e12SArchie Cobbs 		*lenp = 1;
1569f8307e12SArchie Cobbs 		return T_LBRACE;
1570f8307e12SArchie Cobbs 	case '}':
1571f8307e12SArchie Cobbs 		*lenp = 1;
1572f8307e12SArchie Cobbs 		return T_RBRACE;
1573f8307e12SArchie Cobbs 	case '[':
1574f8307e12SArchie Cobbs 		*lenp = 1;
1575f8307e12SArchie Cobbs 		return T_LBRACKET;
1576f8307e12SArchie Cobbs 	case ']':
1577f8307e12SArchie Cobbs 		*lenp = 1;
1578f8307e12SArchie Cobbs 		return T_RBRACKET;
1579f8307e12SArchie Cobbs 	case '=':
1580f8307e12SArchie Cobbs 		*lenp = 1;
1581f8307e12SArchie Cobbs 		return T_EQUALS;
1582f8307e12SArchie Cobbs 	case '"':
158327121ab1SBrian Somers 		if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1584f8307e12SArchie Cobbs 			return T_ERROR;
1585f8307e12SArchie Cobbs 		FREE(t, M_NETGRAPH);
1586f8307e12SArchie Cobbs 		return T_STRING;
1587f8307e12SArchie Cobbs 	default:
1588f8307e12SArchie Cobbs 		for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1589f8307e12SArchie Cobbs 		    && s[i] != '{' && s[i] != '}' && s[i] != '['
1590f8307e12SArchie Cobbs 		    && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1591f8307e12SArchie Cobbs 			;
1592f8307e12SArchie Cobbs 		*lenp = i - *startp;
1593f8307e12SArchie Cobbs 		return T_WORD;
1594f8307e12SArchie Cobbs 	}
1595f8307e12SArchie Cobbs }
1596f8307e12SArchie Cobbs 
1597f8307e12SArchie Cobbs /*
1598f8307e12SArchie Cobbs  * Get a string token, which must be enclosed in double quotes.
1599f8307e12SArchie Cobbs  * The normal C backslash escapes are recognized.
1600f8307e12SArchie Cobbs  */
1601f8307e12SArchie Cobbs char *
160227121ab1SBrian Somers ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1603f8307e12SArchie Cobbs {
1604f8307e12SArchie Cobbs 	char *cbuf, *p;
1605f8307e12SArchie Cobbs 	int start, off;
160627121ab1SBrian Somers 	int slen;
1607f8307e12SArchie Cobbs 
1608f8307e12SArchie Cobbs 	while (isspace(s[*startp]))
1609f8307e12SArchie Cobbs 		(*startp)++;
1610f8307e12SArchie Cobbs 	start = *startp;
1611f8307e12SArchie Cobbs 	if (s[*startp] != '"')
1612f8307e12SArchie Cobbs 		return (NULL);
1613f8307e12SArchie Cobbs 	MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT);
1614f8307e12SArchie Cobbs 	if (cbuf == NULL)
1615f8307e12SArchie Cobbs 		return (NULL);
1616f8307e12SArchie Cobbs 	strcpy(cbuf, s + start + 1);
161727121ab1SBrian Somers 	for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1618f8307e12SArchie Cobbs 		if (*p == '"') {
1619f8307e12SArchie Cobbs 			*p = '\0';
1620f8307e12SArchie Cobbs 			*lenp = off + 1;
162127121ab1SBrian Somers 			if (slenp != NULL)
162227121ab1SBrian Somers 				*slenp = slen;
1623f8307e12SArchie Cobbs 			return (cbuf);
1624f8307e12SArchie Cobbs 		} else if (p[0] == '\\' && p[1] != '\0') {
1625f8307e12SArchie Cobbs 			int x, k;
1626f8307e12SArchie Cobbs 			char *v;
1627f8307e12SArchie Cobbs 
1628f8307e12SArchie Cobbs 			strcpy(p, p + 1);
1629f8307e12SArchie Cobbs 			v = p;
1630f8307e12SArchie Cobbs 			switch (*p) {
1631f8307e12SArchie Cobbs 			case 't':
1632f8307e12SArchie Cobbs 				*v = '\t';
1633f8307e12SArchie Cobbs 				off++;
1634f8307e12SArchie Cobbs 				continue;
1635f8307e12SArchie Cobbs 			case 'n':
1636f8307e12SArchie Cobbs 				*v = '\n';
1637f8307e12SArchie Cobbs 				off++;
1638f8307e12SArchie Cobbs 				continue;
1639f8307e12SArchie Cobbs 			case 'r':
1640f8307e12SArchie Cobbs 				*v = '\r';
1641f8307e12SArchie Cobbs 				off++;
1642f8307e12SArchie Cobbs 				continue;
1643f8307e12SArchie Cobbs 			case 'v':
1644f8307e12SArchie Cobbs 				*v =  '\v';
1645f8307e12SArchie Cobbs 				off++;
1646f8307e12SArchie Cobbs 				continue;
1647f8307e12SArchie Cobbs 			case 'f':
1648f8307e12SArchie Cobbs 				*v =  '\f';
1649f8307e12SArchie Cobbs 				off++;
1650f8307e12SArchie Cobbs 				continue;
1651f8307e12SArchie Cobbs 			case '"':
1652f8307e12SArchie Cobbs 				*v =  '"';
1653f8307e12SArchie Cobbs 				off++;
1654f8307e12SArchie Cobbs 				continue;
1655f8307e12SArchie Cobbs 			case '0': case '1': case '2': case '3':
1656f8307e12SArchie Cobbs 			case '4': case '5': case '6': case '7':
1657f8307e12SArchie Cobbs 				for (x = k = 0;
1658f8307e12SArchie Cobbs 				    k < 3 && *v >= '0' && *v <= '7'; v++) {
1659f8307e12SArchie Cobbs 					x = (x << 3) + (*v - '0');
1660f8307e12SArchie Cobbs 					off++;
1661f8307e12SArchie Cobbs 				}
1662f8307e12SArchie Cobbs 				*--v = (char)x;
1663f8307e12SArchie Cobbs 				break;
1664f8307e12SArchie Cobbs 			case 'x':
1665f8307e12SArchie Cobbs 				for (v++, x = k = 0;
1666f8307e12SArchie Cobbs 				    k < 2 && isxdigit(*v); v++) {
1667f8307e12SArchie Cobbs 					x = (x << 4) + (isdigit(*v) ?
1668f8307e12SArchie Cobbs 					      (*v - '0') :
1669f8307e12SArchie Cobbs 					      (tolower(*v) - 'a' + 10));
1670f8307e12SArchie Cobbs 					off++;
1671f8307e12SArchie Cobbs 				}
1672f8307e12SArchie Cobbs 				*--v = (char)x;
1673f8307e12SArchie Cobbs 				break;
1674f8307e12SArchie Cobbs 			default:
1675f8307e12SArchie Cobbs 				continue;
1676f8307e12SArchie Cobbs 			}
1677f8307e12SArchie Cobbs 			strcpy(p, v);
1678f8307e12SArchie Cobbs 		}
1679f8307e12SArchie Cobbs 	}
1680f8307e12SArchie Cobbs 	return (NULL);		/* no closing quote */
1681f8307e12SArchie Cobbs }
1682f8307e12SArchie Cobbs 
1683f8307e12SArchie Cobbs /*
1684f8307e12SArchie Cobbs  * Encode a string so it can be safely put in double quotes.
168527121ab1SBrian Somers  * Caller must free the result. Exactly "slen" characters
168627121ab1SBrian Somers  * are encoded.
1687f8307e12SArchie Cobbs  */
1688f8307e12SArchie Cobbs char *
168927121ab1SBrian Somers ng_encode_string(const char *raw, int slen)
1690f8307e12SArchie Cobbs {
1691f8307e12SArchie Cobbs 	char *cbuf;
1692f8307e12SArchie Cobbs 	int off = 0;
169327121ab1SBrian Somers 	int i;
1694f8307e12SArchie Cobbs 
1695f8307e12SArchie Cobbs 	MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1696f8307e12SArchie Cobbs 	if (cbuf == NULL)
1697f8307e12SArchie Cobbs 		return (NULL);
1698f8307e12SArchie Cobbs 	cbuf[off++] = '"';
169927121ab1SBrian Somers 	for (i = 0; i < slen; i++, raw++) {
1700f8307e12SArchie Cobbs 		switch (*raw) {
1701f8307e12SArchie Cobbs 		case '\t':
1702f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1703f8307e12SArchie Cobbs 			cbuf[off++] = 't';
1704f8307e12SArchie Cobbs 			break;
1705f8307e12SArchie Cobbs 		case '\f':
1706f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1707f8307e12SArchie Cobbs 			cbuf[off++] = 'f';
1708f8307e12SArchie Cobbs 			break;
1709f8307e12SArchie Cobbs 		case '\n':
1710f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1711f8307e12SArchie Cobbs 			cbuf[off++] = 'n';
1712f8307e12SArchie Cobbs 			break;
1713f8307e12SArchie Cobbs 		case '\r':
1714f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1715f8307e12SArchie Cobbs 			cbuf[off++] = 'r';
1716f8307e12SArchie Cobbs 			break;
1717f8307e12SArchie Cobbs 		case '\v':
1718f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1719f8307e12SArchie Cobbs 			cbuf[off++] = 'v';
1720f8307e12SArchie Cobbs 			break;
1721f8307e12SArchie Cobbs 		case '"':
1722f8307e12SArchie Cobbs 		case '\\':
1723f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1724f8307e12SArchie Cobbs 			cbuf[off++] = *raw;
1725f8307e12SArchie Cobbs 			break;
1726f8307e12SArchie Cobbs 		default:
1727f8307e12SArchie Cobbs 			if (*raw < 0x20 || *raw > 0x7e) {
1728f8307e12SArchie Cobbs 				off += sprintf(cbuf + off,
1729f8307e12SArchie Cobbs 				    "\\x%02x", (u_char)*raw);
1730f8307e12SArchie Cobbs 				break;
1731f8307e12SArchie Cobbs 			}
1732f8307e12SArchie Cobbs 			cbuf[off++] = *raw;
1733f8307e12SArchie Cobbs 			break;
1734f8307e12SArchie Cobbs 		}
1735f8307e12SArchie Cobbs 	}
1736f8307e12SArchie Cobbs 	cbuf[off++] = '"';
1737f8307e12SArchie Cobbs 	cbuf[off] = '\0';
1738f8307e12SArchie Cobbs 	return (cbuf);
1739f8307e12SArchie Cobbs }
1740f8307e12SArchie Cobbs 
1741f8307e12SArchie Cobbs /************************************************************************
1742f8307e12SArchie Cobbs 			VIRTUAL METHOD LOOKUP
1743f8307e12SArchie Cobbs  ************************************************************************/
1744f8307e12SArchie Cobbs 
1745f8307e12SArchie Cobbs static ng_parse_t *
1746f8307e12SArchie Cobbs ng_get_parse_method(const struct ng_parse_type *t)
1747f8307e12SArchie Cobbs {
1748f8307e12SArchie Cobbs 	while (t != NULL && t->parse == NULL)
1749f8307e12SArchie Cobbs 		t = t->supertype;
1750f8307e12SArchie Cobbs 	return (t ? t->parse : NULL);
1751f8307e12SArchie Cobbs }
1752f8307e12SArchie Cobbs 
1753f8307e12SArchie Cobbs static ng_unparse_t *
1754f8307e12SArchie Cobbs ng_get_unparse_method(const struct ng_parse_type *t)
1755f8307e12SArchie Cobbs {
1756f8307e12SArchie Cobbs 	while (t != NULL && t->unparse == NULL)
1757f8307e12SArchie Cobbs 		t = t->supertype;
1758f8307e12SArchie Cobbs 	return (t ? t->unparse : NULL);
1759f8307e12SArchie Cobbs }
1760f8307e12SArchie Cobbs 
1761f8307e12SArchie Cobbs static ng_getDefault_t *
1762f8307e12SArchie Cobbs ng_get_getDefault_method(const struct ng_parse_type *t)
1763f8307e12SArchie Cobbs {
1764f8307e12SArchie Cobbs 	while (t != NULL && t->getDefault == NULL)
1765f8307e12SArchie Cobbs 		t = t->supertype;
1766f8307e12SArchie Cobbs 	return (t ? t->getDefault : NULL);
1767f8307e12SArchie Cobbs }
1768f8307e12SArchie Cobbs 
1769f8307e12SArchie Cobbs static ng_getAlign_t *
1770f8307e12SArchie Cobbs ng_get_getAlign_method(const struct ng_parse_type *t)
1771f8307e12SArchie Cobbs {
1772f8307e12SArchie Cobbs 	while (t != NULL && t->getAlign == NULL)
1773f8307e12SArchie Cobbs 		t = t->supertype;
1774f8307e12SArchie Cobbs 	return (t ? t->getAlign : NULL);
1775f8307e12SArchie Cobbs }
1776f8307e12SArchie Cobbs 
1777