Skip to content

Commit 89fa9a6

Browse files
committed
networkd: add shared parser for mud urls
The same buggy code was triplicated…
1 parent bc1f27f commit 89fa9a6

File tree

5 files changed

+58
-77
lines changed

5 files changed

+58
-77
lines changed

src/network/networkd-dhcp4.c

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <linux/if.h>
66
#include <linux/if_arp.h>
77

8-
#include "escape.h"
98
#include "alloc-util.h"
109
#include "dhcp-client-internal.h"
1110
#include "hostname-setup.h"
@@ -26,7 +25,6 @@
2625
#include "string-table.h"
2726
#include "strv.h"
2827
#include "sysctl-util.h"
29-
#include "web-util.h"
3028

3129
static int dhcp4_request_address_and_routes(Link *link, bool announce);
3230
static int dhcp4_remove_all(Link *link);
@@ -1733,34 +1731,12 @@ int config_parse_dhcp_mud_url(
17331731
void *data,
17341732
void *userdata) {
17351733

1736-
_cleanup_free_ char *unescaped = NULL;
17371734
Network *network = data;
1738-
ssize_t l;
17391735

1740-
assert(filename);
1741-
assert(lvalue);
1742-
assert(rvalue);
1743-
1744-
if (isempty(rvalue)) {
1745-
network->dhcp_mudurl = mfree(network->dhcp_mudurl);
1746-
return 0;
1747-
}
1748-
1749-
l = cunescape(rvalue, 0, &unescaped);
1750-
if (l < 0) {
1751-
log_syntax(unit, LOG_WARNING, filename, line, l,
1752-
"Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1753-
return 0;
1754-
}
1755-
1756-
if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
1757-
log_syntax(unit, LOG_WARNING, filename, line, 0,
1758-
"Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1759-
1760-
return 0;
1761-
}
1736+
assert(network);
17621737

1763-
return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
1738+
return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
1739+
&network->dhcp_mudurl);
17641740
}
17651741

17661742
int config_parse_dhcp_fallback_lease_lifetime(const char *unit,

src/network/networkd-dhcp6.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "sd-dhcp6-client.h"
1111

12-
#include "escape.h"
1312
#include "hashmap.h"
1413
#include "hostname-setup.h"
1514
#include "hostname-util.h"
@@ -24,7 +23,6 @@
2423
#include "string-table.h"
2524
#include "string-util.h"
2625
#include "radv-internal.h"
27-
#include "web-util.h"
2826

2927
bool link_dhcp6_with_address_enabled(Link *link) {
3028
if (!link_dhcp6_enabled(link))
@@ -1812,33 +1810,12 @@ int config_parse_dhcp6_mud_url(
18121810
void *data,
18131811
void *userdata) {
18141812

1815-
_cleanup_free_ char *unescaped = NULL;
18161813
Network *network = data;
1817-
ssize_t l;
18181814

1819-
assert(filename);
1820-
assert(lvalue);
1821-
assert(rvalue);
1822-
1823-
if (isempty(rvalue)) {
1824-
network->dhcp6_mudurl = mfree(network->dhcp6_mudurl);
1825-
return 0;
1826-
}
1827-
1828-
l = cunescape(rvalue, 0, &unescaped);
1829-
if (l < 0) {
1830-
log_syntax(unit, LOG_WARNING, filename, line, l,
1831-
"Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
1832-
return 0;
1833-
}
1834-
1835-
if (!http_url_is_valid(unescaped) || strlen(unescaped) > UINT8_MAX) {
1836-
log_syntax(unit, LOG_WARNING, filename, line, 0,
1837-
"Failed to parse MUD URL '%s', ignoring: %m", rvalue);
1838-
return 0;
1839-
}
1815+
assert(network);
18401816

1841-
return free_and_replace(network->dhcp6_mudurl, unescaped);
1817+
return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
1818+
&network->dhcp6_mudurl);
18421819
}
18431820

18441821
DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,

src/network/networkd-lldp-tx.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "alloc-util.h"
99
#include "env-file.h"
10-
#include "escape.h"
1110
#include "fd-util.h"
1211
#include "hostname-util.h"
1312
#include "missing_network.h"
@@ -21,7 +20,6 @@
2120
#include "string-util.h"
2221
#include "strv.h"
2322
#include "unaligned.h"
24-
#include "web-util.h"
2523

2624
/* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
2725
#define LLDP_TX_FAST_INIT 4U
@@ -428,29 +426,12 @@ int config_parse_lldp_mud(
428426
void *data,
429427
void *userdata) {
430428

431-
_cleanup_free_ char *unescaped = NULL;
432-
Network *n = data;
433-
ssize_t l;
434-
435-
assert(filename);
436-
assert(lvalue);
437-
assert(rvalue);
438-
439-
l = cunescape(rvalue, 0, &unescaped);
440-
if (l < 0) {
441-
log_syntax(unit, LOG_WARNING, filename, line, l,
442-
"Failed to Failed to unescape LLDP MUD URL, ignoring: %s", rvalue);
443-
return 0;
444-
}
445-
446-
if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
447-
log_syntax(unit, LOG_WARNING, filename, line, 0,
448-
"Failed to parse LLDP MUD URL '%s', ignoring: %m", rvalue);
429+
Network *network = data;
449430

450-
return 0;
451-
}
431+
assert(network);
452432

453-
return free_and_replace(n->lldp_mud, unescaped);
433+
return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
434+
&network->lldp_mud);
454435
}
455436

456437
static const char * const lldp_emit_table[_LLDP_EMIT_MAX] = {

src/network/networkd-util.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
#include "condition.h"
44
#include "conf-parser.h"
5+
#include "escape.h"
56
#include "networkd-link.h"
67
#include "networkd-util.h"
78
#include "parse-util.h"
89
#include "string-table.h"
910
#include "string-util.h"
10-
#include "util.h"
11+
#include "web-util.h"
1112

1213
static const char* const address_family_table[_ADDRESS_FAMILY_MAX] = {
1314
[ADDRESS_FAMILY_NO] = "no",
@@ -161,6 +162,41 @@ int config_parse_ip_masquerade(
161162
return 0;
162163
}
163164

165+
int config_parse_mud_url(
166+
const char *unit,
167+
const char *filename,
168+
unsigned line,
169+
const char *section,
170+
unsigned section_line,
171+
const char *lvalue,
172+
int ltype,
173+
const char *rvalue,
174+
char **ret) {
175+
176+
assert(filename);
177+
assert(lvalue);
178+
assert(rvalue);
179+
assert(ret);
180+
181+
_cleanup_free_ char *unescaped = NULL;
182+
ssize_t l;
183+
184+
l = cunescape(rvalue, 0, &unescaped);
185+
if (l < 0) {
186+
log_syntax(unit, LOG_WARNING, filename, line, l,
187+
"Failed to unescape MUD URL, ignoring: %s", rvalue);
188+
return 0;
189+
}
190+
191+
if (l > UINT8_MAX || !http_url_is_valid(unescaped)) {
192+
log_syntax(unit, LOG_WARNING, filename, line, 0,
193+
"Invalid MUD URL, ignoring: %s", rvalue);
194+
return 0;
195+
}
196+
197+
return free_and_replace(*ret, unescaped);
198+
}
199+
164200
/* Router lifetime can be set with netlink interface since kernel >= 4.5
165201
* so for the supported kernel we don't need to expire routes in userspace */
166202
int kernel_route_expiration_supported(void) {

src/network/networkd-util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ CONFIG_PARSER_PROTOTYPE(config_parse_link_local_address_family);
2323
CONFIG_PARSER_PROTOTYPE(config_parse_address_family_with_kernel);
2424
CONFIG_PARSER_PROTOTYPE(config_parse_ip_masquerade);
2525

26+
int config_parse_mud_url(
27+
const char *unit,
28+
const char *filename,
29+
unsigned line,
30+
const char *section,
31+
unsigned section_line,
32+
const char *lvalue,
33+
int ltype,
34+
const char *rvalue,
35+
char **ret);
36+
2637
const char *address_family_to_string(AddressFamily b) _const_;
2738
AddressFamily address_family_from_string(const char *s) _pure_;
2839

0 commit comments

Comments
 (0)