diff --git a/build/modules/configRead.o b/build/modules/configRead.o index bc8ab43..9988b76 100644 Binary files a/build/modules/configRead.o and b/build/modules/configRead.o differ diff --git a/build/modules/connection.o b/build/modules/connection.o index aa33402..7066c7b 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index 62707aa..f135fad 100644 Binary files a/build/modules/ftpCommandElaborate.o and b/build/modules/ftpCommandElaborate.o differ diff --git a/build/modules/ftpData.o b/build/modules/ftpData.o index 19f51eb..9794131 100644 Binary files a/build/modules/ftpData.o and b/build/modules/ftpData.o differ diff --git a/build/modules/ftpServer.o b/build/modules/ftpServer.o index 4fdc416..1c71db4 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/uFTP b/build/uFTP index ebcf86c..f6864a0 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index b8dd392..b42d4fd 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -321,9 +321,92 @@ int parseCommandFeat(ftpDataType * data, int socketId) SPSV ESTP 211 End. + + +211-Extensions supported: + EPRT + IDLE + MDTM + SIZE + MFMT + REST STREAM + MLST type*;size*;sizd*;modify*;UNIX.mode*;UNIX.uid*;UNIX.gid*;unique*; + MLSD + AUTH TLS + PBSZ + PROT + UTF8 + TVFS + ESTA + PASV + EPSV + SPSV + ESTP +211 End. + */ int returnCode; - returnCode = socketPrintf(data, socketId, "s", "211-Extensions supported:\r\n PASV\r\nUTF8\r\nAUTH TLS\r\n211 End.\r\n"); + returnCode = socketPrintf(data, socketId, "s", "211-Extensions supported:\r\n PASV\r\nUTF8\r\nAUTH TLS\r\nPBSZ\r\nPROT\r\n211 End.\r\n"); + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; + + return FTP_COMMAND_PROCESSED; +} + +int parseCommandProt(ftpDataType * data, int socketId) +{ + int returnCode; + char *theProtArg; + theProtArg = getFtpCommandArg("PROT", data->clients[socketId].theCommandReceived, 0); + + if (theProtArg[0] == 'C' || theProtArg[0] == 'c') + { + //Clear + printf("\nSet data channel to clear"); + data->clients[socketId].dataChannelIsTls = 0; + returnCode = socketPrintf(data, socketId, "scs", "200 PROT set to ", theProtArg[0], "\r\n"); + + } + else if (theProtArg[0] == 'P' || theProtArg[0] == 'p') + { + //Private + printf("\nSet data channel to private"); + data->clients[socketId].dataChannelIsTls = 1; + returnCode = socketPrintf(data, socketId, "scs", "200 PROT set to ", theProtArg[0], "\r\n"); + } + else + { + returnCode = socketPrintf(data, socketId, "scs", "502 Mode ", theProtArg[0]," is not implemented\r\n"); + } + + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; + + return FTP_COMMAND_PROCESSED; +} + +int parseCommandCcc(ftpDataType * data, int socketId) +{ + #ifdef OPENSSL_ENABLED + int returnCode; + returnCode = socketPrintf(data, socketId, "s", "200 TLS connection aborted\r\n"); + SSL_set_shutdown(data->clients[socketId].ssl, SSL_SENT_SHUTDOWN); + data->clients[socketId].tlsIsEnabled = 0; + + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; + + return FTP_COMMAND_PROCESSED; + #endif +} + +int parseCommandPbsz(ftpDataType * data, int socketId) +{ + int returnCode; + char *thePbszSize; + thePbszSize = getFtpCommandArg("PBSZ", data->clients[socketId].theCommandReceived, 0); + + returnCode = socketPrintf(data, socketId, "sss", "200 PBSZ set to ", thePbszSize, "\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 4c21cc0..1bb997c 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -54,6 +54,12 @@ int parseCommandAuth(ftpDataType * data, int socketId); int parseCommandPwd(ftpDataType * data, int socketId); int parseCommandSyst(ftpDataType * data, int socketId); int parseCommandFeat(ftpDataType * data, int socketId); + +int parseCommandProt(ftpDataType * data, int socketId); +int parseCommandCcc(ftpDataType * data, int socketId); +int parseCommandPbsz(ftpDataType * data, int socketId); + + int parseCommandStruF(ftpDataType * data, int socketId); int parseCommandTypeI(ftpDataType * data, int socketId); int parseCommandModeS(ftpDataType * data, int socketId); diff --git a/ftpData.c b/ftpData.c index 8b8cd52..a4d85d0 100644 --- a/ftpData.c +++ b/ftpData.c @@ -36,6 +36,7 @@ #include "ftpData.h" #include "library/configRead.h" #include "library/fileManagement.h" +#include "library/connection.h" void cleanDynamicStringDataType(dynamicStringDataType *dynamicString, int init) { @@ -218,15 +219,15 @@ void setRandomicPort(ftpDataType *data, int socketPosition) printf("data->clients[%d].workerData.connectionPort = %d", socketPosition, data->clients[socketPosition].workerData.connectionPort); } -int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, int commandType) +int writeListDataInfoToSocket(ftpDataType *ftpData, int clientId, int *filesNumber, int commandType) { int i, x, returnCode; int fileAndFoldersCount = 0; char **fileList = NULL; - FILE_GetDirectoryInodeList(thePath, &fileList, &fileAndFoldersCount, 0); + FILE_GetDirectoryInodeList(ftpData->clients[clientId].listPath.text, &fileList, &fileAndFoldersCount, 0); *filesNumber = fileAndFoldersCount; - returnCode = dprintf(theSocket, "total %d\r\n", fileAndFoldersCount); + returnCode = socketWorkerPrintf(ftpData, clientId, "sds", "total ", fileAndFoldersCount ,"\r\n"); if (returnCode <= 0) { return -1; @@ -311,21 +312,37 @@ int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, i { case COMMAND_TYPE_LIST: { + returnCode = socketWorkerPrintf(ftpData, clientId, "ssdssssslsssss", + data.inodePermissionString == NULL? "Unknown" : data.inodePermissionString + ," " + ,data.numberOfSubDirectories + ," " + ,data.owner == NULL? "Unknown" : data.owner + ," " + ,data.groupOwner == NULL? "Unknown" : data.groupOwner + ," " + ,data.fileSize + ," " + ,data.lastModifiedDataString == NULL? "Unknown" : data.lastModifiedDataString + ," " + ,data.finalStringPath == NULL? "Unknown" : data.finalStringPath + ,"\r\n"); + /* returnCode = dprintf(theSocket, "%s %d %s %s %lld %s %s\r\n", - data.inodePermissionString == NULL? "Uknown" : data.inodePermissionString + data.inodePermissionString == NULL? "Unknown" : data.inodePermissionString ,data.numberOfSubDirectories - ,data.owner == NULL? "Uknown" : data.owner - ,data.groupOwner == NULL? "Uknown" : data.groupOwner + ,data.owner == NULL? "Unknown" : data.owner + ,data.groupOwner == NULL? "Unknown" : data.groupOwner ,data.fileSize - ,data.lastModifiedDataString == NULL? "Uknown" : data.lastModifiedDataString - ,data.finalStringPath == NULL? "Uknown" : data.finalStringPath); - + ,data.lastModifiedDataString == NULL? "Unknown" : data.lastModifiedDataString + ,data.finalStringPath == NULL? "Unknown" : data.finalStringPath); + */ } break; case COMMAND_TYPE_NLST: { - returnCode = dprintf(theSocket, "%s\r\n",data.fileNameNoPath); + returnCode = socketWorkerPrintf(ftpData, clientId, "ss", data.fileNameNoPath, "\r\n"); } break; @@ -536,71 +553,81 @@ void deleteListDataInfoVector(void *TheElementToDelete) } } -void resetWorkerData(workerDataType *workerData, int isInitialization) +void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) { - workerData->connectionPort = 0; - workerData->passiveModeOn = 0; - workerData->socketIsConnected = 0; - workerData->commandIndex = 0; - workerData->passiveListeningSocket = 0; - workerData->socketConnection = 0; - workerData->bufferIndex = 0; - workerData->commandReceived = 0; - workerData->retrRestartAtByte = 0; - workerData->threadIsAlive = 0; - workerData->activeModeOn = 0; - workerData->passiveModeOn = 0; - workerData->activeIpAddressIndex = 0; - memset(workerData->buffer, 0, CLIENT_BUFFER_STRING_SIZE); - memset(workerData->activeIpAddress, 0, CLIENT_BUFFER_STRING_SIZE); - memset(workerData->theCommandReceived, 0, CLIENT_BUFFER_STRING_SIZE); + data->clients[clientId].workerData.connectionPort = 0; + data->clients[clientId].workerData.passiveModeOn = 0; + data->clients[clientId].workerData.socketIsConnected = 0; + data->clients[clientId].workerData.commandIndex = 0; + data->clients[clientId].workerData.passiveListeningSocket = 0; + data->clients[clientId].workerData.socketConnection = 0; + data->clients[clientId].workerData.bufferIndex = 0; + data->clients[clientId].workerData.commandReceived = 0; + data->clients[clientId].workerData.retrRestartAtByte = 0; + data->clients[clientId].workerData.threadIsAlive = 0; + data->clients[clientId].workerData.activeModeOn = 0; + data->clients[clientId].workerData.passiveModeOn = 0; + data->clients[clientId].workerData.activeIpAddressIndex = 0; - cleanDynamicStringDataType(&workerData->ftpCommand.commandArgs, isInitialization); - cleanDynamicStringDataType(&workerData->ftpCommand.commandOps, isInitialization); + memset(data->clients[clientId].workerData.buffer, 0, CLIENT_BUFFER_STRING_SIZE); + memset(data->clients[clientId].workerData.activeIpAddress, 0, CLIENT_BUFFER_STRING_SIZE); + memset(data->clients[clientId].workerData.theCommandReceived, 0, CLIENT_BUFFER_STRING_SIZE); + + cleanDynamicStringDataType(&data->clients[clientId].workerData.ftpCommand.commandArgs, isInitialization); + cleanDynamicStringDataType(&data->clients[clientId].workerData.ftpCommand.commandOps, isInitialization); /* wait main for action */ if (isInitialization != 1) { - pthread_mutex_destroy(&workerData->conditionMutex); - pthread_cond_destroy(&workerData->conditionVariable); + pthread_mutex_destroy(&data->clients[clientId].workerData.conditionMutex); + pthread_cond_destroy(&data->clients[clientId].workerData.conditionVariable); - if (workerData->theStorFile != NULL) + if (data->clients[clientId].workerData.theStorFile != NULL) { - fclose(workerData->theStorFile); - workerData->theStorFile = NULL; + fclose(data->clients[clientId].workerData.theStorFile); + data->clients[clientId].workerData.theStorFile = NULL; } + + #ifdef OPENSSL_ENABLED + SSL_free(data->clients[clientId].workerData.ssl); + #endif } else { - DYNV_VectorGeneric_Init(&workerData->directoryInfo); - workerData->theStorFile = NULL; - workerData->workerThread = 0; + DYNV_VectorGeneric_Init(&data->clients[clientId].workerData.directoryInfo); + data->clients[clientId].workerData.theStorFile = NULL; + data->clients[clientId].workerData.workerThread = 0; } - if (pthread_mutex_init(&workerData->conditionMutex, NULL) != 0) + if (pthread_mutex_init(&data->clients[clientId].workerData.conditionMutex, NULL) != 0) { - printf("\nworkerData->conditionMutex init failed\n"); + printf("\ndata->clients[clientId].workerData.conditionMutex init failed\n"); exit(0); } - if (pthread_cond_init(&workerData->conditionVariable, NULL) != 0) + if (pthread_cond_init(&data->clients[clientId].workerData.conditionVariable, NULL) != 0) { - printf("\nworkerData->conditionVariable init failed\n"); + printf("\ndata->clients[clientId].workerData.conditionVariable init failed\n"); exit(0); } //Clear the dynamic vector structure - int theSize = workerData->directoryInfo.Size; + int theSize = data->clients[clientId].workerData.directoryInfo.Size; char ** lastToDestroy = NULL; if (theSize > 0) { - lastToDestroy = ((ftpListDataType *)workerData->directoryInfo.Data[0])->fileList; - workerData->directoryInfo.Destroy(&workerData->directoryInfo, deleteListDataInfoVector); + lastToDestroy = ((ftpListDataType *)data->clients[clientId].workerData.directoryInfo.Data[0])->fileList; + data->clients[clientId].workerData.directoryInfo.Destroy(&data->clients[clientId].workerData.directoryInfo, deleteListDataInfoVector); free(lastToDestroy); } + + #ifdef OPENSSL_ENABLED + data->clients[clientId].ssl = SSL_new(data->ctx); + #endif + } void resetClientData(ftpDataType *data, int clientId, int isInitialization) @@ -634,6 +661,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) } data->clients[clientId].tlsIsEnabled = 0; + data->clients[clientId].dataChannelIsTls = 0; data->clients[clientId].socketDescriptor = -1; data->clients[clientId].socketCommandReceived = 0; data->clients[clientId].socketIsConnected = 0; @@ -671,7 +699,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) data->clients[clientId].lastActivityTimeStamp = 0; #ifdef OPENSSL_ENABLED - data->clients[clientId].ssl = SSL_new(data->ctx); + data->clients[clientId].workerData.ssl = SSL_new(data->ctx); #endif } diff --git a/ftpData.h b/ftpData.h index 6f5f049..f50ee98 100644 --- a/ftpData.h +++ b/ftpData.h @@ -117,6 +117,10 @@ struct ipData struct workerData { + #ifdef OPENSSL_ENABLED + SSL *ssl; + #endif + int threadIsAlive; int connectionPort; int passiveModeOn; @@ -153,6 +157,7 @@ struct clientData #endif int tlsIsEnabled; + int dataChannelIsTls; pthread_mutex_t writeMutex; int clientProgressiveNumber; @@ -249,12 +254,12 @@ int getSafePath(dynamicStringDataType *safePath, char *theDirectoryName, loginDa void appendToDynamicStringDataType(dynamicStringDataType *dynamicString, char *theString, int stringLen); void setRandomicPort(ftpDataType *data, int socketPosition); void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo); -int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, int commandType); +int writeListDataInfoToSocket(ftpDataType *data, int clientId, int *filesNumber, int commandType); int searchInLoginFailsVector(void *loginFailsVector, void *element); void deleteLoginFailsData(void *element); void deleteListDataInfoVector(void *TheElementToDelete); -void resetWorkerData(workerDataType *pasvData, int isInitialization); +void resetWorkerData(ftpDataType *data, int clientId, int isInitialization); void resetClientData(ftpDataType *data, int clientId, int isInitialization); int compareStringCaseInsensitive(char *stringIn, char* stringRef, int stringLenght); diff --git a/ftpServer.c b/ftpServer.c index fbf6fde..a5277b2 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -55,11 +55,19 @@ void workerCleanup(void *socketId) { int theSocketId = *(int *)socketId; //printf("\nClosing pasv socket (%d) ok!", theSocketId); + #ifdef OPENSSL_ENABLED + if (ftpData.clients[theSocketId].dataChannelIsTls == 1) + { + printf("\nSSL worker Shutdown!"); + SSL_shutdown(ftpData.clients[theSocketId].workerData.ssl); + } + #endif + shutdown(ftpData.clients[theSocketId].workerData.socketConnection, SHUT_RDWR); shutdown(ftpData.clients[theSocketId].workerData.passiveListeningSocket, SHUT_RDWR); close(ftpData.clients[theSocketId].workerData.socketConnection); close(ftpData.clients[theSocketId].workerData.passiveListeningSocket); - resetWorkerData(&ftpData.clients[theSocketId].workerData, 0); + resetWorkerData(&ftpData, theSocketId, 0); } void *connectionWorkerHandle(void * socketId) @@ -94,7 +102,6 @@ void *connectionWorkerHandle(void * socketId) if (ftpData.clients[theSocketId].workerData.socketIsConnected == 0) { - returnCode = socketPrintf(&ftpData, theSocketId, "sdsdsdsdsdsds", "227 Entering Passive Mode (", ftpData.clients[theSocketId].serverIpAddressInteger[0], ",", ftpData.clients[theSocketId].serverIpAddressInteger[1], ",", ftpData.clients[theSocketId].serverIpAddressInteger[2], ",", ftpData.clients[theSocketId].serverIpAddressInteger[3], ",", (ftpData.clients[theSocketId].workerData.connectionPort / 256), ",", (ftpData.clients[theSocketId].workerData.connectionPort % 256), ")\r\n"); if (returnCode <= 0) { @@ -107,6 +114,20 @@ void *connectionWorkerHandle(void * socketId) if ((ftpData.clients[theSocketId].workerData.socketConnection = accept(ftpData.clients[theSocketId].workerData.passiveListeningSocket, 0, 0))!=-1) { ftpData.clients[theSocketId].workerData.socketIsConnected = 1; + #ifdef OPENSSL_ENABLED + if (ftpData.clients[theSocketId].dataChannelIsTls == 1) + { + + SSL_set_fd(ftpData.clients[theSocketId].workerData.ssl, ftpData.clients[theSocketId].workerData.socketConnection); + returnCode = SSL_accept(ftpData.clients[theSocketId].workerData.ssl); + if (returnCode <= 0) { + printf("\nSSL ERRORS ON WORKER"); + } + else { + printf("\nSSL ACCEPTED ON WORKER"); + } + } + #endif } else { @@ -250,7 +271,8 @@ void *connectionWorkerHandle(void * socketId) pthread_exit(NULL); } - returnCode = writeListDataInfoToSocket(ftpData.clients[theSocketId].listPath.text, ftpData.clients[theSocketId].workerData.socketConnection, &theFiles, theCommandType); + //returnCode = writeListDataInfoToSocket(ftpData.clients[theSocketId].listPath.text, ftpData.clients[theSocketId].workerData.socketConnection, &theFiles, theCommandType); + returnCode = writeListDataInfoToSocket(&ftpData, theSocketId, &theFiles, theCommandType); if (returnCode <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -530,47 +552,21 @@ static int processCommand(int processingElement) { printf("\nAUTH COMMAND RECEIVED"); toReturn = parseCommandAuth(&ftpData, processingElement); - - //returnCode = dprintf(theClientData->socketDescriptor, "502 Security extensions not implemented.\r\n"); - //returnCode = dprintf(ftpData.clients[processingElement].socketDescriptor, "234 AUTH TLS OK..\r\n"); - -/* - ftpData.clients[processingElement].tlsIsEnabled = 1; - SSL *ssl; - ssl = SSL_new(ctx); - SSL_set_fd(ssl, ftpData.clients[processingElement].socketDescriptor); - - int sslAcceptTimeout = 0; - do { - returnCode = SSL_accept(ssl); - printf("\nSSL waiting handshake %d.. return code = %d", sslAcceptTimeout, returnCode); - if (returnCode <= 0) { - printf("\nSSL ERRORS"); - ERR_print_errors_fp(stderr); - } - else { - printf("\nSSL ACCEPTED"); - fflush(0); - SSL_write(ssl, "ciao prova\r\n", strlen("ciao prova\r\n")); - } - sslAcceptTimeout++; - - sleep(1); - } - while(returnCode <=0 && sslAcceptTimeout < 3); - printf("\nReading ssl"); - char buffer[100]; - int readenb = 0; - while(1) - { - readenb = SSL_read(ssl, buffer, 100); - if (readenb > 0) - printf("\nSslReaden: %s", buffer); - sleep(1); - - } -*/ - + } + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PROT", strlen("PROT")) == 1) + { + printf("\nPROT COMMAND RECEIVED"); + toReturn = parseCommandProt(&ftpData, processingElement); + } + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PBSZ", strlen("PBSZ")) == 1) + { + printf("\nPBSZ COMMAND RECEIVED"); + toReturn = parseCommandPbsz(&ftpData, processingElement); + } + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "CCC", strlen("CCC")) == 1) + { + printf("\nCCC COMMAND RECEIVED"); + toReturn = parseCommandCcc(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PWD", strlen("PWD")) == 1) { diff --git a/library/configRead.c b/library/configRead.c index 339b873..7a9af21 100644 --- a/library/configRead.c +++ b/library/configRead.c @@ -150,7 +150,7 @@ void initFtpData(ftpDataType *ftpData) //Client data reset to zero for (i = 0; i < ftpData->ftpParameters.maxClients; i++) { - resetWorkerData(&ftpData->clients[i].workerData, 1); + resetWorkerData(ftpData, i, 1); resetClientData(ftpData, i, 1); ftpData->clients[i].clientProgressiveNumber = i; } diff --git a/library/connection.c b/library/connection.c index 4f82510..81a3182 100644 --- a/library/connection.c +++ b/library/connection.c @@ -54,7 +54,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f va_list args; va_start(args, __fmt); - //pthread_mutex_lock(&ftpData->clients[clientId].writeMutex); + pthread_mutex_lock(&ftpData->clients[clientId].writeMutex); while (*__fmt != '\0') { @@ -152,11 +152,122 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f ++__fmt; } - //pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); + pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); va_end(args); return bytesWritten; } +int socketWorkerPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...) +{ + #define SOCKET_PRINTF_BUFFER 2048 + + int bytesWritten = 0; + char theBuffer[2048]; + int theStringSize = 0; + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + printf("\nWriting to worker socket id %d: ", clientId); + + va_list args; + va_start(args, __fmt); + while (*__fmt != '\0') + { + theStringSize = 0; + switch(*__fmt) + { + case 'd': + case 'D': + { + int theInteger = va_arg(args, int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%d", theInteger); + } + break; + + case 'c': + case 'C': + { + int theCharInteger = va_arg(args, int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%c", theCharInteger); + } + break; + + case 'f': + case 'F': + { + float theDouble = va_arg(args, double); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%f", theDouble); + } + break; + + case 's': + case 'S': + { + char * theString = va_arg(args, char *); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%s", theString); + } + break; + + case 'l': + case 'L': + { + long long int theLongLongInt = va_arg(args, long long int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%lld", theLongLongInt); + } + break; + + default: + { + printf("\n Switch is default (%c)", *__fmt); + } + break; + } + + if (theStringSize >= SOCKET_PRINTF_BUFFER) { + printf("\n String buffer is full!"); + } + else if (theStringSize < SOCKET_PRINTF_BUFFER && + theStringSize > 0) + { + int theReturnCode = 0; + + if (ftpData->clients[clientId].dataChannelIsTls != 1) + { + theReturnCode = write(ftpData->clients[clientId].workerData.socketConnection, theBuffer, theStringSize); + } + else if (ftpData->clients[clientId].dataChannelIsTls == 1) + { + #ifdef OPENSSL_ENABLED + theReturnCode = SSL_write(ftpData->clients[clientId].workerData.ssl, theBuffer, theStringSize); + #endif + } + + printf("%s", theBuffer); + + if (theReturnCode > 0) + { + bytesWritten += theReturnCode; + } + else + { + bytesWritten = theReturnCode; + break; + } + } + else if(theStringSize == 0) + { + printf("\n Nothing to write.. "); + } + + ++__fmt; + } + + va_end(args); + return bytesWritten; +} /* Return the higher socket available*/ int getMaximumSocketFd(int mainSocket, ftpDataType * ftpData) @@ -332,7 +443,7 @@ void closeSocket(ftpDataType * ftpData, int processingSocket) close(ftpData->clients[processingSocket].socketDescriptor); resetClientData(ftpData, processingSocket, 0); - resetWorkerData(&ftpData->clients[processingSocket].workerData, 0); + resetWorkerData(ftpData, processingSocket, 0); //Update client connecteds ftpData->connectedClients--; diff --git a/library/connection.h b/library/connection.h index c66deb4..7f95c7d 100644 --- a/library/connection.h +++ b/library/connection.h @@ -49,6 +49,7 @@ int isClientConnected(ftpDataType * ftpData, int cliendId); int getAvailableClientSocketIndex(ftpDataType * ftpData); int evaluateClientSocketConnection(ftpDataType * ftpData); int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...); +int socketWorkerPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...); #ifdef __cplusplus