diff mbox series

[31/90] connection: Fan out route manipulation into callbacks.

Message ID 20231206235056.322578-32-gerickson@nuovations.com (mailing list archive)
State Not Applicable, archived
Headers show
Series Add Gateway Low-priority Default Routes for Non-default Services | expand

Commit Message

Grant Erickson Dec. 6, 2023, 11:49 p.m. UTC
From: Grant Erickson <erick205@umn.edu>

There is an appreciable amount of boilerplate copied-and-pasted among
the four set/unset IPv4/IPv6 gateway default route mutation functions.

This introduces two new functions to handle that common boilerplate:
'set_default_gateway_route_common' and
'unset_default_gateway_route_common' and introduces a new function
pointer, 'mutate_default_gateway_route_cb_t' that they take as a
parameter.

The four remaining set/unset IPv4/IPv6 gateway default route mutation
functions are then solely responsible only for making additions or
deletions to the routing table(s).
---
 src/connection.c | 101 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 91 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/src/connection.c b/src/connection.c
index 2a21e476bc53..1bcf16dee616 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -203,6 +203,9 @@  struct mutate_default_gateway_ops {
 				struct gateway_config *config);
 };
 
+typedef int (*mutate_default_gateway_route_cb_t)(struct gateway_data *data,
+				struct gateway_config *config);
+
 static int unset_default_gateway(struct gateway_data *data,
 				enum connman_ipconfig_type type,
 				const char *function);
@@ -1254,7 +1257,45 @@  static int mutate_default_gateway(struct gateway_data *data,
 	return (status4 < 0 ? status4 : status6);
 }
 
-static int set_ipv4_high_priority_default_gateway(
+static int set_default_gateway_route_common(struct gateway_data *data,
+				struct gateway_config *config,
+				enum gateway_config_type type,
+				mutate_default_gateway_route_cb_t cb)
+{
+	int err = 0;
+
+	if (!data || !config || !cb)
+		return -EINVAL;
+
+	err = cb(data, config);
+	if (err < 0)
+		goto done;
+
+	gateway_config_type_set(config, type);
+
+done:
+	return err;
+}
+
+static int unset_default_gateway_route_common(struct gateway_data *data,
+				struct gateway_config *config,
+				enum gateway_config_type type,
+				mutate_default_gateway_route_cb_t cb)
+{
+	int err = 0;
+
+	if (!data || !config || !cb)
+		return -EINVAL;
+
+	err = cb(data, config);
+	if (err < 0)
+		goto done;
+
+done:
+	return err;
+}
+
+static int set_ipv4_high_priority_default_gateway_route_cb(
 				struct gateway_data *data,
 				struct gateway_config *config)
 {
@@ -1292,14 +1333,11 @@  static int set_ipv4_high_priority_default_gateway(
 			data, data->index, config->gateway);
 	}
 
-	gateway_config_type_set(config,
-		CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT);
-
 done:
 	return err;
 }
 
-static int set_ipv6_high_priority_default_gateway(
+static int set_ipv6_high_priority_default_gateway_route_cb(
 				struct gateway_data *data,
 				struct gateway_config *config)
 {
@@ -1337,13 +1375,32 @@  static int set_ipv6_high_priority_default_gateway(
 			data, data->index, config->gateway);
 	}
 
-	gateway_config_type_set(config,
-		CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT);
-
 done:
 	return err;
 }
 
+static int set_ipv4_high_priority_default_gateway(struct gateway_data *data,
+				struct gateway_config *config)
+{
+	static const enum gateway_config_type type =
+			CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT;
+	static const mutate_default_gateway_route_cb_t cb =
+			set_ipv4_high_priority_default_gateway_route_cb;
+
+	return set_default_gateway_route_common(data, config, type, cb);
+}
+
+static int set_ipv6_high_priority_default_gateway(struct gateway_data *data,
+				struct gateway_config *config)
+{
+	static const enum gateway_config_type type =
+			CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT;
+	static const mutate_default_gateway_route_cb_t cb =
+			set_ipv6_high_priority_default_gateway_route_cb;
+
+	return set_default_gateway_route_common(data, config, type, cb);
+}
+
 /**
  *  @brief
  *    Set, or assign, the gateway, or default route, for the specified
@@ -1401,7 +1458,7 @@  done:
 	return status;
 }
 
-static int unset_ipv4_high_priority_default_gateway(
+static int unset_ipv4_high_priority_default_gateway_route_cb(
 				struct gateway_data *data,
 				struct gateway_config *config)
 {
@@ -1436,7 +1493,7 @@  static int unset_ipv4_high_priority_default_gateway(
 	return err;
 }
 
-static int unset_ipv6_high_priority_default_gateway(
+static int unset_ipv6_high_priority_default_gateway_route_cb(
 				struct gateway_data *data,
 				struct gateway_config *config)
 {
@@ -1471,6 +1528,30 @@  static int unset_ipv6_high_priority_default_gateway(
 	return err;
 }
 
+static int unset_ipv4_high_priority_default_gateway(
+				struct gateway_data *data,
+				struct gateway_config *config)
+{
+	static const enum gateway_config_type type =
+			CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT;
+	static const mutate_default_gateway_route_cb_t cb =
+			unset_ipv4_high_priority_default_gateway_route_cb;
+
+	return unset_default_gateway_route_common(data, config, type, cb);
+}
+
+static int unset_ipv6_high_priority_default_gateway(
+				struct gateway_data *data,
+				struct gateway_config *config)
+{
+	static const enum gateway_config_type type =
+			CONNMAN_GATEWAY_CONFIG_TYPE_HIGH_PRIORITY_DEFAULT;
+	static const mutate_default_gateway_route_cb_t cb =
+			unset_ipv6_high_priority_default_gateway_route_cb;
+
+	return unset_default_gateway_route_common(data, config, type, cb);
+}
+
 /**
  *  @brief
  *    Unset the gateway, or default route, for the specified IP