working on threads

This commit is contained in:
Ugo Cirmignani
2018-12-12 20:40:37 +01:00
parent c8f5c9f6f0
commit 2a4c4f045d
6 changed files with 30 additions and 27 deletions

View File

@ -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]); 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; 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_cancel(data->clients[socketId].workerData.workerThread);
} //}
returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
data->clients[socketId].workerData.passiveModeOn = 0; data->clients[socketId].workerData.passiveModeOn = 0;
data->clients[socketId].workerData.activeModeOn = 1; data->clients[socketId].workerData.activeModeOn = 1;
@ -526,10 +526,10 @@ int parseCommandAbor(ftpDataType * data, int socketId)
if (data->clients[socketId].workerData.threadIsAlive == 1) if (data->clients[socketId].workerData.threadIsAlive == 1)
{ {
void *pReturn; void *pReturn;
if (data->clients[socketId].workerData.threadIsAlive == 1) //if (data->clients[socketId].workerData.threadIsAlive == 1)
{ //{
pthread_cancel(data->clients[socketId].workerData.workerThread); pthread_cancel(data->clients[socketId].workerData.workerThread);
} //}
pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
returnCode = socketPrintf(data, socketId, "s", "426 ABORT\r\n"); 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); 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); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
data->clients[socketId].workerData.commandReceived = 1; 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); 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); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
data->clients[socketId].workerData.commandReceived = 1; data->clients[socketId].workerData.commandReceived = 1;
@ -652,7 +652,7 @@ int parseCommandRetr(ftpDataType * data, int socketId)
if (isSafePath == 1 && if (isSafePath == 1 &&
FILE_IsFile(data->clients[socketId].fileToRetr.text) == 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); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
data->clients[socketId].workerData.commandReceived = 1; data->clients[socketId].workerData.commandReceived = 1;
@ -682,7 +682,7 @@ int parseCommandStor(ftpDataType * data, int socketId)
if (isSafePath == 1) 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); memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived); strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
data->clients[socketId].workerData.commandReceived = 1; data->clients[socketId].workerData.commandReceived = 1;

View File

@ -593,10 +593,10 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
data->clients[clientId].workerData.theStorFile = NULL; data->clients[clientId].workerData.theStorFile = NULL;
} }
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
SSL_free(data->clients[clientId].workerData.serverSsl); SSL_free(data->clients[clientId].workerData.serverSsl);
SSL_free(data->clients[clientId].workerData.clientSsl); SSL_free(data->clients[clientId].workerData.clientSsl);
#endif #endif
} }
else 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); data->clients[clientId].workerData.directoryInfo.Destroy(&data->clients[clientId].workerData.directoryInfo, deleteListDataInfoVector);
free(lastToDestroy); free(lastToDestroy);
} }
#ifdef OPENSSL_ENABLED
data->clients[clientId].workerData.serverSsl = SSL_new(data->serverCtx); #ifdef OPENSSL_ENABLED
data->clients[clientId].workerData.clientSsl = SSL_new(data->clientCtx); data->clients[clientId].workerData.serverSsl = SSL_new(data->serverCtx);
#endif data->clients[clientId].workerData.clientSsl = SSL_new(data->clientCtx);
#endif
} }
void resetClientData(ftpDataType *data, int clientId, int isInitialization) void resetClientData(ftpDataType *data, int clientId, int isInitialization)
@ -647,6 +648,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
else else
{ {
void *pReturn = NULL; void *pReturn = NULL;
pthread_cancel(data->clients[clientId].workerData.workerThread);
pthread_join(data->clients[clientId].workerData.workerThread, &pReturn); pthread_join(data->clients[clientId].workerData.workerThread, &pReturn);
} }
@ -654,7 +656,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
SSL_free(data->clients[clientId].ssl); SSL_free(data->clients[clientId].ssl);
//SSL_free(data->clients[clientId].workerData.ssl); SSL_free(data->clients[clientId].workerData.ssl);
#endif #endif
} }

View File

@ -119,10 +119,10 @@ struct ipData
struct workerData struct workerData
{ {
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
SSL *serverSsl; SSL *serverSsl;
SSL *clientSsl; SSL *clientSsl;
#endif #endif
int threadIsAlive; int threadIsAlive;
int connectionPort; int connectionPort;

View File

@ -570,7 +570,7 @@ void runFtpServer(void)
//Debug print errors //Debug print errors
if (ftpData.clients[processingSock].bufferIndex < 0) if (ftpData.clients[processingSock].bufferIndex < 0)
{ {
ftpData.clients[processingSock].closeTheClient = 1; //ftpData.clients[processingSock].closeTheClient = 1;
printf("\n1 Errno = %d", errno); printf("\n1 Errno = %d", errno);
perror("1 Error: "); perror("1 Error: ");
continue; continue;

View File

@ -460,13 +460,13 @@ void closeClient(ftpDataType * ftpData, int processingSocket)
{ {
printf("\nQUIT FLAG SET!\n"); printf("\nQUIT FLAG SET!\n");
if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1) // if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1)
{ // {
void *pReturn; void *pReturn;
pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread); pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread);
pthread_join(ftpData->clients[processingSocket].workerData.workerThread, &pReturn); pthread_join(ftpData->clients[processingSocket].workerData.workerThread, &pReturn);
printf("\nQuit command received the Pasv Thread has been cancelled."); 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.rsetAll);
FD_CLR(ftpData->clients[processingSocket].socketDescriptor, &ftpData->connectionData.wsetAll); FD_CLR(ftpData->clients[processingSocket].socketDescriptor, &ftpData->connectionData.wsetAll);

View File

@ -8,6 +8,7 @@ sudo apt-get install libssl-dev
#Remove password from a certificate #Remove password from a certificate
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
openssl rsa -in key.pem -out newkey.pem openssl rsa -in key.pem -out newkey.pem
handle SIGPIPE nostop noprint pass
#Testing ssl #Testing ssl