]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/in-addr-util: add IN_ADDR_TO_STRING
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 3 Jun 2022 09:24:02 +0000 (11:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Jun 2022 07:52:52 +0000 (09:52 +0200)
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.

23 files changed:
src/basic/in-addr-util.c
src/basic/in-addr-util.h
src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd-network/sd-ipv4ll.c
src/libsystemd-network/sd-ndisc.c
src/libsystemd-network/sd-radv.c
src/libsystemd-network/test-ipv4ll-manual.c
src/network/netdev/l2tp-tunnel.c
src/network/networkd-address.c
src/network/networkd-bridge-mdb.c
src/network/networkd-dhcp6.c
src/network/networkd-ipv6ll.c
src/network/networkd-ndisc.c
src/network/networkd-neighbor.c
src/network/networkd-nexthop.c
src/nspawn/nspawn-expose-ports.c
src/resolve/resolved-bus.c
src/resolve/resolved-dns-cache.c
src/resolve/resolved-dns-transaction.c
src/resolve/resolved-manager.c
src/resolve/test-resolved-stream.c
src/test/test-in-addr-util.c
src/test/test-utmp.c

index 660c1f18247c93417b523fac20437d1b0ad85718..39ae694f20d4bc60545b00ba979fa5c8c57ceaa7 100644 (file)
@@ -444,7 +444,7 @@ int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
                 return -ENOMEM;
 
         errno = 0;
-        if (!inet_ntop(family, u, x, l))
+        if (!typesafe_inet_ntop(family, u, x, l))
                 return errno_or_else(EINVAL);
 
         *ret = TAKE_PTR(x);
index 5de87a9539f472845a988d25a1137be0389494d0..89d0505c137a45aa6360d2d1f95122407e4c5ef9 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
+#include <arpa/inet.h>
 #include <netinet/in.h>
 #include <stddef.h>
 #include <sys/socket.h>
@@ -68,10 +69,30 @@ int in_addr_prefix_range(
                 unsigned prefixlen,
                 union in_addr_union *ret_start,
                 union in_addr_union *ret_end);
+
 int in_addr_to_string(int family, const union in_addr_union *u, char **ret);
 static inline int in6_addr_to_string(const struct in6_addr *u, char **ret) {
         return in_addr_to_string(AF_INET6, (const union in_addr_union*) u, ret);
 }
+
+static inline const char* typesafe_inet_ntop(int family, const union in_addr_union *a, char *buf, size_t len) {
+        return inet_ntop(family, a, buf, len);
+}
+static inline const char* typesafe_inet_ntop4(const struct in_addr *a, char *buf, size_t len) {
+        return inet_ntop(AF_INET, a, buf, len);
+}
+static inline const char* typesafe_inet_ntop6(const struct in6_addr *a, char *buf, size_t len) {
+        return inet_ntop(AF_INET6, a, buf, len);
+}
+
+/* Note: the lifetime of the compound literal is the immediately surrounding block,
+ * see C11 §6.5.2.5, and
+ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
+#define IN_ADDR_MAX CONST_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)
+#define IN_ADDR_TO_STRING(family, addr) typesafe_inet_ntop(family, addr, (char[IN_ADDR_MAX]){}, IN_ADDR_MAX)
+#define IN4_ADDR_TO_STRING(addr) typesafe_inet_ntop4(addr, (char[INET_ADDRSTRLEN]){}, INET_ADDRSTRLEN)
+#define IN6_ADDR_TO_STRING(addr) typesafe_inet_ntop6(addr, (char[INET6_ADDRSTRLEN]){}, INET6_ADDRSTRLEN)
+
 int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret);
 static inline int in6_addr_prefix_to_string(const struct in6_addr *u, unsigned prefixlen, char **ret) {
         return in_addr_prefix_to_string(AF_INET6, (const union in_addr_union*) u, prefixlen, ret);
index b346a50d780515e10e1cbd5ede39bf7382a4fdff..4ca51591065bec6f458b6e79572d138bebfb89d9 100644 (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;
         }
index ff065fd1b6469d2658250d9ddd3321950d376b12..fe0d8361655063880aa26314902c1a0ddc1e85e1 100644 (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 });
 }
index ecd552ca0cbf7dc84e39f72fb98484ff1e973b69..f7522aa8d6c6f145cc2de14a0abd83c22cb0cdd5 100644 (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:
index 7b5c12e4893925eeba905b15429f1e79b94e65c0..47339cf2391426fcc51cae76c3293fbceaa5da9a 100644 (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;
 }
index 3fea894f302f26edd68e8f474991528ffa6acddc..e79758c5f5c56cce075c20c87032596b37c626a5 100644 (file)
 #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();
index 05af5dbf8988d14dd4c102d14e46cfad171a6f7c..dcb70bfef2dd161f76b20c724dc88241036b8400 100644 (file)
@@ -445,12 +445,9 @@ static int l2tp_create_tunnel(NetDev *netdev) {
         if (r < 0)
                 return log_netdev_error_errno(netdev, r, "Could not find local address.");
 
-        if (t->local_address_type >= 0 && DEBUG_LOGGING) {
-                _cleanup_free_ char *str = NULL;
-
-                (void) in_addr_to_string(t->family, &local_address, &str);
-                log_netdev_debug(netdev, "Local address %s acquired.", strna(str));
-        }
+        if (t->local_address_type >= 0 && DEBUG_LOGGING)
+                log_netdev_debug(netdev, "Local address %s acquired.",
+                                 IN_ADDR_TO_STRING(t->family, &local_address));
 
         r = netdev_l2tp_create_message_tunnel(netdev, &local_address, &m);
         if (r < 0)
index afcd53cbc780ea3a80ec74e3bd3d9a1f45d19f7f..5e37dc9907f9dcecccc08725d445e7320cb45479 100644 (file)
@@ -648,7 +648,7 @@ const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) {
 }
 
 static void log_address_debug(const Address *address, const char *str, const Link *link) {
-        _cleanup_free_ char *state = NULL, *addr = NULL, *peer = NULL, *flags_str = NULL, *scope_str = NULL;
+        _cleanup_free_ char *state = NULL, *flags_str = NULL, *scope_str = NULL;
 
         assert(address);
         assert(str);
@@ -658,16 +658,17 @@ static void log_address_debug(const Address *address, const char *str, const Lin
                 return;
 
         (void) network_config_state_to_string_alloc(address->state, &state);
-        (void) in_addr_to_string(address->family, &address->in_addr, &addr);
-        if (in_addr_is_set(address->family, &address->in_addr_peer))
-                (void) in_addr_to_string(address->family, &address->in_addr_peer, &peer);
+
+        const char *peer = in_addr_is_set(address->family, &address->in_addr_peer) ?
+                IN_ADDR_TO_STRING(address->family, &address->in_addr_peer) : NULL;
 
         (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
         (void) route_scope_to_string_alloc(address->scope, &scope_str);
 
         log_link_debug(link, "%s %s address (%s): %s%s%s/%u (valid %s, preferred %s), flags: %s, scope: %s",
                        str, strna(network_config_source_to_string(address->source)), strna(state),
-                       strnull(addr), peer ? " peer " : "", strempty(peer), address->prefixlen,
+                       IN_ADDR_TO_STRING(address->family, &address->in_addr),
+                       peer ? " peer " : "", strempty(peer), address->prefixlen,
                        FORMAT_LIFETIME(address->lifetime_valid_usec),
                        FORMAT_LIFETIME(address->lifetime_preferred_usec),
                        strna(flags_str), strna(scope_str));
index 3af4afe4a1d2826f3ce6d1cd0b4ede46157dd864..bd1a9745dce63fa73e137ef38a7c9d1df6cabb22 100644 (file)
@@ -119,13 +119,9 @@ static int bridge_mdb_configure(BridgeMDB *mdb, Link *link, Request *req) {
         assert(link->manager);
         assert(req);
 
-        if (DEBUG_LOGGING) {
-                _cleanup_free_ char *a = NULL;
-
-                (void) in_addr_to_string(mdb->family, &mdb->group_addr, &a);
+        if (DEBUG_LOGGING)
                 log_link_debug(link, "Configuring bridge MDB entry: MulticastGroupAddress=%s, VLANId=%u",
-                               strna(a), mdb->vlan_id);
-        }
+                               IN_ADDR_TO_STRING(mdb->family, &mdb->group_addr), mdb->vlan_id);
 
         entry = (struct br_mdb_entry) {
                 /* If MDB entry is added on bridge master, then the state must be MDB_TEMPORARY.
index 5786eca8039e0ce0e4323394d5d1d2c2dea1d157..3dc34f0e52c6bdbd8bc889e834a3987e0966b6a8 100644 (file)
@@ -153,7 +153,6 @@ static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Reques
 }
 
 static int verify_dhcp6_address(Link *link, const Address *address) {
-        _cleanup_free_ char *buffer = NULL;
         bool by_ndisc = false;
         Address *existing;
         int log_level;
@@ -162,7 +161,7 @@ static int verify_dhcp6_address(Link *link, const Address *address) {
         assert(address);
         assert(address->family == AF_INET6);
 
-        (void) in6_addr_to_string(&address->in_addr.in6, &buffer);
+        const char *pretty = IN6_ADDR_TO_STRING(&address->in_addr.in6);
 
         if (address_get(link, address, &existing) < 0 &&
             link_get_address(link, AF_INET6, &address->in_addr, 0, &existing) < 0) {
@@ -180,10 +179,10 @@ static int verify_dhcp6_address(Link *link, const Address *address) {
                 by_ndisc = true;
 
         log_link_warning(link, "Ignoring DHCPv6 address %s/%u (valid %s, preferred %s) which conflicts with %s/%u%s.",
-                         strna(buffer), address->prefixlen,
+                         pretty, address->prefixlen,
                          FORMAT_LIFETIME(address->lifetime_valid_usec),
                          FORMAT_LIFETIME(address->lifetime_preferred_usec),
-                         strna(buffer), existing->prefixlen,
+                         pretty, existing->prefixlen,
                          by_ndisc ? " assigned by NDisc" : "");
         if (by_ndisc)
                 log_link_warning(link, "Hint: use IPv6Token= setting to change the address generated by NDisc or set UseAutonomousPrefix=no.");
@@ -192,7 +191,7 @@ static int verify_dhcp6_address(Link *link, const Address *address) {
 
 simple_log:
         log_link_full(link, log_level, "DHCPv6 address %s/%u (valid %s, preferred %s)",
-                      strna(buffer), address->prefixlen,
+                      pretty, address->prefixlen,
                       FORMAT_LIFETIME(address->lifetime_valid_usec),
                       FORMAT_LIFETIME(address->lifetime_preferred_usec));
         return 0;
@@ -232,13 +231,9 @@ static int dhcp6_request_address(
 
         r = link_request_address(link, TAKE_PTR(addr), true, &link->dhcp6_messages,
                                  dhcp6_address_handler, NULL);
-        if (r < 0) {
-                _cleanup_free_ char *buffer = NULL;
-
-                (void) in6_addr_to_string(ip6_addr, &buffer);
-                return log_link_error_errno(link, r, "Failed to request DHCPv6 address %s/128: %m", strna(buffer));
-        }
-
+        if (r < 0)
+                return log_link_error_errno(link, r, "Failed to request DHCPv6 address %s/128: %m",
+                                            IN6_ADDR_TO_STRING(ip6_addr));
         return 0;
 }
 
index dc09171afee9ef4b92d6b99825d2febe67e0f97b..9b859897019e3d9b664b78c05f608fcba3d08f3f 100644 (file)
@@ -182,7 +182,6 @@ int link_update_ipv6ll_addrgen_mode(Link *link, sd_netlink_message *message) {
 #define STABLE_SECRET_APP_ID_2 SD_ID128_MAKE(52,c4,40,a0,9f,2f,48,58,a9,3a,f6,29,25,ba,7a,7d)
 
 int link_set_ipv6ll_stable_secret(Link *link) {
-        _cleanup_free_ char *str = NULL;
         struct in6_addr a;
         int r;
 
@@ -216,11 +215,8 @@ int link_set_ipv6ll_stable_secret(Link *link) {
                 memcpy(a.s6_addr + sizeof(v), &v, sizeof(v));
         }
 
-        r = in6_addr_to_string(&a, &str);
-        if (r < 0)
-                return r;
-
-        return sysctl_write_ip_property(AF_INET6, link->ifname, "stable_secret", str);
+        return sysctl_write_ip_property(AF_INET6, link->ifname, "stable_secret",
+                                        IN6_ADDR_TO_STRING(&a));
 }
 
 int link_set_ipv6ll_addrgen_mode(Link *link, IPv6LinkLocalAddressGenMode mode) {
index efe407fedb563f185ce1888059409953141adca3..66127f36c9a2d126639ebd7ec5c54355263ecc5b 100644 (file)
@@ -327,13 +327,9 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
                 return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m");
 
         if (link_get_ipv6_address(link, &gateway, 0, NULL) >= 0) {
-                if (DEBUG_LOGGING) {
-                        _cleanup_free_ char *buffer = NULL;
-
-                        (void) in6_addr_to_string(&gateway, &buffer);
+                if (DEBUG_LOGGING)
                         log_link_debug(link, "No NDisc route added, gateway %s matches local address",
-                                       strna(buffer));
-                }
+                                       IN6_ADDR_TO_STRING(&gateway));
                 return 0;
         }
 
@@ -639,12 +635,9 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
                 return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m");
 
         if (link_get_ipv6_address(link, &gateway, 0, NULL) >= 0) {
-                if (DEBUG_LOGGING) {
-                        _cleanup_free_ char *buf = NULL;
-
-                        (void) in6_addr_to_string(&gateway, &buf);
-                        log_link_debug(link, "Advertised route gateway %s is local to the link, ignoring route", strna(buf));
-                }
+                if (DEBUG_LOGGING)
+                        log_link_debug(link, "Advertised route gateway %s is local to the link, ignoring route",
+                                       IN6_ADDR_TO_STRING(&gateway));
                 return 0;
         }
 
@@ -986,13 +979,10 @@ static int ndisc_router_handler(Link *link, sd_ndisc_router *rt) {
 
         if (in6_prefix_is_filtered(&router, 128, link->network->ndisc_allow_listed_router, link->network->ndisc_deny_listed_router)) {
                 if (DEBUG_LOGGING) {
-                        _cleanup_free_ char *buf = NULL;
-
-                        (void) in6_addr_to_string(&router, &buf);
                         if (!set_isempty(link->network->ndisc_allow_listed_router))
-                                log_link_debug(link, "Router '%s' is not in allow list, ignoring", strna(buf));
+                                log_link_debug(link, "Router %s is not in allow list, ignoring.", IN6_ADDR_TO_STRING(&router));
                         else
-                                log_link_debug(link, "Router '%s' is in deny list, ignoring", strna(buf));
+                                log_link_debug(link, "Router %s is in deny list, ignoring.", IN6_ADDR_TO_STRING(&router));
                 }
                 return 0;
         }
index 435fa7002e2c965974e89e62b07d5e46192fa74f..f11f06001bf7517cc5757331e297c99ab69b2cff 100644 (file)
@@ -158,7 +158,7 @@ static int neighbor_add(Link *link, Neighbor *neighbor) {
 }
 
 static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const Link *link) {
-        _cleanup_free_ char *state = NULL, *dst = NULL;
+        _cleanup_free_ char *state = NULL;
 
         assert(neighbor);
         assert(str);
@@ -167,12 +167,12 @@ static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const
                 return;
 
         (void) network_config_state_to_string_alloc(neighbor->state, &state);
-        (void) in_addr_to_string(neighbor->family, &neighbor->in_addr, &dst);
 
         log_link_debug(link,
                        "%s %s neighbor (%s): lladdr: %s, dst: %s",
                        str, strna(network_config_source_to_string(neighbor->source)), strna(state),
-                       HW_ADDR_TO_STR(&neighbor->ll_addr), strna(dst));
+                       HW_ADDR_TO_STR(&neighbor->ll_addr),
+                       IN_ADDR_TO_STRING(neighbor->family, &neighbor->in_addr));
 }
 
 static int neighbor_configure_message(Neighbor *neighbor, Link *link, sd_netlink_message *req) {
index 3eb9d62f7c778ea83835d7d604624f0dfc84822b..a3b50cefe1ad2211fa0c79e2117a9ab54269d825 100644 (file)
@@ -346,7 +346,7 @@ static int nexthop_acquire_id(Manager *manager, NextHop *nexthop) {
 }
 
 static void log_nexthop_debug(const NextHop *nexthop, const char *str, const Link *link) {
-        _cleanup_free_ char *state = NULL, *gw = NULL, *group = NULL, *flags = NULL;
+        _cleanup_free_ char *state = NULL, *group = NULL, *flags = NULL;
         struct nexthop_grp *nhg;
 
         assert(nexthop);
@@ -358,7 +358,6 @@ static void log_nexthop_debug(const NextHop *nexthop, const char *str, const Lin
                 return;
 
         (void) network_config_state_to_string_alloc(nexthop->state, &state);
-        (void) in_addr_to_string(nexthop->family, &nexthop->gw, &gw);
         (void) route_flags_to_string_alloc(nexthop->flags, &flags);
 
         HASHMAP_FOREACH(nhg, nexthop->group)
@@ -366,7 +365,9 @@ static void log_nexthop_debug(const NextHop *nexthop, const char *str, const Lin
 
         log_link_debug(link, "%s %s nexthop (%s): id: %"PRIu32", gw: %s, blackhole: %s, group: %s, flags: %s",
                        str, strna(network_config_source_to_string(nexthop->source)), strna(state),
-                       nexthop->id, strna(gw), yes_no(nexthop->blackhole), strna(group), strna(flags));
+                       nexthop->id,
+                       IN_ADDR_TO_STRING(nexthop->family, &nexthop->gw),
+                       yes_no(nexthop->blackhole), strna(group), strna(flags));
 }
 
 static int nexthop_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
index 166383cce3bc6899b4a805778659ed5c1652e7c3..bb54f8c1e77dc14a96545df7203c90a5871c52e1 100644 (file)
@@ -141,14 +141,9 @@ int expose_port_execute(sd_netlink *rtnl, FirewallContext **fw_ctx, ExposePort *
         if (in_addr_equal(af, exposed, &new_exposed))
                 return 0;
 
-        if (DEBUG_LOGGING) {
-                _cleanup_free_ char *pretty = NULL;
-                in_addr_to_string(af, &new_exposed, &pretty);
-                log_debug("New container IP is %s.", strna(pretty));
-        }
+        log_debug("New container IP is %s.", IN_ADDR_TO_STRING(af, &new_exposed));
 
         LIST_FOREACH(ports, p, l) {
-
                 r = fw_add_local_dnat(fw_ctx,
                                       true,
                                       af,
index 8207a0419674955a0ac724f1861f40c6d329b52a..e19d3de8ea75a377cc316ae3a99df29a9f655e6a 100644 (file)
@@ -596,11 +596,9 @@ static void bus_method_resolve_address_complete(DnsQuery *query) {
         }
 
         if (added <= 0) {
-                _cleanup_free_ char *ip = NULL;
-
-                (void) in_addr_to_string(q->request_family, &q->request_address, &ip);
                 r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR,
-                                               "Address '%s' does not have any RR of requested type", strnull(ip));
+                                        "Address %s does not have any RR of requested type",
+                                        IN_ADDR_TO_STRING(q->request_family, &q->request_address));
                 goto finish;
         }
 
index e7ab4e556937c10cc58bbfab6ce4b59f099946e8..3f107603bf5d85aa1593941bfdf65bf5e7cfda41 100644 (file)
@@ -493,21 +493,15 @@ static int dns_cache_put_positive(
         if (r < 0)
                 return r;
 
-        if (DEBUG_LOGGING) {
-                _cleanup_free_ char *t = NULL;
-
-                (void) in_addr_to_string(i->owner_family, &i->owner_address, &t);
-
-                log_debug("Added positive %s %s%s cache entry for %s "USEC_FMT"s on %s/%s/%s",
-                          FLAGS_SET(i->query_flags, SD_RESOLVED_AUTHENTICATED) ? "authenticated" : "unauthenticated",
-                          FLAGS_SET(i->query_flags, SD_RESOLVED_CONFIDENTIAL) ? "confidential" : "non-confidential",
-                          i->shared_owner ? " shared" : "",
-                          dns_resource_key_to_string(i->key, key_str, sizeof key_str),
-                          (i->until - timestamp) / USEC_PER_SEC,
-                          i->ifindex == 0 ? "*" : FORMAT_IFNAME(i->ifindex),
-                          af_to_name_short(i->owner_family),
-                          strna(t));
-        }
+        log_debug("Added positive %s %s%s cache entry for %s "USEC_FMT"s on %s/%s/%s",
+                  FLAGS_SET(i->query_flags, SD_RESOLVED_AUTHENTICATED) ? "authenticated" : "unauthenticated",
+                  FLAGS_SET(i->query_flags, SD_RESOLVED_CONFIDENTIAL) ? "confidential" : "non-confidential",
+                  i->shared_owner ? " shared" : "",
+                  dns_resource_key_to_string(i->key, key_str, sizeof key_str),
+                  (i->until - timestamp) / USEC_PER_SEC,
+                  i->ifindex == 0 ? "*" : FORMAT_IFNAME(i->ifindex),
+                  af_to_name_short(i->owner_family),
+                  IN_ADDR_TO_STRING(i->owner_family, &i->owner_address));
 
         i = NULL;
         return 0;
index 158e5b44b071af967777085e6300854cdff032a4..ba76725655bad9af1ab791d45d344930a46bb574 100644 (file)
@@ -333,7 +333,6 @@ static void dns_transaction_shuffle_id(DnsTransaction *t) {
 }
 
 static void dns_transaction_tentative(DnsTransaction *t, DnsPacket *p) {
-        _cleanup_free_ char *pretty = NULL;
         char key_str[DNS_RESOURCE_KEY_STRING_MAX];
         DnsZoneItem *z;
 
@@ -344,15 +343,13 @@ static void dns_transaction_tentative(DnsTransaction *t, DnsPacket *p) {
         if (manager_packet_from_local_address(t->scope->manager, p) != 0)
                 return;
 
-        (void) in_addr_to_string(p->family, &p->sender, &pretty);
-
         log_debug("Transaction %" PRIu16 " for <%s> on scope %s on %s/%s got tentative packet from %s.",
                   t->id,
                   dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str),
                   dns_protocol_to_string(t->scope->protocol),
                   t->scope->link ? t->scope->link->ifname : "*",
                   af_to_name_short(t->scope->family),
-                  strnull(pretty));
+                  IN_ADDR_TO_STRING(p->family, &p->sender));
 
         /* RFC 4795, Section 4.1 says that the peer with the
          * lexicographically smaller IP address loses */
index 12e7d87f221ca8e787b08e8f683e409853d2fa9c..d2eaea584be0e21e81c4f369e64566693fc9e8b0 100644 (file)
@@ -870,16 +870,10 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
                         p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
         }
 
-        if (DEBUG_LOGGING) {
-                _cleanup_free_ char *sender_address = NULL, *destination_address = NULL;
-
-                (void) in_addr_to_string(p->family, &p->sender, &sender_address);
-                (void) in_addr_to_string(p->family, &p->destination, &destination_address);
-
-                log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%i, fragsize=%zu, sender=%s, destination=%s",
-                          dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize,
-                          strna(sender_address), strna(destination_address));
-        }
+        log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%i, fragsize=%zu, sender=%s, destination=%s",
+                  dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize,
+                  IN_ADDR_TO_STRING(p->family, &p->sender),
+                  IN_ADDR_TO_STRING(p->family, &p->destination));
 
         *ret = TAKE_PTR(p);
         return 1;
index 267fe83bc75f846835d4c0a06c8369d86680e478..96395ad27a71a50a4b0d0a669d7216d3c60f9f31 100644 (file)
@@ -127,15 +127,15 @@ static void *tls_dns_server(void *p) {
         int r;
         _cleanup_close_ int fd_server = -1, fd_tls = -1;
         _cleanup_free_ char *cert_path = NULL, *key_path = NULL;
-        _cleanup_free_ char *ip_str = NULL, *bind_str = NULL;
+        _cleanup_free_ char *bind_str = NULL;
 
         assert_se(get_testdata_dir("test-resolve/selfsigned.cert", &cert_path) >= 0);
         assert_se(get_testdata_dir("test-resolve/selfsigned.key", &key_path) >= 0);
 
-        assert_se(in_addr_to_string(server_address.in.sin_family,
-                                    sockaddr_in_addr(&server_address.sa),
-                                    &ip_str) >= 0);
-        assert_se(asprintf(&bind_str, "%s:%d", ip_str, be16toh(server_address.in.sin_port)) >= 0);
+        assert_se(asprintf(&bind_str, "%s:%d",
+                           IN_ADDR_TO_STRING(server_address.in.sin_family,
+                                             sockaddr_in_addr(&server_address.sa)),
+                           be16toh(server_address.in.sin_port)) >= 0);
 
         /* We will hook one of the socketpair ends to OpenSSL's TLS server
          * stdin/stdout, so we will be able to read and write plaintext
index 636967c240ea1b8361735e43a60670319ecd1f54..c0808d51b99138529ad03323df62459a75032417 100644 (file)
@@ -347,12 +347,14 @@ TEST(in_addr_prefix_range) {
 
 static void test_in_addr_to_string_one(int f, const char *addr) {
         union in_addr_union ua;
-        _cleanup_free_ char *r = NULL;
+        _cleanup_free_ char *r;
 
         assert_se(in_addr_from_string(f, addr, &ua) >= 0);
         assert_se(in_addr_to_string(f, &ua, &r) >= 0);
         printf("test_in_addr_to_string_one: %s == %s\n", addr, r);
         assert_se(streq(addr, r));
+
+        assert_se(streq(r, IN_ADDR_TO_STRING(f, &ua)));
 }
 
 TEST(in_addr_to_string) {
index 2f66003e65e1013c5fd78770cc197c86c4bd7164..06a0fce764af75ba5ed110da041c6e453b61e6b4 100644 (file)
@@ -41,10 +41,7 @@ TEST(dump_run_utmp) {
 
                 union in_addr_union addr = {};
                 memcpy(&addr, u->ut_addr_v6, MIN(sizeof(addr), sizeof(u->ut_addr_v6)));
-                _cleanup_free_ char *pretty = NULL;
                 bool is_ipv4 = memeqzero((const uint8_t*) &addr + 4, sizeof(addr) - 4);
-                (void) in_addr_to_string(is_ipv4 ? AF_INET : AF_INET6,
-                                         &addr, &pretty);
 
                 log_info("%14s %10"PID_PRI" line=%-7.*s id=%-4.4s name=%-8.*s session=%lu host=%.*s addr=%s",
                          type,
@@ -54,7 +51,7 @@ TEST(dump_run_utmp) {
                          UT_NAMESIZE, u->ut_user,
                          (long unsigned) u->ut_session,
                          UT_HOSTSIZE, u->ut_host,
-                         strempty(pretty));
+                         IN_ADDR_TO_STRING(is_ipv4 ? AF_INET : AF_INET6, &addr));
         }
 }