basic/in-addr-util: add IN_ADDR_TO_STRING

Since we don't need the error value, and the buffer is allocated with a fixed
size, the whole logic provided by in_addr_to_string() becomes unnecessary, so
it's enough to wrap inet_ntop() directly.

inet_ntop() can only fail with ENOSPC. But we specify a buffer that is supposed
to be large enough, so this should never fail. A bunch of tests of this are added.
This allows all the wrappers like strna(), strnull(), strempty() to be dropped.

The guard of 'if (DEBUG_LOGGING)' can be dropped from around log_debug(),
because log_debug() implements the check outside of the function call. But
log_link_debug() does not, so it we need it to avoid unnecessary evaluation of
the formatting.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-06-06 09:52:52 +02:00
parent b547241728
commit 84dbb3fd83
23 changed files with 98 additions and 134 deletions
+3 -6
View File
@@ -1344,13 +1344,10 @@ int sd_dhcp6_client_start(sd_dhcp6_client *client) {
if (client->fd < 0) {
r = dhcp6_network_bind_udp_socket(client->ifindex, &client->local_address);
if (r < 0) {
_cleanup_free_ char *p = NULL;
(void) in6_addr_to_string(&client->local_address, &p);
if (r < 0)
return log_dhcp6_client_errno(client, r,
"Failed to bind to UDP socket at address %s: %m", strna(p));
}
"Failed to bind to UDP socket at address %s: %m",
IN6_ADDR_TO_STRING(&client->local_address));
client->fd = r;
}
+1 -3
View File
@@ -240,7 +240,6 @@ int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) {
#define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
static int ipv4ll_pick_address(sd_ipv4ll *ll) {
_cleanup_free_ char *address = NULL;
be32_t addr;
assert(ll);
@@ -257,8 +256,7 @@ static int ipv4ll_pick_address(sd_ipv4ll *ll) {
} while (addr == ll->address ||
IN_SET(be32toh(addr) & 0x0000FF00U, 0x0000U, 0xFF00U));
(void) in_addr_to_string(AF_INET, &(union in_addr_union) { .in.s_addr = addr }, &address);
log_ipv4ll(ll, "Picked new IP address %s.", strna(address));
log_ipv4ll(ll, "Picked new IP address %s.", IN4_ADDR_TO_STRING((const struct in_addr*) &addr));
return sd_ipv4ll_set_address(ll, &(struct in_addr) { addr });
}
+2 -3
View File
@@ -203,7 +203,6 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda
sd_ndisc *nd = userdata;
ssize_t buflen;
int r;
_cleanup_free_ char *addr = NULL;
assert(s);
assert(nd);
@@ -229,8 +228,8 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda
switch (r) {
case -EADDRNOTAVAIL:
(void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &rt->address, &addr);
log_ndisc(nd, "Received RA from non-link-local address %s. Ignoring", addr);
log_ndisc(nd, "Received RA from non-link-local address %s. Ignoring.",
IN6_ADDR_TO_STRING(&rt->address));
break;
case -EMULTIHOP:
+5 -6
View File
@@ -244,7 +244,6 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_us
static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
sd_radv *ra = userdata;
_cleanup_free_ char *addr = NULL;
struct in6_addr src;
triple_timestamp timestamp;
int r;
@@ -275,8 +274,8 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
switch (r) {
case -EADDRNOTAVAIL:
(void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr);
log_radv(ra, "Received RS from non-link-local address %s. Ignoring", addr);
log_radv(ra, "Received RS from non-link-local address %s. Ignoring",
IN6_ADDR_TO_STRING(&src));
break;
case -EMULTIHOP:
@@ -300,13 +299,13 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
return 0;
}
(void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr);
const char *addr = IN6_ADDR_TO_STRING(&src);
r = radv_send(ra, &src, ra->lifetime_usec);
if (r < 0)
log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", strnull(addr));
log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", addr);
else
log_radv(ra, "Sent solicited Router Advertisement to %s", strnull(addr));
log_radv(ra, "Sent solicited Router Advertisement to %s", addr);
return 0;
}
+5 -8
View File
@@ -19,23 +19,20 @@
#include "util.h"
static void ll_handler(sd_ipv4ll *ll, int event, void *userdata) {
_cleanup_free_ char *address = NULL;
struct in_addr addr = {};
assert_se(ll);
if (sd_ipv4ll_get_address(ll, &addr) >= 0)
assert_se(in_addr_to_string(AF_INET, (const union in_addr_union*) &addr, &address) >= 0);
struct in_addr addr;
const char *pretty = sd_ipv4ll_get_address(ll, &addr) >= 0 ? IN4_ADDR_TO_STRING(&addr) : NULL;
switch (event) {
case SD_IPV4LL_EVENT_BIND:
log_info("bound %s", strna(address));
log_info("bound %s", strna(pretty));
break;
case SD_IPV4LL_EVENT_CONFLICT:
log_info("conflict on %s", strna(address));
log_info("conflict on %s", strna(pretty));
break;
case SD_IPV4LL_EVENT_STOP:
log_error("the client was stopped with address %s", strna(address));
log_error("the client was stopped with address %s", strna(pretty));
break;
default:
assert_not_reached();