aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlatex <latex@disroot.org>2023-01-29 18:17:44 +0100
committerlatex <latex@disroot.org>2023-01-29 18:17:44 +0100
commit2ed3e247d3e5040eedb8a845e1d5ca8744a65436 (patch)
tree4fa8b06c12e6521b8e57e2e2b4954427bb63a585 /src
parent00ab224d50784df9672b7947b48ef7a9f46f0066 (diff)
downloadlibumumble-2ed3e247d3e5040eedb8a845e1d5ca8744a65436.tar.gz
add mumble_connect to connect to TCP
Diffstat (limited to 'src')
-rw-r--r--src/connection.c103
-rw-r--r--src/ctx.c20
2 files changed, 120 insertions, 3 deletions
diff --git a/src/connection.c b/src/connection.c
new file mode 100644
index 0000000..9d64a64
--- /dev/null
+++ b/src/connection.c
@@ -0,0 +1,103 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include <libumumble.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)
+ return;
+ buf->len = suggested_size;
+}
+
+void on_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
+{
+ mumble_ctx_t *ctx = client->data;
+
+ if (nread < 0) {
+ if (nread != UV_EOF) {
+ ctx->error = nread;
+ ctx->status = DISCONNECTED;
+ }
+ uv_close((uv_handle_t*) client, NULL);
+ ctx->status = DISCONNECTED;
+ free(buf->base);
+ free(client);
+ return;
+ }
+
+ char *data = (char*) malloc(sizeof(char) * (nread+1));
+ data[nread] = '\0';
+ strncpy(data, buf->base, nread);
+
+ printf("%s", data);
+ free(data);
+ free(buf->base);
+}
+
+void on_connect_cb(uv_connect_t *req, int status)
+{
+ mumble_ctx_t *ctx = req->data;
+ req->handle->data = ctx;
+
+ printf("Koekje: %d %s\n", status, uv_strerror(status));
+ assert(status >= 0);
+ if (status < 0) {
+ ctx->error = status;
+ ctx->status = DISCONNECTED;
+ return;
+ }
+ ctx->status = CONNECTED;
+
+ uv_read_start(req->handle, alloc_buffer, on_read_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;
+ ctx->status = DISCONNECTED;
+ 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);
+
+ uv_freeaddrinfo(res);
+}
+
+int mumble_connect(mumble_ctx_t *ctx, const char *address, unsigned short 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);
+ assert(result == 0);
+ if (result != 0) {
+ ctx->error = result;
+ return result;
+ }
+
+ ctx->status = CONNECTING;
+ uv_run(&ctx->uv_loop, UV_RUN_DEFAULT);
+ return 0;
+}
diff --git a/src/ctx.c b/src/ctx.c
index 0d7651a..35864d1 100644
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -1,15 +1,29 @@
+#include <assert.h>
+#include <string.h>
+
#include <libumumble.h>
#include <uv.h>
-int mumble_init_ctx(mumble_ctx_t *ctx)
+int mumble_ctx_init(mumble_ctx_t *ctx)
{
int result;
+ memset(ctx, 0, sizeof(mumble_ctx_t));
result = uv_loop_init(&ctx->uv_loop);
- return result;
+ assert(result == 0);
+ if (result != 0)
+ return result;
+
+ ctx->uv_loop.data = ctx;
+ ctx->uv_resolver.data = ctx;
+ ctx->uv_connect_req.data = ctx;
+ ctx->uv_tcp_socket.data = ctx;
+
+ ctx->status = READY;
+ return 0;
}
-void mumble_free_ctx(mumble_ctx_t *ctx)
+void mumble_ctx_close(mumble_ctx_t *ctx)
{
uv_loop_close(&ctx->uv_loop);
}