libjuice-sys 0.9.7

Native bindings for libjuice
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
 * Copyright (c) 2020 Paul-Louis Ageneau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "turn.h"
#include "log.h"
#include "random.h"
#include "socket.h"

#include <string.h>

static bool memory_is_zero(const void *data, size_t size) {
	const char *d = data;
	for (size_t i = 0; i < size; ++i)
		if (d[i])
			return false;

	return true;
}

static uint16_t random_channel_number() {
	/*
	 * RFC 8656 12. Channels
	 * The ChannelData message (see Section 12.4) starts with a two-byte
	 * field that carries the channel number.  The values of this field are
	 * allocated as follows:
	 *
	 *   +------------------------+--------------------------------------+
	 *   | 0x0000 through 0x3FFF: | These values can never be used for   |
	 *   |                        | channel numbers.                     |
	 *   +------------------------+--------------------------------------+
	 *   | 0x4000 through 0x4FFF: | These values are the allowed channel |
	 *   |                        | numbers (4096 possible values).      |
	 *   +------------------------+--------------------------------------+
	 *   | 0x5000 through 0xFFFF: | Reserved (For DTLS-SRTP multiplexing |
	 *   |                        | collision avoidance, see [RFC7983]). |
	 *   +------------------------+--------------------------------------+
	 */
	uint16_t r;
	juice_random(&r, 2);
	return 0x4000 | (r & 0x0FFF);
}

bool is_channel_data(const void *data, size_t size) {
	// According RFC 8656, first byte in [64..79] is TURN Channel
	if (size == 0)
		return false;
	uint8_t b = *((const uint8_t *)data);
	return b >= 64 && b <= 79;
}

bool is_valid_channel(uint16_t channel) { return channel >= 0x4000; }

int turn_wrap_channel_data(char *buffer, size_t size, const char *data, size_t data_size,
                           uint16_t channel) {
	if (!is_valid_channel(channel)) {
		JLOG_WARN("Invalid channel number: 0x%hX", channel);
		return -1;
	}
	if (data_size >= 65536) {
		JLOG_WARN("ChannelData is too long, size=%zu", size);
		return -1;
	}
	if (size < sizeof(struct channel_data_header) + data_size) {
		JLOG_WARN("Buffer is too small to add ChannelData header, size=%zu, needed=%zu", size,
		          sizeof(struct channel_data_header) + data_size);
		return -1;
	}

	memmove(buffer + sizeof(struct channel_data_header), data, data_size);
	struct channel_data_header *header = (struct channel_data_header *)buffer;
	header->channel_number = htons((uint16_t)channel);
	header->length = htons((uint16_t)data_size);
	return (int)(sizeof(struct channel_data_header) + data_size);
}

static int find_ordered_channel_rec(turn_entry_t *const ordered_channels[], uint16_t channel,
                                    int begin, int end) {
	int d = end - begin;
	if (d <= 0)
		return begin;

	int pivot = begin + d / 2;
	const turn_entry_t *entry = ordered_channels[pivot];
	if (channel < entry->channel)
		return find_ordered_channel_rec(ordered_channels, channel, begin, pivot);
	else if (channel > entry->channel)
		return find_ordered_channel_rec(ordered_channels, channel, pivot + 1, end);
	else
		return pivot;
}

static int find_ordered_channel(const turn_map_t *map, uint16_t channel) {
	return find_ordered_channel_rec(map->ordered_channels, channel, 0, map->channels_count);
}

static int find_ordered_transaction_id_rec(turn_entry_t *const ordered_transaction_ids[],
                                           const uint8_t *transaction_id, int begin, int end) {
	int d = end - begin;
	if (d <= 0)
		return begin;

	int pivot = begin + d / 2;
	const turn_entry_t *entry = ordered_transaction_ids[pivot];
	int ret = memcmp(transaction_id, entry->transaction_id, STUN_TRANSACTION_ID_SIZE);
	if (ret < 0)
		return find_ordered_transaction_id_rec(ordered_transaction_ids, transaction_id, begin,
		                                       pivot);
	else if (ret > 0)
		return find_ordered_transaction_id_rec(ordered_transaction_ids, transaction_id, pivot + 1,
		                                       end);
	else
		return pivot;
}

static int find_ordered_transaction_id(const turn_map_t *map, const uint8_t *transaction_id) {
	return find_ordered_transaction_id_rec(map->ordered_transaction_ids, transaction_id, 0,
	                                       map->transaction_ids_count);
}

static void remove_ordered_transaction_id(turn_map_t *map, const uint8_t *transaction_id) {
	int pos = find_ordered_transaction_id(map, transaction_id);
	if (pos < map->transaction_ids_count) {
		memmove(map->ordered_transaction_ids + pos, map->ordered_transaction_ids + pos + 1,
		        (map->transaction_ids_count - (pos + 1)) * sizeof(turn_entry_t *));
		map->transaction_ids_count--;
	}
}
/*
static void remove_ordered_channel(turn_map_t *map, uint16_t channel) {
    int pos = find_ordered_channel(map, channel);
    if (pos < map->channels_count) {
        memmove(map->ordered_channels + pos, map->ordered_channels + pos + 1,
                (map->channels_count - (pos + 1)) * sizeof(turn_entry_t *));
        map->channels_count--;
    }
}

static void delete_entry(turn_map_t *map, turn_entry_t *entry) {
    if (entry->type == TURN_ENTRY_TYPE_EMPTY || entry->type == TURN_ENTRY_TYPE_DELETED)
        return;

    if (!memory_is_zero(entry->transaction_id, STUN_TRANSACTION_ID_SIZE))
        remove_ordered_transaction_id(map, entry->transaction_id);

    if (entry->type == TURN_ENTRY_TYPE_CHANNEL && entry->channel)
        remove_ordered_channel(map, entry->channel);

    memset(entry, 0, sizeof(*entry));
    entry->type = TURN_ENTRY_TYPE_DELETED;
}
*/
static turn_entry_t *find_entry(turn_map_t *map, const addr_record_t *record,
                                turn_entry_type_t type, bool allow_deleted) {
	unsigned long key = (addr_record_hash(record, false) + (int)type) % map->map_size;
	unsigned long pos = key;
	while (true) {
		turn_entry_t *entry = map->map + pos;
		if (entry->type == TURN_ENTRY_TYPE_EMPTY ||
		    (entry->type == type && addr_record_is_equal(&entry->record, record, false)))
			break;

		if (allow_deleted && entry->type == TURN_ENTRY_TYPE_DELETED)
			break;

		pos = (pos + 1) % map->map_size;
		if (pos == key) {
			JLOG_VERBOSE("TURN map is full");
			return NULL;
		}
	}
	return map->map + pos;
}

static bool update_timestamp(turn_map_t *map, turn_entry_type_t type, const uint8_t *transaction_id,
                             const addr_record_t *record, timediff_t duration) {
	turn_entry_t *entry;
	if (record) {
		entry = find_entry(map, record, type, true);
		if (!entry)
			return false;

		if (entry->type == type) {
			if (memcmp(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE) == 0)
				return true;
		} else {
			entry->type = type;
			entry->record = *record;
		}

		if (!memory_is_zero(entry->transaction_id, STUN_TRANSACTION_ID_SIZE))
			remove_ordered_transaction_id(map, entry->transaction_id);

		memcpy(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE);

	} else {
		int pos = find_ordered_transaction_id(map, transaction_id);
		if (pos == map->transaction_ids_count)
			return false;

		entry = map->ordered_transaction_ids[pos];
		if (entry->type != type ||
		    memcmp(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE) != 0)
			return false;
	}

	entry->timestamp = current_timestamp() + duration;
	entry->fresh_transaction_id = false;
	return true;
}

int turn_init_map(turn_map_t *map, int size) {
	memset(map, 0, sizeof(*map));

	map->map_size = size * 2;
	map->channels_count = 0;
	map->transaction_ids_count = 0;

	map->map = calloc(sizeof(turn_entry_t), map->map_size);
	map->ordered_channels = calloc(sizeof(turn_entry_t *), map->map_size);
	map->ordered_transaction_ids = calloc(sizeof(turn_entry_t *), map->map_size);

	if (!map->map || !map->ordered_channels || !map->ordered_transaction_ids) {
		JLOG_ERROR("Failed to allocate TURN map of size %d", size);
		turn_destroy_map(map);
		return -1;
	}

	return 0;
}

void turn_destroy_map(turn_map_t *map) {
	free(map->map);
	free(map->ordered_channels);
	free(map->ordered_transaction_ids);
}

bool turn_set_permission(turn_map_t *map, const uint8_t *transaction_id,
                         const addr_record_t *record, timediff_t duration) {
	return update_timestamp(map, TURN_ENTRY_TYPE_PERMISSION, transaction_id, record, duration);
}

bool turn_has_permission(turn_map_t *map, const addr_record_t *record) {
	turn_entry_t *entry = find_entry(map, record, TURN_ENTRY_TYPE_PERMISSION, false);
	if (!entry || entry->type != TURN_ENTRY_TYPE_PERMISSION)
		return false;

	return current_timestamp() < entry->timestamp;
}

bool turn_bind_channel(turn_map_t *map, const addr_record_t *record, const uint8_t *transaction_id,
                       uint16_t channel, timediff_t duration) {
	if (!is_valid_channel(channel)) {
		JLOG_ERROR("Invalid channel number: 0x%hX", channel);
		return false;
	}

	turn_entry_t *entry = find_entry(map, record, TURN_ENTRY_TYPE_CHANNEL, true);
	if (!entry)
		return false;

	if (entry->type == TURN_ENTRY_TYPE_CHANNEL && entry->channel) {
		if (entry->channel != channel) {
			JLOG_WARN("The record is already bound to a channel");
			return false;
		}

		entry->timestamp = current_timestamp() + duration;
		return true;
	}

	int pos = find_ordered_channel(map, channel);
	if (pos < map->channels_count) {
		const turn_entry_t *other_entry = map->ordered_channels[pos];
		if (other_entry->channel == channel) {
			JLOG_WARN("The channel is already bound to a record");
			return false;
		}
	}

	if (entry->type != TURN_ENTRY_TYPE_CHANNEL) {
		entry->type = TURN_ENTRY_TYPE_CHANNEL;
		entry->record = *record;
	}

	memmove(map->ordered_channels + pos + 1, map->ordered_channels + pos,
	        (map->channels_count - pos) * sizeof(turn_entry_t));
	map->ordered_channels[pos] = entry;
	map->channels_count++;

	entry->channel = channel;
	entry->timestamp = current_timestamp() + duration;

	if (transaction_id) {
		memcpy(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE);
		entry->fresh_transaction_id = true;
	}

	return true;
}

bool turn_bind_random_channel(turn_map_t *map, const addr_record_t *record, uint16_t *channel,
                              timediff_t duration) {
	uint16_t c;
	do {
		c = random_channel_number();
	} while (turn_find_channel(map, c, NULL));

	if (!turn_bind_channel(map, record, NULL, c, duration))
		return false;

	if (channel)
		*channel = c;

	return true;
}

bool turn_bind_current_channel(turn_map_t *map, const uint8_t *transaction_id,
                               const addr_record_t *record, timediff_t duration) {
	return update_timestamp(map, TURN_ENTRY_TYPE_CHANNEL, transaction_id, record, duration);
}

bool turn_get_channel(turn_map_t *map, const addr_record_t *record, uint16_t *channel) {
	turn_entry_t *entry = find_entry(map, record, TURN_ENTRY_TYPE_CHANNEL, false);
	if (!entry || entry->type != TURN_ENTRY_TYPE_CHANNEL)
		return false;

	if (channel)
		*channel = entry->channel;

	return true;
}

bool turn_get_bound_channel(turn_map_t *map, const addr_record_t *record, uint16_t *channel) {
	turn_entry_t *entry = find_entry(map, record, TURN_ENTRY_TYPE_CHANNEL, false);
	if (!entry || entry->type != TURN_ENTRY_TYPE_CHANNEL)
		return false;

	if (!entry->channel || current_timestamp() >= entry->timestamp)
		return false;

	if (channel)
		*channel = entry->channel;

	return true;
}

bool turn_find_channel(turn_map_t *map, uint16_t channel, addr_record_t *record) {
	if (!is_valid_channel(channel)) {
		JLOG_WARN("Invalid channel number: 0x%hX", channel);
		return false;
	}

	int pos = find_ordered_channel(map, channel);
	if (pos == map->channels_count)
		return false;

	const turn_entry_t *entry = map->ordered_channels[pos];
	if (entry->channel != channel)
		return false;

	if (record)
		*record = entry->record;

	return true;
}

bool turn_find_bound_channel(turn_map_t *map, uint16_t channel, addr_record_t *record) {
	if (!is_valid_channel(channel)) {
		JLOG_WARN("Invalid channel number: 0x%hX", channel);
		return false;
	}

	int pos = find_ordered_channel(map, channel);
	if (pos == map->channels_count)
		return false;

	const turn_entry_t *entry = map->ordered_channels[pos];
	if (entry->channel != channel || current_timestamp() >= entry->timestamp)
		return false;

	if (record)
		*record = entry->record;

	return true;
}

static bool set_transaction_id(turn_map_t *map, turn_entry_type_t type, const addr_record_t *record,
                               const uint8_t *transaction_id) {
	if (type != TURN_ENTRY_TYPE_PERMISSION && type != TURN_ENTRY_TYPE_CHANNEL)
		return false;

	turn_entry_t *entry = find_entry(map, record, type, true);
	if (!entry)
		return false;

	if (entry->type == type && !memory_is_zero(entry->transaction_id, STUN_TRANSACTION_ID_SIZE))
		remove_ordered_transaction_id(map, entry->transaction_id);

	int pos = find_ordered_transaction_id(map, transaction_id);
	memmove(map->ordered_transaction_ids + pos + 1, map->ordered_transaction_ids + pos,
	        (map->transaction_ids_count - pos) * sizeof(turn_entry_t *));
	map->ordered_transaction_ids[pos] = entry;
	map->transaction_ids_count++;

	if (entry->type != type) {
		entry->type = type;
		entry->record = *record;
	}

	memcpy(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE);
	entry->fresh_transaction_id = true;
	return true;
}

static bool find_transaction_id(turn_map_t *map, const uint8_t *transaction_id,
                                addr_record_t *record) {
	int pos = find_ordered_transaction_id(map, transaction_id);
	if (pos == map->transaction_ids_count)
		return false;

	const turn_entry_t *entry = map->ordered_transaction_ids[pos];
	if (memcmp(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE) != 0)
		return false;

	if (record)
		*record = entry->record;

	return true;
}

static bool set_random_transaction_id(turn_map_t *map, turn_entry_type_t type,
                                      const addr_record_t *record, uint8_t *transaction_id) {
	turn_entry_t *entry = find_entry(map, record, type, false);
	if (entry && entry->fresh_transaction_id) {
		if (transaction_id)
			memcpy(transaction_id, entry->transaction_id, STUN_TRANSACTION_ID_SIZE);

		return true;
	}

	uint8_t tid[STUN_TRANSACTION_ID_SIZE];
	do {
		juice_random(tid, STUN_TRANSACTION_ID_SIZE);
	} while (find_transaction_id(map, tid, NULL));

	if (!set_transaction_id(map, type, record, tid))
		return false;

	if (transaction_id)
		memcpy(transaction_id, tid, STUN_TRANSACTION_ID_SIZE);

	return true;
}

bool turn_set_permission_transaction_id(turn_map_t *map, const addr_record_t *record,
                                        const uint8_t *transaction_id) {
	return set_transaction_id(map, TURN_ENTRY_TYPE_PERMISSION, record, transaction_id);
}

bool turn_set_channel_transaction_id(turn_map_t *map, const addr_record_t *record,
                                     const uint8_t *transaction_id) {
	return set_transaction_id(map, TURN_ENTRY_TYPE_CHANNEL, record, transaction_id);
}

bool turn_set_random_permission_transaction_id(turn_map_t *map, const addr_record_t *record,
                                               uint8_t *transaction_id) {
	return set_random_transaction_id(map, TURN_ENTRY_TYPE_PERMISSION, record, transaction_id);
}

bool turn_set_random_channel_transaction_id(turn_map_t *map, const addr_record_t *record,
                                            uint8_t *transaction_id) {
	return set_random_transaction_id(map, TURN_ENTRY_TYPE_CHANNEL, record, transaction_id);
}

bool turn_retrieve_transaction_id(turn_map_t *map, const uint8_t *transaction_id,
                                  addr_record_t *record) {
	int pos = find_ordered_transaction_id(map, transaction_id);
	if (pos == map->transaction_ids_count)
		return false;

	turn_entry_t *entry = map->ordered_transaction_ids[pos];
	if (memcmp(entry->transaction_id, transaction_id, STUN_TRANSACTION_ID_SIZE) != 0)
		return false;

	if (record)
		*record = entry->record;

	entry->fresh_transaction_id = false;
	return true;
}