1 /*
2 * ng_parse.c
3 */
4
5 /*-
6 * Copyright (c) 1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
19 *
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41 */
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/ctype.h>
52
53 #include <machine/stdarg.h>
54
55 #include <net/ethernet.h>
56
57 #include <netinet/in.h>
58
59 #include <netgraph/ng_message.h>
60 #include <netgraph/netgraph.h>
61 #include <netgraph/ng_parse.h>
62
63 #ifdef NG_SEPARATE_MALLOC
64 static MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
65 #else
66 #define M_NETGRAPH_PARSE M_NETGRAPH
67 #endif
68
69 /* Compute alignment for primitive integral types */
70 struct int16_temp {
71 char x;
72 int16_t y;
73 };
74
75 struct int32_temp {
76 char x;
77 int32_t y;
78 };
79
80 struct int64_temp {
81 char x;
82 int64_t y;
83 };
84
85 #define INT8_ALIGNMENT 1
86 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
87 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
88 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
89
90 /* Output format for integral types */
91 #define INT_UNSIGNED 0
92 #define INT_SIGNED 1
93 #define INT_HEX 2
94
95 /* Type of composite object: struct, array, or fixedarray */
96 enum comptype {
97 CT_STRUCT,
98 CT_ARRAY,
99 CT_FIXEDARRAY,
100 };
101
102 /* Composite types helper functions */
103 static int ng_parse_composite(const struct ng_parse_type *type,
104 const char *s, int *off, const u_char *start,
105 u_char *const buf, int *buflen, enum comptype ctype);
106 static int ng_unparse_composite(const struct ng_parse_type *type,
107 const u_char *data, int *off, char *cbuf, int cbuflen,
108 enum comptype ctype);
109 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
110 int index, const u_char *start, u_char *buf,
111 int *buflen, enum comptype ctype);
112 static int ng_get_composite_len(const struct ng_parse_type *type,
113 const u_char *start, const u_char *buf,
114 enum comptype ctype);
115 static const struct ng_parse_type *ng_get_composite_etype(const struct
116 ng_parse_type *type, int index, enum comptype ctype);
117 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
118 int index, enum comptype ctype, int posn);
119
120 /* Parsing helper functions */
121 static int ng_parse_skip_value(const char *s, int off, int *lenp);
122 static int ng_parse_append(char **cbufp, int *cbuflenp,
123 const char *fmt, ...);
124
125 /* Poor man's virtual method calls */
126 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
127 #define INVOKE(t,m) (*METHOD(t,m))
128
129 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
130 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
131 static ng_getDefault_t *ng_get_getDefault_method(const
132 struct ng_parse_type *t);
133 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
134
135 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
136 0 : INVOKE(t, getAlign)(t))
137
138 /************************************************************************
139 PUBLIC FUNCTIONS
140 ************************************************************************/
141
142 /*
143 * Convert an ASCII string to binary according to the supplied type descriptor
144 */
145 int
ng_parse(const struct ng_parse_type * type,const char * string,int * off,u_char * buf,int * buflen)146 ng_parse(const struct ng_parse_type *type,
147 const char *string, int *off, u_char *buf, int *buflen)
148 {
149 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
150 }
151
152 /*
153 * Convert binary to an ASCII string according to the supplied type descriptor
154 */
155 int
ng_unparse(const struct ng_parse_type * type,const u_char * data,char * cbuf,int cbuflen)156 ng_unparse(const struct ng_parse_type *type,
157 const u_char *data, char *cbuf, int cbuflen)
158 {
159 int off = 0;
160
161 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
162 }
163
164 /*
165 * Fill in the default value according to the supplied type descriptor
166 */
167 int
ng_parse_getDefault(const struct ng_parse_type * type,u_char * buf,int * buflen)168 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
169 {
170 ng_getDefault_t *const func = METHOD(type, getDefault);
171
172 if (func == NULL)
173 return (EOPNOTSUPP);
174 return (*func)(type, buf, buf, buflen);
175 }
176
177 /************************************************************************
178 STRUCTURE TYPE
179 ************************************************************************/
180
181 static int
ng_struct_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)182 ng_struct_parse(const struct ng_parse_type *type,
183 const char *s, int *off, const u_char *const start,
184 u_char *const buf, int *buflen)
185 {
186 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
187 }
188
189 static int
ng_struct_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)190 ng_struct_unparse(const struct ng_parse_type *type,
191 const u_char *data, int *off, char *cbuf, int cbuflen)
192 {
193 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
194 }
195
196 static int
ng_struct_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)197 ng_struct_getDefault(const struct ng_parse_type *type,
198 const u_char *const start, u_char *buf, int *buflen)
199 {
200 int off = 0;
201
202 return ng_parse_composite(type,
203 "{}", &off, start, buf, buflen, CT_STRUCT);
204 }
205
206 static int
ng_struct_getAlign(const struct ng_parse_type * type)207 ng_struct_getAlign(const struct ng_parse_type *type)
208 {
209 const struct ng_parse_struct_field *field;
210 int align = 0;
211
212 for (field = type->info; field->name != NULL; field++) {
213 int falign = ALIGNMENT(field->type);
214
215 if (falign > align)
216 align = falign;
217 }
218 return align;
219 }
220
221 const struct ng_parse_type ng_parse_struct_type = {
222 NULL,
223 NULL,
224 NULL,
225 ng_struct_parse,
226 ng_struct_unparse,
227 ng_struct_getDefault,
228 ng_struct_getAlign
229 };
230
231 /************************************************************************
232 FIXED LENGTH ARRAY TYPE
233 ************************************************************************/
234
235 static int
ng_fixedarray_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)236 ng_fixedarray_parse(const struct ng_parse_type *type,
237 const char *s, int *off, const u_char *const start,
238 u_char *const buf, int *buflen)
239 {
240 return ng_parse_composite(type,
241 s, off, start, buf, buflen, CT_FIXEDARRAY);
242 }
243
244 static int
ng_fixedarray_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)245 ng_fixedarray_unparse(const struct ng_parse_type *type,
246 const u_char *data, int *off, char *cbuf, int cbuflen)
247 {
248 return ng_unparse_composite(type,
249 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
250 }
251
252 static int
ng_fixedarray_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)253 ng_fixedarray_getDefault(const struct ng_parse_type *type,
254 const u_char *const start, u_char *buf, int *buflen)
255 {
256 int off = 0;
257
258 return ng_parse_composite(type,
259 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
260 }
261
262 static int
ng_fixedarray_getAlign(const struct ng_parse_type * type)263 ng_fixedarray_getAlign(const struct ng_parse_type *type)
264 {
265 const struct ng_parse_fixedarray_info *fi = type->info;
266
267 return ALIGNMENT(fi->elementType);
268 }
269
270 const struct ng_parse_type ng_parse_fixedarray_type = {
271 NULL,
272 NULL,
273 NULL,
274 ng_fixedarray_parse,
275 ng_fixedarray_unparse,
276 ng_fixedarray_getDefault,
277 ng_fixedarray_getAlign
278 };
279
280 /************************************************************************
281 VARIABLE LENGTH ARRAY TYPE
282 ************************************************************************/
283
284 static int
ng_array_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)285 ng_array_parse(const struct ng_parse_type *type,
286 const char *s, int *off, const u_char *const start,
287 u_char *const buf, int *buflen)
288 {
289 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
290 }
291
292 static int
ng_array_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)293 ng_array_unparse(const struct ng_parse_type *type,
294 const u_char *data, int *off, char *cbuf, int cbuflen)
295 {
296 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
297 }
298
299 static int
ng_array_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)300 ng_array_getDefault(const struct ng_parse_type *type,
301 const u_char *const start, u_char *buf, int *buflen)
302 {
303 int off = 0;
304
305 return ng_parse_composite(type,
306 "[]", &off, start, buf, buflen, CT_ARRAY);
307 }
308
309 static int
ng_array_getAlign(const struct ng_parse_type * type)310 ng_array_getAlign(const struct ng_parse_type *type)
311 {
312 const struct ng_parse_array_info *ai = type->info;
313
314 return ALIGNMENT(ai->elementType);
315 }
316
317 const struct ng_parse_type ng_parse_array_type = {
318 NULL,
319 NULL,
320 NULL,
321 ng_array_parse,
322 ng_array_unparse,
323 ng_array_getDefault,
324 ng_array_getAlign
325 };
326
327 /************************************************************************
328 INT8 TYPE
329 ************************************************************************/
330
331 static int
ng_int8_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)332 ng_int8_parse(const struct ng_parse_type *type,
333 const char *s, int *off, const u_char *const start,
334 u_char *const buf, int *buflen)
335 {
336 long val;
337 int8_t val8;
338 char *eptr;
339
340 val = strtol(s + *off, &eptr, 0);
341 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
342 return (EINVAL);
343 *off = eptr - s;
344 val8 = (int8_t)val;
345 bcopy(&val8, buf, sizeof(int8_t));
346 *buflen = sizeof(int8_t);
347 return (0);
348 }
349
350 static int
ng_int8_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)351 ng_int8_unparse(const struct ng_parse_type *type,
352 const u_char *data, int *off, char *cbuf, int cbuflen)
353 {
354 const char *fmt;
355 int fval;
356 int error;
357 int8_t val;
358
359 bcopy(data + *off, &val, sizeof(int8_t));
360 switch ((intptr_t)type->info) {
361 case INT_SIGNED:
362 fmt = "%d";
363 fval = val;
364 break;
365 case INT_UNSIGNED:
366 fmt = "%u";
367 fval = (u_int8_t)val;
368 break;
369 case INT_HEX:
370 fmt = "0x%x";
371 fval = (u_int8_t)val;
372 break;
373 default:
374 panic("%s: unknown type", __func__);
375 }
376 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
377 return (error);
378 *off += sizeof(int8_t);
379 return (0);
380 }
381
382 static int
ng_int8_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)383 ng_int8_getDefault(const struct ng_parse_type *type,
384 const u_char *const start, u_char *buf, int *buflen)
385 {
386 int8_t val;
387
388 if (*buflen < sizeof(int8_t))
389 return (ERANGE);
390 val = 0;
391 bcopy(&val, buf, sizeof(int8_t));
392 *buflen = sizeof(int8_t);
393 return (0);
394 }
395
396 static int
ng_int8_getAlign(const struct ng_parse_type * type)397 ng_int8_getAlign(const struct ng_parse_type *type)
398 {
399 return INT8_ALIGNMENT;
400 }
401
402 const struct ng_parse_type ng_parse_int8_type = {
403 NULL,
404 (void *)INT_SIGNED,
405 NULL,
406 ng_int8_parse,
407 ng_int8_unparse,
408 ng_int8_getDefault,
409 ng_int8_getAlign
410 };
411
412 const struct ng_parse_type ng_parse_uint8_type = {
413 &ng_parse_int8_type,
414 (void *)INT_UNSIGNED
415 };
416
417 const struct ng_parse_type ng_parse_hint8_type = {
418 &ng_parse_int8_type,
419 (void *)INT_HEX
420 };
421
422 /************************************************************************
423 INT16 TYPE
424 ************************************************************************/
425
426 static int
ng_int16_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)427 ng_int16_parse(const struct ng_parse_type *type,
428 const char *s, int *off, const u_char *const start,
429 u_char *const buf, int *buflen)
430 {
431 long val;
432 int16_t val16;
433 char *eptr;
434
435 val = strtol(s + *off, &eptr, 0);
436 if (val < (int16_t)0x8000
437 || val > (u_int16_t)0xffff || eptr == s + *off)
438 return (EINVAL);
439 *off = eptr - s;
440 val16 = (int16_t)val;
441 bcopy(&val16, buf, sizeof(int16_t));
442 *buflen = sizeof(int16_t);
443 return (0);
444 }
445
446 static int
ng_int16_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)447 ng_int16_unparse(const struct ng_parse_type *type,
448 const u_char *data, int *off, char *cbuf, int cbuflen)
449 {
450 const char *fmt;
451 int fval;
452 int error;
453 int16_t val;
454
455 bcopy(data + *off, &val, sizeof(int16_t));
456 switch ((intptr_t)type->info) {
457 case INT_SIGNED:
458 fmt = "%d";
459 fval = val;
460 break;
461 case INT_UNSIGNED:
462 fmt = "%u";
463 fval = (u_int16_t)val;
464 break;
465 case INT_HEX:
466 fmt = "0x%x";
467 fval = (u_int16_t)val;
468 break;
469 default:
470 panic("%s: unknown type", __func__);
471 }
472 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
473 return (error);
474 *off += sizeof(int16_t);
475 return (0);
476 }
477
478 static int
ng_int16_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)479 ng_int16_getDefault(const struct ng_parse_type *type,
480 const u_char *const start, u_char *buf, int *buflen)
481 {
482 int16_t val;
483
484 if (*buflen < sizeof(int16_t))
485 return (ERANGE);
486 val = 0;
487 bcopy(&val, buf, sizeof(int16_t));
488 *buflen = sizeof(int16_t);
489 return (0);
490 }
491
492 static int
ng_int16_getAlign(const struct ng_parse_type * type)493 ng_int16_getAlign(const struct ng_parse_type *type)
494 {
495 return INT16_ALIGNMENT;
496 }
497
498 const struct ng_parse_type ng_parse_int16_type = {
499 NULL,
500 (void *)INT_SIGNED,
501 NULL,
502 ng_int16_parse,
503 ng_int16_unparse,
504 ng_int16_getDefault,
505 ng_int16_getAlign
506 };
507
508 const struct ng_parse_type ng_parse_uint16_type = {
509 &ng_parse_int16_type,
510 (void *)INT_UNSIGNED
511 };
512
513 const struct ng_parse_type ng_parse_hint16_type = {
514 &ng_parse_int16_type,
515 (void *)INT_HEX
516 };
517
518 /************************************************************************
519 INT32 TYPE
520 ************************************************************************/
521
522 static int
ng_int32_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)523 ng_int32_parse(const struct ng_parse_type *type,
524 const char *s, int *off, const u_char *const start,
525 u_char *const buf, int *buflen)
526 {
527 long val; /* assumes long is at least 32 bits */
528 int32_t val32;
529 char *eptr;
530
531 if ((intptr_t)type->info == INT_SIGNED)
532 val = strtol(s + *off, &eptr, 0);
533 else
534 val = strtoul(s + *off, &eptr, 0);
535 if (val < (int32_t)0x80000000
536 || val > (u_int32_t)0xffffffff || eptr == s + *off)
537 return (EINVAL);
538 *off = eptr - s;
539 val32 = (int32_t)val;
540 bcopy(&val32, buf, sizeof(int32_t));
541 *buflen = sizeof(int32_t);
542 return (0);
543 }
544
545 static int
ng_int32_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)546 ng_int32_unparse(const struct ng_parse_type *type,
547 const u_char *data, int *off, char *cbuf, int cbuflen)
548 {
549 const char *fmt;
550 long fval;
551 int error;
552 int32_t val;
553
554 bcopy(data + *off, &val, sizeof(int32_t));
555 switch ((intptr_t)type->info) {
556 case INT_SIGNED:
557 fmt = "%ld";
558 fval = val;
559 break;
560 case INT_UNSIGNED:
561 fmt = "%lu";
562 fval = (u_int32_t)val;
563 break;
564 case INT_HEX:
565 fmt = "0x%lx";
566 fval = (u_int32_t)val;
567 break;
568 default:
569 panic("%s: unknown type", __func__);
570 }
571 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
572 return (error);
573 *off += sizeof(int32_t);
574 return (0);
575 }
576
577 static int
ng_int32_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)578 ng_int32_getDefault(const struct ng_parse_type *type,
579 const u_char *const start, u_char *buf, int *buflen)
580 {
581 int32_t val;
582
583 if (*buflen < sizeof(int32_t))
584 return (ERANGE);
585 val = 0;
586 bcopy(&val, buf, sizeof(int32_t));
587 *buflen = sizeof(int32_t);
588 return (0);
589 }
590
591 static int
ng_int32_getAlign(const struct ng_parse_type * type)592 ng_int32_getAlign(const struct ng_parse_type *type)
593 {
594 return INT32_ALIGNMENT;
595 }
596
597 const struct ng_parse_type ng_parse_int32_type = {
598 NULL,
599 (void *)INT_SIGNED,
600 NULL,
601 ng_int32_parse,
602 ng_int32_unparse,
603 ng_int32_getDefault,
604 ng_int32_getAlign
605 };
606
607 const struct ng_parse_type ng_parse_uint32_type = {
608 &ng_parse_int32_type,
609 (void *)INT_UNSIGNED
610 };
611
612 const struct ng_parse_type ng_parse_hint32_type = {
613 &ng_parse_int32_type,
614 (void *)INT_HEX
615 };
616
617 /************************************************************************
618 INT64 TYPE
619 ************************************************************************/
620
621 static int
ng_int64_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)622 ng_int64_parse(const struct ng_parse_type *type,
623 const char *s, int *off, const u_char *const start,
624 u_char *const buf, int *buflen)
625 {
626 quad_t val;
627 int64_t val64;
628 char *eptr;
629
630 val = strtoq(s + *off, &eptr, 0);
631 if (eptr == s + *off)
632 return (EINVAL);
633 *off = eptr - s;
634 val64 = (int64_t)val;
635 bcopy(&val64, buf, sizeof(int64_t));
636 *buflen = sizeof(int64_t);
637 return (0);
638 }
639
640 static int
ng_int64_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)641 ng_int64_unparse(const struct ng_parse_type *type,
642 const u_char *data, int *off, char *cbuf, int cbuflen)
643 {
644 const char *fmt;
645 long long fval;
646 int64_t val;
647 int error;
648
649 bcopy(data + *off, &val, sizeof(int64_t));
650 switch ((intptr_t)type->info) {
651 case INT_SIGNED:
652 fmt = "%lld";
653 fval = val;
654 break;
655 case INT_UNSIGNED:
656 fmt = "%llu";
657 fval = (u_int64_t)val;
658 break;
659 case INT_HEX:
660 fmt = "0x%llx";
661 fval = (u_int64_t)val;
662 break;
663 default:
664 panic("%s: unknown type", __func__);
665 }
666 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
667 return (error);
668 *off += sizeof(int64_t);
669 return (0);
670 }
671
672 static int
ng_int64_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)673 ng_int64_getDefault(const struct ng_parse_type *type,
674 const u_char *const start, u_char *buf, int *buflen)
675 {
676 int64_t val;
677
678 if (*buflen < sizeof(int64_t))
679 return (ERANGE);
680 val = 0;
681 bcopy(&val, buf, sizeof(int64_t));
682 *buflen = sizeof(int64_t);
683 return (0);
684 }
685
686 static int
ng_int64_getAlign(const struct ng_parse_type * type)687 ng_int64_getAlign(const struct ng_parse_type *type)
688 {
689 return INT64_ALIGNMENT;
690 }
691
692 const struct ng_parse_type ng_parse_int64_type = {
693 NULL,
694 (void *)INT_SIGNED,
695 NULL,
696 ng_int64_parse,
697 ng_int64_unparse,
698 ng_int64_getDefault,
699 ng_int64_getAlign
700 };
701
702 const struct ng_parse_type ng_parse_uint64_type = {
703 &ng_parse_int64_type,
704 (void *)INT_UNSIGNED
705 };
706
707 const struct ng_parse_type ng_parse_hint64_type = {
708 &ng_parse_int64_type,
709 (void *)INT_HEX
710 };
711
712 /************************************************************************
713 STRING TYPE
714 ************************************************************************/
715
716 static int
ng_string_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)717 ng_string_parse(const struct ng_parse_type *type,
718 const char *s, int *off, const u_char *const start,
719 u_char *const buf, int *buflen)
720 {
721 char *sval;
722 int len;
723 int slen;
724
725 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
726 return (EINVAL);
727 *off += len;
728 bcopy(sval, buf, slen + 1);
729 free(sval, M_NETGRAPH_PARSE);
730 *buflen = slen + 1;
731 return (0);
732 }
733
734 static int
ng_string_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)735 ng_string_unparse(const struct ng_parse_type *type,
736 const u_char *data, int *off, char *cbuf, int cbuflen)
737 {
738 const char *const raw = (const char *)data + *off;
739 char *const s = ng_encode_string(raw, strlen(raw));
740 int error;
741
742 if (s == NULL)
743 return (ENOMEM);
744 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
745 free(s, M_NETGRAPH_PARSE);
746 return (error);
747 }
748 *off += strlen(raw) + 1;
749 free(s, M_NETGRAPH_PARSE);
750 return (0);
751 }
752
753 static int
ng_string_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)754 ng_string_getDefault(const struct ng_parse_type *type,
755 const u_char *const start, u_char *buf, int *buflen)
756 {
757
758 if (*buflen < 1)
759 return (ERANGE);
760 buf[0] = (u_char)'\0';
761 *buflen = 1;
762 return (0);
763 }
764
765 const struct ng_parse_type ng_parse_string_type = {
766 NULL,
767 NULL,
768 NULL,
769 ng_string_parse,
770 ng_string_unparse,
771 ng_string_getDefault,
772 NULL
773 };
774
775 /************************************************************************
776 FIXED BUFFER STRING TYPE
777 ************************************************************************/
778
779 static int
ng_fixedstring_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)780 ng_fixedstring_parse(const struct ng_parse_type *type,
781 const char *s, int *off, const u_char *const start,
782 u_char *const buf, int *buflen)
783 {
784 const struct ng_parse_fixedstring_info *const fi = type->info;
785 char *sval;
786 int len;
787 int slen;
788
789 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
790 return (EINVAL);
791 if (slen + 1 > fi->bufSize) {
792 free(sval, M_NETGRAPH_PARSE);
793 return (E2BIG);
794 }
795 *off += len;
796 bcopy(sval, buf, slen);
797 free(sval, M_NETGRAPH_PARSE);
798 bzero(buf + slen, fi->bufSize - slen);
799 *buflen = fi->bufSize;
800 return (0);
801 }
802
803 static int
ng_fixedstring_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)804 ng_fixedstring_unparse(const struct ng_parse_type *type,
805 const u_char *data, int *off, char *cbuf, int cbuflen)
806 {
807 const struct ng_parse_fixedstring_info *const fi = type->info;
808 int error, temp = *off;
809
810 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
811 return (error);
812 *off += fi->bufSize;
813 return (0);
814 }
815
816 static int
ng_fixedstring_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)817 ng_fixedstring_getDefault(const struct ng_parse_type *type,
818 const u_char *const start, u_char *buf, int *buflen)
819 {
820 const struct ng_parse_fixedstring_info *const fi = type->info;
821
822 if (*buflen < fi->bufSize)
823 return (ERANGE);
824 bzero(buf, fi->bufSize);
825 *buflen = fi->bufSize;
826 return (0);
827 }
828
829 const struct ng_parse_type ng_parse_fixedstring_type = {
830 NULL,
831 NULL,
832 NULL,
833 ng_fixedstring_parse,
834 ng_fixedstring_unparse,
835 ng_fixedstring_getDefault,
836 NULL
837 };
838
839 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
840 NG_NODESIZ
841 };
842 const struct ng_parse_type ng_parse_nodebuf_type = {
843 &ng_parse_fixedstring_type,
844 &ng_parse_nodebuf_info
845 };
846
847 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
848 NG_HOOKSIZ
849 };
850 const struct ng_parse_type ng_parse_hookbuf_type = {
851 &ng_parse_fixedstring_type,
852 &ng_parse_hookbuf_info
853 };
854
855 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
856 NG_PATHSIZ
857 };
858 const struct ng_parse_type ng_parse_pathbuf_type = {
859 &ng_parse_fixedstring_type,
860 &ng_parse_pathbuf_info
861 };
862
863 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
864 NG_TYPESIZ
865 };
866 const struct ng_parse_type ng_parse_typebuf_type = {
867 &ng_parse_fixedstring_type,
868 &ng_parse_typebuf_info
869 };
870
871 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
872 NG_CMDSTRSIZ
873 };
874 const struct ng_parse_type ng_parse_cmdbuf_type = {
875 &ng_parse_fixedstring_type,
876 &ng_parse_cmdbuf_info
877 };
878
879 /************************************************************************
880 EXPLICITLY SIZED STRING TYPE
881 ************************************************************************/
882
883 static int
ng_sizedstring_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)884 ng_sizedstring_parse(const struct ng_parse_type *type,
885 const char *s, int *off, const u_char *const start,
886 u_char *const buf, int *buflen)
887 {
888 char *sval;
889 int len;
890 int slen;
891
892 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
893 return (EINVAL);
894 if (slen > USHRT_MAX) {
895 free(sval, M_NETGRAPH_PARSE);
896 return (EINVAL);
897 }
898 *off += len;
899 *((u_int16_t *)buf) = (u_int16_t)slen;
900 bcopy(sval, buf + 2, slen);
901 free(sval, M_NETGRAPH_PARSE);
902 *buflen = 2 + slen;
903 return (0);
904 }
905
906 static int
ng_sizedstring_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)907 ng_sizedstring_unparse(const struct ng_parse_type *type,
908 const u_char *data, int *off, char *cbuf, int cbuflen)
909 {
910 const char *const raw = (const char *)data + *off + 2;
911 const int slen = *((const u_int16_t *)(data + *off));
912 char *const s = ng_encode_string(raw, slen);
913 int error;
914
915 if (s == NULL)
916 return (ENOMEM);
917 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
918 free(s, M_NETGRAPH_PARSE);
919 return (error);
920 }
921 free(s, M_NETGRAPH_PARSE);
922 *off += slen + 2;
923 return (0);
924 }
925
926 static int
ng_sizedstring_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)927 ng_sizedstring_getDefault(const struct ng_parse_type *type,
928 const u_char *const start, u_char *buf, int *buflen)
929 {
930 if (*buflen < 2)
931 return (ERANGE);
932 bzero(buf, 2);
933 *buflen = 2;
934 return (0);
935 }
936
937 const struct ng_parse_type ng_parse_sizedstring_type = {
938 NULL,
939 NULL,
940 NULL,
941 ng_sizedstring_parse,
942 ng_sizedstring_unparse,
943 ng_sizedstring_getDefault,
944 NULL
945 };
946
947 /************************************************************************
948 IP ADDRESS TYPE
949 ************************************************************************/
950
951 static int
ng_ipaddr_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)952 ng_ipaddr_parse(const struct ng_parse_type *type,
953 const char *s, int *off, const u_char *const start,
954 u_char *const buf, int *buflen)
955 {
956 int i, error;
957
958 for (i = 0; i < 4; i++) {
959 if ((error = ng_int8_parse(&ng_parse_int8_type,
960 s, off, start, buf + i, buflen)) != 0)
961 return (error);
962 if (i < 3) {
963 if (s[*off] != '.')
964 return (EINVAL);
965 (*off)++;
966 }
967 }
968 *buflen = 4;
969 return (0);
970 }
971
972 static int
ng_ipaddr_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)973 ng_ipaddr_unparse(const struct ng_parse_type *type,
974 const u_char *data, int *off, char *cbuf, int cbuflen)
975 {
976 struct in_addr ip;
977 int error;
978
979 bcopy(data + *off, &ip, sizeof(ip));
980 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
981 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
982 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
983 return (error);
984 *off += sizeof(ip);
985 return (0);
986 }
987
988 static int
ng_ipaddr_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)989 ng_ipaddr_getDefault(const struct ng_parse_type *type,
990 const u_char *const start, u_char *buf, int *buflen)
991 {
992 struct in_addr ip = { 0 };
993
994 if (*buflen < sizeof(ip))
995 return (ERANGE);
996 bcopy(&ip, buf, sizeof(ip));
997 *buflen = sizeof(ip);
998 return (0);
999 }
1000
1001 const struct ng_parse_type ng_parse_ipaddr_type = {
1002 NULL,
1003 NULL,
1004 NULL,
1005 ng_ipaddr_parse,
1006 ng_ipaddr_unparse,
1007 ng_ipaddr_getDefault,
1008 ng_int32_getAlign
1009 };
1010
1011 /************************************************************************
1012 ETHERNET ADDRESS TYPE
1013 ************************************************************************/
1014
1015 static int
ng_enaddr_parse(const struct ng_parse_type * type,const char * s,int * const off,const u_char * const start,u_char * const buf,int * const buflen)1016 ng_enaddr_parse(const struct ng_parse_type *type,
1017 const char *s, int *const off, const u_char *const start,
1018 u_char *const buf, int *const buflen)
1019 {
1020 char *eptr;
1021 u_long val;
1022 int i;
1023
1024 if (*buflen < ETHER_ADDR_LEN)
1025 return (ERANGE);
1026 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1027 val = strtoul(s + *off, &eptr, 16);
1028 if (val > 0xff || eptr == s + *off)
1029 return (EINVAL);
1030 buf[i] = (u_char)val;
1031 *off = (eptr - s);
1032 if (i < ETHER_ADDR_LEN - 1) {
1033 if (*eptr != ':')
1034 return (EINVAL);
1035 (*off)++;
1036 }
1037 }
1038 *buflen = ETHER_ADDR_LEN;
1039 return (0);
1040 }
1041
1042 static int
ng_enaddr_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)1043 ng_enaddr_unparse(const struct ng_parse_type *type,
1044 const u_char *data, int *off, char *cbuf, int cbuflen)
1045 {
1046 int len;
1047
1048 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1049 data[*off], data[*off + 1], data[*off + 2],
1050 data[*off + 3], data[*off + 4], data[*off + 5]);
1051 if (len >= cbuflen)
1052 return (ERANGE);
1053 *off += ETHER_ADDR_LEN;
1054 return (0);
1055 }
1056
1057 const struct ng_parse_type ng_parse_enaddr_type = {
1058 NULL,
1059 NULL,
1060 NULL,
1061 ng_enaddr_parse,
1062 ng_enaddr_unparse,
1063 NULL,
1064 0
1065 };
1066
1067 /************************************************************************
1068 BYTE ARRAY TYPE
1069 ************************************************************************/
1070
1071 /* Get the length of a byte array */
1072 static int
ng_parse_bytearray_subtype_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)1073 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1074 const u_char *start, const u_char *buf)
1075 {
1076 ng_parse_array_getLength_t *const getLength = type->private;
1077
1078 return (*getLength)(type, start, buf);
1079 }
1080
1081 /* Byte array element type is hex int8 */
1082 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1083 &ng_parse_hint8_type,
1084 &ng_parse_bytearray_subtype_getLength,
1085 NULL
1086 };
1087 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1088 &ng_parse_array_type,
1089 &ng_parse_bytearray_subtype_info
1090 };
1091
1092 static int
ng_bytearray_parse(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen)1093 ng_bytearray_parse(const struct ng_parse_type *type,
1094 const char *s, int *off, const u_char *const start,
1095 u_char *const buf, int *buflen)
1096 {
1097 char *str;
1098 int toklen;
1099 int slen;
1100
1101 /* We accept either an array of bytes or a string constant */
1102 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1103 ng_parse_array_getLength_t *const getLength = type->info;
1104 int arraylen;
1105
1106 arraylen = (*getLength)(type, start, buf);
1107 if (arraylen > *buflen) {
1108 free(str, M_NETGRAPH_PARSE);
1109 return (ERANGE);
1110 }
1111 if (slen > arraylen) {
1112 free(str, M_NETGRAPH_PARSE);
1113 return (E2BIG);
1114 }
1115 bcopy(str, buf, slen);
1116 bzero(buf + slen, arraylen - slen);
1117 free(str, M_NETGRAPH_PARSE);
1118 *off += toklen;
1119 *buflen = arraylen;
1120 return (0);
1121 } else {
1122 struct ng_parse_type subtype;
1123
1124 subtype = ng_parse_bytearray_subtype;
1125 subtype.private = __DECONST(void *, type->info);
1126 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1127 }
1128 }
1129
1130 static int
ng_bytearray_unparse(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen)1131 ng_bytearray_unparse(const struct ng_parse_type *type,
1132 const u_char *data, int *off, char *cbuf, int cbuflen)
1133 {
1134 struct ng_parse_type subtype;
1135
1136 subtype = ng_parse_bytearray_subtype;
1137 subtype.private = __DECONST(void *, type->info);
1138 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1139 }
1140
1141 static int
ng_bytearray_getDefault(const struct ng_parse_type * type,const u_char * const start,u_char * buf,int * buflen)1142 ng_bytearray_getDefault(const struct ng_parse_type *type,
1143 const u_char *const start, u_char *buf, int *buflen)
1144 {
1145 struct ng_parse_type subtype;
1146
1147 subtype = ng_parse_bytearray_subtype;
1148 subtype.private = __DECONST(void *, type->info);
1149 return ng_array_getDefault(&subtype, start, buf, buflen);
1150 }
1151
1152 const struct ng_parse_type ng_parse_bytearray_type = {
1153 NULL,
1154 NULL,
1155 NULL,
1156 ng_bytearray_parse,
1157 ng_bytearray_unparse,
1158 ng_bytearray_getDefault,
1159 NULL
1160 };
1161
1162 /************************************************************************
1163 STRUCT NG_MESG TYPE
1164 ************************************************************************/
1165
1166 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1167 static int
ng_parse_ng_mesg_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)1168 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1169 const u_char *start, const u_char *buf)
1170 {
1171 const struct ng_mesg *msg;
1172
1173 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1174 return msg->header.arglen;
1175 }
1176
1177 /* Type for the variable length data portion of a struct ng_mesg */
1178 static const struct ng_parse_type ng_msg_data_type = {
1179 &ng_parse_bytearray_type,
1180 &ng_parse_ng_mesg_getLength
1181 };
1182
1183 /* Type for the entire struct ng_mesg header with data section */
1184 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1185 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1186 const struct ng_parse_type ng_parse_ng_mesg_type = {
1187 &ng_parse_struct_type,
1188 &ng_parse_ng_mesg_type_fields,
1189 };
1190
1191 /************************************************************************
1192 COMPOSITE HELPER ROUTINES
1193 ************************************************************************/
1194
1195 /*
1196 * Convert a structure or array from ASCII to binary
1197 */
1198 static int
ng_parse_composite(const struct ng_parse_type * type,const char * s,int * off,const u_char * const start,u_char * const buf,int * buflen,const enum comptype ctype)1199 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1200 int *off, const u_char *const start, u_char *const buf, int *buflen,
1201 const enum comptype ctype)
1202 {
1203 const int num = ng_get_composite_len(type, start, buf, ctype);
1204 int nextIndex = 0; /* next implicit array index */
1205 u_int index; /* field or element index */
1206 int *foff; /* field value offsets in string */
1207 int align, len, blen, error = 0;
1208
1209 /* Initialize */
1210 if (num < 0)
1211 return (EINVAL);
1212 foff = malloc(num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1213 if (foff == NULL) {
1214 error = ENOMEM;
1215 goto done;
1216 }
1217
1218 /* Get opening brace/bracket */
1219 if (ng_parse_get_token(s, off, &len)
1220 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1221 error = EINVAL;
1222 goto done;
1223 }
1224 *off += len;
1225
1226 /* Get individual element value positions in the string */
1227 for (;;) {
1228 enum ng_parse_token tok;
1229
1230 /* Check for closing brace/bracket */
1231 tok = ng_parse_get_token(s, off, &len);
1232 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1233 *off += len;
1234 break;
1235 }
1236
1237 /* For arrays, the 'name' (ie, index) is optional, so
1238 distinguish name from values by seeing if the next
1239 token is an equals sign */
1240 if (ctype != CT_STRUCT) {
1241 u_long ul;
1242 int len2, off2;
1243 char *eptr;
1244
1245 /* If an opening brace/bracket, index is implied */
1246 if (tok == T_LBRACE || tok == T_LBRACKET) {
1247 index = nextIndex++;
1248 goto gotIndex;
1249 }
1250
1251 /* Might be an index, might be a value, either way... */
1252 if (tok != T_WORD) {
1253 error = EINVAL;
1254 goto done;
1255 }
1256
1257 /* If no equals sign follows, index is implied */
1258 off2 = *off + len;
1259 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1260 index = nextIndex++;
1261 goto gotIndex;
1262 }
1263
1264 /* Index was specified explicitly; parse it */
1265 ul = strtoul(s + *off, &eptr, 0);
1266 if (ul == ULONG_MAX || eptr - (s + *off) != len) {
1267 error = EINVAL;
1268 goto done;
1269 }
1270 index = (u_int)ul;
1271 nextIndex = index + 1;
1272 *off += len + len2;
1273 } else { /* a structure field */
1274 const struct ng_parse_struct_field *const
1275 fields = type->info;
1276
1277 /* Find the field by name (required) in field list */
1278 if (tok != T_WORD) {
1279 error = EINVAL;
1280 goto done;
1281 }
1282 for (index = 0; index < num; index++) {
1283 const struct ng_parse_struct_field *const
1284 field = &fields[index];
1285
1286 if (strncmp(&s[*off], field->name, len) == 0
1287 && field->name[len] == '\0')
1288 break;
1289 }
1290 if (index == num) {
1291 error = ENOENT;
1292 goto done;
1293 }
1294 *off += len;
1295
1296 /* Get equals sign */
1297 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1298 error = EINVAL;
1299 goto done;
1300 }
1301 *off += len;
1302 }
1303 gotIndex:
1304
1305 /* Check array index */
1306 if (index >= num) {
1307 error = E2BIG;
1308 goto done;
1309 }
1310
1311 /* Save value's position and skip over it for now */
1312 if (foff[index] != 0) {
1313 error = EALREADY; /* duplicate */
1314 goto done;
1315 }
1316 while (isspace(s[*off]))
1317 (*off)++;
1318 foff[index] = *off;
1319 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1320 goto done;
1321 *off += len;
1322 }
1323
1324 /* Now build binary structure from supplied values and defaults */
1325 for (blen = index = 0; index < num; index++) {
1326 const struct ng_parse_type *const
1327 etype = ng_get_composite_etype(type, index, ctype);
1328 int k, pad, vlen;
1329
1330 /* Zero-pad any alignment bytes */
1331 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1332 for (k = 0; k < pad; k++) {
1333 if (blen >= *buflen) {
1334 error = ERANGE;
1335 goto done;
1336 }
1337 buf[blen++] = 0;
1338 }
1339
1340 /* Get value */
1341 vlen = *buflen - blen;
1342 if (foff[index] == 0) { /* use default value */
1343 error = ng_get_composite_elem_default(type, index,
1344 start, buf + blen, &vlen, ctype);
1345 } else { /* parse given value */
1346 *off = foff[index];
1347 error = INVOKE(etype, parse)(etype,
1348 s, off, start, buf + blen, &vlen);
1349 }
1350 if (error != 0)
1351 goto done;
1352 blen += vlen;
1353 }
1354
1355 /* Make total composite structure size a multiple of its alignment */
1356 if ((align = ALIGNMENT(type)) != 0) {
1357 while (blen % align != 0) {
1358 if (blen >= *buflen) {
1359 error = ERANGE;
1360 goto done;
1361 }
1362 buf[blen++] = 0;
1363 }
1364 }
1365
1366 /* Done */
1367 *buflen = blen;
1368 done:
1369 if (foff != NULL)
1370 free(foff, M_NETGRAPH_PARSE);
1371 return (error);
1372 }
1373
1374 /*
1375 * Convert an array or structure from binary to ASCII
1376 */
1377 static int
ng_unparse_composite(const struct ng_parse_type * type,const u_char * data,int * off,char * cbuf,int cbuflen,const enum comptype ctype)1378 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1379 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1380 {
1381 const struct ng_mesg *const hdr
1382 = (const struct ng_mesg *)(data - sizeof(*hdr));
1383 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1384 const int workSize = 20 * 1024; /* XXX hard coded constant */
1385 int nextIndex = 0, didOne = 0;
1386 int error, index;
1387 u_char *workBuf;
1388
1389 /* Get workspace for checking default values */
1390 workBuf = malloc(workSize, M_NETGRAPH_PARSE, M_NOWAIT);
1391 if (workBuf == NULL)
1392 return (ENOMEM);
1393
1394 /* Opening brace/bracket */
1395 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1396 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1397 goto fail;
1398
1399 /* Do each item */
1400 for (index = 0; index < num; index++) {
1401 const struct ng_parse_type *const
1402 etype = ng_get_composite_etype(type, index, ctype);
1403
1404 /* Skip any alignment pad bytes */
1405 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1406
1407 /*
1408 * See if element is equal to its default value; skip if so.
1409 * Copy struct ng_mesg header for types that peek into it.
1410 */
1411 if (sizeof(*hdr) + *off < workSize) {
1412 int tempsize = workSize - sizeof(*hdr) - *off;
1413
1414 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1415 if (ng_get_composite_elem_default(type, index, workBuf
1416 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1417 &tempsize, ctype) == 0
1418 && bcmp(workBuf + sizeof(*hdr) + *off,
1419 data + *off, tempsize) == 0) {
1420 *off += tempsize;
1421 continue;
1422 }
1423 }
1424
1425 /* Print name= */
1426 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1427 goto fail;
1428 if (ctype != CT_STRUCT) {
1429 if (index != nextIndex) {
1430 nextIndex = index;
1431 if ((error = ng_parse_append(&cbuf,
1432 &cbuflen, "%d=", index)) != 0)
1433 goto fail;
1434 }
1435 nextIndex++;
1436 } else {
1437 const struct ng_parse_struct_field *const
1438 fields = type->info;
1439
1440 if ((error = ng_parse_append(&cbuf,
1441 &cbuflen, "%s=", fields[index].name)) != 0)
1442 goto fail;
1443 }
1444
1445 /* Print value */
1446 if ((error = INVOKE(etype, unparse)
1447 (etype, data, off, cbuf, cbuflen)) != 0) {
1448 free(workBuf, M_NETGRAPH_PARSE);
1449 return (error);
1450 }
1451 cbuflen -= strlen(cbuf);
1452 cbuf += strlen(cbuf);
1453 didOne = 1;
1454 }
1455
1456 /* Closing brace/bracket */
1457 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1458 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1459
1460 fail:
1461 /* Clean up after failure */
1462 free(workBuf, M_NETGRAPH_PARSE);
1463 return (error);
1464 }
1465
1466 /*
1467 * Generate the default value for an element of an array or structure
1468 * Returns EOPNOTSUPP if default value is unspecified.
1469 */
1470 static int
ng_get_composite_elem_default(const struct ng_parse_type * type,int index,const u_char * const start,u_char * buf,int * buflen,const enum comptype ctype)1471 ng_get_composite_elem_default(const struct ng_parse_type *type,
1472 int index, const u_char *const start, u_char *buf, int *buflen,
1473 const enum comptype ctype)
1474 {
1475 const struct ng_parse_type *etype;
1476 ng_getDefault_t *func;
1477
1478 switch (ctype) {
1479 case CT_STRUCT:
1480 break;
1481 case CT_ARRAY:
1482 {
1483 const struct ng_parse_array_info *const ai = type->info;
1484
1485 if (ai->getDefault != NULL) {
1486 return (*ai->getDefault)(type,
1487 index, start, buf, buflen);
1488 }
1489 break;
1490 }
1491 case CT_FIXEDARRAY:
1492 {
1493 const struct ng_parse_fixedarray_info *const fi = type->info;
1494
1495 if (*fi->getDefault != NULL) {
1496 return (*fi->getDefault)(type,
1497 index, start, buf, buflen);
1498 }
1499 break;
1500 }
1501 default:
1502 panic("%s", __func__);
1503 }
1504
1505 /* Default to element type default */
1506 etype = ng_get_composite_etype(type, index, ctype);
1507 func = METHOD(etype, getDefault);
1508 if (func == NULL)
1509 return (EOPNOTSUPP);
1510 return (*func)(etype, start, buf, buflen);
1511 }
1512
1513 /*
1514 * Get the number of elements in a struct, variable or fixed array.
1515 */
1516 static int
ng_get_composite_len(const struct ng_parse_type * type,const u_char * const start,const u_char * buf,const enum comptype ctype)1517 ng_get_composite_len(const struct ng_parse_type *type,
1518 const u_char *const start, const u_char *buf,
1519 const enum comptype ctype)
1520 {
1521 switch (ctype) {
1522 case CT_STRUCT:
1523 {
1524 const struct ng_parse_struct_field *const fields = type->info;
1525 int numFields = 0;
1526
1527 for (numFields = 0; ; numFields++) {
1528 const struct ng_parse_struct_field *const
1529 fi = &fields[numFields];
1530
1531 if (fi->name == NULL)
1532 break;
1533 }
1534 return (numFields);
1535 }
1536 case CT_ARRAY:
1537 {
1538 const struct ng_parse_array_info *const ai = type->info;
1539
1540 return (*ai->getLength)(type, start, buf);
1541 }
1542 case CT_FIXEDARRAY:
1543 {
1544 const struct ng_parse_fixedarray_info *const fi = type->info;
1545
1546 return fi->length;
1547 }
1548 default:
1549 panic("%s", __func__);
1550 }
1551 return (0);
1552 }
1553
1554 /*
1555 * Return the type of the index'th element of a composite structure
1556 */
1557 static const struct ng_parse_type *
ng_get_composite_etype(const struct ng_parse_type * type,int index,const enum comptype ctype)1558 ng_get_composite_etype(const struct ng_parse_type *type,
1559 int index, const enum comptype ctype)
1560 {
1561 const struct ng_parse_type *etype = NULL;
1562
1563 switch (ctype) {
1564 case CT_STRUCT:
1565 {
1566 const struct ng_parse_struct_field *const fields = type->info;
1567
1568 etype = fields[index].type;
1569 break;
1570 }
1571 case CT_ARRAY:
1572 {
1573 const struct ng_parse_array_info *const ai = type->info;
1574
1575 etype = ai->elementType;
1576 break;
1577 }
1578 case CT_FIXEDARRAY:
1579 {
1580 const struct ng_parse_fixedarray_info *const fi = type->info;
1581
1582 etype = fi->elementType;
1583 break;
1584 }
1585 default:
1586 panic("%s", __func__);
1587 }
1588 return (etype);
1589 }
1590
1591 /*
1592 * Get the number of bytes to skip to align for the next
1593 * element in a composite structure.
1594 */
1595 static int
ng_parse_get_elem_pad(const struct ng_parse_type * type,int index,enum comptype ctype,int posn)1596 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1597 int index, enum comptype ctype, int posn)
1598 {
1599 const struct ng_parse_type *const
1600 etype = ng_get_composite_etype(type, index, ctype);
1601 int align;
1602
1603 /* Get element's alignment, and possibly override */
1604 align = ALIGNMENT(etype);
1605 if (ctype == CT_STRUCT) {
1606 const struct ng_parse_struct_field *const fields = type->info;
1607
1608 if (fields[index].alignment != 0)
1609 align = fields[index].alignment;
1610 }
1611
1612 /* Return number of bytes to skip to align */
1613 return (align ? (align - (posn % align)) % align : 0);
1614 }
1615
1616 /************************************************************************
1617 PARSING HELPER ROUTINES
1618 ************************************************************************/
1619
1620 /*
1621 * Append to a fixed length string buffer.
1622 */
1623 static int
ng_parse_append(char ** cbufp,int * cbuflenp,const char * fmt,...)1624 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1625 {
1626 va_list args;
1627 int len;
1628
1629 va_start(args, fmt);
1630 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1631 va_end(args);
1632 if (len >= *cbuflenp)
1633 return ERANGE;
1634 *cbufp += len;
1635 *cbuflenp -= len;
1636
1637 return (0);
1638 }
1639
1640 /*
1641 * Skip over a value
1642 */
1643 static int
ng_parse_skip_value(const char * s,int off0,int * lenp)1644 ng_parse_skip_value(const char *s, int off0, int *lenp)
1645 {
1646 int len, nbracket, nbrace;
1647 int off = off0;
1648
1649 len = nbracket = nbrace = 0;
1650 do {
1651 switch (ng_parse_get_token(s, &off, &len)) {
1652 case T_LBRACKET:
1653 nbracket++;
1654 break;
1655 case T_LBRACE:
1656 nbrace++;
1657 break;
1658 case T_RBRACKET:
1659 if (nbracket-- == 0)
1660 return (EINVAL);
1661 break;
1662 case T_RBRACE:
1663 if (nbrace-- == 0)
1664 return (EINVAL);
1665 break;
1666 case T_EOF:
1667 return (EINVAL);
1668 default:
1669 break;
1670 }
1671 off += len;
1672 } while (nbracket > 0 || nbrace > 0);
1673 *lenp = off - off0;
1674 return (0);
1675 }
1676
1677 /*
1678 * Find the next token in the string, starting at offset *startp.
1679 * Returns the token type, with *startp pointing to the first char
1680 * and *lenp the length.
1681 */
1682 enum ng_parse_token
ng_parse_get_token(const char * s,int * startp,int * lenp)1683 ng_parse_get_token(const char *s, int *startp, int *lenp)
1684 {
1685 char *t;
1686 int i;
1687
1688 while (isspace(s[*startp]))
1689 (*startp)++;
1690 switch (s[*startp]) {
1691 case '\0':
1692 *lenp = 0;
1693 return T_EOF;
1694 case '{':
1695 *lenp = 1;
1696 return T_LBRACE;
1697 case '}':
1698 *lenp = 1;
1699 return T_RBRACE;
1700 case '[':
1701 *lenp = 1;
1702 return T_LBRACKET;
1703 case ']':
1704 *lenp = 1;
1705 return T_RBRACKET;
1706 case '=':
1707 *lenp = 1;
1708 return T_EQUALS;
1709 case '"':
1710 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1711 return T_ERROR;
1712 free(t, M_NETGRAPH_PARSE);
1713 return T_STRING;
1714 default:
1715 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1716 && s[i] != '{' && s[i] != '}' && s[i] != '['
1717 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1718 ;
1719 *lenp = i - *startp;
1720 return T_WORD;
1721 }
1722 }
1723
1724 /*
1725 * Get a string token, which must be enclosed in double quotes.
1726 * The normal C backslash escapes are recognized.
1727 */
1728 char *
ng_get_string_token(const char * s,int * startp,int * lenp,int * slenp)1729 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1730 {
1731 char *cbuf, *p;
1732 int start, off;
1733 int slen;
1734
1735 while (isspace(s[*startp]))
1736 (*startp)++;
1737 start = *startp;
1738 if (s[*startp] != '"')
1739 return (NULL);
1740 cbuf = malloc(strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1741 if (cbuf == NULL)
1742 return (NULL);
1743 strcpy(cbuf, s + start + 1);
1744 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1745 if (*p == '"') {
1746 *p = '\0';
1747 *lenp = off + 1;
1748 if (slenp != NULL)
1749 *slenp = slen;
1750 return (cbuf);
1751 } else if (p[0] == '\\' && p[1] != '\0') {
1752 int x, k;
1753 char *v;
1754
1755 strcpy(p, p + 1);
1756 v = p;
1757 switch (*p) {
1758 case 't':
1759 *v = '\t';
1760 off++;
1761 continue;
1762 case 'n':
1763 *v = '\n';
1764 off++;
1765 continue;
1766 case 'r':
1767 *v = '\r';
1768 off++;
1769 continue;
1770 case 'v':
1771 *v = '\v';
1772 off++;
1773 continue;
1774 case 'f':
1775 *v = '\f';
1776 off++;
1777 continue;
1778 case '"':
1779 *v = '"';
1780 off++;
1781 continue;
1782 case '0': case '1': case '2': case '3':
1783 case '4': case '5': case '6': case '7':
1784 for (x = k = 0;
1785 k < 3 && *v >= '0' && *v <= '7'; v++) {
1786 x = (x << 3) + (*v - '0');
1787 off++;
1788 }
1789 *--v = (char)x;
1790 break;
1791 case 'x':
1792 for (v++, x = k = 0;
1793 k < 2 && isxdigit(*v); v++) {
1794 x = (x << 4) + (isdigit(*v) ?
1795 (*v - '0') :
1796 (tolower(*v) - 'a' + 10));
1797 off++;
1798 }
1799 *--v = (char)x;
1800 break;
1801 default:
1802 continue;
1803 }
1804 strcpy(p, v);
1805 }
1806 }
1807 free(cbuf, M_NETGRAPH_PARSE);
1808 return (NULL); /* no closing quote */
1809 }
1810
1811 /*
1812 * Encode a string so it can be safely put in double quotes.
1813 * Caller must free the result. Exactly "slen" characters
1814 * are encoded.
1815 */
1816 char *
ng_encode_string(const char * raw,int slen)1817 ng_encode_string(const char *raw, int slen)
1818 {
1819 char *cbuf;
1820 int off = 0;
1821 int i;
1822
1823 cbuf = malloc(strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1824 if (cbuf == NULL)
1825 return (NULL);
1826 cbuf[off++] = '"';
1827 for (i = 0; i < slen; i++, raw++) {
1828 switch (*raw) {
1829 case '\t':
1830 cbuf[off++] = '\\';
1831 cbuf[off++] = 't';
1832 break;
1833 case '\f':
1834 cbuf[off++] = '\\';
1835 cbuf[off++] = 'f';
1836 break;
1837 case '\n':
1838 cbuf[off++] = '\\';
1839 cbuf[off++] = 'n';
1840 break;
1841 case '\r':
1842 cbuf[off++] = '\\';
1843 cbuf[off++] = 'r';
1844 break;
1845 case '\v':
1846 cbuf[off++] = '\\';
1847 cbuf[off++] = 'v';
1848 break;
1849 case '"':
1850 case '\\':
1851 cbuf[off++] = '\\';
1852 cbuf[off++] = *raw;
1853 break;
1854 default:
1855 if (*raw < 0x20 || *raw > 0x7e) {
1856 off += sprintf(cbuf + off,
1857 "\\x%02x", (u_char)*raw);
1858 break;
1859 }
1860 cbuf[off++] = *raw;
1861 break;
1862 }
1863 }
1864 cbuf[off++] = '"';
1865 cbuf[off] = '\0';
1866 return (cbuf);
1867 }
1868
1869 /************************************************************************
1870 VIRTUAL METHOD LOOKUP
1871 ************************************************************************/
1872
1873 static ng_parse_t *
ng_get_parse_method(const struct ng_parse_type * t)1874 ng_get_parse_method(const struct ng_parse_type *t)
1875 {
1876 while (t != NULL && t->parse == NULL)
1877 t = t->supertype;
1878 return (t ? t->parse : NULL);
1879 }
1880
1881 static ng_unparse_t *
ng_get_unparse_method(const struct ng_parse_type * t)1882 ng_get_unparse_method(const struct ng_parse_type *t)
1883 {
1884 while (t != NULL && t->unparse == NULL)
1885 t = t->supertype;
1886 return (t ? t->unparse : NULL);
1887 }
1888
1889 static ng_getDefault_t *
ng_get_getDefault_method(const struct ng_parse_type * t)1890 ng_get_getDefault_method(const struct ng_parse_type *t)
1891 {
1892 while (t != NULL && t->getDefault == NULL)
1893 t = t->supertype;
1894 return (t ? t->getDefault : NULL);
1895 }
1896
1897 static ng_getAlign_t *
ng_get_getAlign_method(const struct ng_parse_type * t)1898 ng_get_getAlign_method(const struct ng_parse_type *t)
1899 {
1900 while (t != NULL && t->getAlign == NULL)
1901 t = t->supertype;
1902 return (t ? t->getAlign : NULL);
1903 }
1904