From be034a1f3f2fc757ff011f054abb1606e9e8febc Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 7 Jan 2025 08:28:01 +0100 Subject: [PATCH] device: simplify the nm_utils_complete_generic() machinery The point is to get rid of device/connection type specific arguments, to eventually be able to complete the connection on AddAndActivate before knowing which factory is going to take care of creating the device. Aside from that, the whole thing is pretty awful -- with complicated macros and variadic argument (ugh). Let's get rid of that. --- src/core/NetworkManagerUtils.c | 48 ++++++++-------- src/core/NetworkManagerUtils.h | 62 +++------------------ src/core/devices/adsl/nm-device-adsl.c | 3 +- src/core/devices/bluetooth/nm-device-bt.c | 3 +- src/core/devices/nm-device-6lowpan.c | 3 +- src/core/devices/nm-device-bond.c | 3 +- src/core/devices/nm-device-bridge.c | 3 +- src/core/devices/nm-device-dummy.c | 16 +++--- src/core/devices/nm-device-ethernet.c | 6 +- src/core/devices/nm-device-infiniband.c | 3 +- src/core/devices/nm-device-ip-tunnel.c | 3 +- src/core/devices/nm-device-loopback.c | 16 +++--- src/core/devices/nm-device-macvlan.c | 3 +- src/core/devices/nm-device-tun.c | 3 +- src/core/devices/nm-device-vlan.c | 3 +- src/core/devices/nm-device-vrf.c | 3 +- src/core/devices/nm-device-vxlan.c | 3 +- src/core/devices/nm-device-wpan.c | 3 +- src/core/devices/team/nm-device-team.c | 3 +- src/core/devices/wifi/nm-device-iwd-p2p.c | 3 +- src/core/devices/wifi/nm-device-iwd.c | 3 +- src/core/devices/wifi/nm-device-olpc-mesh.c | 3 +- src/core/devices/wifi/nm-device-wifi-p2p.c | 3 +- src/core/devices/wifi/nm-device-wifi.c | 3 +- src/core/devices/wwan/nm-modem-broadband.c | 6 +- src/core/nm-manager.c | 3 +- 26 files changed, 71 insertions(+), 143 deletions(-) diff --git a/src/core/NetworkManagerUtils.c b/src/core/NetworkManagerUtils.c index d4180b0bc2..6c9e2f3d19 100644 --- a/src/core/NetworkManagerUtils.c +++ b/src/core/NetworkManagerUtils.c @@ -250,23 +250,19 @@ nm_utils_ppp_ip_methods_enabled(NMConnection *connection, /*****************************************************************************/ void -_nm_utils_complete_generic_with_params(NMPlatform *platform, - NMConnection *connection, - const char *ctype, - NMConnection *const *existing_connections, - const char *preferred_id, - const char *fallback_id_prefix, - const char *ifname_prefix, - const char *ifname, - ...) +nm_utils_complete_generic(NMPlatform *platform, + NMConnection *connection, + const char *ctype, + NMConnection *const *existing_connections, + const char *preferred_id, + const char *fallback_id_prefix, + const char *ifname_prefix, + const char *ifname) { NMSettingConnection *s_con; char *id; char *generated_ifname; gs_unref_hashtable GHashTable *parameters = NULL; - va_list ap; - const char *p_val; - const char *p_key; g_assert(fallback_id_prefix); g_return_if_fail(ifname_prefix == NULL || ifname == NULL); @@ -301,20 +297,22 @@ _nm_utils_complete_generic_with_params(NMPlatform *platform, g_free(generated_ifname); } - /* Normalize */ - va_start(ap, ifname); - while ((p_key = va_arg(ap, const char *))) { - p_val = va_arg(ap, const char *); - if (!p_val) { - if (parameters) - g_hash_table_remove(parameters, p_key); - continue; - } - if (!parameters) - parameters = g_hash_table_new(nm_str_hash, g_str_equal); - g_hash_table_insert(parameters, (char *) p_key, (char *) p_val); + if (nm_connection_get_setting_adsl(connection) || nm_connection_get_setting_cdma(connection) + || nm_connection_get_setting_olpc_mesh(connection) + || nm_connection_get_setting_pppoe(connection) + || nm_connection_get_setting_vpn(connection)) { + parameters = g_hash_table_new(nm_str_hash, g_str_equal); + g_hash_table_insert(parameters, + NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD, + NM_SETTING_IP6_CONFIG_METHOD_IGNORE); + } else { + parameters = NULL; } - va_end(ap); + + /* We ignore the result, because the caller validates the connection. + * The only reason we do a normalization attempt here is + * NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD. + * Could we perhaps, one day, get rid of it? */ nm_connection_normalize(connection, parameters, NULL, NULL); } diff --git a/src/core/NetworkManagerUtils.h b/src/core/NetworkManagerUtils.h index 7d8afe5a2b..01f5bdb07d 100644 --- a/src/core/NetworkManagerUtils.h +++ b/src/core/NetworkManagerUtils.h @@ -23,60 +23,14 @@ void nm_utils_ppp_ip_methods_enabled(NMConnection *connection, gboolean *out_ip4_enabled, gboolean *out_ip6_enabled); -void _nm_utils_complete_generic_with_params(NMPlatform *platform, - NMConnection *connection, - const char *ctype, - NMConnection *const *existing_connections, - const char *preferred_id, - const char *fallback_id_prefix, - const char *ifname_prefix, - const char *ifname, - ...) G_GNUC_NULL_TERMINATED; - -#define nm_utils_complete_generic_with_params(platform, \ - connection, \ - ctype, \ - existing_connections, \ - preferred_id, \ - fallback_id_prefix, \ - ifname_prefix, \ - ifname, \ - ...) \ - _nm_utils_complete_generic_with_params(platform, \ - connection, \ - ctype, \ - existing_connections, \ - preferred_id, \ - fallback_id_prefix, \ - ifname_prefix, \ - ifname, \ - ##__VA_ARGS__, \ - NULL) - -static inline void -nm_utils_complete_generic(NMPlatform *platform, - NMConnection *connection, - const char *ctype, - NMConnection *const *existing_connections, - const char *preferred_id, - const char *fallback_id_prefix, - const char *ifname_prefix, - const char *ifname, - gboolean default_enable_ipv6) -{ - nm_utils_complete_generic_with_params(platform, - connection, - ctype, - existing_connections, - preferred_id, - fallback_id_prefix, - ifname_prefix, - ifname, - NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD, - default_enable_ipv6 - ? NM_SETTING_IP6_CONFIG_METHOD_AUTO - : NM_SETTING_IP6_CONFIG_METHOD_IGNORE); -} +void nm_utils_complete_generic(NMPlatform *platform, + NMConnection *connection, + const char *ctype, + NMConnection *const *existing_connections, + const char *preferred_id, + const char *fallback_id_prefix, + const char *ifname_prefix, + const char *ifname); typedef gboolean(NMUtilsMatchFilterFunc)(NMConnection *connection, gpointer user_data); diff --git a/src/core/devices/adsl/nm-device-adsl.c b/src/core/devices/adsl/nm-device-adsl.c index 5c3432b286..a6dc63269c 100644 --- a/src/core/devices/adsl/nm-device-adsl.c +++ b/src/core/devices/adsl/nm-device-adsl.c @@ -117,8 +117,7 @@ complete_connection(NMDevice *device, NULL, _("ADSL connection"), NULL, - NULL, - FALSE); /* No IPv6 yet by default */ + NULL); return TRUE; } diff --git a/src/core/devices/bluetooth/nm-device-bt.c b/src/core/devices/bluetooth/nm-device-bt.c index ce110aa0e6..4406bcf4eb 100644 --- a/src/core/devices/bluetooth/nm-device-bt.c +++ b/src/core/devices/bluetooth/nm-device-bt.c @@ -404,8 +404,7 @@ complete_connection(NMDevice *device, preferred, fallback_prefix, NULL, - NULL, - is_dun ? FALSE : TRUE); /* No IPv6 yet for DUN */ + NULL); setting_bdaddr = nm_setting_bluetooth_get_bdaddr(s_bt); if (setting_bdaddr) { diff --git a/src/core/devices/nm-device-6lowpan.c b/src/core/devices/nm-device-6lowpan.c index f4b8791cbc..3dabcb9bdc 100644 --- a/src/core/devices/nm-device-6lowpan.c +++ b/src/core/devices/nm-device-6lowpan.c @@ -161,8 +161,7 @@ complete_connection(NMDevice *device, NULL, _("6LOWPAN connection"), NULL, - NULL, - TRUE); + NULL); s_6lowpan = NM_SETTING_6LOWPAN(nm_connection_get_setting(connection, NM_TYPE_SETTING_6LOWPAN)); if (!s_6lowpan) { diff --git a/src/core/devices/nm-device-bond.c b/src/core/devices/nm-device-bond.c index 49ca156d5f..53b324660e 100644 --- a/src/core/devices/nm-device-bond.c +++ b/src/core/devices/nm-device-bond.c @@ -94,8 +94,7 @@ complete_connection(NMDevice *device, NULL, _("Bond connection"), "bond", - NULL, - TRUE); + NULL); _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_BOND); diff --git a/src/core/devices/nm-device-bridge.c b/src/core/devices/nm-device-bridge.c index 6f06dbf490..7c34fde07f 100644 --- a/src/core/devices/nm-device-bridge.c +++ b/src/core/devices/nm-device-bridge.c @@ -164,8 +164,7 @@ complete_connection(NMDevice *device, NULL, _("Bridge connection"), "bridge", - NULL, - TRUE); + NULL); _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_BRIDGE); diff --git a/src/core/devices/nm-device-dummy.c b/src/core/devices/nm-device-dummy.c index b7c4106a0a..1bc5447f5c 100644 --- a/src/core/devices/nm-device-dummy.c +++ b/src/core/devices/nm-device-dummy.c @@ -48,14 +48,14 @@ complete_connection(NMDevice *device, NMConnection *const *existing_connections, GError **error) { - nm_utils_complete_generic_with_params(nm_device_get_platform(device), - connection, - NM_SETTING_DUMMY_SETTING_NAME, - existing_connections, - NULL, - _("Dummy connection"), - NULL, - nm_device_get_ip_iface(device)); + nm_utils_complete_generic(nm_device_get_platform(device), + connection, + NM_SETTING_DUMMY_SETTING_NAME, + existing_connections, + NULL, + _("Dummy connection"), + NULL, + nm_device_get_ip_iface(device)); _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_DUMMY); diff --git a/src/core/devices/nm-device-ethernet.c b/src/core/devices/nm-device-ethernet.c index fc47e1385c..4034fdaad6 100644 --- a/src/core/devices/nm-device-ethernet.c +++ b/src/core/devices/nm-device-ethernet.c @@ -1640,8 +1640,7 @@ complete_connection(NMDevice *device, NULL, _("Veth connection"), "veth", - NULL, - TRUE); + NULL); s_veth = _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_VETH); @@ -1698,8 +1697,7 @@ complete_connection(NMDevice *device, NULL, s_pppoe ? _("PPPoE connection") : _("Wired connection"), NULL, - nm_setting_wired_get_mac_address(s_wired) ? NULL : nm_device_get_iface(device), - s_pppoe ? FALSE : TRUE); /* No IPv6 by default yet for PPPoE */ + nm_setting_wired_get_mac_address(s_wired) ? NULL : nm_device_get_iface(device)); return TRUE; } diff --git a/src/core/devices/nm-device-infiniband.c b/src/core/devices/nm-device-infiniband.c index be6ae583d5..a5a82b94f7 100644 --- a/src/core/devices/nm-device-infiniband.c +++ b/src/core/devices/nm-device-infiniband.c @@ -159,8 +159,7 @@ complete_connection(NMDevice *device, NULL, _("InfiniBand connection"), NULL, - nm_setting_infiniband_get_mac_address(s_infiniband) ? NULL : nm_device_get_iface(device), - TRUE); + nm_setting_infiniband_get_mac_address(s_infiniband) ? NULL : nm_device_get_iface(device)); if (!nm_setting_infiniband_get_transport_mode(s_infiniband)) g_object_set(G_OBJECT(s_infiniband), diff --git a/src/core/devices/nm-device-ip-tunnel.c b/src/core/devices/nm-device-ip-tunnel.c index 26d83e19d2..2ecfe4531f 100644 --- a/src/core/devices/nm-device-ip-tunnel.c +++ b/src/core/devices/nm-device-ip-tunnel.c @@ -402,8 +402,7 @@ complete_connection(NMDevice *device, NULL, _("IP tunnel connection"), NULL, - NULL, - TRUE); + NULL); s_ip_tunnel = nm_connection_get_setting_ip_tunnel(connection); if (!s_ip_tunnel) { diff --git a/src/core/devices/nm-device-loopback.c b/src/core/devices/nm-device-loopback.c index ec72aa963d..268e1fb1e9 100644 --- a/src/core/devices/nm-device-loopback.c +++ b/src/core/devices/nm-device-loopback.c @@ -59,14 +59,14 @@ complete_connection(NMDevice *device, NMConnection *const *existing_connections, GError **error) { - nm_utils_complete_generic_with_params(nm_device_get_platform(device), - connection, - NM_SETTING_LOOPBACK_SETTING_NAME, - existing_connections, - NULL, - _("Loopback connection"), - NULL, - nm_device_get_ip_iface(device)); + nm_utils_complete_generic(nm_device_get_platform(device), + connection, + NM_SETTING_LOOPBACK_SETTING_NAME, + existing_connections, + NULL, + _("Loopback connection"), + NULL, + nm_device_get_ip_iface(device)); _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_LOOPBACK); diff --git a/src/core/devices/nm-device-macvlan.c b/src/core/devices/nm-device-macvlan.c index daf2554da7..8300023662 100644 --- a/src/core/devices/nm-device-macvlan.c +++ b/src/core/devices/nm-device-macvlan.c @@ -365,8 +365,7 @@ complete_connection(NMDevice *device, NULL, _("MACVLAN connection"), NULL, - NULL, - TRUE); + NULL); s_macvlan = nm_connection_get_setting_macvlan(connection); if (!s_macvlan) { diff --git a/src/core/devices/nm-device-tun.c b/src/core/devices/nm-device-tun.c index 28b03cec12..faab86d0a9 100644 --- a/src/core/devices/nm-device-tun.c +++ b/src/core/devices/nm-device-tun.c @@ -143,8 +143,7 @@ complete_connection(NMDevice *device, NULL, _("TUN connection"), NULL, - NULL, - TRUE); + NULL); s_tun = nm_connection_get_setting_tun(connection); if (!s_tun) { diff --git a/src/core/devices/nm-device-vlan.c b/src/core/devices/nm-device-vlan.c index 08e6c30afc..59a429ca7f 100644 --- a/src/core/devices/nm-device-vlan.c +++ b/src/core/devices/nm-device-vlan.c @@ -379,8 +379,7 @@ complete_connection(NMDevice *device, NULL, _("VLAN connection"), NULL, - NULL, - TRUE); + NULL); s_vlan = nm_connection_get_setting_vlan(connection); if (!s_vlan) { diff --git a/src/core/devices/nm-device-vrf.c b/src/core/devices/nm-device-vrf.c index 48365ac68d..7dfd6504a1 100644 --- a/src/core/devices/nm-device-vrf.c +++ b/src/core/devices/nm-device-vrf.c @@ -184,8 +184,7 @@ complete_connection(NMDevice *device, NULL, _("VRF connection"), NULL, - NULL, - TRUE); + NULL); s_vrf = _nm_connection_get_setting(connection, NM_TYPE_SETTING_VRF); if (!s_vrf) { diff --git a/src/core/devices/nm-device-vxlan.c b/src/core/devices/nm-device-vxlan.c index 9c54791462..4058287c00 100644 --- a/src/core/devices/nm-device-vxlan.c +++ b/src/core/devices/nm-device-vxlan.c @@ -384,8 +384,7 @@ complete_connection(NMDevice *device, NULL, _("VXLAN connection"), NULL, - NULL, - TRUE); + NULL); s_vxlan = nm_connection_get_setting_vxlan(connection); if (!s_vxlan) { diff --git a/src/core/devices/nm-device-wpan.c b/src/core/devices/nm-device-wpan.c index 7885355db7..67f845c868 100644 --- a/src/core/devices/nm-device-wpan.c +++ b/src/core/devices/nm-device-wpan.c @@ -53,8 +53,7 @@ complete_connection(NMDevice *device, NULL, _("WPAN connection"), NULL, - NULL, - TRUE); + NULL); s_wpan = NM_SETTING_WPAN(nm_connection_get_setting(connection, NM_TYPE_SETTING_WPAN)); if (!s_wpan) { diff --git a/src/core/devices/team/nm-device-team.c b/src/core/devices/team/nm-device-team.c index a4c77f7f49..40779c8997 100644 --- a/src/core/devices/team/nm-device-team.c +++ b/src/core/devices/team/nm-device-team.c @@ -130,8 +130,7 @@ complete_connection(NMDevice *device, NULL, _("Team connection"), "team", - NULL, - TRUE); + NULL); _nm_connection_ensure_setting(connection, NM_TYPE_SETTING_TEAM); diff --git a/src/core/devices/wifi/nm-device-iwd-p2p.c b/src/core/devices/wifi/nm-device-iwd-p2p.c index fadc672233..184c8ec583 100644 --- a/src/core/devices/wifi/nm-device-iwd-p2p.c +++ b/src/core/devices/wifi/nm-device-iwd-p2p.c @@ -301,8 +301,7 @@ complete_connection(NMDevice *device, setting_name, setting_name, NULL, - NULL, - TRUE); + NULL); return TRUE; } diff --git a/src/core/devices/wifi/nm-device-iwd.c b/src/core/devices/wifi/nm-device-iwd.c index a7ea14e4e4..fa6e2f9d3a 100644 --- a/src/core/devices/wifi/nm-device-iwd.c +++ b/src/core/devices/wifi/nm-device-iwd.c @@ -1074,8 +1074,7 @@ complete_connection(NMDevice *device, ssid_utf8, ssid_utf8, NULL, - NULL, - TRUE); + NULL); if (hidden) g_object_set(s_wifi, NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL); diff --git a/src/core/devices/wifi/nm-device-olpc-mesh.c b/src/core/devices/wifi/nm-device-olpc-mesh.c index 8e1e779b34..b62dc3115b 100644 --- a/src/core/devices/wifi/nm-device-olpc-mesh.c +++ b/src/core/devices/wifi/nm-device-olpc-mesh.c @@ -111,8 +111,7 @@ complete_connection(NMDevice *device, NULL, _("Mesh"), NULL, - NULL, - FALSE); /* No IPv6 by default */ + NULL); return TRUE; } diff --git a/src/core/devices/wifi/nm-device-wifi-p2p.c b/src/core/devices/wifi/nm-device-wifi-p2p.c index f06383b102..957a2df64f 100644 --- a/src/core/devices/wifi/nm-device-wifi-p2p.c +++ b/src/core/devices/wifi/nm-device-wifi-p2p.c @@ -319,8 +319,7 @@ complete_connection(NMDevice *device, setting_name, setting_name, NULL, - NULL, - TRUE); + NULL); return TRUE; } diff --git a/src/core/devices/wifi/nm-device-wifi.c b/src/core/devices/wifi/nm-device-wifi.c index 7b17f55c2d..06eee14245 100644 --- a/src/core/devices/wifi/nm-device-wifi.c +++ b/src/core/devices/wifi/nm-device-wifi.c @@ -1296,8 +1296,7 @@ complete_connection(NMDevice *device, ssid_utf8, ssid_utf8, NULL, - nm_setting_wireless_get_mac_address(s_wifi) ? NULL : nm_device_get_iface(device), - TRUE); + nm_setting_wireless_get_mac_address(s_wifi) ? NULL : nm_device_get_iface(device)); if (hidden) g_object_set(s_wifi, NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL); diff --git a/src/core/devices/wwan/nm-modem-broadband.c b/src/core/devices/wwan/nm-modem-broadband.c index 8ea16ee75d..018e53066c 100644 --- a/src/core/devices/wwan/nm-modem-broadband.c +++ b/src/core/devices/wwan/nm-modem-broadband.c @@ -917,8 +917,7 @@ complete_connection(NMModem *modem, NULL, _("GSM connection"), NULL, - NULL, - TRUE); + NULL); return TRUE; } @@ -938,8 +937,7 @@ complete_connection(NMModem *modem, NULL, _("CDMA connection"), NULL, - iface, - TRUE); + iface); return TRUE; } diff --git a/src/core/nm-manager.c b/src/core/nm-manager.c index a4a43b37cc..71d8f83cf7 100644 --- a/src/core/nm-manager.c +++ b/src/core/nm-manager.c @@ -6952,8 +6952,7 @@ impl_manager_add_and_activate_connection(NMDBusObject *obj, NULL, _("VPN connection"), NULL, - NULL, - FALSE); /* No IPv6 by default for now */ + NULL); } else { conns = nm_settings_connections_array_to_connections( nm_settings_get_connections(priv->settings, NULL),