json-util: optionally accept string-based serialization for IPv4 addresses

This commit is contained in:
Lennart Poettering
2026-03-20 14:59:27 +01:00
parent 172ae47e65
commit 3839f5efec
2 changed files with 118 additions and 1 deletions

View File

@@ -9,6 +9,7 @@
#include "errno-util.h"
#include "fd-util.h"
#include "glyph-util.h"
#include "in-addr-util.h"
#include "iovec-util.h"
#include "json-util.h"
#include "log.h"
@@ -189,12 +190,25 @@ int json_dispatch_in_addr(const char *name, sd_json_variant *variant, sd_json_di
return 0;
}
/* We support a more human readable string based encoding, and an array based encoding */
if (sd_json_variant_is_string(variant)) {
union in_addr_union a;
r = in_addr_from_string(AF_INET, sd_json_variant_string(variant), &a);
if (r < 0)
return json_log(variant, flags, r,
"JSON field '%s' is not a valid IPv4 address string: %s", strna(name), sd_json_variant_string(variant));
*address = a.in;
return 0;
}
r = json_dispatch_byte_array_iovec(name, variant, flags, &iov);
if (r < 0)
return r;
if (iov.iov_len != sizeof(struct in_addr))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name));
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL),
"Expected JSON field '%s' to be an array of %zu bytes.", strna(name), sizeof(struct in_addr));
memcpy(address, iov.iov_base, iov.iov_len);
return 0;

View File

@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <float.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <unistd.h>
@@ -1630,4 +1631,106 @@ TEST(must_be) {
ASSERT_OK(sd_json_parse("[]", SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_MUST_BE_ARRAY, /* ret= */ NULL, /* reterr_line= */ NULL, /* reterr_column= */ NULL));
}
TEST(json_dispatch_in_addr) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
/* 192.168.1.1 = { 192, 168, 1, 1 } */
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", JSON_BUILD_IN4_ADDR(&(const struct in_addr) { .s_addr = htobe32(0xC0A80101U) })),
SD_JSON_BUILD_PAIR("null_addr", SD_JSON_BUILD_NULL))));
struct {
struct in_addr addr;
struct in_addr null_addr;
} data = {};
ASSERT_OK(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, offsetof(typeof(data), addr) },
{ "null_addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, offsetof(typeof(data), null_addr) },
{},
},
/* flags= */ 0,
&data));
ASSERT_EQ(be32toh(data.addr.s_addr), 0xC0A80101U);
ASSERT_EQ(data.null_addr.s_addr, 0U);
struct in_addr dummy = {};
/* Too few bytes (3 instead of 4) */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(192), SD_JSON_BUILD_UNSIGNED(168), SD_JSON_BUILD_UNSIGNED(1))))));
ASSERT_ERROR(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&dummy), EINVAL);
/* Too many bytes (5 instead of 4) */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(192), SD_JSON_BUILD_UNSIGNED(168), SD_JSON_BUILD_UNSIGNED(1), SD_JSON_BUILD_UNSIGNED(1), SD_JSON_BUILD_UNSIGNED(0))))));
ASSERT_ERROR(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&dummy), EINVAL);
/* Not an array or string */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_BOOLEAN(true)))));
ASSERT_ERROR(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&dummy), EINVAL);
/* A string */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", JSON_BUILD_CONST_STRING("192.168.1.1")))));
zero(data);
ASSERT_OK(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&data));
ASSERT_EQ(be32toh(data.addr.s_addr), 0xC0A80101U);
/* Byte value out of range (> 255) */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(256), SD_JSON_BUILD_UNSIGNED(0), SD_JSON_BUILD_UNSIGNED(0), SD_JSON_BUILD_UNSIGNED(1))))));
ASSERT_ERROR(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&dummy), EINVAL);
/* Negative element */
j = sd_json_variant_unref(j);
ASSERT_OK(sd_json_build(&j, SD_JSON_BUILD_OBJECT(
SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_INTEGER(-1), SD_JSON_BUILD_UNSIGNED(0), SD_JSON_BUILD_UNSIGNED(0), SD_JSON_BUILD_UNSIGNED(1))))));
ASSERT_ERROR(sd_json_dispatch(j,
(const sd_json_dispatch_field[]) {
{ "addr", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_in_addr, 0 },
{},
},
/* flags= */ 0,
&dummy), EINVAL);
}
DEFINE_TEST_MAIN(LOG_DEBUG);