summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-07-17 01:39:46 +0200
committerLennart Poettering <lennart@poettering.net>2014-07-17 01:42:26 +0200
commita2ba62c719224a4b47751623ca5e8b0333f49721 (patch)
tree9d8d2332a7c63ab55089e83c6fd82ca85bd73efc /src
parentb45d9e86e41c453e0b57f2f8316bef06dee42e5a (diff)
sd-network: remove redundant array size parameter from functions that return arrays
As long as the number of array entries is relatively small it's nicer to simply return the number of entries directly, instead of using a size_t* return parameter for it.
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd-network/network-internal.c18
-rw-r--r--src/libsystemd-network/network-internal.h7
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c40
-rw-r--r--src/network/networkd-link.c25
-rw-r--r--src/network/sd-network.c26
-rw-r--r--src/network/test-network.c6
-rw-r--r--src/resolve/resolved-link.c28
-rw-r--r--src/systemd/sd-dhcp-lease.h6
-rw-r--r--src/systemd/sd-network.h8
9 files changed, 77 insertions, 87 deletions
diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index 7a7a4eac9d..e209b0f56e 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -339,7 +339,7 @@ int net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
return 0;
}
-void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size) {
+void serialize_in_addrs(FILE *f, const char *key, const struct in_addr *addresses, size_t size) {
unsigned i;
assert(f);
@@ -356,14 +356,13 @@ void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, siz
fputs("\n", f);
}
-int deserialize_in_addrs(struct in_addr **ret, size_t *ret_size, const char *string) {
+int deserialize_in_addrs(struct in_addr **ret, const char *string) {
_cleanup_free_ struct in_addr *addresses = NULL;
- size_t size = 0;
+ int size = 0;
char *word, *state;
size_t len;
assert(ret);
- assert(ret_size);
assert(string);
FOREACH_WORD(word, len, string, state) {
@@ -388,21 +387,19 @@ int deserialize_in_addrs(struct in_addr **ret, size_t *ret_size, const char *str
size ++;
}
- *ret_size = size;
*ret = addresses;
addresses = NULL;
- return 0;
+ return size;
}
-int deserialize_in6_addrs(struct in6_addr **ret, size_t *ret_size, const char *string) {
+int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
_cleanup_free_ struct in6_addr *addresses = NULL;
- size_t size = 0;
+ int size = 0;
char *word, *state;
size_t len;
assert(ret);
- assert(ret_size);
assert(string);
FOREACH_WORD(word, len, string, state) {
@@ -427,11 +424,10 @@ int deserialize_in6_addrs(struct in6_addr **ret, size_t *ret_size, const char *s
size++;
}
- *ret_size = size;
*ret = addresses;
addresses = NULL;
- return 0;
+ return size;
}
void serialize_dhcp_routes(FILE *f, const char *key, struct sd_dhcp_route *routes, size_t size) {
diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h
index 4bde14641e..53c364a4c9 100644
--- a/src/libsystemd-network/network-internal.h
+++ b/src/libsystemd-network/network-internal.h
@@ -67,10 +67,9 @@ int net_parse_inaddr(const char *address, unsigned char *family, void *dst);
int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
const char *net_get_name(struct udev_device *device);
-void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size);
-int deserialize_in_addrs(struct in_addr **addresses, size_t *size, const char *string);
-int deserialize_in6_addrs(struct in6_addr **addresses, size_t *size, const char *string);
-
+void serialize_in_addrs(FILE *f, const char *key, const struct in_addr *addresses, size_t size);
+int deserialize_in_addrs(struct in_addr **addresses, const char *string);
+int deserialize_in6_addrs(struct in6_addr **addresses, const char *string);
/* don't include "dhcp-lease-internal.h" as it causes conflicts between netinet/ip.h and linux/ip.h */
struct sd_dhcp_route;
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 20a95f6de6..4ed30f5d03 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -68,28 +68,26 @@ int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
return 0;
}
-int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size) {
+int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
- assert_return(addr_size, -EINVAL);
if (lease->dns_size) {
- *addr_size = lease->dns_size;
*addr = lease->dns;
+ return lease->dns_size;
} else
return -ENOENT;
return 0;
}
-int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size) {
+int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
- assert_return(addr_size, -EINVAL);
if (lease->ntp_size) {
- *addr_size = lease->ntp_size;
*addr = lease->ntp;
+ return lease->ntp_size;
} else
return -ENOENT;
@@ -171,16 +169,14 @@ int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) {
return 0;
}
-int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes,
- size_t *routes_size) {
+int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes) {
assert_return(lease, -EINVAL);
assert_return(routes, -EINVAL);
- assert_return(routes_size, -EINVAL);
if (lease->static_route_size) {
*routes = lease->static_route;
- *routes_size = lease->static_route_size;
+ return lease->static_route_size;
} else
return -ENOENT;
@@ -587,12 +583,10 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
struct in_addr address;
- struct in_addr *addresses;
- size_t addresses_size;
+ const struct in_addr *addresses;
const char *string;
uint16_t mtu;
struct sd_dhcp_route *routes;
- size_t routes_size;
int r;
assert(lease);
@@ -635,13 +629,13 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r >= 0)
fprintf(f, "MTU=%" PRIu16 "\n", mtu);
- r = sd_dhcp_lease_get_dns(lease, &addresses, &addresses_size);
+ r = sd_dhcp_lease_get_dns(lease, &addresses);
if (r >= 0)
- serialize_in_addrs(f, "DNS", addresses, addresses_size);
+ serialize_in_addrs(f, "DNS", addresses, r);
- r = sd_dhcp_lease_get_ntp(lease, &addresses, &addresses_size);
+ r = sd_dhcp_lease_get_ntp(lease, &addresses);
if (r >= 0)
- serialize_in_addrs(f, "NTP", addresses, addresses_size);
+ serialize_in_addrs(f, "NTP", addresses, r);
r = sd_dhcp_lease_get_domainname(lease, &string);
if (r >= 0)
@@ -655,9 +649,9 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r >= 0)
fprintf(f, "ROOT_PATH=%s\n", string);
- r = sd_dhcp_lease_get_routes(lease, &routes, &routes_size);
+ r = sd_dhcp_lease_get_routes(lease, &routes);
if (r >= 0)
- serialize_dhcp_routes(f, "ROUTES", routes, routes_size);
+ serialize_dhcp_routes(f, "ROUTES", routes, r);
r = 0;
@@ -750,15 +744,19 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
}
if (dns) {
- r = deserialize_in_addrs(&lease->dns, &lease->dns_size, dns);
+ r = deserialize_in_addrs(&lease->dns, dns);
if (r < 0)
return r;
+
+ lease->dns_size = r;
}
if (ntp) {
- r = deserialize_in_addrs(&lease->ntp, &lease->ntp_size, dns);
+ r = deserialize_in_addrs(&lease->ntp, ntp);
if (r < 0)
return r;
+
+ lease->ntp_size = r;
}
if (mtu) {
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 0fb323b9a5..94683a5403 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -401,20 +401,18 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
static int link_set_dhcp_routes(Link *link) {
struct sd_dhcp_route *static_routes;
- size_t static_routes_size;
- int r;
- unsigned i;
+ int r, n, i;
assert(link);
- r = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes, &static_routes_size);
- if (r < 0) {
- if (r != -ENOENT)
- log_warning_link(link, "DHCP error: could not get routes: %s", strerror(-r));
- return r;
+ n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
+ if (n < 0) {
+ if (n != -ENOENT)
+ log_warning_link(link, "DHCP error: could not get routes: %s", strerror(-n));
+ return n;
}
- for (i = 0; i < static_routes_size; i++) {
+ for (i = 0; i < n; i++) {
_cleanup_route_free_ Route *route = NULL;
r = route_new_dynamic(&route);
@@ -955,7 +953,6 @@ static int dhcp_lease_lost(Link *link) {
struct in_addr netmask;
struct in_addr gateway;
unsigned prefixlen;
- unsigned i;
int r;
assert(link);
@@ -965,11 +962,11 @@ static int dhcp_lease_lost(Link *link) {
if (link->network->dhcp_routes) {
struct sd_dhcp_route *routes;
- size_t routes_size;
+ int n, i;
- r = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes, &routes_size);
- if (r >= 0) {
- for (i = 0; i < routes_size; i++) {
+ n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
+ if (n >= 0) {
+ for (i = 0; i < n; i++) {
_cleanup_route_free_ Route *route = NULL;
r = route_new_dynamic(&route);
diff --git a/src/network/sd-network.c b/src/network/sd-network.c
index 2057e53774..317d087b98 100644
--- a/src/network/sd-network.c
+++ b/src/network/sd-network.c
@@ -168,13 +168,12 @@ _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
return 0;
}
-static int network_get_in_addr(const char *key, unsigned index, struct in_addr **addr, size_t *addr_size) {
+static int network_get_in_addr(const char *key, unsigned index, struct in_addr **addr) {
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
assert_return(index, -EINVAL);
assert_return(addr, -EINVAL);
- assert_return(addr_size, -EINVAL);
if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
return -ENOMEM;
@@ -185,24 +184,23 @@ static int network_get_in_addr(const char *key, unsigned index, struct in_addr *
else if (!s)
return -EIO;
- return deserialize_in_addrs(addr, addr_size, s);
+ return deserialize_in_addrs(addr, s);
}
-_public_ int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *addr_size) {
- return network_get_in_addr("DNS", index, addr, addr_size);
+_public_ int sd_network_get_dns(unsigned index, struct in_addr **addr) {
+ return network_get_in_addr("DNS", index, addr);
}
-_public_ int sd_network_get_ntp(unsigned index, struct in_addr **addr, size_t *addr_size) {
- return network_get_in_addr("NTP", index, addr, addr_size);
+_public_ int sd_network_get_ntp(unsigned index, struct in_addr **addr) {
+ return network_get_in_addr("NTP", index, addr);
}
-static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr **addr, size_t *addr_size) {
+static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr **addr) {
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
assert_return(index, -EINVAL);
assert_return(addr, -EINVAL);
- assert_return(addr_size, -EINVAL);
if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
return -ENOMEM;
@@ -213,15 +211,15 @@ static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr
else if (!s)
return -EIO;
- return deserialize_in6_addrs(addr, addr_size, s);
+ return deserialize_in6_addrs(addr, s);
}
-_public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t *addr_size) {
- return network_get_in6_addr("DNS", index, addr, addr_size);
+_public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr) {
+ return network_get_in6_addr("DNS", index, addr);
}
-_public_ int sd_network_get_ntp6(unsigned index, struct in6_addr **addr, size_t *addr_size) {
- return network_get_in6_addr("NTP", index, addr, addr_size);
+_public_ int sd_network_get_ntp6(unsigned index, struct in6_addr **addr) {
+ return network_get_in6_addr("NTP", index, addr);
}
static int network_get_boolean(const char *key, unsigned index) {
diff --git a/src/network/test-network.c b/src/network/test-network.c
index 08b37b10b3..3dc051dd92 100644
--- a/src/network/test-network.c
+++ b/src/network/test-network.c
@@ -28,7 +28,7 @@ static void test_deserialize_in_addr(void) {
_cleanup_free_ struct in6_addr *addresses6 = NULL;
struct in_addr a, b, c;
struct in6_addr d, e, f;
- size_t size;
+ int size;
const char *addresses_string = "192.168.0.1 0:0:0:0:0:FFFF:204.152.189.116 192.168.0.2 ::1 192.168.0.3 1:0:0:0:0:0:0:8";
assert_se(inet_pton(AF_INET, "0:0:0:0:0:FFFF:204.152.189.116", &a) == 0);
@@ -41,13 +41,13 @@ static void test_deserialize_in_addr(void) {
assert_se(inet_pton(AF_INET6, "::1", &e) == 1);
assert_se(inet_pton(AF_INET6, "1:0:0:0:0:0:0:8", &f) == 1);
- assert_se(deserialize_in_addrs(&addresses, &size, addresses_string) >= 0);
+ assert_se((size = deserialize_in_addrs(&addresses, addresses_string)) >= 0);
assert_se(size == 3);
assert_se(!memcmp(&a, &addresses[0], sizeof(struct in_addr)));
assert_se(!memcmp(&b, &addresses[1], sizeof(struct in_addr)));
assert_se(!memcmp(&c, &addresses[2], sizeof(struct in_addr)));
- assert_se(deserialize_in6_addrs(&addresses6, &size, addresses_string) >= 0);
+ assert_se((size = deserialize_in6_addrs(&addresses6, addresses_string)) >= 0);
assert_se(size == 3);
assert_se(!memcmp(&d, &addresses6[0], sizeof(struct in6_addr)));
assert_se(!memcmp(&e, &addresses6[1], sizeof(struct in6_addr)));
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c
index 61b112cb05..e309b80728 100644
--- a/src/resolve/resolved-link.c
+++ b/src/resolve/resolved-link.c
@@ -100,11 +100,9 @@ int link_update_rtnl(Link *l, sd_rtnl_message *m) {
static int update_dhcp_dns_servers(Link *l) {
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
- struct in_addr *nameservers = NULL;
+ const struct in_addr *nameservers = NULL;
DnsServer *s, *nx;
- unsigned i;
- size_t n;
- int r;
+ int r, n, i;
assert(l);
@@ -119,9 +117,11 @@ static int update_dhcp_dns_servers(Link *l) {
LIST_FOREACH(servers, s, l->dhcp_dns_servers)
s->marked = true;
- r = sd_dhcp_lease_get_dns(lease, &nameservers, &n);
- if (r < 0)
+ n = sd_dhcp_lease_get_dns(lease, &nameservers);
+ if (n < 0) {
+ r = n;
goto clear;
+ }
for (i = 0; i < n; i++) {
union in_addr_union a = { .in = nameservers[i] };
@@ -153,18 +153,18 @@ static int update_link_dns_servers(Link *l) {
_cleanup_free_ struct in_addr *nameservers = NULL;
_cleanup_free_ struct in6_addr *nameservers6 = NULL;
DnsServer *s, *nx;
- unsigned i;
- size_t n;
- int r;
+ int r, n, i;
assert(l);
LIST_FOREACH(servers, s, l->link_dns_servers)
s->marked = true;
- r = sd_network_get_dns(l->ifindex, &nameservers, &n);
- if (r < 0)
+ n = sd_network_get_dns(l->ifindex, &nameservers);
+ if (n < 0) {
+ r = n;
goto clear;
+ }
for (i = 0; i < n; i++) {
union in_addr_union a = { .in = nameservers[i] };
@@ -179,9 +179,11 @@ static int update_link_dns_servers(Link *l) {
}
}
- r = sd_network_get_dns6(l->ifindex, &nameservers6, &n);
- if (r < 0)
+ n = sd_network_get_dns6(l->ifindex, &nameservers6);
+ if (n < 0) {
+ r = n;
goto clear;
+ }
for (i = 0; i < n; i++) {
union in_addr_union a = { .in6 = nameservers6[i] };
diff --git a/src/systemd/sd-dhcp-lease.h b/src/systemd/sd-dhcp-lease.h
index 6e8cefff4f..a3728a702f 100644
--- a/src/systemd/sd-dhcp-lease.h
+++ b/src/systemd/sd-dhcp-lease.h
@@ -38,12 +38,12 @@ int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr);
int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr);
-int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size);
-int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size);
+int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr);
+int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu);
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname);
int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname);
int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path);
-int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes, size_t *routes_size);
+int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routesgn);
#endif
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index 0eaefdc7b1..30e920db32 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -87,16 +87,16 @@ int sd_network_dhcp_use_dns(unsigned index);
int sd_network_dhcp_use_ntp(unsigned index);
/* Get IPv4 DNS entries statically configured for the link */
-int sd_network_get_dns(unsigned index, struct in_addr **addr, size_t *addr_size);
+int sd_network_get_dns(unsigned index, struct in_addr **addr);
/* Get IPv4 NTP entries statically configured for the link */
-int sd_network_get_ntp(unsigned index, struct in_addr **addr, size_t *addr_size);
+int sd_network_get_ntp(unsigned index, struct in_addr **addr);
/* Get IPv6 DNS entries statically configured for the link */
-int sd_network_get_dns6(unsigned index, struct in6_addr **addr, size_t *addr_size);
+int sd_network_get_dns6(unsigned index, struct in6_addr **addr);
/* Get IPv6 NTP entries statically configured for the link */
-int sd_network_get_ntp6(unsigned index, struct in6_addr **addr, size_t *addr_size);
+int sd_network_get_ntp6(unsigned index, struct in6_addr **addr);
/* Get all network interfaces' indices, and store them in *indices. Returns
* the number of indices. If indices is NULL, only returns the number of indices. */