diff options
author | Thomas Haller <thaller@redhat.com> | 2021-04-23 11:15:19 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-04-27 08:03:39 +0200 |
commit | e99d68818fcc1594b348aeaeb6e1706b7273ebba (patch) | |
tree | 542cde5345340256a5cc2ea81ed6faaf1b8a1aa4 | |
parent | 59bca17c3ce1a22c47cd08283f424cd337e89796 (diff) | |
download | NetworkManager-e99d68818fcc1594b348aeaeb6e1706b7273ebba.tar.gz |
dhcp: refactor NMDhcpClient to use client flags
The DHCP client has potentially a large number of options,
including boolean options (flags). It is cumbersome to implement
them one by one. Instead, make more prominent use of NMDhcpClientFlags.
-rw-r--r-- | src/core/devices/nm-device.c | 3 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-client.c | 38 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-client.h | 11 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-dhclient.c | 32 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-dhcpcd.c | 2 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-nettools.c | 2 | ||||
-rw-r--r-- | src/core/dhcp/nm-dhcp-systemd.c | 20 |
7 files changed, 53 insertions, 55 deletions
diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 64ed4b3d7a..73480bce0c 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -16154,7 +16154,8 @@ nm_device_spawn_iface_helper(NMDevice *self) hostname = nm_dhcp_client_get_hostname(priv->dhcp_data_4.client); if (hostname) { - if (nm_dhcp_client_get_use_fqdn(priv->dhcp_data_4.client)) + if (NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(priv->dhcp_data_4.client), + NM_DHCP_CLIENT_FLAGS_USE_FQDN)) g_ptr_array_add(argv, g_strdup("--dhcp4-fqdn")); else g_ptr_array_add(argv, g_strdup("--dhcp4-hostname")); diff --git a/src/core/dhcp/nm-dhcp-client.c b/src/core/dhcp/nm-dhcp-client.c index 6c5305e170..00e84d1008 100644 --- a/src/core/dhcp/nm-dhcp-client.c +++ b/src/core/dhcp/nm-dhcp-client.c @@ -73,8 +73,7 @@ typedef struct _NMDhcpClientPrivate { guint32 iaid; NMDhcpState state; NMDhcpHostnameFlags hostname_flags; - bool info_only : 1; - bool use_fqdn : 1; + NMDhcpClientFlags client_flags; bool iaid_explicit : 1; } NMDhcpClientPrivate; @@ -292,20 +291,12 @@ nm_dhcp_client_get_hostname_flags(NMDhcpClient *self) return NM_DHCP_CLIENT_GET_PRIVATE(self)->hostname_flags; } -gboolean -nm_dhcp_client_get_info_only(NMDhcpClient *self) -{ - g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE); - - return NM_DHCP_CLIENT_GET_PRIVATE(self)->info_only; -} - -gboolean -nm_dhcp_client_get_use_fqdn(NMDhcpClient *self) +NMDhcpClientFlags +nm_dhcp_client_get_client_flags(NMDhcpClient *self) { - g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE); + g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), NM_DHCP_CLIENT_FLAGS_NONE); - return NM_DHCP_CLIENT_GET_PRIVATE(self)->use_fqdn; + return NM_DHCP_CLIENT_GET_PRIVATE(self)->client_flags; } const char * @@ -930,12 +921,12 @@ nm_dhcp_client_handle_event(gpointer unused, priv->route_metric)); } else { prefix = nm_dhcp_utils_ip6_prefix_from_options(str_options); - ip_config = NM_IP_CONFIG_CAST( - nm_dhcp_utils_ip6_config_from_options(nm_dhcp_client_get_multi_idx(self), - priv->ifindex, - priv->iface, - str_options, - priv->info_only)); + ip_config = NM_IP_CONFIG_CAST(nm_dhcp_utils_ip6_config_from_options( + nm_dhcp_client_get_multi_idx(self), + priv->ifindex, + priv->iface, + str_options, + NM_FLAGS_HAS(priv->client_flags, NM_DHCP_CLIENT_FLAGS_INFO_ONLY))); } } else g_warn_if_reached(); @@ -1052,11 +1043,8 @@ set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *ps case PROP_FLAGS: /* construct-only */ flags = g_value_get_uint(value); - nm_assert( - (flags & ~((guint)(NM_DHCP_CLIENT_FLAGS_INFO_ONLY | NM_DHCP_CLIENT_FLAGS_USE_FQDN))) - == 0); - priv->info_only = NM_FLAGS_HAS(flags, NM_DHCP_CLIENT_FLAGS_INFO_ONLY); - priv->use_fqdn = NM_FLAGS_HAS(flags, NM_DHCP_CLIENT_FLAGS_USE_FQDN); + nm_assert(!NM_FLAGS_ANY(flags, ~((guint) NM_DHCP_CLIENT_FLAGS_ALL))); + priv->client_flags = flags; break; case PROP_MULTI_IDX: /* construct-only */ diff --git a/src/core/dhcp/nm-dhcp-client.h b/src/core/dhcp/nm-dhcp-client.h index af3406ccea..4fc9f1e7c8 100644 --- a/src/core/dhcp/nm-dhcp-client.h +++ b/src/core/dhcp/nm-dhcp-client.h @@ -68,9 +68,14 @@ typedef struct { CList dhcp_client_lst; } NMDhcpClient; -typedef enum { +typedef enum _nm_packed { + NM_DHCP_CLIENT_FLAGS_NONE = 0, + NM_DHCP_CLIENT_FLAGS_INFO_ONLY = (1LL << 0), NM_DHCP_CLIENT_FLAGS_USE_FQDN = (1LL << 1), + + _NM_DHCP_CLIENT_FLAGS_LAST, + NM_DHCP_CLIENT_FLAGS_ALL = ((_NM_DHCP_CLIENT_FLAGS_LAST - 1) << 1) - 1, } NMDhcpClientFlags; typedef struct { @@ -148,9 +153,7 @@ const char *const *nm_dhcp_client_get_reject_servers(NMDhcpClient *self); NMDhcpHostnameFlags nm_dhcp_client_get_hostname_flags(NMDhcpClient *self); -gboolean nm_dhcp_client_get_info_only(NMDhcpClient *self); - -gboolean nm_dhcp_client_get_use_fqdn(NMDhcpClient *self); +NMDhcpClientFlags nm_dhcp_client_get_client_flags(NMDhcpClient *self); GBytes *nm_dhcp_client_get_vendor_class_identifier(NMDhcpClient *self); diff --git a/src/core/dhcp/nm-dhcp-dhclient.c b/src/core/dhcp/nm-dhcp-dhclient.c index 6384f3920c..199f0ad3bc 100644 --- a/src/core/dhcp/nm-dhcp-dhclient.c +++ b/src/core/dhcp/nm-dhcp-dhclient.c @@ -520,19 +520,20 @@ ip4_start(NMDhcpClient *client, client_id = nm_dhcp_client_get_client_id(client); - priv->conf_file = create_dhclient_config(self, - AF_INET, - nm_dhcp_client_get_iface(client), - nm_dhcp_client_get_uuid(client), - client_id, - dhcp_anycast_addr, - nm_dhcp_client_get_hostname(client), - nm_dhcp_client_get_timeout(client), - nm_dhcp_client_get_use_fqdn(client), - nm_dhcp_client_get_hostname_flags(client), - nm_dhcp_client_get_mud_url(client), - nm_dhcp_client_get_reject_servers(client), - &new_client_id); + priv->conf_file = create_dhclient_config( + self, + AF_INET, + nm_dhcp_client_get_iface(client), + nm_dhcp_client_get_uuid(client), + client_id, + dhcp_anycast_addr, + nm_dhcp_client_get_hostname(client), + nm_dhcp_client_get_timeout(client), + NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(client), NM_DHCP_CLIENT_FLAGS_USE_FQDN), + nm_dhcp_client_get_hostname_flags(client), + nm_dhcp_client_get_mud_url(client), + nm_dhcp_client_get_reject_servers(client), + &new_client_id); if (!priv->conf_file) { nm_utils_error_set_literal(error, NM_UTILS_ERROR_UNKNOWN, @@ -582,7 +583,10 @@ ip6_start(NMDhcpClient * client, } return dhclient_start(client, - nm_dhcp_client_get_info_only(NM_DHCP_CLIENT(self)) ? "-S" : "-N", + NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(NM_DHCP_CLIENT(self)), + NM_DHCP_CLIENT_FLAGS_INFO_ONLY) + ? "-S" + : "-N", FALSE, NULL, needed_prefixes, diff --git a/src/core/dhcp/nm-dhcp-dhcpcd.c b/src/core/dhcp/nm-dhcp-dhcpcd.c index cf9fe5c2f6..8c31aed0cf 100644 --- a/src/core/dhcp/nm-dhcp-dhcpcd.c +++ b/src/core/dhcp/nm-dhcp-dhcpcd.c @@ -122,7 +122,7 @@ ip4_start(NMDhcpClient *client, hostname = nm_dhcp_client_get_hostname(client); if (hostname) { - if (nm_dhcp_client_get_use_fqdn(client)) { + if (NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(client), NM_DHCP_CLIENT_FLAGS_USE_FQDN)) { g_ptr_array_add(argv, (gpointer) "-h"); g_ptr_array_add(argv, (gpointer) hostname); g_ptr_array_add(argv, (gpointer) "-F"); diff --git a/src/core/dhcp/nm-dhcp-nettools.c b/src/core/dhcp/nm-dhcp-nettools.c index dc9cbf58c4..7c76f52a05 100644 --- a/src/core/dhcp/nm-dhcp-nettools.c +++ b/src/core/dhcp/nm-dhcp-nettools.c @@ -1110,7 +1110,7 @@ ip4_start(NMDhcpClient *client, } hostname = nm_dhcp_client_get_hostname(client); if (hostname) { - if (nm_dhcp_client_get_use_fqdn(client)) { + if (NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(client), NM_DHCP_CLIENT_FLAGS_USE_FQDN)) { uint8_t buffer[255]; NMDhcpHostnameFlags flags; size_t fqdn_len; diff --git a/src/core/dhcp/nm-dhcp-systemd.c b/src/core/dhcp/nm-dhcp-systemd.c index de8e5142a4..509607a943 100644 --- a/src/core/dhcp/nm-dhcp-systemd.c +++ b/src/core/dhcp/nm-dhcp-systemd.c @@ -844,14 +844,16 @@ bound6_handle(NMDhcpSystemd *self) _LOGD("lease available"); - ip6_config = lease_to_ip6_config(nm_dhcp_client_get_multi_idx(NM_DHCP_CLIENT(self)), - iface, - nm_dhcp_client_get_ifindex(NM_DHCP_CLIENT(self)), - lease, - nm_dhcp_client_get_info_only(NM_DHCP_CLIENT(self)), - &options, - ts, - &error); + ip6_config = + lease_to_ip6_config(nm_dhcp_client_get_multi_idx(NM_DHCP_CLIENT(self)), + iface, + nm_dhcp_client_get_ifindex(NM_DHCP_CLIENT(self)), + lease, + NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(NM_DHCP_CLIENT(self)), + NM_DHCP_CLIENT_FLAGS_INFO_ONLY), + &options, + ts, + &error); if (!ip6_config) { _LOGW("%s", error->message); @@ -938,7 +940,7 @@ ip6_start(NMDhcpClient * client, _LOGT("dhcp-client6: set %p", sd_client); - if (nm_dhcp_client_get_info_only(client)) { + if (NM_FLAGS_HAS(nm_dhcp_client_get_client_flags(client), NM_DHCP_CLIENT_FLAGS_INFO_ONLY)) { sd_dhcp6_client_set_address_request(sd_client, 0); if (needed_prefixes == 0) sd_dhcp6_client_set_information_request(sd_client, 1); |