From 2a4c4f045dc8f645bfe796937509dc3e82065704 Mon Sep 17 00:00:00 2001 From: Ugo Cirmignani Date: Wed, 12 Dec 2018 20:40:37 +0100 Subject: [PATCH] working on threads --- ftpCommandElaborate.c | 20 ++++++++++---------- ftpData.c | 20 +++++++++++--------- ftpData.h | 8 ++++---- ftpServer.c | 2 +- library/connection.c | 6 +++--- testNotes.txt | 1 + 6 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index bf88b7d..515fed9 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -495,10 +495,10 @@ int parseCommandPort(ftpDataType * data, int socketId) returnCode = snprintf(data->clients[socketId].workerData.activeIpAddress, CLIENT_BUFFER_STRING_SIZE, "%d.%d.%d.%d", ipAddressBytes[0],ipAddressBytes[1],ipAddressBytes[2],ipAddressBytes[3]); void *pReturn; - if (data->clients[socketId].workerData.threadIsAlive == 1) - { + //if (data->clients[socketId].workerData.threadIsAlive == 1) + //{ returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread); - } + //} returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); data->clients[socketId].workerData.passiveModeOn = 0; data->clients[socketId].workerData.activeModeOn = 1; @@ -526,10 +526,10 @@ int parseCommandAbor(ftpDataType * data, int socketId) if (data->clients[socketId].workerData.threadIsAlive == 1) { void *pReturn; - if (data->clients[socketId].workerData.threadIsAlive == 1) - { + //if (data->clients[socketId].workerData.threadIsAlive == 1) + //{ pthread_cancel(data->clients[socketId].workerData.workerThread); - } + //} pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); returnCode = socketPrintf(data, socketId, "s", "426 ABORT\r\n"); @@ -596,7 +596,7 @@ int parseCommandList(ftpDataType * data, int socketId) setDynamicStringDataType(&data->clients[socketId].listPath, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen); } - pthread_mutex_lock(&data->clients[socketId].workerData.conditionMutex); + pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); data->clients[socketId].workerData.commandReceived = 1; @@ -627,7 +627,7 @@ int parseCommandNlst(ftpDataType * data, int socketId) setDynamicStringDataType(&data->clients[socketId].nlistPath, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen); } - pthread_mutex_lock(&data->clients[socketId].workerData.conditionMutex); + pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); data->clients[socketId].workerData.commandReceived = 1; @@ -652,7 +652,7 @@ int parseCommandRetr(ftpDataType * data, int socketId) if (isSafePath == 1 && FILE_IsFile(data->clients[socketId].fileToRetr.text) == 1) { - pthread_mutex_lock(&data->clients[socketId].workerData.conditionMutex); + pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); data->clients[socketId].workerData.commandReceived = 1; @@ -682,7 +682,7 @@ int parseCommandStor(ftpDataType * data, int socketId) if (isSafePath == 1) { - pthread_mutex_lock(&data->clients[socketId].workerData.conditionMutex); + pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); data->clients[socketId].workerData.commandReceived = 1; diff --git a/ftpData.c b/ftpData.c index e8d40e4..9a2775b 100644 --- a/ftpData.c +++ b/ftpData.c @@ -593,10 +593,10 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) data->clients[clientId].workerData.theStorFile = NULL; } - #ifdef OPENSSL_ENABLED - SSL_free(data->clients[clientId].workerData.serverSsl); - SSL_free(data->clients[clientId].workerData.clientSsl); - #endif + #ifdef OPENSSL_ENABLED + SSL_free(data->clients[clientId].workerData.serverSsl); + SSL_free(data->clients[clientId].workerData.clientSsl); + #endif } else { @@ -627,10 +627,11 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) data->clients[clientId].workerData.directoryInfo.Destroy(&data->clients[clientId].workerData.directoryInfo, deleteListDataInfoVector); free(lastToDestroy); } - #ifdef OPENSSL_ENABLED - data->clients[clientId].workerData.serverSsl = SSL_new(data->serverCtx); - data->clients[clientId].workerData.clientSsl = SSL_new(data->clientCtx); - #endif + + #ifdef OPENSSL_ENABLED + data->clients[clientId].workerData.serverSsl = SSL_new(data->serverCtx); + data->clients[clientId].workerData.clientSsl = SSL_new(data->clientCtx); + #endif } void resetClientData(ftpDataType *data, int clientId, int isInitialization) @@ -647,6 +648,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) else { void *pReturn = NULL; + pthread_cancel(data->clients[clientId].workerData.workerThread); pthread_join(data->clients[clientId].workerData.workerThread, &pReturn); } @@ -654,7 +656,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) #ifdef OPENSSL_ENABLED SSL_free(data->clients[clientId].ssl); - //SSL_free(data->clients[clientId].workerData.ssl); + SSL_free(data->clients[clientId].workerData.ssl); #endif } diff --git a/ftpData.h b/ftpData.h index 284fd0b..d637850 100644 --- a/ftpData.h +++ b/ftpData.h @@ -119,10 +119,10 @@ struct ipData struct workerData { - #ifdef OPENSSL_ENABLED - SSL *serverSsl; - SSL *clientSsl; - #endif + #ifdef OPENSSL_ENABLED + SSL *serverSsl; + SSL *clientSsl; + #endif int threadIsAlive; int connectionPort; diff --git a/ftpServer.c b/ftpServer.c index d40c729..9251396 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -570,7 +570,7 @@ void runFtpServer(void) //Debug print errors if (ftpData.clients[processingSock].bufferIndex < 0) { - ftpData.clients[processingSock].closeTheClient = 1; + //ftpData.clients[processingSock].closeTheClient = 1; printf("\n1 Errno = %d", errno); perror("1 Error: "); continue; diff --git a/library/connection.c b/library/connection.c index 165b12d..dbc3984 100644 --- a/library/connection.c +++ b/library/connection.c @@ -460,13 +460,13 @@ void closeClient(ftpDataType * ftpData, int processingSocket) { printf("\nQUIT FLAG SET!\n"); - if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1) - { + // if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1) + // { void *pReturn; pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread); pthread_join(ftpData->clients[processingSocket].workerData.workerThread, &pReturn); printf("\nQuit command received the Pasv Thread has been cancelled."); - } + // } FD_CLR(ftpData->clients[processingSocket].socketDescriptor, &ftpData->connectionData.rsetAll); FD_CLR(ftpData->clients[processingSocket].socketDescriptor, &ftpData->connectionData.wsetAll); diff --git a/testNotes.txt b/testNotes.txt index 95e9809..26c89c6 100644 --- a/testNotes.txt +++ b/testNotes.txt @@ -8,6 +8,7 @@ sudo apt-get install libssl-dev #Remove password from a certificate openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 openssl rsa -in key.pem -out newkey.pem +handle SIGPIPE nostop noprint pass #Testing ssl