core: use streq() instead of strcmp() for comparing ip-config methods

Refactor some code to use nm_streq() and NM_IN_STRSET() instead of
strcmp().

Note that nm_utils_get_ip_config_method() never returns %NULL (not even
with g_return*() assertion failures). nm_streq() is sufficent.
This commit is contained in:
Thomas Haller 2018-12-12 17:11:34 +01:00
parent 589063db3b
commit b16e09a707
4 changed files with 77 additions and 91 deletions

View file

@ -49,11 +49,11 @@ nm_utils_get_shared_wifi_permission (NMConnection *connection)
{
NMSettingWireless *s_wifi;
NMSettingWirelessSecurity *s_wsec;
const char *method = NULL;
const char *method;
method = nm_utils_get_ip_config_method (connection, AF_INET);
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0)
return NULL; /* Not shared */
if (!nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED))
return NULL;
s_wifi = nm_connection_get_setting_wireless (connection);
if (s_wifi) {
@ -223,16 +223,13 @@ nm_utils_connection_has_default_route (NMConnection *connection,
goto out;
}
method = nm_utils_get_ip_config_method (connection, addr_family);
if (addr_family == AF_INET) {
method = nm_utils_get_ip_config_method (connection, AF_INET);
if (NM_IN_STRSET (method, NULL,
NM_SETTING_IP4_CONFIG_METHOD_DISABLED,
if (NM_IN_STRSET (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED,
NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL))
goto out;
} else {
method = nm_utils_get_ip_config_method (connection, AF_INET6);
if (NM_IN_STRSET (method, NULL,
NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
if (NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL))
goto out;
}
@ -343,29 +340,28 @@ check_ip6_method (NMConnection *orig,
if (!props)
return TRUE;
/* If the generated connection is 'link-local' and the candidate is both 'auto'
* and may-fail=TRUE, then the candidate is OK to use. may-fail is included
* in the decision because if the candidate is 'auto' but may-fail=FALSE, then
* the connection could not possibly have been previously activated on the
* device if the device has no non-link-local IPv6 address.
*/
orig_ip6_method = nm_utils_get_ip_config_method (orig, AF_INET6);
candidate_ip6_method = nm_utils_get_ip_config_method (candidate, AF_INET6);
candidate_ip6 = nm_connection_get_setting_ip6_config (candidate);
if ( strcmp (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0
&& strcmp (candidate_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0
&& (!candidate_ip6 || nm_setting_ip_config_get_may_fail (candidate_ip6))) {
if ( nm_streq (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)
&& nm_streq (candidate_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)
&& ( !candidate_ip6
|| nm_setting_ip_config_get_may_fail (candidate_ip6))) {
/* If the generated connection is 'link-local' and the candidate is both 'auto'
* and may-fail=TRUE, then the candidate is OK to use. may-fail is included
* in the decision because if the candidate is 'auto' but may-fail=FALSE, then
* the connection could not possibly have been previously activated on the
* device if the device has no non-link-local IPv6 address.
*/
allow = TRUE;
}
/* If the generated connection method is 'link-local' or 'auto' and the candidate
* method is 'ignore' we can take the connection, because NM didn't simply take care
* of IPv6.
*/
if ( ( strcmp (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0
|| strcmp (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0)
&& strcmp (candidate_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) {
} else if ( NM_IN_STRSET (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL,
NM_SETTING_IP6_CONFIG_METHOD_AUTO)
&& nm_streq0 (candidate_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) {
/* If the generated connection method is 'link-local' or 'auto' and the candidate
* method is 'ignore' we can take the connection, because NM didn't simply take care
* of IPv6.
*/
allow = TRUE;
}
@ -374,6 +370,7 @@ check_ip6_method (NMConnection *orig,
NM_SETTING_IP6_CONFIG_SETTING_NAME,
NM_SETTING_IP_CONFIG_METHOD);
}
return allow;
}
@ -521,19 +518,20 @@ check_ip4_method (NMConnection *orig,
if (!props)
return TRUE;
/* If the generated connection is 'disabled' (device had no IP addresses)
* but it has no carrier, that most likely means that IP addressing could
* not complete and thus no IP addresses were assigned. In that case, allow
* matching to the "auto" method.
*/
orig_ip4_method = nm_utils_get_ip_config_method (orig, AF_INET);
candidate_ip4_method = nm_utils_get_ip_config_method (candidate, AF_INET);
candidate_ip4 = nm_connection_get_setting_ip4_config (candidate);
if ( strcmp (orig_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0
&& strcmp (candidate_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0
&& (!candidate_ip4 || nm_setting_ip_config_get_may_fail (candidate_ip4))
&& (device_has_carrier == FALSE)) {
if ( nm_streq (orig_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)
&& nm_streq (candidate_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)
&& ( !candidate_ip4
|| nm_setting_ip_config_get_may_fail (candidate_ip4))
&& !device_has_carrier) {
/* If the generated connection is 'disabled' (device had no IP addresses)
* but it has no carrier, that most likely means that IP addressing could
* not complete and thus no IP addresses were assigned. In that case, allow
* matching to the "auto" method.
*/
remove_from_hash (settings, props,
NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP_CONFIG_METHOD);

View file

@ -2860,6 +2860,7 @@ nm_device_get_effective_ip_config_method (NMDevice *self,
nm_assert_addr_family (addr_family);
method = nm_utils_get_ip_config_method (connection, addr_family);
if ( (addr_family == AF_INET && nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO))
|| (addr_family == AF_INET6 && nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO))) {
klass = NM_DEVICE_GET_CLASS (self);
@ -6931,7 +6932,7 @@ nm_device_handle_ipv4ll_event (sd_ipv4ll *ll, int event, void *data)
/* Ignore if the connection isn't an AutoIP connection */
method = nm_device_get_effective_ip_config_method (self, AF_INET);
if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) != 0)
if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL))
return;
switch (event) {
@ -7863,34 +7864,26 @@ static gboolean
connection_ip4_method_requires_carrier (NMConnection *connection,
gboolean *out_ip4_enabled)
{
const char *method = nm_utils_get_ip_config_method (connection, AF_INET);
static const char *ip4_carrier_methods[] = {
NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
NULL
};
const char *method;
if (out_ip4_enabled)
*out_ip4_enabled = !!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED);
return g_strv_contains (ip4_carrier_methods, method);
method = nm_utils_get_ip_config_method (connection, AF_INET);
NM_SET_OUT (out_ip4_enabled, !nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED));
return NM_IN_STRSET (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL);
}
static gboolean
connection_ip6_method_requires_carrier (NMConnection *connection,
gboolean *out_ip6_enabled)
{
const char *method = nm_utils_get_ip_config_method (connection, AF_INET6);
static const char *ip6_carrier_methods[] = {
NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_METHOD_DHCP,
NM_SETTING_IP6_CONFIG_METHOD_SHARED,
NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL,
NULL
};
const char *method;
if (out_ip6_enabled)
*out_ip6_enabled = !!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
return g_strv_contains (ip6_carrier_methods, method);
method = nm_utils_get_ip_config_method (connection, AF_INET6);
NM_SET_OUT (out_ip6_enabled, !nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE));
return NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_METHOD_DHCP,
NM_SETTING_IP6_CONFIG_METHOD_SHARED,
NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL);
}
static gboolean
@ -7964,7 +7957,7 @@ ip4_requires_slaves (NMDevice *self)
const char *method;
method = nm_device_get_effective_ip_config_method (self, AF_INET);
return strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0;
return nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
}
static NMActStageReturn
@ -8818,15 +8811,15 @@ linklocal6_check_complete (NMDevice *self)
_LOGD (LOGD_DEVICE, "linklocal6: waiting for link-local addresses successful, continue with method %s", method);
if ( strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0
|| strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0)
if (NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_METHOD_SHARED))
addrconf6_start_with_link_ready (self);
else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) {
else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) {
if (!dhcp6_start_with_link_ready (self, connection)) {
/* Time out IPv6 instead of failing the entire activation */
nm_device_activate_schedule_ip6_config_timeout (self);
}
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0)
} else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL))
nm_device_activate_schedule_ip6_config_result (self);
else
g_return_if_fail (FALSE);
@ -8911,7 +8904,6 @@ static gboolean
linklocal6_start (NMDevice *self)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
const char *method;
nm_clear_g_source (&priv->linklocal6_timeout_id);
@ -8921,8 +8913,8 @@ linklocal6_start (NMDevice *self)
| NM_PLATFORM_MATCH_WITH_ADDRSTATE_NORMAL))
return TRUE;
method = nm_device_get_effective_ip_config_method (self, AF_INET6);
_LOGD (LOGD_DEVICE, "linklocal6: starting IPv6 with method '%s', but the device has no link-local addresses configured. Wait.", method);
_LOGD (LOGD_DEVICE, "linklocal6: starting IPv6 with method '%s', but the device has no link-local addresses configured. Wait.",
nm_device_get_effective_ip_config_method (self, AF_INET6));
check_and_add_ipv6ll_addr (self);
@ -9427,11 +9419,10 @@ addrconf6_start_with_link_ready (NMDevice *self)
static NMNDiscNodeType
ndisc_node_type (NMDevice *self)
{
if (strcmp (nm_device_get_effective_ip_config_method (self, AF_INET6),
NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0)
if (nm_streq (nm_device_get_effective_ip_config_method (self, AF_INET6),
NM_SETTING_IP4_CONFIG_METHOD_SHARED))
return NM_NDISC_NODE_TYPE_ROUTER;
else
return NM_NDISC_NODE_TYPE_HOST;
return NM_NDISC_NODE_TYPE_HOST;
}
static gboolean
@ -9690,8 +9681,8 @@ ip6_requires_slaves (NMDevice *self)
/* SLAAC, DHCP, and Link-Local depend on connectivity (and thus slaves)
* to complete addressing. SLAAC and DHCP need a peer to provide a prefix.
*/
return strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0
|| strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0;
return NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_METHOD_DHCP);
}
static NMActStageReturn
@ -9730,8 +9721,7 @@ act_stage3_ip6_config_start (NMDevice *self,
priv->dhcp6.mode = NM_NDISC_DHCP_LEVEL_NONE;
method = nm_device_get_effective_ip_config_method (self, AF_INET6);
if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) {
if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) {
if ( !priv->master
&& !nm_device_sys_iface_state_is_external (self)) {
gboolean ipv6ll_handle_old = priv->ipv6ll_handle;
@ -9777,27 +9767,27 @@ act_stage3_ip6_config_start (NMDevice *self,
ip6_privacy = _ip6_privacy_get (self);
if ( strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0
|| strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0) {
if (NM_IN_STRSET (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_METHOD_SHARED)) {
if (!addrconf6_start (self, ip6_privacy)) {
/* IPv6 might be disabled; allow IPv4 to proceed */
ret = NM_ACT_STAGE_RETURN_IP_FAIL;
} else
ret = NM_ACT_STAGE_RETURN_POSTPONE;
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) {
} else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) {
ret = linklocal6_start (self)
? NM_ACT_STAGE_RETURN_SUCCESS
: NM_ACT_STAGE_RETURN_POSTPONE;
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) {
} else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) {
priv->dhcp6.mode = NM_NDISC_DHCP_LEVEL_MANAGED;
if (!dhcp6_start (self, TRUE)) {
/* IPv6 might be disabled; allow IPv4 to proceed */
ret = NM_ACT_STAGE_RETURN_IP_FAIL;
} else
ret = NM_ACT_STAGE_RETURN_POSTPONE;
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0) {
} else if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL))
ret = NM_ACT_STAGE_RETURN_SUCCESS;
} else
else
_LOGW (LOGD_IP6, "unhandled IPv6 config method '%s'; will fail", method);
if ( ret != NM_ACT_STAGE_RETURN_FAILURE
@ -10397,8 +10387,7 @@ activate_stage5_ip4_config_result (NMDevice *self)
/* Start IPv4 sharing if we need it */
method = nm_device_get_effective_ip_config_method (self, AF_INET);
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0) {
if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) {
gs_free_error GError *error = NULL;
if (!start_sharing (self, priv->ip_config_4, &error)) {
@ -10581,8 +10570,7 @@ activate_stage5_ip6_config_commit (NMDevice *self)
/* Start IPv6 forwarding if we need it */
method = nm_device_get_effective_ip_config_method (self, AF_INET6);
if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0) {
if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) {
if (!nm_platform_sysctl_set (nm_device_get_platform (self), NMP_SYSCTL_PATHID_ABSOLUTE ("/proc/sys/net/ipv6/conf/all/forwarding"), "1")) {
errsv = errno;
_LOGE (LOGD_SHARING, "share: error enabling IPv6 forwarding: (%d) %s", errsv, strerror (errsv));
@ -14544,7 +14532,7 @@ nm_device_spawn_iface_helper (NMDevice *self)
dhcp4_address = find_dhcp4_address (self);
method = nm_device_get_effective_ip_config_method (self, AF_INET);
if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) {
if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
NMSettingIPConfig *s_ip4;
s_ip4 = nm_connection_get_setting_ip4_config (connection);
@ -14587,7 +14575,7 @@ nm_device_spawn_iface_helper (NMDevice *self)
}
method = nm_utils_get_ip_config_method (connection, AF_INET6);
if (g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) {
if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) {
NMSettingIPConfig *s_ip6;
NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT;

View file

@ -709,10 +709,10 @@ nm_modem_stage3_ip4_config_start (NMModem *self,
method = nm_utils_get_ip_config_method (connection, AF_INET);
/* Only Disabled and Auto methods make sense for WWAN */
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0)
if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED))
return NM_ACT_STAGE_RETURN_SUCCESS;
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) != 0) {
if (!nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
_LOGE ("unhandled WWAN IPv4 method '%s'; will fail", method);
NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_IP_METHOD_UNSUPPORTED);
return NM_ACT_STAGE_RETURN_FAILURE;
@ -826,10 +826,10 @@ nm_modem_stage3_ip6_config_start (NMModem *self,
method = nm_utils_get_ip_config_method (connection, AF_INET6);
/* Only Ignore and Auto methods make sense for WWAN */
if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0)
if (nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE))
return NM_ACT_STAGE_RETURN_IP_DONE;
if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) != 0) {
if (!nm_streq (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) {
_LOGW ("unhandled WWAN IPv6 method '%s'; will fail",
method);
NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE);

View file

@ -1031,9 +1031,9 @@ _ppp_manager_start (NMPPPManager *self,
/* Figure out what address methods should be enabled */
ip4_method = nm_utils_get_ip_config_method (connection, AF_INET);
ip4_enabled = g_strcmp0 (ip4_method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0;
ip4_enabled = nm_streq (ip4_method, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
ip6_method = nm_utils_get_ip_config_method (connection, AF_INET6);
ip6_enabled = g_strcmp0 (ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0;
ip6_enabled = nm_streq (ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO);
ppp_cmd = create_pppd_cmd_line (self,
s_ppp,