summaryrefslogtreecommitdiffstats
path: root/src/libnftables.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-04-10 19:00:23 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2018-04-11 09:57:28 +0200
commit4885331de84cbb895d31b83696a9b6d6542f2a81 (patch)
tree615ada138977eddfc5e48e5b852d1fc5b122b562 /src/libnftables.c
parent40d94ca1dcd8ee0c6df4a33707e224dbe6a684b3 (diff)
libnftables: Simplify cookie integration
This increases the size of struct output_ctx quite a bit, but allows to simplify internal functions dealing with the cookies mainly because output_fp becomes accessible from struct cookie. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/libnftables.c')
-rw-r--r--src/libnftables.c95
1 files changed, 34 insertions, 61 deletions
diff --git a/src/libnftables.c b/src/libnftables.c
index 73363e3a..44e06e1a 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -177,14 +177,6 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
return ctx;
}
-static void free_cookie(struct cookie *cookie)
-{
- if (cookie) {
- free(cookie->buf);
- free(cookie);
- }
-}
-
static ssize_t cookie_write(void *cptr, const char *buf, size_t buflen)
{
struct cookie *cookie = cptr;
@@ -208,20 +200,13 @@ static ssize_t cookie_write(void *cptr, const char *buf, size_t buflen)
return buflen;
}
-static int init_cookie(struct cookie **cpptr, FILE **fp)
+static int init_cookie(struct cookie *cookie)
{
- struct cookie *cookie;
cookie_io_functions_t cookie_fops = {
.write = cookie_write,
};
- FILE *cookie_fp;
-
- if (!cpptr || !fp)
- return 1;
-
- cookie = *cpptr;
- if (cookie) { /* just rewind buffer */
+ if (cookie->orig_fp) { /* just rewind buffer */
if (cookie->buflen) {
cookie->pos = 0;
cookie->buf[0] = '\0';
@@ -229,69 +214,61 @@ static int init_cookie(struct cookie **cpptr, FILE **fp)
return 0;
}
- cookie = xzalloc(sizeof(*cookie));
- cookie->orig_fp = *fp;
+ cookie->orig_fp = cookie->fp;
- cookie_fp = fopencookie(cookie, "w", cookie_fops);
- if (!cookie_fp) {
- free(cookie);
+ cookie->fp = fopencookie(cookie, "w", cookie_fops);
+ if (!cookie->fp) {
+ cookie->fp = cookie->orig_fp;
return 1;
}
- *cpptr = cookie;
- *fp = cookie_fp;
return 0;
}
-int nft_ctx_buffer_output(struct nft_ctx *ctx)
+static int exit_cookie(struct cookie *cookie)
{
- struct output_ctx *octx = &ctx->output;
+ if (!cookie->orig_fp)
+ return 1;
- return init_cookie(&octx->output_cookie, &octx->output_fp);
+ fclose(cookie->fp);
+ cookie->fp = cookie->orig_fp;
+ free(cookie->buf);
+ cookie->buf = NULL;
+ cookie->buflen = 0;
+ cookie->pos = 0;
+ return 0;
}
-int nft_ctx_unbuffer_output(struct nft_ctx *ctx)
+int nft_ctx_buffer_output(struct nft_ctx *ctx)
{
- if (!ctx->output.output_cookie)
- return 1;
+ return init_cookie(&ctx->output.output_cookie);
+}
- ctx->output.output_fp = ctx->output.output_cookie->orig_fp;
- free_cookie(ctx->output.output_cookie);
- ctx->output.output_cookie = NULL;
- return 0;
+int nft_ctx_unbuffer_output(struct nft_ctx *ctx)
+{
+ return exit_cookie(&ctx->output.output_cookie);
}
int nft_ctx_buffer_error(struct nft_ctx *ctx)
{
- struct output_ctx *octx = &ctx->output;
-
- return init_cookie(&octx->error_cookie, &octx->error_fp);
+ return init_cookie(&ctx->output.error_cookie);
}
int nft_ctx_unbuffer_error(struct nft_ctx *ctx)
{
- if (!ctx->output.error_cookie)
- return 1;
-
- ctx->output.error_fp = ctx->output.error_cookie->orig_fp;
- free_cookie(ctx->output.error_cookie);
- ctx->output.error_cookie = NULL;
- return 0;
+ return exit_cookie(&ctx->output.error_cookie);
}
-static const char *get_cookie_buffer(struct cookie *cookie, FILE *cookie_fp)
+static const char *get_cookie_buffer(struct cookie *cookie)
{
- if (!cookie)
- return NULL;
-
- fflush(cookie_fp);
+ fflush(cookie->fp);
/* This is a bit tricky: Rewind the buffer for future use and return
- * the old content at the same time.
- * Therefore just reset buffer position, don't change it's content. And
- * return an empty string if buffer position is zero. */
+ * the old content at the same time. Therefore return an empty string
+ * if buffer position is zero, otherwise just rewind buffer position
+ * and return the unmodified buffer. */
- if (!cookie->buflen || !cookie->pos)
+ if (!cookie->pos)
return "";
cookie->pos = 0;
@@ -300,16 +277,12 @@ static const char *get_cookie_buffer(struct cookie *cookie, FILE *cookie_fp)
const char *nft_ctx_get_output_buffer(struct nft_ctx *ctx)
{
- struct output_ctx *octx = &ctx->output;
-
- return get_cookie_buffer(octx->output_cookie, octx->output_fp);
+ return get_cookie_buffer(&ctx->output.output_cookie);
}
const char *nft_ctx_get_error_buffer(struct nft_ctx *ctx)
{
- struct output_ctx *octx = &ctx->output;
-
- return get_cookie_buffer(octx->error_cookie, octx->error_fp);
+ return get_cookie_buffer(&ctx->output.error_cookie);
}
void nft_ctx_free(struct nft_ctx *ctx)
@@ -317,8 +290,8 @@ void nft_ctx_free(struct nft_ctx *ctx)
if (ctx->nf_sock)
netlink_close_sock(ctx->nf_sock);
- free_cookie(ctx->output.output_cookie);
- free_cookie(ctx->output.error_cookie);
+ exit_cookie(&ctx->output.output_cookie);
+ exit_cookie(&ctx->output.error_cookie);
iface_cache_release();
cache_release(&ctx->cache);
nft_ctx_clear_include_paths(ctx);