1f8307e12SArchie Cobbs 2f8307e12SArchie Cobbs /* 3f8307e12SArchie Cobbs * ng_parse.h 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.h,v 1.2 1999/11/29 01:43:48 archie Exp $ 40f8307e12SArchie Cobbs * $FreeBSD$ 41f8307e12SArchie Cobbs */ 42f8307e12SArchie Cobbs 43f8307e12SArchie Cobbs #ifndef _NETGRAPH_PARSE_H_ 44f8307e12SArchie Cobbs #define _NETGRAPH_PARSE_H_ 45f8307e12SArchie Cobbs 46f8307e12SArchie Cobbs /* 47f8307e12SArchie Cobbs 48f8307e12SArchie Cobbs This defines a library of routines for converting between various C 49f8307e12SArchie Cobbs language types in binary form and ASCII strings. Types are user 5063e2ac54SArchie Cobbs definable. Several pre-defined types are supplied, for some common 5163e2ac54SArchie Cobbs C types: structures, variable and fixed length arrays, integer types, 5263e2ac54SArchie Cobbs variable and fixed length strings, IP addresses, etc. 5363e2ac54SArchie Cobbs 5463e2ac54SArchie Cobbs A netgraph node type may provide a list of types that correspond to 5563e2ac54SArchie Cobbs the structures it expects to send and receive in the arguments field 5663e2ac54SArchie Cobbs of a control message. This allows these messages to be converted 5763e2ac54SArchie Cobbs between their native binary form and the corresponding ASCII form. 5863e2ac54SArchie Cobbs 5963e2ac54SArchie Cobbs A future use of the ASCII form may be for inter-machine communication 6063e2ac54SArchie Cobbs of control messages, because the ASCII form is machine independent 6163e2ac54SArchie Cobbs whereas the native binary form is not. 62f8307e12SArchie Cobbs 63f8307e12SArchie Cobbs Syntax 64f8307e12SArchie Cobbs ------ 65f8307e12SArchie Cobbs 66f8307e12SArchie Cobbs Structures: 67f8307e12SArchie Cobbs 68f8307e12SArchie Cobbs '{' [ <name>=<value> ... ] '}' 69f8307e12SArchie Cobbs 70f8307e12SArchie Cobbs Omitted fields have their default values by implication. 71f8307e12SArchie Cobbs The order in which the fields are specified does not matter. 72f8307e12SArchie Cobbs 73f8307e12SArchie Cobbs Arrays: 74f8307e12SArchie Cobbs 75f8307e12SArchie Cobbs '[' [ [index=]<value> ... ] ']' 76f8307e12SArchie Cobbs 77f8307e12SArchie Cobbs Element value may be specified with or without the "<index>=" prefix; 78f8307e12SArchie Cobbs If omitted, the index after the previous element is used. 79f8307e12SArchie Cobbs Omitted fields have their default values by implication. 80f8307e12SArchie Cobbs 81f8307e12SArchie Cobbs Strings: 82f8307e12SArchie Cobbs 83f8307e12SArchie Cobbs "foo bar blah\r\n" 84f8307e12SArchie Cobbs 85f8307e12SArchie Cobbs That is, strings are specified just like C strings. The usual 86f8307e12SArchie Cobbs backslash escapes are accepted. 87f8307e12SArchie Cobbs 8863e2ac54SArchie Cobbs Other simple types (integers, IP addresses) have their obvious forms. 89f8307e12SArchie Cobbs 90f8307e12SArchie Cobbs Example 91f8307e12SArchie Cobbs ------- 92f8307e12SArchie Cobbs 9363e2ac54SArchie Cobbs Suppose we have a netgraph command that takes as an argument 9463e2ac54SArchie Cobbs a 'struct foo' shown below. Here is an example of a possible 9563e2ac54SArchie Cobbs value for the structure, and the corresponding ASCII encoding 9663e2ac54SArchie Cobbs of that value: 9763e2ac54SArchie Cobbs 9863e2ac54SArchie Cobbs Structure Binary value 9963e2ac54SArchie Cobbs --------- ------------ 100f8307e12SArchie Cobbs 101f8307e12SArchie Cobbs struct foo { 10263e2ac54SArchie Cobbs struct in_addr ip; 01 02 03 04 103f8307e12SArchie Cobbs int bar; 00 00 00 00 10463e2ac54SArchie Cobbs char label[8]; 61 62 63 0a 00 00 00 00 10563e2ac54SArchie Cobbs u_char alen; 03 00 10663e2ac54SArchie Cobbs short ary[0]; 05 00 00 00 0a 00 107f8307e12SArchie Cobbs }; 108f8307e12SArchie Cobbs 10963e2ac54SArchie Cobbs ASCII value 11063e2ac54SArchie Cobbs ----------- 111f8307e12SArchie Cobbs 11263e2ac54SArchie Cobbs { ip=1.2.3.4 label="abc\n" alen=3 ary=[ 5 2=10 ] } 11363e2ac54SArchie Cobbs 11463e2ac54SArchie Cobbs Note that omitted fields and array elements get their default 11563e2ac54SArchie Cobbs values ("bar" and ary[2]), and that the alignment is handled 11663e2ac54SArchie Cobbs automatically (the extra 00 byte after "num"). Also, since byte 11763e2ac54SArchie Cobbs order and alignment are inherently machine dependent, so is this 11863e2ac54SArchie Cobbs conversion process. The above example shows an x86 (little 11963e2ac54SArchie Cobbs endian) encoding. Also the above example is tricky because the 12063e2ac54SArchie Cobbs structure is variable length, depending on 'alen', the number of 12163e2ac54SArchie Cobbs elements in the array 'ary'. 12263e2ac54SArchie Cobbs 12363e2ac54SArchie Cobbs Here is how one would define a parse type for the above structure, 12463e2ac54SArchie Cobbs subclassing the pre-defined types below. We construct the type in 12563e2ac54SArchie Cobbs a 'bottom up' fashion, defining each field's type first, then the 12663e2ac54SArchie Cobbs type for the whole structure ('//' comments used to avoid breakage). 12763e2ac54SArchie Cobbs 12863e2ac54SArchie Cobbs // Super-type info for 'label' field 129c1b9e5f2SArchie Cobbs struct ng_parse_fixedstring_info foo_label_info = { 8 }; 13063e2ac54SArchie Cobbs 13163e2ac54SArchie Cobbs // Parse type for 'label' field 13263e2ac54SArchie Cobbs struct ng_parse_type foo_label_type = { 13363e2ac54SArchie Cobbs &ng_parse_fixedstring_type // super-type 13463e2ac54SArchie Cobbs &foo_label_info // super-type info 13563e2ac54SArchie Cobbs }; 13663e2ac54SArchie Cobbs 13763e2ac54SArchie Cobbs #define OFFSETOF(s, e) ((char *)&((s *)0)->e - (char *)((s *)0)) 13863e2ac54SArchie Cobbs 13963e2ac54SArchie Cobbs // Function to compute the length of the array 'ary', which 14063e2ac54SArchie Cobbs // is variable length, depending on the previous field 'alen'. 14163e2ac54SArchie Cobbs // Upon entry 'buf' will be pointing at &ary[0]. 14263e2ac54SArchie Cobbs int 14363e2ac54SArchie Cobbs foo_ary_getLength(const struct ng_parse_type *type, 14463e2ac54SArchie Cobbs const u_char *start, const u_char *buf) 14563e2ac54SArchie Cobbs { 14663e2ac54SArchie Cobbs const struct foo *f; 14763e2ac54SArchie Cobbs 14863e2ac54SArchie Cobbs f = (const struct foo *)(buf - OFFSETOF(struct foo, ary)); 14963e2ac54SArchie Cobbs return f->alen; 15063e2ac54SArchie Cobbs } 15163e2ac54SArchie Cobbs 15263e2ac54SArchie Cobbs // Super-type info for 'ary' field 15363e2ac54SArchie Cobbs struct ng_parse_array_info foo_ary_info = { 15463e2ac54SArchie Cobbs &ng_parse_int16_type, // element type 15563e2ac54SArchie Cobbs &foo_ary_getLength // func to get array length 15663e2ac54SArchie Cobbs } 15763e2ac54SArchie Cobbs 15863e2ac54SArchie Cobbs // Parse type for 'ary' field 15963e2ac54SArchie Cobbs struct ng_parse_type foo_ary_type = { 16063e2ac54SArchie Cobbs &ng_parse_array_type, // super-type 16163e2ac54SArchie Cobbs &foo_ary_info // super-type info 16263e2ac54SArchie Cobbs }; 16363e2ac54SArchie Cobbs 16463e2ac54SArchie Cobbs // Super-type info for struct foo 16563e2ac54SArchie Cobbs struct ng_parse_struct_info foo_fields = { 1667b3bf3f9SArchie Cobbs { 16763e2ac54SArchie Cobbs { "ip", &ng_parse_ipaddr_type }, 16863e2ac54SArchie Cobbs { "bar", &ng_parse_int32_type }, 16963e2ac54SArchie Cobbs { "label", &foo_label_type }, 1707b3bf3f9SArchie Cobbs { "alen", &ng_parse_uint8_type }, 17163e2ac54SArchie Cobbs { "ary", &foo_ary_type }, 17263e2ac54SArchie Cobbs { NULL } 1737b3bf3f9SArchie Cobbs } 17463e2ac54SArchie Cobbs }; 17563e2ac54SArchie Cobbs 17663e2ac54SArchie Cobbs // Parse type for struct foo 17763e2ac54SArchie Cobbs struct ng_parse_type foo_type = { 17863e2ac54SArchie Cobbs &ng_parse_struct_type, // super-type 17963e2ac54SArchie Cobbs &foo_fields // super-type info 18063e2ac54SArchie Cobbs }; 181f8307e12SArchie Cobbs 182f8307e12SArchie Cobbs To define a type, you can define it as a sub-type of a predefined 18363e2ac54SArchie Cobbs type as shown above, possibly overriding some of the predefined 18463e2ac54SArchie Cobbs type's methods, or define an entirely new syntax, with the restriction 18563e2ac54SArchie Cobbs that the ASCII representation of your type's value must not contain 18663e2ac54SArchie Cobbs any whitespace or any of these characters: { } [ ] = " 18763e2ac54SArchie Cobbs 18863e2ac54SArchie Cobbs See ng_ksocket.c for an example of how to do this for 'struct sockaddr'. 18963e2ac54SArchie Cobbs See ng_parse.c to see implementations of the pre-defined types below. 190f8307e12SArchie Cobbs 191f8307e12SArchie Cobbs */ 192f8307e12SArchie Cobbs 193f8307e12SArchie Cobbs /************************************************************************ 194f8307e12SArchie Cobbs METHODS REQUIRED BY A TYPE 195f8307e12SArchie Cobbs ************************************************************************/ 196f8307e12SArchie Cobbs 197f8307e12SArchie Cobbs /* 198f8307e12SArchie Cobbs * Three methods are required for a type. These may be given explicitly 19963e2ac54SArchie Cobbs * or, if NULL, inherited from the super-type. The 'getDefault' method 20063e2ac54SArchie Cobbs * is always optional; the others are required if there is no super-type. 201f8307e12SArchie Cobbs */ 202f8307e12SArchie Cobbs 203f8307e12SArchie Cobbs struct ng_parse_type; 204f8307e12SArchie Cobbs 205f8307e12SArchie Cobbs /* 206f8307e12SArchie Cobbs * Convert ASCII to binary according to the supplied type. 207f8307e12SArchie Cobbs * 208f8307e12SArchie Cobbs * The ASCII characters begin at offset *off in 'string'. The binary 209f8307e12SArchie Cobbs * representation is put into 'buf', which has at least *buflen bytes. 210f8307e12SArchie Cobbs * 'start' points to the first byte output by ng_parse() (ie, start <= buf). 211f8307e12SArchie Cobbs * 212f8307e12SArchie Cobbs * Upon return, *buflen contains the length of the new binary data, and 213f8307e12SArchie Cobbs * *off is updated to point just past the end of the parsed range of 214f8307e12SArchie Cobbs * characters, or, in the case of an error, to the offending character(s). 215f8307e12SArchie Cobbs * 216f8307e12SArchie Cobbs * Return values: 217f8307e12SArchie Cobbs * 0 Success; *buflen holds the length of the data 218f8307e12SArchie Cobbs * and *off points just past the last char parsed. 219f8307e12SArchie Cobbs * EALREADY Field specified twice 220f8307e12SArchie Cobbs * ENOENT Unknown field 221f8307e12SArchie Cobbs * E2BIG Array or character string overflow 222f8307e12SArchie Cobbs * ERANGE Output was longer than *buflen bytes 223f8307e12SArchie Cobbs * EINVAL Parse failure or other invalid content 224f8307e12SArchie Cobbs * ENOMEM Out of memory 225f8307e12SArchie Cobbs * EOPNOTSUPP Mandatory array/structure element missing 226f8307e12SArchie Cobbs */ 227f8307e12SArchie Cobbs typedef int ng_parse_t(const struct ng_parse_type *type, const char *string, 228f8307e12SArchie Cobbs int *off, const u_char *start, 229f8307e12SArchie Cobbs u_char *buf, int *buflen); 230f8307e12SArchie Cobbs 231f8307e12SArchie Cobbs /* 232f8307e12SArchie Cobbs * Convert binary to ASCII according to the supplied type. 233f8307e12SArchie Cobbs * 234f8307e12SArchie Cobbs * The results are put into 'buf', which is at least buflen bytes long. 235f8307e12SArchie Cobbs * *off points to the current byte in 'data' and should be updated 236f8307e12SArchie Cobbs * before return to point just past the last byte unparsed. 237f8307e12SArchie Cobbs * 238f8307e12SArchie Cobbs * Returns: 239f8307e12SArchie Cobbs * 0 Success 240f8307e12SArchie Cobbs * ERANGE Output was longer than buflen bytes 241f8307e12SArchie Cobbs */ 242f8307e12SArchie Cobbs typedef int ng_unparse_t(const struct ng_parse_type *type, 243f8307e12SArchie Cobbs const u_char *data, int *off, char *buf, int buflen); 244f8307e12SArchie Cobbs 245f8307e12SArchie Cobbs /* 246f8307e12SArchie Cobbs * Compute the default value according to the supplied type. 247f8307e12SArchie Cobbs * 248f8307e12SArchie Cobbs * Store the result in 'buf', which is at least *buflen bytes long. 249f8307e12SArchie Cobbs * Upon return *buflen contains the length of the output. 250f8307e12SArchie Cobbs * 251f8307e12SArchie Cobbs * Returns: 252f8307e12SArchie Cobbs * 0 Success 253f8307e12SArchie Cobbs * ERANGE Output was longer than *buflen bytes 254f8307e12SArchie Cobbs * EOPNOTSUPP Default value is not specified for this type 255f8307e12SArchie Cobbs */ 256f8307e12SArchie Cobbs typedef int ng_getDefault_t(const struct ng_parse_type *type, 257f8307e12SArchie Cobbs const u_char *start, u_char *buf, int *buflen); 258f8307e12SArchie Cobbs 259f8307e12SArchie Cobbs /* 260f8307e12SArchie Cobbs * Return the alignment requirement of this type. Zero is same as one. 261f8307e12SArchie Cobbs */ 262f8307e12SArchie Cobbs typedef int ng_getAlign_t(const struct ng_parse_type *type); 263f8307e12SArchie Cobbs 264f8307e12SArchie Cobbs /************************************************************************ 265f8307e12SArchie Cobbs TYPE DEFINITION 266f8307e12SArchie Cobbs ************************************************************************/ 267f8307e12SArchie Cobbs 268f8307e12SArchie Cobbs /* 269f8307e12SArchie Cobbs * This structure describes a type, which may be a sub-type of another 27063e2ac54SArchie Cobbs * type by pointing to it with 'supertype' and possibly omitting methods. 27163e2ac54SArchie Cobbs * Typically the super-type requires some type-specific info, which is 27263e2ac54SArchie Cobbs * supplied by the 'info' field. 27363e2ac54SArchie Cobbs * 27463e2ac54SArchie Cobbs * The 'private' field is ignored by all of the pre-defined types. 27563e2ac54SArchie Cobbs * Sub-types may use it as they see fit. 27663e2ac54SArchie Cobbs * 27763e2ac54SArchie Cobbs * The 'getDefault' method may always be omitted (even if there is no 27863e2ac54SArchie Cobbs * super-type), which means the value for any item of this type must 27963e2ac54SArchie Cobbs * always be explicitly given. 280f8307e12SArchie Cobbs */ 281f8307e12SArchie Cobbs struct ng_parse_type { 282f8307e12SArchie Cobbs const struct ng_parse_type *supertype; /* super-type, if any */ 283f8307e12SArchie Cobbs const void *info; /* type-specific info */ 284f8307e12SArchie Cobbs void *private; /* client private info */ 285f8307e12SArchie Cobbs ng_parse_t *parse; /* parse method */ 286f8307e12SArchie Cobbs ng_unparse_t *unparse; /* unparse method */ 287f8307e12SArchie Cobbs ng_getDefault_t *getDefault; /* get default value method */ 288f8307e12SArchie Cobbs ng_getAlign_t *getAlign; /* get alignment */ 289f8307e12SArchie Cobbs }; 290f8307e12SArchie Cobbs 291f8307e12SArchie Cobbs /************************************************************************ 292f8307e12SArchie Cobbs PRE-DEFINED TYPES 293f8307e12SArchie Cobbs ************************************************************************/ 294f8307e12SArchie Cobbs 295f8307e12SArchie Cobbs /* 29663e2ac54SArchie Cobbs * STRUCTURE TYPE 29763e2ac54SArchie Cobbs * 29863e2ac54SArchie Cobbs * This type supports arbitrary C structures. The normal field alignment 29963e2ac54SArchie Cobbs * rules for the local machine are applied. Fields are always parsed in 30063e2ac54SArchie Cobbs * field order, no matter what order they are listed in the ASCII string. 301f8307e12SArchie Cobbs * 302f8307e12SArchie Cobbs * Default value: Determined on a per-field basis 303f8307e12SArchie Cobbs * Additional info: struct ng_parse_struct_info * 304f8307e12SArchie Cobbs */ 305f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_struct_type; 306f8307e12SArchie Cobbs 307f8307e12SArchie Cobbs /* Each field has a name, type, and optional alignment override. If the 308f8307e12SArchie Cobbs override is non-zero, the alignment is determined from the field type. 309f8307e12SArchie Cobbs Note: add an extra struct ng_parse_struct_field with name == NULL 310f8307e12SArchie Cobbs to indicate the end of the list. */ 311f8307e12SArchie Cobbs struct ng_parse_struct_info { 312f8307e12SArchie Cobbs struct ng_parse_struct_field { 313f8307e12SArchie Cobbs const char *name; /* field name */ 314f8307e12SArchie Cobbs const struct ng_parse_type 315f8307e12SArchie Cobbs *type; /* field type */ 316f8307e12SArchie Cobbs int alignment; /* override alignment */ 317f8307e12SArchie Cobbs } fields[0]; 318f8307e12SArchie Cobbs }; 319f8307e12SArchie Cobbs 320f8307e12SArchie Cobbs /* 32163e2ac54SArchie Cobbs * FIXED LENGTH ARRAY TYPE 322f8307e12SArchie Cobbs * 32363e2ac54SArchie Cobbs * This type supports fixed length arrays, having any element type. 32463e2ac54SArchie Cobbs * 32563e2ac54SArchie Cobbs * Default value: As returned by getDefault for each index 326f8307e12SArchie Cobbs * Additional info: struct ng_parse_fixedarray_info * 327f8307e12SArchie Cobbs */ 328f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_fixedarray_type; 329f8307e12SArchie Cobbs 33063e2ac54SArchie Cobbs /* 33163e2ac54SArchie Cobbs * Get the default value for the element at index 'index'. This method 33263e2ac54SArchie Cobbs * may be NULL, in which case the default value is computed from the 33363e2ac54SArchie Cobbs * element type. Otherwise, it should fill in the default value at *buf 33463e2ac54SArchie Cobbs * (having size *buflen) and update *buflen to the length of the filled-in 33563e2ac54SArchie Cobbs * value before return. If there is not enough routine return ERANGE. 33663e2ac54SArchie Cobbs */ 337f8307e12SArchie Cobbs typedef int ng_parse_array_getDefault_t(const struct ng_parse_type *type, 338f8307e12SArchie Cobbs int index, const u_char *start, 339f8307e12SArchie Cobbs u_char *buf, int *buflen); 340f8307e12SArchie Cobbs 341f8307e12SArchie Cobbs struct ng_parse_fixedarray_info { 342f8307e12SArchie Cobbs const struct ng_parse_type *elementType; 343f8307e12SArchie Cobbs int length; 344f8307e12SArchie Cobbs ng_parse_array_getDefault_t *getDefault; 345f8307e12SArchie Cobbs }; 346f8307e12SArchie Cobbs 347f8307e12SArchie Cobbs /* 34863e2ac54SArchie Cobbs * VARIABLE LENGTH ARRAY TYPE 34963e2ac54SArchie Cobbs * 35063e2ac54SArchie Cobbs * Same as fixed length arrays, except that the length is determined 35163e2ac54SArchie Cobbs * by a function instead of a constant value. 352f8307e12SArchie Cobbs * 353f8307e12SArchie Cobbs * Default value: Same as with fixed length arrays 354f8307e12SArchie Cobbs * Additional info: struct ng_parse_array_info * 355f8307e12SArchie Cobbs */ 356f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_array_type; 357f8307e12SArchie Cobbs 35863e2ac54SArchie Cobbs /* 35963e2ac54SArchie Cobbs * Return the length of the array. If the array is a field in a structure, 36063e2ac54SArchie Cobbs * all prior fields are guaranteed to be filled in already. Upon entry, 36163e2ac54SArchie Cobbs * 'start' is equal to the first byte parsed in this run, while 'buf' points 36263e2ac54SArchie Cobbs * to the first element of the array to be filled in. 36363e2ac54SArchie Cobbs */ 36463e2ac54SArchie Cobbs typedef int ng_parse_array_getLength_t(const struct ng_parse_type *type, 36563e2ac54SArchie Cobbs const u_char *start, const u_char *buf); 36663e2ac54SArchie Cobbs 367f8307e12SArchie Cobbs struct ng_parse_array_info { 368f8307e12SArchie Cobbs const struct ng_parse_type *elementType; 369f8307e12SArchie Cobbs ng_parse_array_getLength_t *getLength; 370f8307e12SArchie Cobbs ng_parse_array_getDefault_t *getDefault; 371f8307e12SArchie Cobbs }; 372f8307e12SArchie Cobbs 373f8307e12SArchie Cobbs /* 37463e2ac54SArchie Cobbs * ARBITRARY LENGTH STRING TYPE 37563e2ac54SArchie Cobbs * 37663e2ac54SArchie Cobbs * For arbirary length, NUL-terminated strings. 377f8307e12SArchie Cobbs * 378f8307e12SArchie Cobbs * Default value: Empty string 379f8307e12SArchie Cobbs * Additional info: None required 380f8307e12SArchie Cobbs */ 381f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_string_type; 382f8307e12SArchie Cobbs 383f8307e12SArchie Cobbs /* 38463e2ac54SArchie Cobbs * BOUNDED LENGTH STRING TYPE 38563e2ac54SArchie Cobbs * 38663e2ac54SArchie Cobbs * These are strings that have a fixed-size buffer, and always include 38763e2ac54SArchie Cobbs * a terminating NUL character. 388f8307e12SArchie Cobbs * 389f8307e12SArchie Cobbs * Default value: Empty string 390c1b9e5f2SArchie Cobbs * Additional info: struct ng_parse_fixedstring_info * 391f8307e12SArchie Cobbs */ 392f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_fixedstring_type; 393f8307e12SArchie Cobbs 394c1b9e5f2SArchie Cobbs struct ng_parse_fixedstring_info { 395f8307e12SArchie Cobbs int bufSize; /* size of buffer (including NUL) */ 396f8307e12SArchie Cobbs }; 397f8307e12SArchie Cobbs 398f8307e12SArchie Cobbs /* 39963e2ac54SArchie Cobbs * COMMONLY USED BOUNDED LENGTH STRING TYPES 400f8307e12SArchie Cobbs */ 401f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_nodebuf_type; /* NG_NODELEN + 1 */ 402f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_hookbuf_type; /* NG_HOOKLEN + 1 */ 403f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_pathbuf_type; /* NG_PATHLEN + 1 */ 404f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_typebuf_type; /* NG_TYPELEN + 1 */ 405f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_cmdbuf_type; /* NG_CMDSTRLEN + 1 */ 406f8307e12SArchie Cobbs 407f8307e12SArchie Cobbs /* 40863e2ac54SArchie Cobbs * INTEGER TYPES 409f8307e12SArchie Cobbs * 410f8307e12SArchie Cobbs * Default value: 0 411f8307e12SArchie Cobbs * Additional info: None required 412f8307e12SArchie Cobbs */ 413f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_int8_type; 414f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_int16_type; 415f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_int32_type; 416f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_int64_type; 417f8307e12SArchie Cobbs 4187b3bf3f9SArchie Cobbs /* Same thing but unparse as unsigned quantities */ 4197b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_uint8_type; 4207b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_uint16_type; 4217b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_uint32_type; 4227b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_uint64_type; 4237b3bf3f9SArchie Cobbs 4247b3bf3f9SArchie Cobbs /* Same thing but unparse as hex quantities, e.g., "0xe7" */ 4257b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_hint8_type; 4267b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_hint16_type; 4277b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_hint32_type; 4287b3bf3f9SArchie Cobbs extern const struct ng_parse_type ng_parse_hint64_type; 4297b3bf3f9SArchie Cobbs 430f8307e12SArchie Cobbs /* 43163e2ac54SArchie Cobbs * IP ADDRESS TYPE 432f8307e12SArchie Cobbs * 433f8307e12SArchie Cobbs * Default value: 0.0.0.0 434f8307e12SArchie Cobbs * Additional info: None required 435f8307e12SArchie Cobbs */ 436f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_ipaddr_type; 437f8307e12SArchie Cobbs 438f8307e12SArchie Cobbs /* 43963e2ac54SArchie Cobbs * VARIABLE LENGTH BYTE ARRAY TYPE 44063e2ac54SArchie Cobbs * 44163e2ac54SArchie Cobbs * The bytes are displayed in hex. The ASCII form may be either an 44263e2ac54SArchie Cobbs * array of bytes or a string constant, in which case the array is 44363e2ac54SArchie Cobbs * zero-filled after the string bytes. 444f8307e12SArchie Cobbs * 445f8307e12SArchie Cobbs * Default value: All bytes are zero 446f8307e12SArchie Cobbs * Additional info: ng_parse_array_getLength_t * 447f8307e12SArchie Cobbs */ 448f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_bytearray_type; 449f8307e12SArchie Cobbs 450f8307e12SArchie Cobbs /* 45163e2ac54SArchie Cobbs * NETGRAPH CONTROL MESSAGE TYPE 45263e2ac54SArchie Cobbs * 45363e2ac54SArchie Cobbs * This is the parse type for a struct ng_mesg. 454f8307e12SArchie Cobbs * 455f8307e12SArchie Cobbs * Default value: All fields zero 456f8307e12SArchie Cobbs * Additional info: None required 457f8307e12SArchie Cobbs */ 458f8307e12SArchie Cobbs extern const struct ng_parse_type ng_parse_ng_mesg_type; 459f8307e12SArchie Cobbs 460f8307e12SArchie Cobbs /************************************************************************ 461f8307e12SArchie Cobbs CONVERSTION AND PARSING ROUTINES 462f8307e12SArchie Cobbs ************************************************************************/ 463f8307e12SArchie Cobbs 464f8307e12SArchie Cobbs /* Tokens for parsing structs and arrays */ 465f8307e12SArchie Cobbs enum ng_parse_token { 466f8307e12SArchie Cobbs T_LBRACE, /* '{' */ 467f8307e12SArchie Cobbs T_RBRACE, /* '}' */ 468f8307e12SArchie Cobbs T_LBRACKET, /* '[' */ 469f8307e12SArchie Cobbs T_RBRACKET, /* ']' */ 470f8307e12SArchie Cobbs T_EQUALS, /* '=' */ 471f8307e12SArchie Cobbs T_STRING, /* string in double quotes */ 472f8307e12SArchie Cobbs T_ERROR, /* error parsing string in double quotes */ 473f8307e12SArchie Cobbs T_WORD, /* anything else containing no whitespace */ 474f8307e12SArchie Cobbs T_EOF, /* end of string reached */ 475f8307e12SArchie Cobbs }; 476f8307e12SArchie Cobbs 477f8307e12SArchie Cobbs /* 478f8307e12SArchie Cobbs * See typedef ng_parse_t for definition 479f8307e12SArchie Cobbs */ 480f8307e12SArchie Cobbs extern int ng_parse(const struct ng_parse_type *type, const char *string, 481f8307e12SArchie Cobbs int *off, u_char *buf, int *buflen); 482f8307e12SArchie Cobbs 483f8307e12SArchie Cobbs /* 484f8307e12SArchie Cobbs * See typedef ng_unparse_t for definition (*off assumed to be zero). 485f8307e12SArchie Cobbs */ 486f8307e12SArchie Cobbs extern int ng_unparse(const struct ng_parse_type *type, 487f8307e12SArchie Cobbs const u_char *data, char *buf, int buflen); 488f8307e12SArchie Cobbs 489f8307e12SArchie Cobbs /* 490f8307e12SArchie Cobbs * See typedef ng_getDefault_t for definition 491f8307e12SArchie Cobbs */ 492f8307e12SArchie Cobbs extern int ng_parse_getDefault(const struct ng_parse_type *type, 493f8307e12SArchie Cobbs u_char *buf, int *buflen); 494f8307e12SArchie Cobbs 495f8307e12SArchie Cobbs /* 496f8307e12SArchie Cobbs * Parse a token: '*startp' is the offset to start looking. Upon 497f8307e12SArchie Cobbs * successful return, '*startp' equals the beginning of the token 498f8307e12SArchie Cobbs * and '*lenp' the length. If error, '*startp' points at the 499f8307e12SArchie Cobbs * offending character(s). 500f8307e12SArchie Cobbs */ 501f8307e12SArchie Cobbs extern enum ng_parse_token ng_parse_get_token(const char *s, 502f8307e12SArchie Cobbs int *startp, int *lenp); 503f8307e12SArchie Cobbs 504f8307e12SArchie Cobbs /* 505f8307e12SArchie Cobbs * Like above, but specifically for getting a string token and returning 506f8307e12SArchie Cobbs * the string value. The string token must be enclosed in double quotes 507f8307e12SArchie Cobbs * and the normal C backslash escapes are recognized. The caller must 508f8307e12SArchie Cobbs * eventually free() the returned result. Returns NULL if token is 509f8307e12SArchie Cobbs * not a string token, or parse or other error. 510f8307e12SArchie Cobbs */ 511f8307e12SArchie Cobbs extern char *ng_get_string_token(const char *s, int *startp, int *lenp); 512f8307e12SArchie Cobbs 513f8307e12SArchie Cobbs /* 514f8307e12SArchie Cobbs * Convert a raw string into a doubly-quoted string including any 515f8307e12SArchie Cobbs * necessary backslash escapes. Caller must free the result. 516f8307e12SArchie Cobbs * Returns NULL if ENOMEM. 517f8307e12SArchie Cobbs */ 518f8307e12SArchie Cobbs extern char *ng_encode_string(const char *s); 519f8307e12SArchie Cobbs 520f8307e12SArchie Cobbs #endif /* _NETGRAPH_PARSE_H_ */ 521f8307e12SArchie Cobbs 522