-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathcontrol_socket.cpp
More file actions
1106 lines (967 loc) · 45 KB
/
control_socket.cpp
File metadata and controls
1106 lines (967 loc) · 45 KB
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file control_socket.cpp
* @author Martin Pulec <pulec@cesnet.cz>
*/
/*
* Copyright (c) 2013-2026 CESNET, zájmové sdružení právnických osob
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "control_socket.h"
#include <cassert> // for assert
#include <cctype> // for isspace, isdigit
#include <cerrno> // for errno, EAFNOSUPPORT
#include <condition_variable>
#include <cstdio> // for snprintf, perror
#include <cstdlib> // for atoi, free, malloc, abort, strtoll
#include <cstring> // for strlen, NULL, strncpy, strchr, strcmp
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <fcntl.h> // for fcntl, F_SETFL, O_NONBLOCK
#include <sys/types.h> // for ssize_t
#include <unistd.h> // for write
#endif
#include <cstdint> // for uint32_t
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include "debug.h"
#include "compat/net.h" // for net related
#include "compat/platform_pipe.h"
#include "compat/strings.h" // for strncasecmp, strcasecmp
#include "compat/time.h" // for timeval, gettimeofday
#include "host.h"
#include "messaging.h"
#include "module.h"
#include "rtp/net_udp.h" // socket_error
#include "types.h" // for tx_media_type
#include "utils/color_out.h" // for TBOLD, color_printf
#include "utils/net.h"
#include "utils/macros.h" // for MODULE_MAGIC
#include "utils/thread.h"
#define MAX_CLIENTS 16
#define MOD_NAME "[control_socket] "
#define MODULE_MAGIC to_fourcc('c', 't', 'r', 'l')
#ifdef _WIN32
typedef const char *sso_val_type;
#else
typedef void *sso_val_type;
#endif // defined _WIN32
using namespace std;
struct client {
fd_t fd;
char buff[2048];
int buff_len;
struct client *prev;
struct client *next;
};
enum connection_type {
SERVER,
CLIENT
};
#define MAX_STAT_EVENT_QUEUE 100
struct control_state {
const uint32_t magic = MODULE_MAGIC;
struct module mod;
thread control_thread_id;
/// @var internal_fd is used for internal communication
fd_t internal_fd[2];
int network_port;
struct module *root_module;
std::mutex stats_lock;
enum connection_type connection_type;
fd_t socket_fd = INVALID_SOCKET;
bool started;
thread stat_event_thread_id;
condition_variable stat_event_cv;
queue<string> stat_event_queue;
bool stats_on;
int audio_channel_report_count = 16;
};
#define CONTROL_EXIT -1
#define CONTROL_CLOSE_HANDLE -2
static bool parse_msg(char *buffer, int buffer_len, /* out */ char *message, int *new_buffer_len);
static int process_msg(struct control_state *s, fd_t client_fd, char *message, struct client *clients);
static ssize_t write_all(fd_t fd, const void *buf, size_t count);
static void * control_thread(void *args);
static void * stat_event_thread(void *args);
static void send_response(fd_t fd, struct response *resp);
static void print_control_help();
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
static ssize_t write_all(fd_t fd, const void *buf, size_t count)
{
const char *p = (const char *) buf;
size_t rest = count;
ssize_t w = 0;
while (rest > 0 && ((fd > 2 && (w = send(fd, p, rest, MSG_NOSIGNAL)) > 0) ||
(w = write(fd, p, rest)) > 0)) {
p += w;
rest -= w;
}
if (rest > 0)
return w;
else
return count;
}
static void new_message(struct module *m) {
control_state *s = (control_state *) m->priv_data;
if(s->started) {
// just wake up from select
int ret = write_all(s->internal_fd[1], "noop\r\n", 6);
if (ret <= 0) {
log_msg(LOG_LEVEL_ERROR, "[control] Cannot write to pipe!\n");
}
}
}
ADD_TO_PARAM("control-report-audio-ch-count", "* control-report-audio-ch-count=<count>\n"
" The number of channels reported over control port.\n");
int control_init(int port, int connection_type, struct control_state **state, struct module *root_module, int force_ip_version)
{
control_state *s = new control_state();
s->root_module = root_module;
s->started = false;
module_init_default(&s->mod);
s->mod.cls = MODULE_CLASS_CONTROL;
s->mod.new_message = new_message;
s->mod.priv_data = s;
if(connection_type == 0) {
s->network_port = port;
s->connection_type = SERVER;
} else {
s->network_port = port;
s->connection_type = CLIENT;
}
if(s->connection_type == SERVER) {
int ip_version = 6;
if (force_ip_version != 4) {
s->socket_fd = socket(AF_INET6, SOCK_STREAM, 0);
if (s->socket_fd == INVALID_SOCKET) {
socket_error("Control socket - IPv6 not available");
}
}
if (force_ip_version == 4 || (force_ip_version == 0 && s->socket_fd == INVALID_SOCKET && errno == EAFNOSUPPORT)) { // try IPv4
ip_version = 4;
s->socket_fd = socket(AF_INET, SOCK_STREAM, 0);
}
if (s->socket_fd == INVALID_SOCKET) {
socket_error("Control socket - socket");
goto error;
}
int val = 1;
int rc;
rc = setsockopt(s->socket_fd, SOL_SOCKET, SO_REUSEADDR,
(sso_val_type) &val, sizeof(val));
if (rc != 0) {
socket_error("Control socket - setsockopt");
}
int ipv6only = 0;
if (ip_version == 6 && setsockopt(s->socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only,
sizeof(ipv6only)) != 0) {
socket_error("setsockopt IPV6_V6ONLY");
}
/* setting address to in6addr_any allows connections to be established
* from both IPv4 and IPv6 hosts. This behavior can be modified
* using the IPPROTO_IPV6 level socket option IPV6_V6ONLY if required.*/
struct sockaddr_storage ss{};
socklen_t s_len = 0;
if (ip_version == 4) {
auto *s_in = reinterpret_cast<struct sockaddr_in *>(&ss);
s_in->sin_family = AF_INET;
s_in->sin_addr.s_addr = htonl(INADDR_ANY);
s_in->sin_port = htons(s->network_port);
s_len = sizeof *s_in;
} else {
auto *s_in6 = reinterpret_cast<struct sockaddr_in6 *>(&ss);
s_in6->sin6_family = AF_INET6;
s_in6->sin6_addr = in6addr_any;
s_in6->sin6_port = htons(s->network_port);
s_len = sizeof *s_in6;
}
rc = ::bind(s->socket_fd, reinterpret_cast<const struct sockaddr *>(&ss), s_len);
if (rc != 0) {
socket_error("Control socket - bind");
goto error;
} else {
rc = listen(s->socket_fd, MAX_CLIENTS);
if (rc != 0) {
socket_error("Control socket - listen");
goto error;
}
}
} else {
if (force_ip_version == 6) {
log_msg(LOG_LEVEL_ERROR, "Control socket: IPv6 unimplemented in client mode!\n");
goto error;
}
s->socket_fd = socket(AF_INET, SOCK_STREAM, 0);
assert(s->socket_fd != INVALID_SOCKET);
struct addrinfo hints, *res, *res0;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
char port_str[10];
snprintf(port_str, 10, "%d", s->network_port);
err = getaddrinfo("127.0.0.1", port_str, &hints, &res0);
if(err) {
MSG(ERROR, "Unable to get address: %s\n", gai_strerror(err));
goto error;
}
bool connected = false;
for (res = res0; res; res = res->ai_next) {
if(connect(s->socket_fd, res->ai_addr, res->ai_addrlen) == -1) {
continue;
}
connected = true;
break; /* okay we got one */
}
freeaddrinfo(res0);
if(!connected) {
MSG(ERROR, "Unable to connect to localhost:%d\n", s->network_port);
goto error;
}
}
module_register(&s->mod, root_module);
if (const char *val = get_commandline_param("control-report-audio-ch-count")) {
s->audio_channel_report_count = strtoll(val, NULL, 0);
}
*state = s;
return 0;
error:
if (s->socket_fd != INVALID_SOCKET) {
CLOSESOCKET(s->socket_fd);
}
delete s;
return -1;
}
void control_start(struct control_state *s)
{
if (s == NULL) {
return;
}
platform_pipe_init(s->internal_fd);
s->control_thread_id = thread(control_thread, s);
s->stat_event_thread_id = thread(stat_event_thread, s);
log_msg(LOG_LEVEL_NOTICE, "Control socket listening on port %d\n",
socket_get_recv_port(s->socket_fd));
s->started = true;
}
#define prefix_matches(x,y) strncasecmp(x, y, strlen(y)) == 0
#define suffix(x,y) x + strlen(y)
#define is_internal_port(x) (x == s->internal_fd[0])
static struct response *
process_audio_message(struct module *root_module, const char *cmd)
{
char path[STR_LEN] = "";
if (strcmp(cmd, "mute") == 0 || strstr(cmd, "-receiver") != nullptr) {
strncpy(path, "audio.receiver", sizeof path);
auto *msg = (struct msg_receiver *) new_message(
sizeof(struct msg_receiver));
if (strcmp(cmd, "mute") == 0) {
msg->type = RECEIVER_MSG_MUTE_TOGGLE;
} else if (prefix_matches(cmd, "mute-")) {
msg->type = RECEIVER_MSG_MUTE;
} else if (prefix_matches(cmd, "unmute-")) {
msg->type = RECEIVER_MSG_UNMUTE;
} else {
free_message((struct message *) msg, nullptr);
return new_response(RESPONSE_BAD_REQUEST,
"malformed audio recv mute msg");
}
return send_message(root_module, path, (struct message *) msg);
}
if (strstr(cmd, "-sender") != nullptr) {
strncpy(path, "audio.sender", sizeof path);
auto *msg = (struct msg_sender *) new_message(
sizeof(struct msg_sender));
if (prefix_matches(cmd, "mute-")) {
msg->type = SENDER_MSG_MUTE;
} else if (prefix_matches(cmd, "unmute-")) {
msg->type = SENDER_MSG_UNMUTE;
} else {
free_message((struct message *) msg, nullptr);
return new_response(RESPONSE_BAD_REQUEST,
"malformed audio send mute msg");
}
return send_message(root_module, path, (struct message *) msg);
}
if (prefix_matches(cmd, "volume ")) {
strncpy(path, "audio.receiver", sizeof path);
auto *msg = (struct msg_receiver *) new_message(
sizeof(struct msg_receiver));
const char *dir = suffix(cmd, "volume ");
if (strcmp(dir, "up") == 0) {
msg->type = RECEIVER_MSG_INCREASE_VOLUME;
} else if (strcmp(dir, "down") == 0) {
msg->type = RECEIVER_MSG_DECREASE_VOLUME;
} else {
free_message((struct message *) msg, nullptr);
return new_response(RESPONSE_BAD_REQUEST, nullptr);
}
return send_message(root_module, path, (struct message *) msg);
}
return new_response(RESPONSE_BAD_REQUEST, "unexpected audio msg");
}
/**
* @retval -1 exit thread
* @retval -2 close handle
*/
static int process_msg(struct control_state *s, fd_t client_fd, char *message, struct client *clients)
{
int ret = 0;
struct response *resp = NULL;
char path[1024] = ""; // path for msg receiver (usually video)
char path_audio[1024] = ""; // auxiliary buffer used when we need to signalize both audio
// and video
char buf[1048];
if(prefix_matches(message, "port ")) {
message = suffix(message, "port ");
if (isdigit(message[0])) { // index is a number
snprintf(path, 1024, "%s[%d]", module_class_name(MODULE_CLASS_PORT), atoi(message));
while(isdigit(*message))
message++;
while(isspace(*message))
message++;
} else { // index is a name
char *port_id_name = message;
while (!isspace(message[0]) && message[0] != '\0')
message++;
if (message[0] != '\0') {
message[0] = '\0';
message++;
}
snprintf(path, 1024, "%s[%s]", module_class_name(MODULE_CLASS_PORT), port_id_name);
while (isspace(message[0]) && message[0] != '\0')
message++;
}
/* "port n compress" messages get forwarded to
* port[n].sender.compress, but that is wrong, because frames
* are now compressed before they are passed to the sender
* (sender compression is now always set to "none").
*/
if (prefix_matches(message, "compress ")){
log_msg(LOG_LEVEL_ERROR, "\"port n compress\" was deprecated. Use \"port[n] compress\" instead\n");
return ret;
}
}
if(strcasecmp(message, "quit") == 0) {
return CONTROL_EXIT;
} else if (strcasecmp(message, "exit") == 0) {
exit_uv(0);
resp = new_response(RESPONSE_OK, NULL);
} else if (strcasecmp(message, "help") == 0) {
print_control_help();
return ret;
} else if (strcasecmp(message, "noop") == 0) {
return ret;
} else if (prefix_matches(message, "stats ") || prefix_matches(message, "event ")) {
if (is_internal_port(client_fd)) {
struct client *cur = clients;
char *new_msg = NULL;
while (cur) {
if(is_internal_port(cur->fd)) { // skip local FD
cur = cur->next;
continue;
}
if (!new_msg) {
// append <CR><LF> again
new_msg = (char *) malloc(strlen(message) + 1 + 2);
strcpy(new_msg, message);
new_msg[strlen(message)] = '\r';
new_msg[strlen(message) + 1] = '\n';
new_msg[strlen(message) + 2] = '\0';
}
int ret = write_all(cur->fd, new_msg, strlen(new_msg));
if (ret != (int) strlen(new_msg)) {
log_msg(LOG_LEVEL_WARNING, "Cannot write stats/event!\n");
}
cur = cur->next;
}
free(new_msg);
return ret;
} else if (prefix_matches(message, "stats ")) {
const char *toggle = suffix(message, "stats ");
if (strcasecmp(toggle, "on") == 0) {
s->stats_on = true;
resp = new_response(RESPONSE_OK, NULL);
} else if (strcasecmp(toggle, "off") == 0) {
s->stats_on = false;
resp = new_response(RESPONSE_OK, NULL);
} else {
resp = new_response(RESPONSE_BAD_REQUEST, NULL);
}
}
} else if (prefix_matches(message, "sender-port ")) {
struct msg_sender *msg = (struct msg_sender *)
new_message(sizeof(struct msg_sender));
struct msg_sender *msg_audio = (struct msg_sender *)
new_message(sizeof(struct msg_sender));
char *port_str = suffix(message, "sender-port ");
msg->type = SENDER_MSG_CHANGE_PORT;
msg_audio->type = SENDER_MSG_CHANGE_PORT;
if (strchr(port_str, ':')) {
char *save_ptr, *item;
item = strtok_r(port_str, ":", &save_ptr);
msg->rx_port = atoi(item);
item = strtok_r(NULL, ":", &save_ptr);
msg->tx_port = atoi(item);
item = strtok_r(NULL, ":", &save_ptr);
if (item) {
char *item2 = strtok_r(NULL, ":", &save_ptr);
if (item2) { // change only if both RX and TX given
msg_audio->rx_port = atoi(item);
msg_audio->tx_port = atoi(item2);
} else {
log_msg(LOG_LEVEL_ERROR, "Not changing audio port!\n");
}
}
} else {
msg->tx_port = atoi(port_str);
}
if (msg_audio->rx_port == 0) {
msg_audio->rx_port = msg->rx_port ? msg->rx_port + 2 : 0;
}
if (msg_audio->tx_port == 0) {
msg_audio->tx_port = msg->tx_port + 2;
}
enum module_class path_sender[] = { MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
enum module_class path_sender_audio[] = { MODULE_CLASS_AUDIO, MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
memcpy(path_audio, path, sizeof(path_audio));
append_message_path(path, sizeof(path), path_sender);
append_message_path(path_audio, sizeof(path_audio), path_sender_audio);
resp =
send_message(s->root_module, path, (struct message *) msg);
struct response *resp_audio =
send_message(s->root_module, path_audio, (struct message *) msg_audio);
free_response(resp_audio);
} else if(prefix_matches(message, "receiver ") || prefix_matches(message, "play") ||
prefix_matches(message, "pause") || prefix_matches(message, "reset-ssrc")) {
struct msg_sender *msg =
(struct msg_sender *)
new_message(sizeof(struct msg_sender));
if(prefix_matches(message, "receiver ")) {
strncpy(msg->receiver, suffix(message, "receiver "), sizeof(msg->receiver) - 1);
msg->type = SENDER_MSG_CHANGE_RECEIVER;
} else if(prefix_matches(message, "reset-ssrc")) {
msg->type = SENDER_MSG_RESET_SSRC;
} else {
abort();
}
struct msg_sender *msg_audio = (struct msg_sender *) malloc(sizeof(struct msg_sender));
memcpy(msg_audio, msg, sizeof(struct msg_sender));
enum module_class path_sender[] = { MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
enum module_class path_sender_audio[] = { MODULE_CLASS_AUDIO, MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
memcpy(path_audio, path, sizeof(path_audio));
append_message_path(path, sizeof(path), path_sender);
append_message_path(path_audio, sizeof(path_audio), path_sender_audio);
resp =
send_message(s->root_module, path, (struct message *) msg);
struct response *resp_audio =
send_message(s->root_module, path_audio, (struct message *) msg_audio);
free_response(resp_audio);
} else if (prefix_matches(message, "receiver-port ")) {
struct msg_receiver *msg =
(struct msg_receiver *)
new_message(sizeof(struct msg_receiver));
struct msg_receiver *msg_audio =
(struct msg_receiver *)
new_message(sizeof(struct msg_receiver));
msg->type = RECEIVER_MSG_CHANGE_RX_PORT;
msg_audio->type = RECEIVER_MSG_CHANGE_RX_PORT;
char *port_str = suffix(message, "receiver-port ");
msg->new_rx_port = atoi(port_str);
if (strchr(port_str, ':')) {
msg_audio->new_rx_port = atoi(strchr(port_str, ':') + 1);
} else {
msg_audio->new_rx_port = msg->new_rx_port + 2;
}
enum module_class path_receiver[] = { MODULE_CLASS_RECEIVER, MODULE_CLASS_NONE };
enum module_class path_audio_receiver[] = { MODULE_CLASS_AUDIO, MODULE_CLASS_RECEIVER, MODULE_CLASS_NONE };
append_message_path(path, sizeof(path), path_receiver);
append_message_path(path_audio, sizeof(path_audio), path_audio_receiver);
resp =
send_message(s->root_module, path, (struct message *) msg);
struct response *resp_audio =
send_message(s->root_module, path_audio, (struct message *) msg_audio);
free_response(resp_audio);
} else if(prefix_matches(message, "fec ")) {
auto *msg = reinterpret_cast<struct msg_universal *>(new_message(sizeof(struct msg_universal)));
char *fec = suffix(message, "fec ");
enum tx_media_type media_type{};
strncpy(msg->text, MSG_UNIVERSAL_TAG_TX "fec ", sizeof(msg->text) - 1);
if(strncasecmp(fec, "audio ", 6) == 0) {
media_type = TX_MEDIA_AUDIO;
strncat(msg->text, fec + 6, sizeof(msg->text) - strlen(msg->text) - 1);
} else if(strncasecmp(fec, "video ", 6) == 0) {
media_type = TX_MEDIA_VIDEO;
strncat(msg->text, fec + 6, sizeof(msg->text) - strlen(msg->text) - 1);
} else {
resp = new_response(RESPONSE_NOT_FOUND, "unknown media type");
free(msg);
msg = nullptr;
}
if (msg) {
if (media_type == TX_MEDIA_VIDEO) {
enum module_class path_tx[] = { MODULE_CLASS_SENDER, MODULE_CLASS_TX, MODULE_CLASS_NONE };
append_message_path(path, sizeof(path),
path_tx);
} else {
enum module_class path_audio_tx[] = { MODULE_CLASS_AUDIO, MODULE_CLASS_SENDER, MODULE_CLASS_TX, MODULE_CLASS_NONE };
append_message_path(path, sizeof(path),
path_audio_tx);
}
resp = send_message(s->root_module, path, (struct message *) msg);
}
} else if(prefix_matches(message, "compress ")) {
struct msg_change_compress_data *msg =
(struct msg_change_compress_data *)
new_message(sizeof(struct msg_change_compress_data));
char *compress = suffix(message, "compress ");
if(prefix_matches(compress, "param ")) {
compress = suffix(compress, "param ");
msg->what = CHANGE_PARAMS;
} else {
msg->what = CHANGE_COMPRESS;
}
strncpy(msg->config_string, compress, sizeof(msg->config_string) - 1);
if(!resp) {
enum module_class path_compress[] = { MODULE_CLASS_SENDER, MODULE_CLASS_COMPRESS, MODULE_CLASS_NONE };
append_message_path(path, sizeof(path), path_compress);
resp = send_message(s->root_module, path, (struct message *) msg);
}
} else if (prefix_matches(message, "volume ") ||
prefix_matches(message, "mute") ||
prefix_matches(message, "unmute")) {
resp = process_audio_message(s->root_module, message);
} else if (prefix_matches(message, "av-delay ")) {
int val = atoi(suffix(message, "av-delay "));
set_audio_delay(val);
resp = new_response(RESPONSE_OK, NULL);
} else if (prefix_matches(message, "postprocess ")) {
strncpy(path, "display", sizeof path);
struct msg_universal *msg = (struct msg_universal *) new_message(sizeof(struct msg_universal));
strncpy(msg->text, message, sizeof msg->text - 1);
msg->text[sizeof msg->text - 1] = '\0';
resp = send_message(s->root_module, path, (struct message *) msg);
} else if(strcasecmp(message, "bye") == 0) {
ret = CONTROL_CLOSE_HANDLE;
resp = new_response(RESPONSE_OK, NULL);
} else if(strcmp(message, "dump-tree") == 0) {
dump_tree(s->root_module, 0);
resp = new_response(RESPONSE_OK, NULL);
} else { // assume message in format "path message"
struct msg_universal *msg = (struct msg_universal *)
new_message(sizeof(struct msg_universal));
if (strchr(message, ' ')) {
size_t path_len = strchr(message, ' ') - message;
memcpy(path, message, path_len);
path[path_len] = '\0';
strncpy(msg->text, strchr(message, ' ') + 1, sizeof(msg->text) - 1);
// If path is "root", send directly to root module
if (strcmp(path, "root") == 0) {
path[0] = '\0';
}
} else {
path[0] = '\0';
strncpy(msg->text, message, sizeof(msg->text) - 1);
}
resp = send_message_sync(s->root_module, path, (struct message *) msg, 100, 0);
}
if(!resp) {
MSG(ERROR, "No response received!\n");
snprintf(buf, sizeof(buf), "(unknown path: %s)", path);
resp = new_response(RESPONSE_INT_SERV_ERR, buf);
}
send_response(client_fd, resp);
return ret;
}
static void send_response(fd_t fd, struct response *resp)
{
char buffer[1024];
snprintf(buffer, sizeof(buffer) - 2, "%d %s", response_get_status(resp),
response_status_to_text(response_get_status(resp)));
if (response_get_text(resp)) {
strncat(buffer, " ", sizeof(buffer) - strlen(buffer) - 1 - 2);
strncat(buffer, response_get_text(resp), sizeof(buffer) - strlen(buffer) - 1 - 2);
}
strcat(buffer, "\r\n");
ssize_t ret = write_all(fd, buffer, strlen(buffer));
if (ret < 0) {
socket_error("Unable to write response");
}
#ifndef _WIN32
// Force flush on POSIX systems
fsync(fd);
#endif
free_response(resp);
}
static bool parse_msg(char *buffer, int buffer_len, /* out */ char *message, int *new_buffer_len)
{
bool ret = false;
int i = 0;
while (i < buffer_len) {
if(buffer[i] == '\0' || buffer[i] == '\n' || buffer[i] == '\r') {
++i;
} else {
break;
}
}
int start = i;
for( ; i < buffer_len; ++i) {
if(buffer[i] == '\0' || buffer[i] == '\n' || buffer[i] == '\r') {
memcpy(message, buffer + start, i - start);
message[i - start] = '\0';
ret = true;
break;
}
}
if(ret) {
memmove(buffer, buffer + i, buffer_len - i);
*new_buffer_len = buffer_len - i;
}
return ret;
}
/**
* prepends itself at the head of the list
*/
static struct client *add_client(struct client *clients, fd_t fd) {
struct client *new_client = (struct client *)
malloc(sizeof(struct client));
new_client->fd = fd;
new_client->prev = NULL;
new_client->next = clients;
if (new_client->next) {
new_client->next->prev = new_client;
}
new_client->buff_len = 0;
return new_client;
}
static void process_messages(struct control_state *s)
{
struct message *msg;
while((msg = check_message(&s->mod))) {
struct msg_universal *m = (struct msg_universal *) msg;
if (strcmp(m->text, "get_port") == 0) {
struct response *r;
uint16_t port = socket_get_recv_port(s->socket_fd);
if (port) {
char port_str[6];
snprintf(port_str, sizeof port_str, "%hu", port);
r = new_response(RESPONSE_OK, port_str);
} else {
r = new_response(RESPONSE_INT_SERV_ERR, "get_recv_port");
}
free_message(msg, r);
} else if (strstr(m->text, "execute ") == m->text) {
process_msg(s, 1, m->text + strlen("execute "), nullptr);
free_message(msg, new_response(RESPONSE_OK, nullptr));
} else {
log_msg(LOG_LEVEL_WARNING, "[control] Unrecognized command: %s\n", m->text);
free_message(msg, new_response(RESPONSE_NOT_IMPL, NULL));
continue;
}
}
}
static void set_socket_nonblock(fd_t fd) {
#ifdef _WIN32
unsigned long ul;
ioctlsocket(fd, FIONBIO, &ul);
#else
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
}
#endif
}
ADD_TO_PARAM("control-accept-global", "* control-accept-global\n"
" Open control socket to public network.\n");
static void * control_thread(void *args)
{
set_thread_name(__func__);
struct control_state *s = (struct control_state *) args;
struct client *clients = NULL;
assert(s->socket_fd != INVALID_SOCKET);
assert(s->internal_fd[0] != INVALID_SOCKET);
if(s->connection_type == CLIENT) {
clients = add_client(clients, s->socket_fd);
}
struct sockaddr_storage client_addr;
socklen_t len = sizeof client_addr;
errno = 0;
clients = add_client(clients, s->internal_fd[0]);
bool should_exit = false;
struct timeval last_report_sent;
const int report_interval_sec = 5;
gettimeofday(&last_report_sent, NULL);
while(!should_exit) {
process_messages(s);
fd_t max_fd = 0;
fd_set fds;
FD_ZERO(&fds);
if (s->connection_type == SERVER) {
FD_SET(s->socket_fd, &fds);
max_fd = s->socket_fd + 1;
}
struct client *cur = clients;
while(cur) {
FD_SET(cur->fd, &fds);
if(cur->fd + 1 > max_fd) {
max_fd = cur->fd + 1;
}
cur = cur->next;
}
struct timeval timeout;
timeout.tv_sec = report_interval_sec;
timeout.tv_usec = 0;
struct timeval *timeout_ptr = NULL;
if(clients->next != NULL) { // some remote client
timeout_ptr = &timeout;
}
int rc;
if ((rc = select(max_fd, &fds, NULL, NULL, timeout_ptr)) >= 1) {
if(s->connection_type == SERVER && FD_ISSET(s->socket_fd, &fds)) {
fd_t fd = accept(s->socket_fd, (struct sockaddr *) &client_addr, &len);
if (fd == INVALID_SOCKET) {
socket_error("[control socket] accept");
continue;
}
// all remote sockets are written sequentially so
// we don't want to block if one gets stuck
set_socket_nonblock(fd);
// refuse remote connections by default
if (!get_commandline_param("control-accept-global") &&
!is_addr_loopback((struct sockaddr *) &client_addr)) {
log_msg(LOG_LEVEL_WARNING, "[control socket] Refusing remote connection. Use \"--param control-accept-global\" to allow UG control from a remote host.\n");
const char *msg = "unauthorized\r\n";
write_all(fd, msg, strlen(msg));
CLOSESOCKET(fd);
continue;
}
// add to list
clients = add_client(clients, fd);
}
struct client *cur = clients;
while(cur) {
if(FD_ISSET(cur->fd, &fds)) {
ssize_t ret = PLATFORM_PIPE_READ(cur->fd, cur->buff + cur->buff_len,
sizeof(cur->buff) - cur->buff_len);
if(ret == -1) {
MSG(ERROR, "Error reading socket, closing!!!\n");
}
if(ret <= 0) {
struct client *next;
CLOSESOCKET(cur->fd);
if (cur->prev) {
cur->prev->next = cur->next;
} else {
clients = cur->next;
}
if (cur->next) {
cur->next->prev = cur->prev;
}
next = cur->next;
free(cur);
cur = next;
continue;
}
cur->buff_len += ret;
}
cur = cur->next;
}
} else if (rc < 0) {
socket_error("[control socket] select");
}
cur = clients;
while(cur) {
char msg[sizeof(cur->buff) + 1];
int cur_buffer_len;
while(parse_msg(cur->buff, cur->buff_len, msg, &cur_buffer_len)) {
cur->buff_len = cur_buffer_len;
int ret = process_msg(s, cur->fd, msg, clients);
if(ret == CONTROL_EXIT && is_internal_port(cur->fd)) {
should_exit = true;
} else if(ret == CONTROL_CLOSE_HANDLE) {
shutdown(cur->fd, SHUT_RDWR);
}
}
if(cur->buff_len == sizeof(cur->buff)) {
MSG(ERROR, "Socket buffer full and no "
"delimited message. Discarding.\n");
cur->buff_len = 0;
}
cur = cur->next;
}
}
// notify clients about exit
struct client *cur = clients;
while(cur) {
struct client *tmp = cur;
if (!is_internal_port(cur->fd)) {
const char *msg = "event exit\r\n";
write_all(cur->fd, msg, strlen(msg));
}
CLOSESOCKET(cur->fd);
cur = cur->next;
free(tmp);
}
platform_pipe_close(s->internal_fd[0]);
return NULL;
}
static void *stat_event_thread(void *args)
{
set_thread_name(__func__);
struct control_state *s = (struct control_state *) args;
while (1) {
std::unique_lock<std::mutex> lk(s->stats_lock);
s->stat_event_cv.wait(lk, [s] { return s->stat_event_queue.size() > 0; });
string &line = s->stat_event_queue.front();
if (line.empty()) {
break;
}
int ret = write_all(s->internal_fd[1], line.c_str(), line.length());
s->stat_event_queue.pop();
if (ret <= 0) {
MSG(ERROR, "Cannot write stat line!\n");
}
}
return NULL;
}
void control_done(struct control_state *s)
{
if(!s) {
return;
}
assert(s->magic == MODULE_MAGIC);
if(s->started) {
s->stats_lock.lock();
s->stat_event_queue.push({});
s->stats_lock.unlock();
s->stat_event_cv.notify_one();
s->stat_event_thread_id.join();
int ret = write_all(s->internal_fd[1], "quit\r\n", 6);
if (ret > 0) {
s->control_thread_id.join();
platform_pipe_close(s->internal_fd[1]);