]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkd: route/address - simplify and unify creators
authorTom Gundersen <teg@jklm.no>
Mon, 21 Sep 2015 13:53:40 +0000 (15:53 +0200)
committerTom Gundersen <teg@jklm.no>
Sun, 11 Oct 2015 12:21:41 +0000 (14:21 +0200)
Rename new_dynamic() to simply _new() and reuse that from new_static().

src/network/networkd-address.c
src/network/networkd-address.h
src/network/networkd-dhcp4.c
src/network/networkd-dhcp6.c
src/network/networkd-ipv4ll.c
src/network/networkd-link.c
src/network/networkd-route.c
src/network/networkd-route.h
src/network/test-network.c

index 388beb5d4cf405bed4f5178b3d30433f43c071a1..b2f24ec305a1d1c4591dd8f85e4fa1252466b046 100644 (file)
 #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;
 
index 39789a2382d55b5eeb5366e783022e8cb08cae1a..74c0c0be89878fa7c8e262b38a3a15ad6e681833 100644 (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);
index 04f04df117698836b11d0b7fe807e17c6ae5ea79..2a0900d5b15de31f4626a669097533c39bd11c67 100644 (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;
 
index 3cb7b8d9caccf4448dd51b280b2aa34dd163d4fb..a58af06198f2bab97cc70409c06c2d5d207e4d03 100644 (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;
 
index 1902b3d23ab09321142e222806136a8d5e35b182..f55b00d36c4068f4f713edeeffebde24923c848e 100644 (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;
 
index ffc9578e86841642693588af1468b86bfb038642..e9047dd6a6d0d91b659873156792d47a5669008a 100644 (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;
 
index ee1ddd81fe84c6983986e2a1ed94a9dce688199f..28ce12684908330a41b6ba17972e8645d0db97ae 100644 (file)
 #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;
index 11e94d44fbf1fcf69ad8cb48d23bc7ab91532d4a..b7e5bfb057d880f7a72668c40220136ec31e407a 100644 (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);
index 5909cc790e842af25a94b462f16edaf4234dbbe8..80676651a97edc7d8574ad6ca87b7c285711e633 100644 (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));