#include #include #include "connection.h" #include "cmumble.h" #include "varint.h" static gboolean read_cb(GObject *pollable_stream, gpointer data) { GPollableInputStream *input = G_POLLABLE_INPUT_STREAM(pollable_stream); struct cmumlbe *cm = data; gint count; do { count = cmumble_recv_msg(cm); } while (count && g_pollable_input_stream_is_readable(input)); 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) { struct cmumlbe *cm = user_data; struct cmumble_connection *con = &cm->con; GError *error = NULL; con->conn = g_socket_client_connect_to_host_finish (con->sock_client, res, &error); if (error) { g_printerr("connect failed: %s\n", error->message); g_main_loop_quit(cm->loop); g_error_free(error); return; } g_object_get(G_OBJECT(con->conn), "input-stream", &con->input, "output-stream", &con->output, NULL); if (!G_IS_POLLABLE_INPUT_STREAM(con->input) || !g_pollable_input_stream_can_poll(con->input)) { g_printerr("Error: GSocketConnection is not pollable\n"); g_main_loop_quit(cm->loop); return; } 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); cmumble_protocol_init(cm); } int cmumble_connection_init(struct cmumlbe *cm, const char *host, int port) { struct cmumble_connection *con = &cm->con; con->sock_client = g_socket_client_new(); g_socket_client_set_tls(con->sock_client, TRUE); g_socket_client_set_tls_validation_flags(con->sock_client, G_TLS_CERTIFICATE_INSECURE); g_socket_client_set_family(con->sock_client, G_SOCKET_FAMILY_IPV4); g_socket_client_set_protocol(con->sock_client, G_SOCKET_PROTOCOL_TCP); g_socket_client_set_socket_type(con->sock_client, G_SOCKET_TYPE_STREAM); g_socket_client_connect_to_host_async(con->sock_client, host, port, NULL, connection_ready, cm); return 0; } int cmumble_connection_fini(struct cmumlbe *cm) { if (cm->con.source) { g_source_remove(g_source_get_id(cm->con.source)); g_source_unref(cm->con.source); } if (cm->con.conn) { g_object_unref(G_OBJECT(cm->con.input)); g_object_unref(G_OBJECT(cm->con.output)); g_io_stream_close(G_IO_STREAM(cm->con.conn), NULL, NULL); g_object_unref(G_OBJECT(cm->con.conn)); } if (cm->con.sock_client) g_object_unref(G_OBJECT(cm->con.sock_client)); return 0; }