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 * 37f8307e12SArchie Cobbs * Author: Archie Cobbs <archie@whistle.com> 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/mbuf.h> 49f8307e12SArchie Cobbs #include <sys/socket.h> 50f8307e12SArchie Cobbs #include <sys/ctype.h> 51f8307e12SArchie Cobbs 52f8307e12SArchie Cobbs #include <netinet/in.h> 53f8307e12SArchie Cobbs 54f8307e12SArchie Cobbs #include <netgraph/ng_message.h> 55f8307e12SArchie Cobbs #include <netgraph/netgraph.h> 56f8307e12SArchie Cobbs #include <netgraph/ng_parse.h> 57f8307e12SArchie Cobbs 58f8307e12SArchie Cobbs /* Compute alignment for primitive integral types */ 59f8307e12SArchie Cobbs struct int16_temp { 60f8307e12SArchie Cobbs char x; 61f8307e12SArchie Cobbs int16_t y; 62f8307e12SArchie Cobbs }; 63f8307e12SArchie Cobbs 64f8307e12SArchie Cobbs struct int32_temp { 65f8307e12SArchie Cobbs char x; 66f8307e12SArchie Cobbs int32_t y; 67f8307e12SArchie Cobbs }; 68f8307e12SArchie Cobbs 69f8307e12SArchie Cobbs struct int64_temp { 70f8307e12SArchie Cobbs char x; 71f8307e12SArchie Cobbs int64_t y; 72f8307e12SArchie Cobbs }; 73f8307e12SArchie Cobbs 74f8307e12SArchie Cobbs #define INT8_ALIGNMENT 1 75f8307e12SArchie Cobbs #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y) 76f8307e12SArchie Cobbs #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y) 77f8307e12SArchie Cobbs #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y) 78f8307e12SArchie Cobbs 79f8307e12SArchie Cobbs /* Type of composite object: struct, array, or fixedarray */ 80f8307e12SArchie Cobbs enum comptype { 81f8307e12SArchie Cobbs CT_STRUCT, 82f8307e12SArchie Cobbs CT_ARRAY, 83f8307e12SArchie Cobbs CT_FIXEDARRAY, 84f8307e12SArchie Cobbs }; 85f8307e12SArchie Cobbs 86f8307e12SArchie Cobbs /* Composite types helper functions */ 87f8307e12SArchie Cobbs static int ng_parse_composite(const struct ng_parse_type *type, 88f8307e12SArchie Cobbs const char *s, int *off, const u_char *start, 89f8307e12SArchie Cobbs u_char *const buf, int *buflen, enum comptype ctype); 90f8307e12SArchie Cobbs static int ng_unparse_composite(const struct ng_parse_type *type, 91f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen, 92f8307e12SArchie Cobbs enum comptype ctype); 93f8307e12SArchie Cobbs static int ng_get_composite_elem_default(const struct ng_parse_type *type, 94f8307e12SArchie Cobbs int index, const u_char *start, u_char *buf, 95f8307e12SArchie Cobbs int *buflen, enum comptype ctype); 96f8307e12SArchie Cobbs static int ng_get_composite_len(const struct ng_parse_type *type, 97f8307e12SArchie Cobbs const u_char *start, const u_char *buf, 98f8307e12SArchie Cobbs enum comptype ctype); 99f8307e12SArchie Cobbs static const struct ng_parse_type *ng_get_composite_etype(const struct 100f8307e12SArchie Cobbs ng_parse_type *type, int index, enum comptype ctype); 101f8307e12SArchie Cobbs static int ng_parse_get_elem_pad(const struct ng_parse_type *type, 102f8307e12SArchie Cobbs int index, enum comptype ctype, int posn); 103f8307e12SArchie Cobbs 104f8307e12SArchie Cobbs /* Parsing helper functions */ 105f8307e12SArchie Cobbs static int ng_parse_skip_value(const char *s, int off, int *lenp); 106f8307e12SArchie Cobbs 107f8307e12SArchie Cobbs /* Poor man's virtual method calls */ 108f8307e12SArchie Cobbs #define METHOD(t,m) (ng_get_ ## m ## _method(t)) 109f8307e12SArchie Cobbs #define INVOKE(t,m) (*METHOD(t,m)) 110f8307e12SArchie Cobbs 111f8307e12SArchie Cobbs static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t); 112f8307e12SArchie Cobbs static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t); 113f8307e12SArchie Cobbs static ng_getDefault_t *ng_get_getDefault_method(const 114f8307e12SArchie Cobbs struct ng_parse_type *t); 115f8307e12SArchie Cobbs static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t); 116f8307e12SArchie Cobbs 117f8307e12SArchie Cobbs #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \ 118f8307e12SArchie Cobbs 0 : INVOKE(t, getAlign)(t)) 119f8307e12SArchie Cobbs 120f8307e12SArchie Cobbs /* For converting binary to string */ 121f8307e12SArchie Cobbs #define NG_PARSE_APPEND(fmt, args...) \ 122f8307e12SArchie Cobbs do { \ 123f8307e12SArchie Cobbs int len; \ 124f8307e12SArchie Cobbs \ 125f8307e12SArchie Cobbs len = snprintf((cbuf), (cbuflen), \ 126f8307e12SArchie Cobbs fmt , ## args); \ 127f8307e12SArchie Cobbs if (len >= (cbuflen)) \ 128f8307e12SArchie Cobbs return (ERANGE); \ 129f8307e12SArchie Cobbs (cbuf) += len; \ 130f8307e12SArchie Cobbs (cbuflen) -= len; \ 131f8307e12SArchie Cobbs } while (0) 132f8307e12SArchie Cobbs 133f8307e12SArchie Cobbs /************************************************************************ 134f8307e12SArchie Cobbs PUBLIC FUNCTIONS 135f8307e12SArchie Cobbs ************************************************************************/ 136f8307e12SArchie Cobbs 137f8307e12SArchie Cobbs /* 138f8307e12SArchie Cobbs * Convert an ASCII string to binary according to the supplied type descriptor 139f8307e12SArchie Cobbs */ 140f8307e12SArchie Cobbs int 141f8307e12SArchie Cobbs ng_parse(const struct ng_parse_type *type, 142f8307e12SArchie Cobbs const char *string, int *off, u_char *buf, int *buflen) 143f8307e12SArchie Cobbs { 144f8307e12SArchie Cobbs return INVOKE(type, parse)(type, string, off, buf, buf, buflen); 145f8307e12SArchie Cobbs } 146f8307e12SArchie Cobbs 147f8307e12SArchie Cobbs /* 148f8307e12SArchie Cobbs * Convert binary to an ASCII string according to the supplied type descriptor 149f8307e12SArchie Cobbs */ 150f8307e12SArchie Cobbs int 151f8307e12SArchie Cobbs ng_unparse(const struct ng_parse_type *type, 152f8307e12SArchie Cobbs const u_char *data, char *cbuf, int cbuflen) 153f8307e12SArchie Cobbs { 154f8307e12SArchie Cobbs int off = 0; 155f8307e12SArchie Cobbs 156f8307e12SArchie Cobbs return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen); 157f8307e12SArchie Cobbs } 158f8307e12SArchie Cobbs 159f8307e12SArchie Cobbs /* 160f8307e12SArchie Cobbs * Fill in the default value according to the supplied type descriptor 161f8307e12SArchie Cobbs */ 162f8307e12SArchie Cobbs int 163f8307e12SArchie Cobbs ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen) 164f8307e12SArchie Cobbs { 165f8307e12SArchie Cobbs ng_getDefault_t *const func = METHOD(type, getDefault); 166f8307e12SArchie Cobbs 167f8307e12SArchie Cobbs if (func == NULL) 168f8307e12SArchie Cobbs return (EOPNOTSUPP); 169f8307e12SArchie Cobbs return (*func)(type, buf, buf, buflen); 170f8307e12SArchie Cobbs } 171f8307e12SArchie Cobbs 172f8307e12SArchie Cobbs 173f8307e12SArchie Cobbs /************************************************************************ 174f8307e12SArchie Cobbs STRUCTURE TYPE 175f8307e12SArchie Cobbs ************************************************************************/ 176f8307e12SArchie Cobbs 177f8307e12SArchie Cobbs static int 178f8307e12SArchie Cobbs ng_struct_parse(const struct ng_parse_type *type, 179f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 180f8307e12SArchie Cobbs u_char *const buf, int *buflen) 181f8307e12SArchie Cobbs { 182f8307e12SArchie Cobbs return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT); 183f8307e12SArchie Cobbs } 184f8307e12SArchie Cobbs 185f8307e12SArchie Cobbs static int 186f8307e12SArchie Cobbs ng_struct_unparse(const struct ng_parse_type *type, 187f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 188f8307e12SArchie Cobbs { 189f8307e12SArchie Cobbs return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT); 190f8307e12SArchie Cobbs } 191f8307e12SArchie Cobbs 192f8307e12SArchie Cobbs static int 193f8307e12SArchie Cobbs ng_struct_getDefault(const struct ng_parse_type *type, 194f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 195f8307e12SArchie Cobbs { 196f8307e12SArchie Cobbs int off = 0; 197f8307e12SArchie Cobbs 198f8307e12SArchie Cobbs return ng_parse_composite(type, 199f8307e12SArchie Cobbs "{}", &off, start, buf, buflen, CT_STRUCT); 200f8307e12SArchie Cobbs } 201f8307e12SArchie Cobbs 202f8307e12SArchie Cobbs static int 203f8307e12SArchie Cobbs ng_struct_getAlign(const struct ng_parse_type *type) 204f8307e12SArchie Cobbs { 205f8307e12SArchie Cobbs const struct ng_parse_struct_info *si = type->info; 206f8307e12SArchie Cobbs const struct ng_parse_struct_field *field; 207f8307e12SArchie Cobbs int align = 0; 208f8307e12SArchie Cobbs 209f8307e12SArchie Cobbs for (field = si->fields; field->name != NULL; field++) { 210f8307e12SArchie Cobbs int falign = ALIGNMENT(field->type); 211f8307e12SArchie Cobbs 212f8307e12SArchie Cobbs if (falign > align) 213f8307e12SArchie Cobbs align = falign; 214f8307e12SArchie Cobbs } 215f8307e12SArchie Cobbs return align; 216f8307e12SArchie Cobbs } 217f8307e12SArchie Cobbs 218f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_struct_type = { 219f8307e12SArchie Cobbs NULL, 220f8307e12SArchie Cobbs NULL, 221f8307e12SArchie Cobbs NULL, 222f8307e12SArchie Cobbs ng_struct_parse, 223f8307e12SArchie Cobbs ng_struct_unparse, 224f8307e12SArchie Cobbs ng_struct_getDefault, 225f8307e12SArchie Cobbs ng_struct_getAlign 226f8307e12SArchie Cobbs }; 227f8307e12SArchie Cobbs 228f8307e12SArchie Cobbs /************************************************************************ 229f8307e12SArchie Cobbs FIXED LENGTH ARRAY TYPE 230f8307e12SArchie Cobbs ************************************************************************/ 231f8307e12SArchie Cobbs 232f8307e12SArchie Cobbs static int 233f8307e12SArchie Cobbs ng_fixedarray_parse(const struct ng_parse_type *type, 234f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 235f8307e12SArchie Cobbs u_char *const buf, int *buflen) 236f8307e12SArchie Cobbs { 237f8307e12SArchie Cobbs return ng_parse_composite(type, 238f8307e12SArchie Cobbs s, off, start, buf, buflen, CT_FIXEDARRAY); 239f8307e12SArchie Cobbs } 240f8307e12SArchie Cobbs 241f8307e12SArchie Cobbs static int 242f8307e12SArchie Cobbs ng_fixedarray_unparse(const struct ng_parse_type *type, 243f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 244f8307e12SArchie Cobbs { 245f8307e12SArchie Cobbs return ng_unparse_composite(type, 246f8307e12SArchie Cobbs data, off, cbuf, cbuflen, CT_FIXEDARRAY); 247f8307e12SArchie Cobbs } 248f8307e12SArchie Cobbs 249f8307e12SArchie Cobbs static int 250f8307e12SArchie Cobbs ng_fixedarray_getDefault(const struct ng_parse_type *type, 251f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 252f8307e12SArchie Cobbs { 253f8307e12SArchie Cobbs int off = 0; 254f8307e12SArchie Cobbs 255f8307e12SArchie Cobbs return ng_parse_composite(type, 256f8307e12SArchie Cobbs "[]", &off, start, buf, buflen, CT_FIXEDARRAY); 257f8307e12SArchie Cobbs } 258f8307e12SArchie Cobbs 259f8307e12SArchie Cobbs static int 260f8307e12SArchie Cobbs ng_fixedarray_getAlign(const struct ng_parse_type *type) 261f8307e12SArchie Cobbs { 262f8307e12SArchie Cobbs const struct ng_parse_fixedarray_info *fi = type->info; 263f8307e12SArchie Cobbs 264f8307e12SArchie Cobbs return ALIGNMENT(fi->elementType); 265f8307e12SArchie Cobbs } 266f8307e12SArchie Cobbs 267f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedarray_type = { 268f8307e12SArchie Cobbs NULL, 269f8307e12SArchie Cobbs NULL, 270f8307e12SArchie Cobbs NULL, 271f8307e12SArchie Cobbs ng_fixedarray_parse, 272f8307e12SArchie Cobbs ng_fixedarray_unparse, 273f8307e12SArchie Cobbs ng_fixedarray_getDefault, 274f8307e12SArchie Cobbs ng_fixedarray_getAlign 275f8307e12SArchie Cobbs }; 276f8307e12SArchie Cobbs 277f8307e12SArchie Cobbs /************************************************************************ 278f8307e12SArchie Cobbs VARIABLE LENGTH ARRAY TYPE 279f8307e12SArchie Cobbs ************************************************************************/ 280f8307e12SArchie Cobbs 281f8307e12SArchie Cobbs static int 282f8307e12SArchie Cobbs ng_array_parse(const struct ng_parse_type *type, 283f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 284f8307e12SArchie Cobbs u_char *const buf, int *buflen) 285f8307e12SArchie Cobbs { 286f8307e12SArchie Cobbs return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY); 287f8307e12SArchie Cobbs } 288f8307e12SArchie Cobbs 289f8307e12SArchie Cobbs static int 290f8307e12SArchie Cobbs ng_array_unparse(const struct ng_parse_type *type, 291f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 292f8307e12SArchie Cobbs { 293f8307e12SArchie Cobbs return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY); 294f8307e12SArchie Cobbs } 295f8307e12SArchie Cobbs 296f8307e12SArchie Cobbs static int 297f8307e12SArchie Cobbs ng_array_getDefault(const struct ng_parse_type *type, 298f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 299f8307e12SArchie Cobbs { 300f8307e12SArchie Cobbs int off = 0; 301f8307e12SArchie Cobbs 302f8307e12SArchie Cobbs return ng_parse_composite(type, 303f8307e12SArchie Cobbs "[]", &off, start, buf, buflen, CT_ARRAY); 304f8307e12SArchie Cobbs } 305f8307e12SArchie Cobbs 306f8307e12SArchie Cobbs static int 307f8307e12SArchie Cobbs ng_array_getAlign(const struct ng_parse_type *type) 308f8307e12SArchie Cobbs { 309f8307e12SArchie Cobbs const struct ng_parse_array_info *ai = type->info; 310f8307e12SArchie Cobbs 311f8307e12SArchie Cobbs return ALIGNMENT(ai->elementType); 312f8307e12SArchie Cobbs } 313f8307e12SArchie Cobbs 314f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_array_type = { 315f8307e12SArchie Cobbs NULL, 316f8307e12SArchie Cobbs NULL, 317f8307e12SArchie Cobbs NULL, 318f8307e12SArchie Cobbs ng_array_parse, 319f8307e12SArchie Cobbs ng_array_unparse, 320f8307e12SArchie Cobbs ng_array_getDefault, 321f8307e12SArchie Cobbs ng_array_getAlign 322f8307e12SArchie Cobbs }; 323f8307e12SArchie Cobbs 324f8307e12SArchie Cobbs /************************************************************************ 325f8307e12SArchie Cobbs INT8 TYPE 326f8307e12SArchie Cobbs ************************************************************************/ 327f8307e12SArchie Cobbs 328f8307e12SArchie Cobbs static int 329f8307e12SArchie Cobbs ng_int8_parse(const struct ng_parse_type *type, 330f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 331f8307e12SArchie Cobbs u_char *const buf, int *buflen) 332f8307e12SArchie Cobbs { 333f8307e12SArchie Cobbs long val; 334f8307e12SArchie Cobbs int8_t val8; 335f8307e12SArchie Cobbs char *eptr; 336f8307e12SArchie Cobbs 337f8307e12SArchie Cobbs val = strtol(s + *off, &eptr, 0); 338f8307e12SArchie Cobbs if (val < -0x80 || val > 0xff || eptr == s + *off) 339f8307e12SArchie Cobbs return (EINVAL); 340f8307e12SArchie Cobbs *off = eptr - s; 341f8307e12SArchie Cobbs val8 = (int8_t)val; 342f8307e12SArchie Cobbs bcopy(&val8, buf, sizeof(int8_t)); 343f8307e12SArchie Cobbs *buflen = sizeof(int8_t); 344f8307e12SArchie Cobbs return (0); 345f8307e12SArchie Cobbs } 346f8307e12SArchie Cobbs 347f8307e12SArchie Cobbs static int 348f8307e12SArchie Cobbs ng_int8_unparse(const struct ng_parse_type *type, 349f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 350f8307e12SArchie Cobbs { 351f8307e12SArchie Cobbs int8_t val; 352f8307e12SArchie Cobbs 353f8307e12SArchie Cobbs bcopy(data + *off, &val, sizeof(int8_t)); 354f8307e12SArchie Cobbs NG_PARSE_APPEND("%d", (int)val); 355f8307e12SArchie Cobbs *off += sizeof(int8_t); 356f8307e12SArchie Cobbs return (0); 357f8307e12SArchie Cobbs } 358f8307e12SArchie Cobbs 359f8307e12SArchie Cobbs static int 360f8307e12SArchie Cobbs ng_int8_getDefault(const struct ng_parse_type *type, 361f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 362f8307e12SArchie Cobbs { 363f8307e12SArchie Cobbs int8_t val; 364f8307e12SArchie Cobbs 365f8307e12SArchie Cobbs if (*buflen < sizeof(int8_t)) 366f8307e12SArchie Cobbs return (ERANGE); 367f8307e12SArchie Cobbs val = 0; 368f8307e12SArchie Cobbs bcopy(&val, buf, sizeof(int8_t)); 369f8307e12SArchie Cobbs *buflen = sizeof(int8_t); 370f8307e12SArchie Cobbs return (0); 371f8307e12SArchie Cobbs } 372f8307e12SArchie Cobbs 373f8307e12SArchie Cobbs static int 374f8307e12SArchie Cobbs ng_int8_getAlign(const struct ng_parse_type *type) 375f8307e12SArchie Cobbs { 376f8307e12SArchie Cobbs return INT8_ALIGNMENT; 377f8307e12SArchie Cobbs } 378f8307e12SArchie Cobbs 379f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int8_type = { 380f8307e12SArchie Cobbs NULL, 381f8307e12SArchie Cobbs NULL, 382f8307e12SArchie Cobbs NULL, 383f8307e12SArchie Cobbs ng_int8_parse, 384f8307e12SArchie Cobbs ng_int8_unparse, 385f8307e12SArchie Cobbs ng_int8_getDefault, 386f8307e12SArchie Cobbs ng_int8_getAlign 387f8307e12SArchie Cobbs }; 388f8307e12SArchie Cobbs 389f8307e12SArchie Cobbs /************************************************************************ 390f8307e12SArchie Cobbs INT16 TYPE 391f8307e12SArchie Cobbs ************************************************************************/ 392f8307e12SArchie Cobbs 393f8307e12SArchie Cobbs static int 394f8307e12SArchie Cobbs ng_int16_parse(const struct ng_parse_type *type, 395f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 396f8307e12SArchie Cobbs u_char *const buf, int *buflen) 397f8307e12SArchie Cobbs { 398f8307e12SArchie Cobbs long val; 399f8307e12SArchie Cobbs int16_t val16; 400f8307e12SArchie Cobbs char *eptr; 401f8307e12SArchie Cobbs 402f8307e12SArchie Cobbs val = strtol(s + *off, &eptr, 0); 403f8307e12SArchie Cobbs if (val < -0x8000 || val > 0xffff || eptr == s + *off) 404f8307e12SArchie Cobbs return (EINVAL); 405f8307e12SArchie Cobbs *off = eptr - s; 406f8307e12SArchie Cobbs val16 = (int16_t)val; 407f8307e12SArchie Cobbs bcopy(&val16, buf, sizeof(int16_t)); 408f8307e12SArchie Cobbs *buflen = sizeof(int16_t); 409f8307e12SArchie Cobbs return (0); 410f8307e12SArchie Cobbs } 411f8307e12SArchie Cobbs 412f8307e12SArchie Cobbs static int 413f8307e12SArchie Cobbs ng_int16_unparse(const struct ng_parse_type *type, 414f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 415f8307e12SArchie Cobbs { 416f8307e12SArchie Cobbs int16_t val; 417f8307e12SArchie Cobbs 418f8307e12SArchie Cobbs bcopy(data + *off, &val, sizeof(int16_t)); 419f8307e12SArchie Cobbs NG_PARSE_APPEND("%d", (int)val); 420f8307e12SArchie Cobbs *off += sizeof(int16_t); 421f8307e12SArchie Cobbs return (0); 422f8307e12SArchie Cobbs } 423f8307e12SArchie Cobbs 424f8307e12SArchie Cobbs static int 425f8307e12SArchie Cobbs ng_int16_getDefault(const struct ng_parse_type *type, 426f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 427f8307e12SArchie Cobbs { 428f8307e12SArchie Cobbs int16_t val; 429f8307e12SArchie Cobbs 430f8307e12SArchie Cobbs if (*buflen < sizeof(int16_t)) 431f8307e12SArchie Cobbs return (ERANGE); 432f8307e12SArchie Cobbs val = 0; 433f8307e12SArchie Cobbs bcopy(&val, buf, sizeof(int16_t)); 434f8307e12SArchie Cobbs *buflen = sizeof(int16_t); 435f8307e12SArchie Cobbs return (0); 436f8307e12SArchie Cobbs } 437f8307e12SArchie Cobbs 438f8307e12SArchie Cobbs static int 439f8307e12SArchie Cobbs ng_int16_getAlign(const struct ng_parse_type *type) 440f8307e12SArchie Cobbs { 441f8307e12SArchie Cobbs return INT16_ALIGNMENT; 442f8307e12SArchie Cobbs } 443f8307e12SArchie Cobbs 444f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int16_type = { 445f8307e12SArchie Cobbs NULL, 446f8307e12SArchie Cobbs NULL, 447f8307e12SArchie Cobbs NULL, 448f8307e12SArchie Cobbs ng_int16_parse, 449f8307e12SArchie Cobbs ng_int16_unparse, 450f8307e12SArchie Cobbs ng_int16_getDefault, 451f8307e12SArchie Cobbs ng_int16_getAlign 452f8307e12SArchie Cobbs }; 453f8307e12SArchie Cobbs 454f8307e12SArchie Cobbs /************************************************************************ 455f8307e12SArchie Cobbs INT32 TYPE 456f8307e12SArchie Cobbs ************************************************************************/ 457f8307e12SArchie Cobbs 458f8307e12SArchie Cobbs static int 459f8307e12SArchie Cobbs ng_int32_parse(const struct ng_parse_type *type, 460f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 461f8307e12SArchie Cobbs u_char *const buf, int *buflen) 462f8307e12SArchie Cobbs { 463f8307e12SArchie Cobbs long val; /* assumes long is at least 32 bits */ 464f8307e12SArchie Cobbs int32_t val32; 465f8307e12SArchie Cobbs char *eptr; 466f8307e12SArchie Cobbs 467f8307e12SArchie Cobbs val = strtol(s + *off, &eptr, 0); 468f8307e12SArchie Cobbs if (val < -0x80000000 || val > 0xffffffff || eptr == s + *off) 469f8307e12SArchie Cobbs return (EINVAL); 470f8307e12SArchie Cobbs *off = eptr - s; 471f8307e12SArchie Cobbs val32 = (int32_t)val; 472f8307e12SArchie Cobbs bcopy(&val32, buf, sizeof(int32_t)); 473f8307e12SArchie Cobbs *buflen = sizeof(int32_t); 474f8307e12SArchie Cobbs return (0); 475f8307e12SArchie Cobbs } 476f8307e12SArchie Cobbs 477f8307e12SArchie Cobbs static int 478f8307e12SArchie Cobbs ng_int32_unparse(const struct ng_parse_type *type, 479f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 480f8307e12SArchie Cobbs { 481f8307e12SArchie Cobbs int32_t val; 482f8307e12SArchie Cobbs 483f8307e12SArchie Cobbs bcopy(data + *off, &val, sizeof(int32_t)); 484f8307e12SArchie Cobbs NG_PARSE_APPEND("%ld", (long)val); 485f8307e12SArchie Cobbs *off += sizeof(int32_t); 486f8307e12SArchie Cobbs return (0); 487f8307e12SArchie Cobbs } 488f8307e12SArchie Cobbs 489f8307e12SArchie Cobbs static int 490f8307e12SArchie Cobbs ng_int32_getDefault(const struct ng_parse_type *type, 491f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 492f8307e12SArchie Cobbs { 493f8307e12SArchie Cobbs int32_t val; 494f8307e12SArchie Cobbs 495f8307e12SArchie Cobbs if (*buflen < sizeof(int32_t)) 496f8307e12SArchie Cobbs return (ERANGE); 497f8307e12SArchie Cobbs val = 0; 498f8307e12SArchie Cobbs bcopy(&val, buf, sizeof(int32_t)); 499f8307e12SArchie Cobbs *buflen = sizeof(int32_t); 500f8307e12SArchie Cobbs return (0); 501f8307e12SArchie Cobbs } 502f8307e12SArchie Cobbs 503f8307e12SArchie Cobbs static int 504f8307e12SArchie Cobbs ng_int32_getAlign(const struct ng_parse_type *type) 505f8307e12SArchie Cobbs { 506f8307e12SArchie Cobbs return INT32_ALIGNMENT; 507f8307e12SArchie Cobbs } 508f8307e12SArchie Cobbs 509f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int32_type = { 510f8307e12SArchie Cobbs NULL, 511f8307e12SArchie Cobbs NULL, 512f8307e12SArchie Cobbs NULL, 513f8307e12SArchie Cobbs ng_int32_parse, 514f8307e12SArchie Cobbs ng_int32_unparse, 515f8307e12SArchie Cobbs ng_int32_getDefault, 516f8307e12SArchie Cobbs ng_int32_getAlign 517f8307e12SArchie Cobbs }; 518f8307e12SArchie Cobbs 519f8307e12SArchie Cobbs /************************************************************************ 520f8307e12SArchie Cobbs INT64 TYPE 521f8307e12SArchie Cobbs ************************************************************************/ 522f8307e12SArchie Cobbs 523f8307e12SArchie Cobbs static int 524f8307e12SArchie Cobbs ng_int64_parse(const struct ng_parse_type *type, 525f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 526f8307e12SArchie Cobbs u_char *const buf, int *buflen) 527f8307e12SArchie Cobbs { 528f8307e12SArchie Cobbs quad_t val; 529f8307e12SArchie Cobbs int64_t val64; 530f8307e12SArchie Cobbs char *eptr; 531f8307e12SArchie Cobbs 532f8307e12SArchie Cobbs val = strtoq(s + *off, &eptr, 0); 533f8307e12SArchie Cobbs if (eptr == s + *off) 534f8307e12SArchie Cobbs return (EINVAL); 535f8307e12SArchie Cobbs *off = eptr - s; 536f8307e12SArchie Cobbs val64 = (int64_t)val; 537f8307e12SArchie Cobbs bcopy(&val64, buf, sizeof(int64_t)); 538f8307e12SArchie Cobbs *buflen = sizeof(int64_t); 539f8307e12SArchie Cobbs return (0); 540f8307e12SArchie Cobbs } 541f8307e12SArchie Cobbs 542f8307e12SArchie Cobbs static int 543f8307e12SArchie Cobbs ng_int64_unparse(const struct ng_parse_type *type, 544f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 545f8307e12SArchie Cobbs { 546f8307e12SArchie Cobbs int64_t val; 547f8307e12SArchie Cobbs 548f8307e12SArchie Cobbs bcopy(data + *off, &val, sizeof(int64_t)); 549f8307e12SArchie Cobbs NG_PARSE_APPEND("%lld", (long long)val); 550f8307e12SArchie Cobbs *off += sizeof(int64_t); 551f8307e12SArchie Cobbs return (0); 552f8307e12SArchie Cobbs } 553f8307e12SArchie Cobbs 554f8307e12SArchie Cobbs static int 555f8307e12SArchie Cobbs ng_int64_getDefault(const struct ng_parse_type *type, 556f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 557f8307e12SArchie Cobbs { 558f8307e12SArchie Cobbs int64_t val; 559f8307e12SArchie Cobbs 560f8307e12SArchie Cobbs if (*buflen < sizeof(int64_t)) 561f8307e12SArchie Cobbs return (ERANGE); 562f8307e12SArchie Cobbs val = 0; 563f8307e12SArchie Cobbs bcopy(&val, buf, sizeof(int64_t)); 564f8307e12SArchie Cobbs *buflen = sizeof(int64_t); 565f8307e12SArchie Cobbs return (0); 566f8307e12SArchie Cobbs } 567f8307e12SArchie Cobbs 568f8307e12SArchie Cobbs static int 569f8307e12SArchie Cobbs ng_int64_getAlign(const struct ng_parse_type *type) 570f8307e12SArchie Cobbs { 571f8307e12SArchie Cobbs return INT64_ALIGNMENT; 572f8307e12SArchie Cobbs } 573f8307e12SArchie Cobbs 574f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int64_type = { 575f8307e12SArchie Cobbs NULL, 576f8307e12SArchie Cobbs NULL, 577f8307e12SArchie Cobbs NULL, 578f8307e12SArchie Cobbs ng_int64_parse, 579f8307e12SArchie Cobbs ng_int64_unparse, 580f8307e12SArchie Cobbs ng_int64_getDefault, 581f8307e12SArchie Cobbs ng_int64_getAlign 582f8307e12SArchie Cobbs }; 583f8307e12SArchie Cobbs 584f8307e12SArchie Cobbs /************************************************************************ 585f8307e12SArchie Cobbs STRING TYPE 586f8307e12SArchie Cobbs ************************************************************************/ 587f8307e12SArchie Cobbs 588f8307e12SArchie Cobbs static int 589f8307e12SArchie Cobbs ng_string_parse(const struct ng_parse_type *type, 590f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 591f8307e12SArchie Cobbs u_char *const buf, int *buflen) 592f8307e12SArchie Cobbs { 593f8307e12SArchie Cobbs char *sval; 594f8307e12SArchie Cobbs int len; 595f8307e12SArchie Cobbs 596f8307e12SArchie Cobbs if ((sval = ng_get_string_token(s, off, &len)) == NULL) 597f8307e12SArchie Cobbs return (EINVAL); 598f8307e12SArchie Cobbs *off += len; 599f8307e12SArchie Cobbs len = strlen(sval) + 1; 600f8307e12SArchie Cobbs bcopy(sval, buf, len); 601f8307e12SArchie Cobbs FREE(sval, M_NETGRAPH); 602f8307e12SArchie Cobbs *buflen = len; 603f8307e12SArchie Cobbs return (0); 604f8307e12SArchie Cobbs } 605f8307e12SArchie Cobbs 606f8307e12SArchie Cobbs static int 607f8307e12SArchie Cobbs ng_string_unparse(const struct ng_parse_type *type, 608f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 609f8307e12SArchie Cobbs { 610f8307e12SArchie Cobbs const char *const raw = (const char *)data + *off; 611f8307e12SArchie Cobbs char *const s = ng_encode_string(raw); 612f8307e12SArchie Cobbs 613f8307e12SArchie Cobbs if (s == NULL) 614f8307e12SArchie Cobbs return (ENOMEM); 615f8307e12SArchie Cobbs NG_PARSE_APPEND("%s", s); 616f8307e12SArchie Cobbs *off += strlen(raw) + 1; 617f8307e12SArchie Cobbs FREE(s, M_NETGRAPH); 618f8307e12SArchie Cobbs return (0); 619f8307e12SArchie Cobbs } 620f8307e12SArchie Cobbs 621f8307e12SArchie Cobbs static int 622f8307e12SArchie Cobbs ng_string_getDefault(const struct ng_parse_type *type, 623f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 624f8307e12SArchie Cobbs { 625f8307e12SArchie Cobbs 626f8307e12SArchie Cobbs if (*buflen < 1) 627f8307e12SArchie Cobbs return (ERANGE); 628f8307e12SArchie Cobbs buf[0] = (u_char)'\0'; 629f8307e12SArchie Cobbs *buflen = 1; 630f8307e12SArchie Cobbs return (0); 631f8307e12SArchie Cobbs } 632f8307e12SArchie Cobbs 633f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_string_type = { 634f8307e12SArchie Cobbs NULL, 635f8307e12SArchie Cobbs NULL, 636f8307e12SArchie Cobbs NULL, 637f8307e12SArchie Cobbs ng_string_parse, 638f8307e12SArchie Cobbs ng_string_unparse, 639f8307e12SArchie Cobbs ng_string_getDefault, 640f8307e12SArchie Cobbs NULL 641f8307e12SArchie Cobbs }; 642f8307e12SArchie Cobbs 643f8307e12SArchie Cobbs /************************************************************************ 644f8307e12SArchie Cobbs FIXED BUFFER STRING TYPE 645f8307e12SArchie Cobbs ************************************************************************/ 646f8307e12SArchie Cobbs 647f8307e12SArchie Cobbs static int 648f8307e12SArchie Cobbs ng_fixedstring_parse(const struct ng_parse_type *type, 649f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 650f8307e12SArchie Cobbs u_char *const buf, int *buflen) 651f8307e12SArchie Cobbs { 652f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info *const fi = type->info; 653f8307e12SArchie Cobbs char *sval; 654f8307e12SArchie Cobbs int len; 655f8307e12SArchie Cobbs 656f8307e12SArchie Cobbs if ((sval = ng_get_string_token(s, off, &len)) == NULL) 657f8307e12SArchie Cobbs return (EINVAL); 658f8307e12SArchie Cobbs if (strlen(sval) + 1 > fi->bufSize) 659f8307e12SArchie Cobbs return (E2BIG); 660f8307e12SArchie Cobbs *off += len; 661f8307e12SArchie Cobbs len = strlen(sval) + 1; 662f8307e12SArchie Cobbs bcopy(sval, buf, len); 663f8307e12SArchie Cobbs FREE(sval, M_NETGRAPH); 664f8307e12SArchie Cobbs bzero(buf + len, fi->bufSize - len); 665f8307e12SArchie Cobbs *buflen = fi->bufSize; 666f8307e12SArchie Cobbs return (0); 667f8307e12SArchie Cobbs } 668f8307e12SArchie Cobbs 669f8307e12SArchie Cobbs static int 670f8307e12SArchie Cobbs ng_fixedstring_unparse(const struct ng_parse_type *type, 671f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 672f8307e12SArchie Cobbs { 673f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info *const fi = type->info; 674f8307e12SArchie Cobbs int error, temp = *off; 675f8307e12SArchie Cobbs 676f8307e12SArchie Cobbs if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0) 677f8307e12SArchie Cobbs return (error); 678f8307e12SArchie Cobbs *off += fi->bufSize; 679f8307e12SArchie Cobbs return (0); 680f8307e12SArchie Cobbs } 681f8307e12SArchie Cobbs 682f8307e12SArchie Cobbs static int 683f8307e12SArchie Cobbs ng_fixedstring_getDefault(const struct ng_parse_type *type, 684f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 685f8307e12SArchie Cobbs { 686f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info *const fi = type->info; 687f8307e12SArchie Cobbs 688f8307e12SArchie Cobbs if (*buflen < fi->bufSize) 689f8307e12SArchie Cobbs return (ERANGE); 690f8307e12SArchie Cobbs bzero(buf, fi->bufSize); 691f8307e12SArchie Cobbs *buflen = fi->bufSize; 692f8307e12SArchie Cobbs return (0); 693f8307e12SArchie Cobbs } 694f8307e12SArchie Cobbs 695f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedstring_type = { 696f8307e12SArchie Cobbs NULL, 697f8307e12SArchie Cobbs NULL, 698f8307e12SArchie Cobbs NULL, 699f8307e12SArchie Cobbs ng_fixedstring_parse, 700f8307e12SArchie Cobbs ng_fixedstring_unparse, 701f8307e12SArchie Cobbs ng_fixedstring_getDefault, 702f8307e12SArchie Cobbs NULL 703f8307e12SArchie Cobbs }; 704f8307e12SArchie Cobbs 705f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info ng_parse_nodebuf_info = { 706f8307e12SArchie Cobbs NG_NODELEN + 1 707f8307e12SArchie Cobbs }; 708f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_nodebuf_type = { 709f8307e12SArchie Cobbs &ng_parse_fixedstring_type, 710f8307e12SArchie Cobbs &ng_parse_nodebuf_info 711f8307e12SArchie Cobbs }; 712f8307e12SArchie Cobbs 713f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info ng_parse_hookbuf_info = { 714f8307e12SArchie Cobbs NG_HOOKLEN + 1 715f8307e12SArchie Cobbs }; 716f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_hookbuf_type = { 717f8307e12SArchie Cobbs &ng_parse_fixedstring_type, 718f8307e12SArchie Cobbs &ng_parse_hookbuf_info 719f8307e12SArchie Cobbs }; 720f8307e12SArchie Cobbs 721f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info ng_parse_pathbuf_info = { 722f8307e12SArchie Cobbs NG_PATHLEN + 1 723f8307e12SArchie Cobbs }; 724f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_pathbuf_type = { 725f8307e12SArchie Cobbs &ng_parse_fixedstring_type, 726f8307e12SArchie Cobbs &ng_parse_pathbuf_info 727f8307e12SArchie Cobbs }; 728f8307e12SArchie Cobbs 729f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info ng_parse_typebuf_info = { 730f8307e12SArchie Cobbs NG_TYPELEN + 1 731f8307e12SArchie Cobbs }; 732f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_typebuf_type = { 733f8307e12SArchie Cobbs &ng_parse_fixedstring_type, 734f8307e12SArchie Cobbs &ng_parse_typebuf_info 735f8307e12SArchie Cobbs }; 736f8307e12SArchie Cobbs 737f8307e12SArchie Cobbs const struct ng_parse_fixedsstring_info ng_parse_cmdbuf_info = { 738f8307e12SArchie Cobbs NG_CMDSTRLEN + 1 739f8307e12SArchie Cobbs }; 740f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_cmdbuf_type = { 741f8307e12SArchie Cobbs &ng_parse_fixedstring_type, 742f8307e12SArchie Cobbs &ng_parse_cmdbuf_info 743f8307e12SArchie Cobbs }; 744f8307e12SArchie Cobbs 745f8307e12SArchie Cobbs /************************************************************************ 746f8307e12SArchie Cobbs IP ADDRESS TYPE 747f8307e12SArchie Cobbs ************************************************************************/ 748f8307e12SArchie Cobbs 749f8307e12SArchie Cobbs static int 750f8307e12SArchie Cobbs ng_ipaddr_parse(const struct ng_parse_type *type, 751f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 752f8307e12SArchie Cobbs u_char *const buf, int *buflen) 753f8307e12SArchie Cobbs { 754f8307e12SArchie Cobbs int i, error; 755f8307e12SArchie Cobbs 756f8307e12SArchie Cobbs for (i = 0; i < 4; i++) { 757f8307e12SArchie Cobbs if ((error = ng_int8_parse(&ng_parse_int8_type, 758f8307e12SArchie Cobbs s, off, start, buf + i, buflen)) != 0) 759f8307e12SArchie Cobbs return (error); 760f8307e12SArchie Cobbs if (i < 3 && s[*off] != '.') 761f8307e12SArchie Cobbs return (EINVAL); 762f8307e12SArchie Cobbs (*off)++; 763f8307e12SArchie Cobbs } 764f8307e12SArchie Cobbs *buflen = 4; 765f8307e12SArchie Cobbs return (0); 766f8307e12SArchie Cobbs } 767f8307e12SArchie Cobbs 768f8307e12SArchie Cobbs static int 769f8307e12SArchie Cobbs ng_ipaddr_unparse(const struct ng_parse_type *type, 770f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 771f8307e12SArchie Cobbs { 772f8307e12SArchie Cobbs struct in_addr ip; 773f8307e12SArchie Cobbs 774f8307e12SArchie Cobbs bcopy(data + *off, &ip, sizeof(ip)); 775f8307e12SArchie Cobbs NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0], 776f8307e12SArchie Cobbs ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]); 777f8307e12SArchie Cobbs *off += sizeof(ip); 778f8307e12SArchie Cobbs return (0); 779f8307e12SArchie Cobbs } 780f8307e12SArchie Cobbs 781f8307e12SArchie Cobbs static int 782f8307e12SArchie Cobbs ng_ipaddr_getDefault(const struct ng_parse_type *type, 783f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 784f8307e12SArchie Cobbs { 785f8307e12SArchie Cobbs struct in_addr ip = { 0 }; 786f8307e12SArchie Cobbs 787f8307e12SArchie Cobbs if (*buflen < sizeof(ip)) 788f8307e12SArchie Cobbs return (ERANGE); 789f8307e12SArchie Cobbs bcopy(&ip, buf, sizeof(ip)); 790f8307e12SArchie Cobbs *buflen = sizeof(ip); 791f8307e12SArchie Cobbs return (0); 792f8307e12SArchie Cobbs } 793f8307e12SArchie Cobbs 794f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ipaddr_type = { 795f8307e12SArchie Cobbs NULL, 796f8307e12SArchie Cobbs NULL, 797f8307e12SArchie Cobbs NULL, 798f8307e12SArchie Cobbs ng_ipaddr_parse, 799f8307e12SArchie Cobbs ng_ipaddr_unparse, 800f8307e12SArchie Cobbs ng_ipaddr_getDefault, 801f8307e12SArchie Cobbs ng_int32_getAlign 802f8307e12SArchie Cobbs }; 803f8307e12SArchie Cobbs 804f8307e12SArchie Cobbs /************************************************************************ 805f8307e12SArchie Cobbs BYTE ARRAY TYPE 806f8307e12SArchie Cobbs ************************************************************************/ 807f8307e12SArchie Cobbs 808f8307e12SArchie Cobbs /* Get the length of a byte array */ 809f8307e12SArchie Cobbs static int 810f8307e12SArchie Cobbs ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type, 811f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 812f8307e12SArchie Cobbs { 813f8307e12SArchie Cobbs ng_parse_array_getLength_t *const getLength = type->private; 814f8307e12SArchie Cobbs 815f8307e12SArchie Cobbs return (*getLength)(type, start, buf); 816f8307e12SArchie Cobbs } 817f8307e12SArchie Cobbs 818f8307e12SArchie Cobbs static int 819f8307e12SArchie Cobbs ng_bytearray_elem_unparse(const struct ng_parse_type *type, 820f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 821f8307e12SArchie Cobbs { 822f8307e12SArchie Cobbs int8_t val; 823f8307e12SArchie Cobbs 824f8307e12SArchie Cobbs bcopy(data + *off, &val, sizeof(int8_t)); 825f8307e12SArchie Cobbs NG_PARSE_APPEND("0x%02x", (int)val & 0xff); /* always hex format */ 826f8307e12SArchie Cobbs *off += sizeof(int8_t); 827f8307e12SArchie Cobbs return (0); 828f8307e12SArchie Cobbs } 829f8307e12SArchie Cobbs 830f8307e12SArchie Cobbs /* Byte array element type is int8, but always output in hex format */ 831f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_bytearray_elem_type = { 832f8307e12SArchie Cobbs &ng_parse_int8_type, 833f8307e12SArchie Cobbs NULL, 834f8307e12SArchie Cobbs NULL, 835f8307e12SArchie Cobbs NULL, 836f8307e12SArchie Cobbs ng_bytearray_elem_unparse, 837f8307e12SArchie Cobbs NULL, 838f8307e12SArchie Cobbs NULL 839f8307e12SArchie Cobbs }; 840f8307e12SArchie Cobbs 841f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = { 842f8307e12SArchie Cobbs &ng_parse_bytearray_elem_type, 843f8307e12SArchie Cobbs &ng_parse_bytearray_subtype_getLength, 844f8307e12SArchie Cobbs NULL 845f8307e12SArchie Cobbs }; 846f8307e12SArchie Cobbs static const struct ng_parse_type ng_parse_bytearray_subtype = { 847f8307e12SArchie Cobbs &ng_parse_array_type, 848f8307e12SArchie Cobbs &ng_parse_bytearray_subtype_info 849f8307e12SArchie Cobbs }; 850f8307e12SArchie Cobbs 851f8307e12SArchie Cobbs static int 852f8307e12SArchie Cobbs ng_bytearray_parse(const struct ng_parse_type *type, 853f8307e12SArchie Cobbs const char *s, int *off, const u_char *const start, 854f8307e12SArchie Cobbs u_char *const buf, int *buflen) 855f8307e12SArchie Cobbs { 856f8307e12SArchie Cobbs char *str; 857f8307e12SArchie Cobbs int toklen; 858f8307e12SArchie Cobbs 859f8307e12SArchie Cobbs /* We accept either an array of bytes or a string constant */ 860f8307e12SArchie Cobbs if ((str = ng_get_string_token(s, off, &toklen)) != NULL) { 861f8307e12SArchie Cobbs ng_parse_array_getLength_t *const getLength = type->info; 862f8307e12SArchie Cobbs int arraylen, slen; 863f8307e12SArchie Cobbs 864f8307e12SArchie Cobbs arraylen = (*getLength)(type, start, buf); 865f8307e12SArchie Cobbs if (arraylen > *buflen) { 866f8307e12SArchie Cobbs FREE(str, M_NETGRAPH); 867f8307e12SArchie Cobbs return (ERANGE); 868f8307e12SArchie Cobbs } 869f8307e12SArchie Cobbs slen = strlen(str) + 1; 870f8307e12SArchie Cobbs if (slen > arraylen) { 871f8307e12SArchie Cobbs FREE(str, M_NETGRAPH); 872f8307e12SArchie Cobbs return (E2BIG); 873f8307e12SArchie Cobbs } 874f8307e12SArchie Cobbs bcopy(str, buf, slen); 875f8307e12SArchie Cobbs bzero(buf + slen, arraylen - slen); 876f8307e12SArchie Cobbs FREE(str, M_NETGRAPH); 877f8307e12SArchie Cobbs *off += toklen; 878f8307e12SArchie Cobbs *buflen = arraylen; 879f8307e12SArchie Cobbs return (0); 880f8307e12SArchie Cobbs } else { 881f8307e12SArchie Cobbs struct ng_parse_type subtype; 882f8307e12SArchie Cobbs 883f8307e12SArchie Cobbs subtype = ng_parse_bytearray_subtype; 884f8307e12SArchie Cobbs (const void *)subtype.private = type->info; 885f8307e12SArchie Cobbs return ng_array_parse(&subtype, s, off, start, buf, buflen); 886f8307e12SArchie Cobbs } 887f8307e12SArchie Cobbs } 888f8307e12SArchie Cobbs 889f8307e12SArchie Cobbs static int 890f8307e12SArchie Cobbs ng_bytearray_unparse(const struct ng_parse_type *type, 891f8307e12SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 892f8307e12SArchie Cobbs { 893f8307e12SArchie Cobbs struct ng_parse_type subtype; 894f8307e12SArchie Cobbs 895f8307e12SArchie Cobbs subtype = ng_parse_bytearray_subtype; 896f8307e12SArchie Cobbs (const void *)subtype.private = type->info; 897f8307e12SArchie Cobbs return ng_array_unparse(&subtype, data, off, cbuf, cbuflen); 898f8307e12SArchie Cobbs } 899f8307e12SArchie Cobbs 900f8307e12SArchie Cobbs static int 901f8307e12SArchie Cobbs ng_bytearray_getDefault(const struct ng_parse_type *type, 902f8307e12SArchie Cobbs const u_char *const start, u_char *buf, int *buflen) 903f8307e12SArchie Cobbs { 904f8307e12SArchie Cobbs struct ng_parse_type subtype; 905f8307e12SArchie Cobbs 906f8307e12SArchie Cobbs subtype = ng_parse_bytearray_subtype; 907f8307e12SArchie Cobbs (const void *)subtype.private = type->info; 908f8307e12SArchie Cobbs return ng_array_getDefault(&subtype, start, buf, buflen); 909f8307e12SArchie Cobbs } 910f8307e12SArchie Cobbs 911f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_bytearray_type = { 912f8307e12SArchie Cobbs NULL, 913f8307e12SArchie Cobbs NULL, 914f8307e12SArchie Cobbs NULL, 915f8307e12SArchie Cobbs ng_bytearray_parse, 916f8307e12SArchie Cobbs ng_bytearray_unparse, 917f8307e12SArchie Cobbs ng_bytearray_getDefault, 918f8307e12SArchie Cobbs NULL 919f8307e12SArchie Cobbs }; 920f8307e12SArchie Cobbs 921f8307e12SArchie Cobbs /************************************************************************ 922f8307e12SArchie Cobbs STRUCT NG_MESG TYPE 923f8307e12SArchie Cobbs ************************************************************************/ 924f8307e12SArchie Cobbs 925f8307e12SArchie Cobbs /* Get msg->header.arglen when "buf" is pointing to msg->data */ 926f8307e12SArchie Cobbs static int 927f8307e12SArchie Cobbs ng_parse_ng_mesg_getLength(const struct ng_parse_type *type, 928f8307e12SArchie Cobbs const u_char *start, const u_char *buf) 929f8307e12SArchie Cobbs { 930f8307e12SArchie Cobbs const struct ng_mesg *msg; 931f8307e12SArchie Cobbs 932f8307e12SArchie Cobbs msg = (const struct ng_mesg *)(buf - sizeof(*msg)); 933f8307e12SArchie Cobbs return msg->header.arglen; 934f8307e12SArchie Cobbs } 935f8307e12SArchie Cobbs 936f8307e12SArchie Cobbs /* Type for the variable length data portion of a struct ng_mesg */ 937f8307e12SArchie Cobbs static const struct ng_parse_type ng_msg_data_type = { 938f8307e12SArchie Cobbs &ng_parse_bytearray_type, 939f8307e12SArchie Cobbs &ng_parse_ng_mesg_getLength 940f8307e12SArchie Cobbs }; 941f8307e12SArchie Cobbs 942f8307e12SArchie Cobbs /* Type for the entire struct ng_mesg header with data section */ 943f8307e12SArchie Cobbs static const struct ng_parse_struct_info 944f8307e12SArchie Cobbs ng_parse_ng_mesg_type_info = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type); 945f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ng_mesg_type = { 946f8307e12SArchie Cobbs &ng_parse_struct_type, 947f8307e12SArchie Cobbs &ng_parse_ng_mesg_type_info, 948f8307e12SArchie Cobbs }; 949f8307e12SArchie Cobbs 950f8307e12SArchie Cobbs /************************************************************************ 951f8307e12SArchie Cobbs COMPOSITE HELPER ROUTINES 952f8307e12SArchie Cobbs ************************************************************************/ 953f8307e12SArchie Cobbs 954f8307e12SArchie Cobbs /* 955f8307e12SArchie Cobbs * Convert a structure or array from ASCII to binary 956f8307e12SArchie Cobbs */ 957f8307e12SArchie Cobbs static int 958f8307e12SArchie Cobbs ng_parse_composite(const struct ng_parse_type *type, const char *s, 959f8307e12SArchie Cobbs int *off, const u_char *const start, u_char *const buf, int *buflen, 960f8307e12SArchie Cobbs const enum comptype ctype) 961f8307e12SArchie Cobbs { 962f8307e12SArchie Cobbs const int num = ng_get_composite_len(type, start, buf, ctype); 963f8307e12SArchie Cobbs int nextIndex = 0; /* next implicit array index */ 964f8307e12SArchie Cobbs u_int index; /* field or element index */ 965f8307e12SArchie Cobbs int *foff; /* field value offsets in string */ 966f8307e12SArchie Cobbs int align, len, blen, error = 0; 967f8307e12SArchie Cobbs 968f8307e12SArchie Cobbs /* Initialize */ 969f8307e12SArchie Cobbs MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT); 970f8307e12SArchie Cobbs if (foff == NULL) { 971f8307e12SArchie Cobbs error = ENOMEM; 972f8307e12SArchie Cobbs goto done; 973f8307e12SArchie Cobbs } 974f8307e12SArchie Cobbs bzero(foff, num * sizeof(*foff)); 975f8307e12SArchie Cobbs 976f8307e12SArchie Cobbs /* Get opening brace/bracket */ 977f8307e12SArchie Cobbs if (ng_parse_get_token(s, off, &len) 978f8307e12SArchie Cobbs != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) { 979f8307e12SArchie Cobbs error = EINVAL; 980f8307e12SArchie Cobbs goto done; 981f8307e12SArchie Cobbs } 982f8307e12SArchie Cobbs *off += len; 983f8307e12SArchie Cobbs 984f8307e12SArchie Cobbs /* Get individual element value positions in the string */ 985f8307e12SArchie Cobbs for (;;) { 986f8307e12SArchie Cobbs enum ng_parse_token tok; 987f8307e12SArchie Cobbs 988f8307e12SArchie Cobbs /* Check for closing brace/bracket */ 989f8307e12SArchie Cobbs tok = ng_parse_get_token(s, off, &len); 990f8307e12SArchie Cobbs if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) { 991f8307e12SArchie Cobbs *off += len; 992f8307e12SArchie Cobbs break; 993f8307e12SArchie Cobbs } 994f8307e12SArchie Cobbs 995f8307e12SArchie Cobbs /* For arrays, the 'name' (ie, index) is optional, so 996f8307e12SArchie Cobbs distinguish name from values by seeing if the next 997f8307e12SArchie Cobbs token is an equals sign */ 998f8307e12SArchie Cobbs if (ctype != CT_STRUCT) { 999f8307e12SArchie Cobbs int len2, off2; 1000f8307e12SArchie Cobbs char *eptr; 1001f8307e12SArchie Cobbs 1002f8307e12SArchie Cobbs /* If an opening brace/bracket, index is implied */ 1003f8307e12SArchie Cobbs if (tok == T_LBRACE || tok == T_LBRACKET) { 1004f8307e12SArchie Cobbs index = nextIndex++; 1005f8307e12SArchie Cobbs goto gotIndex; 1006f8307e12SArchie Cobbs } 1007f8307e12SArchie Cobbs 1008f8307e12SArchie Cobbs /* Might be an index, might be a value, either way... */ 1009f8307e12SArchie Cobbs if (tok != T_WORD) { 1010f8307e12SArchie Cobbs error = EINVAL; 1011f8307e12SArchie Cobbs goto done; 1012f8307e12SArchie Cobbs } 1013f8307e12SArchie Cobbs 1014f8307e12SArchie Cobbs /* If no equals sign follows, index is implied */ 1015f8307e12SArchie Cobbs off2 = *off + len; 1016f8307e12SArchie Cobbs if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) { 1017f8307e12SArchie Cobbs index = nextIndex++; 1018f8307e12SArchie Cobbs goto gotIndex; 1019f8307e12SArchie Cobbs } 1020f8307e12SArchie Cobbs 1021f8307e12SArchie Cobbs /* Index was specified explicitly; parse it */ 1022f8307e12SArchie Cobbs index = (u_int)strtoul(s + *off, &eptr, 0); 1023f8307e12SArchie Cobbs if (index < 0 || eptr - (s + *off) != len) { 1024f8307e12SArchie Cobbs error = EINVAL; 1025f8307e12SArchie Cobbs goto done; 1026f8307e12SArchie Cobbs } 1027f8307e12SArchie Cobbs nextIndex = index + 1; 1028f8307e12SArchie Cobbs *off += len + len2; 1029f8307e12SArchie Cobbs gotIndex: 1030f8307e12SArchie Cobbs } else { /* a structure field */ 1031f8307e12SArchie Cobbs const struct ng_parse_struct_field *field = NULL; 1032f8307e12SArchie Cobbs const struct ng_parse_struct_info *si = type->info; 1033f8307e12SArchie Cobbs 1034f8307e12SArchie Cobbs /* Find the field by name (required) in field list */ 1035f8307e12SArchie Cobbs if (tok != T_WORD) { 1036f8307e12SArchie Cobbs error = EINVAL; 1037f8307e12SArchie Cobbs goto done; 1038f8307e12SArchie Cobbs } 1039f8307e12SArchie Cobbs for (index = 0; index < num; index++) { 1040f8307e12SArchie Cobbs field = &si->fields[index]; 1041f8307e12SArchie Cobbs if (strncmp(&s[*off], field->name, len) == 0 1042f8307e12SArchie Cobbs && field->name[len] == '\0') 1043f8307e12SArchie Cobbs break; 1044f8307e12SArchie Cobbs } 1045f8307e12SArchie Cobbs if (index == num) { 1046f8307e12SArchie Cobbs error = ENOENT; 1047f8307e12SArchie Cobbs goto done; 1048f8307e12SArchie Cobbs } 1049f8307e12SArchie Cobbs *off += len; 1050f8307e12SArchie Cobbs 1051f8307e12SArchie Cobbs /* Get equals sign */ 1052f8307e12SArchie Cobbs if (ng_parse_get_token(s, off, &len) != T_EQUALS) { 1053f8307e12SArchie Cobbs error = EINVAL; 1054f8307e12SArchie Cobbs goto done; 1055f8307e12SArchie Cobbs } 1056f8307e12SArchie Cobbs *off += len; 1057f8307e12SArchie Cobbs } 1058f8307e12SArchie Cobbs 1059f8307e12SArchie Cobbs /* Check array index */ 1060f8307e12SArchie Cobbs if (index >= num) { 1061f8307e12SArchie Cobbs error = E2BIG; 1062f8307e12SArchie Cobbs goto done; 1063f8307e12SArchie Cobbs } 1064f8307e12SArchie Cobbs 1065f8307e12SArchie Cobbs /* Save value's position and skip over it for now */ 1066f8307e12SArchie Cobbs if (foff[index] != 0) { 1067f8307e12SArchie Cobbs error = EALREADY; /* duplicate */ 1068f8307e12SArchie Cobbs goto done; 1069f8307e12SArchie Cobbs } 1070f8307e12SArchie Cobbs while (isspace(s[*off])) 1071f8307e12SArchie Cobbs (*off)++; 1072f8307e12SArchie Cobbs foff[index] = *off; 1073f8307e12SArchie Cobbs if ((error = ng_parse_skip_value(s, *off, &len)) != 0) 1074f8307e12SArchie Cobbs goto done; 1075f8307e12SArchie Cobbs *off += len; 1076f8307e12SArchie Cobbs } 1077f8307e12SArchie Cobbs 1078f8307e12SArchie Cobbs /* Now build binary structure from supplied values and defaults */ 1079f8307e12SArchie Cobbs for (blen = index = 0; index < num; index++) { 1080f8307e12SArchie Cobbs const struct ng_parse_type *const 1081f8307e12SArchie Cobbs etype = ng_get_composite_etype(type, index, ctype); 1082f8307e12SArchie Cobbs int k, pad, vlen; 1083f8307e12SArchie Cobbs 1084f8307e12SArchie Cobbs /* Zero-pad any alignment bytes */ 1085f8307e12SArchie Cobbs pad = ng_parse_get_elem_pad(type, index, ctype, blen); 1086f8307e12SArchie Cobbs for (k = 0; k < pad; k++) { 1087f8307e12SArchie Cobbs if (blen >= *buflen) { 1088f8307e12SArchie Cobbs error = ERANGE; 1089f8307e12SArchie Cobbs goto done; 1090f8307e12SArchie Cobbs } 1091f8307e12SArchie Cobbs buf[blen++] = 0; 1092f8307e12SArchie Cobbs } 1093f8307e12SArchie Cobbs 1094f8307e12SArchie Cobbs /* Get value */ 1095f8307e12SArchie Cobbs vlen = *buflen - blen; 1096f8307e12SArchie Cobbs if (foff[index] == 0) { /* use default value */ 1097f8307e12SArchie Cobbs error = ng_get_composite_elem_default(type, index, 1098f8307e12SArchie Cobbs start, buf + blen, &vlen, ctype); 1099f8307e12SArchie Cobbs } else { /* parse given value */ 1100f8307e12SArchie Cobbs *off = foff[index]; 1101f8307e12SArchie Cobbs error = INVOKE(etype, parse)(etype, 1102f8307e12SArchie Cobbs s, off, start, buf + blen, &vlen); 1103f8307e12SArchie Cobbs } 1104f8307e12SArchie Cobbs if (error != 0) 1105f8307e12SArchie Cobbs goto done; 1106f8307e12SArchie Cobbs blen += vlen; 1107f8307e12SArchie Cobbs } 1108f8307e12SArchie Cobbs 1109f8307e12SArchie Cobbs /* Make total composite structure size a multiple of its alignment */ 1110f8307e12SArchie Cobbs if ((align = ALIGNMENT(type)) != 0) { 1111f8307e12SArchie Cobbs while (blen % align != 0) { 1112f8307e12SArchie Cobbs if (blen >= *buflen) { 1113f8307e12SArchie Cobbs error = ERANGE; 1114f8307e12SArchie Cobbs goto done; 1115f8307e12SArchie Cobbs } 1116f8307e12SArchie Cobbs buf[blen++] = 0; 1117f8307e12SArchie Cobbs } 1118f8307e12SArchie Cobbs } 1119f8307e12SArchie Cobbs 1120f8307e12SArchie Cobbs /* Done */ 1121f8307e12SArchie Cobbs *buflen = blen; 1122f8307e12SArchie Cobbs done: 1123f8307e12SArchie Cobbs FREE(foff, M_NETGRAPH); 1124f8307e12SArchie Cobbs return (error); 1125f8307e12SArchie Cobbs } 1126f8307e12SArchie Cobbs 1127f8307e12SArchie Cobbs /* 1128f8307e12SArchie Cobbs * Convert an array or structure from binary to ASCII 1129f8307e12SArchie Cobbs */ 1130f8307e12SArchie Cobbs static int 1131f8307e12SArchie Cobbs ng_unparse_composite(const struct ng_parse_type *type, const u_char *data, 1132f8307e12SArchie Cobbs int *off, char *cbuf, int cbuflen, const enum comptype ctype) 1133f8307e12SArchie Cobbs { 1134f8307e12SArchie Cobbs const int num = ng_get_composite_len(type, data, data + *off, ctype); 1135f8307e12SArchie Cobbs int nextIndex = 0, didOne = 0; 1136f8307e12SArchie Cobbs int error, index; 1137f8307e12SArchie Cobbs 1138f8307e12SArchie Cobbs /* Opening brace/bracket */ 1139f8307e12SArchie Cobbs NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '['); 1140f8307e12SArchie Cobbs 1141f8307e12SArchie Cobbs /* Do each item */ 1142f8307e12SArchie Cobbs for (index = 0; index < num; index++) { 1143f8307e12SArchie Cobbs const struct ng_parse_type *const 1144f8307e12SArchie Cobbs etype = ng_get_composite_etype(type, index, ctype); 1145f8307e12SArchie Cobbs u_char temp[1024]; 1146f8307e12SArchie Cobbs 1147f8307e12SArchie Cobbs /* Skip any alignment pad bytes */ 1148f8307e12SArchie Cobbs *off += ng_parse_get_elem_pad(type, index, ctype, *off); 1149f8307e12SArchie Cobbs 1150f8307e12SArchie Cobbs /* See if element is equal to its default value; skip if so */ 1151f8307e12SArchie Cobbs if (*off < sizeof(temp)) { 1152f8307e12SArchie Cobbs int tempsize = sizeof(temp) - *off; 1153f8307e12SArchie Cobbs 1154f8307e12SArchie Cobbs bcopy(data, temp, *off); 1155f8307e12SArchie Cobbs if (ng_get_composite_elem_default(type, index, temp, 1156f8307e12SArchie Cobbs temp + *off, &tempsize, ctype) == 0 1157f8307e12SArchie Cobbs && bcmp(temp + *off, data + *off, tempsize) == 0) { 1158f8307e12SArchie Cobbs *off += tempsize; 1159f8307e12SArchie Cobbs continue; 1160f8307e12SArchie Cobbs } 1161f8307e12SArchie Cobbs } 1162f8307e12SArchie Cobbs 1163f8307e12SArchie Cobbs /* Print name= */ 1164f8307e12SArchie Cobbs NG_PARSE_APPEND(" "); 1165f8307e12SArchie Cobbs if (ctype != CT_STRUCT) { 1166f8307e12SArchie Cobbs if (index != nextIndex) { 1167f8307e12SArchie Cobbs nextIndex = index; 1168f8307e12SArchie Cobbs NG_PARSE_APPEND("%d=", index); 1169f8307e12SArchie Cobbs } 1170f8307e12SArchie Cobbs nextIndex++; 1171f8307e12SArchie Cobbs } else { 1172f8307e12SArchie Cobbs const struct ng_parse_struct_info *si = type->info; 1173f8307e12SArchie Cobbs 1174f8307e12SArchie Cobbs NG_PARSE_APPEND("%s=", si->fields[index].name); 1175f8307e12SArchie Cobbs } 1176f8307e12SArchie Cobbs 1177f8307e12SArchie Cobbs /* Print value */ 1178f8307e12SArchie Cobbs if ((error = INVOKE(etype, unparse) 1179f8307e12SArchie Cobbs (etype, data, off, cbuf, cbuflen)) != 0) 1180f8307e12SArchie Cobbs return (error); 1181f8307e12SArchie Cobbs cbuflen -= strlen(cbuf); 1182f8307e12SArchie Cobbs cbuf += strlen(cbuf); 1183f8307e12SArchie Cobbs didOne = 1; 1184f8307e12SArchie Cobbs } 1185f8307e12SArchie Cobbs 1186f8307e12SArchie Cobbs /* Closing brace/bracket */ 1187f8307e12SArchie Cobbs NG_PARSE_APPEND("%s%c", 1188f8307e12SArchie Cobbs didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']'); 1189f8307e12SArchie Cobbs return (0); 1190f8307e12SArchie Cobbs } 1191f8307e12SArchie Cobbs 1192f8307e12SArchie Cobbs /* 1193f8307e12SArchie Cobbs * Generate the default value for an element of an array or structure 1194f8307e12SArchie Cobbs * Returns EOPNOTSUPP if default value is unspecified. 1195f8307e12SArchie Cobbs */ 1196f8307e12SArchie Cobbs static int 1197f8307e12SArchie Cobbs ng_get_composite_elem_default(const struct ng_parse_type *type, 1198f8307e12SArchie Cobbs int index, const u_char *const start, u_char *buf, int *buflen, 1199f8307e12SArchie Cobbs const enum comptype ctype) 1200f8307e12SArchie Cobbs { 1201f8307e12SArchie Cobbs const struct ng_parse_type *etype; 1202f8307e12SArchie Cobbs ng_getDefault_t *func; 1203f8307e12SArchie Cobbs 1204f8307e12SArchie Cobbs switch (ctype) { 1205f8307e12SArchie Cobbs case CT_STRUCT: 1206f8307e12SArchie Cobbs break; 1207f8307e12SArchie Cobbs case CT_ARRAY: 1208f8307e12SArchie Cobbs { 1209f8307e12SArchie Cobbs const struct ng_parse_array_info *const ai = type->info; 1210f8307e12SArchie Cobbs 1211f8307e12SArchie Cobbs if (ai->getDefault != NULL) { 1212f8307e12SArchie Cobbs return (*ai->getDefault)(type, 1213f8307e12SArchie Cobbs index, start, buf, buflen); 1214f8307e12SArchie Cobbs } 1215f8307e12SArchie Cobbs break; 1216f8307e12SArchie Cobbs } 1217f8307e12SArchie Cobbs case CT_FIXEDARRAY: 1218f8307e12SArchie Cobbs { 1219f8307e12SArchie Cobbs const struct ng_parse_fixedarray_info *const fi = type->info; 1220f8307e12SArchie Cobbs 1221f8307e12SArchie Cobbs if (*fi->getDefault != NULL) { 1222f8307e12SArchie Cobbs return (*fi->getDefault)(type, 1223f8307e12SArchie Cobbs index, start, buf, buflen); 1224f8307e12SArchie Cobbs } 1225f8307e12SArchie Cobbs break; 1226f8307e12SArchie Cobbs } 1227f8307e12SArchie Cobbs default: 1228f8307e12SArchie Cobbs panic("%s", __FUNCTION__); 1229f8307e12SArchie Cobbs } 1230f8307e12SArchie Cobbs 1231f8307e12SArchie Cobbs /* Default to element type default */ 1232f8307e12SArchie Cobbs etype = ng_get_composite_etype(type, index, ctype); 1233f8307e12SArchie Cobbs func = METHOD(etype, getDefault); 1234f8307e12SArchie Cobbs if (func == NULL) 1235f8307e12SArchie Cobbs return (EOPNOTSUPP); 1236f8307e12SArchie Cobbs return (*func)(etype, start, buf, buflen); 1237f8307e12SArchie Cobbs } 1238f8307e12SArchie Cobbs 1239f8307e12SArchie Cobbs /* 1240f8307e12SArchie Cobbs * Get the number of elements in a struct, variable or fixed array. 1241f8307e12SArchie Cobbs */ 1242f8307e12SArchie Cobbs static int 1243f8307e12SArchie Cobbs ng_get_composite_len(const struct ng_parse_type *type, 1244f8307e12SArchie Cobbs const u_char *const start, const u_char *buf, 1245f8307e12SArchie Cobbs const enum comptype ctype) 1246f8307e12SArchie Cobbs { 1247f8307e12SArchie Cobbs switch (ctype) { 1248f8307e12SArchie Cobbs case CT_STRUCT: 1249f8307e12SArchie Cobbs { 1250f8307e12SArchie Cobbs const struct ng_parse_struct_info *const si = type->info; 1251f8307e12SArchie Cobbs int numFields = 0; 1252f8307e12SArchie Cobbs 1253f8307e12SArchie Cobbs for (numFields = 0; ; numFields++) { 1254f8307e12SArchie Cobbs const struct ng_parse_struct_field *const 1255f8307e12SArchie Cobbs fi = &si->fields[numFields]; 1256f8307e12SArchie Cobbs 1257f8307e12SArchie Cobbs if (fi->name == NULL) 1258f8307e12SArchie Cobbs break; 1259f8307e12SArchie Cobbs } 1260f8307e12SArchie Cobbs return (numFields); 1261f8307e12SArchie Cobbs } 1262f8307e12SArchie Cobbs case CT_ARRAY: 1263f8307e12SArchie Cobbs { 1264f8307e12SArchie Cobbs const struct ng_parse_array_info *const ai = type->info; 1265f8307e12SArchie Cobbs 1266f8307e12SArchie Cobbs return (*ai->getLength)(type, start, buf); 1267f8307e12SArchie Cobbs } 1268f8307e12SArchie Cobbs case CT_FIXEDARRAY: 1269f8307e12SArchie Cobbs { 1270f8307e12SArchie Cobbs const struct ng_parse_fixedarray_info *const fi = type->info; 1271f8307e12SArchie Cobbs 1272f8307e12SArchie Cobbs return fi->length; 1273f8307e12SArchie Cobbs } 1274f8307e12SArchie Cobbs default: 1275f8307e12SArchie Cobbs panic("%s", __FUNCTION__); 1276f8307e12SArchie Cobbs } 1277f8307e12SArchie Cobbs return (0); 1278f8307e12SArchie Cobbs } 1279f8307e12SArchie Cobbs 1280f8307e12SArchie Cobbs /* 1281f8307e12SArchie Cobbs * Return the type of the index'th element of a composite structure 1282f8307e12SArchie Cobbs */ 1283f8307e12SArchie Cobbs static const struct ng_parse_type * 1284f8307e12SArchie Cobbs ng_get_composite_etype(const struct ng_parse_type *type, 1285f8307e12SArchie Cobbs int index, const enum comptype ctype) 1286f8307e12SArchie Cobbs { 1287f8307e12SArchie Cobbs const struct ng_parse_type *etype = NULL; 1288f8307e12SArchie Cobbs 1289f8307e12SArchie Cobbs switch (ctype) { 1290f8307e12SArchie Cobbs case CT_STRUCT: 1291f8307e12SArchie Cobbs { 1292f8307e12SArchie Cobbs const struct ng_parse_struct_info *const si = type->info; 1293f8307e12SArchie Cobbs 1294f8307e12SArchie Cobbs etype = si->fields[index].type; 1295f8307e12SArchie Cobbs break; 1296f8307e12SArchie Cobbs } 1297f8307e12SArchie Cobbs case CT_ARRAY: 1298f8307e12SArchie Cobbs { 1299f8307e12SArchie Cobbs const struct ng_parse_array_info *const ai = type->info; 1300f8307e12SArchie Cobbs 1301f8307e12SArchie Cobbs etype = ai->elementType; 1302f8307e12SArchie Cobbs break; 1303f8307e12SArchie Cobbs } 1304f8307e12SArchie Cobbs case CT_FIXEDARRAY: 1305f8307e12SArchie Cobbs { 1306f8307e12SArchie Cobbs const struct ng_parse_fixedarray_info *const fi = type->info; 1307f8307e12SArchie Cobbs 1308f8307e12SArchie Cobbs etype = fi->elementType; 1309f8307e12SArchie Cobbs break; 1310f8307e12SArchie Cobbs } 1311f8307e12SArchie Cobbs default: 1312f8307e12SArchie Cobbs panic("%s", __FUNCTION__); 1313f8307e12SArchie Cobbs } 1314f8307e12SArchie Cobbs return (etype); 1315f8307e12SArchie Cobbs } 1316f8307e12SArchie Cobbs 1317f8307e12SArchie Cobbs /* 1318f8307e12SArchie Cobbs * Get the number of bytes to skip to align for the next 1319f8307e12SArchie Cobbs * element in a composite structure. 1320f8307e12SArchie Cobbs */ 1321f8307e12SArchie Cobbs static int 1322f8307e12SArchie Cobbs ng_parse_get_elem_pad(const struct ng_parse_type *type, 1323f8307e12SArchie Cobbs int index, enum comptype ctype, int posn) 1324f8307e12SArchie Cobbs { 1325f8307e12SArchie Cobbs const struct ng_parse_type *const 1326f8307e12SArchie Cobbs etype = ng_get_composite_etype(type, index, ctype); 1327f8307e12SArchie Cobbs int align; 1328f8307e12SArchie Cobbs 1329f8307e12SArchie Cobbs /* Get element's alignment, and possibly override */ 1330f8307e12SArchie Cobbs align = ALIGNMENT(etype); 1331f8307e12SArchie Cobbs if (ctype == CT_STRUCT) { 1332f8307e12SArchie Cobbs const struct ng_parse_struct_info *si = type->info; 1333f8307e12SArchie Cobbs 1334f8307e12SArchie Cobbs if (si->fields[index].alignment != 0) 1335f8307e12SArchie Cobbs align = si->fields[index].alignment; 1336f8307e12SArchie Cobbs } 1337f8307e12SArchie Cobbs 1338f8307e12SArchie Cobbs /* Return number of bytes to skip to align */ 1339f8307e12SArchie Cobbs return (align ? (align - (posn % align)) % align : 0); 1340f8307e12SArchie Cobbs } 1341f8307e12SArchie Cobbs 1342f8307e12SArchie Cobbs /************************************************************************ 1343f8307e12SArchie Cobbs PARSING HELPER ROUTINES 1344f8307e12SArchie Cobbs ************************************************************************/ 1345f8307e12SArchie Cobbs 1346f8307e12SArchie Cobbs /* 1347f8307e12SArchie Cobbs * Skip over a value 1348f8307e12SArchie Cobbs */ 1349f8307e12SArchie Cobbs static int 1350f8307e12SArchie Cobbs ng_parse_skip_value(const char *s, int off0, int *lenp) 1351f8307e12SArchie Cobbs { 1352f8307e12SArchie Cobbs int len, nbracket, nbrace; 1353f8307e12SArchie Cobbs int off = off0; 1354f8307e12SArchie Cobbs 1355f8307e12SArchie Cobbs len = nbracket = nbrace = 0; 1356f8307e12SArchie Cobbs do { 1357f8307e12SArchie Cobbs switch (ng_parse_get_token(s, &off, &len)) { 1358f8307e12SArchie Cobbs case T_LBRACKET: 1359f8307e12SArchie Cobbs nbracket++; 1360f8307e12SArchie Cobbs break; 1361f8307e12SArchie Cobbs case T_LBRACE: 1362f8307e12SArchie Cobbs nbrace++; 1363f8307e12SArchie Cobbs break; 1364f8307e12SArchie Cobbs case T_RBRACKET: 1365f8307e12SArchie Cobbs if (nbracket-- == 0) 1366f8307e12SArchie Cobbs return (EINVAL); 1367f8307e12SArchie Cobbs break; 1368f8307e12SArchie Cobbs case T_RBRACE: 1369f8307e12SArchie Cobbs if (nbrace-- == 0) 1370f8307e12SArchie Cobbs return (EINVAL); 1371f8307e12SArchie Cobbs break; 1372f8307e12SArchie Cobbs case T_EOF: 1373f8307e12SArchie Cobbs return (EINVAL); 1374f8307e12SArchie Cobbs default: 1375f8307e12SArchie Cobbs break; 1376f8307e12SArchie Cobbs } 1377f8307e12SArchie Cobbs off += len; 1378f8307e12SArchie Cobbs } while (nbracket > 0 || nbrace > 0); 1379f8307e12SArchie Cobbs *lenp = off - off0; 1380f8307e12SArchie Cobbs return (0); 1381f8307e12SArchie Cobbs } 1382f8307e12SArchie Cobbs 1383f8307e12SArchie Cobbs /* 1384f8307e12SArchie Cobbs * Find the next token in the string, starting at offset *startp. 1385f8307e12SArchie Cobbs * Returns the token type, with *startp pointing to the first char 1386f8307e12SArchie Cobbs * and *lenp the length. 1387f8307e12SArchie Cobbs */ 1388f8307e12SArchie Cobbs enum ng_parse_token 1389f8307e12SArchie Cobbs ng_parse_get_token(const char *s, int *startp, int *lenp) 1390f8307e12SArchie Cobbs { 1391f8307e12SArchie Cobbs char *t; 1392f8307e12SArchie Cobbs int i; 1393f8307e12SArchie Cobbs 1394f8307e12SArchie Cobbs while (isspace(s[*startp])) 1395f8307e12SArchie Cobbs (*startp)++; 1396f8307e12SArchie Cobbs switch (s[*startp]) { 1397f8307e12SArchie Cobbs case '\0': 1398f8307e12SArchie Cobbs *lenp = 0; 1399f8307e12SArchie Cobbs return T_EOF; 1400f8307e12SArchie Cobbs case '{': 1401f8307e12SArchie Cobbs *lenp = 1; 1402f8307e12SArchie Cobbs return T_LBRACE; 1403f8307e12SArchie Cobbs case '}': 1404f8307e12SArchie Cobbs *lenp = 1; 1405f8307e12SArchie Cobbs return T_RBRACE; 1406f8307e12SArchie Cobbs case '[': 1407f8307e12SArchie Cobbs *lenp = 1; 1408f8307e12SArchie Cobbs return T_LBRACKET; 1409f8307e12SArchie Cobbs case ']': 1410f8307e12SArchie Cobbs *lenp = 1; 1411f8307e12SArchie Cobbs return T_RBRACKET; 1412f8307e12SArchie Cobbs case '=': 1413f8307e12SArchie Cobbs *lenp = 1; 1414f8307e12SArchie Cobbs return T_EQUALS; 1415f8307e12SArchie Cobbs case '"': 1416f8307e12SArchie Cobbs if ((t = ng_get_string_token(s, startp, lenp)) == NULL) 1417f8307e12SArchie Cobbs return T_ERROR; 1418f8307e12SArchie Cobbs FREE(t, M_NETGRAPH); 1419f8307e12SArchie Cobbs return T_STRING; 1420f8307e12SArchie Cobbs default: 1421f8307e12SArchie Cobbs for (i = *startp + 1; s[i] != '\0' && !isspace(s[i]) 1422f8307e12SArchie Cobbs && s[i] != '{' && s[i] != '}' && s[i] != '[' 1423f8307e12SArchie Cobbs && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++) 1424f8307e12SArchie Cobbs ; 1425f8307e12SArchie Cobbs *lenp = i - *startp; 1426f8307e12SArchie Cobbs return T_WORD; 1427f8307e12SArchie Cobbs } 1428f8307e12SArchie Cobbs } 1429f8307e12SArchie Cobbs 1430f8307e12SArchie Cobbs /* 1431f8307e12SArchie Cobbs * Get a string token, which must be enclosed in double quotes. 1432f8307e12SArchie Cobbs * The normal C backslash escapes are recognized. 1433f8307e12SArchie Cobbs */ 1434f8307e12SArchie Cobbs char * 1435f8307e12SArchie Cobbs ng_get_string_token(const char *s, int *startp, int *lenp) 1436f8307e12SArchie Cobbs { 1437f8307e12SArchie Cobbs char *cbuf, *p; 1438f8307e12SArchie Cobbs int start, off; 1439f8307e12SArchie Cobbs 1440f8307e12SArchie Cobbs while (isspace(s[*startp])) 1441f8307e12SArchie Cobbs (*startp)++; 1442f8307e12SArchie Cobbs start = *startp; 1443f8307e12SArchie Cobbs if (s[*startp] != '"') 1444f8307e12SArchie Cobbs return (NULL); 1445f8307e12SArchie Cobbs MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT); 1446f8307e12SArchie Cobbs if (cbuf == NULL) 1447f8307e12SArchie Cobbs return (NULL); 1448f8307e12SArchie Cobbs strcpy(cbuf, s + start + 1); 1449f8307e12SArchie Cobbs for (off = 1, p = cbuf; *p != '\0'; off++, p++) { 1450f8307e12SArchie Cobbs if (*p == '"') { 1451f8307e12SArchie Cobbs *p = '\0'; 1452f8307e12SArchie Cobbs *lenp = off + 1; 1453f8307e12SArchie Cobbs return (cbuf); 1454f8307e12SArchie Cobbs } else if (p[0] == '\\' && p[1] != '\0') { 1455f8307e12SArchie Cobbs int x, k; 1456f8307e12SArchie Cobbs char *v; 1457f8307e12SArchie Cobbs 1458f8307e12SArchie Cobbs strcpy(p, p + 1); 1459f8307e12SArchie Cobbs v = p; 1460f8307e12SArchie Cobbs switch (*p) { 1461f8307e12SArchie Cobbs case 't': 1462f8307e12SArchie Cobbs *v = '\t'; 1463f8307e12SArchie Cobbs off++; 1464f8307e12SArchie Cobbs continue; 1465f8307e12SArchie Cobbs case 'n': 1466f8307e12SArchie Cobbs *v = '\n'; 1467f8307e12SArchie Cobbs off++; 1468f8307e12SArchie Cobbs continue; 1469f8307e12SArchie Cobbs case 'r': 1470f8307e12SArchie Cobbs *v = '\r'; 1471f8307e12SArchie Cobbs off++; 1472f8307e12SArchie Cobbs continue; 1473f8307e12SArchie Cobbs case 'v': 1474f8307e12SArchie Cobbs *v = '\v'; 1475f8307e12SArchie Cobbs off++; 1476f8307e12SArchie Cobbs continue; 1477f8307e12SArchie Cobbs case 'f': 1478f8307e12SArchie Cobbs *v = '\f'; 1479f8307e12SArchie Cobbs off++; 1480f8307e12SArchie Cobbs continue; 1481f8307e12SArchie Cobbs case '"': 1482f8307e12SArchie Cobbs *v = '"'; 1483f8307e12SArchie Cobbs off++; 1484f8307e12SArchie Cobbs continue; 1485f8307e12SArchie Cobbs case '0': case '1': case '2': case '3': 1486f8307e12SArchie Cobbs case '4': case '5': case '6': case '7': 1487f8307e12SArchie Cobbs for (x = k = 0; 1488f8307e12SArchie Cobbs k < 3 && *v >= '0' && *v <= '7'; v++) { 1489f8307e12SArchie Cobbs x = (x << 3) + (*v - '0'); 1490f8307e12SArchie Cobbs off++; 1491f8307e12SArchie Cobbs } 1492f8307e12SArchie Cobbs *--v = (char)x; 1493f8307e12SArchie Cobbs break; 1494f8307e12SArchie Cobbs case 'x': 1495f8307e12SArchie Cobbs for (v++, x = k = 0; 1496f8307e12SArchie Cobbs k < 2 && isxdigit(*v); v++) { 1497f8307e12SArchie Cobbs x = (x << 4) + (isdigit(*v) ? 1498f8307e12SArchie Cobbs (*v - '0') : 1499f8307e12SArchie Cobbs (tolower(*v) - 'a' + 10)); 1500f8307e12SArchie Cobbs off++; 1501f8307e12SArchie Cobbs } 1502f8307e12SArchie Cobbs *--v = (char)x; 1503f8307e12SArchie Cobbs break; 1504f8307e12SArchie Cobbs default: 1505f8307e12SArchie Cobbs continue; 1506f8307e12SArchie Cobbs } 1507f8307e12SArchie Cobbs strcpy(p, v); 1508f8307e12SArchie Cobbs } 1509f8307e12SArchie Cobbs } 1510f8307e12SArchie Cobbs return (NULL); /* no closing quote */ 1511f8307e12SArchie Cobbs } 1512f8307e12SArchie Cobbs 1513f8307e12SArchie Cobbs /* 1514f8307e12SArchie Cobbs * Encode a string so it can be safely put in double quotes. 1515f8307e12SArchie Cobbs * Caller must free the result. 1516f8307e12SArchie Cobbs */ 1517f8307e12SArchie Cobbs char * 1518f8307e12SArchie Cobbs ng_encode_string(const char *raw) 1519f8307e12SArchie Cobbs { 1520f8307e12SArchie Cobbs char *cbuf; 1521f8307e12SArchie Cobbs int off = 0; 1522f8307e12SArchie Cobbs 1523f8307e12SArchie Cobbs MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT); 1524f8307e12SArchie Cobbs if (cbuf == NULL) 1525f8307e12SArchie Cobbs return (NULL); 1526f8307e12SArchie Cobbs cbuf[off++] = '"'; 1527f8307e12SArchie Cobbs for ( ; *raw != '\0'; raw++) { 1528f8307e12SArchie Cobbs switch (*raw) { 1529f8307e12SArchie Cobbs case '\t': 1530f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1531f8307e12SArchie Cobbs cbuf[off++] = 't'; 1532f8307e12SArchie Cobbs break; 1533f8307e12SArchie Cobbs case '\f': 1534f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1535f8307e12SArchie Cobbs cbuf[off++] = 'f'; 1536f8307e12SArchie Cobbs break; 1537f8307e12SArchie Cobbs case '\n': 1538f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1539f8307e12SArchie Cobbs cbuf[off++] = 'n'; 1540f8307e12SArchie Cobbs break; 1541f8307e12SArchie Cobbs case '\r': 1542f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1543f8307e12SArchie Cobbs cbuf[off++] = 'r'; 1544f8307e12SArchie Cobbs break; 1545f8307e12SArchie Cobbs case '\v': 1546f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1547f8307e12SArchie Cobbs cbuf[off++] = 'v'; 1548f8307e12SArchie Cobbs break; 1549f8307e12SArchie Cobbs case '"': 1550f8307e12SArchie Cobbs case '\\': 1551f8307e12SArchie Cobbs cbuf[off++] = '\\'; 1552f8307e12SArchie Cobbs cbuf[off++] = *raw; 1553f8307e12SArchie Cobbs break; 1554f8307e12SArchie Cobbs default: 1555f8307e12SArchie Cobbs if (*raw < 0x20 || *raw > 0x7e) { 1556f8307e12SArchie Cobbs off += sprintf(cbuf + off, 1557f8307e12SArchie Cobbs "\\x%02x", (u_char)*raw); 1558f8307e12SArchie Cobbs break; 1559f8307e12SArchie Cobbs } 1560f8307e12SArchie Cobbs cbuf[off++] = *raw; 1561f8307e12SArchie Cobbs break; 1562f8307e12SArchie Cobbs } 1563f8307e12SArchie Cobbs } 1564f8307e12SArchie Cobbs cbuf[off++] = '"'; 1565f8307e12SArchie Cobbs cbuf[off] = '\0'; 1566f8307e12SArchie Cobbs return (cbuf); 1567f8307e12SArchie Cobbs } 1568f8307e12SArchie Cobbs 1569f8307e12SArchie Cobbs /************************************************************************ 1570f8307e12SArchie Cobbs VIRTUAL METHOD LOOKUP 1571f8307e12SArchie Cobbs ************************************************************************/ 1572f8307e12SArchie Cobbs 1573f8307e12SArchie Cobbs static ng_parse_t * 1574f8307e12SArchie Cobbs ng_get_parse_method(const struct ng_parse_type *t) 1575f8307e12SArchie Cobbs { 1576f8307e12SArchie Cobbs while (t != NULL && t->parse == NULL) 1577f8307e12SArchie Cobbs t = t->supertype; 1578f8307e12SArchie Cobbs return (t ? t->parse : NULL); 1579f8307e12SArchie Cobbs } 1580f8307e12SArchie Cobbs 1581f8307e12SArchie Cobbs static ng_unparse_t * 1582f8307e12SArchie Cobbs ng_get_unparse_method(const struct ng_parse_type *t) 1583f8307e12SArchie Cobbs { 1584f8307e12SArchie Cobbs while (t != NULL && t->unparse == NULL) 1585f8307e12SArchie Cobbs t = t->supertype; 1586f8307e12SArchie Cobbs return (t ? t->unparse : NULL); 1587f8307e12SArchie Cobbs } 1588f8307e12SArchie Cobbs 1589f8307e12SArchie Cobbs static ng_getDefault_t * 1590f8307e12SArchie Cobbs ng_get_getDefault_method(const struct ng_parse_type *t) 1591f8307e12SArchie Cobbs { 1592f8307e12SArchie Cobbs while (t != NULL && t->getDefault == NULL) 1593f8307e12SArchie Cobbs t = t->supertype; 1594f8307e12SArchie Cobbs return (t ? t->getDefault : NULL); 1595f8307e12SArchie Cobbs } 1596f8307e12SArchie Cobbs 1597f8307e12SArchie Cobbs static ng_getAlign_t * 1598f8307e12SArchie Cobbs ng_get_getAlign_method(const struct ng_parse_type *t) 1599f8307e12SArchie Cobbs { 1600f8307e12SArchie Cobbs while (t != NULL && t->getAlign == NULL) 1601f8307e12SArchie Cobbs t = t->supertype; 1602f8307e12SArchie Cobbs return (t ? t->getAlign : NULL); 1603f8307e12SArchie Cobbs } 1604f8307e12SArchie Cobbs 1605