2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdlib.h>
2N/A#include <ctype.h>
2N/A#include <strings.h>
2N/A#include <limits.h>
2N/A#include <errno.h>
2N/A#include <dhcp_impl.h>
2N/A
2N/A#include "dhcp_symbol.h"
2N/A
2N/A/*
2N/A * The following structure and table are used to define the attributes
2N/A * of a DHCP symbol category.
2N/A */
2N/Atypedef struct dsym_cat {
2N/A char *dc_string; /* string value for the category */
2N/A int dc_minlen; /* min. chars of dc_string to match */
2N/A dsym_category_t dc_id; /* numerical value for the category */
2N/A boolean_t dc_dhcptab; /* valid for dhcptab use? */
2N/A ushort_t dc_min; /* minimum valid code */
2N/A ushort_t dc_max; /* maximum valid code */
2N/A} dsym_cat_t;
2N/A
2N/Astatic dsym_cat_t cats[] = {
2N/A { "Extend", 6, DSYM_EXTEND, B_TRUE, DHCP_LAST_STD + 1,
2N/A DHCP_SITE_OPT - 1 },
2N/A { "Vendor=", 6, DSYM_VENDOR, B_TRUE, DHCP_FIRST_OPT,
2N/A DHCP_LAST_OPT },
2N/A { "Site", 4, DSYM_SITE, B_TRUE, DHCP_SITE_OPT, DHCP_LAST_OPT },
2N/A { "Standard", 8, DSYM_STANDARD, B_FALSE, DHCP_FIRST_OPT,
2N/A DHCP_LAST_STD },
2N/A { "Field", 5, DSYM_FIELD, B_FALSE, CD_PACKET_START,
2N/A CD_PACKET_END },
2N/A { "Internal", 8, DSYM_INTERNAL, B_FALSE, CD_INTRNL_START,
2N/A CD_INTRNL_END }
2N/A};
2N/A
2N/A/*
2N/A * The following structure and table are used to define the attributes
2N/A * of a DHCP symbol type.
2N/A */
2N/Atypedef struct dsym_type {
2N/A char *dt_string; /* string value for the type */
2N/A dsym_cdtype_t dt_id; /* numerical value for the type */
2N/A boolean_t dt_dhcptab; /* valid for dhcptab use? */
2N/A} dsym_type_t;
2N/A
2N/Astatic dsym_type_t types[] = {
2N/A { "ASCII", DSYM_ASCII, B_TRUE },
2N/A { "OCTET", DSYM_OCTET, B_TRUE },
2N/A { "IP", DSYM_IP, B_TRUE },
2N/A { "NUMBER", DSYM_NUMBER, B_TRUE },
2N/A { "BOOL", DSYM_BOOL, B_TRUE },
2N/A { "INCLUDE", DSYM_INCLUDE, B_FALSE },
2N/A { "UNUMBER8", DSYM_UNUMBER8, B_TRUE },
2N/A { "UNUMBER16", DSYM_UNUMBER16, B_TRUE },
2N/A { "UNUMBER24", DSYM_UNUMBER24, B_TRUE },
2N/A { "UNUMBER32", DSYM_UNUMBER32, B_TRUE },
2N/A { "UNUMBER64", DSYM_UNUMBER64, B_TRUE },
2N/A { "SNUMBER8", DSYM_SNUMBER8, B_TRUE },
2N/A { "SNUMBER16", DSYM_SNUMBER16, B_TRUE },
2N/A { "SNUMBER32", DSYM_SNUMBER32, B_TRUE },
2N/A { "SNUMBER64", DSYM_SNUMBER64, B_TRUE },
2N/A { "IPV6", DSYM_IPV6, B_TRUE },
2N/A { "DUID", DSYM_DUID, B_TRUE },
2N/A { "DOMAIN", DSYM_DOMAIN, B_TRUE }
2N/A};
2N/A
2N/A/*
2N/A * symbol delimiters and constants
2N/A */
2N/A#define DSYM_CLASS_DEL " \t\n"
2N/A#define DSYM_FIELD_DEL ","
2N/A#define DSYM_VENDOR_DEL '='
2N/A#define DSYM_QUOTE '"'
2N/A
2N/A/*
2N/A * dsym_trim(): trims all whitespace from either side of a string
2N/A *
2N/A * input: char **: a pointer to a string to trim of whitespace.
2N/A * output: none
2N/A */
2N/A
2N/Astatic void
2N/Adsym_trim(char **str)
2N/A{
2N/A
2N/A char *tmpstr = *str;
2N/A
2N/A /*
2N/A * Trim all whitespace from the front of the string.
2N/A */
2N/A while (*tmpstr != '\0' && isspace(*tmpstr)) {
2N/A tmpstr++;
2N/A }
2N/A
2N/A /*
2N/A * Move the str pointer to first non-whitespace char.
2N/A */
2N/A *str = tmpstr;
2N/A
2N/A /*
2N/A * Check case where the string is nothing but whitespace.
2N/A */
2N/A if (*tmpstr == '\0') {
2N/A
2N/A /*
2N/A * Trim all whitespace from the end of the string.
2N/A */
2N/A tmpstr = *str + strlen(*str) - 1;
2N/A while (tmpstr >= *str && isspace(*tmpstr)) {
2N/A tmpstr--;
2N/A }
2N/A
2N/A /*
2N/A * terminate after last non-whitespace char.
2N/A */
2N/A *(tmpstr+1) = '\0';
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_token(): strtok_r() like routine, except consecutive delimiters
2N/A * result in an empty string
2N/A *
2N/A * note: original string is modified
2N/A *
2N/A * input: char *: string in which to search for tokens
2N/A * char *: list of possible token delimiter characters
2N/A * char **: location for next call to routine
2N/A * boolean_t: should delimiters be ignored if within quoted string?
2N/A * output: char *: token, or NULL if no more tokens
2N/A */
2N/A
2N/Astatic char *
2N/Adsym_get_token(char *str, char *dels, char **lasts, boolean_t quote_support)
2N/A{
2N/A
2N/A char *ptr = str;
2N/A char *del;
2N/A boolean_t found = B_FALSE;
2N/A boolean_t in_quote = B_FALSE;
2N/A
2N/A /*
2N/A * If incoming string has no tokens return a NULL
2N/A * pointer to signify no more tokens.
2N/A */
2N/A if (*ptr == '\0') {
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Loop until either a token has been identified or until end
2N/A * of string has been reached.
2N/A */
2N/A while (!found && *ptr != '\0') {
2N/A
2N/A /*
2N/A * If pointer currently lies within a quoted string,
2N/A * then do not check for the delimiter.
2N/A */
2N/A if (!in_quote) {
2N/A for (del = dels; !found && *del != '\0'; del++) {
2N/A if (*del == *ptr) {
2N/A *ptr++ = '\0';
2N/A found = B_TRUE;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If the pointer is pointing at a delimiter, then
2N/A * check to see if it points to at a quote and update
2N/A * the state appropriately.
2N/A */
2N/A if (!found) {
2N/A if (quote_support && *ptr == DSYM_QUOTE) {
2N/A in_quote = !in_quote;
2N/A }
2N/A ptr++;
2N/A }
2N/A }
2N/A
2N/A *lasts = ptr;
2N/A
2N/A return (str);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_long(): given a numeric string, returns its long value
2N/A *
2N/A * input: const char *: the numeric string
2N/A * long *: the return location for the long value
2N/A * output: DSYM_SUCCESS, DSYM_VALUE_OUT_OF_RANGE or DSYM_SYNTAX_ERROR
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_get_long(const char *str, long *val)
2N/A{
2N/A
2N/A int ret = DSYM_SUCCESS;
2N/A int i;
2N/A
2N/A for (i = 0; str[i] != '\0'; i++) {
2N/A if (!isdigit(str[i])) {
2N/A return (DSYM_SYNTAX_ERROR);
2N/A }
2N/A }
2N/A
2N/A errno = 0;
2N/A *val = strtol(str, NULL, 10);
2N/A if (errno != 0) {
2N/A ret = DSYM_VALUE_OUT_OF_RANGE;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_free_classes(): frees the classes allocated by dsym_parse_classes()
2N/A *
2N/A * input: dhcp_classes_t *: pointer to structure containing classes to free
2N/A * output: none
2N/A */
2N/A
2N/Avoid
2N/Adsym_free_classes(dhcp_classes_t *classes)
2N/A{
2N/A
2N/A int i;
2N/A
2N/A if (classes->dc_names == NULL) {
2N/A return;
2N/A }
2N/A
2N/A for (i = 0; i < classes->dc_cnt; i++) {
2N/A free(classes->dc_names[i]);
2N/A }
2N/A
2N/A free(classes->dc_names);
2N/A classes->dc_names = NULL;
2N/A classes->dc_cnt = 0;
2N/A}
2N/A
2N/A/*
2N/A * dsym_parse_classes(): given a "Vendor" class string, builds and returns
2N/A * the list of vendor classes
2N/A *
2N/A * input: char *: the "Vendor" class string
2N/A * dhcp_classes_t *: pointer to the classes structure
2N/A * output: DSYM_SUCCESS, DSYM_INVALID_CAT, DSYM_EXCEEDS_MAX_CLASS_SIZE,
2N/A * DSYM_EXCEEDS_CLASS_SIZE, DSYM_SYNTAX_ERROR, or DSYM_NO_MEMORY
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_parse_classes(char *ptr, dhcp_classes_t *classes_ret)
2N/A{
2N/A
2N/A char **classes = NULL;
2N/A char *cp;
2N/A int len;
2N/A int ret = DSYM_SUCCESS;
2N/A int i;
2N/A
2N/A while (*ptr != '\0') {
2N/A if (*ptr == DSYM_VENDOR_DEL) {
2N/A ptr++;
2N/A break;
2N/A }
2N/A ptr++;
2N/A }
2N/A
2N/A if (*ptr == '\0') {
2N/A return (DSYM_INVALID_CAT);
2N/A }
2N/A
2N/A if (strlen(ptr) > DSYM_MAX_CLASS_SIZE) {
2N/A return (DSYM_EXCEEDS_MAX_CLASS_SIZE);
2N/A }
2N/A
2N/A dsym_trim(&ptr);
2N/A classes_ret->dc_cnt = 0;
2N/A for (i = 0; ret == DSYM_SUCCESS; i++) {
2N/A cp = dsym_get_token(ptr, DSYM_CLASS_DEL, &ptr, B_TRUE);
2N/A if (cp == NULL) {
2N/A break;
2N/A }
2N/A
2N/A len = strlen(cp);
2N/A
2N/A if (len == 0) {
2N/A continue;
2N/A } else if (len > DSYM_CLASS_SIZE) {
2N/A ret = DSYM_EXCEEDS_CLASS_SIZE;
2N/A continue;
2N/A }
2N/A
2N/A if (cp[0] == DSYM_QUOTE && cp[len-1] != DSYM_QUOTE) {
2N/A ret = DSYM_SYNTAX_ERROR;
2N/A continue;
2N/A }
2N/A
2N/A /* Strip off the quotes */
2N/A if (cp[0] == DSYM_QUOTE) {
2N/A cp[len-1] = '\0';
2N/A cp++;
2N/A }
2N/A
2N/A classes = realloc(classes_ret->dc_names,
2N/A (sizeof (char **)) * (classes_ret->dc_cnt + 1));
2N/A if (classes == NULL ||
2N/A (classes[classes_ret->dc_cnt] = strdup(cp))
2N/A == NULL) {
2N/A ret = DSYM_NO_MEMORY;
2N/A continue;
2N/A }
2N/A classes_ret->dc_names = classes;
2N/A classes_ret->dc_cnt++;
2N/A }
2N/A
2N/A if (ret != DSYM_SUCCESS) {
2N/A dsym_free_classes(classes_ret);
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_cat_by_name(): given a category field, returns the pointer to its
2N/A * entry in the internal category table.
2N/A *
2N/A * input: const char *: the category name
2N/A * dsym_cat_t *: the return location for the pointer to the table entry
2N/A * boolean_t: case-sensitive name compare
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_get_cat_by_name(const char *cat, dsym_cat_t **entry, boolean_t cs)
2N/A{
2N/A
2N/A dsym_cat_t *entryp = NULL;
2N/A int ret = DSYM_SUCCESS;
2N/A int cnt = sizeof (cats) / sizeof (dsym_cat_t);
2N/A int result;
2N/A int len;
2N/A int i;
2N/A
2N/A for (i = 0; i < cnt; i++) {
2N/A
2N/A len = cats[i].dc_minlen;
2N/A if (cs) {
2N/A result = strncmp(cat, cats[i].dc_string, len);
2N/A } else {
2N/A result = strncasecmp(cat, cats[i].dc_string, len);
2N/A }
2N/A
2N/A if (result == 0) {
2N/A entryp = &cats[i];
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (entryp != NULL) {
2N/A /*
2N/A * Special code required for the Vendor category, because we
2N/A * allow whitespace between the keyword and the delimiter.
2N/A * If there is no delimiter, then this is an illegal category.
2N/A */
2N/A const char *ptr = cat + entryp->dc_minlen;
2N/A if (entryp->dc_id == DSYM_VENDOR) {
2N/A while (*ptr != '\0' && isspace(*ptr)) {
2N/A ptr++;
2N/A }
2N/A if (*ptr != DSYM_VENDOR_DEL) {
2N/A ret = DSYM_INVALID_CAT;
2N/A }
2N/A } else {
2N/A if (*ptr != '\0') {
2N/A ret = DSYM_INVALID_CAT;
2N/A }
2N/A }
2N/A } else {
2N/A ret = DSYM_INVALID_CAT;
2N/A }
2N/A
2N/A if (ret == DSYM_SUCCESS) {
2N/A *entry = entryp;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_parse_cat(): given a category field, returns the category value
2N/A * Note: The category must be a valid dhcptab category.
2N/A *
2N/A * input: const char *: a category field
2N/A * dsym_category_t *: the return location for the category value
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_parse_cat(const char *field, dsym_category_t *cat)
2N/A{
2N/A
2N/A dsym_cat_t *entry;
2N/A int ret;
2N/A
2N/A ret = dsym_get_cat_by_name(field, &entry, B_TRUE);
2N/A if (ret == DSYM_SUCCESS) {
2N/A /*
2N/A * Since this routine is meant to be used to parse dhcptab
2N/A * symbol definitions, only a subset of the DHCP categories
2N/A * are valid in this context.
2N/A */
2N/A if (entry->dc_dhcptab) {
2N/A *cat = entry->dc_id;
2N/A } else {
2N/A ret = DSYM_INVALID_CAT;
2N/A }
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_parse_intrange(): given a DHCP integer field, returns the value
2N/A *
2N/A * input: const char *: a DHCP code field
2N/A * int *: the return location for the value
2N/A * int: the minimum valid value
2N/A * int: the maximum valid value
2N/A * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, or DSYM_VALUE_OUT_OF_RANGE
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_parse_intrange(const char *field, int *intval, int min, int max)
2N/A{
2N/A
2N/A int ret;
2N/A long longval;
2N/A
2N/A ret = dsym_get_long(field, &longval);
2N/A if (ret == DSYM_SUCCESS) {
2N/A if (longval < min || longval > max) {
2N/A ret = DSYM_VALUE_OUT_OF_RANGE;
2N/A } else {
2N/A *intval = (int)longval;
2N/A }
2N/A }
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_validate_code(): given a symbol category and code, validates
2N/A * that the code is valid for the category
2N/A *
2N/A * input: dsym_category_t: the symbol category
2N/A * uint16_t: the symbol code
2N/A * output: DSYM_SUCCESS, DSYM_INVALID_CAT or DSYM_CODE_OUT_OF_RANGE
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_validate_code(dsym_category_t cat, ushort_t code)
2N/A{
2N/A
2N/A int cnt = sizeof (cats) / sizeof (dsym_cat_t);
2N/A int i;
2N/A
2N/A /*
2N/A * Find the category entry from the internal table.
2N/A */
2N/A for (i = 0; i < cnt; i++) {
2N/A dsym_cat_t *entry;
2N/A if (cat == cats[i].dc_id) {
2N/A entry = &cats[i];
2N/A if (code < entry->dc_min || code > entry->dc_max) {
2N/A return (DSYM_CODE_OUT_OF_RANGE);
2N/A }
2N/A return (DSYM_SUCCESS);
2N/A }
2N/A }
2N/A
2N/A return (DSYM_INVALID_CAT);
2N/A}
2N/A
2N/A/*
2N/A * dsym_validate_granularity(): given a symbol type, validates
2N/A * that the granularity is valid for the type
2N/A *
2N/A * input: dsym_cdtype_t: the symbol type
2N/A * uchar_t: the symbol granularity
2N/A * output: DSYM_SUCCESS or DSYM_VALUE_OUT_OF_RANGE
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_validate_granularity(dsym_cdtype_t type, uchar_t gran)
2N/A{
2N/A /*
2N/A * We only need to check for a 0 with non-boolean types, as
2N/A * anything else is already validated by the ranges passed to
2N/A * dsym_parse_intrange() in dsym_parse_field().
2N/A */
2N/A if (gran == 0 && type != DSYM_BOOL) {
2N/A return (DSYM_VALUE_OUT_OF_RANGE);
2N/A }
2N/A return (DSYM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_type_by_name(): given a type field, returns the pointer to its
2N/A * entry in the internal type table.
2N/A *
2N/A * input: const char *: the type name
2N/A * dsym_type_t *: the return location for the pointer to the table entry
2N/A * boolean_t: case-sensitive name compare
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_get_type_by_name(const char *type, dsym_type_t **entry, boolean_t cs)
2N/A{
2N/A int cnt = sizeof (types) / sizeof (dsym_type_t);
2N/A int result;
2N/A int i;
2N/A
2N/A for (i = 0; i < cnt; i++) {
2N/A
2N/A if (cs) {
2N/A result = strcmp(type, types[i].dt_string);
2N/A } else {
2N/A result = strcasecmp(type, types[i].dt_string);
2N/A }
2N/A
2N/A if (result == 0) {
2N/A *entry = &types[i];
2N/A return (DSYM_SUCCESS);
2N/A }
2N/A }
2N/A
2N/A return (DSYM_INVALID_TYPE);
2N/A}
2N/A
2N/A/*
2N/A * dsym_parse_type(): given a DHCP type string, returns the type id
2N/A *
2N/A * input: char *: a DHCP type string
2N/A * dsym_cdtype_t *: the return location for the type id
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
2N/A */
2N/A
2N/Astatic dsym_errcode_t
2N/Adsym_parse_type(char *field, dsym_cdtype_t *type)
2N/A{
2N/A
2N/A dsym_type_t *entry;
2N/A int ret;
2N/A
2N/A ret = dsym_get_type_by_name(field, &entry, B_TRUE);
2N/A if (ret == DSYM_SUCCESS) {
2N/A /*
2N/A * Since this routine is meant to be used to parse dhcptab
2N/A * symbol definitions, only a subset of the DHCP type
2N/A * are valid in this context.
2N/A */
2N/A if (entry->dt_dhcptab) {
2N/A *type = entry->dt_id;
2N/A } else {
2N/A ret = DSYM_INVALID_TYPE;
2N/A }
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_free_fields(): frees an array of fields allocated by
2N/A * dsym_init_parser().
2N/A *
2N/A * input: char **: array of fields to free
2N/A * output: none
2N/A */
2N/A
2N/Avoid
2N/Adsym_free_fields(char **fields)
2N/A{
2N/A int i;
2N/A if (fields != NULL) {
2N/A for (i = 0; i < DSYM_NUM_FIELDS; i++) {
2N/A free(fields[i]);
2N/A }
2N/A free(fields);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * dsym_close_parser(): free up all resources associated with the parser
2N/A *
2N/A * input: char **: the fields allocated by dsym_init_parser()
2N/A * dhcp_symbol_t *: the structure populated by dsym_init_parser()
2N/A * output: none
2N/A */
2N/A
2N/Avoid
2N/Adsym_close_parser(char **fields, dhcp_symbol_t *sym)
2N/A{
2N/A dsym_free_fields(fields);
2N/A dsym_free_classes(&sym->ds_classes);
2N/A}
2N/A
2N/A/*
2N/A * dsym_init_parser(): initializes the structures used to parse a symbol
2N/A * value.
2N/A *
2N/A * input: const char *: the symbol name
2N/A * const char *: the symbol value in dhcptab format
2N/A * char ***: the return location for the symbol fields
2N/A * dhcp_symbol_t *: the structure which eventually will
2N/A * be the repository for the parsed symbol data
2N/A * output: int: DSYM_SUCCESS, DYSM_NO_MEMORY, DSYM_NULL_FIELD or
2N/A * DSYM_TOO_MANY_FIELDS
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_init_parser(const char *name, const char *value, char ***fields_ret,
2N/A dhcp_symbol_t *sym)
2N/A{
2N/A
2N/A int ret = DSYM_SUCCESS;
2N/A char *cp;
2N/A char *next;
2N/A char *field;
2N/A char **fields;
2N/A int i;
2N/A
2N/A /*
2N/A * Initialize the symbol structure.
2N/A */
2N/A sym->ds_category = 0;
2N/A sym->ds_code = 0;
2N/A (void) strlcpy(sym->ds_name, name, DSYM_MAX_SYM_LEN);
2N/A sym->ds_type = 0;
2N/A sym->ds_gran = 0;
2N/A sym->ds_max = 0;
2N/A sym->ds_classes.dc_names = NULL;
2N/A sym->ds_classes.dc_cnt = 0;
2N/A
2N/A if ((cp = strdup(value)) == NULL ||
2N/A (fields = calloc(DSYM_NUM_FIELDS, sizeof (char *))) == NULL) {
2N/A ret = DSYM_NO_MEMORY;
2N/A }
2N/A
2N/A next = cp;
2N/A for (i = 0; ret == DSYM_SUCCESS && i < DSYM_NUM_FIELDS; i++) {
2N/A
2N/A field = dsym_get_token(next, DSYM_FIELD_DEL, &next,
2N/A B_FALSE);
2N/A
2N/A if (field == NULL) {
2N/A ret = DSYM_NULL_FIELD;
2N/A continue;
2N/A }
2N/A
2N/A dsym_trim(&field);
2N/A
2N/A if (strlen(field) == 0) {
2N/A ret = DSYM_NULL_FIELD;
2N/A continue;
2N/A }
2N/A
2N/A if ((fields[i] = strdup(field)) == NULL) {
2N/A ret = DSYM_NO_MEMORY;
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A if (ret == DSYM_SUCCESS &&
2N/A dsym_get_token(next, DSYM_FIELD_DEL, &next, B_FALSE) != NULL) {
2N/A ret = DSYM_TOO_MANY_FIELDS;
2N/A }
2N/A
2N/A if (ret != DSYM_SUCCESS) {
2N/A dsym_free_fields(fields);
2N/A } else {
2N/A *fields_ret = fields;
2N/A }
2N/A
2N/A free(cp);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_parse_field(): parses the specified symbol field.
2N/A *
2N/A * input: int: the field number to be parsed.
2N/A * char **: symbol fields initialized by dsym_init_parser()
2N/A * dhcp_symbol_t *: the structure which will be the repository
2N/A * for the parsed field
2N/A * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
2N/A * DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
2N/A * DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY,
2N/A * DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_parse_field(int field_num, char **fields, dhcp_symbol_t *sym)
2N/A{
2N/A
2N/A int ret = DSYM_SUCCESS;
2N/A int intval;
2N/A
2N/A switch (field_num) {
2N/A
2N/A case DSYM_CAT_FIELD:
2N/A ret = dsym_parse_cat(fields[field_num], &sym->ds_category);
2N/A if (ret == DSYM_SUCCESS && sym->ds_category == DSYM_VENDOR) {
2N/A ret = dsym_parse_classes(fields[field_num],
2N/A &sym->ds_classes);
2N/A }
2N/A break;
2N/A
2N/A case DSYM_CODE_FIELD:
2N/A ret = dsym_parse_intrange(fields[field_num], &intval, 0,
2N/A USHRT_MAX);
2N/A if (ret == DSYM_SUCCESS) {
2N/A sym->ds_code = (ushort_t)intval;
2N/A ret = dsym_validate_code(sym->ds_category,
2N/A sym->ds_code);
2N/A }
2N/A break;
2N/A
2N/A case DSYM_TYPE_FIELD:
2N/A ret = dsym_parse_type(fields[field_num], &sym->ds_type);
2N/A break;
2N/A
2N/A case DSYM_GRAN_FIELD:
2N/A ret = dsym_parse_intrange(fields[field_num], &intval, 0,
2N/A UCHAR_MAX);
2N/A if (ret == DSYM_SUCCESS) {
2N/A sym->ds_gran = (uchar_t)intval;
2N/A ret = dsym_validate_granularity(sym->ds_type,
2N/A sym->ds_gran);
2N/A }
2N/A break;
2N/A
2N/A case DSYM_MAX_FIELD:
2N/A ret = dsym_parse_intrange(fields[field_num], &intval, 0,
2N/A UCHAR_MAX);
2N/A if (ret == DSYM_SUCCESS) {
2N/A sym->ds_max = (uchar_t)intval;
2N/A }
2N/A break;
2N/A default:
2N/A ret = DSYM_INVALID_FIELD_NUM;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_parser(): parses a DHCP symbol value
2N/A *
2N/A * input: char **: symbol fields initialized by dsym_init_parser()
2N/A * dhcp_symbol_t *: the structure which will be the repository
2N/A * for the parsed field
2N/A * int *: last field processed
2N/A * boolean_t: parse all fields even though errors occur?
2N/A * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
2N/A * DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
2N/A * DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY
2N/A * DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_parser(char **fields, dhcp_symbol_t *sym, int *lastField,
2N/A boolean_t bestEffort)
2N/A{
2N/A
2N/A int ret = DSYM_SUCCESS;
2N/A int tret = DSYM_SUCCESS;
2N/A int i;
2N/A
2N/A *lastField = -1;
2N/A for (i = DSYM_FIRST_FIELD;
2N/A tret == DSYM_SUCCESS && i < DSYM_NUM_FIELDS; i++) {
2N/A
2N/A tret = dsym_parse_field(i, fields, sym);
2N/A if (tret != DSYM_SUCCESS) {
2N/A if (ret == DSYM_SUCCESS) {
2N/A ret = tret;
2N/A }
2N/A if (bestEffort) {
2N/A *lastField = i;
2N/A tret = DSYM_SUCCESS;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (*lastField == -1) {
2N/A *lastField = i - 1;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_cat_id(): given a category string, return the associated id.
2N/A *
2N/A * input: const char *: the category name
2N/A * dsym_category_t *: the return location for the id
2N/A * boolean_t: case-sensitive name compare
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_get_cat_id(const char *cat, dsym_category_t *id, boolean_t cs)
2N/A{
2N/A
2N/A dsym_cat_t *entry;
2N/A int ret;
2N/A
2N/A ret = dsym_get_cat_by_name(cat, &entry, cs);
2N/A if (ret == DSYM_SUCCESS) {
2N/A *id = entry->dc_id;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_code_ranges(): given a category field, returns its valid code
2N/A * ranges.
2N/A *
2N/A * input: const char *: the category name
2N/A * ushort *: return location for the minimum code value.
2N/A * ushort *: return location for the maximum code value.
2N/A * boolean_t: case-sensitive name compare
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_get_code_ranges(const char *cat, ushort_t *min, ushort_t *max,
2N/A boolean_t cs)
2N/A{
2N/A
2N/A dsym_cat_t *entry;
2N/A int ret;
2N/A
2N/A ret = dsym_get_cat_by_name(cat, &entry, cs);
2N/A if (ret == DSYM_SUCCESS) {
2N/A *min = entry->dc_min;
2N/A *max = entry->dc_max;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * dsym_get_type_id(): given a type string, return the associated type id.
2N/A *
2N/A * input: const char *: the type name
2N/A * dsym_cdtype_t *: the return location for the id
2N/A * boolean_t: case-sensitive name compare
2N/A * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
2N/A */
2N/A
2N/Adsym_errcode_t
2N/Adsym_get_type_id(const char *type, dsym_cdtype_t *id, boolean_t cs)
2N/A{
2N/A
2N/A dsym_type_t *entry;
2N/A int ret;
2N/A
2N/A ret = dsym_get_type_by_name(type, &entry, cs);
2N/A if (ret == DSYM_SUCCESS) {
2N/A *id = entry->dt_id;
2N/A }
2N/A
2N/A return (ret);
2N/A}