diff options
Diffstat (limited to 'src/connection.c')
-rw-r--r-- | src/connection.c | 69 |
1 files changed, 43 insertions, 26 deletions
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); +} |