mirror of
https://github.com/kingk85/uFTP.git
synced 2025-07-25 13:16:12 +03:00
Working on TLS
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
build/uFTP
BIN
build/uFTP
Binary file not shown.
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
118
ftpData.c
118
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
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
84
ftpServer.c
84
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)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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--;
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user