diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-05-02 14:26:21 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-06-12 18:42:22 +0200 |
commit | 309a747fa6cfeac0a0165543f23a924866727c9b (patch) | |
tree | a51242aa3ac87243c0614bf93d27e9083182e485 /src/nss-resolve | |
parent | json: merge json_dispatch_path() + json_dispatch_absolute_path() (diff) | |
download | systemd-309a747fa6cfeac0a0165543f23a924866727c9b.tar.xz systemd-309a747fa6cfeac0a0165543f23a924866727c9b.zip |
libsystemd: turn json.[ch] into a public API
This is preparation for making our Varlink API a public API. Since our
Varlink API is built on top of our JSON API we need to make that public
first (it's a nice API, but JSON APIs there are already enough, this is
purely about the Varlink angle).
I made most of the json.h APIs public, and just placed them in
sd-json.h. Sometimes I wasn't so sure however, since the underlying data
structures would have to be made public too. If in doubt I didn#t risk
it, and moved the relevant API to src/libsystemd/sd-json/json-util.h
instead (without any sd_* symbol prefixes).
This is mostly a giant search/replace patch.
Diffstat (limited to 'src/nss-resolve')
-rw-r--r-- | src/nss-resolve/nss-resolve.c | 121 |
1 files changed, 61 insertions, 60 deletions
diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 3cefb6394c..21e278f5a2 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -12,6 +12,7 @@ #include "errno-util.h" #include "glyph-util.h" #include "in-addr-util.h" +#include "json-util.h" #include "macro.h" #include "nss-util.h" #include "resolved-def.h" @@ -20,13 +21,13 @@ #include "strv.h" #include "varlink.h" -static JsonDispatchFlags json_dispatch_flags = JSON_ALLOW_EXTENSIONS; +static sd_json_dispatch_flags_t json_dispatch_flags = SD_JSON_ALLOW_EXTENSIONS; static void setup_logging(void) { log_parse_environment_variables(); if (DEBUG_LOGGING) - json_dispatch_flags = JSON_LOG; + json_dispatch_flags = SD_JSON_LOG; } static void setup_logging_once(void) { @@ -94,16 +95,16 @@ static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) { return in6_addr_is_link_local(&in6) ? ifindex : 0; } -static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_ifindex(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *ifi = ASSERT_PTR(userdata); int64_t t; assert(variant); - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - t = json_variant_integer(variant); + t = sd_json_variant_integer(variant); if (t > INT_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is out of bounds for an interface index.", strna(name)); @@ -111,16 +112,16 @@ static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDis return 0; } -static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_family(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *family = ASSERT_PTR(userdata); int64_t t; assert(variant); - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - t = json_variant_integer(variant); + t = sd_json_variant_integer(variant); if (t < 0 || t > INT_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid family.", strna(name)); @@ -129,7 +130,7 @@ static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDisp } typedef struct ResolveHostnameReply { - JsonVariant *addresses; + sd_json_variant *addresses; char *name; uint64_t flags; } ResolveHostnameReply; @@ -137,14 +138,14 @@ typedef struct ResolveHostnameReply { static void resolve_hostname_reply_destroy(ResolveHostnameReply *p) { assert(p); - json_variant_unref(p->addresses); + sd_json_variant_unref(p->addresses); free(p->name); } -static const JsonDispatch resolve_hostname_reply_dispatch_table[] = { - { "addresses", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveHostnameReply, addresses), JSON_MANDATORY }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(ResolveHostnameReply, name), 0 }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(ResolveHostnameReply, flags), 0 }, +static const sd_json_dispatch_field resolve_hostname_reply_dispatch_table[] = { + { "addresses", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(ResolveHostnameReply, addresses), SD_JSON_MANDATORY }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ResolveHostnameReply, name), 0 }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ResolveHostnameReply, flags), 0 }, {} }; @@ -155,28 +156,28 @@ typedef struct AddressParameters { size_t address_size; } AddressParameters; -static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_address(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { AddressParameters *p = ASSERT_PTR(userdata); union in_addr_union buf = {}; - JsonVariant *i; + sd_json_variant *i; size_t n, k = 0; assert(variant); - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - n = json_variant_elements(variant); + n = sd_json_variant_elements(variant); if (!IN_SET(n, 4, 16)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(i, variant) { int64_t b; - if (!json_variant_is_integer(i)) + if (!sd_json_variant_is_integer(i)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name)); - b = json_variant_integer(i); + b = sd_json_variant_integer(i); if (b < 0 || b > 0xff) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is out of range 0%s255.", @@ -191,10 +192,10 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis return 0; } -static const JsonDispatch address_parameters_dispatch_table[] = { - { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(AddressParameters, ifindex), 0 }, - { "family", JSON_VARIANT_INTEGER, json_dispatch_family, offsetof(AddressParameters, family), JSON_MANDATORY }, - { "address", JSON_VARIANT_ARRAY, json_dispatch_address, 0, JSON_MANDATORY }, +static const sd_json_dispatch_field address_parameters_dispatch_table[] = { + { "ifindex", SD_JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(AddressParameters, ifindex), 0 }, + { "family", SD_JSON_VARIANT_INTEGER, json_dispatch_family, offsetof(AddressParameters, family), SD_JSON_MANDATORY }, + { "address", SD_JSON_VARIANT_ARRAY, json_dispatch_address, 0, SD_JSON_MANDATORY }, {} }; @@ -232,9 +233,9 @@ enum nss_status _nss_resolve_gethostbyname4_r( int32_t *ttlp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -250,9 +251,9 @@ enum nss_status _nss_resolve_gethostbyname4_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -275,17 +276,17 @@ enum nss_status _nss_resolve_gethostbyname4_r( goto not_found; } - r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.addresses)) + if (sd_json_variant_is_blank_object(p.addresses)) goto not_found; size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -323,7 +324,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -391,9 +392,9 @@ enum nss_status _nss_resolve_gethostbyname3_r( char **canonp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -417,9 +418,9 @@ enum nss_status _nss_resolve_gethostbyname3_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(af)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -437,17 +438,17 @@ enum nss_status _nss_resolve_gethostbyname3_r( goto not_found; } - r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.addresses)) + if (sd_json_variant_is_blank_object(p.addresses)) goto not_found; size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -493,7 +494,7 @@ enum nss_status _nss_resolve_gethostbyname3_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -563,19 +564,19 @@ try_again: } typedef struct ResolveAddressReply { - JsonVariant *names; + sd_json_variant *names; uint64_t flags; } ResolveAddressReply; static void resolve_address_reply_destroy(ResolveAddressReply *p) { assert(p); - json_variant_unref(p->names); + sd_json_variant_unref(p->names); } -static const JsonDispatch resolve_address_reply_dispatch_table[] = { - { "names", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveAddressReply, names), JSON_MANDATORY }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(ResolveAddressReply, flags), 0 }, +static const sd_json_dispatch_field resolve_address_reply_dispatch_table[] = { + { "names", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(ResolveAddressReply, names), SD_JSON_MANDATORY }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ResolveAddressReply, flags), 0 }, {} }; @@ -590,9 +591,9 @@ static void name_parameters_destroy(NameParameters *p) { free(p->name); } -static const JsonDispatch name_parameters_dispatch_table[] = { - { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(NameParameters, ifindex), 0 }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(NameParameters, name), JSON_MANDATORY }, +static const sd_json_dispatch_field name_parameters_dispatch_table[] = { + { "ifindex", SD_JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(NameParameters, ifindex), 0 }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(NameParameters, name), SD_JSON_MANDATORY }, {} }; @@ -605,9 +606,9 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( int32_t *ttlp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_address_reply_destroy) ResolveAddressReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -635,9 +636,9 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("address", JSON_BUILD_BYTE_ARRAY(addr, len)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("address", SD_JSON_BUILD_BYTE_ARRAY(addr, len)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(af)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -653,10 +654,10 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( goto not_found; } - r = json_dispatch(rparams, resolve_address_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_address_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.names)) + if (sd_json_variant_is_blank_object(p.names)) goto not_found; size_t ms = 0, idx; @@ -664,14 +665,14 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { _cleanup_(name_parameters_destroy) NameParameters q = {}; - r = json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; ms += ALIGN(strlen(q.name) + 1); } - size_t n_names = json_variant_elements(p.names); + size_t n_names = sd_json_variant_elements(p.names); ms += ALIGN(len) + /* the address */ 2 * sizeof(char*) + /* pointer to the address, plus trailing NULL */ n_names * sizeof(char*); /* pointers to aliases, plus trailing NULL */ @@ -705,7 +706,7 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { _cleanup_(name_parameters_destroy) NameParameters q = {}; - r = json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; |