diff mbox series

[2/4] connection: Simplify 'unset_default_gateway' flow.

Message ID 20231130052622.1335266-3-gerickson@nuovations.com (mailing list archive)
State Not Applicable, archived
Headers show
Series connection: Simplify '{un,}set_default_gateway' Flow | expand

Commit Message

Grant Erickson Nov. 30, 2023, 5:26 a.m. UTC
Ostensibly over time, the logic of 'unset_default_gateway' has become
a bit complicated with early returns and repeated state checks.

However, amidst all of that, there is a simpler flow to surface. At
its core, the function is two outer conditional blocks, one for IPv4
and one for IPv6, and three inner conditional blocks within those: one
for a VPN, one for a gateway with an any/unspecified address, and one
for everything else.

This simplifies the flow of 'unset_default_gateway' accordingly,
curtailing the number of early returns and reducing the repeated state
checks to one through the use of those simplified conditional blocks.
---
 src/connection.c | 70 +++++++++++++++++++++---------------------------
 1 file changed, 31 insertions(+), 39 deletions(-)
diff mbox series

Patch

diff --git a/src/connection.c b/src/connection.c
index 9979ca02c356..2460ca91074c 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -957,53 +957,45 @@  static void unset_default_gateway(struct gateway_data *data,
 	else
 		return;
 
-	if (do_ipv4 && data->ipv4_config &&
-					data->ipv4_config->vpn) {
-		connman_inet_clear_gateway_interface(data->index);
-		data->ipv4_config->active = false;
-
-		DBG("unset %p index %d vpn %s index %d phy %s",
-			data, data->index, data->ipv4_config->vpn_ip,
-			data->ipv4_config->vpn_phy_index,
-			data->ipv4_config->vpn_phy_ip);
-
-		return;
-	}
+	if (do_ipv4 && data->ipv4_config) {
+		if (data->ipv4_config->vpn) {
+			connman_inet_clear_gateway_interface(data->index);
 
-	if (do_ipv6 && data->ipv6_config &&
-					data->ipv6_config->vpn) {
-		connman_inet_clear_ipv6_gateway_interface(data->index);
-		data->ipv6_config->active = false;
+			data->ipv4_config->active = false;
 
-		DBG("unset %p index %d vpn %s index %d phy %s",
-			data, data->index, data->ipv6_config->vpn_ip,
-			data->ipv6_config->vpn_phy_index,
-			data->ipv6_config->vpn_phy_ip);
+			DBG("unset %p index %d vpn %s index %d phy %s",
+				data, data->index, data->ipv4_config->vpn_ip,
+				data->ipv4_config->vpn_phy_index,
+				data->ipv4_config->vpn_phy_ip);
+		} else if (is_ipv4_addr_any_str(data->ipv4_config->gateway)) {
+			connman_inet_clear_gateway_interface(data->index);
 
-		return;
+			data->ipv4_config->active = false;
+		} else {
+			connman_inet_clear_gateway_address(data->index,
+						data->ipv4_config->gateway);
+		}
 	}
 
-	if (do_ipv4 && data->ipv4_config &&
-			is_ipv4_addr_any_str(data->ipv4_config->gateway)) {
-		connman_inet_clear_gateway_interface(data->index);
-		data->ipv4_config->active = false;
-		return;
-	}
+	if (do_ipv6 && data->ipv6_config) {
+		if (data->ipv6_config->vpn) {
+			connman_inet_clear_ipv6_gateway_interface(data->index);
 
-	if (do_ipv6 && data->ipv6_config &&
-			is_ipv6_addr_any_str(data->ipv6_config->gateway)) {
-		connman_inet_clear_ipv6_gateway_interface(data->index);
-		data->ipv6_config->active = false;
-		return;
-	}
+			data->ipv6_config->active = false;
 
-	if (do_ipv6 && data->ipv6_config)
-		connman_inet_clear_ipv6_gateway_address(data->index,
-						data->ipv6_config->gateway);
+			DBG("unset %p index %d vpn %s index %d phy %s",
+				data, data->index, data->ipv6_config->vpn_ip,
+				data->ipv6_config->vpn_phy_index,
+				data->ipv6_config->vpn_phy_ip);
+		} else if (is_ipv6_addr_any_str(data->ipv6_config->gateway)) {
+			connman_inet_clear_ipv6_gateway_interface(data->index);
 
-	if (do_ipv4 && data->ipv4_config)
-		connman_inet_clear_gateway_address(data->index,
-						data->ipv4_config->gateway);
+			data->ipv6_config->active = false;
+		} else {
+			connman_inet_clear_ipv6_gateway_address(data->index,
+						data->ipv6_config->gateway);
+		}
+	}
 }
 
 /**