1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-10 00:58:20 +03:00

Merge pull request #23616 from keszybz/in-addr-to-string-formatting

Add macros that allocate a fixed buffer for in_addr_to_string(), in_addr_prefix+to_string()
This commit is contained in:
Yu Watanabe 2022-06-09 15:21:57 +09:00 committed by GitHub
commit 377157e6bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 324 additions and 445 deletions

View File

@ -645,6 +645,11 @@ SPDX-License-Identifier: LGPL-2.1-or-later
`uint16_t`. Also, "network byte order" is just a weird name for "big endian",
hence we might want to call it "big endian" right-away.
- Use `typesafe_inet_ntop()`, `typesafe_inet_ntop4()`, and
`typesafe_inet_ntop6()` instead of `inet_ntop()`. But better yet, use the
`IN_ADDR_TO_STRING()`, `IN4_ADDR_TO_STRING()`, and `IN6_ADDR_TO_STRING()`
macros which allocate an anynomous buffer internally.
- Please never use `dup()`. Use `fcntl(fd, F_DUPFD_CLOEXEC, 3)` instead. For
two reasons: first, you want `O_CLOEXEC` set on the new `fd` (see
above). Second, `dup()` will happily duplicate your `fd` as 0, 1, 2,

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"
@ -444,44 +445,33 @@ 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);
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 + 3;
else if (family == AF_INET6)
l = INET6_ADDRSTRLEN + 4;
else
if (!IN_SET(family, AF_INET, AF_INET6))
return -EAFNOSUPPORT;
if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8)
return -EINVAL;
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;
}

View 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,14 +69,62 @@ 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);
}
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);
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 *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);

View File

@ -490,9 +490,7 @@ int sockaddr_pretty(
if (r < 0)
return -ENOMEM;
} else {
char a[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
if (include_port) {
if (asprintf(&p,

View File

@ -577,23 +577,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);

View File

@ -1849,25 +1849,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);

View File

@ -858,14 +858,12 @@ static int instance_from_socket(int fd, unsigned nr, char **instance) {
be16toh(remote.in6.sin6_port)) < 0)
return -ENOMEM;
} else {
char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
if (asprintf(&r,
"%u-%s:%u-%s:%u",
nr,
inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
IN6_ADDR_TO_STRING(&local.in6.sin6_addr),
be16toh(local.in6.sin6_port),
inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
IN6_ADDR_TO_STRING(&remote.in6.sin6_addr),
be16toh(remote.in6.sin6_port)) < 0)
return -ENOMEM;
}

View File

@ -28,14 +28,12 @@ size_t serialize_in_addrs(FILE *f,
with_leading_space = &_space;
for (size_t i = 0; i < size; i++) {
char sbuf[INET_ADDRSTRLEN];
if (predicate && !predicate(&addresses[i]))
continue;
if (*with_leading_space)
fputc(' ', f);
fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
fputs(IN4_ADDR_TO_STRING(&addresses[i]), f);
count++;
*with_leading_space = true;
}
@ -89,11 +87,9 @@ void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size,
with_leading_space = &_space;
for (size_t i = 0; i < size; i++) {
char buffer[INET6_ADDRSTRLEN];
if (*with_leading_space)
fputc(' ', f);
fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
fputs(IN6_ADDR_TO_STRING(&addresses[i]), f);
*with_leading_space = true;
}
}
@ -143,7 +139,6 @@ void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, siz
fprintf(f, "%s=", key);
for (size_t i = 0; i < size; i++) {
char sbuf[INET_ADDRSTRLEN];
struct in_addr dest, gw;
uint8_t length;
@ -151,8 +146,10 @@ void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, siz
assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
fprintf(f, "%s/%" PRIu8, inet_ntop(AF_INET, &dest, sbuf, sizeof sbuf), length);
fprintf(f, ",%s%s", inet_ntop(AF_INET, &gw, sbuf, sizeof sbuf), i < size - 1 ? " ": "");
fprintf(f, "%s,%s%s",
IN4_ADDR_PREFIX_TO_STRING(&dest, length),
IN4_ADDR_TO_STRING(&gw),
i < size - 1 ? " ": "");
}
fputs("\n", f);

View File

@ -967,7 +967,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
const struct in_addr *addresses;
const void *client_id, *data;
size_t client_id_len, data_len;
char sbuf[INET_ADDRSTRLEN];
const char *string;
uint16_t mtu;
_cleanup_free_ sd_dhcp_route **routes = NULL;
@ -989,11 +988,11 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
r = sd_dhcp_lease_get_address(lease, &address);
if (r >= 0)
fprintf(f, "ADDRESS=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf)));
fprintf(f, "ADDRESS=%s\n", IN4_ADDR_TO_STRING(&address));
r = sd_dhcp_lease_get_netmask(lease, &address);
if (r >= 0)
fprintf(f, "NETMASK=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf)));
fprintf(f, "NETMASK=%s\n", IN4_ADDR_TO_STRING(&address));
r = sd_dhcp_lease_get_router(lease, &addresses);
if (r > 0) {
@ -1004,15 +1003,15 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
r = sd_dhcp_lease_get_server_identifier(lease, &address);
if (r >= 0)
fprintf(f, "SERVER_ADDRESS=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf)));
fprintf(f, "SERVER_ADDRESS=%s\n", IN4_ADDR_TO_STRING(&address));
r = sd_dhcp_lease_get_next_server(lease, &address);
if (r >= 0)
fprintf(f, "NEXT_SERVER=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf)));
fprintf(f, "NEXT_SERVER=%s\n", IN4_ADDR_TO_STRING(&address));
r = sd_dhcp_lease_get_broadcast(lease, &address);
if (r >= 0)
fprintf(f, "BROADCAST=%s\n", inet_ntop(AF_INET, &address, sbuf, sizeof(sbuf)));
fprintf(f, "BROADCAST=%s\n", IN4_ADDR_TO_STRING(&address));
r = sd_dhcp_lease_get_mtu(lease, &mtu);
if (r >= 0)

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;
}

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 });
}

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:

View File

@ -244,18 +244,15 @@ 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;
ssize_t buflen;
_cleanup_free_ char *buf = NULL;
assert(s);
assert(ra);
assert(ra->event);
buflen = next_datagram_size_fd(fd);
ssize_t buflen = next_datagram_size_fd(fd);
if (buflen < 0) {
if (ERRNO_IS_TRANSIENT(buflen) || ERRNO_IS_DISCONNECT(buflen))
return 0;
@ -264,7 +261,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
return 0;
}
buf = new0(char, buflen);
_cleanup_free_ char *buf = new0(char, buflen);
if (!buf)
return -ENOMEM;
@ -275,8 +272,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 +297,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;
}
@ -575,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;
@ -586,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,
@ -605,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) {
@ -624,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 {
@ -634,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)
@ -646,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;
}
@ -680,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,
@ -706,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) {

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();

View File

@ -105,7 +105,6 @@ static void router_dump(sd_ndisc_router *rt) {
unsigned prefix_len;
uint8_t pfl;
struct in6_addr a;
char buff[INET6_ADDRSTRLEN];
assert_se(sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime_valid) >= 0);
log_info("Valid Lifetime: %" PRIu32, lifetime_valid);
@ -122,7 +121,7 @@ static void router_dump(sd_ndisc_router *rt) {
log_info("Prefix Length: %u", prefix_len);
assert_se(sd_ndisc_router_prefix_get_address(rt, &a) >= 0);
log_info("Prefix: %s", inet_ntop(AF_INET6, &a, buff, sizeof(buff)));
log_info("Prefix: %s", IN6_ADDR_TO_STRING(&a));
break;
}
@ -135,10 +134,8 @@ static void router_dump(sd_ndisc_router *rt) {
n = sd_ndisc_router_rdnss_get_addresses(rt, &a);
assert_se(n > 0);
for (i = 0; i < n; i++) {
char buff[INET6_ADDRSTRLEN];
log_info("DNS: %s", inet_ntop(AF_INET6, a + i, buff, sizeof(buff)));
}
for (i = 0; i < n; i++)
log_info("DNS: %s", IN6_ADDR_TO_STRING(a + i));
assert_se(sd_ndisc_router_rdnss_get_lifetime(rt, &lt) >= 0);
log_info("Lifetime: %" PRIu32, lt);

View File

@ -192,7 +192,7 @@ static int call_get_addresses(
int family;
const void *a;
size_t sz;
char buf_ifi[DECIMAL_STR_MAX(int) + 2], buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
char buf_ifi[1 + DECIMAL_STR_MAX(int)] = "";
r = sd_bus_message_read(reply, "i", &family);
if (r < 0)
@ -204,13 +204,8 @@ static int call_get_addresses(
if (family == AF_INET6 && ifi > 0)
xsprintf(buf_ifi, "%%%i", ifi);
else
strcpy(buf_ifi, "");
if (!strextend(&addresses,
prefix,
inet_ntop(family, a, buffer, sizeof(buffer)),
buf_ifi))
if (!strextend(&addresses, prefix, IN_ADDR_TO_STRING(family, a), buf_ifi))
return log_oom();
r = sd_bus_message_exit_container(reply);

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;
}

View 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)

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)

View File

@ -1040,24 +1040,18 @@ static int dump_gateways(
return n;
for (int i = 0; i < n; i++) {
_cleanup_free_ char *gateway = NULL, *description = NULL;
r = in_addr_to_string(local[i].family, &local[i].address, &gateway);
if (r < 0)
return log_oom();
_cleanup_free_ char *description = NULL;
r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description);
if (r < 0)
log_debug_errno(r, "Could not get description of gateway, ignoring: %m");
if (description) {
if (!strextend(&gateway, " (", description, ")"))
return log_oom();
}
/* Show interface name for the entry if we show entries for all interfaces */
r = strv_extendf(&buf, "%s%s%s",
gateway,
r = strv_extendf(&buf, "%s%s%s%s%s%s",
IN_ADDR_TO_STRING(local[i].family, &local[i].address),
description ? " (" : "",
strempty(description),
description ? ")" : "",
ifindex <= 0 ? " on " : "",
ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
if (r < 0)
@ -1089,29 +1083,17 @@ static int dump_addresses(
(void) sd_dhcp_lease_get_address(lease, &dhcp4_address);
for (int i = 0; i < n; i++) {
_cleanup_free_ char *pretty = NULL;
struct in_addr server_address;
bool dhcp4 = false;
r = in_addr_to_string(local[i].family, &local[i].address, &pretty);
if (r < 0)
return r;
if (local[i].family == AF_INET && in4_addr_equal(&local[i].address.in, &dhcp4_address))
dhcp4 = sd_dhcp_lease_get_server_identifier(lease, &server_address) >= 0;
if (local[i].family == AF_INET && in4_addr_equal(&local[i].address.in, &dhcp4_address)) {
struct in_addr server_address;
char *p, s[INET_ADDRSTRLEN];
r = sd_dhcp_lease_get_server_identifier(lease, &server_address);
if (r >= 0 && inet_ntop(AF_INET, &server_address, s, sizeof(s)))
p = strjoin(pretty, " (DHCP4 via ", s, ")");
else
p = strjoin(pretty, " (DHCP4)");
if (!p)
return log_oom();
free_and_replace(pretty, p);
}
r = strv_extendf(&buf, "%s%s%s",
pretty,
r = strv_extendf(&buf, "%s%s%s%s%s%s",
IN_ADDR_TO_STRING(local[i].family, &local[i].address),
dhcp4 ? " (DHCP4 via " : "",
dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "",
dhcp4 ? ")" : "",
ifindex <= 0 ? " on " : "",
ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : "");
if (r < 0)
@ -1160,8 +1142,7 @@ static int dump_address_labels(sd_netlink *rtnl) {
(void) table_set_align_percent(table, cell, 100);
for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
_cleanup_free_ char *pretty = NULL;
union in_addr_union prefix = IN_ADDR_NULL;
struct in6_addr prefix;
uint8_t prefixlen;
uint32_t label;
@ -1177,11 +1158,7 @@ static int dump_address_labels(sd_netlink *rtnl) {
continue;
}
r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6);
if (r < 0)
continue;
r = in_addr_to_string(AF_INET6, &prefix, &pretty);
r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix);
if (r < 0)
continue;
@ -1193,7 +1170,7 @@ static int dump_address_labels(sd_netlink *rtnl) {
if (r < 0)
return table_log_add_error(r);
r = table_add_cell_stringf(table, NULL, "%s/%u", pretty, prefixlen);
r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen);
if (r < 0)
return table_log_add_error(r);
}

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;

View File

@ -756,7 +756,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);
@ -766,16 +766,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));
@ -1572,11 +1573,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;
}
@ -2164,12 +2162,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);

View 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.

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);

View 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;
}

View 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) {

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__);

View 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;
}
@ -424,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;
}
@ -557,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;
}
@ -621,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;
}
@ -639,12 +628,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 +972,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;
}

View 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) {

View 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) {

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;
}

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;

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));
}

View 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,

View 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;
}

View File

@ -412,7 +412,6 @@ static int dns_cache_put_positive(
int owner_family,
const union in_addr_union *owner_address) {
_cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL;
char key_str[DNS_RESOURCE_KEY_STRING_MAX];
DnsCacheItem *existing;
uint32_t min_ttl;
@ -469,7 +468,7 @@ static int dns_cache_put_positive(
dns_cache_make_space(c, 1);
i = new(DnsCacheItem, 1);
_cleanup_(dns_cache_item_freep) DnsCacheItem *i = new(DnsCacheItem, 1);
if (!i)
return -ENOMEM;
@ -493,23 +492,17 @@ static int dns_cache_put_positive(
if (r < 0)
return r;
if (DEBUG_LOGGING) {
_cleanup_free_ char *t = NULL;
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));
(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));
}
i = NULL;
TAKE_PTR(i);
return 0;
}

View 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 */

View 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;

View 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

View File

@ -339,7 +339,7 @@ static int output_timestamp_monotonic(FILE *f, sd_journal *j, const char *monoto
}
static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, OutputFlags flags, const char *realtime) {
char buf[MAX(FORMAT_TIMESTAMP_MAX, 64U)];
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, 64U)];
uint64_t x;
int r;

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();
}

View File

@ -3,11 +3,40 @@
#include "in-addr-prefix-util.h"
#include "tests.h"
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;
static void test_in_addr_prefix_to_string_one(int f, const char *addr, unsigned prefixlen) {
union in_addr_union ua;
assert_se(in_addr_from_string(f, addr, &ua) >= 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) {
test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 0);
test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 1);
test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 31);
test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 32);
test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 256);
test_in_addr_prefix_to_string_one(AF_INET, "10.11.12.13", UINT_MAX);
test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT_MAX);
test_in_addr_prefix_to_string_one(AF_INET6, "::1", 11);
test_in_addr_prefix_to_string_one(AF_INET6, "fe80::", 33);
}
static void test_config_parse_in_addr_prefixes_one(int family, const union in_addr_union *addr, uint8_t prefixlen, Set **prefixes) {
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) {

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);
@ -347,12 +346,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);
printf("%s: %s == %s\n", __func__, addr, r);
assert_se(streq(addr, r));
assert_se(streq(r, IN_ADDR_TO_STRING(f, &ua)));
}
TEST(in_addr_to_string) {

View File

@ -314,7 +314,7 @@ TEST(usec_sub_signed) {
TEST(format_timestamp) {
for (unsigned i = 0; i < 100; i++) {
char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
usec_t x, y;
x = random_u64_range(2147483600 * USEC_PER_SEC) + 1;
@ -374,7 +374,7 @@ TEST(FORMAT_TIMESTAMP) {
}
TEST(format_timestamp_relative) {
char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
usec_t x;
/* Only testing timestamps in the past so we don't need to add some delta to account for time passing

View 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));
}
}