ng_parse.h revision 97685
153913Sarchie
253913Sarchie/*
353913Sarchie * ng_parse.h
453913Sarchie *
553913Sarchie * Copyright (c) 1999 Whistle Communications, Inc.
653913Sarchie * All rights reserved.
753913Sarchie *
853913Sarchie * Subject to the following obligations and disclaimer of warranty, use and
953913Sarchie * redistribution of this software, in source or object code forms, with or
1053913Sarchie * without modifications are expressly permitted by Whistle Communications;
1153913Sarchie * provided, however, that:
1253913Sarchie * 1. Any and all reproductions of the source or object code must include the
1353913Sarchie *    copyright notice above and the following disclaimer of warranties; and
1453913Sarchie * 2. No rights are granted, in any manner or form, to use Whistle
1553913Sarchie *    Communications, Inc. trademarks, including the mark "WHISTLE
1653913Sarchie *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1753913Sarchie *    such appears in the above copyright notice or in the software.
1853913Sarchie *
1953913Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2053913Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2153913Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2253913Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2353913Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2453913Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2553913Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2653913Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2753913Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2853913Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2953913Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3053913Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3153913Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3253913Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3353913Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3453913Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3553913Sarchie * OF SUCH DAMAGE.
3653913Sarchie *
3767506Sjulian * Author: Archie Cobbs <archie@freebsd.org>
3853913Sarchie *
3953913Sarchie * $Whistle: ng_parse.h,v 1.2 1999/11/29 01:43:48 archie Exp $
4053913Sarchie * $FreeBSD: head/sys/netgraph/ng_parse.h 97685 2002-05-31 23:48:03Z archie $
4153913Sarchie */
4253913Sarchie
4353913Sarchie#ifndef _NETGRAPH_PARSE_H_
4453913Sarchie#define _NETGRAPH_PARSE_H_
4553913Sarchie
4653913Sarchie/*
4753913Sarchie
4853913Sarchie  This defines a library of routines for converting between various C
4953913Sarchie  language types in binary form and ASCII strings.  Types are user
5053998Sarchie  definable.  Several pre-defined types are supplied, for some common
5153998Sarchie  C types: structures, variable and fixed length arrays, integer types,
5253998Sarchie  variable and fixed length strings, IP addresses, etc.
5353913Sarchie
5453998Sarchie  A netgraph node type may provide a list of types that correspond to
5553998Sarchie  the structures it expects to send and receive in the arguments field
5653998Sarchie  of a control message.  This allows these messages to be converted
5753998Sarchie  between their native binary form and the corresponding ASCII form.
5853998Sarchie
5953998Sarchie  A future use of the ASCII form may be for inter-machine communication
6053998Sarchie  of control messages, because the ASCII form is machine independent
6153998Sarchie  whereas the native binary form is not.
6253998Sarchie
6353913Sarchie  Syntax
6453913Sarchie  ------
6553913Sarchie
6653913Sarchie    Structures:
6753913Sarchie
6853913Sarchie      '{' [ <name>=<value> ... ] '}'
6953913Sarchie
7053913Sarchie      Omitted fields have their default values by implication.
7153913Sarchie      The order in which the fields are specified does not matter.
7253913Sarchie
7353913Sarchie    Arrays:
7453913Sarchie
7553913Sarchie      '[' [ [index=]<value> ... ] ']'
7653913Sarchie
7753913Sarchie      Element value may be specified with or without the "<index>=" prefix;
7853913Sarchie      If omitted, the index after the previous element is used.
7953913Sarchie      Omitted fields have their default values by implication.
8053913Sarchie
8153913Sarchie    Strings:
8253913Sarchie
8353913Sarchie      "foo bar blah\r\n"
8453913Sarchie
8553913Sarchie      That is, strings are specified just like C strings. The usual
8653913Sarchie      backslash escapes are accepted.
8753913Sarchie
8853998Sarchie    Other simple types (integers, IP addresses) have their obvious forms.
8953913Sarchie
9053913Sarchie  Example
9153913Sarchie  -------
9253913Sarchie
9353998Sarchie    Suppose we have a netgraph command that takes as an argument
9453998Sarchie    a 'struct foo' shown below.  Here is an example of a possible
9553998Sarchie    value for the structure, and the corresponding ASCII encoding
9653998Sarchie    of that value:
9753913Sarchie
9853998Sarchie	Structure			Binary value
9953998Sarchie	---------			------------
10053913Sarchie
10153998Sarchie	struct foo {
10253998Sarchie	    struct in_addr ip;  	01 02 03 04
10353998Sarchie	    int bar;			00 00 00 00
10453998Sarchie	    char label[8];		61 62 63 0a 00 00 00 00
10553998Sarchie	    u_char alen;		03 00
10653998Sarchie	    short ary[0];	  	05 00 00 00 0a 00
10753998Sarchie	};
10853913Sarchie
10953998Sarchie	ASCII value
11053998Sarchie	-----------
11153913Sarchie
11253998Sarchie	{ ip=1.2.3.4 label="abc\n" alen=3 ary=[ 5 2=10 ] }
11353998Sarchie
11453998Sarchie    Note that omitted fields and array elements get their default
11553998Sarchie    values ("bar" and ary[2]), and that the alignment is handled
11653998Sarchie    automatically (the extra 00 byte after "num").  Also, since byte
11753998Sarchie    order and alignment are inherently machine dependent, so is this
11853998Sarchie    conversion process.  The above example shows an x86 (little
11953998Sarchie    endian) encoding.  Also the above example is tricky because the
12053998Sarchie    structure is variable length, depending on 'alen', the number of
12153998Sarchie    elements in the array 'ary'.
12253998Sarchie
12353998Sarchie    Here is how one would define a parse type for the above structure,
12453998Sarchie    subclassing the pre-defined types below.  We construct the type in
12553998Sarchie    a 'bottom up' fashion, defining each field's type first, then the
12653998Sarchie    type for the whole structure ('//' comments used to avoid breakage).
12753998Sarchie
12853998Sarchie    // Super-type info for 'label' field
12958011Sarchie    struct ng_parse_fixedstring_info foo_label_info = { 8 };
13053998Sarchie
13153998Sarchie    // Parse type for 'label' field
13253998Sarchie    struct ng_parse_type foo_label_type = {
13353998Sarchie	    &ng_parse_fixedstring_type		// super-type
13453998Sarchie	    &foo_label_info			// super-type info
13553998Sarchie    };
13653998Sarchie
13753998Sarchie    #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
13853998Sarchie
13953998Sarchie    // Function to compute the length of the array 'ary', which
14053998Sarchie    // is variable length, depending on the previous field 'alen'.
14153998Sarchie    // Upon entry 'buf' will be pointing at &ary[0].
14253998Sarchie    int
14353998Sarchie    foo_ary_getLength(const struct ng_parse_type *type,
14453998Sarchie	    const u_char *start, const u_char *buf)
14553998Sarchie    {
14653998Sarchie	    const struct foo *f;
14753998Sarchie
14853998Sarchie	    f = (const struct foo *)(buf - OFFSETOF(struct foo, ary));
14953998Sarchie	    return f->alen;
15053998Sarchie    }
15153998Sarchie
15253998Sarchie    // Super-type info for 'ary' field
15353998Sarchie    struct ng_parse_array_info foo_ary_info = {
15453998Sarchie	    &ng_parse_int16_type,		// element type
15553998Sarchie	    &foo_ary_getLength			// func to get array length
15653998Sarchie    }
15753998Sarchie
15853998Sarchie    // Parse type for 'ary' field
15953998Sarchie    struct ng_parse_type foo_ary_type = {
16053998Sarchie	    &ng_parse_array_type,		// super-type
16153998Sarchie	    &foo_ary_info			// super-type info
16253998Sarchie    };
16353998Sarchie
16453998Sarchie    // Super-type info for struct foo
16553998Sarchie    struct ng_parse_struct_field foo_fields[] = {
16664506Sarchie	    { "ip",	&ng_parse_ipaddr_type	},
16764506Sarchie	    { "bar",	&ng_parse_int32_type	},
16853998Sarchie	    { "label",	&foo_label_type		},
16953998Sarchie	    { "alen",	&ng_parse_uint8_type	},
17064506Sarchie	    { "ary",	&foo_ary_type		},
17153998Sarchie	    { NULL }
17253998Sarchie    };
17364506Sarchie
17453998Sarchie    // Parse type for struct foo
17553998Sarchie    struct ng_parse_type foo_type = {
17653998Sarchie	    &ng_parse_struct_type,		// super-type
17753998Sarchie	    &foo_fields				// super-type info
17853998Sarchie    };
17953998Sarchie
18053998Sarchie  To define a type, you can define it as a sub-type of a predefined
18153998Sarchie  type as shown above, possibly overriding some of the predefined
18253913Sarchie  type's methods, or define an entirely new syntax, with the restriction
18353998Sarchie  that the ASCII representation of your type's value must not contain
18453998Sarchie  any whitespace or any of these characters: { } [ ] = "
18553998Sarchie
18653998Sarchie  See ng_ksocket.c for an example of how to do this for 'struct sockaddr'.
18753913Sarchie  See ng_parse.c to see implementations of the pre-defined types below.
18853998Sarchie
18953998Sarchie*/
19053998Sarchie
19153913Sarchie/************************************************************************
19253913Sarchie			METHODS REQUIRED BY A TYPE
19353913Sarchie ************************************************************************/
19453913Sarchie
19553913Sarchie/*
19653913Sarchie * Three methods are required for a type. These may be given explicitly
19753913Sarchie * or, if NULL, inherited from the super-type.  The 'getDefault' method
19853913Sarchie * is always optional; the others are required if there is no super-type.
19953998Sarchie */
20053998Sarchie
20153913Sarchiestruct ng_parse_type;
20253913Sarchie
20353913Sarchie/*
20453913Sarchie * Convert ASCII to binary according to the supplied type.
20553913Sarchie *
20653913Sarchie * The ASCII characters begin at offset *off in 'string'.  The binary
20753913Sarchie * representation is put into 'buf', which has at least *buflen bytes.
20853913Sarchie * 'start' points to the first byte output by ng_parse() (ie, start <= buf).
20953913Sarchie *
21053913Sarchie * Upon return, *buflen contains the length of the new binary data, and
21153913Sarchie * *off is updated to point just past the end of the parsed range of
21253913Sarchie * characters, or, in the case of an error, to the offending character(s).
21353913Sarchie *
21453913Sarchie * Return values:
21553913Sarchie *	0		Success; *buflen holds the length of the data
21653913Sarchie *			and *off points just past the last char parsed.
21753913Sarchie *	EALREADY	Field specified twice
21853913Sarchie *	ENOENT		Unknown field
21953913Sarchie *	E2BIG		Array or character string overflow
22053913Sarchie *	ERANGE		Output was longer than *buflen bytes
22153913Sarchie *	EINVAL		Parse failure or other invalid content
22253913Sarchie *	ENOMEM		Out of memory
22353913Sarchie *	EOPNOTSUPP	Mandatory array/structure element missing
22453913Sarchie */
22553913Sarchietypedef	int	ng_parse_t(const struct ng_parse_type *type, const char *string,
22653913Sarchie			int *off, const u_char *start,
22753913Sarchie			u_char *buf, int *buflen);
22853913Sarchie
22953913Sarchie/*
23053913Sarchie * Convert binary to ASCII according to the supplied type.
23153913Sarchie *
23253913Sarchie * The results are put into 'buf', which is at least buflen bytes long.
23353913Sarchie * *off points to the current byte in 'data' and should be updated
23453913Sarchie * before return to point just past the last byte unparsed.
23553913Sarchie *
23653913Sarchie * Returns:
23753913Sarchie *	0		Success
23853913Sarchie *	ERANGE		Output was longer than buflen bytes
23953913Sarchie */
24053913Sarchietypedef	int	ng_unparse_t(const struct ng_parse_type *type,
24153913Sarchie			const u_char *data, int *off, char *buf, int buflen);
24253913Sarchie
24353913Sarchie/*
24453913Sarchie * Compute the default value according to the supplied type.
24553913Sarchie *
24653913Sarchie * Store the result in 'buf', which is at least *buflen bytes long.
24753913Sarchie * Upon return *buflen contains the length of the output.
24853913Sarchie *
24953913Sarchie * Returns:
25053913Sarchie *	0		Success
25153913Sarchie *	ERANGE		Output was longer than *buflen bytes
25253913Sarchie *	EOPNOTSUPP	Default value is not specified for this type
25353913Sarchie */
25453913Sarchietypedef	int	ng_getDefault_t(const struct ng_parse_type *type,
25553913Sarchie			const u_char *start, u_char *buf, int *buflen);
25653913Sarchie
25753913Sarchie/*
25853913Sarchie * Return the alignment requirement of this type.  Zero is same as one.
25953913Sarchie */
26053913Sarchietypedef	int	ng_getAlign_t(const struct ng_parse_type *type);
26153913Sarchie
26253913Sarchie/************************************************************************
26353913Sarchie			TYPE DEFINITION
26453913Sarchie ************************************************************************/
26553913Sarchie
26653913Sarchie/*
26753913Sarchie * This structure describes a type, which may be a sub-type of another
26853913Sarchie * type by pointing to it with 'supertype' and possibly omitting methods.
26953913Sarchie * Typically the super-type requires some type-specific info, which is
27053998Sarchie * supplied by the 'info' field.
27153998Sarchie *
27253998Sarchie * The 'private' field is ignored by all of the pre-defined types.
27353998Sarchie * Sub-types may use it as they see fit.
27453998Sarchie *
27553998Sarchie * The 'getDefault' method may always be omitted (even if there is no
27653998Sarchie * super-type), which means the value for any item of this type must
27753998Sarchie * always be explicitly given.
27853998Sarchie */
27953998Sarchiestruct ng_parse_type {
28053913Sarchie	const struct ng_parse_type *supertype;	/* super-type, if any */
28153913Sarchie	const void		*info;		/* type-specific info */
28253913Sarchie	void			*private;	/* client private info */
28353913Sarchie	ng_parse_t		*parse;		/* parse method */
28453913Sarchie	ng_unparse_t		*unparse;	/* unparse method */
28553913Sarchie	ng_getDefault_t		*getDefault;	/* get default value method */
28653913Sarchie	ng_getAlign_t		*getAlign;	/* get alignment */
28753913Sarchie};
28853913Sarchie
28953913Sarchie/************************************************************************
29053913Sarchie			PRE-DEFINED TYPES
29153913Sarchie ************************************************************************/
29253913Sarchie
29353913Sarchie/*
29453913Sarchie * STRUCTURE TYPE
29553913Sarchie *
29653998Sarchie * This type supports arbitrary C structures.  The normal field alignment
29753913Sarchie * rules for the local machine are applied.  Fields are always parsed in
29853998Sarchie * field order, no matter what order they are listed in the ASCII string.
29953998Sarchie *
30053998Sarchie *   Default value:		Determined on a per-field basis
30153998Sarchie *   Additional info:		struct ng_parse_struct_field *
30253913Sarchie */
30353913Sarchieextern const struct ng_parse_type ng_parse_struct_type;
30453913Sarchie
30553913Sarchie/* Each field has a name, type, and optional alignment override. If the
30653913Sarchie   override is non-zero, the alignment is determined from the field type.
30753913Sarchie   Note: add an extra struct ng_parse_struct_field with name == NULL
30853913Sarchie   to indicate the end of the list. */
30953913Sarchiestruct ng_parse_struct_field {
31053913Sarchie	const char			*name;		/* field name */
31153913Sarchie	const struct ng_parse_type	*type;		/* field type */
31253913Sarchie	int				alignment;	/* override alignment */
31353913Sarchie};
31453913Sarchie
31553913Sarchie/*
31653913Sarchie * FIXED LENGTH ARRAY TYPE
31753913Sarchie *
31853913Sarchie * This type supports fixed length arrays, having any element type.
31953913Sarchie *
32053913Sarchie *   Default value:		As returned by getDefault for each index
32153998Sarchie *   Additional info:		struct ng_parse_fixedarray_info *
32253913Sarchie */
32353998Sarchieextern const struct ng_parse_type ng_parse_fixedarray_type;
32453998Sarchie
32553998Sarchie/*
32653913Sarchie * Get the default value for the element at index 'index'.  This method
32753913Sarchie * may be NULL, in which case the default value is computed from the
32853913Sarchie * element type.  Otherwise, it should fill in the default value at *buf
32953913Sarchie * (having size *buflen) and update *buflen to the length of the filled-in
33053998Sarchie * value before return.  If there is not enough routine return ERANGE.
33153998Sarchie */
33253998Sarchietypedef	int	ng_parse_array_getDefault_t(const struct ng_parse_type *type,
33353998Sarchie				int index, const u_char *start,
33453998Sarchie				u_char *buf, int *buflen);
33553998Sarchie
33653998Sarchiestruct ng_parse_fixedarray_info {
33753913Sarchie	const struct ng_parse_type	*elementType;
33853913Sarchie	int				length;
33953913Sarchie	ng_parse_array_getDefault_t	*getDefault;
34053913Sarchie};
34153913Sarchie
34253913Sarchie/*
34353913Sarchie * VARIABLE LENGTH ARRAY TYPE
34453913Sarchie *
34553913Sarchie * Same as fixed length arrays, except that the length is determined
34653913Sarchie * by a function instead of a constant value.
34753913Sarchie *
34853998Sarchie *   Default value:		Same as with fixed length arrays
34953913Sarchie *   Additional info:		struct ng_parse_array_info *
35053998Sarchie */
35153998Sarchieextern const struct ng_parse_type ng_parse_array_type;
35253998Sarchie
35353913Sarchie/*
35453913Sarchie * Return the length of the array.  If the array is a field in a structure,
35553913Sarchie * all prior fields are guaranteed to be filled in already.  Upon entry,
35653913Sarchie * 'start' is equal to the first byte parsed in this run, while 'buf' points
35753913Sarchie * to the first element of the array to be filled in.
35853998Sarchie */
35953998Sarchietypedef int	ng_parse_array_getLength_t(const struct ng_parse_type *type,
36053998Sarchie				const u_char *start, const u_char *buf);
36153998Sarchie
36253998Sarchiestruct ng_parse_array_info {
36353998Sarchie	const struct ng_parse_type	*elementType;
36453998Sarchie	ng_parse_array_getLength_t	*getLength;
36553998Sarchie	ng_parse_array_getDefault_t	*getDefault;
36653998Sarchie};
36753913Sarchie
36853913Sarchie/*
36953913Sarchie * ARBITRARY LENGTH STRING TYPE
37053913Sarchie *
37153913Sarchie * For arbirary length, NUL-terminated strings.
37253913Sarchie *
37353913Sarchie *   Default value:		Empty string
37453998Sarchie *   Additional info:		None required
37553913Sarchie */
37653998Sarchieextern const struct ng_parse_type ng_parse_string_type;
37753998Sarchie
37853913Sarchie/*
37953913Sarchie * BOUNDED LENGTH STRING TYPE
38053913Sarchie *
38153913Sarchie * These are strings that have a fixed-size buffer, and always include
38253913Sarchie * a terminating NUL character.
38353913Sarchie *
38453998Sarchie *   Default value:		Empty string
38553913Sarchie *   Additional info:		struct ng_parse_fixedstring_info *
38653998Sarchie */
38753998Sarchieextern const struct ng_parse_type ng_parse_fixedstring_type;
38853998Sarchie
38953913Sarchiestruct ng_parse_fixedstring_info {
39058011Sarchie	int	bufSize;	/* size of buffer (including NUL) */
39153913Sarchie};
39253913Sarchie
39353913Sarchie/*
39458011Sarchie * EXPLICITLY SIZED STRING TYPE
39553913Sarchie *
39653913Sarchie * These are strings that have a two byte length field preceding them.
39753913Sarchie * Parsed strings are NOT NUL-terminated.
39853913Sarchie *
39953998Sarchie *   Default value:		Empty string
40053913Sarchie *   Additional info:		None
40153913Sarchie */
40253913Sarchieextern const struct ng_parse_type ng_parse_sizedstring_type;
40353913Sarchie
40453913Sarchie/*
40553913Sarchie * COMMONLY USED BOUNDED LENGTH STRING TYPES
40653913Sarchie */
40753913Sarchieextern const struct ng_parse_type ng_parse_nodebuf_type;  /* NG_NODELEN + 1 */
40853998Sarchieextern const struct ng_parse_type ng_parse_hookbuf_type;  /* NG_HOOKLEN + 1 */
40953913Sarchieextern const struct ng_parse_type ng_parse_pathbuf_type;  /* NG_PATHLEN + 1 */
41053913Sarchieextern const struct ng_parse_type ng_parse_typebuf_type;  /* NG_TYPELEN + 1 */
41153913Sarchieextern const struct ng_parse_type ng_parse_cmdbuf_type;   /* NG_CMDSTRLEN + 1 */
41253913Sarchie
41353913Sarchie/*
41453913Sarchie * INTEGER TYPES
41553913Sarchie *
41653913Sarchie *   Default value:		0
41753913Sarchie *   Additional info:		None required
41864506Sarchie */
41964506Sarchieextern const struct ng_parse_type ng_parse_int8_type;
42064506Sarchieextern const struct ng_parse_type ng_parse_int16_type;
42164506Sarchieextern const struct ng_parse_type ng_parse_int32_type;
42264506Sarchieextern const struct ng_parse_type ng_parse_int64_type;
42364506Sarchie
42464506Sarchie/* Same thing but unparse as unsigned quantities */
42564506Sarchieextern const struct ng_parse_type ng_parse_uint8_type;
42664506Sarchieextern const struct ng_parse_type ng_parse_uint16_type;
42764506Sarchieextern const struct ng_parse_type ng_parse_uint32_type;
42864506Sarchieextern const struct ng_parse_type ng_parse_uint64_type;
42964506Sarchie
43053913Sarchie/* Same thing but unparse as hex quantities, e.g., "0xe7" */
43153998Sarchieextern const struct ng_parse_type ng_parse_hint8_type;
43253913Sarchieextern const struct ng_parse_type ng_parse_hint16_type;
43353913Sarchieextern const struct ng_parse_type ng_parse_hint32_type;
43453913Sarchieextern const struct ng_parse_type ng_parse_hint64_type;
43553913Sarchie
43653913Sarchie/*
43753913Sarchie * IP ADDRESS TYPE
43853913Sarchie *
43953998Sarchie *   Default value:		0.0.0.0
44053913Sarchie *   Additional info:		None required
44153998Sarchie */
44253998Sarchieextern const struct ng_parse_type ng_parse_ipaddr_type;
44353998Sarchie
44453998Sarchie/*
44553913Sarchie * VARIABLE LENGTH BYTE ARRAY TYPE
44653913Sarchie *
44753913Sarchie * The bytes are displayed in hex.  The ASCII form may be either an
44853913Sarchie * array of bytes or a string constant, in which case the array is
44953913Sarchie * zero-filled after the string bytes.
45053913Sarchie *
45153998Sarchie *   Default value:		All bytes are zero
45253913Sarchie *   Additional info:		ng_parse_array_getLength_t *
45353998Sarchie */
45453998Sarchieextern const struct ng_parse_type ng_parse_bytearray_type;
45553913Sarchie
45653913Sarchie/*
45753913Sarchie * NETGRAPH CONTROL MESSAGE TYPE
45853913Sarchie *
45953913Sarchie * This is the parse type for a struct ng_mesg.
46053913Sarchie *
46153913Sarchie *   Default value:		All fields zero
46253913Sarchie *   Additional info:		None required
46353913Sarchie */
46453913Sarchieextern const struct ng_parse_type ng_parse_ng_mesg_type;
46553913Sarchie
46653913Sarchie/************************************************************************
46753913Sarchie		CONVERSTION AND PARSING ROUTINES
46853913Sarchie ************************************************************************/
46953913Sarchie
47053913Sarchie/* Tokens for parsing structs and arrays */
47153913Sarchieenum ng_parse_token {
47253913Sarchie	T_LBRACE,		/* '{' */
47353913Sarchie	T_RBRACE,		/* '}' */
47453913Sarchie	T_LBRACKET,		/* '[' */
47553913Sarchie	T_RBRACKET,		/* ']' */
47653913Sarchie	T_EQUALS,		/* '=' */
47753913Sarchie	T_STRING,		/* string in double quotes */
47853913Sarchie	T_ERROR,		/* error parsing string in double quotes */
47953913Sarchie	T_WORD,			/* anything else containing no whitespace */
48053913Sarchie	T_EOF,			/* end of string reached */
48153913Sarchie};
48253913Sarchie
48353913Sarchie/*
48453913Sarchie * See typedef ng_parse_t for definition
48553913Sarchie */
48653913Sarchieextern int	ng_parse(const struct ng_parse_type *type, const char *string,
48753913Sarchie			int *off, u_char *buf, int *buflen);
48853913Sarchie
48953913Sarchie/*
49053913Sarchie * See typedef ng_unparse_t for definition (*off assumed to be zero).
49153913Sarchie */
49253913Sarchieextern int	ng_unparse(const struct ng_parse_type *type,
49353913Sarchie			const u_char *data, char *buf, int buflen);
49453913Sarchie
49553913Sarchie/*
49653913Sarchie * See typedef ng_getDefault_t for definition
49753913Sarchie */
49853913Sarchieextern int	ng_parse_getDefault(const struct ng_parse_type *type,
49953913Sarchie			u_char *buf, int *buflen);
50053913Sarchie
50153913Sarchie/*
50253913Sarchie * Parse a token: '*startp' is the offset to start looking.  Upon
50353913Sarchie * successful return, '*startp' equals the beginning of the token
50453913Sarchie * and '*lenp' the length.  If error, '*startp' points at the
50553913Sarchie * offending character(s).
50653913Sarchie */
50753913Sarchieextern enum	ng_parse_token ng_parse_get_token(const char *s,
50853913Sarchie			int *startp, int *lenp);
50953913Sarchie
51053913Sarchie/*
51153913Sarchie * Like above, but specifically for getting a string token and returning
51253913Sarchie * the string value.  The string token must be enclosed in double quotes
51353913Sarchie * and the normal C backslash escapes are recognized.  The caller must
51453913Sarchie * eventually free() the returned result.  Returns NULL if token is
51553913Sarchie * not a string token, or parse or other error. Otherwise, *lenp contains
51653913Sarchie * the number of characters parsed, and *slenp (if not NULL) contains
51753913Sarchie * the actual number of characters in the parsed string.
51853913Sarchie */
51953913Sarchieextern char	*ng_get_string_token(const char *s, int *startp,
52053913Sarchie			int *lenp, int *slenp);
52153913Sarchie
522/*
523 * Convert a raw string into a doubly-quoted string including any
524 * necessary backslash escapes.  Caller must free the result.
525 * Returns NULL if ENOMEM. Normally "slen" should equal strlen(s)
526 * unless you want to encode NUL bytes.
527 */
528extern char	*ng_encode_string(const char *s, int slen);
529
530#endif /* _NETGRAPH_PARSE_H_ */
531
532