Merge pull request #29224 from keszybz/netdev-config-parsing
Use a helper to simplify parsing of ranges in netdev config and related changes
This commit is contained in:
@@ -1908,6 +1908,44 @@ int config_parse_in_addr_non_null(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_parse_unsigned_bounded(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *name,
|
||||
const char *value,
|
||||
unsigned min,
|
||||
unsigned max,
|
||||
bool ignoring,
|
||||
unsigned *ret) {
|
||||
|
||||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(name);
|
||||
assert(value);
|
||||
assert(ret);
|
||||
|
||||
r = safe_atou_bounded(value, min, max, ret);
|
||||
if (r == -ERANGE)
|
||||
log_syntax(unit, LOG_WARNING, filename, line, r,
|
||||
"Invalid '%s=%s', allowed range is %u..%u%s.",
|
||||
name, value, min, max, ignoring ? ", ignoring" : "");
|
||||
else if (r < 0)
|
||||
log_syntax(unit, LOG_WARNING, filename, line, r,
|
||||
"Failed to parse '%s=%s'%s: %m",
|
||||
name, value, ignoring ? ", ignoring" : "");
|
||||
|
||||
if (r >= 0)
|
||||
return 1; /* Return 1 if something was set */
|
||||
else if (ignoring)
|
||||
return 0;
|
||||
else
|
||||
return r;
|
||||
}
|
||||
|
||||
DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent, "Failed to parse percent value");
|
||||
DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad, "Failed to parse permyriad value");
|
||||
DEFINE_CONFIG_PARSE_PTR(config_parse_sec_fix_0, parse_sec_fix_0, usec_t, "Failed to parse time value");
|
||||
|
||||
@@ -385,3 +385,97 @@ typedef enum ConfigParseStringFlags {
|
||||
\
|
||||
return free_and_replace(*enums, xs); \
|
||||
}
|
||||
|
||||
int config_parse_unsigned_bounded(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *name,
|
||||
const char *value,
|
||||
unsigned min,
|
||||
unsigned max,
|
||||
bool ignoring,
|
||||
unsigned *ret);
|
||||
|
||||
static inline int config_parse_uint32_bounded(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *name,
|
||||
const char *value,
|
||||
uint32_t min,
|
||||
uint32_t max,
|
||||
bool ignoring,
|
||||
uint32_t *ret) {
|
||||
|
||||
unsigned t;
|
||||
int r;
|
||||
|
||||
r = config_parse_unsigned_bounded(
|
||||
unit, filename, line, section, section_line, name, value,
|
||||
min, max, ignoring,
|
||||
&t);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
assert(t <= UINT32_MAX);
|
||||
*ret = t;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline int config_parse_uint16_bounded(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *name,
|
||||
const char *value,
|
||||
uint16_t min,
|
||||
uint16_t max,
|
||||
bool ignoring,
|
||||
uint16_t *ret) {
|
||||
|
||||
unsigned t;
|
||||
int r;
|
||||
|
||||
r = config_parse_unsigned_bounded(
|
||||
unit, filename, line, section, section_line, name, value,
|
||||
min, max, ignoring,
|
||||
&t);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
assert(t <= UINT16_MAX);
|
||||
*ret = t;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline int config_parse_uint8_bounded(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *name,
|
||||
const char *value,
|
||||
uint8_t min,
|
||||
uint8_t max,
|
||||
bool ignoring,
|
||||
uint8_t *ret) {
|
||||
|
||||
unsigned t;
|
||||
int r;
|
||||
|
||||
r = config_parse_unsigned_bounded(
|
||||
unit, filename, line, section, section_line, name, value,
|
||||
min, max, ignoring,
|
||||
&t);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
assert(t <= UINT8_MAX);
|
||||
*ret = t;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -37,33 +37,40 @@ int ip_protocol_from_name(const char *name) {
|
||||
return sc->id;
|
||||
}
|
||||
|
||||
int parse_ip_protocol(const char *s) {
|
||||
_cleanup_free_ char *str = NULL;
|
||||
int i, r;
|
||||
int parse_ip_protocol_full(const char *s, bool relaxed) {
|
||||
int r, p;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (isempty(s))
|
||||
return IPPROTO_IP;
|
||||
|
||||
/* Do not use strdupa() here, as the input string may come from *
|
||||
* command line or config files. */
|
||||
str = strdup(s);
|
||||
if (!str)
|
||||
return -ENOMEM;
|
||||
|
||||
i = ip_protocol_from_name(ascii_strlower(str));
|
||||
if (i >= 0)
|
||||
return i;
|
||||
|
||||
r = safe_atoi(str, &i);
|
||||
if (r < 0)
|
||||
/* People commonly use lowercase protocol names, which we can look up very quickly, so let's try that
|
||||
* first. */
|
||||
r = ip_protocol_from_name(s);
|
||||
if (r >= 0)
|
||||
return r;
|
||||
|
||||
if (!ip_protocol_to_name(i))
|
||||
return -EINVAL;
|
||||
/* Do not use strdupa() here, as the input string may come from command line or config files. */
|
||||
_cleanup_free_ char *t = strdup(s);
|
||||
if (!t)
|
||||
return -ENOMEM;
|
||||
|
||||
return i;
|
||||
r = ip_protocol_from_name(ascii_strlower(t));
|
||||
if (r >= 0)
|
||||
return r;
|
||||
|
||||
r = safe_atoi(t, &p);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (p < 0)
|
||||
return -ERANGE;
|
||||
|
||||
/* If @relaxed, we don't check that we have a name for the protocol. */
|
||||
if (!relaxed && !ip_protocol_to_name(p))
|
||||
return -EPROTONOSUPPORT;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
const char *ip_protocol_to_tcp_udp(int id) {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
const char *ip_protocol_to_name(int id);
|
||||
int ip_protocol_from_name(const char *name);
|
||||
int parse_ip_protocol(const char *s);
|
||||
int parse_ip_protocol_full(const char *s, bool relaxed);
|
||||
static inline int parse_ip_protocol(const char *s) {
|
||||
return parse_ip_protocol_full(s, false);
|
||||
}
|
||||
|
||||
const char *ip_protocol_to_tcp_udp(int id);
|
||||
int ip_protocol_from_tcp_udp(const char *ip_protocol);
|
||||
|
||||
Reference in New Issue
Block a user