networkd: add shared parser for mud urls

The same buggy code was triplicated…
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-07-09 15:13:12 +02:00
parent bc1f27ff55
commit 89fa9a6b7b
5 changed files with 58 additions and 77 deletions
+37 -1
View File
@@ -2,12 +2,13 @@
#include "condition.h"
#include "conf-parser.h"
#include "escape.h"
#include "networkd-link.h"
#include "networkd-util.h"
#include "parse-util.h"
#include "string-table.h"
#include "string-util.h"
#include "util.h"
#include "web-util.h"
static const char* const address_family_table[_ADDRESS_FAMILY_MAX] = {
[ADDRESS_FAMILY_NO] = "no",
@@ -161,6 +162,41 @@ int config_parse_ip_masquerade(
return 0;
}
int config_parse_mud_url(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
char **ret) {
assert(filename);
assert(lvalue);
assert(rvalue);
assert(ret);
_cleanup_free_ char *unescaped = NULL;
ssize_t l;
l = cunescape(rvalue, 0, &unescaped);
if (l < 0) {
log_syntax(unit, LOG_WARNING, filename, line, l,
"Failed to unescape MUD URL, ignoring: %s", rvalue);
return 0;
}
if (l > UINT8_MAX || !http_url_is_valid(unescaped)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid MUD URL, ignoring: %s", rvalue);
return 0;
}
return free_and_replace(*ret, unescaped);
}
/* Router lifetime can be set with netlink interface since kernel >= 4.5
* so for the supported kernel we don't need to expire routes in userspace */
int kernel_route_expiration_supported(void) {