summaryrefslogtreecommitdiff
path: root/src/socket.c
blob: 690b94fd9d314c9bf09f128695e85b5c7a70e7ab (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include <sys/types.h>

#include "mumble.pb-c.h"

#include "polarssl/net.h"
#include "polarssl/ssl.h"
#include "polarssl/havege.h"

#include <celt/celt.h>
#include <celt/celt_header.h>
#include <speex/speex_jitter.h>

#include <glib.h>
#include <glib-object.h>

#include "messages.h"

#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))

#define PREAMBLE_SIZE 6

struct context {
	GMainLoop *loop;

	uint32_t session;
	bool authenticated;

	ssl_context ssl;
	havege_state hs;
	ssl_session ssn;
	int sock;
	GIOChannel *sock_channel;

	CELTHeader celt_header;
	CELTMode *celt_mode;
};

enum udp_message_type {
	udp_voice_celt_alpha,
	udp_ping,
	udp_voice_speex,
	udp_voice_celt_beta,
};

int64_t
decode_varint(uint8_t *data, uint32_t *read, uint32_t left)
{
	int64_t varint = 0;

	/* 1 byte with 7 · 8 + 1 leading zeroes */
	if ((data[0] & 0x80) == 0x00) {
		varint = data[0] & 0x7F;
		*read = 1;
	/* 2 bytes with 6 · 8 + 2 leading zeroes */
	} else if ((data[0] & 0xC0) == 0x80) {
		varint = ((data[0] & 0x3F) << 8) | data[1];
		*read = 2;
	/* 3 bytes with 5 · 8 + 3 leading zeroes */
	} else if ((data[0] & 0xE0) == 0xC0) {
		varint = (((data[0] & 0x1F) << 16) |
			    (data[1] << 8) | (data[2]));
		*read = 3;
	/* 4 bytes with 4 · 8 + 4 leading zeroes */
	} else if ((data[0] & 0xF0) == 0xE0) {
		varint = (((data[0] & 0x0F) << 24) | (data[1] << 16) |
			  (data[2] << 8) | (data[3]));
		*read = 4;
	} else /* if ((data[pos] & 0xF0) == 0xF0) */ {
		switch (data[0] & 0xFC) {
		/* 32-bit positive number */
		case 0xF0:
			varint = ((data[1] << 24) | (data[2] << 16) |
				  (data[3] << 8) | data[4]);
			*read = 1 + 4;
			break;
		/* 64-bit number */
		case 0xF4:
			varint =
				((int64_t)data[1] << 56) | ((int64_t)data[2] << 48) |
				((int64_t)data[3] << 40) | ((int64_t)data[4] << 32) |
				(data[5] << 24) | (data[6] << 16) |
				(data[7] <<  8) | (data[8] <<  0);
			*read = 1 + 8;
			break;
		/* Negative varint */
		case 0xF8:
			/* FIXME: handle endless recursion */
			varint = -decode_varint(&data[1], read, left - 1);
			*read += 1;
			break;
		/* Negative two bit number */
		case 0xFC:
			varint = -(int)(data[0] & 0x03);
			*read = 1;
			break;
		}
	}

	return varint;
}

static void
handle_udp(struct context *ctx, uint8_t *data, uint32_t len)
{
	int64_t session;
	int64_t sequence;
	int pos = 1;
	int read = 0;

#define PCM_SIZE (48000/100 * 1)
	int16_t pcm[PCM_SIZE];
	uint8_t buf[BUFSIZ];
	FILE *f;
	int frame_len, term;
	CELTDecoder *dec_state;
	JitterBuffer *jitter;
	CELTMode *mode;
	static int iseq = 0;

	session  = decode_varint(&data[pos], &read, len-pos);
	pos += read;
	sequence = decode_varint(&data[pos], &read, len-pos);
	pos += read;
	printf("session: %ld, sequence: %ld\n", session, sequence);

	f = fopen("foo", "a+");

	dec_state = celt_decoder_create(ctx->celt_mode,
					ctx->celt_header.nb_channels, NULL);

	jitter = jitter_buffer_init(ctx->celt_header.frame_size);
	jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MARGIN,
			  &ctx->celt_header.frame_size);

	do {
		frame_len = (data[pos] & 0x7F);
		term = (data[pos] & 0x80) == 0x80;
		printf("_len: %d, term: %d\n", frame_len, term);
		pos += 1;
		
		if (frame_len == 0)
			break;

		JitterBufferPacket packet;
		packet.data = &data[pos];
		packet.len = frame_len;
		packet.timestamp = ctx->celt_header.frame_size * iseq++;
		packet.span = ctx->celt_header.frame_size;
		packet.sequence = 0;

		jitter_buffer_put(jitter, &packet);
		
		packet.data = buf;
		packet.len = BUFSIZ;
		jitter_buffer_tick(jitter);
		jitter_buffer_get(jitter, &packet, ctx->celt_header.frame_size, NULL);

		if (packet.len == 0)
			packet.data = NULL;

		celt_decode(dec_state, packet.data, packet.len, pcm);
		fwrite(pcm, sizeof(int16_t), PCM_SIZE, f);

		pos += frame_len;
		sequence++;
	} while (term);

	fclose(f);
	celt_decoder_destroy(dec_state);
}

static void
add_preamble(uint8_t *buffer, uint16_t type, uint32_t len)
{
	buffer[1] = (type) & 0xff;
	buffer[0] = (type >> 8) & 0xff;

	buffer[5] = (len) & 0xff;
	buffer[4] = (len >> 8) & 0xff;
	buffer[3] = (len >> 16) & 0xff;
	buffer[2] = (len >> 24) & 0xff;	
}

static void
get_preamble(uint8_t *buffer, int *type, int *len)
{
	uint16_t msgType;
	uint32_t msgLen;

	msgType = buffer[1] | (buffer[0] << 8);
	msgLen = buffer[5] | (buffer[4] << 8) | (buffer[3] << 16) | (buffer[2] << 24);
	*type = (int)msgType;
	*len = (int)msgLen;
}

static void
recv_version(MumbleProto__Version *version, struct context *ctx)
{
	printf("version: 0x%x\n", version->version);
	printf("release: %s\n", version->release);
}

static void
recv_channel_state(MumbleProto__ChannelState *state, struct context *ctx)
{
	printf("channel: id: %u, parent: %u, name: %s, description: %s, temporary: %d, position: %d\n",
	       state->channel_id, state->parent, state->name, state->description, state->temporary, state->position);
}

static void
send_msg(struct context *ctx, ProtobufCMessage *msg)
{
	uint8_t pad[128];
	uint8_t preamble[PREAMBLE_SIZE];
	int ret = 0;
	int type = -1;
	int i;
	ProtobufCBufferSimple buffer = PROTOBUF_C_BUFFER_SIMPLE_INIT(pad);
	
	for (i = 0; i < ARRAY_SIZE(messages); ++i)
		if (messages[i].descriptor == msg->descriptor)
			type = i;
	assert(type >= 0);

	protobuf_c_message_pack_to_buffer(msg, &buffer.base);
	add_preamble(preamble, type, buffer.len);

	while ((ret = ssl_write(&ctx->ssl, preamble, PREAMBLE_SIZE)) <= 0) {
		if (ret != POLARSSL_ERR_NET_TRY_AGAIN) {
			printf("write failed: %d\n", ret);
			abort();
		}
	}
	while ((ret = ssl_write(&ctx->ssl, buffer.data, buffer.len)) < buffer.len) {
		if (ret != POLARSSL_ERR_NET_TRY_AGAIN) {
			printf("write failed: %d\n", ret);
			abort();
		}
	}

	PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&buffer);
}

typedef void (*callback_t)(void *, void *);

static void
recv_msg(struct context *ctx, const callback_t *callbacks, uint32_t callback_size)
{
	uint8_t preamble[PREAMBLE_SIZE];
	ProtobufCMessage *msg;
	void *data;
	int type, len;
	int ret, i;

	printf("recv msg\n");

	do {
		ret = ssl_read(&ctx->ssl, preamble, 6);
		if (ret == POLARSSL_ERR_NET_CONN_RESET) {
			printf("conn reset\n");
			exit(1);
		}
	} while (ret == POLARSSL_ERR_NET_TRY_AGAIN);

	if (ret <= 0) {
		printf("read failed: %d\n", ret);
		return;
	}
	
	get_preamble(preamble, &type, &len);

	if (!(type >= 0 && type < ARRAY_SIZE(messages))) {
		printf("unknown message type: %d\n", type);
		return;
	}

	if (len <= 0) {
		printf("length 0\n");
		return;
	}

	data = malloc(len);
	if (data == NULL) {
		printf("out of mem\n");
		abort();
	}
	ret = ssl_read(&ctx->ssl, data, len);
	if (ret == POLARSSL_ERR_NET_CONN_RESET) {
		printf("conn reset\n");
		exit(1);
	}

	/* tunneled udp data - not a regular protobuf message */
	if (type == 1) {
		handle_udp(ctx, data, len);
		free(data);
		return;
	}

	msg = protobuf_c_message_unpack(messages[type].descriptor, NULL,
					len, data);
	if (msg == NULL) {
		printf("message unpack failure\n");
		return ;
	}

	printf("debug: received message: %s type:%d, len:%d\n", messages[type].name, type, len);
	if (callbacks[type])
		callbacks[type](msg, ctx);

	protobuf_c_message_free_unpacked(msg, NULL);
	free(data);
}

static void
my_debug(void *ctx, int level, const char *str)
{
	if (level <= 1)
		printf("polarssl [level %d]: %s\n", level, str);
}

static void
do_ping(struct context *ctx)
{
	MumbleProto__Ping ping;
	struct timeval tv;

	gettimeofday(&tv, NULL);
	mumble_proto__ping__init(&ping);

	ping.timestamp = tv.tv_sec;
	ping.resync = 1;

	send_msg(ctx, &ping.base);
}

static const callback_t callbacks[] = {
	/* VERSION */ (callback_t) recv_version,
	[7] = (callback_t) recv_channel_state,
	[127] = NULL,
};

static gboolean
_recv(GIOChannel *source, GIOCondition condition, gpointer data)
{
	struct context *ctx = data;
	do {
		recv_msg(ctx, callbacks, ARRAY_SIZE(callbacks));
	} while (ssl_get_bytes_avail(&ctx->ssl) > 0);

	do_ping(ctx);

	return TRUE;
}

int main(int argc, char **argv)
{
#if 1
	char *host = "localhost";
	unsigned int port = 64738;
#else
	char *host = "85.214.21.153";
	unsigned int port = 33321;
#endif
	struct context ctx;
	int ret;

	memset(&ctx, 0, sizeof(ctx));

	ssl_init(&ctx.ssl);
	havege_init( &ctx.hs );

	ret = net_connect(&ctx.sock, host, port);
	ssl_set_endpoint(&ctx.ssl, SSL_IS_CLIENT);
	ssl_set_authmode(&ctx.ssl, SSL_VERIFY_NONE);

	ssl_set_rng(&ctx.ssl, havege_rand, &ctx.hs);
	ssl_set_dbg(&ctx.ssl, my_debug, NULL);
	ssl_set_bio(&ctx.ssl, net_recv, &ctx.sock, net_send, &ctx.sock);

	//ssl_set_session(&ctx.ssl, 1, 600, &ssn);
	ssl_set_session(&ctx.ssl, 0, 0, &ctx.ssn);
	ssl_set_ciphers(&ctx.ssl, ssl_default_ciphers);

	{
		MumbleProto__Version version;
		mumble_proto__version__init(&version);
		version.version = 0x010203;
		version.release = "cmumble 0.1";
		version.os = "Gentoo/Linux";
		send_msg(&ctx, &version.base);
	}

	{
		MumbleProto__Authenticate authenticate;
		mumble_proto__authenticate__init(&authenticate);
		authenticate.username = "ben2";
		authenticate.password = "";
		authenticate.n_celt_versions = 1;
		authenticate.celt_versions = (int32_t[]) { 0x8000000b };
		send_msg(&ctx, &authenticate.base);
	}

	do_ping(&ctx);

#define SAMPLERATE 48000
#define CHANNELS 1
	ctx.celt_mode = celt_mode_create(SAMPLERATE, SAMPLERATE / 100, NULL);
	celt_header_init(&ctx.celt_header, ctx.celt_mode, CHANNELS);
	uint8_t celt_header_packet[sizeof(CELTHeader)];
	printf("extra headers: %d\n", ctx.celt_header.extra_headers);
	celt_header_to_packet(&ctx.celt_header, celt_header_packet, sizeof(CELTHeader));

	g_type_init();
	ctx.loop = g_main_loop_new(NULL, FALSE);

	ctx.sock_channel = g_io_channel_unix_new(ctx.sock);
	g_io_add_watch(ctx.sock_channel, G_IO_IN | G_IO_ERR, _recv, &ctx);

	g_main_loop_run(ctx.loop);

	g_main_loop_unref(ctx.loop);

	net_close(ctx.sock);
	ssl_free(&ctx.ssl);
	memset(&ctx.ssl, 0, sizeof(ctx.ssl));

}