Fix a segfault that happens when a client fails a handshake.

This commit is contained in:
Barra Ó Catháin 2023-04-13 02:25:43 +01:00
parent b3ef4c1bb9
commit c032aa7dad
1 changed files with 34 additions and 24 deletions

View File

@ -40,7 +40,7 @@ int main(int argc, char ** argv)
time_t currentTime; time_t currentTime;
unsigned delay = 800; unsigned delay = 800;
int socketFileDesc, connectionFileDesc, length, clientsAmount, int socketFileDesc, connectionFileDesc, length, clientsAmount,
socketCheck, activityCheck, returnVal; socketCheck, activityCheck;
fd_set connectedClients; fd_set connectedClients;
pthread_t gameLogicThread, outputThread, schemeThread; pthread_t gameLogicThread, outputThread, schemeThread;
int clientSockets[PLAYERCOUNT]; int clientSockets[PLAYERCOUNT];
@ -280,36 +280,46 @@ int main(int argc, char ** argv)
} }
// See if we can put in the client: // See if we can put in the client:
for (int index = 0; index < PLAYERCOUNT; index++) for (int index = 0; index < PLAYERCOUNT; index++)
{ {
printf("Checking for slot: %d\n", index);
// When there is an empty slot, pop it in: // When there is an empty slot, pop it in:
if (clientSockets[index] == 0) if (clientSockets[index] == 0)
{ {
clientSockets[index] = connectionFileDesc; volatile int handshakeReturnValue = 0;
//printf("Adding to list of sockets as %d.\n", index); clientSockets[index] = connectionFileDesc;
gnutls_transport_set_int(tlssessions[index], clientSockets[index]); gnutls_transport_set_int(tlssessions[index], clientSockets[index]);
do do
{ {
returnVal = gnutls_handshake(tlssessions[index]); handshakeReturnValue = gnutls_handshake(tlssessions[index]);
} while (handshakeReturnValue < 0 && gnutls_error_is_fatal(handshakeReturnValue) == 0);
// If it's good, send them the welcome message:
if(handshakeReturnValue == 0)
{
// Send a greeting message:
memcpy(sendBuffer.senderName, "\0 Login > \0", 11);
strcpy(sendBuffer.messageContent, "Welcome to the server!");
messageSend(tlssessions[index], &sendBuffer);
strcpy(receiveBuffer.messageContent, "/look");
// Allocate the memory for a new input message:
inputMessage * newMessage = malloc(sizeof(inputMessage));
newMessage->content = malloc(sizeof(userMessage));
// Copy in the correct data:
memcpy(newMessage->content, &receiveBuffer, sizeof(userMessage));
newMessage->sender = &connectedPlayers[index];
// Push the new message onto the queue:
pushQueue(inputQueue, newMessage, INPUT_MESSAGE);
break;
} }
while (returnVal < 0 && gnutls_error_is_fatal(returnVal) == 0);
// Send a greeting message:
memcpy(sendBuffer.senderName, "\0 Login > \0", 11);
strcpy(sendBuffer.messageContent, "Welcome to the server!");
messageSend(tlssessions[index], &sendBuffer);
strcpy(receiveBuffer.messageContent, "/look");
// Allocate the memory for a new input message: // If it's not good, close it, we don't want it:
inputMessage * newMessage = malloc(sizeof(inputMessage)); shutdown(clientSockets[index], 2);
newMessage->content = malloc(sizeof(userMessage)); close(clientSockets[index]);
clientSockets[index] = 0;
// Copy in the correct data: break;
memcpy(newMessage->content, &receiveBuffer, sizeof(userMessage));
newMessage->sender = &connectedPlayers[index];
// Push the new message onto the queue:
pushQueue(inputQueue, newMessage, INPUT_MESSAGE);
break;
} }
} }
} }