summaryrefslogtreecommitdiff
path: root/src/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/connection.c')
-rw-r--r--src/connection.c136
1 files changed, 135 insertions, 1 deletions
diff --git a/src/connection.c b/src/connection.c
index c93dbc7..66b2cdd 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -3,6 +3,7 @@
#include "connection.h"
#include "cmumble.h"
+#include "varint.h"
static gboolean
read_cb(GObject *pollable_stream, gpointer data)
@@ -18,6 +19,140 @@ read_cb(GObject *pollable_stream, gpointer data)
return TRUE;
}
+static gboolean
+read_udp(GSocket *socket, GIOCondition condition, gpointer user_data)
+{
+ GError *error = NULL;
+ gchar buf[1024];
+ gssize size;
+
+ size = g_socket_receive(socket, buf, sizeof(buf), NULL, &error);
+
+ g_print("got udp data: %ld\n", size);
+
+ return TRUE;
+}
+
+static gboolean
+read_udp_io(GIOChannel *source, GIOCondition condition, gpointer data)
+{
+ g_print("got udp data from channel\n");
+
+ return TRUE;
+}
+
+static void
+print_hex(char *hex, int num)
+{
+ int i;
+
+ for (i = 0; i < num; ++i)
+ printf("%02x", hex[i] & 0xff);
+
+}
+
+static void
+do_udp_ping(struct cmumble_context *ctx)
+{
+ uint8_t data[16];
+ uint32_t write = 0, pos = 0;
+ GTimeVal tv;
+ GError *error = NULL;
+ gssize sent;
+ int i;
+
+ g_get_current_time(&tv);
+
+ data[pos++] = (udp_ping << 5);
+ encode_varint(&data[pos], &write, tv.tv_sec, 16-pos);
+ pos += write;
+ g_print("write: %d\n", write);
+
+ char tag[16] = { 0 };
+ char foo[16] = { 0 };
+
+ /* Increase nonce, see:
+ * http://www.cs.ucdavis.edu/~rogaway/ocb/ocb-back.htm#nonce */
+ for (i = 0; i < 16; ++i)
+ if (++ctx->ocb_client_nonce[i])
+ break;
+
+ ocb_aes_encrypt(ctx->ocb, ctx->ocb_client_nonce,
+ data, pos,
+ foo+4, tag);
+
+ printf("\n");
+ printf("nonce: 0x");
+ print_hex(ctx->ocb_client_nonce, 16);
+ printf("\n");
+
+ printf("tag: 0x");
+ print_hex(tag, 3);
+ printf("\n");
+
+ printf("data: 0x");
+ print_hex((char *)data, pos);
+ printf("\n");
+
+ printf("foo: 0x");
+ print_hex(foo, pos+4);
+ printf("\n");
+
+ //memset(tag, 0, 128);
+
+#if 0
+ ocb_aes_decrypt(ctx->ocb, ctx->ocb_client_nonce,
+ foo+4, pos,
+ bar, tag);
+
+ printf("decrypt: 0x");
+ print_hex(bar, pos);
+ printf("\n");
+#endif
+
+ foo[0] = ctx->ocb_client_nonce[0];
+ foo[1] = tag[0];
+ foo[2] = tag[1];
+ foo[3] = tag[2];
+
+ sent = g_socket_send(ctx->con.udp.sock, foo, pos+4, NULL, &error);
+
+ g_print("udp sent: %ld\n", sent);
+}
+
+void
+cmumble_connection_udp_init(struct cmumble_context *ctx)
+{
+ GError *error = NULL;
+
+ ctx->con.udp.connected = FALSE;
+ ctx->con.udp.sock = g_socket_new(G_SOCKET_FAMILY_IPV4,
+ G_SOCKET_TYPE_DATAGRAM,
+ G_SOCKET_PROTOCOL_UDP,
+ &error);
+ g_assert(error == NULL);
+
+ GInetAddress *addr = g_inet_address_new_from_string("192.168.2.232");
+ g_assert(addr);
+ GSocketAddress *saddr = g_inet_socket_address_new(addr, 64738);
+
+ if (!g_socket_connect(ctx->con.udp.sock, saddr, NULL, &error))
+ return;
+
+ ctx->con.udp.connected = TRUE;
+ ctx->con.udp.source = g_socket_create_source(ctx->con.udp.sock, G_IO_IN, NULL);
+ g_source_set_callback(ctx->con.udp.source, (GSourceFunc) read_udp, ctx, NULL);
+ g_source_attach(ctx->con.udp.source, NULL);
+
+ int fd = g_socket_get_fd(ctx->con.udp.sock);
+ GIOChannel* channel = g_io_channel_unix_new(fd);
+ g_io_add_watch(channel, G_IO_IN,
+ (GIOFunc) read_udp_io, ctx);
+ g_io_channel_unref(channel);
+
+ do_udp_ping(ctx);
+}
+
static void
connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
@@ -48,7 +183,6 @@ connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data)
con->source = g_pollable_input_stream_create_source(con->input, NULL);
g_source_set_callback(con->source, (GSourceFunc) read_cb, ctx, NULL);
g_source_attach(con->source, NULL);
- g_source_unref(con->source);
cmumble_protocol_init(ctx);
}