json-util: generalize json_dispatch_address_family
There are several places where address family is dispatched from JSON, so take json_dispatch_address_family from networkd and put it in json-util.c. Update the all instances in the tree to use this new function, adding SD_JSON_RELAX to the dispatch flags if AF_UNSPEC should be allowed in that case.
This commit is contained in:
@@ -918,3 +918,27 @@ int json_variant_compare(sd_json_variant *a, sd_json_variant *b) {
|
||||
|
||||
return CMP(sd_json_variant_type(a), sd_json_variant_type(b));
|
||||
}
|
||||
|
||||
int json_dispatch_address_family(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
|
||||
int *family = ASSERT_PTR(userdata), r;
|
||||
|
||||
assert(variant);
|
||||
|
||||
if (sd_json_variant_is_negative(variant))
|
||||
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL),
|
||||
"JSON field '%s' for an address family cannot be negative.", strna(name));
|
||||
|
||||
int k = AF_UNSPEC;
|
||||
if (!sd_json_variant_is_null(variant)) {
|
||||
r = sd_json_dispatch_int(name, variant, flags, &k);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
if (!IN_SET(k, AF_INET, AF_INET6) && !(FLAGS_SET(flags, SD_JSON_RELAX) && k == AF_UNSPEC))
|
||||
return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
|
||||
"JSON field '%s' out of range for an address family.", strna(name));
|
||||
|
||||
*family = k;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -133,6 +133,7 @@ int json_dispatch_log_level(const char *name, sd_json_variant *variant, sd_json_
|
||||
int json_dispatch_strv_environment(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);
|
||||
int json_dispatch_access_mode(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);
|
||||
int json_dispatch_job_id(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);
|
||||
int json_dispatch_address_family(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);
|
||||
|
||||
Reference in New Issue
Block a user