diff options
Diffstat (limited to '')
-rw-r--r-- | src/aclk/mqtt_websockets/.github/workflows/run-tests.yaml (renamed from mqtt_websockets/.github/workflows/run-tests.yaml) | 0 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/.gitignore (renamed from mqtt_websockets/.gitignore) | 0 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/README.md | 7 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c | 203 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h | 47 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h | 37 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c-rbuf/ringbuffer_test.c (renamed from mqtt_websockets/c-rbuf/tests/ringbuffer_test.c) | 8 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c_rhash/c_rhash.c (renamed from mqtt_websockets/c_rhash/src/c_rhash.c) | 4 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c_rhash/c_rhash.h (renamed from mqtt_websockets/c_rhash/include/c_rhash.h) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c_rhash/c_rhash_internal.h (renamed from mqtt_websockets/c_rhash/src/c_rhash_internal.h) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/c_rhash/tests.c (renamed from mqtt_websockets/c_rhash/src/tests.c) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/common_internal.h | 27 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/common_public.c (renamed from mqtt_websockets/src/common_public.c) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/common_public.h (renamed from mqtt_websockets/src/include/common_public.h) | 0 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/endian_compat.h | 31 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_constants.h (renamed from mqtt_websockets/src/include/mqtt_constants.h) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_ng.c (renamed from mqtt_websockets/src/mqtt_ng.c) | 24 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_ng.h (renamed from mqtt_websockets/src/include/mqtt_ng.h) | 6 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_wss_client.c (renamed from mqtt_websockets/src/mqtt_wss_client.c) | 38 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_wss_client.h (renamed from mqtt_websockets/src/include/mqtt_wss_client.h) | 12 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_wss_log.c (renamed from mqtt_websockets/src/mqtt_wss_log.c) | 5 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/mqtt_wss_log.h (renamed from mqtt_websockets/src/include/mqtt_wss_log.h) | 2 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/test.c (renamed from mqtt_websockets/src/test.c) | 12 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/ws_client.c (renamed from mqtt_websockets/src/ws_client.c) | 4 | ||||
-rw-r--r-- | src/aclk/mqtt_websockets/ws_client.h (renamed from mqtt_websockets/src/include/ws_client.h) | 14 |
25 files changed, 412 insertions, 79 deletions
diff --git a/mqtt_websockets/.github/workflows/run-tests.yaml b/src/aclk/mqtt_websockets/.github/workflows/run-tests.yaml index da5dde821..da5dde821 100644 --- a/mqtt_websockets/.github/workflows/run-tests.yaml +++ b/src/aclk/mqtt_websockets/.github/workflows/run-tests.yaml diff --git a/mqtt_websockets/.gitignore b/src/aclk/mqtt_websockets/.gitignore index 9f1a0d89a..9f1a0d89a 100644 --- a/mqtt_websockets/.gitignore +++ b/src/aclk/mqtt_websockets/.gitignore diff --git a/src/aclk/mqtt_websockets/README.md b/src/aclk/mqtt_websockets/README.md new file mode 100644 index 000000000..b159686df --- /dev/null +++ b/src/aclk/mqtt_websockets/README.md @@ -0,0 +1,7 @@ +# mqtt_websockets + +Library to connect MQTT client over Websockets Secure (WSS). + +## License + +The Project is released under GPL v3 license. See [License](LICENSE) diff --git a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c new file mode 100644 index 000000000..8950c6906 --- /dev/null +++ b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c @@ -0,0 +1,203 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + +#include "cringbuffer.h" +#include "cringbuffer_internal.h" + +#include <stdlib.h> +#include <assert.h> +#include <string.h> + +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) + +// this allows user to use their own +// custom memory allocation functions +#ifdef RBUF_CUSTOM_MALLOC +#include "../../helpers/ringbuffer_pal.h" +#else +#define crbuf_malloc(...) malloc(__VA_ARGS__) +#define crbuf_free(...) free(__VA_ARGS__) +#endif + +rbuf_t rbuf_create(size_t size) +{ + rbuf_t buffer = crbuf_malloc(sizeof(struct rbuf_t) + size); + if (!buffer) + return NULL; + + memset(buffer, 0, sizeof(struct rbuf_t)); + + buffer->data = ((char*)buffer) + sizeof(struct rbuf_t); + + buffer->head = buffer->data; + buffer->tail = buffer->data; + buffer->size = size; + buffer->end = buffer->data + size; + + return buffer; +} + +void rbuf_free(rbuf_t buffer) +{ + crbuf_free(buffer); +} + +void rbuf_flush(rbuf_t buffer) +{ + buffer->head = buffer->data; + buffer->tail = buffer->data; + buffer->size_data = 0; +} + +char *rbuf_get_linear_insert_range(rbuf_t buffer, size_t *bytes) +{ + *bytes = 0; + if (buffer->head == buffer->tail && buffer->size_data) + return NULL; + + *bytes = ((buffer->head >= buffer->tail) ? buffer->end : buffer->tail) - buffer->head; + return buffer->head; +} + +char *rbuf_get_linear_read_range(rbuf_t buffer, size_t *bytes) +{ + *bytes = 0; + if(buffer->head == buffer->tail && !buffer->size_data) + return NULL; + + *bytes = ((buffer->tail >= buffer->head) ? buffer->end : buffer->head) - buffer->tail; + + return buffer->tail; +} + +int rbuf_bump_head(rbuf_t buffer, size_t bytes) +{ + size_t free_bytes = rbuf_bytes_free(buffer); + if (bytes > free_bytes) + return 0; + int i = buffer->head - buffer->data; + buffer->head = &buffer->data[(i + bytes) % buffer->size]; + buffer->size_data += bytes; + return 1; +} + +int rbuf_bump_tail(rbuf_t buffer, size_t bytes) +{ + if(!rbuf_bump_tail_noopt(buffer, bytes)) + return 0; + + // if tail catched up with head + // start writing buffer from beggining + // this is not necessary (rbuf must work well without it) + // but helps to optimize big writes as rbuf_get_linear_insert_range + // will return bigger continuous region + if(buffer->tail == buffer->head) { + assert(buffer->size_data == 0); + rbuf_flush(buffer); + } + + return 1; +} + +size_t rbuf_get_capacity(rbuf_t buffer) +{ + return buffer->size; +} + +size_t rbuf_bytes_available(rbuf_t buffer) +{ + return buffer->size_data; +} + +size_t rbuf_bytes_free(rbuf_t buffer) +{ + return buffer->size - buffer->size_data; +} + +size_t rbuf_push(rbuf_t buffer, const char *data, size_t len) +{ + size_t to_cpy; + char *w_ptr = rbuf_get_linear_insert_range(buffer, &to_cpy); + if(!to_cpy) + return to_cpy; + + to_cpy = MIN(to_cpy, len); + memcpy(w_ptr, data, to_cpy); + rbuf_bump_head(buffer, to_cpy); + if(to_cpy < len) + to_cpy += rbuf_push(buffer, &data[to_cpy], len - to_cpy); + return to_cpy; +} + +size_t rbuf_pop(rbuf_t buffer, char *data, size_t len) +{ + size_t to_cpy; + const char *r_ptr = rbuf_get_linear_read_range(buffer, &to_cpy); + if(!to_cpy) + return to_cpy; + + to_cpy = MIN(to_cpy, len); + memcpy(data, r_ptr, to_cpy); + rbuf_bump_tail(buffer, to_cpy); + if(to_cpy < len) + to_cpy += rbuf_pop(buffer, &data[to_cpy], len - to_cpy); + return to_cpy; +} + +static inline void rbuf_ptr_inc(rbuf_t buffer, const char **ptr) +{ + (*ptr)++; + if(*ptr >= buffer->end) + *ptr = buffer->data; +} + +int rbuf_memcmp(rbuf_t buffer, const char *haystack, const char *needle, size_t needle_bytes) +{ + const char *end = needle + needle_bytes; + + // as head==tail can mean 2 things here + if (haystack == buffer->head && buffer->size_data) { + if (*haystack != *needle) + return (*haystack - *needle); + rbuf_ptr_inc(buffer, &haystack); + needle++; + } + + while (haystack != buffer->head && needle != end) { + if (*haystack != *needle) + return (*haystack - *needle); + rbuf_ptr_inc(buffer, &haystack); + needle++; + } + return 0; +} + +int rbuf_memcmp_n(rbuf_t buffer, const char *to_cmp, size_t to_cmp_bytes) +{ + return rbuf_memcmp(buffer, buffer->tail, to_cmp, to_cmp_bytes); +} + +char *rbuf_find_bytes(rbuf_t buffer, const char *needle, size_t needle_bytes, int *found_idx) +{ + const char *ptr = buffer->tail; + *found_idx = 0; + + if (!rbuf_bytes_available(buffer)) + return NULL; + + if (buffer->head == buffer->tail && buffer->size_data) { + if(!rbuf_memcmp(buffer, ptr, needle, needle_bytes)) + return (char *)ptr; + rbuf_ptr_inc(buffer, &ptr); + (*found_idx)++; + } + + while (ptr != buffer->head) + { + if(!rbuf_memcmp(buffer, ptr, needle, needle_bytes)) + return (char *)ptr; + rbuf_ptr_inc(buffer, &ptr); + (*found_idx)++; + } + return NULL; +} diff --git a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h new file mode 100644 index 000000000..eb98035a9 --- /dev/null +++ b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h @@ -0,0 +1,47 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + +#ifndef CRINGBUFFER_H +#define CRINGBUFFER_H + +#include <stddef.h> + +typedef struct rbuf_t *rbuf_t; + +rbuf_t rbuf_create(size_t size); +void rbuf_free(rbuf_t buffer); +void rbuf_flush(rbuf_t buffer); + +/* /param bytes how much bytes can be copied into pointer returned + * /return pointer where data can be copied to or NULL if buffer full + */ +char *rbuf_get_linear_insert_range(rbuf_t buffer, size_t *bytes); +char *rbuf_get_linear_read_range(rbuf_t buffer, size_t *bytes); + +int rbuf_bump_head(rbuf_t buffer, size_t bytes); +int rbuf_bump_tail(rbuf_t buffer, size_t bytes); + +/* @param buffer related buffer instance + * @returns total capacity of buffer in bytes (not free/used) + */ +size_t rbuf_get_capacity(rbuf_t buffer); + +/* @param buffer related buffer instance + * @returns count of bytes stored in the buffer + */ +size_t rbuf_bytes_available(rbuf_t buffer); + +/* @param buffer related buffer instance + * @returns count of bytes available/free in the buffer (how many more bytes you can store in this buffer) + */ +size_t rbuf_bytes_free(rbuf_t buffer); + +/* writes as many bytes from `data` into the `buffer` as possible + * but maximum `len` bytes + */ +size_t rbuf_push(rbuf_t buffer, const char *data, size_t len); +size_t rbuf_pop(rbuf_t buffer, char *data, size_t len); + +char *rbuf_find_bytes(rbuf_t buffer, const char *needle, size_t needle_bytes, int *found_idx); +int rbuf_memcmp_n(rbuf_t buffer, const char *to_cmp, size_t to_cmp_bytes); + +#endif diff --git a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h new file mode 100644 index 000000000..d32de187c --- /dev/null +++ b/src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h @@ -0,0 +1,37 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + +#ifndef CRINGBUFFER_INTERNAL_H +#define CRINGBUFFER_INTERNAL_H + +struct rbuf_t { + char *data; + + // points to next byte where we can write + char *head; + // points to oldest (next to be poped) readable byte + char *tail; + + // to avoid calculating data + size + // all the time + char *end; + + size_t size; + size_t size_data; +}; + +/* this exists so that it can be tested by unit tests + * without optimization that resets head and tail to + * beginning if buffer empty + */ +inline static int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes) +{ + if (bytes > buffer->size_data) + return 0; + int i = buffer->tail - buffer->data; + buffer->tail = &buffer->data[(i + bytes) % buffer->size]; + buffer->size_data -= bytes; + + return 1; +} + +#endif diff --git a/mqtt_websockets/c-rbuf/tests/ringbuffer_test.c b/src/aclk/mqtt_websockets/c-rbuf/ringbuffer_test.c index d810ea5a1..6a17c9956 100644 --- a/mqtt_websockets/c-rbuf/tests/ringbuffer_test.c +++ b/src/aclk/mqtt_websockets/c-rbuf/ringbuffer_test.c @@ -1,10 +1,4 @@ -/* - * - * Copyright: SPDX-License-Identifier: LGPL-3.0-only - * - * Author: Timotej Šiškovič <timotejs@gmail.com> - * - */ +// Copyright: SPDX-License-Identifier: GPL-3.0-only #include "ringbuffer.h" diff --git a/mqtt_websockets/c_rhash/src/c_rhash.c b/src/aclk/mqtt_websockets/c_rhash/c_rhash.c index fd130a442..a71b500e2 100644 --- a/mqtt_websockets/c_rhash/src/c_rhash.c +++ b/src/aclk/mqtt_websockets/c_rhash/c_rhash.c @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include "c_rhash_internal.h" #include <stdlib.h> @@ -259,4 +261,4 @@ void c_rhash_destroy(c_rhash hash) { c_rhash_destroy_bin(hash->bins[i]); } c_rfree(hash); -}
\ No newline at end of file +} diff --git a/mqtt_websockets/c_rhash/include/c_rhash.h b/src/aclk/mqtt_websockets/c_rhash/c_rhash.h index e14fea5de..37addd161 100644 --- a/mqtt_websockets/c_rhash/include/c_rhash.h +++ b/src/aclk/mqtt_websockets/c_rhash/c_rhash.h @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include <sys/types.h> #include <stdint.h> #include <stddef.h> diff --git a/mqtt_websockets/c_rhash/src/c_rhash_internal.h b/src/aclk/mqtt_websockets/c_rhash/c_rhash_internal.h index aefa9453c..20f741076 100644 --- a/mqtt_websockets/c_rhash/src/c_rhash_internal.h +++ b/src/aclk/mqtt_websockets/c_rhash/c_rhash_internal.h @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include "c_rhash.h" struct bin_item { diff --git a/mqtt_websockets/c_rhash/src/tests.c b/src/aclk/mqtt_websockets/c_rhash/tests.c index 652aad69b..909c5562d 100644 --- a/mqtt_websockets/c_rhash/src/tests.c +++ b/src/aclk/mqtt_websockets/c_rhash/tests.c @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include <stdio.h> #include <string.h> diff --git a/src/aclk/mqtt_websockets/common_internal.h b/src/aclk/mqtt_websockets/common_internal.h new file mode 100644 index 000000000..2be1c45b8 --- /dev/null +++ b/src/aclk/mqtt_websockets/common_internal.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-3.0-only + +#ifndef COMMON_INTERNAL_H +#define COMMON_INTERNAL_H + +#include "endian_compat.h" + +#ifdef MQTT_WSS_CUSTOM_ALLOC +#include "../helpers/mqtt_wss_pal.h" +#else +#define mw_malloc(...) malloc(__VA_ARGS__) +#define mw_calloc(...) calloc(__VA_ARGS__) +#define mw_free(...) free(__VA_ARGS__) +#define mw_strdup(...) strdup(__VA_ARGS__) +#define mw_realloc(...) realloc(__VA_ARGS__) +#endif + +#ifndef MQTT_WSS_FRAG_MEMALIGN +#define MQTT_WSS_FRAG_MEMALIGN (8) +#endif + +#define OPENSSL_VERSION_095 0x00905100L +#define OPENSSL_VERSION_097 0x00907000L +#define OPENSSL_VERSION_110 0x10100000L +#define OPENSSL_VERSION_111 0x10101000L + +#endif /* COMMON_INTERNAL_H */ diff --git a/mqtt_websockets/src/common_public.c b/src/aclk/mqtt_websockets/common_public.c index 7f74fa511..7991b0c23 100644 --- a/mqtt_websockets/src/common_public.c +++ b/src/aclk/mqtt_websockets/common_public.c @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include "common_public.h" // this dummy exists to have a special pointer with special meaning diff --git a/mqtt_websockets/src/include/common_public.h b/src/aclk/mqtt_websockets/common_public.h index a855737f9..a855737f9 100644 --- a/mqtt_websockets/src/include/common_public.h +++ b/src/aclk/mqtt_websockets/common_public.h diff --git a/src/aclk/mqtt_websockets/endian_compat.h b/src/aclk/mqtt_websockets/endian_compat.h new file mode 100644 index 000000000..b36d2c858 --- /dev/null +++ b/src/aclk/mqtt_websockets/endian_compat.h @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-3.0-only + +#ifndef MQTT_WSS_ENDIAN_COMPAT_H +#define MQTT_WSS_ENDIAN_COMPAT_H + +#ifdef __APPLE__ + #include <libkern/OSByteOrder.h> + + #define htobe16(x) OSSwapHostToBigInt16(x) + #define htole16(x) OSSwapHostToLittleInt16(x) + #define be16toh(x) OSSwapBigToHostInt16(x) + #define le16toh(x) OSSwapLittleToHostInt16(x) + + #define htobe32(x) OSSwapHostToBigInt32(x) + #define htole32(x) OSSwapHostToLittleInt32(x) + #define be32toh(x) OSSwapBigToHostInt32(x) + #define le32toh(x) OSSwapLittleToHostInt32(x) + + #define htobe64(x) OSSwapHostToBigInt64(x) + #define htole64(x) OSSwapHostToLittleInt64(x) + #define be64toh(x) OSSwapBigToHostInt64(x) + #define le64toh(x) OSSwapLittleToHostInt64(x) +#else +#ifdef __FreeBSD__ + #include <sys/endian.h> +#else + #include <endian.h> +#endif +#endif + +#endif /* MQTT_WSS_ENDIAN_COMPAT_H */ diff --git a/mqtt_websockets/src/include/mqtt_constants.h b/src/aclk/mqtt_websockets/mqtt_constants.h index 1db498976..3d6a2aa4a 100644 --- a/mqtt_websockets/src/include/mqtt_constants.h +++ b/src/aclk/mqtt_websockets/mqtt_constants.h @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #ifndef MQTT_CONSTANTS_H #define MQTT_CONSTANTS_H diff --git a/mqtt_websockets/src/mqtt_ng.c b/src/aclk/mqtt_websockets/mqtt_ng.c index 81cffccf0..f570fde71 100644 --- a/mqtt_websockets/src/mqtt_ng.c +++ b/src/aclk/mqtt_websockets/mqtt_ng.c @@ -1,4 +1,8 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include <stdint.h> #include <stdlib.h> @@ -6,7 +10,7 @@ #include <pthread.h> #include <inttypes.h> -#include "c_rhash.h" +#include "c_rhash/c_rhash.h" #include "common_internal.h" #include "mqtt_constants.h" @@ -47,7 +51,7 @@ struct buffer_fragment { size_t sent; buffer_frag_flag_t flags; void (*free_fnc)(void *ptr); - char *data; + unsigned char *data; uint16_t packet_id; @@ -60,8 +64,8 @@ typedef struct buffer_fragment *mqtt_msg_data; // not for actual data sent struct header_buffer { size_t size; - char *data; - char *tail; + unsigned char *data; + unsigned char *tail; struct buffer_fragment *tail_frag; }; @@ -257,7 +261,7 @@ struct mqtt_ng_client { size_t max_msg_size; }; -char pingreq[] = { MQTT_CPT_PINGREQ << 4, 0x00 }; +unsigned char pingreq[] = { MQTT_CPT_PINGREQ << 4, 0x00 }; struct buffer_fragment ping_frag = { .data = pingreq, @@ -269,7 +273,7 @@ struct buffer_fragment ping_frag = { .packet_id = 0 }; -int uint32_to_mqtt_vbi(uint32_t input, char *output) { +int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output) { int i = 1; *output = 0; @@ -476,7 +480,7 @@ static void buffer_rebuild(struct header_buffer *buf) { struct buffer_fragment *frag = (struct buffer_fragment*)buf->data; do { - buf->tail = (char*)frag + sizeof(struct buffer_fragment); + buf->tail = (unsigned char *) frag + sizeof(struct buffer_fragment); buf->tail_frag = frag; if (!(frag->flags & BUFFER_FRAG_DATA_EXTERNAL)) { buf->tail_frag->data = buf->tail; @@ -527,7 +531,7 @@ static void buffer_garbage_collect(struct header_buffer *buf, mqtt_wss_log_ctx_t } #endif - memmove(buf->data, frag, buf->tail - (char*)frag); + memmove(buf->data, frag, buf->tail - (unsigned char *) frag); buffer_rebuild(buf); } @@ -933,7 +937,7 @@ mqtt_msg_data mqtt_ng_generate_connect(struct transaction_buffer *trx_buf, DATA_ADVANCE(&trx_buf->hdr_buffer, sizeof(mqtt_protocol_name_frag), frag); // [MQTT-3.1.2.3] Connect flags - char *connect_flags = WRITE_POS(frag); + unsigned char *connect_flags = WRITE_POS(frag); *connect_flags = 0; if (auth->username) *connect_flags |= MQTT_CONNECT_FLAG_USERNAME; @@ -1947,7 +1951,7 @@ static int send_fragment(struct mqtt_ng_client *client) { struct buffer_fragment *frag = client->main_buffer.sending_frag; // for readability - char *ptr = frag->data + frag->sent; + unsigned char *ptr = frag->data + frag->sent; size_t bytes = frag->len - frag->sent; size_t processed = 0; diff --git a/mqtt_websockets/src/include/mqtt_ng.h b/src/aclk/mqtt_websockets/mqtt_ng.h index 09668d09b..4b0584d58 100644 --- a/mqtt_websockets/src/include/mqtt_ng.h +++ b/src/aclk/mqtt_websockets/mqtt_ng.h @@ -1,8 +1,10 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include <stdint.h> #include <sys/types.h> #include <time.h> -#include "ringbuffer.h" +#include "c-rbuf/cringbuffer.h" #include "common_public.h" #define MQTT_NG_MSGGEN_OK 0 @@ -19,7 +21,7 @@ struct mqtt_ng_client; * @param output pointer to memory where output will be written to. Must allow up to 4 bytes to be written. * @return number of bytes written to output or <= 0 if error in which case contents of output are undefined */ -int uint32_to_mqtt_vbi(uint32_t input, char *output); +int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output); struct mqtt_lwt_properties { char *will_topic; diff --git a/mqtt_websockets/src/mqtt_wss_client.c b/src/aclk/mqtt_websockets/mqtt_wss_client.c index 01e2ffce7..a2aef80ce 100644 --- a/mqtt_websockets/src/mqtt_wss_client.c +++ b/src/aclk/mqtt_websockets/mqtt_wss_client.c @@ -1,16 +1,9 @@ -// Copyright (C) 2020 Timotej Šiškovič // SPDX-License-Identifier: GPL-3.0-only -// -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. -// -// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with this program. -// If not, see <https://www.gnu.org/licenses/>. +// Copyright (C) 2020 Timotej Šiškovič + +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include "mqtt_wss_client.h" #include "mqtt_ng.h" @@ -152,16 +145,16 @@ static void mws_connack_callback_ng(void *user_ctx, int code) static ssize_t mqtt_send_cb(void *user_ctx, const void* buf, size_t len) { - mqtt_wss_client mqtt_wss_client = user_ctx; + mqtt_wss_client client = user_ctx; #ifdef DEBUG_ULTRA_VERBOSE - mws_debug(mqtt_wss_client->log, "mqtt_pal_sendall(len=%d)", len); + mws_debug(client->log, "mqtt_pal_sendall(len=%d)", len); #endif - int ret = ws_client_send(mqtt_wss_client->ws_client, WS_OP_BINARY_FRAME, buf, len); + int ret = ws_client_send(client->ws_client, WS_OP_BINARY_FRAME, buf, len); if (ret >= 0 && (size_t)ret != len) { #ifdef DEBUG_ULTRA_VERBOSE - mws_debug(mqtt_wss_client->log, "Not complete message sent (Msg=%d,Sent=%d). Need to arm POLLOUT!", len, ret); + mws_debug(client->log, "Not complete message sent (Msg=%d,Sent=%d). Need to arm POLLOUT!", len, ret); #endif - mqtt_wss_client->mqtt_didnt_finish_write = 1; + client->mqtt_didnt_finish_write = 1; } return ret; } @@ -590,12 +583,18 @@ int mqtt_wss_connect(mqtt_wss_client client, char *host, int port, struct mqtt_c if (client->sockfd > 0) close(client->sockfd); - client->sockfd = socket(AF_INET, SOCK_STREAM, 0); + client->sockfd = socket(AF_INET, SOCK_STREAM | DEFAULT_SOCKET_FLAGS, 0); if (client->sockfd < 0) { mws_error(client->log, "Couldn't create socket()"); return -1; } +#ifndef SOCK_CLOEXEC + int flags = fcntl(client->sockfd, F_GETFD); + if (flags != -1) + (void) fcntl(client->sockfd, F_SETFD, flags| FD_CLOEXEC); +#endif + int flag = 1; int result = setsockopt(client->sockfd, IPPROTO_TCP, @@ -680,11 +679,6 @@ int mqtt_wss_connect(mqtt_wss_client client, char *host, int port, struct mqtt_c } } - uint8_t mqtt_flags = (mqtt_params->will_flags & MQTT_WSS_PUB_QOSMASK) << 3; - if (mqtt_params->will_flags & MQTT_WSS_PUB_RETAIN) - mqtt_flags |= MQTT_CONNECT_WILL_RETAIN; - mqtt_flags |= MQTT_CONNECT_CLEAN_SESSION; - client->mqtt_keepalive = (mqtt_params->keep_alive ? mqtt_params->keep_alive : 400); mws_info(client->log, "Going to connect using internal MQTT 5 implementation"); diff --git a/mqtt_websockets/src/include/mqtt_wss_client.h b/src/aclk/mqtt_websockets/mqtt_wss_client.h index e325961b7..4bdea4db9 100644 --- a/mqtt_websockets/src/include/mqtt_wss_client.h +++ b/src/aclk/mqtt_websockets/mqtt_wss_client.h @@ -1,15 +1,5 @@ -// Copyright (C) 2020 Timotej Šiškovič // SPDX-License-Identifier: GPL-3.0-only -// -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. -// -// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with this program. -// If not, see <https://www.gnu.org/licenses/>. +// Copyright (C) 2020 Timotej Šiškovič #ifndef MQTT_WSS_CLIENT_H #define MQTT_WSS_CLIENT_H diff --git a/mqtt_websockets/src/mqtt_wss_log.c b/src/aclk/mqtt_websockets/mqtt_wss_log.c index 2c8cf32e5..5e606c12b 100644 --- a/mqtt_websockets/src/mqtt_wss_log.c +++ b/src/aclk/mqtt_websockets/mqtt_wss_log.c @@ -1,8 +1,11 @@ -#include "mqtt_wss_log.h" +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <stdio.h> + +#include "mqtt_wss_log.h" #include "common_internal.h" struct mqtt_wss_log_ctx { diff --git a/mqtt_websockets/src/include/mqtt_wss_log.h b/src/aclk/mqtt_websockets/mqtt_wss_log.h index a33c460c9..6ae60d870 100644 --- a/mqtt_websockets/src/include/mqtt_wss_log.h +++ b/src/aclk/mqtt_websockets/mqtt_wss_log.h @@ -1,3 +1,5 @@ +// Copyright: SPDX-License-Identifier: GPL-3.0-only + #ifndef MQTT_WSS_LOG_H #define MQTT_WSS_LOG_H diff --git a/mqtt_websockets/src/test.c b/src/aclk/mqtt_websockets/test.c index aee23545a..59a9f3474 100644 --- a/mqtt_websockets/src/test.c +++ b/src/aclk/mqtt_websockets/test.c @@ -1,15 +1,5 @@ -// Copyright (C) 2020 Timotej Šiškovič // SPDX-License-Identifier: GPL-3.0-only -// -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. -// -// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with this program. -// If not, see <https://www.gnu.org/licenses/>. +// Copyright (C) 2020 Timotej Šiškovič #include <unistd.h> #include <stdio.h> diff --git a/mqtt_websockets/src/ws_client.c b/src/aclk/mqtt_websockets/ws_client.c index 47f633e74..240e889ca 100644 --- a/mqtt_websockets/src/ws_client.c +++ b/src/aclk/mqtt_websockets/ws_client.c @@ -72,7 +72,7 @@ ws_client *ws_client_new(size_t buf_size, char **host, mqtt_wss_log_ctx_t log) if (!client->buf_to_mqtt) goto cleanup_2; - client->entropy_fd = open(ENTROPY_SOURCE, O_RDONLY); + client->entropy_fd = open(ENTROPY_SOURCE, O_RDONLY | O_CLOEXEC); if (client->entropy_fd < 1) { ERROR("Error opening entropy source \"" ENTROPY_SOURCE "\". Reason: \"%s\"", strerror(errno)); goto cleanup_3; @@ -164,7 +164,7 @@ static int ws_client_get_nonce(ws_client *client, char *dest, unsigned int size) // we do not need crypto secure random here // it's just used for protocol negotiation int rd; - int f = open(RAND_SRC, O_RDONLY); + int f = open(RAND_SRC, O_RDONLY | O_CLOEXEC); if (f < 0) { ERROR("Error opening \"%s\". Err: \"%s\"", RAND_SRC, strerror(errno)); return -2; diff --git a/mqtt_websockets/src/include/ws_client.h b/src/aclk/mqtt_websockets/ws_client.h index de4fac40b..0ccbd29a8 100644 --- a/mqtt_websockets/src/include/ws_client.h +++ b/src/aclk/mqtt_websockets/ws_client.h @@ -1,20 +1,10 @@ -// Copyright (C) 2020 Timotej Šiškovič // SPDX-License-Identifier: GPL-3.0-only -// -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. -// -// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with this program. -// If not, see <https://www.gnu.org/licenses/>. +// Copyright (C) 2020 Timotej Šiškovič #ifndef WS_CLIENT_H #define WS_CLIENT_H -#include "ringbuffer.h" +#include "c-rbuf/cringbuffer.h" #include "mqtt_wss_log.h" #include <stdint.h> |