networkd: route/address - simplify and unify creators

Rename new_dynamic() to simply _new() and reuse that from new_static().
This commit is contained in:
Tom Gundersen 2015-09-21 15:53:40 +02:00
parent e930d14ac8
commit f0213e3796
9 changed files with 55 additions and 65 deletions

View File

@ -30,17 +30,27 @@
#include "networkd.h"
#include "networkd-address.h"
static void address_init(Address *address) {
assert(address);
int address_new(Address **ret) {
_cleanup_address_free_ Address *address = NULL;
address = new0(Address, 1);
if (!address)
return -ENOMEM;
address->family = AF_UNSPEC;
address->scope = RT_SCOPE_UNIVERSE;
address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME;
*ret = address;
address = NULL;
return 0;
}
int address_new_static(Network *network, unsigned section, Address **ret) {
_cleanup_address_free_ Address *address = NULL;
int r;
if (section) {
address = hashmap_get(network->addresses_by_section, UINT_TO_PTR(section));
@ -52,11 +62,9 @@ int address_new_static(Network *network, unsigned section, Address **ret) {
}
}
address = new0(Address, 1);
if (!address)
return -ENOMEM;
address_init(address);
r = address_new(&address);
if (r < 0)
return r;
address->network = network;
@ -74,21 +82,6 @@ int address_new_static(Network *network, unsigned section, Address **ret) {
return 0;
}
int address_new_dynamic(Address **ret) {
_cleanup_address_free_ Address *address = NULL;
address = new0(Address, 1);
if (!address)
return -ENOMEM;
address_init(address);
*ret = address;
address = NULL;
return 0;
}
void address_free(Address *address) {
if (!address)
return;
@ -292,7 +285,7 @@ static int address_acquire(Link *link, Address *original, Address **ret) {
} else if (original->family == AF_INET6)
in_addr.in6.s6_addr[15] |= 1;
r = address_new_dynamic(&na);
r = address_new(&na);
if (r < 0)
return r;

View File

@ -56,7 +56,7 @@ struct Address {
};
int address_new_static(Network *network, unsigned section, Address **ret);
int address_new_dynamic(Address **ret);
int address_new(Address **ret);
void address_free(Address *address);
int address_configure(Address *address, Link *link, sd_netlink_message_handler_t callback);
int address_update(Address *address, Link *link, sd_netlink_message_handler_t callback);

View File

@ -72,11 +72,11 @@ static int link_set_dhcp_routes(Link *link) {
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
r = route_new_dynamic(&route, RTPROT_DHCP);
r = route_new(&route, RTPROT_DHCP);
if (r < 0)
return log_link_error_errno(link, r, "Could not allocate route: %m");
r = route_new_dynamic(&route_gw, RTPROT_DHCP);
r = route_new(&route_gw, RTPROT_DHCP);
if (r < 0)
return log_link_error_errno(link, r, "Could not allocate route: %m");
@ -120,7 +120,7 @@ static int link_set_dhcp_routes(Link *link) {
for (i = 0; i < n; i++) {
_cleanup_route_free_ Route *route = NULL;
r = route_new_dynamic(&route, RTPROT_DHCP);
r = route_new(&route, RTPROT_DHCP);
if (r < 0)
return log_link_error_errno(link, r, "Could not allocate route: %m");
@ -162,7 +162,7 @@ static int dhcp_lease_lost(Link *link) {
for (i = 0; i < n; i++) {
_cleanup_route_free_ Route *route = NULL;
r = route_new_dynamic(&route, RTPROT_UNSPEC);
r = route_new(&route, RTPROT_UNSPEC);
if (r >= 0) {
route->family = AF_INET;
route->in_addr.in = routes[i].gw_addr;
@ -176,14 +176,14 @@ static int dhcp_lease_lost(Link *link) {
}
}
r = address_new_dynamic(&address);
r = address_new(&address);
if (r >= 0) {
r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
if (r >= 0) {
_cleanup_route_free_ Route *route_gw = NULL;
_cleanup_route_free_ Route *route = NULL;
r = route_new_dynamic(&route_gw, RTPROT_UNSPEC);
r = route_new(&route_gw, RTPROT_UNSPEC);
if (r >= 0) {
route_gw->family = AF_INET;
route_gw->dst_addr.in = gateway;
@ -194,7 +194,7 @@ static int dhcp_lease_lost(Link *link) {
&link_route_drop_handler);
}
r = route_new_dynamic(&route, RTPROT_UNSPEC);
r = route_new(&route, RTPROT_UNSPEC);
if (r >= 0) {
route->family = AF_INET;
route->in_addr.in = gateway;
@ -288,7 +288,7 @@ static int dhcp4_update_address(Link *link,
prefixlen = in_addr_netmask_to_prefixlen(netmask);
r = address_new_dynamic(&addr);
r = address_new(&addr);
if (r < 0)
return r;

View File

@ -69,7 +69,7 @@ static int dhcp6_address_update(Link *link, struct in6_addr *ip6_addr,
int r;
_cleanup_address_free_ Address *addr = NULL;
r = address_new_dynamic(&addr);
r = address_new(&addr);
if (r < 0)
return r;

View File

@ -42,7 +42,7 @@ static int ipv4ll_address_lost(Link *link) {
log_link_debug(link, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr));
r = address_new_dynamic(&address);
r = address_new(&address);
if (r < 0) {
log_link_error_errno(link, r, "Could not allocate address: %m");
return r;
@ -55,7 +55,7 @@ static int ipv4ll_address_lost(Link *link) {
address_drop(address, link, &link_address_drop_handler);
r = route_new_dynamic(&route, RTPROT_UNSPEC);
r = route_new(&route, RTPROT_UNSPEC);
if (r < 0) {
log_link_error_errno(link, r, "Could not allocate route: %m");
return r;
@ -133,7 +133,7 @@ static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
log_link_debug(link, "IPv4 link-local claim %u.%u.%u.%u",
ADDRESS_FMT_VAL(address));
r = address_new_dynamic(&ll_addr);
r = address_new(&ll_addr);
if (r < 0)
return r;
@ -149,7 +149,7 @@ static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
link->ipv4ll_address = false;
r = route_new_dynamic(&route, RTPROT_STATIC);
r = route_new(&route, RTPROT_STATIC);
if (r < 0)
return r;

View File

@ -2074,7 +2074,7 @@ int link_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, voi
}
}
r = address_new_dynamic(&address);
r = address_new(&address);
if (r < 0)
return r;

View File

@ -26,8 +26,26 @@
#include "networkd.h"
#include "networkd-route.h"
int route_new(Route **ret, unsigned char rtm_protocol) {
_cleanup_route_free_ Route *route = NULL;
route = new0(Route, 1);
if (!route)
return -ENOMEM;
route->family = AF_UNSPEC;
route->scope = RT_SCOPE_UNIVERSE;
route->protocol = rtm_protocol;
*ret = route;
route = NULL;
return 0;
}
int route_new_static(Network *network, unsigned section, Route **ret) {
_cleanup_route_free_ Route *route = NULL;
int r;
if (section) {
route = hashmap_get(network->routes_by_section,
@ -40,13 +58,9 @@ int route_new_static(Network *network, unsigned section, Route **ret) {
}
}
route = new0(Route, 1);
if (!route)
return -ENOMEM;
route->family = AF_UNSPEC;
route->scope = RT_SCOPE_UNIVERSE;
route->protocol = RTPROT_STATIC;
r = route_new(&route, RTPROT_STATIC);
if (r < 0)
return r;
route->network = network;
@ -64,23 +78,6 @@ int route_new_static(Network *network, unsigned section, Route **ret) {
return 0;
}
int route_new_dynamic(Route **ret, unsigned char rtm_protocol) {
_cleanup_route_free_ Route *route = NULL;
route = new0(Route, 1);
if (!route)
return -ENOMEM;
route->family = AF_UNSPEC;
route->scope = RT_SCOPE_UNIVERSE;
route->protocol = rtm_protocol;
*ret = route;
route = NULL;
return 0;
}
void route_free(Route *route) {
if (!route)
return;

View File

@ -46,7 +46,7 @@ struct Route {
};
int route_new_static(Network *network, unsigned section, Route **ret);
int route_new_dynamic(Route **ret, unsigned char rtm_protocol);
int route_new(Route **ret, unsigned char rtm_protocol);
void route_free(Route *route);
int route_configure(Route *route, Link *link, sd_netlink_message_handler_t callback);
int route_drop(Route *route, Link *link, sd_netlink_message_handler_t callback);

View File

@ -143,8 +143,8 @@ static void test_network_get(Manager *manager, struct udev_device *loopback) {
static void test_address_equality(void) {
_cleanup_address_free_ Address *a1 = NULL, *a2 = NULL;
assert_se(address_new_dynamic(&a1) >= 0);
assert_se(address_new_dynamic(&a2) >= 0);
assert_se(address_new(&a1) >= 0);
assert_se(address_new(&a2) >= 0);
assert_se(address_equal(NULL, NULL));
assert_se(!address_equal(a1, NULL));