-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrawrtc-terminal.c
892 lines (754 loc) · 27.7 KB
/
rawrtc-terminal.c
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
#include <string.h> // memcpy
#include <unistd.h> // STDIN_FILENO, STDOUT_FILENO, close, execvp, read, write
#include <limits.h> // USHRT_MAX
#include <signal.h> // SIGTERM, kill
#include <stdlib.h> // setenv
#include <termios.h> // ioctl, struct winsize
#include <pty.h> // forkpty
#include <rawrtc.h>
#include "helper/utils.h"
#include "helper/handler.h"
#include "helper/parameters.h"
#define DEBUG_MODULE "rawrtc-terminal"
#define DEBUG_LEVEL 7
#include <re_dbg.h>
enum {
PIPE_READ_BUFFER = 4096
};
// Control message types
enum {
CONTROL_MESSAGE_WINDOW_SIZE_TYPE = 0
};
// Control message lengths
enum {
CONTROL_MESSAGE_WINDOW_SIZE_LENGTH = 5
};
static char const ws_uri_regex[] = "ws:[^]*";
struct parameters {
struct rawrtc_ice_parameters* ice_parameters;
struct rawrtc_ice_candidates* ice_candidates;
struct rawrtc_dtls_parameters* dtls_parameters;
struct sctp_parameters sctp_parameters;
};
// Note: Shadows struct client
struct terminal_client {
char* name;
char** ice_candidate_types;
size_t n_ice_candidate_types;
char* shell;
char* ws_uri;
struct rawrtc_ice_gather_options* gather_options;
enum rawrtc_ice_role role;
struct dnsc* dns_client;
struct http_cli* http_client;
struct websock* ws_socket;
struct rawrtc_certificate* certificate;
struct rawrtc_ice_gatherer* gatherer;
struct rawrtc_ice_transport* ice_transport;
struct rawrtc_dtls_transport* dtls_transport;
struct rawrtc_sctp_transport* sctp_transport;
struct rawrtc_data_transport* data_transport;
struct websock_conn* ws_connection;
struct list data_channels;
struct parameters local_parameters;
struct parameters remote_parameters;
};
struct terminal_client_channel {
pid_t pid;
int pty;
};
static void client_start_transports(
struct terminal_client* const client
);
static void client_stop(
struct terminal_client* const client
);
static void client_apply_parameters(
struct terminal_client* const client
);
static enum rawrtc_code client_decode_parameters(
struct parameters* const parametersp,
struct odict* const dict,
struct terminal_client* const client
);
static struct odict* client_encode_parameters(
struct terminal_client* const client
);
/*
* Print the WS close event.
*/
static void ws_close_handler(
int err,
void* arg
) {
struct terminal_client* const client = arg;
DEBUG_PRINTF("(%s) WS connection closed, reason: %m\n", client->name, err);
}
/*
* Receive the JSON encoded remote parameters, parse and apply them.
*/
static void ws_receive_handler(
struct websock_hdr const* header,
struct mbuf* buffer,
void* arg
) {
struct terminal_client* const client = arg;
enum rawrtc_code error;
struct odict* dict;
(void) header;
DEBUG_PRINTF("(%s) WS message of %zu bytes received\n", client->name, mbuf_get_left(buffer));
// Check opcode
if (header->opcode != WEBSOCK_TEXT) {
DEBUG_NOTICE("(%s) Unexpected opcode (%u) in WS message\n", client->name, header->opcode);
return;
}
// Decode JSON
error = rawrtc_error_to_code(json_decode_odict(
&dict, 16, (char*) mbuf_buf(buffer), mbuf_get_left(buffer), 3));
if (error) {
DEBUG_WARNING("(%s) Invalid remote parameters\n", client->name);
return;
}
// Decode parameters
if (client_decode_parameters(&client->remote_parameters, dict, client) == RAWRTC_CODE_SUCCESS) {
// Set parameters & start transports
client_apply_parameters(client);
client_start_transports(client);
// Close WS connection
EOR(websock_close(client->ws_connection, WEBSOCK_NORMAL_CLOSURE, NULL));
client->ws_connection = mem_deref(client->ws_connection);
}
// Un-reference
mem_deref(dict);
}
/*
* Send the JSON encoded local parameters to the other peer.
*/
static void ws_established_handler(
void* arg
) {
struct terminal_client* const client = arg;
struct odict* dict;
DEBUG_PRINTF("(%s) WS connection established\n", client->name);
// Encode parameters
dict = client_encode_parameters(client);
// Send as JSON
DEBUG_INFO("(%s) Sending local parameters\n", client->name);
EOR(websock_send(client->ws_connection, WEBSOCK_TEXT, "%H", json_encode_odict, dict));
// Un-reference
mem_deref(dict);
}
/*
* Parse the JSON encoded remote parameters and apply them.
*/
static void stdin_receive_handler(
int flags,
void* arg
) {
struct terminal_client* const client = arg;
struct odict* dict = NULL;
enum rawrtc_code error;
(void) flags;
// Get dict from JSON
error = get_json_stdin(&dict);
if (error) {
goto out;
}
// Decode parameters
if (client_decode_parameters(&client->remote_parameters, dict, client) == RAWRTC_CODE_SUCCESS) {
// Set parameters & start transports
client_apply_parameters(client);
client_start_transports(client);
}
out:
// Un-reference
mem_deref(dict);
// Exit?
if (error == RAWRTC_CODE_NO_VALUE) {
DEBUG_NOTICE("Exiting\n");
// Stop client & bye
client_stop(client);
before_exit();
exit(0);
}
}
/*
* Print the JSON encoded local parameters for the other peer.
*/
static void print_local_parameters(
struct terminal_client* const client
) {
struct odict* dict;
// Encode parameters
dict = client_encode_parameters(client);
// Print as JSON
DEBUG_INFO("Local Parameters:\n%H\n", json_encode_odict, dict);
// Un-reference
mem_deref(dict);
}
/*
* Print the local candidate. Open a connection to the WS server in
* case all candidates have been gathered.
*/
static void ice_gatherer_local_candidate_handler(
struct rawrtc_ice_candidate* const candidate,
char const * const url, // read-only
void* const arg
) {
struct terminal_client* const client = arg;
// Print local candidate
default_ice_gatherer_local_candidate_handler(candidate, url, arg);
// Print or send local parameters (if last candidate)
if (!candidate) {
if (client->ws_socket) {
EOR(websock_connect(
&client->ws_connection, client->ws_socket, client->http_client,
client->ws_uri, 30000,
ws_established_handler, ws_receive_handler, ws_close_handler,
client, NULL));
} else {
print_local_parameters(client);
}
}
}
/*
* Write the received data channel message's data to the PTY (or handle
* a control message).
*/
void data_channel_message_handler(
struct mbuf* const buffer,
enum rawrtc_data_channel_message_flag const flags,
void* const arg // will be casted to `struct data_channel_helper*`
) {
struct data_channel_helper* const channel = arg;
struct terminal_client_channel* const client_channel = channel->arg;
struct terminal_client* const client =
(struct terminal_client* const) channel->client;
size_t const length = mbuf_get_left(buffer);
(void) flags;
DEBUG_PRINTF("(%s.%s) Received %zu bytes\n", client->name, channel->label, length);
if (flags & RAWRTC_DATA_CHANNEL_MESSAGE_FLAG_IS_BINARY) {
uint_fast8_t type;
// Check size
if (length < 1) {
DEBUG_WARNING("(%s.%s) Invalid control message of size %zu\n",
client->name, channel->label, length);
return;
}
// Get type
type = mbuf_read_u8(buffer);
// Handle control message
switch (type) {
case CONTROL_MESSAGE_WINDOW_SIZE_TYPE:
// Check size
if (length < CONTROL_MESSAGE_WINDOW_SIZE_LENGTH) {
DEBUG_WARNING("(%s.%s) Invalid window size message of size %zu\n",
client->name, channel->label, length);
return;
}
{
uint_fast16_t columns;
uint_fast16_t rows;
struct winsize window_size = {0};
// Get window size
columns = ntohs(mbuf_read_u16(buffer));
rows = ntohs(mbuf_read_u16(buffer));
// Check window size
#if (UINT16_MAX > USHRT_MAX)
if (columns > USHRT_MAX || rows > USHRT_MAX) {
DEBUG_WARNING("(%s.%s) Invalid window size value\n",
client->name, channel->label);
return;
}
#endif
// Set window size
window_size.ws_col = (unsigned short) columns;
window_size.ws_row = (unsigned short) rows;
// Apply window size
DEBUG_PRINTF("(%s.%s) Resizing terminal to %"PRIuFAST16" columns and "
"%"PRIuFAST16" rows\n", client->name, channel->label, columns, rows);
EOP(ioctl(client_channel->pty, TIOCSWINSZ, &window_size));
}
break;
default:
DEBUG_WARNING("(%s.%s) Unknown control message %"PRIuFAST8"\n",
client->name, channel->label, length);
break;
}
} else {
// Write into PTY
// TODO: Handle EAGAIN?
DEBUG_PRINTF("(%s.%s) Piping %zu bytes into process...\n",
client->name, channel->label, length);
EOP(write(client_channel->pty, mbuf_buf(buffer), length));
DEBUG_PRINTF("(%s.%s) ... completed!\n", client->name, channel->label);
}
}
/*
* Stop the PTY.
*/
static void stop_process(
struct terminal_client_channel* const channel
) {
// Close PTY (if not already closed)
if (channel->pty != -1) {
// Stop listening on PTY
fd_close(channel->pty);
EOP(close(channel->pty));
// Invalidate PTY
channel->pty = -1;
}
// Stop process (if not already stopped)
if (channel->pid != -1) {
// Terminate process
EOP(kill(channel->pid, SIGTERM));
// Invalidate process
channel->pid = -1;
}
}
/*
* Stop the forked process on error event.
*/
static void data_channel_error_handler(
void* const arg // will be casted to `struct data_channel_helper*`
) {
struct data_channel_helper* const channel = arg;
struct terminal_client_channel* const client_channel = channel->arg;
// Print error event
default_data_channel_error_handler(arg);
// Stop forked process
if (client_channel->pid != -1) {
DEBUG_INFO("(%s.%s) Stopping process\n", channel->client->name, channel->label);
}
stop_process(client_channel);
}
/*
* Stop the forked process on close event.
*/
void data_channel_close_handler(
void* const arg // will be casted to `struct data_channel_helper*`
) {
struct data_channel_helper* const channel = arg;
struct terminal_client_channel* const client_channel = channel->arg;
// Print close event
default_data_channel_close_handler(arg);
// Stop forked process
if (client_channel->pid != -1) {
DEBUG_INFO("(%s.%s) Stopping process\n", channel->client->name, channel->label);
}
stop_process(client_channel);
}
/*
* Send the PTY's data on the data channel.
*/
static void pty_read_handler(
int flags,
void* arg
) {
struct data_channel_helper* const channel = arg;
struct terminal_client_channel* const client_channel = channel->arg;
struct terminal_client* const client =
(struct terminal_client* const) channel->client;
ssize_t length;
(void) flags;
// Create buffer
struct mbuf* const buffer = mbuf_alloc(PIPE_READ_BUFFER);
// Read from PTY into buffer
// TODO: Handle EAGAIN?
DEBUG_PRINTF("(%s.%s) Reading from process...\n", client->name, channel->label);
length = read(client_channel->pty, mbuf_buf(buffer), mbuf_get_space(buffer));
if (length == -1) {
switch (errno) {
case EIO:
// This happens when invoking 'exit' or similar commands
length = 0;
break;
default:
EOR(errno);
break;
}
}
mbuf_set_end(buffer, (size_t) length);
DEBUG_PRINTF("(%s.%s) ... read %zu bytes\n",
client->name, channel->label, mbuf_get_left(buffer));
// Process terminated?
if (length == 0) {
// Stop listening
if (client_channel->pid != -1) {
DEBUG_INFO("(%s.%s) Stopping process\n", channel->client->name, channel->label);
}
stop_process(client_channel);
// Close data channel
EOE(rawrtc_data_channel_close(channel->channel));
// Unreference helper
mem_deref(channel);
} else {
// Send the buffer
DEBUG_PRINTF("(%s.%s) Sending %zu bytes\n", client->name, channel->label, length);
EOE(rawrtc_data_channel_send(channel->channel, buffer, false));
}
// Clean up
mem_deref(buffer);
}
/*
* Fork and start the process on open event.
*/
static void data_channel_open_handler(
void* const arg // will be casted to `struct data_channel_helper*`
) {
struct data_channel_helper* const channel = arg;
struct terminal_client_channel* const client_channel = channel->arg;
struct terminal_client* const client =
(struct terminal_client* const) channel->client;
pid_t pid;
int pty;
// Print open event
default_data_channel_open_handler(arg);
// Fork to pseudo-terminal
// TODO: Check bounds (PID_MAX < INT_MAX...)
// TODO: Fix leaking FDs
DEBUG_INFO("(%s) Starting process for data channel %s\n",
channel->client->name, channel->label);
pid = (pid_t) forkpty(&pty, NULL, NULL, NULL);
EOP(pid);
// Child process
if (pid == 0) {
char* const args[] = {client->shell, NULL};
// Make it colourful!
EOP(setenv("TERM", "xterm-256color", 1));
// Run terminal
EOP(execvp(args[0], args));
EWE("Child process returned!\n");
}
// Set fields
client_channel->pid = pid;
client_channel->pty = pty;
// Listen on PTY
EOR(fd_listen(client_channel->pty, FD_READ, pty_read_handler, channel));
}
static void terminal_client_channel_destroy(
void* arg
) {
struct terminal_client_channel* const client_channel = arg;
// Stop process
stop_process(client_channel);
}
/*
* Handle the newly created data channel.
*/
static void data_channel_handler(
struct rawrtc_data_channel* const channel, // read-only, MUST be referenced when used
void* const arg // will be casted to `struct client*`
) {
struct terminal_client* const client = arg;
struct terminal_client_channel* client_channel;
struct data_channel_helper* channel_helper;
// Print channel
default_data_channel_handler(channel, arg);
// Create terminal client channel instance
client_channel = mem_zalloc(sizeof(*client_channel), terminal_client_channel_destroy);
if (!client_channel) {
EOE(RAWRTC_CODE_NO_MEMORY);
return;
}
// Set fields
client_channel->pid = -1;
client_channel->pty = -1;
// Create data channel helper instance
// Note: In this case we need to reference the channel because we have not created it
data_channel_helper_create_from_channel(&channel_helper, mem_ref(channel), arg, client_channel);
mem_deref(client_channel);
// Add to list
list_append(&client->data_channels, &channel_helper->le, channel_helper);
// Set handler argument & handlers
EOE(rawrtc_data_channel_set_arg(channel, channel_helper));
EOE(rawrtc_data_channel_set_open_handler(channel, data_channel_open_handler));
EOE(rawrtc_data_channel_set_buffered_amount_low_handler(
channel, default_data_channel_buffered_amount_low_handler));
EOE(rawrtc_data_channel_set_error_handler(channel, data_channel_error_handler));
EOE(rawrtc_data_channel_set_close_handler(channel, data_channel_close_handler));
EOE(rawrtc_data_channel_set_message_handler(channel, data_channel_message_handler));
}
static void client_init(
struct terminal_client* const client
) {
struct rawrtc_certificate* certificates[1];
if (client->ws_uri) {
// Create DNS client
EOR(dnsc_alloc(&client->dns_client, NULL, NULL, 0));
// Create HTTP client
EOR(http_client_alloc(&client->http_client, client->dns_client));
// Create WS Socket
EOR(websock_alloc(&client->ws_socket, NULL, client));
}
// Generate certificates
EOE(rawrtc_certificate_generate(&client->certificate, NULL));
certificates[0] = client->certificate;
// Create ICE gatherer
EOE(rawrtc_ice_gatherer_create(
&client->gatherer, client->gather_options,
default_ice_gatherer_state_change_handler, default_ice_gatherer_error_handler,
ice_gatherer_local_candidate_handler, client));
// Create ICE transport
EOE(rawrtc_ice_transport_create(
&client->ice_transport, client->gatherer,
default_ice_transport_state_change_handler,
default_ice_transport_candidate_pair_change_handler, client));
// Create DTLS transport
EOE(rawrtc_dtls_transport_create(
&client->dtls_transport, client->ice_transport, certificates, ARRAY_SIZE(certificates),
default_dtls_transport_state_change_handler, default_dtls_transport_error_handler,
client));
// Create SCTP transport
EOE(rawrtc_sctp_transport_create(
&client->sctp_transport, client->dtls_transport,
client->local_parameters.sctp_parameters.port,
data_channel_handler, default_sctp_transport_state_change_handler, client));
// Get data transport
EOE(rawrtc_sctp_transport_get_data_transport(
&client->data_transport, client->sctp_transport));
}
static void client_start_gathering(
struct terminal_client* const client
) {
// Start gathering
EOE(rawrtc_ice_gatherer_gather(client->gatherer, NULL));
}
static void client_start_transports(
struct terminal_client* const client
) {
struct parameters* const remote_parameters = &client->remote_parameters;
DEBUG_INFO("(%s) Starting transports\n", client->name);
// Start ICE transport
EOE(rawrtc_ice_transport_start(
client->ice_transport, client->gatherer, remote_parameters->ice_parameters,
client->role));
// Start DTLS transport
EOE(rawrtc_dtls_transport_start(
client->dtls_transport, remote_parameters->dtls_parameters));
// Start SCTP transport
EOE(rawrtc_sctp_transport_start(
client->sctp_transport, remote_parameters->sctp_parameters.capabilities,
remote_parameters->sctp_parameters.port));
}
static void parameters_destroy(
struct parameters* const parameters
) {
// Un-reference
parameters->ice_parameters = mem_deref(parameters->ice_parameters);
parameters->ice_candidates = mem_deref(parameters->ice_candidates);
parameters->dtls_parameters = mem_deref(parameters->dtls_parameters);
if (parameters->sctp_parameters.capabilities) {
parameters->sctp_parameters.capabilities =
mem_deref(parameters->sctp_parameters.capabilities);
}
}
static void client_stop(
struct terminal_client* const client
) {
DEBUG_INFO("(%s) Stopping transports\n", client->name);
// Clear data channels
list_flush(&client->data_channels);
// Stop all transports & gatherer
EOE(rawrtc_sctp_transport_stop(client->sctp_transport));
EOE(rawrtc_dtls_transport_stop(client->dtls_transport));
EOE(rawrtc_ice_transport_stop(client->ice_transport));
EOE(rawrtc_ice_gatherer_close(client->gatherer));
// Close WS connection
if (client->ws_connection) {
EOR(websock_close(client->ws_connection, WEBSOCK_GOING_AWAY, NULL));
}
// Stop listening on STDIN
fd_close(STDIN_FILENO);
// Un-reference & close
parameters_destroy(&client->remote_parameters);
parameters_destroy(&client->local_parameters);
client->ws_connection = mem_deref(client->ws_connection);
client->data_transport = mem_deref(client->data_transport);
client->sctp_transport = mem_deref(client->sctp_transport);
client->dtls_transport = mem_deref(client->dtls_transport);
client->ice_transport = mem_deref(client->ice_transport);
client->gatherer = mem_deref(client->gatherer);
client->certificate = mem_deref(client->certificate);
client->ws_socket = mem_deref(client->ws_socket);
client->http_client = mem_deref(client->http_client);
client->dns_client = mem_deref(client->dns_client);
client->gather_options = mem_deref(client->gather_options);
client->ws_uri = mem_deref(client->ws_uri);
client->shell = mem_deref(client->shell);
}
static void client_apply_parameters(
struct terminal_client* const client
) {
struct parameters* const remote_parameters = &client->remote_parameters;
DEBUG_INFO("(%s) Applying remote parameters\n", client->name);
// Set remote ICE candidates
EOE(rawrtc_ice_transport_set_remote_candidates(
client->ice_transport, remote_parameters->ice_candidates->candidates,
remote_parameters->ice_candidates->n_candidates));
}
static enum rawrtc_code client_decode_parameters(
struct parameters* const parametersp,
struct odict* const dict,
struct terminal_client* const client
) {
enum rawrtc_code error = RAWRTC_CODE_SUCCESS;
struct odict* node;
struct parameters parameters = {0};
// Decode nodes
error |= dict_get_entry(&node, dict, "iceParameters", ODICT_OBJECT, true);
error |= get_ice_parameters(¶meters.ice_parameters, node);
error |= dict_get_entry(&node, dict, "iceCandidates", ODICT_ARRAY, true);
error |= get_ice_candidates(¶meters.ice_candidates, node, (struct client* const) client);
error |= dict_get_entry(&node, dict, "dtlsParameters", ODICT_OBJECT, true);
error |= get_dtls_parameters(¶meters.dtls_parameters, node);
error |= dict_get_entry(&node, dict, "sctpParameters", ODICT_OBJECT, true);
error |= get_sctp_parameters(¶meters.sctp_parameters, node);
// Ok?
if (error) {
DEBUG_WARNING("(%s) Invalid remote parameters\n", client->name);
goto out;
}
out:
if (error) {
// Un-reference
mem_deref(parameters.sctp_parameters.capabilities);
mem_deref(parameters.dtls_parameters);
mem_deref(parameters.ice_candidates);
mem_deref(parameters.ice_parameters);
} else {
// Copy parameters
memcpy(parametersp, ¶meters, sizeof(parameters));
}
return error;
}
static void client_get_parameters(
struct terminal_client* const client
) {
struct parameters* const local_parameters = &client->local_parameters;
// Get local ICE parameters
EOE(rawrtc_ice_gatherer_get_local_parameters(
&local_parameters->ice_parameters, client->gatherer));
// Get local ICE candidates
EOE(rawrtc_ice_gatherer_get_local_candidates(
&local_parameters->ice_candidates, client->gatherer));
// Get local DTLS parameters
EOE(rawrtc_dtls_transport_get_local_parameters(
&local_parameters->dtls_parameters, client->dtls_transport));
// Get local SCTP parameters
EOE(rawrtc_sctp_transport_get_capabilities(
&local_parameters->sctp_parameters.capabilities));
EOE(rawrtc_sctp_transport_get_port(
&local_parameters->sctp_parameters.port, client->sctp_transport));
}
static struct odict* client_encode_parameters(
struct terminal_client* const client
) {
struct odict* dict;
struct odict* node;
// Get local parameters
client_get_parameters(client);
// Create dict
EOR(odict_alloc(&dict, 16));
// Create nodes
EOR(odict_alloc(&node, 16));
set_ice_parameters(client->local_parameters.ice_parameters, node);
EOR(odict_entry_add(dict, "iceParameters", ODICT_OBJECT, node));
mem_deref(node);
EOR(odict_alloc(&node, 16));
set_ice_candidates(client->local_parameters.ice_candidates, node);
EOR(odict_entry_add(dict, "iceCandidates", ODICT_ARRAY, node));
mem_deref(node);
EOR(odict_alloc(&node, 16));
set_dtls_parameters(client->local_parameters.dtls_parameters, node);
EOR(odict_entry_add(dict, "dtlsParameters", ODICT_OBJECT, node));
mem_deref(node);
EOR(odict_alloc(&node, 16));
set_sctp_parameters(client->sctp_transport, &client->local_parameters.sctp_parameters, node);
EOR(odict_entry_add(dict, "sctpParameters", ODICT_OBJECT, node));
mem_deref(node);
// Done
return dict;
}
static void exit_with_usage(char* program) {
DEBUG_WARNING("Usage: %s <0|1 (ice-role)> [<ws-uri>] [<shell>] [<sctp-port>] "
"[<ice-candidate-type> ...]", program);
exit(1);
}
int main(int argc, char* argv[argc + 1]) {
char** ice_candidate_types = NULL;
size_t n_ice_candidate_types = 0;
enum rawrtc_ice_role role;
struct rawrtc_ice_gather_options* gather_options;
char* const stun_google_com_urls[] = {"stun:stun.l.google.com:19302",
"stun:stun1.l.google.com:19302"};
char* const turn_threema_ch_urls[] = {"turn:turn.threema.ch:443"};
struct terminal_client client = {0};
(void) client.ice_candidate_types; (void) client.n_ice_candidate_types;
// Initialise
EOE(rawrtc_init(true));
// Debug
dbg_init(DBG_DEBUG, DBG_ALL);
DEBUG_PRINTF("Init\n");
// Check arguments length
if (argc < 2) {
exit_with_usage(argv[0]);
}
// Get ICE role
if (get_ice_role(&role, argv[1])) {
exit_with_usage(argv[0]);
}
// Get WS URI (optional)
if (argc >= 3 && re_regex(argv[2], strlen(argv[2]), ws_uri_regex, NULL) == 0) {
EOE(rawrtc_sdprintf(&client.ws_uri, argv[2]));
DEBUG_PRINTF("Using mode: WebSocket\n");
} else {
DEBUG_PRINTF("Using mode: Copy & Paste\n");
}
// Get shell (optional)
if (argc >= 4) {
EOE(rawrtc_sdprintf(&client.shell, argv[3]));
} else {
EOE(rawrtc_sdprintf(&client.shell, "bash"));
}
DEBUG_PRINTF("Using process: %s\n", client.shell);
// Get SCTP port (optional)
if (argc >= 5 && !str_to_uint16(&client.local_parameters.sctp_parameters.port, argv[4])) {
exit_with_usage(argv[0]);
}
// Get enabled ICE candidate types to be added (optional)
if (argc >= 6) {
ice_candidate_types = &argv[5];
n_ice_candidate_types = (size_t) argc - 5;
}
// Create ICE gather options
EOE(rawrtc_ice_gather_options_create(&gather_options, RAWRTC_ICE_GATHER_POLICY_ALL));
// Add ICE servers to ICE gather options
EOE(rawrtc_ice_gather_options_add_server(
gather_options, stun_google_com_urls, ARRAY_SIZE(stun_google_com_urls),
NULL, NULL, RAWRTC_ICE_CREDENTIAL_TYPE_NONE));
EOE(rawrtc_ice_gather_options_add_server(
gather_options, turn_threema_ch_urls, ARRAY_SIZE(turn_threema_ch_urls),
"threema-angular", "Uv0LcCq3kyx6EiRwQW5jVigkhzbp70CjN2CJqzmRxG3UGIdJHSJV6tpo7Gj7YnGB",
RAWRTC_ICE_CREDENTIAL_TYPE_PASSWORD));
// Set client fields
client.name = "A";
client.ice_candidate_types = ice_candidate_types;
client.n_ice_candidate_types = n_ice_candidate_types;
client.gather_options = gather_options;
client.role = role;
list_init(&client.data_channels);
// Setup client
client_init(&client);
// Start gathering
client_start_gathering(&client);
// Listen on stdin
EOR(fd_listen(STDIN_FILENO, FD_READ, stdin_receive_handler, &client));
// Start main loop
// TODO: Wrap re_main?
EOR(re_main(default_signal_handler));
// Stop client & bye
client_stop(&client);
before_exit();
return 0;
}