feat: Remove the FFI stuff and hopefully allow directTLS

This commit is contained in:
2022-11-08 14:22:12 +01:00
parent 3414e2ca2e
commit d00d0e35b0
12 changed files with 76 additions and 509 deletions

View File

@@ -3,7 +3,7 @@ project(mbedsock VERSION 1.0.0 LANGUAGES C)
add_library(mbedsock SHARED mbedsock.c mbedsock.h mbedsock.def)
target_include_directories(mbedsock PUBLIC ${MBEDTLS_ROOT_DIR}/include)
target_link_libraries(mbedsock mbedtls mbedx509 mbedcrypto)
target_link_libraries(mbedsock mbedtls mbedx509 mbedcrypto pthread)
link_directories(${MBEDTLS_ROOT_DIR}/lib)
set_target_properties(mbedsock PROPERTIES

View File

@@ -35,7 +35,7 @@ int mbedsock_ctx_new(struct mbedsock_ctx *ctx, const char *capath) {
if((ret = mbedtls_x509_crt_parse_path(&ctx->chain, capath)) < 0 )
return ret;
return 0;
}
@@ -58,6 +58,7 @@ int mbedsock_new(struct mbedsock_ctx *ctx, struct mbedsock *sock) {
return ret;
sock->secure = false;
sock->read_cb = NULL;
return 0;
}
@@ -186,3 +187,54 @@ int mbedsock_read(struct mbedsock *sock, unsigned char *buf, int len) {
bool mbedsock_is_secure(struct mbedsock *sock) {
return sock->secure;
}
void mbedsock_set_read_cb(struct mbedsock *sock, void (*read_cb)(int)) {
sock->read_cb = read_cb;
}
struct mbedsock_thread_data {
struct mbedsock *sock;
unsigned char *buf;
int len;
};
void _mbedsock_read_loop(void *args) {
struct mbedsock_thread_data *data = (struct mbedsock_thread_data *) args;
struct mbedsock *sock = data->sock;
unsigned char *buf = data->buf;
int len = data->len;
int result = 1;
printf("args2: %p\n", args);
printf("bufptr2: %p\n", buf);
printf("len: %d\n", len);
free(data);
while (true) {
result = mbedsock_read(sock, buf, len);
sock->read_cb(result);
if (result <= 0)
break;
}
pthread_exit(NULL);
}
int mbedsock_run_read_loop(struct mbedsock *sock, unsigned char *buf, int len) {
if (sock->read_cb == NULL)
return -1;
sock->read_cb(42);
struct mbedsock_thread_data *data = malloc(sizeof(struct mbedsock_thread_data));
data->sock = sock;
data->buf = buf;
data->len = len;
printf("bufptr: %p\n", buf);
printf("args: %p\n", &data);
pthread_create(&sock->thread, NULL, &_mbedsock_read_loop, (void *) data);
return 0;
}

View File

@@ -7,6 +7,7 @@
#include "mbedtls/ctr_drbg.h"
#include <stdbool.h>
#include <pthread.h>
#define SSL_PERS "moxxmpp_socket"
#define SSL_PERS_LEN sizeof(SSL_PERS)/sizeof(char)
@@ -29,6 +30,12 @@ struct mbedsock {
mbedtls_ssl_config conf;
mbedtls_net_context server_fd;
// The thread the socket runs in
pthread_t thread;
// The callback function when the read loop is running
void (*read_cb)(int);
// Indicates whether the socket is secured using TLS (true) or not (false).
bool secure;
};
@@ -106,4 +113,8 @@ int mbedsock_read(struct mbedsock *sock, unsigned char *buf, int len);
bool mbedsock_is_secure(struct mbedsock *sock);
void mbedsock_set_read_cb(struct mbedsock *sock, void (*read_cb)(int));
int mbedsock_run_read_loop(struct mbedsock *sock, unsigned char *buf, int len);
#endif // __MBEDSOCK_H__