about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorlatex <latex@disroot.org>2023-02-05 04:39:05 +0100
committerlatex <latex@disroot.org>2023-02-05 04:39:05 +0100
commit9df686fe72799f5ffc10b49027c37ebd1811eda8 (patch)
tree77994bdb951e77c10ed6f7b221d79d6b01832aaf
parent20c71f96968a235189cee8b388ac58e9b45f099e (diff)
downloadlibumumble-9df686fe72799f5ffc10b49027c37ebd1811eda8.tar.gz
add TLS support
-rw-r--r--config.mk4
-rw-r--r--include/libumumble.h6
-rw-r--r--src/connection.c69
-rw-r--r--tests/Makefile4
-rw-r--r--tests/test.c3
5 files changed, 56 insertions, 30 deletions
diff --git a/config.mk b/config.mk
index 8f1c34d..18edba8 100644
--- a/config.mk
+++ b/config.mk
@@ -9,7 +9,9 @@ DNAME = $(NAME).so
 
 CFLAGS = -W -Wall -Wvla -std=gnu99 -g -fPIC
 CFLAGS += $(shell pkg-config --cflags libuv)
-LDLIBS = $(shell pkg-config --libs libuv)
+LDLIBS = -ltlsuv -luv_link_t
+LDLIBS += -lmbedtls -lmbedcrypto -lmbedx509
+LDLIBS += $(shell pkg-config --libs libuv)
 NANOPB_DIR = deps/nanopb
 
 INCLUDES = -I$(NANOPB_DIR) -I. -Iinclude -Isrc
diff --git a/include/libumumble.h b/include/libumumble.h
index 7bd03b3..43ba3ec 100644
--- a/include/libumumble.h
+++ b/include/libumumble.h
@@ -6,6 +6,7 @@ extern "C" {
 #endif /* __cplusplus */
 
 #include <uv.h>
+#include <tlsuv/tlsuv.h>
 
 enum mumble_ctx_status {
 	UNINITIALIZED = 0,
@@ -21,6 +22,7 @@ typedef struct mumble_ctx {
 	uv_loop_t uv_loop;
 	uv_getaddrinfo_t uv_resolver;
 	uv_connect_t uv_connect_req;
+	tlsuv_stream_t tls_stream;
 	uv_tcp_t uv_tcp_socket;
 } mumble_ctx_t;
 
@@ -42,7 +44,9 @@ int mumble_ctx_init(mumble_ctx_t *ctx);
 */
 void mumble_ctx_close(mumble_ctx_t *ctx);
 
-int mumble_connect(mumble_ctx_t *ctx, const char *address, const unsigned short port);
+int mumble_connect(mumble_ctx_t *ctx, const char *host, int port);
+
+int mumble_run(mumble_ctx_t *ctx);
 
 #ifdef __cplusplus
 }
diff --git a/src/connection.c b/src/connection.c
index 9d64a64..c484163 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -2,17 +2,34 @@
 #include <assert.h>
 
 #include <libumumble.h>
+#include <tlsuv/tlsuv.h>
 #include <uv.h>
 #include "Mumble.pb.h"
 
 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
 	buf->base = malloc(suggested_size);
 	assert(buf->base != NULL);
-	if (buf->base == NULL)
+	if (buf->base == NULL) {
+		buf->len = 0;
 		return;
+	}
 	buf->len = suggested_size;
 }
 
+void on_close_cb(uv_handle_t *handle)
+{
+	tlsuv_stream_free((tlsuv_stream_t *) handle);
+}
+
+void on_write_cb(uv_write_t *req, int status)
+{
+	if (status < 0) {
+		tlsuv_stream_close((tlsuv_stream_t *) req->handle, on_close_cb);
+	}
+
+	free(req);
+}
+
 void on_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
 {
 	mumble_ctx_t *ctx = client->data;
@@ -22,7 +39,7 @@ void on_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
 			ctx->error = nread;
 			ctx->status = DISCONNECTED;
 		}
-		uv_close((uv_handle_t*) client, NULL);
+		tlsuv_stream_close((tlsuv_stream_t *) client, on_close_cb);
 		ctx->status = DISCONNECTED;
 		free(buf->base);
 		free(client);
@@ -41,25 +58,34 @@ void on_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
 void on_connect_cb(uv_connect_t *req, int status)
 {
 	mumble_ctx_t *ctx = req->data;
-	req->handle->data = ctx;
+	tlsuv_stream_t *tls_stream = (tlsuv_stream_t *) req->handle;
 
-	printf("Koekje: %d %s\n", status, uv_strerror(status));
-	assert(status >= 0);
 	if (status < 0) {
 		ctx->error = status;
 		ctx->status = DISCONNECTED;
+		tlsuv_stream_close((tlsuv_stream_t *) req->handle, on_close_cb);
 		return;
 	}
 	ctx->status = CONNECTED;
 
-	uv_read_start(req->handle, alloc_buffer, on_read_cb);
+	tlsuv_stream_read(tls_stream, alloc_buffer, on_read_cb);
+
+	uv_write_t *wr = malloc(sizeof(uv_write_t));
+	char write_req[] = "GET / HTTP/1.1\r\n"
+		"Accept: */*\r\n"
+		"Connection: close\r\n"
+		"Host: www.wttr.in\r\n"
+		"User-Agent: curl/0.5.0\r\n"
+		"\r\n";
+
+	uv_buf_t buf = uv_buf_init(write_req, sizeof(write_req));
+	tlsuv_stream_write(wr, tls_stream, &buf, on_write_cb);
 }
 
 void on_resolve_cb(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
 {
 	mumble_ctx_t *ctx = resolver->data;
 
-	printf("bruh momento %d", status);
 	assert(status >= 0);
 	if (status < 0) {
 		ctx->error = status;
@@ -67,37 +93,28 @@ void on_resolve_cb(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
 		return;
 	}
 
-	uv_tcp_init(&ctx->uv_loop, &ctx->uv_tcp_socket);
-	uv_tcp_connect(&ctx->uv_connect_req, &ctx->uv_tcp_socket,
-			(const struct sockaddr*) res->ai_addr, on_connect_cb);
+	tlsuv_stream_init(&ctx->uv_loop, &ctx->tls_stream, NULL);
+	//tlsuv_stream_connect_addr(&ctx->uv_connect_req, &ctx->tls_stream, res, on_connect_cb);
 
 	uv_freeaddrinfo(res);
 }
 
-int mumble_connect(mumble_ctx_t *ctx, const char *address, unsigned short port)
+int mumble_connect(mumble_ctx_t *ctx, const char *host, int port)
 {
 	if (port == 0)
 		port = 64738;
 
-	struct addrinfo hints;
-	hints.ai_family = PF_INET;
-	hints.ai_socktype = SOCK_STREAM;
-	hints.ai_protocol = IPPROTO_TCP;
-	hints.ai_flags = 0;
-
-	/* convert port to char array */
-	char service[6] = { 0 };
-	snprintf(service, sizeof(service), "%hu", port);
-
-	int result = uv_getaddrinfo(&ctx->uv_loop, &ctx->uv_resolver,
-			on_resolve_cb, address, service, &hints);
+	tlsuv_stream_init(&ctx->uv_loop, &ctx->tls_stream, NULL);
+	int result = tlsuv_stream_connect(&ctx->uv_connect_req, &ctx->tls_stream, host, port, on_connect_cb);
 	assert(result == 0);
 	if (result != 0) {
 		ctx->error = result;
 		return result;
 	}
-
-	ctx->status = CONNECTING;
-	uv_run(&ctx->uv_loop, UV_RUN_DEFAULT);
 	return 0;
 }
+
+int mumble_run(mumble_ctx_t *ctx)
+{
+	return uv_run(&ctx->uv_loop, UV_RUN_DEFAULT);
+}
diff --git a/tests/Makefile b/tests/Makefile
index cbd765c..95a146b 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -4,8 +4,8 @@ INCLUDES = -I../include
 SRC = $(shell find -type f -name '*.c')
 BIN = $(SRC:%.c=%)
 
-LDLIBS = -l:libumumble.a -luv
-LDFLAGS = -L..
+LDLIBS := -l:libumumble.a $(LDLIBS)
+LDFLAGS += -L..
 
 .PHONY: all
 
diff --git a/tests/test.c b/tests/test.c
index fc55e90..1c673a0 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -8,6 +8,9 @@ int main(int argc, char *argv[])
 	printf("%d\n", ctx.status);
 
 	mumble_connect(&ctx, "127.0.0.1", 0);
+	mumble_run(&ctx);
+	if (ctx.error != 0)
+		printf("%d %s", ctx.error, uv_strerror(ctx.error));
 
 	mumble_ctx_close(&ctx);
 	printf("%ld\n", ctx.uv_loop.time);