4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <dhcp_impl.h>
35 #include "dhcp_symbol.h"
38 * The following structure and table are used to define the attributes
39 * of a DHCP symbol category.
41 typedef struct dsym_cat
{
42 char *dc_string
; /* string value for the category */
43 int dc_minlen
; /* min. chars of dc_string to match */
44 dsym_category_t dc_id
; /* numerical value for the category */
45 boolean_t dc_dhcptab
; /* valid for dhcptab use? */
46 ushort_t dc_min
; /* minimum valid code */
47 ushort_t dc_max
; /* maximum valid code */
50 static dsym_cat_t cats
[] = {
51 { "Extend", 6, DSYM_EXTEND
, B_TRUE
, DHCP_LAST_STD
+ 1,
53 { "Vendor=", 6, DSYM_VENDOR
, B_TRUE
, DHCP_FIRST_OPT
,
55 { "Site", 4, DSYM_SITE
, B_TRUE
, DHCP_SITE_OPT
, DHCP_LAST_OPT
},
56 { "Standard", 8, DSYM_STANDARD
, B_FALSE
, DHCP_FIRST_OPT
,
58 { "Field", 5, DSYM_FIELD
, B_FALSE
, CD_PACKET_START
,
60 { "Internal", 8, DSYM_INTERNAL
, B_FALSE
, CD_INTRNL_START
,
65 * The following structure and table are used to define the attributes
66 * of a DHCP symbol type.
68 typedef struct dsym_type
{
69 char *dt_string
; /* string value for the type */
70 dsym_cdtype_t dt_id
; /* numerical value for the type */
71 boolean_t dt_dhcptab
; /* valid for dhcptab use? */
74 static dsym_type_t types
[] = {
75 { "ASCII", DSYM_ASCII
, B_TRUE
},
76 { "OCTET", DSYM_OCTET
, B_TRUE
},
77 { "IP", DSYM_IP
, B_TRUE
},
78 { "NUMBER", DSYM_NUMBER
, B_TRUE
},
79 { "BOOL", DSYM_BOOL
, B_TRUE
},
80 { "INCLUDE", DSYM_INCLUDE
, B_FALSE
},
81 { "UNUMBER8", DSYM_UNUMBER8
, B_TRUE
},
82 { "UNUMBER16", DSYM_UNUMBER16
, B_TRUE
},
83 { "UNUMBER24", DSYM_UNUMBER24
, B_TRUE
},
84 { "UNUMBER32", DSYM_UNUMBER32
, B_TRUE
},
85 { "UNUMBER64", DSYM_UNUMBER64
, B_TRUE
},
86 { "SNUMBER8", DSYM_SNUMBER8
, B_TRUE
},
87 { "SNUMBER16", DSYM_SNUMBER16
, B_TRUE
},
88 { "SNUMBER32", DSYM_SNUMBER32
, B_TRUE
},
89 { "SNUMBER64", DSYM_SNUMBER64
, B_TRUE
},
90 { "IPV6", DSYM_IPV6
, B_TRUE
},
91 { "DUID", DSYM_DUID
, B_TRUE
},
92 { "DOMAIN", DSYM_DOMAIN
, B_TRUE
}
96 * symbol delimiters and constants
98 #define DSYM_CLASS_DEL " \t\n"
99 #define DSYM_FIELD_DEL ","
100 #define DSYM_VENDOR_DEL '='
101 #define DSYM_QUOTE '"'
104 * dsym_trim(): trims all whitespace from either side of a string
106 * input: char **: a pointer to a string to trim of whitespace.
111 dsym_trim(char **str
)
117 * Trim all whitespace from the front of the string.
119 while (*tmpstr
!= '\0' && isspace(*tmpstr
)) {
124 * Move the str pointer to first non-whitespace char.
129 * Check case where the string is nothing but whitespace.
131 if (*tmpstr
== '\0') {
134 * Trim all whitespace from the end of the string.
136 tmpstr
= *str
+ strlen(*str
) - 1;
137 while (tmpstr
>= *str
&& isspace(*tmpstr
)) {
142 * terminate after last non-whitespace char.
149 * dsym_get_token(): strtok_r() like routine, except consecutive delimiters
150 * result in an empty string
152 * note: original string is modified
154 * input: char *: string in which to search for tokens
155 * char *: list of possible token delimiter characters
156 * char **: location for next call to routine
157 * boolean_t: should delimiters be ignored if within quoted string?
158 * output: char *: token, or NULL if no more tokens
162 dsym_get_token(char *str
, char *dels
, char **lasts
, boolean_t quote_support
)
167 boolean_t found
= B_FALSE
;
168 boolean_t in_quote
= B_FALSE
;
171 * If incoming string has no tokens return a NULL
172 * pointer to signify no more tokens.
179 * Loop until either a token has been identified or until end
180 * of string has been reached.
182 while (!found
&& *ptr
!= '\0') {
185 * If pointer currently lies within a quoted string,
186 * then do not check for the delimiter.
189 for (del
= dels
; !found
&& *del
!= '\0'; del
++) {
198 * If the pointer is pointing at a delimiter, then
199 * check to see if it points to at a quote and update
200 * the state appropriately.
203 if (quote_support
&& *ptr
== DSYM_QUOTE
) {
204 in_quote
= !in_quote
;
216 * dsym_get_long(): given a numeric string, returns its long value
218 * input: const char *: the numeric string
219 * long *: the return location for the long value
220 * output: DSYM_SUCCESS, DSYM_VALUE_OUT_OF_RANGE or DSYM_SYNTAX_ERROR
223 static dsym_errcode_t
224 dsym_get_long(const char *str
, long *val
)
227 int ret
= DSYM_SUCCESS
;
230 for (i
= 0; str
[i
] != '\0'; i
++) {
231 if (!isdigit(str
[i
])) {
232 return (DSYM_SYNTAX_ERROR
);
237 *val
= strtol(str
, NULL
, 10);
239 ret
= DSYM_VALUE_OUT_OF_RANGE
;
246 * dsym_free_classes(): frees the classes allocated by dsym_parse_classes()
248 * input: dhcp_classes_t *: pointer to structure containing classes to free
253 dsym_free_classes(dhcp_classes_t
*classes
)
258 if (classes
->dc_names
== NULL
) {
262 for (i
= 0; i
< classes
->dc_cnt
; i
++) {
263 free(classes
->dc_names
[i
]);
266 free(classes
->dc_names
);
267 classes
->dc_names
= NULL
;
272 * dsym_parse_classes(): given a "Vendor" class string, builds and returns
273 * the list of vendor classes
275 * input: char *: the "Vendor" class string
276 * dhcp_classes_t *: pointer to the classes structure
277 * output: DSYM_SUCCESS, DSYM_INVALID_CAT, DSYM_EXCEEDS_MAX_CLASS_SIZE,
278 * DSYM_EXCEEDS_CLASS_SIZE, DSYM_SYNTAX_ERROR, or DSYM_NO_MEMORY
281 static dsym_errcode_t
282 dsym_parse_classes(char *ptr
, dhcp_classes_t
*classes_ret
)
285 char **classes
= NULL
;
288 int ret
= DSYM_SUCCESS
;
291 while (*ptr
!= '\0') {
292 if (*ptr
== DSYM_VENDOR_DEL
) {
300 return (DSYM_INVALID_CAT
);
303 if (strlen(ptr
) > DSYM_MAX_CLASS_SIZE
) {
304 return (DSYM_EXCEEDS_MAX_CLASS_SIZE
);
308 classes_ret
->dc_cnt
= 0;
309 for (i
= 0; ret
== DSYM_SUCCESS
; i
++) {
310 cp
= dsym_get_token(ptr
, DSYM_CLASS_DEL
, &ptr
, B_TRUE
);
319 } else if (len
> DSYM_CLASS_SIZE
) {
320 ret
= DSYM_EXCEEDS_CLASS_SIZE
;
324 if (cp
[0] == DSYM_QUOTE
&& cp
[len
-1] != DSYM_QUOTE
) {
325 ret
= DSYM_SYNTAX_ERROR
;
329 /* Strip off the quotes */
330 if (cp
[0] == DSYM_QUOTE
) {
335 classes
= reallocarray(classes_ret
->dc_names
,
336 classes_ret
->dc_cnt
+ 1, sizeof (char **));
337 if (classes
== NULL
||
338 (classes
[classes_ret
->dc_cnt
] = strdup(cp
))
340 ret
= DSYM_NO_MEMORY
;
343 classes_ret
->dc_names
= classes
;
344 classes_ret
->dc_cnt
++;
347 if (ret
!= DSYM_SUCCESS
) {
348 dsym_free_classes(classes_ret
);
355 * dsym_get_cat_by_name(): given a category field, returns the pointer to its
356 * entry in the internal category table.
358 * input: const char *: the category name
359 * dsym_cat_t *: the return location for the pointer to the table entry
360 * boolean_t: case-sensitive name compare
361 * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
364 static dsym_errcode_t
365 dsym_get_cat_by_name(const char *cat
, dsym_cat_t
**entry
, boolean_t cs
)
368 dsym_cat_t
*entryp
= NULL
;
369 int ret
= DSYM_SUCCESS
;
370 int cnt
= sizeof (cats
) / sizeof (dsym_cat_t
);
375 for (i
= 0; i
< cnt
; i
++) {
377 len
= cats
[i
].dc_minlen
;
379 result
= strncmp(cat
, cats
[i
].dc_string
, len
);
381 result
= strncasecmp(cat
, cats
[i
].dc_string
, len
);
390 if (entryp
!= NULL
) {
392 * Special code required for the Vendor category, because we
393 * allow whitespace between the keyword and the delimiter.
394 * If there is no delimiter, then this is an illegal category.
396 const char *ptr
= cat
+ entryp
->dc_minlen
;
397 if (entryp
->dc_id
== DSYM_VENDOR
) {
398 while (*ptr
!= '\0' && isspace(*ptr
)) {
401 if (*ptr
!= DSYM_VENDOR_DEL
) {
402 ret
= DSYM_INVALID_CAT
;
406 ret
= DSYM_INVALID_CAT
;
410 ret
= DSYM_INVALID_CAT
;
413 if (ret
== DSYM_SUCCESS
) {
421 * dsym_parse_cat(): given a category field, returns the category value
422 * Note: The category must be a valid dhcptab category.
424 * input: const char *: a category field
425 * dsym_category_t *: the return location for the category value
426 * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
429 static dsym_errcode_t
430 dsym_parse_cat(const char *field
, dsym_category_t
*cat
)
436 ret
= dsym_get_cat_by_name(field
, &entry
, B_TRUE
);
437 if (ret
== DSYM_SUCCESS
) {
439 * Since this routine is meant to be used to parse dhcptab
440 * symbol definitions, only a subset of the DHCP categories
441 * are valid in this context.
443 if (entry
->dc_dhcptab
) {
446 ret
= DSYM_INVALID_CAT
;
454 * dsym_parse_intrange(): given a DHCP integer field, returns the value
456 * input: const char *: a DHCP code field
457 * int *: the return location for the value
458 * int: the minimum valid value
459 * int: the maximum valid value
460 * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, or DSYM_VALUE_OUT_OF_RANGE
463 static dsym_errcode_t
464 dsym_parse_intrange(const char *field
, int *intval
, int min
, int max
)
470 ret
= dsym_get_long(field
, &longval
);
471 if (ret
== DSYM_SUCCESS
) {
472 if (longval
< min
|| longval
> max
) {
473 ret
= DSYM_VALUE_OUT_OF_RANGE
;
475 *intval
= (int)longval
;
482 * dsym_validate_code(): given a symbol category and code, validates
483 * that the code is valid for the category
485 * input: dsym_category_t: the symbol category
486 * uint16_t: the symbol code
487 * output: DSYM_SUCCESS, DSYM_INVALID_CAT or DSYM_CODE_OUT_OF_RANGE
490 static dsym_errcode_t
491 dsym_validate_code(dsym_category_t cat
, ushort_t code
)
494 int cnt
= sizeof (cats
) / sizeof (dsym_cat_t
);
498 * Find the category entry from the internal table.
500 for (i
= 0; i
< cnt
; i
++) {
502 if (cat
== cats
[i
].dc_id
) {
504 if (code
< entry
->dc_min
|| code
> entry
->dc_max
) {
505 return (DSYM_CODE_OUT_OF_RANGE
);
507 return (DSYM_SUCCESS
);
511 return (DSYM_INVALID_CAT
);
515 * dsym_validate_granularity(): given a symbol type, validates
516 * that the granularity is valid for the type
518 * input: dsym_cdtype_t: the symbol type
519 * uchar_t: the symbol granularity
520 * output: DSYM_SUCCESS or DSYM_VALUE_OUT_OF_RANGE
523 static dsym_errcode_t
524 dsym_validate_granularity(dsym_cdtype_t type
, uchar_t gran
)
527 * We only need to check for a 0 with non-boolean types, as
528 * anything else is already validated by the ranges passed to
529 * dsym_parse_intrange() in dsym_parse_field().
531 if (gran
== 0 && type
!= DSYM_BOOL
) {
532 return (DSYM_VALUE_OUT_OF_RANGE
);
534 return (DSYM_SUCCESS
);
538 * dsym_get_type_by_name(): given a type field, returns the pointer to its
539 * entry in the internal type table.
541 * input: const char *: the type name
542 * dsym_type_t *: the return location for the pointer to the table entry
543 * boolean_t: case-sensitive name compare
544 * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
547 static dsym_errcode_t
548 dsym_get_type_by_name(const char *type
, dsym_type_t
**entry
, boolean_t cs
)
550 int cnt
= sizeof (types
) / sizeof (dsym_type_t
);
554 for (i
= 0; i
< cnt
; i
++) {
557 result
= strcmp(type
, types
[i
].dt_string
);
559 result
= strcasecmp(type
, types
[i
].dt_string
);
564 return (DSYM_SUCCESS
);
568 return (DSYM_INVALID_TYPE
);
572 * dsym_parse_type(): given a DHCP type string, returns the type id
574 * input: char *: a DHCP type string
575 * dsym_cdtype_t *: the return location for the type id
576 * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
579 static dsym_errcode_t
580 dsym_parse_type(char *field
, dsym_cdtype_t
*type
)
586 ret
= dsym_get_type_by_name(field
, &entry
, B_TRUE
);
587 if (ret
== DSYM_SUCCESS
) {
589 * Since this routine is meant to be used to parse dhcptab
590 * symbol definitions, only a subset of the DHCP type
591 * are valid in this context.
593 if (entry
->dt_dhcptab
) {
594 *type
= entry
->dt_id
;
596 ret
= DSYM_INVALID_TYPE
;
604 * dsym_free_fields(): frees an array of fields allocated by
605 * dsym_init_parser().
607 * input: char **: array of fields to free
612 dsym_free_fields(char **fields
)
615 if (fields
!= NULL
) {
616 for (i
= 0; i
< DSYM_NUM_FIELDS
; i
++) {
624 * dsym_close_parser(): free up all resources associated with the parser
626 * input: char **: the fields allocated by dsym_init_parser()
627 * dhcp_symbol_t *: the structure populated by dsym_init_parser()
632 dsym_close_parser(char **fields
, dhcp_symbol_t
*sym
)
634 dsym_free_fields(fields
);
635 dsym_free_classes(&sym
->ds_classes
);
639 * dsym_init_parser(): initializes the structures used to parse a symbol
642 * input: const char *: the symbol name
643 * const char *: the symbol value in dhcptab format
644 * char ***: the return location for the symbol fields
645 * dhcp_symbol_t *: the structure which eventually will
646 * be the repository for the parsed symbol data
647 * output: int: DSYM_SUCCESS, DYSM_NO_MEMORY, DSYM_NULL_FIELD or
648 * DSYM_TOO_MANY_FIELDS
652 dsym_init_parser(const char *name
, const char *value
, char ***fields_ret
,
656 int ret
= DSYM_SUCCESS
;
664 * Initialize the symbol structure.
666 sym
->ds_category
= 0;
668 (void) strlcpy(sym
->ds_name
, name
, DSYM_MAX_SYM_LEN
);
672 sym
->ds_classes
.dc_names
= NULL
;
673 sym
->ds_classes
.dc_cnt
= 0;
675 if ((cp
= strdup(value
)) == NULL
||
676 (fields
= calloc(DSYM_NUM_FIELDS
, sizeof (char *))) == NULL
) {
677 ret
= DSYM_NO_MEMORY
;
681 for (i
= 0; ret
== DSYM_SUCCESS
&& i
< DSYM_NUM_FIELDS
; i
++) {
683 field
= dsym_get_token(next
, DSYM_FIELD_DEL
, &next
,
687 ret
= DSYM_NULL_FIELD
;
693 if (strlen(field
) == 0) {
694 ret
= DSYM_NULL_FIELD
;
698 if ((fields
[i
] = strdup(field
)) == NULL
) {
699 ret
= DSYM_NO_MEMORY
;
704 if (ret
== DSYM_SUCCESS
&&
705 dsym_get_token(next
, DSYM_FIELD_DEL
, &next
, B_FALSE
) != NULL
) {
706 ret
= DSYM_TOO_MANY_FIELDS
;
709 if (ret
!= DSYM_SUCCESS
) {
710 dsym_free_fields(fields
);
712 *fields_ret
= fields
;
720 * dsym_parse_field(): parses the specified symbol field.
722 * input: int: the field number to be parsed.
723 * char **: symbol fields initialized by dsym_init_parser()
724 * dhcp_symbol_t *: the structure which will be the repository
725 * for the parsed field
726 * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
727 * DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
728 * DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY,
729 * DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
733 dsym_parse_field(int field_num
, char **fields
, dhcp_symbol_t
*sym
)
736 int ret
= DSYM_SUCCESS
;
742 ret
= dsym_parse_cat(fields
[field_num
], &sym
->ds_category
);
743 if (ret
== DSYM_SUCCESS
&& sym
->ds_category
== DSYM_VENDOR
) {
744 ret
= dsym_parse_classes(fields
[field_num
],
749 case DSYM_CODE_FIELD
:
750 ret
= dsym_parse_intrange(fields
[field_num
], &intval
, 0,
752 if (ret
== DSYM_SUCCESS
) {
753 sym
->ds_code
= (ushort_t
)intval
;
754 ret
= dsym_validate_code(sym
->ds_category
,
759 case DSYM_TYPE_FIELD
:
760 ret
= dsym_parse_type(fields
[field_num
], &sym
->ds_type
);
763 case DSYM_GRAN_FIELD
:
764 ret
= dsym_parse_intrange(fields
[field_num
], &intval
, 0,
766 if (ret
== DSYM_SUCCESS
) {
767 sym
->ds_gran
= (uchar_t
)intval
;
768 ret
= dsym_validate_granularity(sym
->ds_type
,
774 ret
= dsym_parse_intrange(fields
[field_num
], &intval
, 0,
776 if (ret
== DSYM_SUCCESS
) {
777 sym
->ds_max
= (uchar_t
)intval
;
781 ret
= DSYM_INVALID_FIELD_NUM
;
788 * dsym_parser(): parses a DHCP symbol value
790 * input: char **: symbol fields initialized by dsym_init_parser()
791 * dhcp_symbol_t *: the structure which will be the repository
792 * for the parsed field
793 * int *: last field processed
794 * boolean_t: parse all fields even though errors occur?
795 * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
796 * DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
797 * DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY
798 * DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
802 dsym_parser(char **fields
, dhcp_symbol_t
*sym
, int *lastField
,
803 boolean_t bestEffort
)
806 int ret
= DSYM_SUCCESS
;
807 int tret
= DSYM_SUCCESS
;
811 for (i
= DSYM_FIRST_FIELD
;
812 tret
== DSYM_SUCCESS
&& i
< DSYM_NUM_FIELDS
; i
++) {
814 tret
= dsym_parse_field(i
, fields
, sym
);
815 if (tret
!= DSYM_SUCCESS
) {
816 if (ret
== DSYM_SUCCESS
) {
826 if (*lastField
== -1) {
834 * dsym_get_cat_id(): given a category string, return the associated id.
836 * input: const char *: the category name
837 * dsym_category_t *: the return location for the id
838 * boolean_t: case-sensitive name compare
839 * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
843 dsym_get_cat_id(const char *cat
, dsym_category_t
*id
, boolean_t cs
)
849 ret
= dsym_get_cat_by_name(cat
, &entry
, cs
);
850 if (ret
== DSYM_SUCCESS
) {
858 * dsym_get_code_ranges(): given a category field, returns its valid code
861 * input: const char *: the category name
862 * ushort *: return location for the minimum code value.
863 * ushort *: return location for the maximum code value.
864 * boolean_t: case-sensitive name compare
865 * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
869 dsym_get_code_ranges(const char *cat
, ushort_t
*min
, ushort_t
*max
,
876 ret
= dsym_get_cat_by_name(cat
, &entry
, cs
);
877 if (ret
== DSYM_SUCCESS
) {
878 *min
= entry
->dc_min
;
879 *max
= entry
->dc_max
;
886 * dsym_get_type_id(): given a type string, return the associated type id.
888 * input: const char *: the type name
889 * dsym_cdtype_t *: the return location for the id
890 * boolean_t: case-sensitive name compare
891 * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
895 dsym_get_type_id(const char *type
, dsym_cdtype_t
*id
, boolean_t cs
)
901 ret
= dsym_get_type_by_name(type
, &entry
, cs
);
902 if (ret
== DSYM_SUCCESS
) {