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:
@@ -347,12 +347,14 @@ TEST(in_addr_prefix_range) {
|
||||
|
||||
static void test_in_addr_to_string_one(int f, const char *addr) {
|
||||
union in_addr_union ua;
|
||||
_cleanup_free_ char *r = NULL;
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(in_addr_from_string(f, addr, &ua) >= 0);
|
||||
assert_se(in_addr_to_string(f, &ua, &r) >= 0);
|
||||
printf("test_in_addr_to_string_one: %s == %s\n", addr, r);
|
||||
assert_se(streq(addr, r));
|
||||
|
||||
assert_se(streq(r, IN_ADDR_TO_STRING(f, &ua)));
|
||||
}
|
||||
|
||||
TEST(in_addr_to_string) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user