1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#include <glib.h>
#include <gio/gio.h>
#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 cmumble *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_socket(GSocket *socket, GIOCondition condition, gpointer user_data)
{
struct cmumble *cm = user_data;
GError *error = NULL;
guchar data[1024];
guchar plain[1020];
gssize size;
size = g_socket_receive(socket, (gchar *) data, sizeof(data),
NULL, &error);
if (size <= 4)
return TRUE;
if (!CryptState_decrypt(&cm->crypt, data, plain, size))
return TRUE;
cm->con.udp.connected = TRUE;
cmumble_read_udp_data(cm, plain, size - 4);
return TRUE;
}
static void
send_udp_tcptunnel(struct cmumble *cm, guint8 *data, gsize len)
{
MumbleProto__UDPTunnel tunnel;
mumble_proto__udptunnel__init(&tunnel);
tunnel.packet.data = data;
tunnel.packet.len = len;
cmumble_send_msg(cm, &tunnel.base);
}
static void
send_udp(struct cmumble *cm, guint8 *data, gsize len)
{
guint8 encrypted[1024];
GError *error = NULL;
g_assert(len <= (sizeof(encrypted) - 4));
CryptState_encrypt(&cm->crypt, data, encrypted, len);
g_socket_send(cm->con.udp.sock, (gchar *) encrypted, len + 4,
NULL, &error);
}
void
cmumble_connection_send_udp_data(struct cmumble *cm, guint8 *data, gsize len)
{
if (cm->con.udp.connected)
send_udp(cm, data, len);
else
send_udp_tcptunnel(cm, data, len);
}
static void
do_udp_ping(struct cmumble *cm)
{
uint8_t data[16];
uint32_t written = 0, pos = 0;
GTimeVal tv;
g_get_current_time(&tv);
data[pos++] = (udp_ping << 5);
encode_varint(&data[pos], &written, tv.tv_sec, sizeof(data)-pos);
pos += written;
send_udp(cm, data, pos);
g_print("udp ping sent: timestamp: %ld\n", tv.tv_sec);
}
void
cmumble_connection_udp_init(struct cmumble *cm)
{
GError *error = NULL;
GSocketAddress *saddr;
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);
saddr = g_socket_connection_get_remote_address(cm->con.conn, &error);
if (saddr == NULL) {
g_printerr("Failed to get remote address for udp setup: %s\n",
error->message);
return;
}
if (!g_socket_connect(cm->con.udp.sock, saddr, NULL, &error)) {
g_object_unref(saddr);
g_object_unref(cm->con.udp.sock);
cm->con.udp.sock = NULL;
return;
}
g_object_unref(saddr);
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_socket, 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 cmumble *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 cmumble *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 cmumble *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;
}
|