diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-11-01 18:01:08 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-11-04 11:42:37 +0100 |
commit | dfaff662a08e97ff2e3ad640594b40c3c5adb82b (patch) | |
tree | 0792f3cc237da3a314818964e5599ee0849fdc5a /src | |
parent | update TODO (diff) | |
download | systemd-dfaff662a08e97ff2e3ad640594b40c3c5adb82b.tar.xz systemd-dfaff662a08e97ff2e3ad640594b40c3c5adb82b.zip |
json-util: generalize json_dispatch_ifindex()
Let's move the helper from nss-resolve.c to generic code, as it's going
to be useful in #34640.
Also, let's tighten the rules, and refuse negative ifindexes, because
they are invalid.
Diffstat (limited to 'src')
-rw-r--r-- | src/libsystemd/sd-json/json-util.c | 20 | ||||
-rw-r--r-- | src/libsystemd/sd-json/json-util.h | 1 | ||||
-rw-r--r-- | src/nss-resolve/nss-resolve.c | 17 |
3 files changed, 21 insertions, 17 deletions
diff --git a/src/libsystemd/sd-json/json-util.c b/src/libsystemd/sd-json/json-util.c index a1b2ebc0fe..db7a2ac1e8 100644 --- a/src/libsystemd/sd-json/json-util.c +++ b/src/libsystemd/sd-json/json-util.c @@ -306,6 +306,26 @@ int json_dispatch_pidref(const char *name, sd_json_variant *variant, sd_json_dis return 0; } +int json_dispatch_ifindex(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + int *ifi = ASSERT_PTR(userdata), r, t; + + if (sd_json_variant_is_null(variant)) { + *ifi = 0; + return 0; + } + + r = sd_json_dispatch_int(name, variant, flags, &t); + if (r < 0) + return r; + + /* If SD_JSON_RELAX is set allow a zero interface index, otherwise refuse. */ + if (t < (FLAGS_SET(flags, SD_JSON_RELAX) ? 0 : 1)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is out of bounds for an interface index.", strna(name)); + + *ifi = t; + return 0; +} + int json_variant_new_devnum(sd_json_variant **ret, dev_t devnum) { if (devnum == 0) return sd_json_variant_new_null(ret); diff --git a/src/libsystemd/sd-json/json-util.h b/src/libsystemd/sd-json/json-util.h index 81c0fd02a6..b824fa8a81 100644 --- a/src/libsystemd/sd-json/json-util.h +++ b/src/libsystemd/sd-json/json-util.h @@ -115,6 +115,7 @@ int json_dispatch_in_addr(const char *name, sd_json_variant *variant, sd_json_di int json_dispatch_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int json_dispatch_pidref(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); int json_dispatch_devnum(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_ifindex(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); static inline int json_variant_unbase64_iovec(sd_json_variant *v, struct iovec *ret) { return sd_json_variant_unbase64(v, ret ? &ret->iov_base : NULL, ret ? &ret->iov_len : NULL); diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 91c5f11cb2..1c64ac2fd0 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -96,23 +96,6 @@ 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, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - int *ifi = ASSERT_PTR(userdata); - int64_t t; - - assert(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 = 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)); - - *ifi = (int) t; - return 0; -} - 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; |