diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2008-11-25 01:03:19 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2008-11-25 01:03:19 +0100 |
commit | 20506e55b12ba22b761a1ad84dc8a47ce8c82f2e (patch) | |
tree | a23824017b20e4161e6310fefdfd0a20503fca99 /src/conntrack/stack.c | |
parent | 972e6b3c19f3c79b59804308efac447bd2d016ec (diff) |
bsf: major rework of the BSF generation code
This patch reworks the BSF automatic generation code. This
feature needs more love and it has several limitations like
that the maximum number of IPs are 127 due to BSF code
restrictions. See this patch as a first step forward.
This patch also adds the stack data type, which is used to
resolve jump dynamically instead of the previous static
approach.
This patch also includes fixes in the limitations, previous
calculations were wrong.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/conntrack/stack.c')
-rw-r--r-- | src/conntrack/stack.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/conntrack/stack.c b/src/conntrack/stack.c new file mode 100644 index 0000000..404e38b --- /dev/null +++ b/src/conntrack/stack.c @@ -0,0 +1,67 @@ +/* + * (C) 2008 by Pablo Neira Ayuso <pablo@netfilter.org> + * + * This software may be used and distributed according to the terms + * of the GNU General Public License, incorporated herein by reference. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "internal/stack.h" + +struct stack { + int num_elems; + int max_elems; + size_t elem_size; + char *data; +}; + +struct stack *stack_create(size_t elem_size, int max_elems) +{ + struct stack *s; + + s = calloc(sizeof(struct stack), 1); + if (s == NULL) + return NULL; + + s->data = calloc(elem_size * max_elems, 1); + if (s->data == NULL) { + free(s); + return NULL; + } + s->elem_size = elem_size; + s->max_elems = max_elems; + + return s; +} + +void stack_destroy(struct stack *s) +{ + free(s->data); + free(s); +} + +int stack_push(struct stack *s, void *data) +{ + if (s->num_elems >= s->max_elems) { + errno = ENOSPC; + return -1; + } + memcpy(s->data + (s->elem_size * s->num_elems), data, s->elem_size); + s->num_elems++; + return 0; +} + +int stack_pop(struct stack *s, void *data) +{ + if (s->num_elems <= 0) { + errno = EINVAL; + return -1; + } + s->num_elems--; + memcpy(data, s->data + (s->elem_size * s->num_elems), s->elem_size); + return 0; +} |