basic/in-addr-util: add IN_ADDR_PREFIX_TO_STRING

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-06-06 09:52:52 +02:00
parent 61af181344
commit c71384a9ee
18 changed files with 155 additions and 223 deletions
+14 -21
View File
@@ -14,6 +14,7 @@
#include "macro.h"
#include "parse-util.h"
#include "random-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strxcpyx.h"
#include "util.h"
@@ -451,34 +452,26 @@ int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
return 0;
}
int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret) {
_cleanup_free_ char *x = NULL;
char *p;
size_t l;
int in_addr_prefix_to_string(
int family,
const union in_addr_union *u,
unsigned prefixlen,
char *buf,
size_t buf_len) {
assert(u);
assert(ret);
assert(buf);
if (family == AF_INET)
l = INET_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(unsigned);
else if (family == AF_INET6)
l = INET6_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(unsigned);
else
if (!IN_SET(family, AF_INET, AF_INET6))
return -EAFNOSUPPORT;
x = new(char, l);
if (!x)
return -ENOMEM;
errno = 0;
if (!inet_ntop(family, u, x, l))
return errno_or_else(EINVAL);
if (!typesafe_inet_ntop(family, u, buf, buf_len))
return errno_or_else(ENOSPC);
p = x + strlen(x);
l -= strlen(x);
(void) strpcpyf(&p, l, "/%u", prefixlen);
*ret = TAKE_PTR(x);
size_t l = strlen(buf);
if (!snprintf_ok(buf + l, buf_len - l, "/%u", prefixlen))
return -ENOSPC;
return 0;
}
+31 -3
View File
@@ -93,10 +93,38 @@ static inline const char* typesafe_inet_ntop6(const struct in6_addr *a, char *bu
#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);
int in_addr_prefix_to_string(
int family,
const union in_addr_union *u,
unsigned prefixlen,
char *buf,
size_t buf_len);
static inline const char* _in_addr_prefix_to_string(
int family,
const union in_addr_union *u,
unsigned prefixlen,
char *buf,
size_t buf_len) {
/* We assume that this is called with an appropriately sized buffer and can never fail. */
assert_se(in_addr_prefix_to_string(family, u, prefixlen, buf, buf_len) == 0);
return buf;
}
static inline const char* _in4_addr_prefix_to_string(const struct in_addr *a, unsigned prefixlen, char *buf, size_t buf_len) {
return _in_addr_prefix_to_string(AF_INET, (const union in_addr_union *) a, prefixlen, buf, buf_len);
}
static inline const char* _in6_addr_prefix_to_string(const struct in6_addr *a, unsigned prefixlen, char *buf, size_t buf_len) {
return _in_addr_prefix_to_string(AF_INET6, (const union in_addr_union *) a, prefixlen, buf, buf_len);
}
#define PREFIX_SUFFIX_MAX (1 + DECIMAL_STR_MAX(unsigned))
#define IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen) \
_in_addr_prefix_to_string(family, addr, prefixlen, (char[IN_ADDR_MAX + PREFIX_SUFFIX_MAX]){}, IN_ADDR_MAX + PREFIX_SUFFIX_MAX)
#define IN4_ADDR_PREFIX_TO_STRING(addr, prefixlen) \
_in4_addr_prefix_to_string(addr, prefixlen, (char[INET_ADDRSTRLEN + PREFIX_SUFFIX_MAX]){}, INET_ADDRSTRLEN + PREFIX_SUFFIX_MAX)
#define IN6_ADDR_PREFIX_TO_STRING(addr, prefixlen) \
_in6_addr_prefix_to_string(addr, prefixlen, (char[INET6_ADDRSTRLEN + PREFIX_SUFFIX_MAX]){}, INET6_ADDRSTRLEN + PREFIX_SUFFIX_MAX)
int in_addr_port_ifindex_name_to_string(int family, const union in_addr_union *u, uint16_t port, int ifindex, const char *server_name, char **ret);
static inline int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) {
return in_addr_port_ifindex_name_to_string(family, u, 0, ifindex, NULL, ret);
+6 -14
View File
@@ -574,23 +574,15 @@ void cgroup_context_dump(Unit *u, FILE* f, const char *prefix) {
FORMAT_BYTES(b->wbps));
}
SET_FOREACH(iaai, c->ip_address_allow) {
_cleanup_free_ char *k = NULL;
(void) in_addr_prefix_to_string(iaai->family, &iaai->address, iaai->prefixlen, &k);
fprintf(f, "%sIPAddressAllow: %s\n", prefix, strnull(k));
}
SET_FOREACH(iaai, c->ip_address_deny) {
_cleanup_free_ char *k = NULL;
(void) in_addr_prefix_to_string(iaai->family, &iaai->address, iaai->prefixlen, &k);
fprintf(f, "%sIPAddressDeny: %s\n", prefix, strnull(k));
}
SET_FOREACH(iaai, c->ip_address_allow)
fprintf(f, "%sIPAddressAllow: %s\n", prefix,
IN_ADDR_PREFIX_TO_STRING(iaai->family, &iaai->address, iaai->prefixlen));
SET_FOREACH(iaai, c->ip_address_deny)
fprintf(f, "%sIPAddressDeny: %s\n", prefix,
IN_ADDR_PREFIX_TO_STRING(iaai->family, &iaai->address, iaai->prefixlen));
STRV_FOREACH(path, c->ip_filters_ingress)
fprintf(f, "%sIPIngressFilterPath: %s\n", prefix, *path);
STRV_FOREACH(path, c->ip_filters_egress)
fprintf(f, "%sIPEgressFilterPath: %s\n", prefix, *path);
+4 -13
View File
@@ -1817,25 +1817,16 @@ int bus_cgroup_set_property(
fputs(name, f);
fputs("=\n", f);
} else {
struct in_addr_prefix *p;
*reduced = false;
r = in_addr_prefixes_merge(prefixes, new_prefixes);
if (r < 0)
return r;
SET_FOREACH(p, new_prefixes) {
_cleanup_free_ char *buffer = NULL;
r = in_addr_prefix_to_string(p->family, &p->address, p->prefixlen, &buffer);
if (r == -ENOMEM)
return r;
if (r < 0)
continue;
fprintf(f, "%s=%s\n", name, buffer);
}
const struct in_addr_prefix *p;
SET_FOREACH(p, new_prefixes)
fprintf(f, "%s=%s\n", name,
IN_ADDR_PREFIX_TO_STRING(p->family, &p->address, p->prefixlen));
}
r = fflush_and_check(f);
+10 -17
View File
@@ -572,7 +572,6 @@ int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
}
int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
_cleanup_free_ char *addr_p = NULL;
sd_radv_prefix *found = NULL;
int r;
@@ -583,10 +582,9 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
if (in6_addr_is_null(&p->opt.in6_addr))
return -ENOEXEC;
(void) in6_addr_prefix_to_string(&p->opt.in6_addr, p->opt.prefixlen, &addr_p);
const char *addr_p = IN6_ADDR_PREFIX_TO_STRING(&p->opt.in6_addr, p->opt.prefixlen);
LIST_FOREACH(prefix, cur, ra->prefixes) {
r = in_addr_prefix_intersect(AF_INET6,
(const union in_addr_union*) &cur->opt.in6_addr,
cur->opt.prefixlen,
@@ -602,11 +600,10 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
break;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
addr_p,
IN6_ADDR_PREFIX_TO_STRING(&cur->opt.in6_addr, cur->opt.prefixlen));
}
if (found) {
@@ -621,7 +618,7 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
LIST_APPEND(prefix, ra->prefixes, p);
log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)",
strna(addr_p),
addr_p,
FORMAT_TIMESPAN(p->lifetime_preferred_usec, USEC_PER_SEC),
FORMAT_TIMESPAN(p->lifetime_valid_usec, USEC_PER_SEC));
} else {
@@ -631,7 +628,7 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
LIST_APPEND(prefix, ra->prefixes, p);
ra->n_prefixes++;
log_radv(ra, "Added prefix %s", strna(addr_p));
log_radv(ra, "Added prefix %s", addr_p);
}
if (ra->state == RADV_STATE_IDLE)
@@ -643,10 +640,9 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
/* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */
r = radv_send(ra, NULL, ra->lifetime_usec);
if (r < 0)
log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s: %m",
strna(addr_p));
log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s: %m", addr_p);
else
log_radv(ra, "Sent Router Advertisement for added/updated prefix %s.", strna(addr_p));
log_radv(ra, "Sent Router Advertisement for added/updated prefix %s.", addr_p);
return 0;
}
@@ -677,17 +673,15 @@ void sd_radv_remove_prefix(
}
int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
_cleanup_free_ char *addr_p = NULL;
sd_radv_route_prefix *found = NULL;
int r;
assert_return(ra, -EINVAL);
assert_return(p, -EINVAL);
(void) in6_addr_prefix_to_string(&p->opt.in6_addr, p->opt.prefixlen, &addr_p);
const char *addr_p = IN6_ADDR_PREFIX_TO_STRING(&p->opt.in6_addr, p->opt.prefixlen);
LIST_FOREACH(prefix, cur, ra->route_prefixes) {
r = in_addr_prefix_intersect(AF_INET6,
(const union in_addr_union*) &cur->opt.in6_addr,
cur->opt.prefixlen,
@@ -703,11 +697,10 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
break;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 route prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
addr_p,
IN6_ADDR_PREFIX_TO_STRING(&cur->opt.in6_addr, cur->opt.prefixlen));
}
if (found) {
+9 -34
View File
@@ -1081,48 +1081,23 @@ void context_clear(Context *context) {
}
static int address_dump(Address *address, FILE *f) {
_cleanup_free_ char *addr = NULL, *peer = NULL;
int r;
r = in_addr_prefix_to_string(address->family, &address->address, address->prefixlen, &addr);
if (r < 0)
return r;
if (in_addr_is_set(address->family, &address->peer)) {
r = in_addr_to_string(address->family, &address->peer, &peer);
if (r < 0)
return r;
}
fprintf(f,
"\n[Address]\n"
"Address=%s\n",
addr);
if (peer)
fprintf(f, "Peer=%s\n", peer);
IN_ADDR_PREFIX_TO_STRING(address->family, &address->address, address->prefixlen));
if (in_addr_is_set(address->family, &address->peer))
fprintf(f, "Peer=%s\n",
IN_ADDR_TO_STRING(address->family, &address->peer));
return 0;
}
static int route_dump(Route *route, FILE *f) {
_cleanup_free_ char *dest = NULL, *gateway = NULL;
int r;
if (in_addr_is_set(route->family, &route->dest)) {
r = in_addr_prefix_to_string(route->family, &route->dest, route->prefixlen, &dest);
if (r < 0)
return r;
}
r = in_addr_to_string(route->family, &route->gateway, &gateway);
if (r < 0)
return r;
fputs("\n[Route]\n", f);
if (dest)
fprintf(f, "Destination=%s\n", dest);
fprintf(f, "Gateway=%s\n", gateway);
if (in_addr_is_set(route->family, &route->dest))
fprintf(f, "Destination=%s\n",
IN_ADDR_PREFIX_TO_STRING(route->family, &route->dest, route->prefixlen));
fprintf(f, "Gateway=%s\n",
IN_ADDR_TO_STRING(route->family, &route->gateway));
return 0;
}
+4 -6
View File
@@ -712,13 +712,11 @@ int config_parse_wireguard_allowed_ips(
masked = addr;
assert_se(in_addr_mask(family, &masked, prefixlen) >= 0);
if (!in_addr_equal(family, &masked, &addr)) {
_cleanup_free_ char *buf = NULL;
(void) in_addr_prefix_to_string(family, &masked, prefixlen, &buf);
if (!in_addr_equal(family, &masked, &addr))
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Specified address '%s' is not properly masked, assuming '%s'.", word, strna(buf));
}
"Specified address '%s' is not properly masked, assuming '%s'.",
word,
IN_ADDR_PREFIX_TO_STRING(family, &masked, prefixlen));
ipmask = new(WireguardIPmask, 1);
if (!ipmask)
+1 -6
View File
@@ -146,12 +146,7 @@ static int address_pool_acquire_one(AddressPool *p, int family, unsigned prefixl
return r;
if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
if (DEBUG_LOGGING) {
_cleanup_free_ char *s = NULL;
(void) in_addr_prefix_to_string(p->family, &u, prefixlen, &s);
log_debug("Found range %s", strna(s));
}
log_debug("Found range %s", IN_ADDR_PREFIX_TO_STRING(p->family, &u, prefixlen));
*found = u;
return 1;
+4 -8
View File
@@ -1465,11 +1465,8 @@ int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message,
r = address_add(link, tmp);
if (r < 0) {
_cleanup_free_ char *buf = NULL;
(void) in_addr_prefix_to_string(tmp->family, &tmp->in_addr, tmp->prefixlen, &buf);
log_link_warning_errno(link, r, "Failed to remember foreign address %s, ignoring: %m",
strnull(buf));
IN_ADDR_PREFIX_TO_STRING(tmp->family, &tmp->in_addr, tmp->prefixlen));
return 0;
}
@@ -2022,12 +2019,11 @@ int network_drop_invalid_addresses(Network *network) {
/* Always use the setting specified later. So, remove the previously assigned setting. */
dup = set_remove(addresses, address);
if (dup) {
_cleanup_free_ char *buf = NULL;
(void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buf);
log_warning("%s: Duplicated address %s is specified at line %u and %u, "
"dropping the address setting specified at line %u.",
dup->section->filename, strna(buf), address->section->line,
dup->section->filename,
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen),
address->section->line,
dup->section->line, dup->section->line);
/* address_free() will drop the address from addresses_by_section. */
address_free(dup);
+20 -39
View File
@@ -358,21 +358,16 @@ static int dhcp_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Requ
}
static void log_dhcp_pd_address(Link *link, const Address *address) {
_cleanup_free_ char *buffer = NULL;
int log_level;
assert(address);
assert(address->family == AF_INET6);
log_level = address_get(link, address, NULL) >= 0 ? LOG_DEBUG : LOG_INFO;
int log_level = address_get(link, address, NULL) >= 0 ? LOG_DEBUG : LOG_INFO;
if (log_level < log_get_max_level())
return;
(void) in6_addr_prefix_to_string(&address->in_addr.in6, address->prefixlen, &buffer);
log_link_full(link, log_level, "DHCP-PD address %s (valid %s, preferred %s)",
strna(buffer),
IN6_ADDR_PREFIX_TO_STRING(&address->in_addr.in6, address->prefixlen),
FORMAT_LIFETIME(address->lifetime_valid_usec),
FORMAT_LIFETIME(address->lifetime_preferred_usec));
}
@@ -524,7 +519,6 @@ static int dhcp_pd_assign_subnet_prefix(
usec_t lifetime_valid_usec,
bool is_uplink) {
_cleanup_free_ char *buf = NULL;
struct in6_addr prefix;
int r;
@@ -536,7 +530,7 @@ static int dhcp_pd_assign_subnet_prefix(
if (r < 0)
return r == -ERANGE ? 0 : r;
(void) in6_addr_prefix_to_string(&prefix, 64, &buf);
const char *pretty = IN6_ADDR_PREFIX_TO_STRING(&prefix, 64);
if (link_radv_enabled(link) && link->network->dhcp_pd_announce) {
if (is_uplink)
@@ -546,23 +540,21 @@ static int dhcp_pd_assign_subnet_prefix(
if (r < 0)
return log_link_warning_errno(link, r,
"Failed to assign/update prefix %s to IPv6 Router Advertisement: %m",
strna(buf));
pretty);
}
}
r = dhcp_pd_request_route(link, &prefix, lifetime_valid_usec);
if (r < 0)
return log_link_warning_errno(link, r,
"Failed to assign/update route for prefix %s: %m",
strna(buf));
"Failed to assign/update route for prefix %s: %m", pretty);
r = dhcp_pd_request_address(link, &prefix, lifetime_preferred_usec, lifetime_valid_usec);
if (r < 0)
return log_link_warning_errno(link, r,
"Failed to assign/update address for prefix %s: %m",
strna(buf));
"Failed to assign/update address for prefix %s: %m", pretty);
log_link_debug(link, "Assigned prefix %s", strna(buf));
log_link_debug(link, "Assigned prefix %s", pretty);
return 1;
}
@@ -706,11 +698,8 @@ static int dhcp_request_unreachable_route(
assert(callback);
if (prefixlen >= 64) {
_cleanup_free_ char *buf = NULL;
(void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
log_link_debug(link, "Not adding a blocking route for DHCP delegated prefix %s since the prefix has length >= 64.",
strna(buf));
IN6_ADDR_PREFIX_TO_STRING(addr, prefixlen));
return 0;
}
@@ -734,13 +723,9 @@ static int dhcp_request_unreachable_route(
route_unmark(existing);
r = link_request_route(link, TAKE_PTR(route), true, counter, callback, NULL);
if (r < 0) {
_cleanup_free_ char *buf = NULL;
(void) in6_addr_prefix_to_string(addr, prefixlen, &buf);
if (r < 0)
return log_link_error_errno(link, r, "Failed to request unreachable route for DHCP delegated prefix %s: %m",
strna(buf));
}
IN6_ADDR_PREFIX_TO_STRING(addr, prefixlen));
return 0;
}
@@ -770,7 +755,6 @@ static int dhcp6_request_unreachable_route(
}
static int dhcp_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t prefixlen) {
_cleanup_free_ char *buf = NULL;
struct in_addr_prefix *p;
int r;
@@ -787,21 +771,20 @@ static int dhcp_pd_prefix_add(Link *link, const struct in6_addr *prefix, uint8_t
.address.in6 = *prefix,
};
(void) in6_addr_prefix_to_string(prefix, prefixlen, &buf);
int log_level = set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG :
prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO;
log_link_full(link,
set_contains(link->dhcp_pd_prefixes, p) ? LOG_DEBUG :
prefixlen > 64 || prefixlen < 48 ? LOG_WARNING : LOG_INFO,
log_level,
"DHCP: received delegated prefix %s%s",
strna(buf),
IN6_ADDR_PREFIX_TO_STRING(prefix, prefixlen),
prefixlen > 64 ? " with prefix length > 64, ignoring." :
prefixlen < 48 ? " with prefix length < 48, looks unusual.": "");
/* Store PD prefix even if prefixlen > 64, not to make logged at warning level so frequently. */
r = set_ensure_consume(&link->dhcp_pd_prefixes, &in_addr_prefix_hash_ops_free, p);
if (r < 0)
return log_link_error_errno(link, r, "Failed to store DHCP delegated prefix %s: %m", strna(buf));
return log_link_error_errno(link, r, "Failed to store DHCP delegated prefix %s: %m",
IN6_ADDR_PREFIX_TO_STRING(prefix, prefixlen));
return 0;
}
@@ -968,13 +951,11 @@ int dhcp4_pd_prefix_acquired(Link *uplink) {
if (r < 0)
return log_link_warning_errno(uplink, r, "Failed to get DHCPv4 6rd option: %m");
if (DEBUG_LOGGING) {
_cleanup_free_ char *buf = NULL;
(void) in6_addr_prefix_to_string(&sixrd_prefix, sixrd_prefixlen, &buf);
if (DEBUG_LOGGING)
log_link_debug(uplink, "DHCPv4: 6rd option is acquired: IPv4_masklen=%u, 6rd_prefix=%s, br_address="IPV4_ADDRESS_FMT_STR,
ipv4masklen, strna(buf), IPV4_ADDRESS_FMT_VAL(*br_addresses));
}
ipv4masklen,
IN6_ADDR_PREFIX_TO_STRING(&sixrd_prefix, sixrd_prefixlen),
IPV4_ADDRESS_FMT_VAL(*br_addresses));
/* Calculate PD prefix */
dhcp4_calculate_pd_prefix(&ipv4address, ipv4masklen, &sixrd_prefix, sixrd_prefixlen, &pd_prefix, &pd_prefixlen);
+3 -6
View File
@@ -426,12 +426,9 @@ void link_check_ready(Link *link) {
return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
SET_FOREACH(a, link->addresses)
if (!address_is_ready(a)) {
_cleanup_free_ char *str = NULL;
(void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
return (void) log_link_debug(link, "%s(): address %s is not ready.", __func__, strna(str));
}
if (!address_is_ready(a))
return (void) log_link_debug(link, "%s(): address %s is not ready.", __func__,
IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
if (!link->static_address_labels_configured)
return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
+15 -22
View File
@@ -420,10 +420,8 @@ static int ndisc_router_process_autonomous_prefix(Link *link, sd_ndisc_router *r
/* ndisc_generate_addresses() below requires the prefix length <= 64. */
if (prefixlen > 64) {
_cleanup_free_ char *buf = NULL;
(void) in6_addr_prefix_to_string(&prefix, prefixlen, &buf);
log_link_debug(link, "Prefix is longer than 64, ignoring autonomous prefix %s.", strna(buf));
log_link_debug(link, "Prefix is longer than 64, ignoring autonomous prefix %s.",
IN6_ADDR_PREFIX_TO_STRING(&prefix, prefixlen));
return 0;
}
@@ -553,15 +551,11 @@ static int ndisc_router_process_prefix(Link *link, sd_ndisc_router *rt) {
return log_link_error_errno(link, r, "Failed to get prefix length: %m");
if (in6_prefix_is_filtered(&a, prefixlen, link->network->ndisc_allow_listed_prefix, link->network->ndisc_deny_listed_prefix)) {
if (DEBUG_LOGGING) {
_cleanup_free_ char *b = NULL;
(void) in6_addr_prefix_to_string(&a, prefixlen, &b);
if (!set_isempty(link->network->ndisc_allow_listed_prefix))
log_link_debug(link, "Prefix '%s' is not in allow list, ignoring", strna(b));
else
log_link_debug(link, "Prefix '%s' is in deny list, ignoring", strna(b));
}
if (DEBUG_LOGGING)
log_link_debug(link, "Prefix '%s' is %s, ignoring",
!set_isempty(link->network->ndisc_allow_listed_prefix) ? "not in allow list"
: "in deny list",
IN6_ADDR_PREFIX_TO_STRING(&a, prefixlen));
return 0;
}
@@ -617,16 +611,15 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
return 0;
}
if (in6_prefix_is_filtered(&dst, prefixlen, link->network->ndisc_allow_listed_route_prefix, link->network->ndisc_deny_listed_route_prefix)) {
if (DEBUG_LOGGING) {
_cleanup_free_ char *buf = NULL;
if (in6_prefix_is_filtered(&dst, prefixlen,
link->network->ndisc_allow_listed_route_prefix,
link->network->ndisc_deny_listed_route_prefix)) {
(void) in6_addr_prefix_to_string(&dst, prefixlen, &buf);
if (!set_isempty(link->network->ndisc_allow_listed_route_prefix))
log_link_debug(link, "Route prefix '%s' is not in allow list, ignoring", strna(buf));
else
log_link_debug(link, "Route prefix '%s' is in deny list, ignoring", strna(buf));
}
if (DEBUG_LOGGING)
log_link_debug(link, "Route prefix %s is %s, ignoring",
!set_isempty(link->network->ndisc_allow_listed_route_prefix) ? "not in allow list"
: "in deny list",
IN6_ADDR_PREFIX_TO_STRING(&dst, prefixlen));
return 0;
}
+4 -8
View File
@@ -711,15 +711,11 @@ static int prefix_section_verify(Prefix *p) {
p->section->filename, p->prefixlen, p->section->line);
if (p->prefixlen > 64) {
_cleanup_free_ char *str = NULL;
if (p->assign)
(void) in6_addr_prefix_to_string(&p->prefix, p->prefixlen, &str);
log_info("%s: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %u%s%s.",
p->section->filename, p->prefixlen, p->section->line,
log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
p->section->filename, p->section->line,
p->prefixlen,
p->assign ? ", refusing to assign an address in " : "",
p->assign ? strna(str) : "");
p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix, p->prefixlen) : "");
p->assign = false;
}
+8 -6
View File
@@ -544,9 +544,9 @@ void link_mark_routes(Link *link, NetworkConfigSource source, const struct in6_a
}
static void log_route_debug(const Route *route, const char *str, const Link *link, const Manager *manager) {
_cleanup_free_ char *state = NULL, *dst = NULL, *src = NULL, *gw_alloc = NULL, *prefsrc = NULL,
_cleanup_free_ char *state = NULL, *gw_alloc = NULL, *prefsrc = NULL,
*table = NULL, *scope = NULL, *proto = NULL, *flags = NULL;
const char *gw = NULL;
const char *gw = NULL, *dst, *src;
assert(route);
assert(str);
@@ -558,10 +558,12 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin
return;
(void) network_config_state_to_string_alloc(route->state, &state);
if (in_addr_is_set(route->family, &route->dst) || route->dst_prefixlen > 0)
(void) in_addr_prefix_to_string(route->family, &route->dst, route->dst_prefixlen, &dst);
if (in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0)
(void) in_addr_prefix_to_string(route->family, &route->src, route->src_prefixlen, &src);
dst = in_addr_is_set(route->family, &route->dst) || route->dst_prefixlen > 0 ?
IN_ADDR_PREFIX_TO_STRING(route->family, &route->dst, route->dst_prefixlen) : NULL;
src = in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0 ?
IN_ADDR_PREFIX_TO_STRING(route->family, &route->src, route->src_prefixlen) : NULL;
if (in_addr_is_set(route->gw_family, &route->gw)) {
(void) in_addr_to_string(route->gw_family, &route->gw, &gw_alloc);
gw = gw_alloc;
+4 -4
View File
@@ -414,7 +414,7 @@ static int routing_policy_rule_acquire_priority(Manager *manager, RoutingPolicyR
}
static void log_routing_policy_rule_debug(const RoutingPolicyRule *rule, const char *str, const Link *link, const Manager *m) {
_cleanup_free_ char *state = NULL, *from = NULL, *to = NULL, *table = NULL;
_cleanup_free_ char *state = NULL, *table = NULL;
assert(rule);
assert(IN_SET(rule->family, AF_INET, AF_INET6));
@@ -427,14 +427,14 @@ static void log_routing_policy_rule_debug(const RoutingPolicyRule *rule, const c
return;
(void) network_config_state_to_string_alloc(rule->state, &state);
(void) in_addr_prefix_to_string(rule->family, &rule->from, rule->from_prefixlen, &from);
(void) in_addr_prefix_to_string(rule->family, &rule->to, rule->to_prefixlen, &to);
(void) manager_get_route_table_to_string(m, rule->table, &table);
log_link_debug(link,
"%s %s routing policy rule (%s): priority: %"PRIu32", %s -> %s, iif: %s, oif: %s, table: %s",
str, strna(network_config_source_to_string(rule->source)), strna(state),
rule->priority, strna(from), strna(to),
rule->priority,
IN_ADDR_PREFIX_TO_STRING(rule->family, &rule->from, rule->from_prefixlen),
IN_ADDR_PREFIX_TO_STRING(rule->family, &rule->to, rule->to_prefixlen),
strna(rule->iif), strna(rule->oif), strna(table));
}
+2 -5
View File
@@ -1454,7 +1454,6 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
return bus_log_parse_error(r);
for (;;) {
_cleanup_free_ char *str = NULL;
uint32_t prefixlen;
int32_t family;
const void *ap;
@@ -1491,10 +1490,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8)
continue;
if (in_addr_prefix_to_string(family, (const union in_addr_union*) ap, prefixlen, &str) < 0)
continue;
if (!strextend_with_separator(&addresses, " ", str))
if (!strextend_with_separator(&addresses, " ",
IN_ADDR_PREFIX_TO_STRING(family, ap, prefixlen)))
return log_oom();
}
+11 -5
View File
@@ -5,12 +5,15 @@
static void test_in_addr_prefix_to_string_one(int f, const char *addr, unsigned prefixlen) {
union in_addr_union ua;
_cleanup_free_ char *r;
assert_se(in_addr_from_string(f, addr, &ua) >= 0);
assert_se(in_addr_prefix_to_string(f, &ua, prefixlen, &r) >= 0);
const char *r = IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen);
assert_se(r);
printf("%s: %s/%u == %s\n", __func__, addr, prefixlen, r);
assert_se(startswith(r, addr));
assert_se(streq(r, IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen)));
assert_se(streq(IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen), r));
}
TEST(in_addr_to_string_prefix) {
@@ -27,10 +30,13 @@ TEST(in_addr_to_string_prefix) {
}
static void test_config_parse_in_addr_prefixes_one(int family, const union in_addr_union *addr, uint8_t prefixlen, Set **prefixes) {
_cleanup_free_ char *str = NULL;
const char *str = IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen);
assert_se(str);
assert_se(in_addr_prefix_to_string(family, addr, prefixlen, &str) >= 0);
assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, str, prefixes, NULL) >= 0);
assert_se(streq(str, IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen)));
assert_se(streq(IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen), str));
}
static void test_config_parse_in_addr_prefixes(Set **ret) {
+5 -6
View File
@@ -86,28 +86,27 @@ TEST(in_addr_prefix_from_string) {
}
static void test_in_addr_prefix_to_string_valid(int family, const char *p) {
_cleanup_free_ char *str = NULL;
union in_addr_union u;
unsigned char l;
log_info("%s: %s", __func__, p);
assert_se(in_addr_prefix_from_string(p, family, &u, &l) >= 0);
assert_se(in_addr_prefix_to_string(family, &u, l, &str) >= 0);
assert_se(streq(str, p));
assert_se(streq(p, IN_ADDR_PREFIX_TO_STRING(family, &u, l)));
}
static void test_in_addr_prefix_to_string_unoptimized(int family, const char *p) {
_cleanup_free_ char *str1 = NULL, *str2 = NULL;
union in_addr_union u1, u2;
unsigned char len1, len2;
log_info("%s: %s", __func__, p);
assert_se(in_addr_prefix_from_string(p, family, &u1, &len1) >= 0);
assert_se(in_addr_prefix_to_string(family, &u1, len1, &str1) >= 0);
const char *str1 = IN_ADDR_PREFIX_TO_STRING(family, &u1, len1);
assert_se(str1);
assert_se(in_addr_prefix_from_string(str1, family, &u2, &len2) >= 0);
assert_se(in_addr_prefix_to_string(family, &u2, len2, &str2) >= 0);
const char *str2 = IN_ADDR_PREFIX_TO_STRING(family, &u2, len2);
assert_se(str2);
assert_se(streq(str1, str2));
assert_se(len1 == len2);