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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
|
/*---------------------------------------------------------*\
| NetworkServer.cpp |
| |
| OpenRGB SDK network server |
| |
| Adam Honse (CalcProgrammer1) 09 May 2020 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <cstring>
#include "NetworkServer.h"
#include "LogManager.h"
#ifndef WIN32
#include <sys/ioctl.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <arpa/inet.h>
#else
#include <ws2tcpip.h>
#endif
#include <memory.h>
#include <errno.h>
#include <stdlib.h>
#include <iostream>
const char yes = 1;
#ifdef WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
using namespace std::chrono_literals;
NetworkClientInfo::NetworkClientInfo()
{
client_string = "Client";
client_ip = OPENRGB_SDK_HOST;
client_sock = INVALID_SOCKET;
client_listen_thread = nullptr;
client_protocol_version = 0;
}
NetworkClientInfo::~NetworkClientInfo()
{
if(client_sock != INVALID_SOCKET)
{
LOG_INFO("[NetworkServer] Closing server connection: %s", client_ip.c_str());
delete client_listen_thread;
shutdown(client_sock, SD_RECEIVE);
closesocket(client_sock);
}
}
NetworkServer::NetworkServer(std::vector<RGBController *>& control) : controllers(control)
{
host = OPENRGB_SDK_HOST;
port_num = OPENRGB_SDK_PORT;
server_online = false;
server_listening = false;
legacy_workaround_enabled = false;
for(int i = 0; i < MAXSOCK; i++)
{
ConnectionThread[i] = nullptr;
}
profile_manager = nullptr;
}
NetworkServer::~NetworkServer()
{
StopServer();
}
void NetworkServer::ClientInfoChanged()
{
ClientInfoChangeMutex.lock();
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < ClientInfoChangeCallbacks.size(); callback_idx++)
{
ClientInfoChangeCallbacks[callback_idx](ClientInfoChangeCallbackArgs[callback_idx]);
}
ClientInfoChangeMutex.unlock();
}
void NetworkServer::DeviceListChanged()
{
/*---------------------------------------------------------*\
| Indicate to the clients that the controller list has |
| changed |
\*---------------------------------------------------------*/
for(unsigned int client_idx = 0; client_idx < ServerClients.size(); client_idx++)
{
SendRequest_DeviceListChanged(ServerClients[client_idx]->client_sock);
}
}
void NetworkServer::ServerListeningChanged()
{
ServerListeningChangeMutex.lock();
/*---------------------------------------------------------*\
| Server state has changed, call the callbacks |
\*---------------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < ServerListeningChangeCallbacks.size(); callback_idx++)
{
ServerListeningChangeCallbacks[callback_idx](ServerListeningChangeCallbackArgs[callback_idx]);
}
ServerListeningChangeMutex.unlock();
}
std::string NetworkServer::GetHost()
{
return host;
}
unsigned short NetworkServer::GetPort()
{
return port_num;
}
bool NetworkServer::GetOnline()
{
return server_online;
}
bool NetworkServer::GetListening()
{
return server_listening;
}
unsigned int NetworkServer::GetNumClients()
{
return (unsigned int)ServerClients.size();
}
const char * NetworkServer::GetClientString(unsigned int client_num)
{
const char * result;
ServerClientsMutex.lock();
if(client_num < ServerClients.size())
{
result = ServerClients[client_num]->client_string.c_str();
}
else
{
result = "";
}
ServerClientsMutex.unlock();
return result;
}
const char * NetworkServer::GetClientIP(unsigned int client_num)
{
const char * result;
ServerClientsMutex.lock();
if(client_num < ServerClients.size())
{
result = ServerClients[client_num]->client_ip.c_str();
}
else
{
result = "";
}
ServerClientsMutex.unlock();
return result;
}
unsigned int NetworkServer::GetClientProtocolVersion(unsigned int client_num)
{
unsigned int result;
ServerClientsMutex.lock();
if(client_num < ServerClients.size())
{
result = ServerClients[client_num]->client_protocol_version;
}
else
{
result = 0;
}
ServerClientsMutex.unlock();
return result;
}
void NetworkServer::RegisterClientInfoChangeCallback(NetServerCallback new_callback, void * new_callback_arg)
{
ClientInfoChangeCallbacks.push_back(new_callback);
ClientInfoChangeCallbackArgs.push_back(new_callback_arg);
}
void NetworkServer::RegisterServerListeningChangeCallback(NetServerCallback new_callback, void * new_callback_arg)
{
ServerListeningChangeCallbacks.push_back(new_callback);
ServerListeningChangeCallbackArgs.push_back(new_callback_arg);
}
void NetworkServer::SetHost(std::string new_host)
{
if(server_online == false)
{
host = new_host;
}
}
void NetworkServer::SetLegacyWorkaroundEnable(bool enable)
{
legacy_workaround_enabled = enable;
}
void NetworkServer::SetPort(unsigned short new_port)
{
if(server_online == false)
{
port_num = new_port;
}
}
void NetworkServer::StartServer()
{
int err;
struct addrinfo hints, *res, *result;
/*---------------------------------------------------------*\
| Start a TCP server and launch threads |
\*---------------------------------------------------------*/
char port_str[6];
snprintf(port_str, 6, "%d", port_num);
socket_count = 0;
/*---------------------------------------------------------*\
| Windows requires WSAStartup before using sockets |
\*---------------------------------------------------------*/
#ifdef WIN32
if(WSAStartup(MAKEWORD(2, 2), &wsa) != NO_ERROR)
{
WSACleanup();
return;
}
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
err = getaddrinfo(host.c_str(), port_str, &hints, &result);
if(err)
{
LOG_ERROR("[NetworkServer] Unable to get address.");
WSACleanup();
return;
}
/*---------------------------------------------------------*\
| Create a server socket for each address returned. |
\*---------------------------------------------------------*/
for(res = result; res && socket_count < MAXSOCK; res = res->ai_next)
{
server_sock[socket_count] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(server_sock[socket_count] == INVALID_SOCKET)
{
LOG_ERROR("[NetworkServer] Network socket could not be created.");
WSACleanup();
return;
}
/*---------------------------------------------------------*\
| Bind the server socket |
\*---------------------------------------------------------*/
if(bind(server_sock[socket_count], res->ai_addr, res->ai_addrlen) == SOCKET_ERROR)
{
if(errno == EADDRINUSE)
{
LOG_ERROR("[NetworkServer] Could not bind network socket. Is port %hu already being used?", GetPort());
}
else if(errno == EACCES)
{
LOG_ERROR("[NetworkServer] Could not bind network socket. Access to socket was denied.");
}
else if(errno == EBADF)
{
LOG_ERROR("[NetworkServer] Could not bind network socket. sockfd is not a valid file descriptor.");
}
else if(errno == EINVAL)
{
LOG_ERROR("[NetworkServer] Could not bind network socket. The socket is already bound to an address, or addrlen is wrong, or addr is not a valid address for this socket's domain.");
}
else if(errno == ENOTSOCK)
{
LOG_ERROR("[NetworkServer] Could not bind network socket. The file descriptor sockfd does not refer to a socket.");
}
else
{
/*---------------------------------------------------------*\
| errno could be a Linux specific error, see: |
| https://man7.org/linux/man-pages/man2/bind.2.html |
\*---------------------------------------------------------*/
LOG_ERROR("[NetworkServer] Could not bind network socket. Error code: %d.", errno);
}
WSACleanup();
return;
}
/*---------------------------------------------------------*\
| Set socket options - no delay |
\*---------------------------------------------------------*/
setsockopt(server_sock[socket_count], IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
socket_count += 1;
}
freeaddrinfo(result);
server_online = true;
/*---------------------------------------------------------*\
| Start the connection thread |
\*---------------------------------------------------------*/
for(int curr_socket = 0; curr_socket < socket_count; curr_socket++)
{
ConnectionThread[curr_socket] = new std::thread(&NetworkServer::ConnectionThreadFunction, this, curr_socket);
ConnectionThread[curr_socket]->detach();
}
}
void NetworkServer::StopServer()
{
int curr_socket;
server_online = false;
ServerClientsMutex.lock();
for(unsigned int client_idx = 0; client_idx < ServerClients.size(); client_idx++)
{
delete ServerClients[client_idx];
}
ServerClients.clear();
for(curr_socket = 0; curr_socket < socket_count; curr_socket++)
{
shutdown(server_sock[curr_socket], SD_RECEIVE);
closesocket(server_sock[curr_socket]);
}
ServerClientsMutex.unlock();
for(curr_socket = 0; curr_socket < socket_count; curr_socket++)
{
if(ConnectionThread[curr_socket])
{
delete ConnectionThread[curr_socket];
ConnectionThread[curr_socket] = nullptr;
}
}
socket_count = 0;
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
ClientInfoChanged();
}
void NetworkServer::ConnectionThreadFunction(int socket_idx)
{
/*---------------------------------------------------------*\
| This thread handles client connections |
\*---------------------------------------------------------*/
LOG_INFO("[NetworkServer] Network connection thread started on port %hu", GetPort());
while(server_online == true)
{
/*---------------------------------------------------------*\
| Create new socket for client connection |
\*---------------------------------------------------------*/
NetworkClientInfo * client_info = new NetworkClientInfo();
/*---------------------------------------------------------*\
| Listen for incoming client connection on the server |
| socket. This call blocks until a connection is |
| established |
\*---------------------------------------------------------*/
if(listen(server_sock[socket_idx], 10) < 0)
{
LOG_INFO("[NetworkServer] Connection thread closed");
server_online = false;
return;
}
server_listening = true;
ServerListeningChanged();
/*---------------------------------------------------------*\
| Accept the client connection |
\*---------------------------------------------------------*/
client_info->client_sock = accept_select((int)server_sock[socket_idx]);
if(client_info->client_sock < 0)
{
LOG_INFO("[NetworkServer] Connection thread closed");
server_online = false;
server_listening = false;
ServerListeningChanged();
return;
}
/*---------------------------------------------------------*\
| Get the new client socket and store it in the clients |
| vector |
\*---------------------------------------------------------*/
u_long arg = 0;
ioctlsocket(client_info->client_sock, FIONBIO, &arg);
setsockopt(client_info->client_sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
/*---------------------------------------------------------*\
| Discover the remote hosts IP |
\*---------------------------------------------------------*/
struct sockaddr_storage tmp_addr;
char ipstr[INET6_ADDRSTRLEN];
socklen_t len;
len = sizeof(tmp_addr);
getpeername(client_info->client_sock, (struct sockaddr*)&tmp_addr, &len);
if(tmp_addr.ss_family == AF_INET)
{
struct sockaddr_in *s_4 = (struct sockaddr_in *)&tmp_addr;
inet_ntop(AF_INET, &s_4->sin_addr, ipstr, sizeof(ipstr));
client_info->client_ip = ipstr;
}
else
{
struct sockaddr_in6 *s_6 = (struct sockaddr_in6 *)&tmp_addr;
inet_ntop(AF_INET6, &s_6->sin6_addr, ipstr, sizeof(ipstr));
client_info->client_ip = ipstr;
}
/*---------------------------------------------------------*\
| We need to lock before the thread could possibly finish |
\*---------------------------------------------------------*/
ServerClientsMutex.lock();
/*---------------------------------------------------------*\
| Start a listener thread for the new client socket |
\*---------------------------------------------------------*/
client_info->client_listen_thread = new std::thread(&NetworkServer::ListenThreadFunction, this, client_info);
client_info->client_listen_thread->detach();
ServerClients.push_back(client_info);
ServerClientsMutex.unlock();
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
ClientInfoChanged();
}
LOG_INFO("[NetworkServer] Connection thread closed");
server_online = false;
server_listening = false;
ServerListeningChanged();
}
int NetworkServer::accept_select(int sockfd)
{
fd_set set;
struct timeval timeout;
while(1)
{
timeout.tv_sec = TCP_TIMEOUT_SECONDS;
timeout.tv_usec = 0;
FD_ZERO(&set);
FD_SET(sockfd, &set);
int rv = select(sockfd + 1, &set, NULL, NULL, &timeout);
if(rv == SOCKET_ERROR || server_online == false)
{
return -1;
}
else if(rv == 0)
{
continue;
}
else
{
return(accept((int)sockfd, NULL, NULL));
}
}
}
int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags)
{
fd_set set;
struct timeval timeout;
while(1)
{
timeout.tv_sec = TCP_TIMEOUT_SECONDS;
timeout.tv_usec = 0;
FD_ZERO(&set);
FD_SET(s, &set);
int rv = select((int)s + 1, &set, NULL, NULL, &timeout);
if(rv == SOCKET_ERROR || server_online == false)
{
return 0;
}
else if(rv == 0)
{
continue;
}
else
{
return(recv(s, buf, len, flags));
}
}
}
void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
{
SOCKET client_sock = client_info->client_sock;
LOG_INFO("[NetworkServer] Network server started");
/*---------------------------------------------------------*\
| This thread handles messages received from clients |
\*---------------------------------------------------------*/
while(server_online == true)
{
NetPacketHeader header;
int bytes_read = 0;
char * data = NULL;
for(unsigned int i = 0; i < 4; i++)
{
/*---------------------------------------------------------*\
| Read byte of magic |
\*---------------------------------------------------------*/
bytes_read = recv_select(client_sock, &header.pkt_magic[i], 1, 0);
if(bytes_read <= 0)
{
LOG_ERROR("[NetworkServer] recv_select failed receiving magic, closing listener");
goto listen_done;
}
/*---------------------------------------------------------*\
| Test characters of magic "ORGB" |
\*---------------------------------------------------------*/
if(header.pkt_magic[i] != openrgb_sdk_magic[i])
{
LOG_ERROR("[NetworkServer] Invalid magic received");
continue;
}
}
/*---------------------------------------------------------*\
| If we get to this point, the magic is correct. Read the |
| rest of the header |
\*---------------------------------------------------------*/
bytes_read = 0;
do
{
int tmp_bytes_read = 0;
tmp_bytes_read = recv_select(client_sock, (char *)&header.pkt_dev_idx + bytes_read, sizeof(header) - sizeof(header.pkt_magic) - bytes_read, 0);
bytes_read += tmp_bytes_read;
if(tmp_bytes_read <= 0)
{
LOG_ERROR("[NetworkServer] recv_select failed receiving header, closing listener");
goto listen_done;
}
} while(bytes_read != sizeof(header) - sizeof(header.pkt_magic));
/*---------------------------------------------------------*\
| Header received, now receive the data |
\*---------------------------------------------------------*/
bytes_read = 0;
if(header.pkt_size > 0)
{
data = new char[header.pkt_size];
do
{
int tmp_bytes_read = 0;
tmp_bytes_read = recv_select(client_sock, &data[(unsigned int)bytes_read], header.pkt_size - bytes_read, 0);
if(tmp_bytes_read <= 0)
{
LOG_ERROR("[NetworkServer] recv_select failed receiving data, closing listener");
goto listen_done;
}
bytes_read += tmp_bytes_read;
} while ((unsigned int)bytes_read < header.pkt_size);
}
/*---------------------------------------------------------*\
| Entire request received, select functionality based on |
| request ID |
\*---------------------------------------------------------*/
switch(header.pkt_id)
{
case NET_PACKET_ID_REQUEST_CONTROLLER_COUNT:
SendReply_ControllerCount(client_sock);
break;
case NET_PACKET_ID_REQUEST_CONTROLLER_DATA:
{
unsigned int protocol_version = 0;
if(header.pkt_size == sizeof(unsigned int))
{
memcpy(&protocol_version, data, sizeof(unsigned int));
}
SendReply_ControllerData(client_sock, header.pkt_dev_idx, protocol_version);
}
break;
case NET_PACKET_ID_REQUEST_PROTOCOL_VERSION:
SendReply_ProtocolVersion(client_sock);
ProcessRequest_ClientProtocolVersion(client_sock, header.pkt_size, data);
break;
case NET_PACKET_ID_SET_CLIENT_NAME:
if(data == NULL)
{
break;
}
ProcessRequest_ClientString(client_sock, header.pkt_size, data);
break;
case NET_PACKET_ID_REQUEST_RESCAN_DEVICES:
ProcessRequest_RescanDevices();
break;
case NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE:
if(data == NULL)
{
break;
}
if((header.pkt_dev_idx < controllers.size()) && (header.pkt_size == (2 * sizeof(int))))
{
int zone;
int new_size;
memcpy(&zone, data, sizeof(int));
memcpy(&new_size, data + sizeof(int), sizeof(int));
controllers[header.pkt_dev_idx]->ResizeZone(zone, new_size);
profile_manager->SaveProfile("sizes", true);
}
break;
case NET_PACKET_ID_RGBCONTROLLER_UPDATELEDS:
if(data == NULL)
{
break;
}
/*---------------------------------------------------------*\
| Verify the color description size (first 4 bytes of data) |
| matches the packet size in the header |
| |
| If protocol version is 4 or below and the legacy SDK |
| compatibility workaround is enabled, ignore this check. |
| This allows backwards compatibility with old versions of |
| SDK applications that didn't properly implement the size |
| field. |
\*---------------------------------------------------------*/
if((header.pkt_size == *((unsigned int*)data))
|| ((client_info->client_protocol_version <= 4)
&& (legacy_workaround_enabled)))
{
if(header.pkt_dev_idx < controllers.size())
{
controllers[header.pkt_dev_idx]->SetColorDescription((unsigned char *)data);
controllers[header.pkt_dev_idx]->UpdateLEDs();
}
}
else
{
LOG_ERROR("[NetworkServer] UpdateLEDs packet has invalid size. Packet size: %d, Data size: %d", header.pkt_size, *((unsigned int*)data));
goto listen_done;
}
break;
case NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS:
if(data == NULL)
{
break;
}
/*---------------------------------------------------------*\
| Verify the color description size (first 4 bytes of data) |
| matches the packet size in the header |
| |
| If protocol version is 4 or below and the legacy SDK |
| compatibility workaround is enabled, ignore this check. |
| This allows backwards compatibility with old versions of |
| SDK applications that didn't properly implement the size |
| field. |
\*---------------------------------------------------------*/
if((header.pkt_size == *((unsigned int*)data))
|| ((client_info->client_protocol_version <= 4)
&& (legacy_workaround_enabled)))
{
if(header.pkt_dev_idx < controllers.size())
{
int zone;
memcpy(&zone, &data[sizeof(unsigned int)], sizeof(int));
controllers[header.pkt_dev_idx]->SetZoneColorDescription((unsigned char *)data);
controllers[header.pkt_dev_idx]->UpdateZoneLEDs(zone);
}
}
else
{
LOG_ERROR("[NetworkServer] UpdateZoneLEDs packet has invalid size. Packet size: %d, Data size: %d", header.pkt_size, *((unsigned int*)data));
goto listen_done;
}
break;
case NET_PACKET_ID_RGBCONTROLLER_UPDATESINGLELED:
if(data == NULL)
{
break;
}
/*---------------------------------------------------------*\
| Verify the single LED color description size (8 bytes) |
| matches the packet size in the header |
\*---------------------------------------------------------*/
if(header.pkt_size == (sizeof(int) + sizeof(RGBColor)))
{
if(header.pkt_dev_idx < controllers.size())
{
int led;
memcpy(&led, data, sizeof(int));
controllers[header.pkt_dev_idx]->SetSingleLEDColorDescription((unsigned char *)data);
controllers[header.pkt_dev_idx]->UpdateSingleLED(led);
}
}
else
{
LOG_ERROR("[NetworkServer] UpdateSingleLED packet has invalid size. Packet size: %d, Data size: %d", header.pkt_size, (sizeof(int) + sizeof(RGBColor)));
goto listen_done;
}
break;
case NET_PACKET_ID_RGBCONTROLLER_SETCUSTOMMODE:
if(header.pkt_dev_idx < controllers.size())
{
controllers[header.pkt_dev_idx]->SetCustomMode();
}
break;
case NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE:
if(data == NULL)
{
break;
}
/*---------------------------------------------------------*\
| Verify the mode description size (first 4 bytes of data) |
| matches the packet size in the header |
| |
| If protocol version is 4 or below and the legacy SDK |
| compatibility workaround is enabled, ignore this check. |
| This allows backwards compatibility with old versions of |
| SDK applications that didn't properly implement the size |
| field. |
\*---------------------------------------------------------*/
if((header.pkt_size == *((unsigned int*)data))
|| ((client_info->client_protocol_version <= 4)
&& (legacy_workaround_enabled)))
{
if(header.pkt_dev_idx < controllers.size())
{
controllers[header.pkt_dev_idx]->SetModeDescription((unsigned char *)data, client_info->client_protocol_version);
controllers[header.pkt_dev_idx]->UpdateMode();
}
}
else
{
LOG_ERROR("[NetworkServer] UpdateMode packet has invalid size. Packet size: %d, Data size: %d", header.pkt_size, *((unsigned int*)data));
goto listen_done;
}
break;
case NET_PACKET_ID_RGBCONTROLLER_SAVEMODE:
if(data == NULL)
{
break;
}
/*---------------------------------------------------------*\
| Verify the mode description size (first 4 bytes of data) |
| matches the packet size in the header |
| |
| If protocol version is 4 or below and the legacy SDK |
| compatibility workaround is enabled, ignore this check. |
| This allows backwards compatibility with old versions of |
| SDK applications that didn't properly implement the size |
| field. |
\*---------------------------------------------------------*/
if((header.pkt_size == *((unsigned int*)data))
|| ((client_info->client_protocol_version <= 4)
&& (legacy_workaround_enabled)))
{
if(header.pkt_dev_idx < controllers.size())
{
controllers[header.pkt_dev_idx]->SetModeDescription((unsigned char *)data, client_info->client_protocol_version);
controllers[header.pkt_dev_idx]->SaveMode();
}
}
break;
case NET_PACKET_ID_REQUEST_PROFILE_LIST:
SendReply_ProfileList(client_sock);
break;
case NET_PACKET_ID_REQUEST_SAVE_PROFILE:
if(data == NULL)
{
break;
}
if(profile_manager)
{
std::string profile_name;
profile_name.assign(data, header.pkt_size);
profile_manager->SaveProfile(profile_name);
}
break;
case NET_PACKET_ID_REQUEST_LOAD_PROFILE:
if(data == NULL)
{
break;
}
if(profile_manager)
{
std::string profile_name;
profile_name.assign(data, header.pkt_size);
profile_manager->LoadProfile(profile_name);
}
for(RGBController* controller : controllers)
{
controller->UpdateLEDs();
}
break;
case NET_PACKET_ID_REQUEST_DELETE_PROFILE:
if(data == NULL)
{
break;
}
if(profile_manager)
{
std::string profile_name;
profile_name.assign(data, header.pkt_size);
profile_manager->DeleteProfile(profile_name);
}
break;
case NET_PACKET_ID_REQUEST_PLUGIN_LIST:
SendReply_PluginList(client_sock);
break;
case NET_PACKET_ID_PLUGIN_SPECIFIC:
{
unsigned int plugin_pkt_type = *((unsigned int*)(data));
unsigned int plugin_pkt_size = header.pkt_size - (sizeof(unsigned int));
unsigned char* plugin_data = (unsigned char*)(data + sizeof(unsigned int));
if(header.pkt_dev_idx < plugins.size())
{
NetworkPlugin plugin = plugins[header.pkt_dev_idx];
unsigned char* output = plugin.callback(plugin.callback_arg, plugin_pkt_type, plugin_data, &plugin_pkt_size);
if(output != nullptr)
{
SendReply_PluginSpecific(client_sock, plugin_pkt_type, output, plugin_pkt_size);
}
}
break;
}
break;
case NET_PACKET_ID_RGBCONTROLLER_CLEARSEGMENTS:
if(data == NULL)
{
break;
}
if((header.pkt_dev_idx < controllers.size()) && (header.pkt_size == sizeof(int)))
{
int zone;
memcpy(&zone, data, sizeof(int));
controllers[header.pkt_dev_idx]->ClearSegments(zone);
profile_manager->SaveProfile("sizes", true);
}
break;
case NET_PACKET_ID_RGBCONTROLLER_ADDSEGMENT:
{
/*---------------------------------------------------------*\
| Verify the segment description size (first 4 bytes of |
| data) matches the packet size in the header |
\*---------------------------------------------------------*/
if(header.pkt_size == *((unsigned int*)data))
{
if(header.pkt_dev_idx < controllers.size())
{
controllers[header.pkt_dev_idx]->SetSegmentDescription((unsigned char *)data);
profile_manager->SaveProfile("sizes", true);
}
}
}
break;
}
delete[] data;
}
listen_done:
ServerClientsMutex.lock();
for(unsigned int this_idx = 0; this_idx < ServerClients.size(); this_idx++)
{
if(ServerClients[this_idx] == client_info)
{
delete client_info;
ServerClients.erase(ServerClients.begin() + this_idx);
break;
}
}
client_info = nullptr;
ServerClientsMutex.unlock();
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
ClientInfoChanged();
}
void NetworkServer::ProcessRequest_ClientProtocolVersion(SOCKET client_sock, unsigned int data_size, char * data)
{
unsigned int protocol_version = 0;
if(data_size == sizeof(unsigned int) && (data != NULL))
{
memcpy(&protocol_version, data, sizeof(unsigned int));
}
if(protocol_version > OPENRGB_SDK_PROTOCOL_VERSION)
{
protocol_version = OPENRGB_SDK_PROTOCOL_VERSION;
}
ServerClientsMutex.lock();
for(unsigned int this_idx = 0; this_idx < ServerClients.size(); this_idx++)
{
if(ServerClients[this_idx]->client_sock == client_sock)
{
ServerClients[this_idx]->client_protocol_version = protocol_version;
break;
}
}
ServerClientsMutex.unlock();
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
ClientInfoChanged();
}
void NetworkServer::ProcessRequest_ClientString(SOCKET client_sock, unsigned int data_size, char * data)
{
ServerClientsMutex.lock();
for(unsigned int this_idx = 0; this_idx < ServerClients.size(); this_idx++)
{
if(ServerClients[this_idx]->client_sock == client_sock)
{
ServerClients[this_idx]->client_string.assign(data, data_size);
break;
}
}
ServerClientsMutex.unlock();
/*---------------------------------------------------------*\
| Client info has changed, call the callbacks |
\*---------------------------------------------------------*/
ClientInfoChanged();
}
void NetworkServer::ProcessRequest_RescanDevices()
{
ResourceManager::get()->DetectDevices();
}
void NetworkServer::SendReply_ControllerCount(SOCKET client_sock)
{
NetPacketHeader reply_hdr;
unsigned int reply_data;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_CONTROLLER_COUNT, sizeof(unsigned int));
reply_data = (unsigned int)controllers.size();
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&reply_data, sizeof(unsigned int), 0);
send_in_progress.unlock();
}
void NetworkServer::SendReply_ControllerData(SOCKET client_sock, unsigned int dev_idx, unsigned int protocol_version)
{
if(dev_idx < controllers.size())
{
NetPacketHeader reply_hdr;
unsigned char *reply_data = controllers[dev_idx]->GetDeviceDescription(protocol_version);
unsigned int reply_size;
memcpy(&reply_size, reply_data, sizeof(reply_size));
InitNetPacketHeader(&reply_hdr, dev_idx, NET_PACKET_ID_REQUEST_CONTROLLER_DATA, reply_size);
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)reply_data, reply_size, 0);
send_in_progress.unlock();
delete[] reply_data;
}
}
void NetworkServer::SendReply_ProtocolVersion(SOCKET client_sock)
{
NetPacketHeader reply_hdr;
unsigned int reply_data;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PROTOCOL_VERSION, sizeof(unsigned int));
reply_data = OPENRGB_SDK_PROTOCOL_VERSION;
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&reply_data, sizeof(unsigned int), 0);
send_in_progress.unlock();
}
void NetworkServer::SendRequest_DeviceListChanged(SOCKET client_sock)
{
NetPacketHeader pkt_hdr;
InitNetPacketHeader(&pkt_hdr, 0, NET_PACKET_ID_DEVICE_LIST_UPDATED, 0);
send_in_progress.lock();
send(client_sock, (char *)&pkt_hdr, sizeof(NetPacketHeader), 0);
send_in_progress.unlock();
}
void NetworkServer::SendReply_ProfileList(SOCKET client_sock)
{
if(!profile_manager)
{
return;
}
NetPacketHeader reply_hdr;
unsigned char *reply_data = profile_manager->GetProfileListDescription();
unsigned int reply_size;
memcpy(&reply_size, reply_data, sizeof(reply_size));
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PROFILE_LIST, reply_size);
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)reply_data, reply_size, 0);
send_in_progress.unlock();
}
void NetworkServer::SendReply_PluginList(SOCKET client_sock)
{
unsigned int data_size = 0;
unsigned int data_ptr = 0;
/*---------------------------------------------------------*\
| Calculate data size |
\*---------------------------------------------------------*/
unsigned short num_plugins = (unsigned short)plugins.size();
data_size += sizeof(data_size);
data_size += sizeof(num_plugins);
for(unsigned int i = 0; i < num_plugins; i++)
{
data_size += sizeof(unsigned short) * 3;
data_size += (unsigned int)strlen(plugins[i].name.c_str()) + 1;
data_size += (unsigned int)strlen(plugins[i].description.c_str()) + 1;
data_size += (unsigned int)strlen(plugins[i].version.c_str()) + 1;
data_size += sizeof(unsigned int) * 2;
}
/*---------------------------------------------------------*\
| Create data buffer |
\*---------------------------------------------------------*/
unsigned char *data_buf = new unsigned char[data_size];
/*---------------------------------------------------------*\
| Copy in data size |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &data_size, sizeof(data_size));
data_ptr += sizeof(data_size);
/*---------------------------------------------------------*\
| Copy in num_plugins |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &num_plugins, sizeof(num_plugins));
data_ptr += sizeof(num_plugins);
for(unsigned int i = 0; i < num_plugins; i++)
{
/*---------------------------------------------------------*\
| Copy in plugin name (size+data) |
\*---------------------------------------------------------*/
unsigned short str_len = (unsigned short)strlen(plugins[i].name.c_str()) + 1;
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
data_ptr += sizeof(unsigned short);
strcpy((char *)&data_buf[data_ptr], plugins[i].name.c_str());
data_ptr += str_len;
/*---------------------------------------------------------*\
| Copy in plugin description (size+data) |
\*---------------------------------------------------------*/
str_len = (unsigned short)strlen(plugins[i].description.c_str()) + 1;
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
data_ptr += sizeof(unsigned short);
strcpy((char *)&data_buf[data_ptr], plugins[i].description.c_str());
data_ptr += str_len;
/*---------------------------------------------------------*\
| Copy in plugin version (size+data) |
\*---------------------------------------------------------*/
str_len = (unsigned short)strlen(plugins[i].version.c_str()) + 1;
memcpy(&data_buf[data_ptr], &str_len, sizeof(unsigned short));
data_ptr += sizeof(unsigned short);
strcpy((char *)&data_buf[data_ptr], plugins[i].version.c_str());
data_ptr += str_len;
/*---------------------------------------------------------*\
| Copy in plugin index (data) |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &i, sizeof(unsigned int));
data_ptr += sizeof(unsigned int);
/*---------------------------------------------------------*\
| Copy in plugin sdk version (data) |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &plugins[i].protocol_version, sizeof(unsigned int));
data_ptr += sizeof(unsigned int);
}
NetPacketHeader reply_hdr;
unsigned int reply_size;
memcpy(&reply_size, data_buf, sizeof(reply_size));
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PLUGIN_LIST, reply_size);
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)data_buf, reply_size, 0);
send_in_progress.unlock();
delete [] data_buf;
}
void NetworkServer::SendReply_PluginSpecific(SOCKET client_sock, unsigned int pkt_type, unsigned char* data, unsigned int data_size)
{
NetPacketHeader reply_hdr;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_PLUGIN_SPECIFIC, data_size + sizeof(pkt_type));
send_in_progress.lock();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&pkt_type, sizeof(pkt_type), 0);
send(client_sock, (const char *)data, data_size, 0);
send_in_progress.unlock();
delete [] data;
}
void NetworkServer::SetProfileManager(ProfileManagerInterface* profile_manager_pointer)
{
profile_manager = profile_manager_pointer;
}
void NetworkServer::RegisterPlugin(NetworkPlugin plugin)
{
plugins.push_back(plugin);
}
void NetworkServer::UnregisterPlugin(std::string plugin_name)
{
for(std::vector<NetworkPlugin>::iterator it = plugins.begin(); it != plugins.end(); it++)
{
if(it->name == plugin_name)
{
plugins.erase(it);
break;
}
}
}
|