diff options
Diffstat (limited to 'src/connection.c')
-rw-r--r-- | src/connection.c | 157 |
1 files changed, 156 insertions, 1 deletions
diff --git a/src/connection.c b/src/connection.c index 7fefdb3..5bc5341 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,161 @@ 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 = 0; + + size = g_socket_receive(socket, buf, sizeof(buf), NULL, &error); + + g_print("got udp data: %ld\n", size); + + return TRUE; +} + +#if 0 +static void +print_hex(char *hex, int num) +{ + int i; + + for (i = 0; i < num; ++i) + printf("%02x", hex[i] & 0xff); + +} +#endif + +static void +do_udp_ping(struct cmumlbe *cm) +{ + uint8_t data[16]; + uint32_t write = 0, pos = 0; + GTimeVal tv; + GError *error = NULL; + gssize sent; + + g_get_current_time(&tv); + + data[pos++] = (udp_ping << 5); + encode_varint(&data[pos], &write, tv.tv_sec, 16-pos); + pos += write; + + char foo[16] = { 0 }; +#if 0 + int i; + char tag[16] = { 0 }; + char bar[16] = { 0 }; + char ha[16] = {0}; + +#if 1 + /* Increase nonce, see: + * http://www.cs.ucdavis.edu/~rogaway/ocb/ocb-back.htm#nonce */ + for (i = 0; i < 16; ++i) + if (++cm->ocb_client_nonce[i]) + break; +#endif + + g_assert(CryptState_isValid(&cm->crypt)); + +#if 1 + bar[0] = cm->ocb_client_nonce[0]; + ocb_aes_encrypt(cm->ocb, cm->ocb_client_nonce, + data, pos, + bar+4, bar+1); + + ha[0] = cm->ocb_client_nonce[0]; + ocb_aes_encrypt(cm->ocb, cm->ocb_client_nonce, + data, pos, + ha+4, ha+1); +#endif +#endif +#if 1 + CryptState_encrypt(&cm->crypt, + data, (uint8_t *) foo, pos); +#endif + +#if 0 + printf("\n"); + printf("nonce: 0x"); + print_hex(cm->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"); + printf("foo decrypted: 0x"); + uint8_t lo[16], tag2[16]; + CryptState_ocb_decrypt(&cm->crypt, (uint8_t *) foo+4, (uint8_t *) lo, pos, cm->ocb_client_nonce, tag2); + print_hex(lo, pos); + printf("\n"); + printf("decrypted tag: 0x"); + print_hex(tag2, 3); + printf("\n"); + + printf("bar: 0x"); + print_hex(bar, pos+4); + printf("\n"); + printf("ha: 0x"); + print_hex(ha, pos+4); + printf("\n"); + + printf("length: %d\n", pos); + + //memset(tag, 0, 128); +#endif + +#if 0 + ocb_aes_decrypt(cm->ocb, cm->ocb_client_nonce, + foo+4, pos, + bar, tag); + + printf("decrypt: 0x"); + print_hex(bar, pos); + printf("\n"); +#endif + + sent = g_socket_send(cm->con.udp.sock, foo, pos+4, NULL, &error); + + g_print("udp sent: %ld\n", sent); +} + +void +cmumble_connection_udp_init(struct cmumlbe *cm) +{ + GError *error = NULL; + + cm->con.udp.connected = FALSE; + cm->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("127.0.0.1"); + g_assert(addr); + GSocketAddress *saddr = g_inet_socket_address_new(addr, 64738); + + if (!g_socket_connect(cm->con.udp.sock, saddr, NULL, &error)) + return; + + cm->con.udp.connected = TRUE; + cm->con.udp.source = g_socket_create_source(cm->con.udp.sock, G_IO_IN, NULL); + g_source_set_callback(cm->con.udp.source, (GSourceFunc) read_udp, cm, NULL); + g_source_attach(cm->con.udp.source, NULL); + + do_udp_ping(cm); +} + static void connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { @@ -48,7 +204,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, cm, NULL); g_source_attach(con->source, NULL); - g_source_unref(con->source); cmumble_protocol_init(cm); } |