153913Sarchie/*
253913Sarchie * ng_parse.h
3139823Simp */
4139823Simp
5139823Simp/*-
653913Sarchie * Copyright (c) 1999 Whistle Communications, Inc.
753913Sarchie * All rights reserved.
853913Sarchie *
953913Sarchie * Subject to the following obligations and disclaimer of warranty, use and
1053913Sarchie * redistribution of this software, in source or object code forms, with or
1153913Sarchie * without modifications are expressly permitted by Whistle Communications;
1253913Sarchie * provided, however, that:
1353913Sarchie * 1. Any and all reproductions of the source or object code must include the
1453913Sarchie *    copyright notice above and the following disclaimer of warranties; and
1553913Sarchie * 2. No rights are granted, in any manner or form, to use Whistle
1653913Sarchie *    Communications, Inc. trademarks, including the mark "WHISTLE
1753913Sarchie *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1853913Sarchie *    such appears in the above copyright notice or in the software.
1953913Sarchie *
2053913Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2153913Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2253913Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2353913Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2453913Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2553913Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2653913Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2753913Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2853913Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2953913Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
3053913Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3153913Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3253913Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3353913Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3453913Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3553913Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3653913Sarchie * OF SUCH DAMAGE.
3753913Sarchie *
3867506Sjulian * Author: Archie Cobbs <archie@freebsd.org>
3953913Sarchie *
4053913Sarchie * $Whistle: ng_parse.h,v 1.2 1999/11/29 01:43:48 archie Exp $
4153913Sarchie * $FreeBSD$
4253913Sarchie */
4353913Sarchie
44122481Sru#ifndef _NETGRAPH_NG_PARSE_H_
45122481Sru#define _NETGRAPH_NG_PARSE_H_
4653913Sarchie
4753913Sarchie/*
4853913Sarchie
4953913Sarchie  This defines a library of routines for converting between various C
5053913Sarchie  language types in binary form and ASCII strings.  Types are user
5153998Sarchie  definable.  Several pre-defined types are supplied, for some common
5253998Sarchie  C types: structures, variable and fixed length arrays, integer types,
5353998Sarchie  variable and fixed length strings, IP addresses, etc.
5453913Sarchie
5553998Sarchie  A netgraph node type may provide a list of types that correspond to
5653998Sarchie  the structures it expects to send and receive in the arguments field
5753998Sarchie  of a control message.  This allows these messages to be converted
5853998Sarchie  between their native binary form and the corresponding ASCII form.
5953998Sarchie
6053998Sarchie  A future use of the ASCII form may be for inter-machine communication
6153998Sarchie  of control messages, because the ASCII form is machine independent
6253998Sarchie  whereas the native binary form is not.
6353998Sarchie
6453913Sarchie  Syntax
6553913Sarchie  ------
6653913Sarchie
6753913Sarchie    Structures:
6853913Sarchie
6953913Sarchie      '{' [ <name>=<value> ... ] '}'
7053913Sarchie
7153913Sarchie      Omitted fields have their default values by implication.
7253913Sarchie      The order in which the fields are specified does not matter.
7353913Sarchie
7453913Sarchie    Arrays:
7553913Sarchie
7653913Sarchie      '[' [ [index=]<value> ... ] ']'
7753913Sarchie
7853913Sarchie      Element value may be specified with or without the "<index>=" prefix;
7953913Sarchie      If omitted, the index after the previous element is used.
8053913Sarchie      Omitted fields have their default values by implication.
8153913Sarchie
8253913Sarchie    Strings:
8353913Sarchie
8453913Sarchie      "foo bar blah\r\n"
8553913Sarchie
8653913Sarchie      That is, strings are specified just like C strings. The usual
8753913Sarchie      backslash escapes are accepted.
8853913Sarchie
8953998Sarchie    Other simple types (integers, IP addresses) have their obvious forms.
9053913Sarchie
9153913Sarchie  Example
9253913Sarchie  -------
9353913Sarchie
9453998Sarchie    Suppose we have a netgraph command that takes as an argument
9553998Sarchie    a 'struct foo' shown below.  Here is an example of a possible
9653998Sarchie    value for the structure, and the corresponding ASCII encoding
9753998Sarchie    of that value:
9853913Sarchie
9953998Sarchie	Structure			Binary value
10053998Sarchie	---------			------------
10153913Sarchie
10253998Sarchie	struct foo {
10353998Sarchie	    struct in_addr ip;  	01 02 03 04
10453998Sarchie	    int bar;			00 00 00 00
10553998Sarchie	    char label[8];		61 62 63 0a 00 00 00 00
10653998Sarchie	    u_char alen;		03 00
107151800Sru	    short ary[];	  	05 00 00 00 0a 00
10853998Sarchie	};
10953913Sarchie
11053998Sarchie	ASCII value
11153998Sarchie	-----------
11253913Sarchie
11353998Sarchie	{ ip=1.2.3.4 label="abc\n" alen=3 ary=[ 5 2=10 ] }
11453998Sarchie
11553998Sarchie    Note that omitted fields and array elements get their default
11653998Sarchie    values ("bar" and ary[2]), and that the alignment is handled
117140357Srik    automatically (the extra 00 byte after "alen").  Also, since byte
11853998Sarchie    order and alignment are inherently machine dependent, so is this
11953998Sarchie    conversion process.  The above example shows an x86 (little
12053998Sarchie    endian) encoding.  Also the above example is tricky because the
12153998Sarchie    structure is variable length, depending on 'alen', the number of
12253998Sarchie    elements in the array 'ary'.
12353998Sarchie
12453998Sarchie    Here is how one would define a parse type for the above structure,
12553998Sarchie    subclassing the pre-defined types below.  We construct the type in
12653998Sarchie    a 'bottom up' fashion, defining each field's type first, then the
12753998Sarchie    type for the whole structure ('//' comments used to avoid breakage).
12853998Sarchie
12953998Sarchie    // Super-type info for 'label' field
13058011Sarchie    struct ng_parse_fixedstring_info foo_label_info = { 8 };
13153998Sarchie
13253998Sarchie    // Parse type for 'label' field
13353998Sarchie    struct ng_parse_type foo_label_type = {
13453998Sarchie	    &ng_parse_fixedstring_type		// super-type
13553998Sarchie	    &foo_label_info			// super-type info
13653998Sarchie    };
13753998Sarchie
13853998Sarchie    #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
13953998Sarchie
14053998Sarchie    // Function to compute the length of the array 'ary', which
14153998Sarchie    // is variable length, depending on the previous field 'alen'.
14253998Sarchie    // Upon entry 'buf' will be pointing at &ary[0].
14353998Sarchie    int
14453998Sarchie    foo_ary_getLength(const struct ng_parse_type *type,
14553998Sarchie	    const u_char *start, const u_char *buf)
14653998Sarchie    {
14753998Sarchie	    const struct foo *f;
14853998Sarchie
14953998Sarchie	    f = (const struct foo *)(buf - OFFSETOF(struct foo, ary));
15053998Sarchie	    return f->alen;
15153998Sarchie    }
15253998Sarchie
15353998Sarchie    // Super-type info for 'ary' field
15453998Sarchie    struct ng_parse_array_info foo_ary_info = {
15553998Sarchie	    &ng_parse_int16_type,		// element type
15653998Sarchie	    &foo_ary_getLength			// func to get array length
15753998Sarchie    }
15853998Sarchie
15953998Sarchie    // Parse type for 'ary' field
16053998Sarchie    struct ng_parse_type foo_ary_type = {
16153998Sarchie	    &ng_parse_array_type,		// super-type
16253998Sarchie	    &foo_ary_info			// super-type info
16353998Sarchie    };
16453998Sarchie
16553998Sarchie    // Super-type info for struct foo
16697685Sarchie    struct ng_parse_struct_field foo_fields[] = {
16764506Sarchie	    { "ip",	&ng_parse_ipaddr_type	},
16853998Sarchie	    { "bar",	&ng_parse_int32_type	},
16953998Sarchie	    { "label",	&foo_label_type		},
17064506Sarchie	    { "alen",	&ng_parse_uint8_type	},
17153998Sarchie	    { "ary",	&foo_ary_type		},
17253998Sarchie	    { NULL }
17353998Sarchie    };
17453998Sarchie
17553998Sarchie    // Parse type for struct foo
17653998Sarchie    struct ng_parse_type foo_type = {
17753998Sarchie	    &ng_parse_struct_type,		// super-type
17853998Sarchie	    &foo_fields				// super-type info
17953998Sarchie    };
18053998Sarchie
18153913Sarchie  To define a type, you can define it as a sub-type of a predefined
18253998Sarchie  type as shown above, possibly overriding some of the predefined
18353998Sarchie  type's methods, or define an entirely new syntax, with the restriction
18453998Sarchie  that the ASCII representation of your type's value must not contain
18553998Sarchie  any whitespace or any of these characters: { } [ ] = "
18653913Sarchie
18753998Sarchie  See ng_ksocket.c for an example of how to do this for 'struct sockaddr'.
18853998Sarchie  See ng_parse.c to see implementations of the pre-defined types below.
18953998Sarchie
19053913Sarchie*/
19153913Sarchie
19253913Sarchie/************************************************************************
19353913Sarchie			METHODS REQUIRED BY A TYPE
19453913Sarchie ************************************************************************/
19553913Sarchie
19653913Sarchie/*
19753913Sarchie * Three methods are required for a type. These may be given explicitly
19853998Sarchie * or, if NULL, inherited from the super-type.  The 'getDefault' method
19953998Sarchie * is always optional; the others are required if there is no super-type.
20053913Sarchie */
20153913Sarchie
20253913Sarchiestruct ng_parse_type;
20353913Sarchie
20453913Sarchie/*
20553913Sarchie * Convert ASCII to binary according to the supplied type.
20653913Sarchie *
20753913Sarchie * The ASCII characters begin at offset *off in 'string'.  The binary
20853913Sarchie * representation is put into 'buf', which has at least *buflen bytes.
20953913Sarchie * 'start' points to the first byte output by ng_parse() (ie, start <= buf).
21053913Sarchie *
21153913Sarchie * Upon return, *buflen contains the length of the new binary data, and
21253913Sarchie * *off is updated to point just past the end of the parsed range of
21353913Sarchie * characters, or, in the case of an error, to the offending character(s).
21453913Sarchie *
21553913Sarchie * Return values:
21653913Sarchie *	0		Success; *buflen holds the length of the data
21753913Sarchie *			and *off points just past the last char parsed.
21853913Sarchie *	EALREADY	Field specified twice
21953913Sarchie *	ENOENT		Unknown field
22053913Sarchie *	E2BIG		Array or character string overflow
22153913Sarchie *	ERANGE		Output was longer than *buflen bytes
22253913Sarchie *	EINVAL		Parse failure or other invalid content
22353913Sarchie *	ENOMEM		Out of memory
22453913Sarchie *	EOPNOTSUPP	Mandatory array/structure element missing
22553913Sarchie */
22653913Sarchietypedef	int	ng_parse_t(const struct ng_parse_type *type, const char *string,
22753913Sarchie			int *off, const u_char *start,
22853913Sarchie			u_char *buf, int *buflen);
22953913Sarchie
23053913Sarchie/*
23153913Sarchie * Convert binary to ASCII according to the supplied type.
23253913Sarchie *
23353913Sarchie * The results are put into 'buf', which is at least buflen bytes long.
23453913Sarchie * *off points to the current byte in 'data' and should be updated
23553913Sarchie * before return to point just past the last byte unparsed.
23653913Sarchie *
23753913Sarchie * Returns:
23853913Sarchie *	0		Success
23953913Sarchie *	ERANGE		Output was longer than buflen bytes
24053913Sarchie */
24153913Sarchietypedef	int	ng_unparse_t(const struct ng_parse_type *type,
24253913Sarchie			const u_char *data, int *off, char *buf, int buflen);
24353913Sarchie
24453913Sarchie/*
24553913Sarchie * Compute the default value according to the supplied type.
24653913Sarchie *
24753913Sarchie * Store the result in 'buf', which is at least *buflen bytes long.
24853913Sarchie * Upon return *buflen contains the length of the output.
24953913Sarchie *
25053913Sarchie * Returns:
25153913Sarchie *	0		Success
25253913Sarchie *	ERANGE		Output was longer than *buflen bytes
25353913Sarchie *	EOPNOTSUPP	Default value is not specified for this type
25453913Sarchie */
25553913Sarchietypedef	int	ng_getDefault_t(const struct ng_parse_type *type,
25653913Sarchie			const u_char *start, u_char *buf, int *buflen);
25753913Sarchie
25853913Sarchie/*
25953913Sarchie * Return the alignment requirement of this type.  Zero is same as one.
26053913Sarchie */
26153913Sarchietypedef	int	ng_getAlign_t(const struct ng_parse_type *type);
26253913Sarchie
26353913Sarchie/************************************************************************
26453913Sarchie			TYPE DEFINITION
26553913Sarchie ************************************************************************/
26653913Sarchie
26753913Sarchie/*
26853913Sarchie * This structure describes a type, which may be a sub-type of another
26953998Sarchie * type by pointing to it with 'supertype' and possibly omitting methods.
27053998Sarchie * Typically the super-type requires some type-specific info, which is
27153998Sarchie * supplied by the 'info' field.
27253998Sarchie *
27353998Sarchie * The 'private' field is ignored by all of the pre-defined types.
27453998Sarchie * Sub-types may use it as they see fit.
27553998Sarchie *
27653998Sarchie * The 'getDefault' method may always be omitted (even if there is no
27753998Sarchie * super-type), which means the value for any item of this type must
27853998Sarchie * always be explicitly given.
27953913Sarchie */
28053913Sarchiestruct ng_parse_type {
28153913Sarchie	const struct ng_parse_type *supertype;	/* super-type, if any */
28253913Sarchie	const void		*info;		/* type-specific info */
28353913Sarchie	void			*private;	/* client private info */
28453913Sarchie	ng_parse_t		*parse;		/* parse method */
28553913Sarchie	ng_unparse_t		*unparse;	/* unparse method */
28653913Sarchie	ng_getDefault_t		*getDefault;	/* get default value method */
28753913Sarchie	ng_getAlign_t		*getAlign;	/* get alignment */
28853913Sarchie};
28953913Sarchie
29053913Sarchie/************************************************************************
29153913Sarchie			PRE-DEFINED TYPES
29253913Sarchie ************************************************************************/
29353913Sarchie
29453913Sarchie/*
29553998Sarchie * STRUCTURE TYPE
29653913Sarchie *
29753998Sarchie * This type supports arbitrary C structures.  The normal field alignment
29853998Sarchie * rules for the local machine are applied.  Fields are always parsed in
29953998Sarchie * field order, no matter what order they are listed in the ASCII string.
30053998Sarchie *
30153913Sarchie *   Default value:		Determined on a per-field basis
30297685Sarchie *   Additional info:		struct ng_parse_struct_field *
30353913Sarchie */
30453913Sarchieextern const struct ng_parse_type ng_parse_struct_type;
30553913Sarchie
30653913Sarchie/* Each field has a name, type, and optional alignment override. If the
30753913Sarchie   override is non-zero, the alignment is determined from the field type.
30853913Sarchie   Note: add an extra struct ng_parse_struct_field with name == NULL
30953913Sarchie   to indicate the end of the list. */
31097685Sarchiestruct ng_parse_struct_field {
31197685Sarchie	const char			*name;		/* field name */
31297685Sarchie	const struct ng_parse_type	*type;		/* field type */
31397685Sarchie	int				alignment;	/* override alignment */
31453913Sarchie};
31553913Sarchie
31653913Sarchie/*
31753998Sarchie * FIXED LENGTH ARRAY TYPE
31853913Sarchie *
31953998Sarchie * This type supports fixed length arrays, having any element type.
32053998Sarchie *
32153998Sarchie *   Default value:		As returned by getDefault for each index
32253913Sarchie *   Additional info:		struct ng_parse_fixedarray_info *
32353913Sarchie */
32453913Sarchieextern const struct ng_parse_type ng_parse_fixedarray_type;
32553913Sarchie
32653998Sarchie/*
32753998Sarchie * Get the default value for the element at index 'index'.  This method
32853998Sarchie * may be NULL, in which case the default value is computed from the
32953998Sarchie * element type.  Otherwise, it should fill in the default value at *buf
33053998Sarchie * (having size *buflen) and update *buflen to the length of the filled-in
33153998Sarchie * value before return.  If there is not enough routine return ERANGE.
33253998Sarchie */
33353913Sarchietypedef	int	ng_parse_array_getDefault_t(const struct ng_parse_type *type,
33453913Sarchie				int index, const u_char *start,
33553913Sarchie				u_char *buf, int *buflen);
33653913Sarchie
33753913Sarchiestruct ng_parse_fixedarray_info {
33853913Sarchie	const struct ng_parse_type	*elementType;
33953913Sarchie	int				length;
34053913Sarchie	ng_parse_array_getDefault_t	*getDefault;
34153913Sarchie};
34253913Sarchie
34353913Sarchie/*
34453998Sarchie * VARIABLE LENGTH ARRAY TYPE
34553913Sarchie *
34653998Sarchie * Same as fixed length arrays, except that the length is determined
34753998Sarchie * by a function instead of a constant value.
34853998Sarchie *
34953913Sarchie *   Default value:		Same as with fixed length arrays
35053913Sarchie *   Additional info:		struct ng_parse_array_info *
35153913Sarchie */
35253913Sarchieextern const struct ng_parse_type ng_parse_array_type;
35353913Sarchie
35453998Sarchie/*
35553998Sarchie * Return the length of the array.  If the array is a field in a structure,
35653998Sarchie * all prior fields are guaranteed to be filled in already.  Upon entry,
35753998Sarchie * 'start' is equal to the first byte parsed in this run, while 'buf' points
35853998Sarchie * to the first element of the array to be filled in.
35953998Sarchie */
36053998Sarchietypedef int	ng_parse_array_getLength_t(const struct ng_parse_type *type,
36153998Sarchie				const u_char *start, const u_char *buf);
36253998Sarchie
36353913Sarchiestruct ng_parse_array_info {
36453913Sarchie	const struct ng_parse_type	*elementType;
36553913Sarchie	ng_parse_array_getLength_t	*getLength;
36653913Sarchie	ng_parse_array_getDefault_t	*getDefault;
36753913Sarchie};
36853913Sarchie
36953913Sarchie/*
37053998Sarchie * ARBITRARY LENGTH STRING TYPE
37153913Sarchie *
37253998Sarchie * For arbirary length, NUL-terminated strings.
37353998Sarchie *
37453913Sarchie *   Default value:		Empty string
37553913Sarchie *   Additional info:		None required
37653913Sarchie */
37753913Sarchieextern const struct ng_parse_type ng_parse_string_type;
37853913Sarchie
37953913Sarchie/*
38053998Sarchie * BOUNDED LENGTH STRING TYPE
38153913Sarchie *
38253998Sarchie * These are strings that have a fixed-size buffer, and always include
38353998Sarchie * a terminating NUL character.
38453998Sarchie *
38553913Sarchie *   Default value:		Empty string
38658011Sarchie *   Additional info:		struct ng_parse_fixedstring_info *
38753913Sarchie */
38853913Sarchieextern const struct ng_parse_type ng_parse_fixedstring_type;
38953913Sarchie
39058011Sarchiestruct ng_parse_fixedstring_info {
39153913Sarchie	int	bufSize;	/* size of buffer (including NUL) */
39253913Sarchie};
39353913Sarchie
39453913Sarchie/*
39568845Sbrian * EXPLICITLY SIZED STRING TYPE
39668845Sbrian *
39768845Sbrian * These are strings that have a two byte length field preceding them.
39868845Sbrian * Parsed strings are NOT NUL-terminated.
39968845Sbrian *
40068845Sbrian *   Default value:		Empty string
40168845Sbrian *   Additional info:		None
40268845Sbrian */
40368845Sbrianextern const struct ng_parse_type ng_parse_sizedstring_type;
40468845Sbrian
40568845Sbrian/*
40653998Sarchie * COMMONLY USED BOUNDED LENGTH STRING TYPES
40753913Sarchie */
408125028Shartiextern const struct ng_parse_type ng_parse_nodebuf_type;  /* NG_NODESIZ */
409125028Shartiextern const struct ng_parse_type ng_parse_hookbuf_type;  /* NG_HOOKSIZ */
410125028Shartiextern const struct ng_parse_type ng_parse_pathbuf_type;  /* NG_PATHSIZ */
411125028Shartiextern const struct ng_parse_type ng_parse_typebuf_type;  /* NG_TYPESIZ */
412125028Shartiextern const struct ng_parse_type ng_parse_cmdbuf_type;   /* NG_CMDSTRSIZ */
41353913Sarchie
41453913Sarchie/*
41553998Sarchie * INTEGER TYPES
41653913Sarchie *
41753913Sarchie *   Default value:		0
41853913Sarchie *   Additional info:		None required
41953913Sarchie */
42053913Sarchieextern const struct ng_parse_type ng_parse_int8_type;
42153913Sarchieextern const struct ng_parse_type ng_parse_int16_type;
42253913Sarchieextern const struct ng_parse_type ng_parse_int32_type;
42353913Sarchieextern const struct ng_parse_type ng_parse_int64_type;
42453913Sarchie
42564506Sarchie/* Same thing but unparse as unsigned quantities */
42664506Sarchieextern const struct ng_parse_type ng_parse_uint8_type;
42764506Sarchieextern const struct ng_parse_type ng_parse_uint16_type;
42864506Sarchieextern const struct ng_parse_type ng_parse_uint32_type;
42964506Sarchieextern const struct ng_parse_type ng_parse_uint64_type;
43064506Sarchie
43164506Sarchie/* Same thing but unparse as hex quantities, e.g., "0xe7" */
43264506Sarchieextern const struct ng_parse_type ng_parse_hint8_type;
43364506Sarchieextern const struct ng_parse_type ng_parse_hint16_type;
43464506Sarchieextern const struct ng_parse_type ng_parse_hint32_type;
43564506Sarchieextern const struct ng_parse_type ng_parse_hint64_type;
43664506Sarchie
43753913Sarchie/*
43853998Sarchie * IP ADDRESS TYPE
43953913Sarchie *
44053913Sarchie *   Default value:		0.0.0.0
44153913Sarchie *   Additional info:		None required
44253913Sarchie */
44353913Sarchieextern const struct ng_parse_type ng_parse_ipaddr_type;
44453913Sarchie
44553913Sarchie/*
446123600Sru * ETHERNET ADDRESS TYPE
447123600Sru *
448123600Sru *   Default value:		None
449123600Sru *   Additional info:		None required
450123600Sru */
451123600Sruextern const struct ng_parse_type ng_parse_enaddr_type;
452123600Sru
453123600Sru/*
45453998Sarchie * VARIABLE LENGTH BYTE ARRAY TYPE
45553913Sarchie *
45653998Sarchie * The bytes are displayed in hex.  The ASCII form may be either an
45753998Sarchie * array of bytes or a string constant, in which case the array is
45853998Sarchie * zero-filled after the string bytes.
45953998Sarchie *
46053913Sarchie *   Default value:		All bytes are zero
46153913Sarchie *   Additional info:		ng_parse_array_getLength_t *
46253913Sarchie */
46353913Sarchieextern const struct ng_parse_type ng_parse_bytearray_type;
46453913Sarchie
46553913Sarchie/*
46653998Sarchie * NETGRAPH CONTROL MESSAGE TYPE
46753913Sarchie *
46853998Sarchie * This is the parse type for a struct ng_mesg.
46953998Sarchie *
47053913Sarchie *   Default value:		All fields zero
47153913Sarchie *   Additional info:		None required
47253913Sarchie */
47353913Sarchieextern const struct ng_parse_type ng_parse_ng_mesg_type;
47453913Sarchie
47553913Sarchie/************************************************************************
47653913Sarchie		CONVERSTION AND PARSING ROUTINES
47753913Sarchie ************************************************************************/
47853913Sarchie
47953913Sarchie/* Tokens for parsing structs and arrays */
48053913Sarchieenum ng_parse_token {
48153913Sarchie	T_LBRACE,		/* '{' */
48253913Sarchie	T_RBRACE,		/* '}' */
48353913Sarchie	T_LBRACKET,		/* '[' */
48453913Sarchie	T_RBRACKET,		/* ']' */
48553913Sarchie	T_EQUALS,		/* '=' */
48653913Sarchie	T_STRING,		/* string in double quotes */
48753913Sarchie	T_ERROR,		/* error parsing string in double quotes */
48853913Sarchie	T_WORD,			/* anything else containing no whitespace */
48953913Sarchie	T_EOF,			/* end of string reached */
49053913Sarchie};
49153913Sarchie
49253913Sarchie/*
49353913Sarchie * See typedef ng_parse_t for definition
49453913Sarchie */
49553913Sarchieextern int	ng_parse(const struct ng_parse_type *type, const char *string,
49653913Sarchie			int *off, u_char *buf, int *buflen);
49753913Sarchie
49853913Sarchie/*
49953913Sarchie * See typedef ng_unparse_t for definition (*off assumed to be zero).
50053913Sarchie */
50153913Sarchieextern int	ng_unparse(const struct ng_parse_type *type,
50253913Sarchie			const u_char *data, char *buf, int buflen);
50353913Sarchie
50453913Sarchie/*
50553913Sarchie * See typedef ng_getDefault_t for definition
50653913Sarchie */
50753913Sarchieextern int	ng_parse_getDefault(const struct ng_parse_type *type,
50853913Sarchie			u_char *buf, int *buflen);
50953913Sarchie
51053913Sarchie/*
51153913Sarchie * Parse a token: '*startp' is the offset to start looking.  Upon
51253913Sarchie * successful return, '*startp' equals the beginning of the token
51353913Sarchie * and '*lenp' the length.  If error, '*startp' points at the
51453913Sarchie * offending character(s).
51553913Sarchie */
51653913Sarchieextern enum	ng_parse_token ng_parse_get_token(const char *s,
51753913Sarchie			int *startp, int *lenp);
51853913Sarchie
51953913Sarchie/*
52053913Sarchie * Like above, but specifically for getting a string token and returning
52153913Sarchie * the string value.  The string token must be enclosed in double quotes
52253913Sarchie * and the normal C backslash escapes are recognized.  The caller must
52353913Sarchie * eventually free() the returned result.  Returns NULL if token is
52468845Sbrian * not a string token, or parse or other error. Otherwise, *lenp contains
52568845Sbrian * the number of characters parsed, and *slenp (if not NULL) contains
52668845Sbrian * the actual number of characters in the parsed string.
52753913Sarchie */
52868845Sbrianextern char	*ng_get_string_token(const char *s, int *startp,
52968845Sbrian			int *lenp, int *slenp);
53053913Sarchie
53153913Sarchie/*
53253913Sarchie * Convert a raw string into a doubly-quoted string including any
53353913Sarchie * necessary backslash escapes.  Caller must free the result.
53468845Sbrian * Returns NULL if ENOMEM. Normally "slen" should equal strlen(s)
53568845Sbrian * unless you want to encode NUL bytes.
53653913Sarchie */
53768845Sbrianextern char	*ng_encode_string(const char *s, int slen);
53853913Sarchie
539122481Sru#endif /* _NETGRAPH_NG_PARSE_H_ */
54053913Sarchie
541