mirror of
https://github.com/systemd/systemd.git
synced 2025-02-04 21:47:31 +03:00
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.
This commit is contained in:
parent
0e44f02e2f
commit
dfaff662a0
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user