Improve logging in NetworkServer.cpp and close listener if any packet with an invalid size is received

This commit is contained in:
Adam Honse 2024-07-27 16:38:41 -05:00
parent 99c1aa0c26
commit e7240ecca0

View file

@ -49,7 +49,7 @@ NetworkClientInfo::~NetworkClientInfo()
{
if(client_sock != INVALID_SOCKET)
{
LOG_INFO("Closing server connection: %s", client_ip.c_str());
LOG_INFO("NetworkServer: Closing server connection: %s", client_ip.c_str());
delete client_listen_thread;
shutdown(client_sock, SD_RECEIVE);
closesocket(client_sock);
@ -261,7 +261,7 @@ void NetworkServer::StartServer()
if(err)
{
printf("Error: Unable to get address.\n");
LOG_ERROR("NetworkServer: Unable to get address.");
WSACleanup();
return;
}
@ -275,7 +275,7 @@ void NetworkServer::StartServer()
if(server_sock[socket_count] == INVALID_SOCKET)
{
printf("Error: network socket could not be created\n");
LOG_ERROR("NetworkServer: Network socket could not be created.");
WSACleanup();
return;
}
@ -287,23 +287,23 @@ void NetworkServer::StartServer()
{
if(errno == EADDRINUSE)
{
printf("Error: Could not bind network socket \nIs port %hu already being used?\n", GetPort());
LOG_ERROR("NetworkServer: Could not bind network socket. Is port %hu already being used?", GetPort());
}
else if(errno == EACCES)
{
printf("Error: Access to socket was denied.\n");
LOG_ERROR("NetworkServer: Could not bind network socket. Access to socket was denied.");
}
else if(errno == EBADF)
{
printf("Error: sockfd is not a valid file descriptor.\n");
LOG_ERROR("NetworkServer: Could not bind network socket. sockfd is not a valid file descriptor.");
}
else if(errno == EINVAL)
{
printf("Error: The socket is already bound to an address, or addrlen is wrong, or addr is not a valid address for this socket's domain..\n");
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)
{
printf("Error: The file descriptor sockfd does not refer to a socket.\n");
LOG_ERROR("NetworkServer: Could not bind network socket. The file descriptor sockfd does not refer to a socket.");
}
else
{
@ -311,7 +311,7 @@ void NetworkServer::StartServer()
| errno could be a Linux specific error, see: |
| https://man7.org/linux/man-pages/man2/bind.2.html |
\*---------------------------------------------------------*/
printf("Error: Could not bind network socket, error code:%d\n", errno);
LOG_ERROR("NetworkManager: Could not bind network socket. Error code: %d.", errno);
}
WSACleanup();
@ -383,7 +383,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
/*---------------------------------------------------------*\
| This thread handles client connections |
\*---------------------------------------------------------*/
printf("Network connection thread started on port %hu\n", GetPort());
LOG_INFO("NetworkServer: Network connection thread started on port %hu", GetPort());
while(server_online == true)
{
@ -399,7 +399,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
\*---------------------------------------------------------*/
if(listen(server_sock[socket_idx], 10) < 0)
{
printf("Connection thread closed\r\n");
LOG_INFO("NetworkServer: Connection thread closed");
server_online = false;
return;
@ -415,7 +415,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
if(client_info->client_sock < 0)
{
printf("Connection thread closed\r\n");
LOG_INFO("NetworkServer: Connection thread closed");
server_online = false;
server_listening = false;
@ -474,7 +474,7 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
ClientInfoChanged();
}
printf("Connection thread closed\r\n");
LOG_INFO("NetworkServer: Connection thread closed");
server_online = false;
server_listening = false;
ServerListeningChanged();
@ -544,7 +544,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
{
SOCKET client_sock = client_info->client_sock;
printf("Network server started\n");
LOG_INFO("NetworkServer: Network server started");
/*---------------------------------------------------------*\
| This thread handles messages received from clients |
@ -564,6 +564,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
if(bytes_read <= 0)
{
LOG_ERROR("NetworkServer: recv_select failed receiving magic, closing listener");
goto listen_done;
}
@ -572,6 +573,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
\*---------------------------------------------------------*/
if(header.pkt_magic[i] != openrgb_sdk_magic[i])
{
LOG_ERROR("NetworkServer: Invalid magic received");
continue;
}
}
@ -591,6 +593,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
if(tmp_bytes_read <= 0)
{
LOG_ERROR("NetworkServer: recv_select failed receiving header, closing listener");
goto listen_done;
}
@ -599,10 +602,9 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
/*---------------------------------------------------------*\
| Header received, now receive the data |
\*---------------------------------------------------------*/
bytes_read = 0;
if(header.pkt_size > 0)
{
bytes_read = 0;
data = new char[header.pkt_size];
do
@ -613,6 +615,7 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
if(tmp_bytes_read <= 0)
{
LOG_ERROR("NetworkServer: recv_select failed receiving data, closing listener");
goto listen_done;
}
bytes_read += tmp_bytes_read;
@ -694,6 +697,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
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:
@ -718,6 +726,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
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:
@ -742,6 +755,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
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:
@ -769,6 +787,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
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: