From 7a1b8a0fefe9a2a0fae6289195e2936ec9c17000 Mon Sep 17 00:00:00 2001 From: cbdev Date: Thu, 20 Jun 2019 15:23:45 +0200 Subject: Implement fixedlength framing --- plugins/framing_fixedlength.c | 36 ++++++++++++++++++++++++++++++++++++ plugins/framing_fixedlength.md | 9 +++++++++ plugins/makefile | 5 ++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 plugins/framing_fixedlength.c create mode 100644 plugins/framing_fixedlength.md (limited to 'plugins') diff --git a/plugins/framing_fixedlength.c b/plugins/framing_fixedlength.c new file mode 100644 index 0000000..1a66df4 --- /dev/null +++ b/plugins/framing_fixedlength.c @@ -0,0 +1,36 @@ +#include + +#include "../websocksy.h" + +static int64_t framing_fixedlen(uint8_t* data, size_t length, size_t last_read, ws_operation* opcode, void** framing_data, const char* config){ + size_t* frame_size = framing_data ? (size_t*) (*framing_data) : NULL; + + if(data && !frame_size){ + frame_size = calloc(1, sizeof(size_t)); + if(!frame_size){ + fprintf(stderr, "Failed to allocate memory\n"); + return -1; + } + + *frame_size = strtoul(config, NULL, 0); + *framing_data = frame_size; + } + else if(!data && frame_size){ + free(*framing_data); + *framing_data = NULL; + } + + if(!(*frame_size)){ + return length; + } + + if(length >= *frame_size){ + return *frame_size; + } + + return 0; +} + +static void __attribute__((constructor)) init(){ + core_register_framing("fixedlength", framing_fixedlen); +} diff --git a/plugins/framing_fixedlength.md b/plugins/framing_fixedlength.md new file mode 100644 index 0000000..bd095c4 --- /dev/null +++ b/plugins/framing_fixedlength.md @@ -0,0 +1,9 @@ +# The `fixedlength` framing function + +The `fixedlength` peer stream framing function segments the peer stream into binary frames +at fixed byte boundaries (for example, aggregating and then sending 32-byte fragments from the +peer stream). + +## Configuration + +The framing configuration string is simply the length of the segments to transmit in bytes. diff --git a/plugins/makefile b/plugins/makefile index 1a6ef36..feab4cd 100644 --- a/plugins/makefile +++ b/plugins/makefile @@ -1,5 +1,5 @@ .PHONY: all clean -PLUGINS = backend_file.so +PLUGINS = backend_file.so framing_fixedlength.so CFLAGS += -fPIC -g -I../ LDFLAGS += -shared @@ -7,6 +7,9 @@ LDFLAGS += -shared %.so :: %.c %.h $(CC) $(CFLAGS) $(LDLIBS) $< -o $@ $(LDFLAGS) +%.so :: %.c + $(CC) $(CFLAGS) $(LDLIBS) $< -o $@ $(LDFLAGS) + all: $(PLUGINS) clean: -- cgit v1.2.3