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