Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Multiprotocol functions #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 77 additions & 25 deletions include/mqtt_pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,26 @@ extern "C" {

#ifndef MQTT_USE_CUSTOM_SOCKET_HANDLE
#ifdef MQTT_USE_MBEDTLS
struct mbedtls_ssl_context;
typedef struct mbedtls_ssl_context *mqtt_pal_socket_handle;
#define mqtt_pal_socket_handle mqtt_pal_mbedtls_socket_handle
#define mqtt_pal_sendall mqtt_pal_mbedtls_sendall
#define mqtt_pal_recvall mqtt_pal_mbedtls_recvall
#elif defined(MQTT_USE_WOLFSSL)
#include <wolfssl/ssl.h>
typedef WOLFSSL* mqtt_pal_socket_handle;
#define mqtt_pal_socket_handle mqtt_pal_wolfssl_socket_handle
#define mqtt_pal_sendall mqtt_pal_wolfssl_sendall
#define mqtt_pal_recvall mqtt_pal_wolfssl_recvall
#elif defined(MQTT_USE_BIO)
#include <openssl/bio.h>
typedef BIO* mqtt_pal_socket_handle;
#define mqtt_pal_socket_handle mqtt_pal_bio_socket_handle
#define mqtt_pal_sendall mqtt_pal_bio_sendall
#define mqtt_pal_recvall mqtt_pal_bio_recvall
#elif defined(MQTT_USE_BEARSSL)
#include <bearssl.h>

typedef struct _bearssl_context {
br_ssl_client_context sc;
br_x509_minimal_context xc;
br_sslio_context ioc;
size_t ta_count;
br_x509_trust_anchor *anchOut;
int fd;
int (*low_read)(void *read_context, unsigned char *buf, size_t len);
int (*low_write)(void *write_context, const unsigned char *buf, size_t len);
} bearssl_context;

typedef bearssl_context* mqtt_pal_socket_handle;
#define mqtt_pal_socket_handle mqtt_pal_bearssl_socket_handle
#define mqtt_pal_sendall mqtt_pal_bearssl_sendall
#define mqtt_pal_recvall mqtt_pal_bearssl_recvall
#else
typedef int mqtt_pal_socket_handle;
#define MQTT_USE_TCP
#define mqtt_pal_socket_handle mqtt_pal_tcp_socket_handle
#define mqtt_pal_sendall mqtt_pal_tcp_sendall
#define mqtt_pal_recvall mqtt_pal_tcp_recvall
#endif
#endif
#elif defined(_MSC_VER)
Expand All @@ -138,18 +133,75 @@ extern "C" {
#define MQTT_PAL_MUTEX_LOCK(mtx_ptr) EnterCriticalSection(mtx_ptr)
#define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) LeaveCriticalSection(mtx_ptr)


#ifndef MQTT_USE_CUSTOM_SOCKET_HANDLE
#ifdef MQTT_USE_BIO
#include <openssl/bio.h>
typedef BIO* mqtt_pal_socket_handle;
#define mqtt_pal_socket_handle mqtt_pal_bio_socket_handle
#define mqtt_pal_sendall mqtt_pal_bio_sendall
#define mqtt_pal_recvall mqtt_pal_bio_recvall
#else
typedef SOCKET mqtt_pal_socket_handle;
#define MQTT_USE_TCP
#define mqtt_pal_socket_handle mqtt_pal_tcp_socket_handle
#define mqtt_pal_sendall mqtt_pal_tcp_sendall
#define mqtt_pal_recvall mqtt_pal_tcp_recvall
#endif
#endif

#endif


#if defined(MQTT_USE_MBEDTLS)
#include <unistd.h>
struct mbedtls_ssl_context;
typedef struct mbedtls_ssl_context *mqtt_pal_mbedtls_socket_handle;

ssize_t mqtt_pal_mbedtls_sendall(mqtt_pal_mbedtls_socket_handle fd, const void* buf, size_t len, int flags);
ssize_t mqtt_pal_mbedtls_recvall(mqtt_pal_mbedtls_socket_handle fd, void* buf, size_t bufsz, int flags);
#endif

#if defined(MQTT_USE_WOLFSSL)
#include <wolfssl/ssl.h>
typedef WOLFSSL* mqtt_pal_wolfssl_socket_handle;

ssize_t mqtt_pal_wolfssl_sendall(mqtt_pal_wolfssl_socket_handle fd, const void* buf, size_t len, int flags);
ssize_t mqtt_pal_wolfssl_recvall(mqtt_pal_wolfssl_socket_handle fd, void* buf, size_t bufsz, int flags);
#endif

#if defined(MQTT_USE_BIO)
#include <openssl/bio.h>
typedef BIO* mqtt_pal_bio_socket_handle;

ssize_t mqtt_pal_bio_sendall(mqtt_pal_bio_socket_handle fd, const void* buf, size_t len, int flags);
ssize_t mqtt_pal_bio_recvall(mqtt_pal_bio_socket_handle fd, void* buf, size_t bufsz, int flags);
#endif

#if defined(MQTT_USE_BEARSSL)
#include <bearssl.h>

typedef struct _bearssl_context {
br_ssl_client_context sc;
br_x509_minimal_context xc;
br_sslio_context ioc;
size_t ta_count;
br_x509_trust_anchor *anchOut;
int fd;
int (*low_read)(void *read_context, unsigned char *buf, size_t len);
int (*low_write)(void *write_context, const unsigned char *buf, size_t len);
} bearssl_context;

typedef bearssl_context* mqtt_pal_bearssl_socket_handle;

ssize_t mqtt_pal_bearssl_sendall(mqtt_pal_bearssl_socket_handle fd, const void* buf, size_t len, int flags);
ssize_t mqtt_pal_bearssl_recvall(mqtt_pal_bearssl_socket_handle fd, void* buf, size_t bufsz, int flags);
#endif

#if defined(MQTT_USE_TCP)
#include <unistd.h>
typedef int mqtt_pal_tcp_socket_handle;

ssize_t mqtt_pal_tcp_sendall(mqtt_pal_tcp_socket_handle fd, const void* buf, size_t len, int flags);
ssize_t mqtt_pal_tcp_recvall(mqtt_pal_tcp_socket_handle fd, void* buf, size_t bufsz, int flags);
#endif

/**
* @brief Sends all the bytes in a buffer.
* @ingroup pal
Expand Down
46 changes: 29 additions & 17 deletions src/mqtt_pal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SOFTWARE.
#ifdef MQTT_USE_MBEDTLS
#include <mbedtls/ssl.h>

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_mbedtls_sendall(mqtt_pal_mbedtls_socket_handle fd, const void* buf, size_t len, int flags) {
size_t sent = 0;
while(sent < len) {
int rv = mbedtls_ssl_write(fd, buf + sent, len - sent);
Expand All @@ -59,7 +59,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return sent;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_mbedtls_recvall(mqtt_pal_mbedtls_socket_handle fd, void* buf, size_t bufsz, int flags) {
const void *const start = buf;
int rv;
do {
Expand Down Expand Up @@ -99,10 +99,12 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int
return buf - start;
}

#elif defined(MQTT_USE_WOLFSSL)
#endif

#if defined(MQTT_USE_WOLFSSL)
#include <wolfssl/ssl.h>

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_wolfssl_sendall(mqtt_pal_wolfssl_socket_handle fd, const void* buf, size_t len, int flags) {
size_t sent = 0;
while (sent < len) {
int tmp = wolfSSL_write(fd, buf + sent, (int)(len - sent));
Expand All @@ -118,7 +120,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return (ssize_t)sent;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_wolfssl_recvall(mqtt_pal_wolfssl_socket_handle fd, void* buf, size_t bufsz, int flags) {
const void* const start = buf;
int tmp;
do {
Expand All @@ -137,7 +139,9 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int
return (ssize_t)(buf - start);
}

#elif defined(MQTT_USE_BEARSSL)
#endif

#if defined(MQTT_USE_BEARSSL)
#include <bearssl.h>
#include <memory.h>

Expand Down Expand Up @@ -182,7 +186,7 @@ static int do_rec_data(mqtt_pal_socket_handle fd, unsigned int status) {
return MQTT_OK;
}

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_bearssl_sendall(mqtt_pal_bearssl_socket_handle fd, const void* buf, size_t len, int flags) {
int rc = MQTT_OK;
uint8_t *buffer;
size_t length;
Expand Down Expand Up @@ -228,7 +232,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return len;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_bearssl_recvall(mqtt_pal_bearssl_socket_handle fd, void* buf, size_t bufsz, int flags) {
int rc = MQTT_OK;
uint8_t *buffer;
size_t length;
Expand Down Expand Up @@ -266,12 +270,14 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int
return bufsz - remaining_bytes;
}

#elif defined(MQTT_USE_BIO)
#endif

#if defined(MQTT_USE_BIO)
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_bio_sendall(mqtt_pal_bio_socket_handle fd, const void* buf, size_t len, int flags) {
size_t sent = 0;
while(sent < len) {
int tmp = BIO_write(fd, (const char*)buf + sent, len - sent);
Expand All @@ -285,7 +291,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return sent;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_bio_recvall(mqtt_pal_bio_socket_handle fd, void* buf, size_t bufsz, int flags) {
const char* const start = buf;
char* bufptr = buf;
int rv;
Expand All @@ -304,11 +310,15 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int
return (ssize_t)(bufptr - start);
}

#elif defined(__unix__) || defined(__APPLE__) || defined(__NuttX__)
#endif

#if defined(MQTT_USE_TCP)

#if defined(__unix__) || defined(__APPLE__) || defined(__NuttX__)

#include <errno.h>

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_tcp_sendall(mqtt_pal_tcp_socket_handle fd, const void* buf, size_t len, int flags) {
size_t sent = 0;
while(sent < len) {
ssize_t tmp = send(fd, buf + sent, len - sent, flags);
Expand All @@ -323,7 +333,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return sent;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_tcp_recvall(mqtt_pal_tcp_socket_handle fd, void* buf, size_t bufsz, int flags) {
const void *const start = buf;
ssize_t rv;
do {
Expand All @@ -345,7 +355,7 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int

#include <errno.h>

ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
ssize_t mqtt_pal_tcp_sendall(mqtt_pal_tcp_socket_handle fd, const void* buf, size_t len, int flags) {
size_t sent = 0;
while(sent < len) {
ssize_t tmp = send(fd, (char*)buf + sent, len - sent, flags);
Expand All @@ -357,7 +367,7 @@ ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len,
return sent;
}

ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
ssize_t mqtt_pal_tcp_recvall(mqtt_pal_tcp_socket_handle fd, void* buf, size_t bufsz, int flags) {
const char *const start = buf;
ssize_t rv;
do {
Expand All @@ -380,7 +390,9 @@ ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int

#else

#error No PAL!
#error No TCP PAL!

#endif

#endif

Expand Down