/** * Validate if a value against a specific integer * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @param min minimum value of number including fractional digits * @param max maximum value of number including fractional digits * @param bytelen number of bytes available for target number * @param fraction number of fractional digits of target number * @param base2 true if number shall use binary prefixes instead of * iso prefixes (1024 instead of 1000) * @return 0 if value is valid, -1 otherwise */ int cfg_validate_int(struct autobuf *out, const char *section_name, const char *entry_name, const char *value, int64_t min, int64_t max, uint16_t bytelen, uint16_t fraction, bool base2) { int64_t i, min64, max64; struct isonumber_str hbuf; if (isonumber_to_s64(&i, value, fraction, base2)) { if (fraction) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s is not a fractional %d-byte integer" " with a maximum of %d fractional digits", value, entry_name, section_name, bytelen, fraction); } else { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s is not a %d-byte integer.", value, entry_name, section_name, bytelen); } return -1; } min64 = INT64_MIN >> (8 * (8 - bytelen)); max64 = INT64_MAX >> (8 * (8 - bytelen)); if (i < min64 || i > max64) { cfg_append_printable_line(out, "Value '%s' for entry '%s' in section %s is " "too %s for a %d-hyte integer with %d fractional digits", value, entry_name, section_name, i < min ? "small" : "large", bytelen, fraction); } if (i < min) { cfg_append_printable_line(out, "Value '%s' for entry '%s' in section %s is " "smaller than %s", value, entry_name, section_name, isonumber_from_s64(&hbuf, min, "", fraction, base2, true)); return -1; } if (i > max) { cfg_append_printable_line(out, "Value '%s' for entry '%s' in section %s is " "larger than %s", value, entry_name, section_name, isonumber_from_s64(&hbuf, min, "", fraction, base2, true)); return -1; } return 0; }
/** * Validate if a value against a specific network address * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @param prefix true if the address might be a network prefix * @param af_types list of address families the address might be * @param af_types_count number of address families specified * @return 0 if value is valid, -1 otherwise */ int cfg_validate_netaddr(struct autobuf *out, const char *section_name, const char *entry_name, const char *value, bool prefix, const int8_t *af_types, size_t af_types_count) { struct netaddr addr; uint8_t max_prefix; size_t i; if (netaddr_from_string(&addr, value)) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s is no valid network address", value, entry_name, section_name); return -1; } max_prefix = netaddr_get_maxprefix(&addr); /* check prefix length */ if (netaddr_get_prefix_length(&addr) > max_prefix) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s has an illegal prefix length", value, entry_name, section_name); return -1; } if (!prefix && netaddr_get_prefix_length(&addr) != max_prefix) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s must be a single address, not a prefix", value, entry_name, section_name); return -1; } for (i=0; i<af_types_count; i++) { if (af_types[i] == netaddr_get_address_family(&addr)) { return 0; } } /* at least one condition was set, but no one matched */ cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section '%s' is wrong address type", value, entry_name, section_name); return -1; }
/** * Validate if a value against a specific string * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @param len maximum length of string * @return 0 if value is valid, -1 otherwise */ int cfg_validate_strlen(struct autobuf *out, const char *section_name, const char *entry_name, const char *value, size_t len) { if (strlen(value) > len) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s is longer than %"PRINTF_SIZE_T_SPECIFIER" characters", value, entry_name, section_name, len); return -1; } return 0; }
void cfg_help_int(struct autobuf *out, int64_t min, int64_t max, uint16_t bytelen, uint16_t fraction, bool base2) { struct isonumber_str hbuf1, hbuf2; int64_t min64, max64; min64 = INT64_MIN >> (8 * (8 - bytelen)); max64 = INT64_MAX >> (8 * (8 - bytelen)); /* get string representation of min/max */ str_to_isonumber_s64(&hbuf1, min, "", fraction, base2, true); str_to_isonumber_s64(&hbuf2, max, "", fraction, base2, true); if (min > min64) { if (max < max64) { cfg_append_printable_line(out, _PREFIX "Parameter must be a %d-byte fractional integer" " between %s and %s with a maximum of %d digits", bytelen, hbuf1.buf, hbuf2.buf, fraction); } else { cfg_append_printable_line(out, _PREFIX "Parameter must be a %d-byte fractional integer" " larger or equal than %s with a maximum of %d digits", bytelen, hbuf1.buf, fraction); } } else { if (max < max64) { cfg_append_printable_line(out, _PREFIX "Parameter must be a %d-byte fractional integer" " smaller or equal than %s with a maximum of %d digits", bytelen, hbuf2.buf, fraction); } else { cfg_append_printable_line(out, _PREFIX "Parameter must be a %d-byte signed integer" " with a maximum of %d digits", bytelen, fraction); } } }
/** * Validate if a value against a specific printable string * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @param len maximum length of printable string * @return 0 if value is valid, -1 otherwise */ int cfg_validate_printable(struct autobuf *out, const char *section_name, const char *entry_name, const char *value, size_t len) { if (cfg_validate_strlen(out, section_name, entry_name, value, len)) { return -1; } if (!str_is_printable(value)) { /* not a printable ascii character */ cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s has non-printable characters", value, entry_name, section_name); return -1; } return 0; }
/** * Validate if a value against a specific list of strings * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @param choices pointer to list of strings * @param choices_count number of strings in list * @return 0 if value is valid, -1 otherwise */ int cfg_validate_choice(struct autobuf *out, const char *section_name, const char *entry_name, const char *value, const char **choices, size_t choices_count) { int i; i = cfg_get_choice_index(value, choices, choices_count); if (i >= 0) { return 0; } cfg_append_printable_line(out, "Unknown value '%s'" " for entry '%s' in section %s", value, entry_name, section_name); return -1; }
void cfg_help_choice(struct autobuf *out, bool preamble, const char **choices, size_t choice_count) { size_t i; if (preamble) { cfg_append_printable_line(out, _PREFIX "Parameter must be on of the following list:"); } abuf_puts(out, " "); for (i=0; i < choice_count; i++) { abuf_appendf(out, "%s'%s'", i==0 ? "" : ", ", choices[i]); } abuf_puts(out, "\n"); }
/** * Validates if a value is valid part of a bitmap256 definition * @param out output buffer for error messages * @param section_name name of configuration section * @param entry_name name of configuration entry * @param value value that needs to be validated * @return 0 if value is valid, -1 otherwise */ int cfg_validate_bitmap256(struct autobuf *out, const char *section_name, const char *entry_name, const char *value){ int num; char *end; if (strcasecmp(value, "all") == 0 || strcasecmp(value, "none") == 0) { return 0; } errno = 0; num = strtol(value, &end, 10); if (errno != 0 || *end != 0 || num < -255 || num > 255) { cfg_append_printable_line(out, "Value '%s' for entry '%s'" " in section %s must be a '" BITMAP256_ALL "', '" BITMAP256_NONE "' or a number between 0 and 255" " (with optional '-' in front of the number)", value, entry_name, section_name); return -1; } return 0; }
void cfg_help_printable(struct autobuf *out, size_t len) { cfg_help_printable(out, len); cfg_append_printable_line(out, _PREFIX "Parameter must only contain printable characters."); }
void cfg_help_strlen(struct autobuf *out, size_t len) { cfg_append_printable_line(out, _PREFIX "Parameter must have a maximum" " length of %"PRINTF_SIZE_T_SPECIFIER" characters", len); }