diff options
-rwxr-xr-x | examples/python/gi/nm-wg-set | 2 | ||||
-rw-r--r-- | libnm-core/nm-keyfile.c | 8 | ||||
-rw-r--r-- | libnm-core/nm-setting-wireguard.c | 36 | ||||
-rw-r--r-- | libnm-core/nm-setting-wireguard.h | 5 | ||||
-rw-r--r-- | libnm-core/tests/test-setting.c | 3 |
5 files changed, 38 insertions, 16 deletions
diff --git a/examples/python/gi/nm-wg-set b/examples/python/gi/nm-wg-set index 9ee61c7dc4..32097f83f3 100755 --- a/examples/python/gi/nm-wg-set +++ b/examples/python/gi/nm-wg-set @@ -369,7 +369,7 @@ def do_set(nm_client, conn, argv): idx += 2 continue if peer and argv[idx] == 'endpoint': - peer.set_endpoint(argv_get_one(argv, idx + 1, None, idx)) + peer.set_endpoint(argv_get_one(argv, idx + 1, None, idx), True) idx += 2 continue if peer and argv[idx] == 'persistent-keepalive': diff --git a/libnm-core/nm-keyfile.c b/libnm-core/nm-keyfile.c index 4f5ec46260..3633281a3d 100644 --- a/libnm-core/nm-keyfile.c +++ b/libnm-core/nm-keyfile.c @@ -2972,16 +2972,12 @@ _read_setting_wireguard_peer (KeyfileReaderInfo *info) key = NM_WIREGUARD_PEER_ATTR_ENDPOINT; str = nm_keyfile_plugin_kf_get_string (info->keyfile, info->group, key, NULL); if (str && str[0]) { - nm_auto_unref_sockaddrendpoint NMSockAddrEndpoint *ep = NULL; - - ep = nm_sock_addr_endpoint_new (str); - if (!nm_sock_addr_endpoint_get_host (ep)) { + if (!nm_wireguard_peer_set_endpoint (peer, str, FALSE)) { if (!handle_warn (info, key, NM_KEYFILE_WARN_SEVERITY_WARN, _("key '%s.%s' is not not a valid endpoint"), info->group, key)) return; - } else - _nm_wireguard_peer_set_endpoint (peer, ep); + } } nm_clear_g_free (&str); diff --git a/libnm-core/nm-setting-wireguard.c b/libnm-core/nm-setting-wireguard.c index 154cc8a798..6785bf4a4b 100644 --- a/libnm-core/nm-setting-wireguard.c +++ b/libnm-core/nm-setting-wireguard.c @@ -524,26 +524,50 @@ _nm_wireguard_peer_set_endpoint (NMWireGuardPeer *self, * nm_wireguard_peer_set_endpoint: * @self: the unsealed #NMWireGuardPeer instance * @endpoint: the socket address endpoint to set or %NULL. + * @allow_invalid: if %TRUE, also invalid values are set. + * If %FALSE, the function does nothing for invalid @endpoint + * arguments. * * Sets or clears the endpoint of @self. * * It is a bug trying to modify a sealed #NMWireGuardPeer instance. * + * Returns: %TRUE if the endpoint is %NULL or valid. For an + * invalid @endpoint argument, %FALSE is returned. Depending + * on @allow_invalid, the instance will be modified. + * * Since: 1.16 */ -void +gboolean nm_wireguard_peer_set_endpoint (NMWireGuardPeer *self, - const char *endpoint) + const char *endpoint, + gboolean allow_invalid) { NMSockAddrEndpoint *old; + NMSockAddrEndpoint *new; + gboolean is_valid; - g_return_if_fail (NM_IS_WIREGUARD_PEER (self, FALSE)); + g_return_val_if_fail (NM_IS_WIREGUARD_PEER (self, FALSE), FALSE); + + if (!endpoint) { + nm_clear_pointer (&self->endpoint, nm_sock_addr_endpoint_unref); + return TRUE; + } + + new = nm_sock_addr_endpoint_new (endpoint); + + is_valid = (nm_sock_addr_endpoint_get_host (new) != NULL); + + if ( !allow_invalid + && !is_valid) { + nm_sock_addr_endpoint_unref (new); + return FALSE; + } old = self->endpoint; - self->endpoint = endpoint - ? nm_sock_addr_endpoint_new (endpoint) - : NULL; + self->endpoint = new; nm_sock_addr_endpoint_unref (old); + return is_valid; } /** diff --git a/libnm-core/nm-setting-wireguard.h b/libnm-core/nm-setting-wireguard.h index 5cf5c1f3c4..017eb1f6d3 100644 --- a/libnm-core/nm-setting-wireguard.h +++ b/libnm-core/nm-setting-wireguard.h @@ -87,8 +87,9 @@ void nm_wireguard_peer_set_persistent_keepalive (NMWireGuardPeer *self, NM_AVAILABLE_IN_1_16 const char *nm_wireguard_peer_get_endpoint (const NMWireGuardPeer *self); NM_AVAILABLE_IN_1_16 -void nm_wireguard_peer_set_endpoint (NMWireGuardPeer *self, - const char *endpoint); +gboolean nm_wireguard_peer_set_endpoint (NMWireGuardPeer *self, + const char *endpoint, + gboolean allow_invalid); NM_AVAILABLE_IN_1_16 guint nm_wireguard_peer_get_allowed_ips_len (const NMWireGuardPeer *self); diff --git a/libnm-core/tests/test-setting.c b/libnm-core/tests/test-setting.c index bbcff8778f..2011273a2b 100644 --- a/libnm-core/tests/test-setting.c +++ b/libnm-core/tests/test-setting.c @@ -2078,7 +2078,8 @@ _rndt_wg_peers_create (void) nm_wireguard_peer_set_persistent_keepalive (peer, nmtst_rand_select ((guint32) 0, nmtst_get_rand_int ())); - nm_wireguard_peer_set_endpoint (peer, nmtst_rand_select (s_endpoint, NULL)); + if (!nm_wireguard_peer_set_endpoint (peer, nmtst_rand_select (s_endpoint, NULL), TRUE)) + g_assert_not_reached (); n_aip = nmtst_rand_select (0, nmtst_get_rand_int () % 10); for (i_aip = 0; i_aip < n_aip; i_aip++) { |