6 * Copyright (c) 1999 Whistle Communications, Inc.
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.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.30 2007/06/23 00:02:20 mjacob Exp $
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
52 #include <sys/ctype.h>
53 #include <sys/stdarg.h>
55 #include <net/ethernet.h>
57 #include <netinet/in.h>
59 #include <netgraph7/ng_message.h>
60 #include <netgraph7/netgraph.h>
61 #include <netgraph7/ng_parse.h>
63 #ifdef NG_SEPARATE_MALLOC
64 MALLOC_DEFINE(M_NETGRAPH_PARSE
, "netgraph_parse", "netgraph parse info");
66 #define M_NETGRAPH_PARSE M_NETGRAPH
69 /* Compute alignment for primitive integral types */
85 #define INT8_ALIGNMENT 1
86 #define INT16_ALIGNMENT __offsetof(struct int16_temp, y)
87 #define INT32_ALIGNMENT __offsetof(struct int32_temp, y)
88 #define INT64_ALIGNMENT __offsetof(struct int64_temp, y)
90 /* Output format for integral types */
91 #define INT_UNSIGNED 0
95 /* Type of composite object: struct, array, or fixedarray */
102 /* Composite types helper functions */
103 static int ng_parse_composite(const struct ng_parse_type
*type
,
104 const char *s
, int *off
, const u_char
*start
,
105 u_char
*const buf
, int *buflen
, enum comptype ctype
);
106 static int ng_unparse_composite(const struct ng_parse_type
*type
,
107 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
,
108 enum comptype ctype
);
109 static int ng_get_composite_elem_default(const struct ng_parse_type
*type
,
110 int index
, const u_char
*start
, u_char
*buf
,
111 int *buflen
, enum comptype ctype
);
112 static int ng_get_composite_len(const struct ng_parse_type
*type
,
113 const u_char
*start
, const u_char
*buf
,
114 enum comptype ctype
);
115 static const struct ng_parse_type
*ng_get_composite_etype(const struct
116 ng_parse_type
*type
, int index
, enum comptype ctype
);
117 static int ng_parse_get_elem_pad(const struct ng_parse_type
*type
,
118 int index
, enum comptype ctype
, int posn
);
120 /* Parsing helper functions */
121 static int ng_parse_skip_value(const char *s
, int off
, int *lenp
);
122 static int ng_parse_append(char **cbufp
, int *cbuflenp
,
123 const char *fmt
, ...) __printflike(3, 4);
125 /* Poor man's virtual method calls */
126 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
127 #define INVOKE(t,m) (*METHOD(t,m))
129 static ng_parse_t
*ng_get_parse_method(const struct ng_parse_type
*t
);
130 static ng_unparse_t
*ng_get_unparse_method(const struct ng_parse_type
*t
);
131 static ng_getDefault_t
*ng_get_getDefault_method(const
132 struct ng_parse_type
*t
);
133 static ng_getAlign_t
*ng_get_getAlign_method(const struct ng_parse_type
*t
);
135 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
136 0 : INVOKE(t, getAlign)(t))
138 /************************************************************************
140 ************************************************************************/
143 * Convert an ASCII string to binary according to the supplied type descriptor
146 ng_parse(const struct ng_parse_type
*type
,
147 const char *string
, int *off
, u_char
*buf
, int *buflen
)
149 return INVOKE(type
, parse
)(type
, string
, off
, buf
, buf
, buflen
);
153 * Convert binary to an ASCII string according to the supplied type descriptor
156 ng_unparse(const struct ng_parse_type
*type
,
157 const u_char
*data
, char *cbuf
, int cbuflen
)
161 return INVOKE(type
, unparse
)(type
, data
, &off
, cbuf
, cbuflen
);
165 * Fill in the default value according to the supplied type descriptor
168 ng_parse_getDefault(const struct ng_parse_type
*type
, u_char
*buf
, int *buflen
)
170 ng_getDefault_t
*const func
= METHOD(type
, getDefault
);
174 return (*func
)(type
, buf
, buf
, buflen
);
178 /************************************************************************
180 ************************************************************************/
183 ng_struct_parse(const struct ng_parse_type
*type
,
184 const char *s
, int *off
, const u_char
*const start
,
185 u_char
*const buf
, int *buflen
)
187 return ng_parse_composite(type
, s
, off
, start
, buf
, buflen
, CT_STRUCT
);
191 ng_struct_unparse(const struct ng_parse_type
*type
,
192 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
194 return ng_unparse_composite(type
, data
, off
, cbuf
, cbuflen
, CT_STRUCT
);
198 ng_struct_getDefault(const struct ng_parse_type
*type
,
199 const u_char
*const start
, u_char
*buf
, int *buflen
)
203 return ng_parse_composite(type
,
204 "{}", &off
, start
, buf
, buflen
, CT_STRUCT
);
208 ng_struct_getAlign(const struct ng_parse_type
*type
)
210 const struct ng_parse_struct_field
*field
;
213 for (field
= type
->info
; field
->name
!= NULL
; field
++) {
214 int falign
= ALIGNMENT(field
->type
);
222 const struct ng_parse_type ng_parse_struct_type
= {
228 ng_struct_getDefault
,
232 /************************************************************************
233 FIXED LENGTH ARRAY TYPE
234 ************************************************************************/
237 ng_fixedarray_parse(const struct ng_parse_type
*type
,
238 const char *s
, int *off
, const u_char
*const start
,
239 u_char
*const buf
, int *buflen
)
241 return ng_parse_composite(type
,
242 s
, off
, start
, buf
, buflen
, CT_FIXEDARRAY
);
246 ng_fixedarray_unparse(const struct ng_parse_type
*type
,
247 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
249 return ng_unparse_composite(type
,
250 data
, off
, cbuf
, cbuflen
, CT_FIXEDARRAY
);
254 ng_fixedarray_getDefault(const struct ng_parse_type
*type
,
255 const u_char
*const start
, u_char
*buf
, int *buflen
)
259 return ng_parse_composite(type
,
260 "[]", &off
, start
, buf
, buflen
, CT_FIXEDARRAY
);
264 ng_fixedarray_getAlign(const struct ng_parse_type
*type
)
266 const struct ng_parse_fixedarray_info
*fi
= type
->info
;
268 return ALIGNMENT(fi
->elementType
);
271 const struct ng_parse_type ng_parse_fixedarray_type
= {
276 ng_fixedarray_unparse
,
277 ng_fixedarray_getDefault
,
278 ng_fixedarray_getAlign
281 /************************************************************************
282 VARIABLE LENGTH ARRAY TYPE
283 ************************************************************************/
286 ng_array_parse(const struct ng_parse_type
*type
,
287 const char *s
, int *off
, const u_char
*const start
,
288 u_char
*const buf
, int *buflen
)
290 return ng_parse_composite(type
, s
, off
, start
, buf
, buflen
, CT_ARRAY
);
294 ng_array_unparse(const struct ng_parse_type
*type
,
295 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
297 return ng_unparse_composite(type
, data
, off
, cbuf
, cbuflen
, CT_ARRAY
);
301 ng_array_getDefault(const struct ng_parse_type
*type
,
302 const u_char
*const start
, u_char
*buf
, int *buflen
)
306 return ng_parse_composite(type
,
307 "[]", &off
, start
, buf
, buflen
, CT_ARRAY
);
311 ng_array_getAlign(const struct ng_parse_type
*type
)
313 const struct ng_parse_array_info
*ai
= type
->info
;
315 return ALIGNMENT(ai
->elementType
);
318 const struct ng_parse_type ng_parse_array_type
= {
328 /************************************************************************
330 ************************************************************************/
333 ng_int8_parse(const struct ng_parse_type
*type
,
334 const char *s
, int *off
, const u_char
*const start
,
335 u_char
*const buf
, int *buflen
)
341 val
= strtol(s
+ *off
, &eptr
, 0);
342 if (val
< (int8_t)0x80 || val
> (u_int8_t
)0xff || eptr
== s
+ *off
)
346 bcopy(&val8
, buf
, sizeof(int8_t));
347 *buflen
= sizeof(int8_t);
352 ng_int8_unparse(const struct ng_parse_type
*type
,
353 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
360 bcopy(data
+ *off
, &val
, sizeof(int8_t));
361 switch ((intptr_t)type
->info
) {
368 fval
= (u_int8_t
)val
;
372 fval
= (u_int8_t
)val
;
375 panic("%s: unknown type", __func__
);
376 #ifdef RESTARTABLE_PANICS
380 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, fmt
, fval
)) != 0)
382 *off
+= sizeof(int8_t);
387 ng_int8_getDefault(const struct ng_parse_type
*type
,
388 const u_char
*const start
, u_char
*buf
, int *buflen
)
392 if (*buflen
< sizeof(int8_t))
395 bcopy(&val
, buf
, sizeof(int8_t));
396 *buflen
= sizeof(int8_t);
401 ng_int8_getAlign(const struct ng_parse_type
*type
)
403 return INT8_ALIGNMENT
;
406 const struct ng_parse_type ng_parse_int8_type
= {
416 const struct ng_parse_type ng_parse_uint8_type
= {
421 const struct ng_parse_type ng_parse_hint8_type
= {
426 /************************************************************************
428 ************************************************************************/
431 ng_int16_parse(const struct ng_parse_type
*type
,
432 const char *s
, int *off
, const u_char
*const start
,
433 u_char
*const buf
, int *buflen
)
439 val
= strtol(s
+ *off
, &eptr
, 0);
440 if (val
< (int16_t)0x8000
441 || val
> (u_int16_t
)0xffff || eptr
== s
+ *off
)
444 val16
= (int16_t)val
;
445 bcopy(&val16
, buf
, sizeof(int16_t));
446 *buflen
= sizeof(int16_t);
451 ng_int16_unparse(const struct ng_parse_type
*type
,
452 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
459 bcopy(data
+ *off
, &val
, sizeof(int16_t));
460 switch ((intptr_t)type
->info
) {
467 fval
= (u_int16_t
)val
;
471 fval
= (u_int16_t
)val
;
474 panic("%s: unknown type", __func__
);
475 #ifdef RESTARTABLE_PANICS
479 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, fmt
, fval
)) != 0)
481 *off
+= sizeof(int16_t);
486 ng_int16_getDefault(const struct ng_parse_type
*type
,
487 const u_char
*const start
, u_char
*buf
, int *buflen
)
491 if (*buflen
< sizeof(int16_t))
494 bcopy(&val
, buf
, sizeof(int16_t));
495 *buflen
= sizeof(int16_t);
500 ng_int16_getAlign(const struct ng_parse_type
*type
)
502 return INT16_ALIGNMENT
;
505 const struct ng_parse_type ng_parse_int16_type
= {
515 const struct ng_parse_type ng_parse_uint16_type
= {
516 &ng_parse_int16_type
,
520 const struct ng_parse_type ng_parse_hint16_type
= {
521 &ng_parse_int16_type
,
525 /************************************************************************
527 ************************************************************************/
530 ng_int32_parse(const struct ng_parse_type
*type
,
531 const char *s
, int *off
, const u_char
*const start
,
532 u_char
*const buf
, int *buflen
)
534 long val
; /* assumes long is at least 32 bits */
538 if ((intptr_t)type
->info
== INT_SIGNED
)
539 val
= strtol(s
+ *off
, &eptr
, 0);
541 val
= strtoul(s
+ *off
, &eptr
, 0);
542 if (val
< (int32_t)0x80000000
543 || val
> (u_int32_t
)0xffffffff || eptr
== s
+ *off
)
546 val32
= (int32_t)val
;
547 bcopy(&val32
, buf
, sizeof(int32_t));
548 *buflen
= sizeof(int32_t);
553 ng_int32_unparse(const struct ng_parse_type
*type
,
554 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
561 bcopy(data
+ *off
, &val
, sizeof(int32_t));
562 switch ((intptr_t)type
->info
) {
569 fval
= (u_int32_t
)val
;
573 fval
= (u_int32_t
)val
;
576 panic("%s: unknown type", __func__
);
577 #ifdef RESTARTABLE_PANICS
581 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, fmt
, fval
)) != 0)
583 *off
+= sizeof(int32_t);
588 ng_int32_getDefault(const struct ng_parse_type
*type
,
589 const u_char
*const start
, u_char
*buf
, int *buflen
)
593 if (*buflen
< sizeof(int32_t))
596 bcopy(&val
, buf
, sizeof(int32_t));
597 *buflen
= sizeof(int32_t);
602 ng_int32_getAlign(const struct ng_parse_type
*type
)
604 return INT32_ALIGNMENT
;
607 const struct ng_parse_type ng_parse_int32_type
= {
617 const struct ng_parse_type ng_parse_uint32_type
= {
618 &ng_parse_int32_type
,
622 const struct ng_parse_type ng_parse_hint32_type
= {
623 &ng_parse_int32_type
,
627 /************************************************************************
629 ************************************************************************/
632 ng_int64_parse(const struct ng_parse_type
*type
,
633 const char *s
, int *off
, const u_char
*const start
,
634 u_char
*const buf
, int *buflen
)
640 val
= strtoq(s
+ *off
, &eptr
, 0);
641 if (eptr
== s
+ *off
)
644 val64
= (int64_t)val
;
645 bcopy(&val64
, buf
, sizeof(int64_t));
646 *buflen
= sizeof(int64_t);
651 ng_int64_unparse(const struct ng_parse_type
*type
,
652 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
659 bcopy(data
+ *off
, &val
, sizeof(int64_t));
660 switch ((intptr_t)type
->info
) {
667 fval
= (u_int64_t
)val
;
671 fval
= (u_int64_t
)val
;
674 panic("%s: unknown type", __func__
);
675 #ifdef RESTARTABLE_PANICS
679 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, fmt
, fval
)) != 0)
681 *off
+= sizeof(int64_t);
686 ng_int64_getDefault(const struct ng_parse_type
*type
,
687 const u_char
*const start
, u_char
*buf
, int *buflen
)
691 if (*buflen
< sizeof(int64_t))
694 bcopy(&val
, buf
, sizeof(int64_t));
695 *buflen
= sizeof(int64_t);
700 ng_int64_getAlign(const struct ng_parse_type
*type
)
702 return INT64_ALIGNMENT
;
705 const struct ng_parse_type ng_parse_int64_type
= {
715 const struct ng_parse_type ng_parse_uint64_type
= {
716 &ng_parse_int64_type
,
720 const struct ng_parse_type ng_parse_hint64_type
= {
721 &ng_parse_int64_type
,
725 /************************************************************************
727 ************************************************************************/
730 ng_string_parse(const struct ng_parse_type
*type
,
731 const char *s
, int *off
, const u_char
*const start
,
732 u_char
*const buf
, int *buflen
)
738 if ((sval
= ng_get_string_token(s
, off
, &len
, &slen
)) == NULL
)
741 bcopy(sval
, buf
, slen
+ 1);
742 kfree(sval
, M_NETGRAPH_PARSE
);
748 ng_string_unparse(const struct ng_parse_type
*type
,
749 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
751 const char *const raw
= (const char *)data
+ *off
;
752 char *const s
= ng_encode_string(raw
, strlen(raw
));
757 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, "%s", s
)) != 0) {
758 kfree(s
, M_NETGRAPH_PARSE
);
761 *off
+= strlen(raw
) + 1;
762 kfree(s
, M_NETGRAPH_PARSE
);
767 ng_string_getDefault(const struct ng_parse_type
*type
,
768 const u_char
*const start
, u_char
*buf
, int *buflen
)
773 buf
[0] = (u_char
)'\0';
778 const struct ng_parse_type ng_parse_string_type
= {
784 ng_string_getDefault
,
788 /************************************************************************
789 FIXED BUFFER STRING TYPE
790 ************************************************************************/
793 ng_fixedstring_parse(const struct ng_parse_type
*type
,
794 const char *s
, int *off
, const u_char
*const start
,
795 u_char
*const buf
, int *buflen
)
797 const struct ng_parse_fixedstring_info
*const fi
= type
->info
;
802 if ((sval
= ng_get_string_token(s
, off
, &len
, &slen
)) == NULL
)
804 if (slen
+ 1 > fi
->bufSize
) {
805 kfree(sval
, M_NETGRAPH_PARSE
);
809 bcopy(sval
, buf
, slen
);
810 kfree(sval
, M_NETGRAPH_PARSE
);
811 bzero(buf
+ slen
, fi
->bufSize
- slen
);
812 *buflen
= fi
->bufSize
;
817 ng_fixedstring_unparse(const struct ng_parse_type
*type
,
818 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
820 const struct ng_parse_fixedstring_info
*const fi
= type
->info
;
821 int error
, temp
= *off
;
823 if ((error
= ng_string_unparse(type
, data
, &temp
, cbuf
, cbuflen
)) != 0)
830 ng_fixedstring_getDefault(const struct ng_parse_type
*type
,
831 const u_char
*const start
, u_char
*buf
, int *buflen
)
833 const struct ng_parse_fixedstring_info
*const fi
= type
->info
;
835 if (*buflen
< fi
->bufSize
)
837 bzero(buf
, fi
->bufSize
);
838 *buflen
= fi
->bufSize
;
842 const struct ng_parse_type ng_parse_fixedstring_type
= {
846 ng_fixedstring_parse
,
847 ng_fixedstring_unparse
,
848 ng_fixedstring_getDefault
,
852 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info
= {
855 const struct ng_parse_type ng_parse_nodebuf_type
= {
856 &ng_parse_fixedstring_type
,
857 &ng_parse_nodebuf_info
860 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info
= {
863 const struct ng_parse_type ng_parse_hookbuf_type
= {
864 &ng_parse_fixedstring_type
,
865 &ng_parse_hookbuf_info
868 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info
= {
871 const struct ng_parse_type ng_parse_pathbuf_type
= {
872 &ng_parse_fixedstring_type
,
873 &ng_parse_pathbuf_info
876 const struct ng_parse_fixedstring_info ng_parse_typebuf_info
= {
879 const struct ng_parse_type ng_parse_typebuf_type
= {
880 &ng_parse_fixedstring_type
,
881 &ng_parse_typebuf_info
884 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info
= {
887 const struct ng_parse_type ng_parse_cmdbuf_type
= {
888 &ng_parse_fixedstring_type
,
889 &ng_parse_cmdbuf_info
892 /************************************************************************
893 EXPLICITLY SIZED STRING TYPE
894 ************************************************************************/
897 ng_sizedstring_parse(const struct ng_parse_type
*type
,
898 const char *s
, int *off
, const u_char
*const start
,
899 u_char
*const buf
, int *buflen
)
905 if ((sval
= ng_get_string_token(s
, off
, &len
, &slen
)) == NULL
)
907 if (slen
> USHRT_MAX
) {
908 kfree(sval
, M_NETGRAPH_PARSE
);
912 *((u_int16_t
*)buf
) = (u_int16_t
)slen
;
913 bcopy(sval
, buf
+ 2, slen
);
914 kfree(sval
, M_NETGRAPH_PARSE
);
920 ng_sizedstring_unparse(const struct ng_parse_type
*type
,
921 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
923 const char *const raw
= (const char *)data
+ *off
+ 2;
924 const int slen
= *((const u_int16_t
*)(data
+ *off
));
925 char *const s
= ng_encode_string(raw
, slen
);
930 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, "%s", s
)) != 0) {
931 kfree(s
, M_NETGRAPH_PARSE
);
934 kfree(s
, M_NETGRAPH_PARSE
);
940 ng_sizedstring_getDefault(const struct ng_parse_type
*type
,
941 const u_char
*const start
, u_char
*buf
, int *buflen
)
950 const struct ng_parse_type ng_parse_sizedstring_type
= {
954 ng_sizedstring_parse
,
955 ng_sizedstring_unparse
,
956 ng_sizedstring_getDefault
,
960 /************************************************************************
962 ************************************************************************/
965 ng_ipaddr_parse(const struct ng_parse_type
*type
,
966 const char *s
, int *off
, const u_char
*const start
,
967 u_char
*const buf
, int *buflen
)
971 for (i
= 0; i
< 4; i
++) {
972 if ((error
= ng_int8_parse(&ng_parse_int8_type
,
973 s
, off
, start
, buf
+ i
, buflen
)) != 0)
975 if (i
< 3 && s
[*off
] != '.')
984 ng_ipaddr_unparse(const struct ng_parse_type
*type
,
985 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
990 bcopy(data
+ *off
, &ip
, sizeof(ip
));
991 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, "%d.%d.%d.%d",
992 ((u_char
*)&ip
)[0], ((u_char
*)&ip
)[1],
993 ((u_char
*)&ip
)[2], ((u_char
*)&ip
)[3])) != 0)
1000 ng_ipaddr_getDefault(const struct ng_parse_type
*type
,
1001 const u_char
*const start
, u_char
*buf
, int *buflen
)
1003 struct in_addr ip
= { 0 };
1005 if (*buflen
< sizeof(ip
))
1007 bcopy(&ip
, buf
, sizeof(ip
));
1008 *buflen
= sizeof(ip
);
1012 const struct ng_parse_type ng_parse_ipaddr_type
= {
1018 ng_ipaddr_getDefault
,
1022 /************************************************************************
1023 ETHERNET ADDRESS TYPE
1024 ************************************************************************/
1027 ng_enaddr_parse(const struct ng_parse_type
*type
,
1028 const char *s
, int *const off
, const u_char
*const start
,
1029 u_char
*const buf
, int *const buflen
)
1035 if (*buflen
< ETHER_ADDR_LEN
)
1037 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++) {
1038 val
= strtoul(s
+ *off
, &eptr
, 16);
1039 if (val
> 0xff || eptr
== s
+ *off
)
1041 buf
[i
] = (u_char
)val
;
1043 if (i
< ETHER_ADDR_LEN
- 1) {
1049 *buflen
= ETHER_ADDR_LEN
;
1054 ng_enaddr_unparse(const struct ng_parse_type
*type
,
1055 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
1059 len
= ksnprintf(cbuf
, cbuflen
, "%02x:%02x:%02x:%02x:%02x:%02x",
1060 data
[*off
], data
[*off
+ 1], data
[*off
+ 2],
1061 data
[*off
+ 3], data
[*off
+ 4], data
[*off
+ 5]);
1064 *off
+= ETHER_ADDR_LEN
;
1068 const struct ng_parse_type ng_parse_enaddr_type
= {
1078 /************************************************************************
1080 ************************************************************************/
1082 /* Get the length of a byte array */
1084 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type
*type
,
1085 const u_char
*start
, const u_char
*buf
)
1087 ng_parse_array_getLength_t
*const getLength
= type
->private;
1089 return (*getLength
)(type
, start
, buf
);
1092 /* Byte array element type is hex int8 */
1093 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info
= {
1094 &ng_parse_hint8_type
,
1095 &ng_parse_bytearray_subtype_getLength
,
1098 static const struct ng_parse_type ng_parse_bytearray_subtype
= {
1099 &ng_parse_array_type
,
1100 &ng_parse_bytearray_subtype_info
1104 ng_bytearray_parse(const struct ng_parse_type
*type
,
1105 const char *s
, int *off
, const u_char
*const start
,
1106 u_char
*const buf
, int *buflen
)
1112 /* We accept either an array of bytes or a string constant */
1113 if ((str
= ng_get_string_token(s
, off
, &toklen
, &slen
)) != NULL
) {
1114 ng_parse_array_getLength_t
*const getLength
= type
->info
;
1117 arraylen
= (*getLength
)(type
, start
, buf
);
1118 if (arraylen
> *buflen
) {
1119 kfree(str
, M_NETGRAPH_PARSE
);
1122 if (slen
> arraylen
) {
1123 kfree(str
, M_NETGRAPH_PARSE
);
1126 bcopy(str
, buf
, slen
);
1127 bzero(buf
+ slen
, arraylen
- slen
);
1128 kfree(str
, M_NETGRAPH_PARSE
);
1133 struct ng_parse_type subtype
;
1135 subtype
= ng_parse_bytearray_subtype
;
1136 *(const void **)(void *)&subtype
.private = type
->info
;
1137 return ng_array_parse(&subtype
, s
, off
, start
, buf
, buflen
);
1142 ng_bytearray_unparse(const struct ng_parse_type
*type
,
1143 const u_char
*data
, int *off
, char *cbuf
, int cbuflen
)
1145 struct ng_parse_type subtype
;
1147 subtype
= ng_parse_bytearray_subtype
;
1148 *(const void **)(void *)&subtype
.private = type
->info
;
1149 return ng_array_unparse(&subtype
, data
, off
, cbuf
, cbuflen
);
1153 ng_bytearray_getDefault(const struct ng_parse_type
*type
,
1154 const u_char
*const start
, u_char
*buf
, int *buflen
)
1156 struct ng_parse_type subtype
;
1158 subtype
= ng_parse_bytearray_subtype
;
1159 *(const void **)(void *)&subtype
.private = type
->info
;
1160 return ng_array_getDefault(&subtype
, start
, buf
, buflen
);
1163 const struct ng_parse_type ng_parse_bytearray_type
= {
1168 ng_bytearray_unparse
,
1169 ng_bytearray_getDefault
,
1173 /************************************************************************
1175 ************************************************************************/
1177 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1179 ng_parse_ng_mesg_getLength(const struct ng_parse_type
*type
,
1180 const u_char
*start
, const u_char
*buf
)
1182 const struct ng_mesg
*msg
;
1184 msg
= (const struct ng_mesg
*)(buf
- sizeof(*msg
));
1185 return msg
->header
.arglen
;
1188 /* Type for the variable length data portion of a struct ng_mesg */
1189 static const struct ng_parse_type ng_msg_data_type
= {
1190 &ng_parse_bytearray_type
,
1191 &ng_parse_ng_mesg_getLength
1194 /* Type for the entire struct ng_mesg header with data section */
1195 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields
[]
1196 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type
);
1197 const struct ng_parse_type ng_parse_ng_mesg_type
= {
1198 &ng_parse_struct_type
,
1199 &ng_parse_ng_mesg_type_fields
,
1202 /************************************************************************
1203 COMPOSITE HELPER ROUTINES
1204 ************************************************************************/
1207 * Convert a structure or array from ASCII to binary
1210 ng_parse_composite(const struct ng_parse_type
*type
, const char *s
,
1211 int *off
, const u_char
*const start
, u_char
*const buf
, int *buflen
,
1212 const enum comptype ctype
)
1214 const int num
= ng_get_composite_len(type
, start
, buf
, ctype
);
1215 int nextIndex
= 0; /* next implicit array index */
1216 u_int index
; /* field or element index */
1217 int *foff
; /* field value offsets in string */
1218 int align
, len
, blen
, error
= 0;
1221 foff
= kmalloc(num
* sizeof(*foff
), M_NETGRAPH_PARSE
,
1222 M_WAITOK
| M_NULLOK
| M_ZERO
);
1228 /* Get opening brace/bracket */
1229 if (ng_parse_get_token(s
, off
, &len
)
1230 != (ctype
== CT_STRUCT
? T_LBRACE
: T_LBRACKET
)) {
1236 /* Get individual element value positions in the string */
1238 enum ng_parse_token tok
;
1240 /* Check for closing brace/bracket */
1241 tok
= ng_parse_get_token(s
, off
, &len
);
1242 if (tok
== (ctype
== CT_STRUCT
? T_RBRACE
: T_RBRACKET
)) {
1247 /* For arrays, the 'name' (ie, index) is optional, so
1248 distinguish name from values by seeing if the next
1249 token is an equals sign */
1250 if (ctype
!= CT_STRUCT
) {
1254 /* If an opening brace/bracket, index is implied */
1255 if (tok
== T_LBRACE
|| tok
== T_LBRACKET
) {
1256 index
= nextIndex
++;
1260 /* Might be an index, might be a value, either way... */
1261 if (tok
!= T_WORD
) {
1266 /* If no equals sign follows, index is implied */
1268 if (ng_parse_get_token(s
, &off2
, &len2
) != T_EQUALS
) {
1269 index
= nextIndex
++;
1273 /* Index was specified explicitly; parse it */
1274 index
= (u_int
)strtoul(s
+ *off
, &eptr
, 0);
1275 if (index
< 0 || eptr
- (s
+ *off
) != len
) {
1279 nextIndex
= index
+ 1;
1281 } else { /* a structure field */
1282 const struct ng_parse_struct_field
*const
1283 fields
= type
->info
;
1285 /* Find the field by name (required) in field list */
1286 if (tok
!= T_WORD
) {
1290 for (index
= 0; index
< num
; index
++) {
1291 const struct ng_parse_struct_field
*const
1292 field
= &fields
[index
];
1294 if (strncmp(&s
[*off
], field
->name
, len
) == 0
1295 && field
->name
[len
] == '\0')
1304 /* Get equals sign */
1305 if (ng_parse_get_token(s
, off
, &len
) != T_EQUALS
) {
1313 /* Check array index */
1319 /* Save value's position and skip over it for now */
1320 if (foff
[index
] != 0) {
1321 error
= EALREADY
; /* duplicate */
1324 while (isspace(s
[*off
]))
1327 if ((error
= ng_parse_skip_value(s
, *off
, &len
)) != 0)
1332 /* Now build binary structure from supplied values and defaults */
1333 for (blen
= index
= 0; index
< num
; index
++) {
1334 const struct ng_parse_type
*const
1335 etype
= ng_get_composite_etype(type
, index
, ctype
);
1338 /* Zero-pad any alignment bytes */
1339 pad
= ng_parse_get_elem_pad(type
, index
, ctype
, blen
);
1340 for (k
= 0; k
< pad
; k
++) {
1341 if (blen
>= *buflen
) {
1349 vlen
= *buflen
- blen
;
1350 if (foff
[index
] == 0) { /* use default value */
1351 error
= ng_get_composite_elem_default(type
, index
,
1352 start
, buf
+ blen
, &vlen
, ctype
);
1353 } else { /* parse given value */
1355 error
= INVOKE(etype
, parse
)(etype
,
1356 s
, off
, start
, buf
+ blen
, &vlen
);
1363 /* Make total composite structure size a multiple of its alignment */
1364 if ((align
= ALIGNMENT(type
)) != 0) {
1365 while (blen
% align
!= 0) {
1366 if (blen
>= *buflen
) {
1378 kfree(foff
, M_NETGRAPH_PARSE
);
1383 * Convert an array or structure from binary to ASCII
1386 ng_unparse_composite(const struct ng_parse_type
*type
, const u_char
*data
,
1387 int *off
, char *cbuf
, int cbuflen
, const enum comptype ctype
)
1389 const struct ng_mesg
*const hdr
1390 = (const struct ng_mesg
*)(data
- sizeof(*hdr
));
1391 const int num
= ng_get_composite_len(type
, data
, data
+ *off
, ctype
);
1392 const int workSize
= 20 * 1024; /* XXX hard coded constant */
1393 int nextIndex
= 0, didOne
= 0;
1397 /* Get workspace for checking default values */
1398 workBuf
= kmalloc(workSize
, M_NETGRAPH_PARSE
, M_WAITOK
| M_NULLOK
);
1399 if (workBuf
== NULL
)
1402 /* Opening brace/bracket */
1403 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, "%c",
1404 (ctype
== CT_STRUCT
) ? '{' : '[')) != 0)
1408 for (index
= 0; index
< num
; index
++) {
1409 const struct ng_parse_type
*const
1410 etype
= ng_get_composite_etype(type
, index
, ctype
);
1412 /* Skip any alignment pad bytes */
1413 *off
+= ng_parse_get_elem_pad(type
, index
, ctype
, *off
);
1416 * See if element is equal to its default value; skip if so.
1417 * Copy struct ng_mesg header for types that peek into it.
1419 if (sizeof(*hdr
) + *off
< workSize
) {
1420 int tempsize
= workSize
- sizeof(*hdr
) - *off
;
1422 bcopy(hdr
, workBuf
, sizeof(*hdr
) + *off
);
1423 if (ng_get_composite_elem_default(type
, index
, workBuf
1424 + sizeof(*hdr
), workBuf
+ sizeof(*hdr
) + *off
,
1425 &tempsize
, ctype
) == 0
1426 && bcmp(workBuf
+ sizeof(*hdr
) + *off
,
1427 data
+ *off
, tempsize
) == 0) {
1434 if ((error
= ng_parse_append(&cbuf
, &cbuflen
, " ")) != 0)
1436 if (ctype
!= CT_STRUCT
) {
1437 if (index
!= nextIndex
) {
1439 if ((error
= ng_parse_append(&cbuf
,
1440 &cbuflen
, "%d=", index
)) != 0)
1445 const struct ng_parse_struct_field
*const
1446 fields
= type
->info
;
1448 if ((error
= ng_parse_append(&cbuf
,
1449 &cbuflen
, "%s=", fields
[index
].name
)) != 0)
1454 if ((error
= INVOKE(etype
, unparse
)
1455 (etype
, data
, off
, cbuf
, cbuflen
)) != 0) {
1456 kfree(workBuf
, M_NETGRAPH_PARSE
);
1459 cbuflen
-= strlen(cbuf
);
1460 cbuf
+= strlen(cbuf
);
1464 /* Closing brace/bracket */
1465 error
= ng_parse_append(&cbuf
, &cbuflen
, "%s%c",
1466 didOne
? " " : "", (ctype
== CT_STRUCT
) ? '}' : ']');
1469 /* Clean up after failure */
1470 kfree(workBuf
, M_NETGRAPH_PARSE
);
1475 * Generate the default value for an element of an array or structure
1476 * Returns EOPNOTSUPP if default value is unspecified.
1479 ng_get_composite_elem_default(const struct ng_parse_type
*type
,
1480 int index
, const u_char
*const start
, u_char
*buf
, int *buflen
,
1481 const enum comptype ctype
)
1483 const struct ng_parse_type
*etype
;
1484 ng_getDefault_t
*func
;
1491 const struct ng_parse_array_info
*const ai
= type
->info
;
1493 if (ai
->getDefault
!= NULL
) {
1494 return (*ai
->getDefault
)(type
,
1495 index
, start
, buf
, buflen
);
1501 const struct ng_parse_fixedarray_info
*const fi
= type
->info
;
1503 if (*fi
->getDefault
!= NULL
) {
1504 return (*fi
->getDefault
)(type
,
1505 index
, start
, buf
, buflen
);
1510 panic("%s", __func__
);
1513 /* Default to element type default */
1514 etype
= ng_get_composite_etype(type
, index
, ctype
);
1515 func
= METHOD(etype
, getDefault
);
1517 return (EOPNOTSUPP
);
1518 return (*func
)(etype
, start
, buf
, buflen
);
1522 * Get the number of elements in a struct, variable or fixed array.
1525 ng_get_composite_len(const struct ng_parse_type
*type
,
1526 const u_char
*const start
, const u_char
*buf
,
1527 const enum comptype ctype
)
1532 const struct ng_parse_struct_field
*const fields
= type
->info
;
1535 for (numFields
= 0; ; numFields
++) {
1536 const struct ng_parse_struct_field
*const
1537 fi
= &fields
[numFields
];
1539 if (fi
->name
== NULL
)
1546 const struct ng_parse_array_info
*const ai
= type
->info
;
1548 return (*ai
->getLength
)(type
, start
, buf
);
1552 const struct ng_parse_fixedarray_info
*const fi
= type
->info
;
1557 panic("%s", __func__
);
1563 * Return the type of the index'th element of a composite structure
1565 static const struct ng_parse_type
*
1566 ng_get_composite_etype(const struct ng_parse_type
*type
,
1567 int index
, const enum comptype ctype
)
1569 const struct ng_parse_type
*etype
= NULL
;
1574 const struct ng_parse_struct_field
*const fields
= type
->info
;
1576 etype
= fields
[index
].type
;
1581 const struct ng_parse_array_info
*const ai
= type
->info
;
1583 etype
= ai
->elementType
;
1588 const struct ng_parse_fixedarray_info
*const fi
= type
->info
;
1590 etype
= fi
->elementType
;
1594 panic("%s", __func__
);
1600 * Get the number of bytes to skip to align for the next
1601 * element in a composite structure.
1604 ng_parse_get_elem_pad(const struct ng_parse_type
*type
,
1605 int index
, enum comptype ctype
, int posn
)
1607 const struct ng_parse_type
*const
1608 etype
= ng_get_composite_etype(type
, index
, ctype
);
1611 /* Get element's alignment, and possibly override */
1612 align
= ALIGNMENT(etype
);
1613 if (ctype
== CT_STRUCT
) {
1614 const struct ng_parse_struct_field
*const fields
= type
->info
;
1616 if (fields
[index
].alignment
!= 0)
1617 align
= fields
[index
].alignment
;
1620 /* Return number of bytes to skip to align */
1621 return (align
? (align
- (posn
% align
)) % align
: 0);
1624 /************************************************************************
1625 PARSING HELPER ROUTINES
1626 ************************************************************************/
1629 * Append to a fixed length string buffer.
1632 ng_parse_append(char **cbufp
, int *cbuflenp
, const char *fmt
, ...)
1637 va_start(args
, fmt
);
1638 len
= kvsnprintf(*cbufp
, *cbuflenp
, fmt
, args
);
1640 if (len
>= *cbuflenp
)
1652 ng_parse_skip_value(const char *s
, int off0
, int *lenp
)
1654 int len
, nbracket
, nbrace
;
1657 len
= nbracket
= nbrace
= 0;
1659 switch (ng_parse_get_token(s
, &off
, &len
)) {
1667 if (nbracket
-- == 0)
1680 } while (nbracket
> 0 || nbrace
> 0);
1686 * Find the next token in the string, starting at offset *startp.
1687 * Returns the token type, with *startp pointing to the first char
1688 * and *lenp the length.
1691 ng_parse_get_token(const char *s
, int *startp
, int *lenp
)
1696 while (isspace(s
[*startp
]))
1698 switch (s
[*startp
]) {
1718 if ((t
= ng_get_string_token(s
, startp
, lenp
, NULL
)) == NULL
)
1720 kfree(t
, M_NETGRAPH_PARSE
);
1723 for (i
= *startp
+ 1; s
[i
] != '\0' && !isspace(s
[i
])
1724 && s
[i
] != '{' && s
[i
] != '}' && s
[i
] != '['
1725 && s
[i
] != ']' && s
[i
] != '=' && s
[i
] != '"'; i
++)
1727 *lenp
= i
- *startp
;
1733 * Get a string token, which must be enclosed in double quotes.
1734 * The normal C backslash escapes are recognized.
1737 ng_get_string_token(const char *s
, int *startp
, int *lenp
, int *slenp
)
1743 while (isspace(s
[*startp
]))
1746 if (s
[*startp
] != '"')
1748 cbuf
= kmalloc(strlen(s
+ start
), M_NETGRAPH_PARSE
,
1749 M_WAITOK
| M_NULLOK
);
1752 strcpy(cbuf
, s
+ start
+ 1);
1753 for (slen
= 0, off
= 1, p
= cbuf
; *p
!= '\0'; slen
++, off
++, p
++) {
1760 } else if (p
[0] == '\\' && p
[1] != '\0') {
1791 case '0': case '1': case '2': case '3':
1792 case '4': case '5': case '6': case '7':
1794 k
< 3 && *v
>= '0' && *v
<= '7'; v
++) {
1795 x
= (x
<< 3) + (*v
- '0');
1801 for (v
++, x
= k
= 0;
1802 k
< 2 && isxdigit(*v
); v
++) {
1803 x
= (x
<< 4) + (isdigit(*v
) ?
1805 (tolower(*v
) - 'a' + 10));
1816 kfree(cbuf
, M_NETGRAPH_PARSE
);
1817 return (NULL
); /* no closing quote */
1821 * Encode a string so it can be safely put in double quotes.
1822 * Caller must free the result. Exactly "slen" characters
1826 ng_encode_string(const char *raw
, int slen
)
1832 cbuf
= kmalloc(strlen(raw
) * 4 + 3, M_NETGRAPH_PARSE
,
1833 M_WAITOK
| M_NULLOK
);
1837 for (i
= 0; i
< slen
; i
++, raw
++) {
1865 if (*raw
< 0x20 || *raw
> 0x7e) {
1866 off
+= ksprintf(cbuf
+ off
,
1867 "\\x%02x", (u_char
)*raw
);
1879 /************************************************************************
1880 VIRTUAL METHOD LOOKUP
1881 ************************************************************************/
1884 ng_get_parse_method(const struct ng_parse_type
*t
)
1886 while (t
!= NULL
&& t
->parse
== NULL
)
1888 return (t
? t
->parse
: NULL
);
1891 static ng_unparse_t
*
1892 ng_get_unparse_method(const struct ng_parse_type
*t
)
1894 while (t
!= NULL
&& t
->unparse
== NULL
)
1896 return (t
? t
->unparse
: NULL
);
1899 static ng_getDefault_t
*
1900 ng_get_getDefault_method(const struct ng_parse_type
*t
)
1902 while (t
!= NULL
&& t
->getDefault
== NULL
)
1904 return (t
? t
->getDefault
: NULL
);
1907 static ng_getAlign_t
*
1908 ng_get_getAlign_method(const struct ng_parse_type
*t
)
1910 while (t
!= NULL
&& t
->getAlign
== NULL
)
1912 return (t
? t
->getAlign
: NULL
);