16 #include <libnftnl/common.h>
17 #include <libnftnl/expr.h>
20 int nftnl_buf_update(
struct nftnl_buf *b,
int ret)
35 int nftnl_buf_done(
struct nftnl_buf *b)
41 if (b->size > 0 && b->buf[b->size - 1] ==
',') {
50 static int nftnl_buf_put(
struct nftnl_buf *b,
const char *fmt, ...)
56 ret = vsnprintf(b->buf + b->off, b->len, fmt, ap);
57 ret = nftnl_buf_update(b, ret);
63 int nftnl_buf_open(
struct nftnl_buf *b,
int type,
const char *tag)
66 case NFTNL_OUTPUT_JSON:
67 return nftnl_buf_put(b,
"{\"%s\":{", tag);
68 case NFTNL_OUTPUT_XML:
74 int nftnl_buf_close(
struct nftnl_buf *b,
int type,
const char *tag)
77 case NFTNL_OUTPUT_JSON:
79 if (b->size > 0 && b->buf[b->size - 1] ==
',') {
85 return nftnl_buf_put(b,
"}}");
86 case NFTNL_OUTPUT_XML:
92 int nftnl_buf_open_array(
struct nftnl_buf *b,
int type,
const char *tag)
95 case NFTNL_OUTPUT_JSON:
96 return nftnl_buf_put(b,
"{\"%s\":[", tag);
97 case NFTNL_OUTPUT_XML:
103 int nftnl_buf_close_array(
struct nftnl_buf *b,
int type,
const char *tag)
106 case NFTNL_OUTPUT_JSON:
107 return nftnl_buf_put(b,
"]}");
108 case NFTNL_OUTPUT_XML:
114 int nftnl_buf_u32(
struct nftnl_buf *b,
int type, uint32_t value,
const char *tag)
117 case NFTNL_OUTPUT_JSON:
118 return nftnl_buf_put(b,
"\"%s\":%u,", tag, value);
119 case NFTNL_OUTPUT_XML:
125 int nftnl_buf_s32(
struct nftnl_buf *b,
int type, uint32_t value,
const char *tag)
128 case NFTNL_OUTPUT_JSON:
129 return nftnl_buf_put(b,
"\"%s\":%d,", tag, value);
130 case NFTNL_OUTPUT_XML:
136 int nftnl_buf_u64(
struct nftnl_buf *b,
int type, uint64_t value,
const char *tag)
139 case NFTNL_OUTPUT_JSON:
140 return nftnl_buf_put(b,
"\"%s\":%"PRIu64
",", tag, value);
141 case NFTNL_OUTPUT_XML:
147 int nftnl_buf_str(
struct nftnl_buf *b,
int type,
const char *str,
const char *tag)
150 case NFTNL_OUTPUT_JSON:
151 return nftnl_buf_put(b,
"\"%s\":\"%s\",", tag, str);
152 case NFTNL_OUTPUT_XML:
158 int nftnl_buf_reg(
struct nftnl_buf *b,
int type,
union nftnl_data_reg *reg,
159 int reg_type,
const char *tag)
164 case NFTNL_OUTPUT_XML:
166 case NFTNL_OUTPUT_JSON:
167 nftnl_buf_put(b,
"\"%s\":{", tag);
168 ret = nftnl_data_reg_snprintf(b->buf + b->off, b->len, reg,
169 NFTNL_OUTPUT_JSON, 0, reg_type);
170 nftnl_buf_update(b, ret);
171 return nftnl_buf_put(b,
"},");
176 int nftnl_buf_expr_open(
struct nftnl_buf *b,
int type)
179 case NFTNL_OUTPUT_XML:
181 case NFTNL_OUTPUT_JSON:
182 return nftnl_buf_put(b,
"\"expr\":[");
187 int nftnl_buf_expr_close(
struct nftnl_buf *b,
int type)
190 case NFTNL_OUTPUT_XML:
192 case NFTNL_OUTPUT_JSON:
194 return nftnl_buf_put(b,
"]");
199 int nftnl_buf_expr(
struct nftnl_buf *b,
int type, uint32_t flags,
200 struct nftnl_expr *expr)
205 case NFTNL_OUTPUT_XML:
207 case NFTNL_OUTPUT_JSON:
208 nftnl_buf_put(b,
"{");
209 nftnl_buf_str(b, type, expr->ops->name, TYPE);
210 ret = nftnl_expr_snprintf(b->buf + b->off, b->len, expr, type,
213 nftnl_buf_update(b, ret);
217 return nftnl_buf_put(b,
"},");