From: Florian Forster Date: Tue, 25 Jan 2011 06:42:53 +0000 (+0100) Subject: src/configfile.c: cf_util_get_port_number: Gracefully handle number arguments as... X-Git-Tag: collectd-5.0.0~14 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=be807fac042c06ae3b41590ece6e96a323ff42ff;p=collectd.git src/configfile.c: cf_util_get_port_number: Gracefully handle number arguments as well. Why force the user into using strings when it's not strictly necessary..? --- diff --git a/src/configfile.c b/src/configfile.c index 33a7c200..54d418b4 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -1058,19 +1058,40 @@ int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */ return (0); } /* }}} int cf_util_get_flag */ -/* Assures that the config option is a string. The string is then converted to - * a port number using `service_name_to_port_number' and returned. Returns the - * port number in the range [1-65535] or less than zero upon failure. */ +/* Assures that the config option is a string or a number if the correct range + * of 1-65535. The string is then converted to a port number using + * `service_name_to_port_number' and returned. + * Returns the port number in the range [1-65535] or less than zero upon + * failure. */ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */ { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + int tmp; + + if ((ci->values_num != 1) + || ((ci->values[0].type != OCONFIG_TYPE_STRING) + && (ci->values[0].type != OCONFIG_TYPE_NUMBER))) { - ERROR ("cf_util_get_port_number: The %s option requires " + ERROR ("cf_util_get_port_number: The \"%s\" option requires " "exactly one string argument.", ci->key); return (-1); } - return (service_name_to_port_number (ci->values[0].value.string)); + if (ci->values[0].type == OCONFIG_TYPE_STRING) + return (service_name_to_port_number (ci->values[0].value.string)); + + assert (ci->values[0].type == OCONFIG_TYPE_NUMBER); + tmp = (int) (ci->values[0].value.number + 0.5); + if ((tmp < 1) || (tmp > 65535)) + { + ERROR ("cf_util_get_port_number: The \"%s\" option requires " + "a service name or a port number. The number " + "you specified, %i, is not in the valid " + "range of 1-65535.", + ci->key, tmp); + return (-1); + } + + return (tmp); } /* }}} int cf_util_get_port_number */ int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ diff --git a/src/configfile.h b/src/configfile.h index 65b1efcd..c381028c 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -109,9 +109,11 @@ int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool); int cf_util_get_flag (const oconfig_item_t *ci, unsigned int *ret_value, unsigned int flag); -/* Assures that the config option is a string. The string is then converted to - * a port number using `service_name_to_port_number' and returned. Returns the - * port number in the range [1-65535] or less than zero upon failure. */ +/* Assures that the config option is a string or a number if the correct range + * of 1-65535. The string is then converted to a port number using + * `service_name_to_port_number' and returned. + * Returns the port number in the range [1-65535] or less than zero upon + * failure. */ int cf_util_get_port_number (const oconfig_item_t *ci); int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value);