diff options
Diffstat (limited to 'src/libsystemd/sd-network')
-rw-r--r-- | src/libsystemd/sd-network/network-util.c | 47 | ||||
-rw-r--r-- | src/libsystemd/sd-network/network-util.h | 11 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index 08ed942638..0addabe10a 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -26,6 +26,7 @@ bool network_is_online(void) { } static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { + [LINK_OPERSTATE_MISSING] = "missing", [LINK_OPERSTATE_OFF] = "off", [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", [LINK_OPERSTATE_DORMANT] = "dormant", @@ -56,3 +57,49 @@ static const char* const link_address_state_table[_LINK_ADDRESS_STATE_MAX] = { }; DEFINE_STRING_TABLE_LOOKUP(link_address_state, LinkAddressState); + +int parse_operational_state_range(const char *str, LinkOperationalStateRange *out) { + LinkOperationalStateRange range = { _LINK_OPERSTATE_INVALID, _LINK_OPERSTATE_INVALID }; + _cleanup_free_ const char *min = NULL; + const char *p; + + assert(str); + assert(out); + + p = strchr(str, ':'); + if (p) { + min = strndup(str, p - str); + + if (!isempty(p + 1)) { + range.max = link_operstate_from_string(p + 1); + if (range.max < 0) + return -EINVAL; + } + } else + min = strdup(str); + + if (!min) + return -ENOMEM; + + if (!isempty(min)) { + range.min = link_operstate_from_string(min); + if (range.min < 0) + return -EINVAL; + } + + /* Fail on empty strings. */ + if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID) + return -EINVAL; + + if (range.min == _LINK_OPERSTATE_INVALID) + range.min = LINK_OPERSTATE_MISSING; + if (range.max == _LINK_OPERSTATE_INVALID) + range.max = LINK_OPERSTATE_ROUTABLE; + + if (range.min > range.max) + return -EINVAL; + + *out = range; + + return 0; +} diff --git a/src/libsystemd/sd-network/network-util.h b/src/libsystemd/sd-network/network-util.h index a19435393d..425d192f64 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -8,6 +8,7 @@ bool network_is_online(void); typedef enum LinkOperationalState { + LINK_OPERSTATE_MISSING, LINK_OPERSTATE_OFF, LINK_OPERSTATE_NO_CARRIER, LINK_OPERSTATE_DORMANT, @@ -47,3 +48,13 @@ LinkCarrierState link_carrier_state_from_string(const char *s) _pure_; const char* link_address_state_to_string(LinkAddressState s) _const_; LinkAddressState link_address_state_from_string(const char *s) _pure_; + +typedef struct LinkOperationalStateRange { + LinkOperationalState min; + LinkOperationalState max; +} LinkOperationalStateRange; + +#define LINK_OPERSTATE_RANGE_DEFAULT (LinkOperationalStateRange) { LINK_OPERSTATE_DEGRADED, \ + LINK_OPERSTATE_ROUTABLE } + +int parse_operational_state_range(const char *str, LinkOperationalStateRange *out); |