diff --git a/build/Debug/GNU-Linux/ftpCommandElaborate.o b/build/Debug/GNU-Linux/ftpCommandElaborate.o index e5310df..c8addf5 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 5c700f9..1172127 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 a0de2f9..bcb3641 100644 Binary files a/build/Debug/GNU-Linux/ftpServer.o and b/build/Debug/GNU-Linux/ftpServer.o differ diff --git a/build/Debug/GNU-Linux/library/configRead.o b/build/Debug/GNU-Linux/library/configRead.o index 08ab09a..b57ac08 100644 Binary files a/build/Debug/GNU-Linux/library/configRead.o and b/build/Debug/GNU-Linux/library/configRead.o differ diff --git a/build/Debug/GNU-Linux/library/connection.o b/build/Debug/GNU-Linux/library/connection.o index 77b6ad2..afca7a9 100644 Binary files a/build/Debug/GNU-Linux/library/connection.o and b/build/Debug/GNU-Linux/library/connection.o differ diff --git a/build/Debug/GNU-Linux/library/daemon.o b/build/Debug/GNU-Linux/library/daemon.o index 58cb4ff..1ca7643 100644 Binary files a/build/Debug/GNU-Linux/library/daemon.o and b/build/Debug/GNU-Linux/library/daemon.o differ diff --git a/build/Debug/GNU-Linux/uFTP.o b/build/Debug/GNU-Linux/uFTP.o index 5675003..5bd4a41 100644 Binary files a/build/Debug/GNU-Linux/uFTP.o and b/build/Debug/GNU-Linux/uFTP.o differ diff --git a/build/modules/configRead.o b/build/modules/configRead.o index 6644500..c477cb4 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 2b63140..7dc6a39 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/daemon.o b/build/modules/daemon.o index 66c893e..16ee921 100644 Binary files a/build/modules/daemon.o and b/build/modules/daemon.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index 820a57b..5002542 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 83f8e9a..bcd931b 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 88b5956..e41bfbe 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/uFTP b/build/uFTP index 835d037..d993b7c 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/dist/Debug/GNU-Linux/uftp b/dist/Debug/GNU-Linux/uftp index db980fc..739d1d4 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 d4d0527..bca58e3 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -50,15 +50,12 @@ int parseCommandUser(clientDataType *theClientData) char *theName; theName = getFtpCommandArg("USER", theClientData->theCommandReceived); - printTimeStamp(); - printf("The Name: %s", theName); - if (strlen(theName) >= 1) { char *theResponse = "331 User ok, Waiting for the password.\r\n"; setDynamicStringDataType(&theClientData->login.name, theName, strlen(theName)); write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); - printf("\nUSER COMMAND OK, USERNAME IS: %s", theClientData->login.name.text); + //printf("\nUSER COMMAND OK, USERNAME IS: %s", theClientData->login.name.text); return 1; } else @@ -75,10 +72,7 @@ int parseCommandSite(clientDataType *theClientData) char *theCommand; theCommand = getFtpCommandArg("SITE", theClientData->theCommandReceived); - printTimeStamp(); - printf("\ntheCommand:%s", theCommand); - if(strncmp(theCommand, "CHMOD", strlen("CHMOD")) == 0 || - strncmp(theCommand, "chmod", strlen("chmod")) == 0) + if(compareStringCaseInsensitive(theCommand, "CHMOD", strlen("CHMOD")) == 1) { setPermissions(theCommand, theClientData->login.absolutePath.text); char *theResponse = "200 Permissions changed\r\n"; @@ -89,21 +83,18 @@ int parseCommandSite(clientDataType *theClientData) char *theResponse = "500 unknown extension\r\n"; write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); } - - - return 1; + //site chmod 777 test //200 Permissions changed on test - //500 SITE ciao is an unknown extension + //500 SITE ciao is an unknown extension + + return 1; } int parseCommandPass(ftpDataType * data, int socketId) { char *thePass; thePass = getFtpCommandArg("PASS", data->clients[socketId].theCommandReceived); - - printTimeStamp(); - printf("The Pass is: %s", thePass); if (strlen(thePass) >= 1) { @@ -115,7 +106,7 @@ int parseCommandPass(ftpDataType * data, int socketId) { char *theResponse = "430 Invalid username or password\r\n"; write(data->clients[socketId].socketDescriptor, theResponse, strlen(theResponse)); - printf("\nLogin Error recorded no such username or password"); + //printf("\nLogin Error recorded no such username or password"); } else { @@ -129,12 +120,12 @@ int parseCommandPass(ftpDataType * data, int socketId) write(data->clients[socketId].socketDescriptor, theResponse, strlen(theResponse)); printTimeStamp(); - printf("PASS COMMAND OK, PASSWORD IS: %s", data->clients[socketId].login.password.text); - printf("\nheClientData->login.homePath: %s", data->clients[socketId].login.homePath.text); - printf("\nheClientData->login.ftpPath: %s", data->clients[socketId].login.ftpPath.text); - printf("\nheClientData->login.absolutePath: %s", data->clients[socketId].login.absolutePath.text); + //printf("PASS COMMAND OK, PASSWORD IS: %s", data->clients[socketId].login.password.text); + //printf("\nheClientData->login.homePath: %s", data->clients[socketId].login.homePath.text); + //printf("\nheClientData->login.ftpPath: %s", data->clients[socketId].login.ftpPath.text); + //printf("\nheClientData->login.absolutePath: %s", data->clients[socketId].login.absolutePath.text); } - + return 1; } else @@ -147,10 +138,10 @@ int parseCommandPass(ftpDataType * data, int socketId) int parseCommandAuth(clientDataType *theClientData) { - char *theAuth; - theAuth = getFtpCommandArg("AUTH", theClientData->theCommandReceived); - printTimeStamp(); - printf("The Auth is: %s", theAuth); + //char *theAuth; + //theAuth = getFtpCommandArg("AUTH", theClientData->theCommandReceived); + //printTimeStamp(); + //printf("The Auth is: %s", theAuth); char *theResponse = "502 Security extensions not implemented.\r\n"; write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); return 1; @@ -239,7 +230,7 @@ int parseCommandPasv(ftpDataType * data, int socketId) void *pReturn; pthread_cancel(data->clients[socketId].workerData.workerThread); pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); - printf("\nThread has been cancelled."); + //printf("\nThread has been cancelled."); } else { @@ -261,11 +252,11 @@ int parseCommandPort(ftpDataType * data, int socketId) int portBytes[2]; theIpAndPort = getFtpCommandArg("PORT", data->clients[socketId].theCommandReceived); sscanf(theIpAndPort, "%d,%d,%d,%d,%d,%d", &ipAddressBytes[0], &ipAddressBytes[1], &ipAddressBytes[2], &ipAddressBytes[3], &portBytes[0], &portBytes[1]); - printf("\ntheIpAndPort: %s", theIpAndPort); + //printf("\ntheIpAndPort: %s", theIpAndPort); data->clients[socketId].workerData.connectionPort = (portBytes[0]*256)+portBytes[1]; sprintf(data->clients[socketId].workerData.activeIpAddress, "%d.%d.%d.%d", ipAddressBytes[0],ipAddressBytes[1],ipAddressBytes[2],ipAddressBytes[3]); - printf("\ndata->clients[socketId].workerData.connectionPort: %d", data->clients[socketId].workerData.connectionPort); - printf("\ndata->clients[socketId].workerData.activeIpAddress: %s", data->clients[socketId].workerData.activeIpAddress); + //printf("\ndata->clients[socketId].workerData.connectionPort: %d", data->clients[socketId].workerData.connectionPort); + //printf("\ndata->clients[socketId].workerData.activeIpAddress: %s", data->clients[socketId].workerData.activeIpAddress); /* Create worker thread */ if (data->clients[socketId].workerData.threadIsAlive == 1) @@ -273,7 +264,7 @@ int parseCommandPort(ftpDataType * data, int socketId) void *pReturn; pthread_cancel(data->clients[socketId].workerData.workerThread); pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); - printf("\nThread has been cancelled."); + //printf("\nThread has been cancelled."); } else { @@ -306,7 +297,7 @@ int parseCommandAbor(ftpDataType * data, int socketId) void *pReturn; pthread_cancel(data->clients[socketId].workerData.workerThread); pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); - printf("\nThread has been cancelled due to ABOR request."); + //printf("\nThread has been cancelled due to ABOR request."); memset(theResponse, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); sprintf(theResponse, "426 ABORT\r\n"); @@ -345,17 +336,14 @@ int parseCommandList(ftpDataType * data, int socketId) -t Sort by modification time */ - - - int isSafePath = 0; char *theNameToList; theNameToList = getFtpCommandArg("LIST", data->clients[socketId].theCommandReceived); getFtpCommandArgWithOptions("LIST", data->clients[socketId].theCommandReceived, &data->clients[socketId].workerData.ftpCommand); - printf("\nLIST COMMAND ARG: %s", data->clients[socketId].workerData.ftpCommand.commandArgs.text); - printf("\nLIST COMMAND OPS: %s", data->clients[socketId].workerData.ftpCommand.commandOps.text); + // printf("\nLIST COMMAND ARG: %s", data->clients[socketId].workerData.ftpCommand.commandArgs.text); + // printf("\nLIST COMMAND OPS: %s", data->clients[socketId].workerData.ftpCommand.commandOps.text); cleanDynamicStringDataType(&data->clients[socketId].workerData.ftpCommand.commandArgs, 0); cleanDynamicStringDataType(&data->clients[socketId].workerData.ftpCommand.commandOps, 0); @@ -454,7 +442,7 @@ int parseCommandStor(ftpDataType * data, int socketId) if (isSafePath == 1) { pthread_mutex_lock(&data->clients[socketId].workerData.conditionMutex); - printf("data->clients[%d].fileToStor = %s", socketId, data->clients[socketId].fileToStor.text); + //printf("data->clients[%d].fileToStor = %s", socketId, data->clients[socketId].fileToStor.text); 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; @@ -529,7 +517,7 @@ int parseCommandCwd(clientDataType *theClientData) if (FILE_IsDirectory(theClientData->login.absolutePath.text) == 1) { - printf("\nDirectory ok found, theClientData->login.ftpPath.text = %s", theClientData->login.ftpPath.text); + //printf("\nDirectory ok found, theClientData->login.ftpPath.text = %s", theClientData->login.ftpPath.text); int theSizeToMalloc = strlen("250 OK. Current directory is ")+theClientData->login.ftpPath.textLen+1; theResponse = (char *) malloc (theSizeToMalloc); memset(theResponse, 0, theSizeToMalloc); @@ -538,7 +526,7 @@ int parseCommandCwd(clientDataType *theClientData) } else { - printf("\n%s does not exist", theClientData->login.absolutePath.text); + //printf("\n%s does not exist", theClientData->login.absolutePath.text); setDynamicStringDataType(&theClientData->login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen); setDynamicStringDataType(&theClientData->login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen); int theSizeToMalloc = strlen("550 Can't change directory to : No such file or directory")+10+strlen(theClientData->login.ftpPath.text); @@ -553,9 +541,9 @@ int parseCommandCwd(clientDataType *theClientData) write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); printTimeStamp(); - printf("\nCwd response : %s", theResponse); - printf("\nUSER COMMAND OK, NEW PATH IS: %s", theClientData->login.absolutePath.text); - printf("\nUSER COMMAND OK, NEW LOCAL PATH IS: %s", theClientData->login.ftpPath.text); + //printf("\nCwd response : %s", theResponse); + //printf("\nUSER COMMAND OK, NEW PATH IS: %s", theClientData->login.absolutePath.text); + //printf("\nUSER COMMAND OK, NEW LOCAL PATH IS: %s", theClientData->login.ftpPath.text); cleanDynamicStringDataType(&absolutePathPrevious, 0); cleanDynamicStringDataType(&ftpPathPrevious, 0); @@ -608,9 +596,6 @@ int parseCommandRest(clientDataType *theClientData) } } - printTimeStamp(); - printf(" REST set to: %s", theSize); - fflush(0); FILE_AppendToString(&theResponse, theSize); FILE_AppendToString(&theResponse, " \r\n"); @@ -627,7 +612,7 @@ int parseCommandMkd(clientDataType *theClientData) char *theDirectoryFilename; dynamicStringDataType theResponse; dynamicStringDataType mkdFileName; - + theDirectoryFilename = getFtpCommandArg("MKD", theClientData->theCommandReceived); cleanDynamicStringDataType(&theResponse, 1); @@ -638,7 +623,7 @@ int parseCommandMkd(clientDataType *theClientData) if (isSafePath == 1) { int returnStatus; - printf("\nThe directory to make is: %s", mkdFileName.text); + //printf("\nThe directory to make is: %s", mkdFileName.text); returnStatus = mkdir(mkdFileName.text, S_IRWXU | S_IRWXG | S_IRWXO); setDynamicStringDataType(&theResponse, "257 \"", strlen("257 \"")); appendToDynamicStringDataType(&theResponse, theDirectoryFilename, strlen(theDirectoryFilename)); @@ -659,9 +644,9 @@ int parseCommandMkd(clientDataType *theClientData) int parseCommandOpts(clientDataType *theClientData) { - char *theCommand; - theCommand = getFtpCommandArg("OPTS", theClientData->theCommandReceived); - printf("\nThe command received: %s", theCommand); + //char *theCommand; + //theCommand = getFtpCommandArg("OPTS", theClientData->theCommandReceived); + //printf("\nThe command received: %s", theCommand); char *theResponse = "200 OK\r\n"; write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); return 1; @@ -684,7 +669,7 @@ int parseCommandDele(clientDataType *theClientData) if (isSafePath == 1) { - printf("\nThe file to delete is: %s", deleFileName.text); + //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)); @@ -744,7 +729,7 @@ int parseCommandRmd(clientDataType *theClientData) if (isSafePath == 1) { - printf("\nThe directory to delete is: %s", rmdFileName.text); + //printf("\nThe directory to delete is: %s", rmdFileName.text); returnStatus = rmdir(rmdFileName.text); setDynamicStringDataType(&theResponse, "250 The directory was successfully removed\r\n", strlen("250 The directory was successfully removed\r\n")); write(theClientData->socketDescriptor, theResponse.text, theResponse.textLen); @@ -780,7 +765,7 @@ int parseCommandSize(clientDataType *theClientData) if (isSafePath == 1) { - printf("\nThe file to get the size is: %s", getSizeFromFileName.text); + // printf("\nThe file to get the size is: %s", getSizeFromFileName.text); if (FILE_IsFile(getSizeFromFileName.text)==1) { @@ -824,7 +809,7 @@ int parseCommandRnfr(clientDataType *theClientData) (FILE_IsFile(theClientData->renameFromFile.text) == 1 || FILE_IsDirectory(theClientData->renameFromFile.text) == 1)) { - printf("\nThe file to check is: %s", theClientData->renameFromFile.text); + // printf("\nThe file to check is: %s", theClientData->renameFromFile.text); if (FILE_IsFile(theClientData->renameFromFile.text) == 1 || FILE_IsDirectory(theClientData->renameFromFile.text) == 1) @@ -862,7 +847,7 @@ int parseCommandRnto(clientDataType *theClientData) if (isSafePath == 1 && theClientData->renameFromFile.textLen > 0) { - printf("\nThe file to check is: %s", theClientData->renameFromFile.text); + // printf("\nThe file to check is: %s", theClientData->renameFromFile.text); if (FILE_IsFile(theClientData->renameFromFile.text) == 1 || FILE_IsDirectory(theClientData->renameFromFile.text) == 1) @@ -913,9 +898,9 @@ int parseCommandCdup(clientDataType *theClientData) FILE_AppendToString(&theResponse, " \r\n"); write(theClientData->socketDescriptor, theResponse, strlen(theResponse)); - printTimeStamp(); - printf("USER COMMAND OK, NEW PATH IS: %s", theClientData->login.absolutePath.text); - printf("\nUSER COMMAND OK, NEW LOCAL PATH IS: %s", theClientData->login.ftpPath.text); + // printTimeStamp(); + // printf("USER COMMAND OK, NEW PATH IS: %s", theClientData->login.absolutePath.text); + //printf("\nUSER COMMAND OK, NEW LOCAL PATH IS: %s", theClientData->login.ftpPath.text); free(theResponse); return 1; } @@ -929,7 +914,7 @@ int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom long long int theFileSize; char buffer[FTP_COMMAND_ELABORATE_CHAR_BUFFER]; - printf("\nOpening: %s", theFilename); + //printf("\nOpening: %s", theFilename); retrFP = fopen(theFilename, "rb"); if (retrFP == NULL) @@ -942,9 +927,9 @@ int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom if (startFrom > 0) { - printf("\nSeek startFrom: %d", startFrom); + //printf("\nSeek startFrom: %d", startFrom); currentPosition = (long int) lseek(fileno(retrFP), startFrom, SEEK_SET); - printf("\nSeek result: %ld", currentPosition); + // printf("\nSeek result: %ld", currentPosition); if (currentPosition == -1) { fclose(retrFP); @@ -957,7 +942,7 @@ int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom toReturn = toReturn + write(thePasvSocketConnection, buffer, readen); } - printf("\n Bytes written: %ld", toReturn); + //printf("\n Bytes written: %ld", toReturn); fclose(retrFP); return toReturn; @@ -1066,7 +1051,6 @@ int getFtpCommandArgWithOptions(char * theCommand, char *theCommandString, ftpCo setDynamicStringDataType(&ftpCommand->commandOps, argSecondary, argSecondaryIndex); return 1; - } int setPermissions(char * permissionsCommand, char * basePath) @@ -1118,15 +1102,15 @@ int setPermissions(char * permissionsCommand, char * basePath) permissionsCommandCursor++; } - printf("\n thePermissionString = %s ", thePermissionString); - printf("\n theLocalPathCursor = %s ", theLocalPath); + //printf("\n thePermissionString = %s ", thePermissionString); + //printf("\n theLocalPathCursor = %s ", theLocalPath); if (basePath[strlen(basePath)-1] != '/') sprintf(theFinalCommand, "chmod %s %s/%s", thePermissionString, basePath, theLocalPath); else sprintf(theFinalCommand, "chmod %s %s%s", thePermissionString, basePath, theLocalPath); - printf("\n theFinalCommand = %s ", theFinalCommand); + //printf("\n theFinalCommand = %s ", theFinalCommand); system(theFinalCommand); diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 6404ed7..e366e92 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -23,20 +23,6 @@ */ - -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -/* - * File: ftpCommandsElaborate.h - * Author: ugo - * - * Created on 8 ottobre 2017, 12.40 - */ - #ifndef FTPCOMMANDSELABORATE_H #define FTPCOMMANDSELABORATE_H diff --git a/ftpData.c b/ftpData.c index af58a29..89b3de1 100644 --- a/ftpData.c +++ b/ftpData.c @@ -178,14 +178,12 @@ int getSafePath(dynamicStringDataType *safePath, char *theDirectoryName, loginDa void appendToDynamicStringDataType(dynamicStringDataType *dynamicString, char *theString, int stringLen) { - printf("\n Appending in %s --> %s", dynamicString->text, theString); int theNewSize = dynamicString->textLen + stringLen; dynamicString->text = realloc(dynamicString->text, theNewSize + 1); memset(dynamicString->text+dynamicString->textLen, 0, stringLen+1); memcpy(dynamicString->text+dynamicString->textLen, theString, stringLen); dynamicString->text[theNewSize] = '\0'; dynamicString->textLen = theNewSize; - printf("\n Append done --> %s", dynamicString->text); } void setRandomicPort(ftpDataType *data, int socketPosition) @@ -193,7 +191,6 @@ void setRandomicPort(ftpDataType *data, int socketPosition) static unsigned short int randomizeInteger = 0; unsigned short int randomicPort = 5000; int i; - time_t t; randomizeInteger += 7; @@ -206,13 +203,12 @@ void setRandomicPort(ftpDataType *data, int socketPosition) randomicPort = ((rand() + socketPosition + randomizeInteger) % (10000 - 50000)) + 10000; i = 0; - printf("randomicPort = %d", randomicPort); + while (i < data->ftpParameters.maxClients) { if (randomicPort == data->clients[i].workerData.connectionPort) { - printf("randomicPort already in use = %d", randomicPort); randomicPort = ((rand() + socketPosition + i + randomizeInteger) % (10000 - 50000)) + 10000; i = 0; } @@ -262,7 +258,7 @@ void getListDataInfo(char * thePath, DYNV_VectorGenericDataType *directoryInfo) } if (data.isDirectory == 0 && data.isFile == 0) { - printf("\nNot a directory, not a file, broken link"); + //printf("\nNot a directory, not a file, broken link"); continue; } @@ -344,58 +340,36 @@ void deleteListDataInfoVector(void *TheElementToDelete) { ftpListDataType *data = (ftpListDataType *)TheElementToDelete; - - - if (data->owner != NULL) { - printf("\nDeleting data->owner:%s", data->owner); - fflush(0); free(data->owner); } - printf("\nDone"); - fflush(0); if (data->groupOwner != NULL) { - printf("\nDeleting data->groupOwner:%s", data->groupOwner); - fflush(0); free(data->groupOwner); } - printf("\nDone"); - fflush(0); + if (data->inodePermissionString != NULL) { - printf("\nDeleting data->inodePermissionString:%s", data->inodePermissionString); - fflush(0); free(data->inodePermissionString); } - printf("\nDone"); - fflush(0); + if (data->fileNameWithPath != NULL) { - printf("\nDeleting data->fileNameWithPath:%s", data->fileNameWithPath); - fflush(0); free(data->fileNameWithPath); } - printf("\nDone"); - fflush(0); + if (data->finalStringPath != NULL) { - printf("\nDeleting data->finalStringPath:%s", data->finalStringPath); - fflush(0); free(data->finalStringPath); } - printf("\nDone"); - fflush(0); + if (data->linkPath != NULL) { - printf("\nDeleting data->linkPath:%s", data->linkPath); - fflush(0); free(data->linkPath); } - printf("\nDone"); - fflush(0); + } void resetWorkerData(workerDataType *workerData, int isInitialization) @@ -444,7 +418,7 @@ void resetClientData(clientDataType *clientData, int isInitialization) void *pReturn; pthread_cancel(clientData->workerData.workerThread); pthread_join(clientData->workerData.workerThread, &pReturn); - printf("\nThread has been cancelled."); + //printf("\nThread has been cancelled."); } else { @@ -492,4 +466,55 @@ void resetClientData(clientDataType *clientData, int isInitialization) clientData->lastActivityTimeStamp = 0; +} + +int compareStringCaseInsensitive(char * stringIn, char * stringRef, int stringLenght) +{ + int i = 0; + char * alfaLowerCase = "qwertyuiopasdfghjklzxcvbnm "; + char * alfaUpperCase = "QWERTYUIOPASDFGHJKLZXCVBNM "; + + int stringInIndex; + int stringRefIndex; + + for (i = 0; i 0) @@ -509,8 +430,7 @@ void runFtpServer(void) if (commandProcessStatus == 0) { char * theNotSupportedString = "500 Unknown command\r\n"; - //write(ftpData.clients[processingSock].socketDescriptor, ftpData.clients[processingSock].buffer, ftpData.clients[processingSock].bufferIndex); - //write(ftpData.clients[processingSock].socketDescriptor, theNotSupportedString, strlen(theNotSupportedString)); + write(ftpData.clients[processingSock].socketDescriptor, theNotSupportedString, strlen(theNotSupportedString)); printf("\n COMMAND NOT SUPPORTED ********* %s", ftpData.clients[processingSock].buffer); } else @@ -522,9 +442,11 @@ void runFtpServer(void) else { //Command overflow can't be processed + char * theNotSupportedString = "500 Unknown command\r\n"; ftpData.clients[processingSock].commandIndex = 0; memset(ftpData.clients[processingSock].theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); //Write some error message to the client + write(ftpData.clients[processingSock].socketDescriptor, theNotSupportedString, strlen(theNotSupportedString)); break; } } @@ -542,320 +464,201 @@ void runFtpServer(void) return; } - int createPassiveSocket(int port) -{ - int sock, returnCode, flags; - struct sockaddr_in temp; - - //Socket creation - sock = socket(AF_INET, SOCK_STREAM, 0); - temp.sin_family = AF_INET; - temp.sin_addr.s_addr = INADDR_ANY; - temp.sin_port = htons(port); - - - int reuse = 1; - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) - perror("setsockopt(SO_REUSEADDR) failed"); - - if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) - perror("setsockopt(SO_REUSEPORT) failed"); - - - //Bind socket - returnCode = bind(sock,(struct sockaddr*) &temp,sizeof(temp)); - - //Number of client allowed - returnCode = listen(sock, 1); - - return sock; -} - -int createActiveSocket(int port, char *ipAddress) -{ - int sockfd; - struct sockaddr_in serv_addr; - struct hostent *hostnm; /* server host name information */ - - memset(&serv_addr, 0, sizeof(struct sockaddr_in)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - if(inet_pton(AF_INET, ipAddress, &serv_addr.sin_addr)<=0) - { - printf("\n inet_pton error occured\n"); - return -1; - } - - if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - printf("\n Error : Could not create socket \n"); - return -1; - } - else - { - printf("\n sockfd = %d \n", sockfd); - } - - - int reuse = 1; - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) - perror("setsockopt(SO_REUSEADDR) failed"); - - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) - perror("setsockopt(SO_REUSEPORT) failed"); - - - - if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) - { - printf("\n Error : Connect Failed \n"); - return -1; - } - - printf("\n Connection socket %d is going to start ip: %s:%d \n",sockfd, ipAddress, port); - - return sockfd; -} - static int processCommand(int processingElement) { int toReturn = 0; printTimeStamp(); printf ("Command received from (%d): %s", processingElement, ftpData.clients[processingElement].theCommandReceived); - //printf ("\nstrncmp(ftpData.clients[processingElement].theCommandReceived, \"USER\", strlen(\"USER\")) %d", strncmp(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER"))); - //printf ("\nstrncmp(ftpData.clients[processingElement].theCommandReceived, \"user\", strlen(\"user\")) %d", strncmp(ftpData.clients[processingElement].theCommandReceived, "user", strlen("user"))); - //printf ("\nstrncmp(ftpData.clients[processingElement].theCommandReceived, \"PASS\", strlen(\"PASS\")) %d", strncmp(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS"))); - //printf ("\nstrncmp(ftpData.clients[processingElement].theCommandReceived, \"pass\", strlen(\"pass\")) %d", strncmp(ftpData.clients[processingElement].theCommandReceived, "pass", strlen("pass"))); - cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandArgs, 0); cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandOps, 0); if (ftpData.clients[processingElement].login.userLoggedIn == 0 && - (strncmp(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "user", strlen("user")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "pass", strlen("pass")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "quit", strlen("quit")) != 0) && - strncmp(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) != 0 && - strncmp(ftpData.clients[processingElement].theCommandReceived, "auth", strlen("auth")) != 0) + (compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) != 1 && + compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) != 1 && + compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) != 1 && + compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) != 1)) { toReturn = notLoggedInMessage(&ftpData.clients[processingElement]); + ftpData.clients[processingElement].commandIndex = 0; + memset(ftpData.clients[processingElement].theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); return 1; } - - + //Process Command - if(strncmp(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "user", strlen("user")) == 0) + if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) == 1) { printf("\nUSER COMMAND RECEIVED"); toReturn = parseCommandUser(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "pass", strlen("pass")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) == 1) { printf("\nPASS COMMAND RECEIVED"); toReturn = parseCommandPass(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SITE", strlen("SITE")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "site", strlen("site")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SITE", strlen("SITE")) == 1) { printf("\nSITE COMMAND RECEIVED"); toReturn = parseCommandSite(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "auth", strlen("auth")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) == 1) { printf("\nAUTH COMMAND RECEIVED"); toReturn = parseCommandAuth(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PWD", strlen("PWD")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "pwd", strlen("pwd")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PWD", strlen("PWD")) == 1) { printf("\nPWD COMMAND RECEIVED"); toReturn = parseCommandPwd(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SYST", strlen("SYST")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "syst", strlen("syst")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SYST", strlen("SYST")) == 1) { printf("\nSYST COMMAND RECEIVED"); toReturn = parseCommandSyst(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "FEAT", strlen("FEAT")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "feat", strlen("feat")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "FEAT", strlen("FEAT")) == 1) { printf("\nFEAT COMMAND RECEIVED"); toReturn = parseCommandFeat(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "TYPE I", strlen("TYPE I")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "type i", strlen("type i")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "TYPE I", strlen("TYPE I")) == 1) { printf("\nTYPE I COMMAND RECEIVED"); toReturn = parseCommandTypeI(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "STRU F", strlen("STRU F")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "stru f", strlen("stru f")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "STRU F", strlen("STRU F")) == 1) { printf("\nTYPE I COMMAND RECEIVED"); toReturn = parseCommandStruF(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MODE S", strlen("TMODE S")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "mode s", strlen("mode s")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MODE S", strlen("MODE S")) == 1) { printf("\nMODE S COMMAND RECEIVED"); toReturn = parseCommandModeS(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "TYPE A", strlen("TYPE A")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "type a", strlen("type a")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "TYPE A", strlen("TYPE A")) == 1) { printf("\nTYPE A COMMAND RECEIVED"); toReturn = parseCommandTypeI(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PASV", strlen("PASV")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "pasv", strlen("pasv")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PASV", strlen("PASV")) == 1) { printf("\nPASV COMMAND RECEIVED"); setRandomicPort(&ftpData, processingElement); toReturn = parseCommandPasv(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PORT", strlen("PORT")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "port", strlen("port")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PORT", strlen("PORT")) == 1) { printf("\nPORT COMMAND RECEIVED"); toReturn = parseCommandPort(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "LIST", strlen("LIST")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "list", strlen("list")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "LIST", strlen("LIST")) == 1) { printf("\nLIST COMMAND RECEIVED"); toReturn = parseCommandList(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "CWD", strlen("CWD")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "cwd", strlen("cwd")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "CWD", strlen("CWD")) == 1) { printf("\nCWD COMMAND RECEIVED"); toReturn = parseCommandCwd(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "CDUP", strlen("CDUP")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "cdup", strlen("cdup")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "CDUP", strlen("CDUP")) == 1) { printf("\nCDUP COMMAND RECEIVED"); toReturn = parseCommandCdup(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "REST", strlen("REST")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "rest", strlen("rest")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "REST", strlen("REST")) == 1) { printf("\nREST COMMAND RECEIVED"); toReturn = parseCommandRest(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RETR", strlen("RETR")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "retr", strlen("retr")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RETR", strlen("RETR")) == 1) { printf("\nRETR COMMAND RECEIVED"); toReturn = parseCommandRetr(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "STOR", strlen("STOR")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "stor", strlen("stor")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "STOR", strlen("STOR")) == 1) { printf("\nSTOR COMMAND RECEIVED"); toReturn = parseCommandStor(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MKD", strlen("MKD")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "mkd", strlen("mkd")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MKD", strlen("MKD")) == 1) { printf("\nMKD command received"); toReturn = parseCommandMkd(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "ABOR", strlen("ABOR")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "abor", strlen("abor")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "ABOR", strlen("ABOR")) == 1) { printf("\nABOR command received"); toReturn = parseCommandAbor(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "dele", strlen("dele")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 1) { printf("\nDELE command received"); toReturn = parseCommandDele(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "opts", strlen("opts")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 1) { printf("\nOPTS command received"); toReturn = parseCommandOpts(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "mtdm", strlen("mtdm")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 1) { printf("\nMTDM command received"); //To implement } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "nlst", strlen("nlst")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 1) { printf("\nNLST command received"); toReturn = parseCommandNlst(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PORT", strlen("PORT")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "port", strlen("port")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PORT", strlen("PORT")) == 1) { printf("\nPORT command received"); //To implement } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "quit", strlen("quit")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) == 1) { printf("\nQUIT command received"); toReturn = parseCommandQuit(&ftpData, processingElement); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RMD", strlen("RMD")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "rmd", strlen("rmd")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RMD", strlen("RMD")) == 1) { printf("\nRMD command received"); toReturn = parseCommandRmd(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RNFR", strlen("RNFR")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "rnfr", strlen("rnfr")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RNFR", strlen("RNFR")) == 1) { printf("\nRNFR command received"); toReturn = parseCommandRnfr(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RNTO", strlen("RNTO")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "rnto", strlen("rnto")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RNTO", strlen("RNTO")) == 1) { printf("\nRNTO command received"); toReturn = parseCommandRnto(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SIZE", strlen("SIZE")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "size", strlen("size")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SIZE", strlen("SIZE")) == 1) { printf("\nSIZE command received"); toReturn = parseCommandSize(&ftpData.clients[processingElement]); } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "APPE", strlen("APPE")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "appe", strlen("appe")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "APPE", strlen("APPE")) == 1) { printf("\nAPPE command received"); //To implement } - else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "NOOP", strlen("NOOP")) == 0 || - strncmp(ftpData.clients[processingElement].theCommandReceived, "noop", strlen("noop")) == 0) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NOOP", strlen("NOOP")) == 1) { printf("\nNOOP command received"); toReturn = parseCommandNoop(&ftpData.clients[processingElement]); } else { - //Parse unsupported command - + ; //Parse unsupported command not needed } ftpData.clients[processingElement].commandIndex = 0; memset(ftpData.clients[processingElement].theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); - return toReturn; } - diff --git a/ftpServer.h b/ftpServer.h index 01153eb..67c7c28 100644 --- a/ftpServer.h +++ b/ftpServer.h @@ -23,20 +23,6 @@ */ - -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -/* - * File: ftpServer.h - * Author: ugo - * - * Created on 7 ottobre 2017, 17.37 - */ - #ifndef FTPSERVER_H #define FTPSERVER_H @@ -44,8 +30,7 @@ void runFtpServer(void); -int createPassiveSocket(int port); -int createActiveSocket(int port, char *ipAddress); + void *connectionWorkerHandle(void * socketId); void workerCleanup(void *socketId); diff --git a/library/connection.c b/library/connection.c index 4e55d44..c51c737 100644 --- a/library/connection.c +++ b/library/connection.c @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2018 ugo. + * Copyright 2018 Ugo Cirmignani. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,14 +29,13 @@ #include #include #include - -#include "../ftpData.h" -#include "connection.h" +#include +#include #include #include - - +#include "../ftpData.h" +#include "connection.h" /* Return the higher socket available*/ int getMaximumSocketFd(int mainSocket, ftpDataType * ftpData) @@ -50,7 +49,8 @@ int getMaximumSocketFd(int mainSocket, ftpDataType * ftpData) toReturn = ftpData->clients[i].socketDescriptor; } } - + //Must be incremented by one + toReturn++; return toReturn; } @@ -85,6 +85,80 @@ int createSocket(ftpDataType * ftpData) return sock; } +int createPassiveSocket(int port) +{ + int sock, returnCode; + struct sockaddr_in temp; + + //Socket creation + sock = socket(AF_INET, SOCK_STREAM, 0); + temp.sin_family = AF_INET; + temp.sin_addr.s_addr = INADDR_ANY; + temp.sin_port = htons(port); + + + int reuse = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) + perror("setsockopt(SO_REUSEADDR) failed"); + + if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) + perror("setsockopt(SO_REUSEPORT) failed"); + + + //Bind socket + returnCode = bind(sock,(struct sockaddr*) &temp,sizeof(temp)); + + //Number of client allowed + returnCode = listen(sock, 1); + + return sock; +} + +int createActiveSocket(int port, char *ipAddress) +{ + int sockfd; + struct sockaddr_in serv_addr; + + memset(&serv_addr, 0, sizeof(struct sockaddr_in)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + if(inet_pton(AF_INET, ipAddress, &serv_addr.sin_addr)<=0) + { + printf("\n inet_pton error occured\n"); + return -1; + } + + if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("\n Error : Could not create socket \n"); + return -1; + } + else + { + printf("\n sockfd = %d \n", sockfd); + } + + + int reuse = 1; + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) + perror("setsockopt(SO_REUSEADDR) failed"); + + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0) + perror("setsockopt(SO_REUSEPORT) failed"); + + + + if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("\n Error : Connect Failed \n"); + return -1; + } + + printf("\n Connection socket %d is going to start ip: %s:%d \n",sockfd, ipAddress, port); + + return sockfd; +} + void fdInit(ftpDataType * ftpData) { FD_ZERO(&ftpData->connectionData.rset); @@ -99,6 +173,21 @@ void fdInit(ftpDataType * ftpData) FD_SET(ftpData->connectionData.theMainSocket, &ftpData->connectionData.esetAll); } +void fdAdd(ftpDataType * ftpData, int index) +{ + FD_SET(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.rsetAll); + FD_SET(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.wsetAll); + FD_SET(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.esetAll); + ftpData->connectionData.maxSocketFD = getMaximumSocketFd(ftpData->connectionData.theMainSocket, ftpData) + 1; +} + +void fdRemove(ftpDataType * ftpData, int index) +{ + FD_CLR(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.rsetAll); + FD_CLR(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.wsetAll); + FD_CLR(ftpData->clients[index].socketDescriptor, &ftpData->connectionData.esetAll); +} + void closeSocket(ftpDataType * ftpData, int processingSocket) { //Close the socket @@ -174,4 +263,105 @@ int selectWait(ftpDataType * ftpData) ftpData->connectionData.wset = ftpData->connectionData.wsetAll; ftpData->connectionData.eset = ftpData->connectionData.esetAll; return select(ftpData->connectionData.maxSocketFD, &ftpData->connectionData.rset, NULL, &ftpData->connectionData.eset, &selectMaximumLockTime); +} + +int isClientConnected(ftpDataType * ftpData, int cliendId) +{ + if (ftpData->clients[cliendId].socketDescriptor < 0 || + ftpData->clients[cliendId].socketIsConnected == 0) + { + return 0; + } + + return 1; +} + +int getAvailableClientSocketIndex(ftpDataType * ftpData) +{ + int socketIndex; + for (socketIndex = 0; socketIndex < ftpData->ftpParameters.maxClients; socketIndex++) + { + if (isClientConnected(ftpData, socketIndex) == 0) + { + return socketIndex; + } + } + + /* no socket are available for a new client connection */ + return -1; +} + +int evaluateClientSocketConnection(ftpDataType * ftpData) +{ + if (FD_ISSET(ftpData->connectionData.theMainSocket, &ftpData->connectionData.rset)) + { + int availableSocketIndex; + if ((availableSocketIndex = getAvailableClientSocketIndex(ftpData)) != -1) //get available socket + { + if ((ftpData->clients[availableSocketIndex].socketDescriptor = accept(ftpData->connectionData.theMainSocket, (struct sockaddr *)&ftpData->clients[availableSocketIndex].client_sockaddr_in, (socklen_t*)&ftpData->clients[availableSocketIndex].sockaddr_in_size))!=-1) + { + int error; + ftpData->connectedClients++; + ftpData->clients[availableSocketIndex].socketIsConnected = 1; + + error = fcntl(ftpData->clients[availableSocketIndex].socketDescriptor, F_SETFL, O_NONBLOCK); + + fdAdd(ftpData, availableSocketIndex); + + error = getsockname(ftpData->clients[availableSocketIndex].socketDescriptor, (struct sockaddr *)&ftpData->clients[availableSocketIndex].server_sockaddr_in, (socklen_t*)&ftpData->clients[availableSocketIndex].sockaddr_in_server_size); + inet_ntop(AF_INET, + &(ftpData->clients[availableSocketIndex].server_sockaddr_in.sin_addr), + ftpData->clients[availableSocketIndex].serverIpAddress, + INET_ADDRSTRLEN); + printf("\n Server IP: %s", ftpData->clients[availableSocketIndex].serverIpAddress); + printf("Server: New client connected with id: %d", availableSocketIndex); + printf("\nServer: Clients connected: %d", ftpData->connectedClients); + sscanf (ftpData->clients[availableSocketIndex].serverIpAddress,"%d.%d.%d.%d", &ftpData->clients[availableSocketIndex].serverIpAddressInteger[0], + &ftpData->clients[availableSocketIndex].serverIpAddressInteger[1], + &ftpData->clients[availableSocketIndex].serverIpAddressInteger[2], + &ftpData->clients[availableSocketIndex].serverIpAddressInteger[3]); + + inet_ntop(AF_INET, + &(ftpData->clients[availableSocketIndex].client_sockaddr_in.sin_addr), + ftpData->clients[availableSocketIndex].clientIpAddress, + INET_ADDRSTRLEN); + printf("\n Client IP: %s", ftpData->clients[availableSocketIndex].clientIpAddress); + ftpData->clients[availableSocketIndex].clientPort = (int) ntohs(ftpData->clients[availableSocketIndex].client_sockaddr_in.sin_port); + printf("\nClient port is: %d\n", ftpData->clients[availableSocketIndex].clientPort); + + ftpData->clients[availableSocketIndex].connectionTimeStamp = (int)time(NULL); + ftpData->clients[availableSocketIndex].lastActivityTimeStamp = (int)time(NULL); + write(ftpData->clients[availableSocketIndex].socketDescriptor, ftpData->welcomeMessage, strlen(ftpData->welcomeMessage)); + return 1; + } + else + { + printf("\nErrno = %d", errno); + return 0; + } + } + else + { + int socketRefuseFd, socketRefuse_in_size; + socketRefuse_in_size = sizeof(struct sockaddr_in); + struct sockaddr_in socketRefuse_sockaddr_in; + if ((socketRefuseFd = accept(ftpData->connectionData.theMainSocket, (struct sockaddr *)&socketRefuse_sockaddr_in, (socklen_t*)&socketRefuse_in_size))!=-1) + { + char *messageToWrite = "530 Server reached the maximum number of connection, please try later.\r\n"; + write(socketRefuseFd, messageToWrite, strlen(messageToWrite)); + shutdown(socketRefuseFd, SHUT_RDWR); + close(socketRefuseFd); + return 0; + } + else + { + printf("\nErrno = %d", errno); + return 0; + } + } + } + else + { + return 0; + } } \ No newline at end of file diff --git a/library/connection.h b/library/connection.h index a031c2e..3a579ed 100644 --- a/library/connection.h +++ b/library/connection.h @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2018 ugo. + * Copyright 2018 Ugo Cirmignani. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,12 +22,6 @@ * THE SOFTWARE. */ -/* - * File: connection.h - * Author: ugo - * - * Created on 25 febbraio 2018, 18.43 - */ #ifndef CONNECTION_H #define CONNECTION_H @@ -40,12 +34,21 @@ extern "C" { int getMaximumSocketFd(int mainSocket, ftpDataType * data); int createSocket(ftpDataType * ftpData); +int createPassiveSocket(int port); +int createActiveSocket(int port, char *ipAddress); void fdInit(ftpDataType * ftpData); +void fdAdd(ftpDataType * ftpData, int index); +void fdRemove(ftpDataType * ftpData, int index); void checkClientConnectionTimeout(ftpDataType * ftpData); void closeSocket(ftpDataType * ftpData, int processingSocket); void closeClient(ftpDataType * ftpData, int processingSocket); int selectWait(ftpDataType * ftpData); +int isClientConnected(ftpDataType * ftpData, int cliendId); +int getAvailableClientSocketIndex(ftpDataType * ftpData); +int evaluateClientSocketConnection(ftpDataType * ftpData); + + #ifdef __cplusplus } diff --git a/library/daemon.c b/library/daemon.c index d5d2b93..b9267d5 100644 --- a/library/daemon.c +++ b/library/daemon.c @@ -50,9 +50,9 @@ int isProcessAlreadyRunning(void) syslog(LOG_ERR, "can’t open %s: %s", LOCKFILE, strerror(errno)); exit(1); } - printf("\nFile pid opened."); + //printf("\nFile pid opened."); - if (returnCode = FILE_LockFile(fd) < 0) + if ((returnCode = FILE_LockFile(fd)) < 0) { if (errno == EACCES || errno == EAGAIN) { @@ -63,7 +63,7 @@ int isProcessAlreadyRunning(void) exit(1); } - printf("\nFILE_LockFile returnCode = %d", returnCode); + //printf("\nFILE_LockFile returnCode = %d", returnCode); ftruncate(fd, 0); sprintf(buf, "%ld", (long)getpid()); write(fd, buf, strlen(buf)+1); @@ -132,5 +132,4 @@ void daemonize(const char *cmd) fd0 = open("/dev/null", O_RDWR); fd1 = dup(0); fd2 = dup(0); - } \ No newline at end of file diff --git a/library/signals.h b/library/signals.h index 7b1ebb3..d6570f1 100644 --- a/library/signals.h +++ b/library/signals.h @@ -22,13 +22,6 @@ * THE SOFTWARE. */ -/* - * File: signals.h - * Author: ugo - * - * Created on 24 febbraio 2018, 19.15 - */ - #ifndef SIGNALS_H #define SIGNALS_H diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index aef7ea3..0fea176 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -6,6 +6,10 @@ - + + file:/home/ugo/NetBeansProjects/uFTP/ftpServer.c + file:/home/ugo/NetBeansProjects/uFTP/uFTP.c + file:/home/ugo/NetBeansProjects/uFTP/ftpData.h + diff --git a/uFTP.c b/uFTP.c index 614feca..8895c92 100644 --- a/uFTP.c +++ b/uFTP.c @@ -22,12 +22,10 @@ * THE SOFTWARE. */ - #include #include #include "ftpServer.h" - int main(int argc, char** argv) { runFtpServer();