lib: use json-printf in filter code

(This is mostly just to exercise the code, the actual replacement needs
to be a cocci script.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2021-11-17 12:11:04 +01:00
parent ad9df66ce2
commit b5bb6c6764

View File

@ -558,18 +558,12 @@ static int filter_show(struct vty *vty, const char *name, afi_t afi,
json_rule); json_rule);
else { else {
if (json) { if (json) {
char buf[BUFSIZ]; json_object_string_addf(
json_rule, "address", "%pI4",
json_object_string_add( &filter->addr);
json_rule, "address", json_object_string_addf(
inet_ntop(AF_INET, json_rule, "mask", "%pI4",
&filter->addr, buf, &filter->addr_mask);
sizeof(buf)));
json_object_string_add(
json_rule, "mask",
inet_ntop(AF_INET,
&filter->addr_mask,
buf, sizeof(buf)));
} else { } else {
if (filter->addr_mask.s_addr if (filter->addr_mask.s_addr
== 0xffffffff) == 0xffffffff)
@ -674,21 +668,15 @@ static void config_write_access_cisco(struct vty *vty, struct filter *mfilter,
filter = &mfilter->u.cfilter; filter = &mfilter->u.cfilter;
if (json) { if (json) {
char buf[BUFSIZ];
json_object_boolean_add(json, "extended", !!filter->extended); json_object_boolean_add(json, "extended", !!filter->extended);
json_object_string_add( json_object_string_addf(json, "sourceAddress", "%pI4",
json, "sourceAddress", &filter->addr);
inet_ntop(AF_INET, &filter->addr, buf, sizeof(buf))); json_object_string_addf(json, "sourceMask", "%pI4",
json_object_string_add(json, "sourceMask", &filter->addr_mask);
inet_ntop(AF_INET, &filter->addr_mask, json_object_string_addf(json, "destinationAddress", "%pI4",
buf, sizeof(buf))); &filter->mask);
json_object_string_add( json_object_string_addf(json, "destinationMask", "%pI4",
json, "destinationAddress", &filter->mask_mask);
inet_ntop(AF_INET, &filter->mask, buf, sizeof(buf)));
json_object_string_add(json, "destinationMask",
inet_ntop(AF_INET, &filter->mask_mask,
buf, sizeof(buf)));
} else { } else {
vty_out(vty, " ip"); vty_out(vty, " ip");
if (filter->addr_mask.s_addr == 0xffffffff) if (filter->addr_mask.s_addr == 0xffffffff)
@ -723,16 +711,13 @@ static void config_write_access_zebra(struct vty *vty, struct filter *mfilter,
p = &filter->prefix; p = &filter->prefix;
if (json) { if (json) {
json_object_string_add(json, "prefix", json_object_string_addf(json, "prefix", "%pFX", p);
prefix2str(p, buf, sizeof(buf)));
json_object_boolean_add(json, "exact-match", !!filter->exact); json_object_boolean_add(json, "exact-match", !!filter->exact);
} else { } else {
if (p->prefixlen == 0 && !filter->exact) if (p->prefixlen == 0 && !filter->exact)
vty_out(vty, " any"); vty_out(vty, " any");
else if (p->family == AF_INET6 || p->family == AF_INET) else if (p->family == AF_INET6 || p->family == AF_INET)
vty_out(vty, " %s/%d%s", vty_out(vty, " %pFX%s", p,
inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
p->prefixlen,
filter->exact ? " exact-match" : ""); filter->exact ? " exact-match" : "");
else if (p->family == AF_ETHERNET) { else if (p->family == AF_ETHERNET) {
if (p->prefixlen == 0) if (p->prefixlen == 0)