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:
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user