diff options
author | Grant Erickson <erick205@umn.edu> | 2023-12-04 23:01:38 -0800 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2023-12-08 01:55:44 +0100 |
commit | 2bc98078d3139d4a800e072639b2242ce6bcca4d (patch) | |
tree | 39cdba78e413b10f8c02847d24a27d8534e2160b | |
parent | 245c985c08fc7cb01221349575cef50bbc97df02 (diff) | |
download | connman-2bc98078d3139d4a800e072639b2242ce6bcca4d.tar.gz |
connection: Fan out route manipulation into callbacks.
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).
-rw-r--r-- | src/connection.c | 101 |
1 files changed, 91 insertions, 10 deletions
diff --git a/src/connection.c b/src/connection.c index 2a21e476b..1bcf16dee 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 |