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