diff --git a/MakeSimple b/MakeSimple new file mode 100644 index 0000000..2e37c24 --- /dev/null +++ b/MakeSimple @@ -0,0 +1 @@ +#Standard GCC MakeFile to be continued... \ No newline at end of file diff --git a/build/Debug/GNU-Linux/ftpCommandElaborate.o b/build/Debug/GNU-Linux/ftpCommandElaborate.o index b07e031..69d2872 100644 Binary files a/build/Debug/GNU-Linux/ftpCommandElaborate.o and b/build/Debug/GNU-Linux/ftpCommandElaborate.o differ diff --git a/build/Debug/GNU-Linux/ftpData.o b/build/Debug/GNU-Linux/ftpData.o index 773138c..dd1f32c 100644 Binary files a/build/Debug/GNU-Linux/ftpData.o and b/build/Debug/GNU-Linux/ftpData.o differ diff --git a/build/Debug/GNU-Linux/ftpServer.o b/build/Debug/GNU-Linux/ftpServer.o index 96cdac4..ecbdd92 100644 Binary files a/build/Debug/GNU-Linux/ftpServer.o and b/build/Debug/GNU-Linux/ftpServer.o differ diff --git a/dist/Debug/GNU-Linux/uftp b/dist/Debug/GNU-Linux/uftp index ba11268..bdf4ad1 100755 Binary files a/dist/Debug/GNU-Linux/uftp and b/dist/Debug/GNU-Linux/uftp differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index 89341a5..a3571f5 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -218,6 +218,44 @@ int parseCommandPasv(ftpDataType * data, int socketId) return 1; } + +int parseCommandAbor(ftpDataType * data, int socketId) +{ + char theResponse[FTP_COMMAND_ELABORATE_CHAR_BUFFER]; + + /* + 426 ABORT + 226-Transfer aborted + 226 3.406 seconds (measured here), 1.58 Mbytes per second + 226 Since you see this ABOR must've succeeded + */ + + if (data->clients[socketId].pasvData.threadIsAlive == 1 && + data->clients[socketId].pasvData.threadIsBusy == 1) + { + void *pReturn; + pthread_cancel(data->clients[socketId].pasvData.pasvThread); + pthread_join(data->clients[socketId].pasvData.pasvThread, &pReturn); + printf("\nThread has been cancelled due to ABOR request."); + + memset(theResponse, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); + sprintf(theResponse, "426 ABORT\r\n"); + write(data->clients[socketId].socketDescriptor, theResponse, strlen(theResponse)); + + memset(theResponse, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); + sprintf(theResponse, "226 Transfer aborted\r\n"); + write(data->clients[socketId].socketDescriptor, theResponse, strlen(theResponse)); + } + else + { + memset(theResponse, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); + sprintf(theResponse, "226 Since you see this ABOR must've succeeded\r\n"); + write(data->clients[socketId].socketDescriptor, theResponse, strlen(theResponse)); + } + + return 1; +} + int parseCommandList(ftpDataType * data, int socketId) { pthread_mutex_lock(&data->clients[socketId].pasvData.conditionMutex); @@ -472,6 +510,47 @@ int parseCommandMkd(clientDataType *theClientData) } + +int parseCommandDele(clientDataType *theClientData) +{ + int returnStatus = 0; + char *theFileToDelete; + dynamicStringDataType theResponse; + dynamicStringDataType deleFileName; + + theFileToDelete = getFtpCommandArg("DELE", theClientData->theCommandReceived); + + + cleanDynamicStringDataType(&theResponse, 1); + cleanDynamicStringDataType(&deleFileName, 1); + + if (theFileToDelete[0] == '/') + { + setDynamicStringDataType(&deleFileName, theFileToDelete, strlen(theFileToDelete)); + } + else + { + setDynamicStringDataType(&deleFileName, theClientData->login.absolutePath.text, theClientData->login.absolutePath.textLen); + appendToDynamicStringDataType(&deleFileName, "/", 1); + appendToDynamicStringDataType(&deleFileName, theFileToDelete, strlen(theFileToDelete)); + } + + if (strlen(theFileToDelete) > 0) + { + printf("\nThe file to delete is: %s", deleFileName.text); + returnStatus = remove(deleFileName.text); + } + + setDynamicStringDataType(&theResponse, "250 Deleted ", strlen("250 Deleted ")); + appendToDynamicStringDataType(&theResponse, theFileToDelete, strlen(theFileToDelete)); + appendToDynamicStringDataType(&theResponse, "\r\n", strlen("\r\n")); + + write(theClientData->socketDescriptor, theResponse.text, theResponse.textLen); + cleanDynamicStringDataType(&theResponse, 0); + cleanDynamicStringDataType(&deleFileName, 0); + return 1; +} + int parseCommandRmd(clientDataType *theClientData) { int returnStatus = 0; @@ -497,7 +576,7 @@ int parseCommandRmd(clientDataType *theClientData) if (strlen(theDirectoryFilename) > 0) { - printf("\nThe directory to make is: %s", mkdFileName.text); + printf("\nThe directory to delete is: %s", mkdFileName.text); returnStatus = rmdir(mkdFileName.text); } diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 451c95d..cabc76c 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -30,6 +30,7 @@ int parseCommandPwd(clientDataType *theClientData); int parseCommandSyst(clientDataType *theClientData); int parseCommandFeat(clientDataType *theClientData); int parseCommandTypeI(clientDataType *theClientData); +int parseCommandAbor(ftpDataType * data, int socketId); int parseCommandPasv(ftpDataType * data, int socketId); int parseCommandList(ftpDataType * data, int socketId); int parseCommandRetr(ftpDataType * data, int socketId); @@ -38,6 +39,7 @@ int parseCommandRmd(clientDataType *theClientData); int parseCommandStor(ftpDataType * data, int socketId); int parseCommandCwd(clientDataType *theClientData); int parseCommandCdup(clientDataType *theClientData); +int parseCommandDele(clientDataType *theClientData); int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom); char *getFtpCommandArg(char * theCommand, char *theCommandString); diff --git a/ftpData.c b/ftpData.c index ad75218..88b6509 100644 --- a/ftpData.c +++ b/ftpData.c @@ -236,6 +236,7 @@ void deleteListDataInfoVector(void *TheElementToDelete) void resetPasvData(passiveDataType *pasvData, int isInitialization) { pasvData->passivePort = 0; + pasvData->threadIsBusy = 0; pasvData->passiveModeOn = 0; pasvData->passiveSocketIsConnected = 0; pasvData->commandIndex = 0; diff --git a/ftpData.h b/ftpData.h index ad58361..3c7e84b 100644 --- a/ftpData.h +++ b/ftpData.h @@ -65,6 +65,7 @@ struct passiveData char theFileNameToStor[CLIENT_COMMAND_STRING_SIZE]; int theFileNameToStorIndex; unsigned long int retrRestartAtByte; + int threadIsBusy; /* The PASV thread will wait the signal before start */ pthread_mutex_t conditionMutex; diff --git a/ftpServer.c b/ftpServer.c index 7b74345..b80882c 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -58,7 +58,7 @@ void *pasvThreadHandler(void * socketId) { int theSocketId = *(int *)socketId; pthread_cleanup_push(pasvThreadHandlerCleanup, (void *) &theSocketId); - + //Setting Alive Flag ftpData.clients[theSocketId].pasvData.threadIsAlive = 1; @@ -127,6 +127,9 @@ void *pasvThreadHandler(void * socketId) (strncmp(ftpData.clients[theSocketId].pasvData.theCommandReceived, "stor", strlen("stor")) == 0)) && ftpData.clients[theSocketId].pasvData.theFileNameToStorIndex > 0) { + //Set the busy flag + ftpData.clients[theSocketId].pasvData.threadIsBusy = 1; + FILE *theStorFile; char theResponse[FTP_COMMAND_ELABORATE_CHAR_BUFFER]; memset(theResponse, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); @@ -145,8 +148,6 @@ void *pasvThreadHandler(void * socketId) theStorFile = fopen(theFullFileName, "wb"); printf("\nSaving new file to the server: %s", theFullFileName); - - strcpy(theResponse, "150 Accepted data connection\r\n"); write(ftpData.clients[theSocketId].socketDescriptor, theResponse, strlen(theResponse)); @@ -172,7 +173,7 @@ void *pasvThreadHandler(void * socketId) sprintf(theResponse, "226 file stor ok\r\n"); write(ftpData.clients[theSocketId].socketDescriptor, theResponse, strlen(theResponse)); break; - } + } else if (ftpData.clients[theSocketId].pasvData.commandReceived == 1 && ((strncmp(ftpData.clients[theSocketId].pasvData.theCommandReceived, "LIST", strlen("LIST")) == 0) || (strncmp(ftpData.clients[theSocketId].pasvData.theCommandReceived, "list", strlen("list")) == 0)) @@ -240,6 +241,9 @@ void *pasvThreadHandler(void * socketId) ((strncmp(ftpData.clients[theSocketId].pasvData.theCommandReceived, "RETR", strlen("RETR")) == 0) || (strncmp(ftpData.clients[theSocketId].pasvData.theCommandReceived, "retr", strlen("retr")) == 0))) { + //Set the busy flag + ftpData.clients[theSocketId].pasvData.threadIsBusy = 1; + int theFileSize = 0; int writenSize = 0; char theResponse[FTP_COMMAND_ELABORATE_CHAR_BUFFER]; @@ -342,7 +346,7 @@ void runFtpServer(void) FD_SET(ftpData.theSocket, &rset); FD_SET(ftpData.theSocket, &wset); FD_SET(ftpData.theSocket, &eset); - + maxSocketFD = ftpData.theSocket+1; //Endless loop ftp process @@ -683,13 +687,13 @@ static int processCommand(int processingElement) strncmp(ftpData.clients[processingElement].theCommandReceived, "abor", strlen("abor")) == 0) { printf("\nABOR command received"); - //To implement + toReturn = parseCommandAbor(&ftpData, processingElement); } else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 0 || strncmp(ftpData.clients[processingElement].theCommandReceived, "dele", strlen("dele")) == 0) { printf("\nDELE command received"); - //To implement + toReturn = parseCommandDele(&ftpData.clients[processingElement]); } else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 0 || strncmp(ftpData.clients[processingElement].theCommandReceived, "mtdm", strlen("mtdm")) == 0) @@ -726,18 +730,24 @@ static int processCommand(int processingElement) { printf("\nRNFR command received"); //To implement + //350 RNFR accepted - file exists, ready for destination + //550 Sorry, but that file doesn't exist } else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RNTO", strlen("RNTO")) == 0 || strncmp(ftpData.clients[processingElement].theCommandReceived, "rnto", strlen("rnto")) == 0) { printf("\nRNTO command received"); //To implement + //503 Need RNFR before RNTO + //250 File successfully renamed or moved } else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SIZE", strlen("SIZE")) == 0 || strncmp(ftpData.clients[processingElement].theCommandReceived, "size", strlen("size")) == 0) { printf("\nSIZE command received"); //To implement + //213 71 + //550 Can't check for file existence } else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "APPE", strlen("APPE")) == 0 || strncmp(ftpData.clients[processingElement].theCommandReceived, "appe", strlen("appe")) == 0) @@ -750,7 +760,8 @@ static int processCommand(int processingElement) { printf("\nNOOP command received"); //To implement - } + //200 Zzz... + } //REST COMMAND //REST 0 //350 Restarting at 0 diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index cc11043..433c9d9 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -40,6 +40,7 @@ displayName="Important Files" projectFiles="false" kind="IMPORTANT_FILES_FOLDER"> + MakeSimple Makefile diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index d3f4e55..08241a0 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -7,11 +7,12 @@ - file:/home/ugo/NetBeansProjects/uFTP/library/fileManagement.c + file:/home/ugo/NetBeansProjects/uFTP/ftpData.c file:/home/ugo/NetBeansProjects/uFTP/ftpCommandElaborate.c - file:/home/ugo/NetBeansProjects/uFTP/library/logFunctions.c file:/home/ugo/NetBeansProjects/uFTP/ftpServer.c file:/home/ugo/NetBeansProjects/uFTP/uFTP.c + file:/home/ugo/NetBeansProjects/uFTP/ftpData.h + file:/home/ugo/NetBeansProjects/uFTP/ftpCommandsElaborate.h