Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse: fix redefinition of gateway(4|6) #460

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
#define vxlan_offset(field) GUINT_TO_POINTER(offsetof(NetplanVxlan, field))

/* convenience macro to avoid strdup'ing a string into a field if it's already set. */
#define set_str_if_null(dst, src) { if (dst) {\
g_assert_cmpstr(src, ==, dst); \
} else { \
#define set_str_if_null(dst, src) { if (dst == NULL) {\
dst = g_strdup(src); \
} }

Expand Down Expand Up @@ -1455,6 +1453,10 @@ handle_gateway4(NetplanParser* npp, yaml_node_t* node, __unused const void* _, G
{
if (!is_ip4_address(scalar(node)))
return yaml_error(npp, node, error, "invalid IPv4 address '%s'", scalar(node));
if (npp->current.netdef->gateway4) {
g_free(npp->current.netdef->gateway4);
npp->current.netdef->gateway4 = NULL;
}
set_str_if_null(npp->current.netdef->gateway4, scalar(node));
mark_data_as_dirty(npp, &npp->current.netdef->gateway4);
g_warning("`gateway4` has been deprecated, use default routes instead.\n"
Expand All @@ -1467,6 +1469,10 @@ handle_gateway6(NetplanParser* npp, yaml_node_t* node, __unused const void* _, G
{
if (!is_ip6_address(scalar(node)))
return yaml_error(npp, node, error, "invalid IPv6 address '%s'", scalar(node));
if (npp->current.netdef->gateway6) {
g_free(npp->current.netdef->gateway6);
npp->current.netdef->gateway6 = NULL;
}
set_str_if_null(npp->current.netdef->gateway6, scalar(node));
mark_data_as_dirty(npp, &npp->current.netdef->gateway6);
g_warning("`gateway6` has been deprecated, use default routes instead.\n"
Expand Down
40 changes: 40 additions & 0 deletions tests/generator/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,3 +1825,43 @@ def test_wifi_access_points_overwriting(self):
psk="bbbbbbbb"
}
""")

def test_gateway6_redefinition_works(self):
self.generate('''network:
version: 2
ethernets:
engreen:
addresses: [2001:FFfe::1/62]
gateway6: 2001:FFfe::2''', confs={'b': '''network:
ethernets:
engreen:
gateway6: 2001:FFfe::34'''}, expect_fail=False)

self.assert_networkd({'engreen.network': '''[Match]
Name=engreen

[Network]
LinkLocalAddressing=ipv6
Address=2001:FFfe::1/62
Gateway=2001:FFfe::34
'''})

def test_gateway4_redefinition_works(self):
self.generate('''network:
version: 2
ethernets:
engreen:
addresses: [192.168.0.1/24]
gateway4: 192.168.0.123''', confs={'b': '''network:
ethernets:
engreen:
gateway4: 192.168.0.254'''}, expect_fail=False)

self.assert_networkd({'engreen.network': '''[Match]
Name=engreen

[Network]
LinkLocalAddressing=ipv6
Address=192.168.0.1/24
Gateway=192.168.0.254
'''})
Loading