- some bugfixes for ftp transfer (i think multiple concurrent users now works properly)

This commit is contained in:
Mark Vejvoda 2011-01-17 07:19:32 +00:00
parent 281f489678
commit e967ac48fd
7 changed files with 325 additions and 102 deletions

View File

@ -70,6 +70,8 @@ typedef struct
uint32_t timeLastCmd; ///< timestamp of last ftp activity (used for timeout generation) uint32_t timeLastCmd; ///< timestamp of last ftp activity (used for timeout generation)
socket_t ctrlSocket; ///< socket for control connection socket_t ctrlSocket; ///< socket for control connection
socket_t passiveDataSocket; ///< listener socket for data connections in passive mode socket_t passiveDataSocket; ///< listener socket for data connections in passive mode
ip_t passiveIp; ///< IP of the FTP Server from the clients perspective related to Passive connection
port_t passivePort; ///< Port of the FTP Server from the clients perspective related to Passive connection
transmission_S activeTrans; ///< infos about a currently active file/directory-transmission transmission_S activeTrans; ///< infos about a currently active file/directory-transmission
}ftpSession_S; }ftpSession_S;

View File

@ -25,12 +25,12 @@
/** /**
* @brief max. possible simultaneous FTP client connections * @brief max. possible simultaneous FTP client connections
*/ */
#define MAX_CONNECTIONS 60 #define MAX_CONNECTIONS 20
/** /**
* @brief max. possible user accounts * @brief max. possible user accounts
*/ */
#define MAX_USERS 20 #define MAX_USERS 10
/** /**
* @brief max. length of a user account name * @brief max. length of a user account name
@ -58,7 +58,7 @@
* The scratch buffer is used for * The scratch buffer is used for
* send / receive of files and directory listings * send / receive of files and directory listings
*/ */
#define LEN_SCRATCHBUF 1024 #define LEN_SCRATCHBUF 1024
/** /**
* @brief Size of the receive buffer for ftp commands * @brief Size of the receive buffer for ftp commands

View File

@ -108,6 +108,7 @@ int ftpExecTransmission(int sessionId)
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpExecTransmission ftpReadFile returned = %d for sessionId = %d\n",len,sessionId);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg001); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg001);
finished = TRUE; finished = TRUE;
} }
@ -124,16 +125,24 @@ int ftpExecTransmission(int sessionId)
{ {
len = ftpReceive(pTrans->dataSocket, &scratchBuf[rxLen], LEN_SCRATCHBUF - rxLen); len = ftpReceive(pTrans->dataSocket, &scratchBuf[rxLen], LEN_SCRATCHBUF - rxLen);
if(len <= 0) if(len <= 0) {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("ftpExecTransmission ERROR ON RECEIVE for socket = %d, data len = %d, error = %d [%s]\n",pTrans->dataSocket,(LEN_SCRATCHBUF - rxLen),errorNumber,errText);
break; break;
}
rxLen += len; rxLen += len;
} while(rxLen < LEN_SCRATCHBUF); } while(rxLen < LEN_SCRATCHBUF);
if(rxLen > 0) if(rxLen > 0)
{ {
if(ftpWriteFile(scratchBuf, 1, rxLen, pTrans->fsHandle) != rxLen) int res = ftpWriteFile(scratchBuf, 1, rxLen, pTrans->fsHandle);
if(res != rxLen)
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpExecTransmission ftpWriteFile returned = %d for sessionId = %d\n",res,sessionId);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg001); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg001);
finished = TRUE; finished = TRUE;
} }
@ -227,6 +236,9 @@ LOCAL int ftpCmdPort(int sessionId, const char* args, int len)
clientIp[0] = clientIp[0]; clientIp[0] = clientIp[0];
if(ftpGetSession(sessionId)->passiveDataSocket >= 0) if(ftpGetSession(sessionId)->passiveDataSocket >= 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPort about to Close socket = %d for sessionId = %d\n",ftpGetSession(sessionId)->passiveDataSocket,sessionId);
ftpUntrackSocket(ftpGetSession(sessionId)->passiveDataSocket);
ftpCloseSocket(&ftpGetSession(sessionId)->passiveDataSocket); ftpCloseSocket(&ftpGetSession(sessionId)->passiveDataSocket);
ftpGetSession(sessionId)->passiveDataSocket = -1; ftpGetSession(sessionId)->passiveDataSocket = -1;
} }
@ -283,7 +295,7 @@ LOCAL int sendListing(socket_t dataSocket, int sessionId, const char* path, int
ftpGetLocalTime(&currTime); ftpGetLocalTime(&currTime);
ftpSendMsg(MSG_NORMAL, sessionId, 150, ftpMsg010); ftpSendMsg(MSG_NORMAL, sessionId, 150, ftpMsg010);
if(VERBOSE_MODE_ENABLED) printf("about to read dir contents [%s]\n", path); if(VERBOSE_MODE_ENABLED) printf("In sendListing about to read dir contents [%s] for sessionId = %d, dataSocket = %d\n", path,sessionId,dataSocket);
haveAnySuccessfulFiles = 0; haveAnySuccessfulFiles = 0;
while((dirEntry = ftpReadDir(dir)) != NULL) while((dirEntry = ftpReadDir(dir)) != NULL)
@ -291,7 +303,7 @@ if(VERBOSE_MODE_ENABLED) printf("about to read dir contents [%s]\n", path);
const char * realPath = ftpGetRealPath(sessionId, dirEntry, FALSE); const char * realPath = ftpGetRealPath(sessionId, dirEntry, FALSE);
int statResult = ftpStat(realPath, &fileInfo); int statResult = ftpStat(realPath, &fileInfo);
if(VERBOSE_MODE_ENABLED) printf("ftpGetRealPath() returned [%s] stat() = %d\n", realPath, statResult); if(VERBOSE_MODE_ENABLED) printf("ftpGetRealPath() returned [%s] stat() = %d\n", realPath, statResult);
if(statResult == 0) if(statResult == 0)
{ {
@ -393,13 +405,19 @@ if(VERBOSE_MODE_ENABLED) printf("ftpGetRealPath() returned [%s] stat() = %d\n",
ftpCloseDir(dir); ftpCloseDir(dir);
if(err && haveAnySuccessfulFiles == 0) if(err && haveAnySuccessfulFiles == 0)
{
if(VERBOSE_MODE_ENABLED) printf("ERROR in sendListing err = %d, path = [%s] for sessionId = %d, dataSocket = %d\n",err,path,sessionId,dataSocket);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg039); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg039);
}
else else
{
ftpSendMsg(MSG_NORMAL, sessionId, 226, ftpMsg013); ftpSendMsg(MSG_NORMAL, sessionId, 226, ftpMsg013);
}
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("opendir [%s] returned errno: %#x\n", path,errno); if(VERBOSE_MODE_ENABLED) printf("ERROR opendir [%s] returned errno: %#x for sessionId = %d, dataSocket = %d\n", path,errno,sessionId,dataSocket);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg038); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg038);
} }
@ -439,7 +457,10 @@ LOCAL int ftpCmdList(int sessionId, const char* args, int len)
} }
} }
sendListing(s, sessionId, realPath, LIST); sendListing(s, sessionId, realPath, LIST);
ftpCloseSocket(&s);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdList about to Close socket = %d for sessionId = %d\n",s,sessionId);
ftpUntrackSocket(s);
ftpCloseSocket(&s);
return 0; return 0;
} }
@ -474,7 +495,10 @@ LOCAL int ftpCmdNlst(int sessionId, const char* args, int len)
} }
} }
sendListing(s, sessionId, realPath, NLST); sendListing(s, sessionId, realPath, NLST);
ftpCloseSocket(&s);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdNlst about to Close socket = %d for sessionId = %d\n",s,sessionId);
ftpUntrackSocket(s);
ftpCloseSocket(&s);
return 0; return 0;
} }
@ -487,13 +511,15 @@ LOCAL int ftpCmdRetr(int sessionId, const char* args, int len)
void *fp; void *fp;
int statResult = 0; int statResult = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpCmdRetr args [%s] realPath [%s]\n", args, realPath); if(VERBOSE_MODE_ENABLED) printf("In ftpCmdRetr args [%s] realPath [%s]\n", args, realPath);
statResult = ftpStat(realPath, &fileInfo); statResult = ftpStat(realPath, &fileInfo);
if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,fileInfo.type); if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,fileInfo.type);
if(statResult || (fileInfo.type != TYPE_FILE)) // file accessible? if(statResult || (fileInfo.type != TYPE_FILE)) // file accessible?
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpCmdRetr [file not available] args [%s] realPath [%s]\n", args, realPath);
ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg032); ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg032);
return 2; return 2;
} }
@ -509,9 +535,13 @@ if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,f
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdRetr about accept passive data connection, args [%s] realPath [%s]\n", args, realPath);
s = ftpAcceptDataConnection(ftpGetSession(sessionId)->passiveDataSocket); s = ftpAcceptDataConnection(ftpGetSession(sessionId)->passiveDataSocket);
if(s < 0) if(s < 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpCmdRetr failed to accept data connection, args [%s] realPath [%s]\n", args, realPath);
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012); ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
return 1; return 1;
} }
@ -522,12 +552,19 @@ if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,f
fp = ftpOpenFile(realPath, "rb"); fp = ftpOpenFile(realPath, "rb");
if(fp) if(fp)
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdRetr opened realPath [%s] [%p] for sessionId = %d for socket = %d\n",realPath,fp,sessionId,s);
ftpOpenTransmission(sessionId, OP_RETR, fp, s, fileInfo.size); ftpOpenTransmission(sessionId, OP_RETR, fp, s, fileInfo.size);
ftpExecTransmission(sessionId); ftpExecTransmission(sessionId);
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpCmdRetr could not open realPath [%s] for sessionId = %d for socket = %d\n",realPath,sessionId,s);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg015); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg015);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdRetr about to Close socket = %d for sessionId = %d\n",s,sessionId);
ftpUntrackSocket(s);
ftpCloseSocket(&s); ftpCloseSocket(&s);
} }
@ -569,8 +606,14 @@ LOCAL int ftpCmdStor(int sessionId, const char* args, int len)
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpCmdStor could not open realPath [%s]\n",realPath);
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg015); ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg015);
ftpCloseSocket(&s);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdStor about to Close socket = %d for sessionId = %d\n",s,sessionId);
ftpUntrackSocket(s);
ftpCloseSocket(&s);
} }
return 0; return 0;
@ -661,29 +704,43 @@ LOCAL int ftpCmdPasv(int sessionId, const char* args, int len)
socket_t s; socket_t s;
uint32_t remoteFTPServerIp; uint32_t remoteFTPServerIp;
if(ftpGetSession(sessionId)->passiveDataSocket >= 0) if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, ftpGetSession(sessionId)->passiveDataSocket = %d\n", sessionId, ftpGetSession(sessionId)->passiveDataSocket);
if(ftpGetSession(sessionId)->passiveDataSocket <= 0)
{ {
ftpCloseSocket(&ftpGetSession(sessionId)->passiveDataSocket); //if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv about to Close socket = %d for sessionId = %d\n",ftpGetSession(sessionId)->passiveDataSocket,sessionId);
ftpGetSession(sessionId)->passiveDataSocket = -1;
} //ftpUntrackSocket(ftpGetSession(sessionId)->passiveDataSocket);
//ftpCloseSocket(&ftpGetSession(sessionId)->passiveDataSocket);
//}
//ftpGetSession(sessionId)->passiveDataSocket = -1; //ftpGetSession(sessionId)->passiveDataSocket = -1;
s = ftpEstablishDataConnection(TRUE, &ip, &port,sessionId); s = ftpEstablishDataConnection(TRUE, &ip, &port,sessionId);
if(s < 0) if(s < 0)
{ {
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012); ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
return 1; return 1;
} }
ftpGetSession(sessionId)->passiveDataSocket = s; ftpGetSession(sessionId)->passiveDataSocket = s;
ftpGetSession(sessionId)->passiveIp = ip;
ftpGetSession(sessionId)->passivePort = port;
}
else
{
s = ftpGetSession(sessionId)->passiveDataSocket;
ip = ftpGetSession(sessionId)->passiveIp;
port = ftpGetSession(sessionId)->passivePort;
}
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, client IP = %u, remote IP = %u, port = %d, ftpAddUPNPPortForward = %p, ftpRemoveUPNPPortForward = %p\n", //if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, client IP = %u, remote IP = %u, port = %d, ftpAddUPNPPortForward = %p, ftpRemoveUPNPPortForward = %p using listener socket = %d\n",
sessionId, ftpGetSession(sessionId)->remoteIp, ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp), port,ftpAddUPNPPortForward,ftpRemoveUPNPPortForward); // sessionId, ftpGetSession(sessionId)->remoteIp, ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp), port,ftpAddUPNPPortForward,ftpRemoveUPNPPortForward,s);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, client IP = %u, remote IP = %u, port = %d, using listener socket = %d\n",
sessionId, ftpGetSession(sessionId)->remoteIp, ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp), port,s);
if(ftpAddUPNPPortForward != NULL && ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp) != 0) if(ftpAddUPNPPortForward != NULL && ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp) != 0)
{ {
ftpGetSession(sessionId)->remoteFTPServerPassivePort = port; ftpGetSession(sessionId)->remoteFTPServerPassivePort = port;
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port forward\n", sessionId); //if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port forward\n", sessionId);
//ftpAddUPNPPortForward(port, port); //ftpAddUPNPPortForward(port, port);
remoteFTPServerIp = ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp); remoteFTPServerIp = ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp);
@ -697,7 +754,7 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port
(port >> 8) & 0xFF, (port >> 8) & 0xFF,
port & 0xFF); port & 0xFF);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, str [%s]\n", sessionId, str); if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, str [%s]\n", sessionId, str);
} }
else else
@ -712,7 +769,9 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, str [%s]\n", sess
port & 0xFF); port & 0xFF);
} }
ftpSendMsg(MSG_NORMAL, sessionId, 227, str); if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, ftpGetSession(sessionId)->passiveDataSocket = %d SENDING 227 to client\n", sessionId, ftpGetSession(sessionId)->passiveDataSocket);
ftpSendMsg(MSG_NORMAL, sessionId, 227, str);
ftpGetSession(sessionId)->passive = TRUE; ftpGetSession(sessionId)->passive = TRUE;
return 0; return 0;
} }
@ -826,7 +885,10 @@ LOCAL int ftpCmdMlsd(int sessionId, const char* args, int len)
} }
} }
sendListing(s, sessionId, realPath, MLSD); sendListing(s, sessionId, realPath, MLSD);
ftpCloseSocket(&s);
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdMlsd about to Close socket = %d for sessionId = %d\n",s,sessionId);
ftpUntrackSocket(s);
ftpCloseSocket(&s);
return 0; return 0;
} }
@ -882,6 +944,8 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
ftpSession_S *pSession = ftpGetSession(sessionId); ftpSession_S *pSession = ftpGetSession(sessionId);
//if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd ARRAY_SIZE(cmds) = %lu for sessionId = %d\n",ARRAY_SIZE(cmds),sessionId);
if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd cmd [%s] for sessionId = %d\n",cmd,sessionId);
for(n = 0; n < ARRAY_SIZE(cmds); n++) for(n = 0; n < ARRAY_SIZE(cmds); n++)
{ {
@ -893,11 +957,14 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
{ {
if((pSession->userId == 0) || (pSession->authenticated == FALSE)) if((pSession->userId == 0) || (pSession->authenticated == FALSE))
{ {
if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd User NOT loggedin for sessionId = %d\n",sessionId);
ftpSendMsg(MSG_NORMAL, sessionId, 530, ftpMsg033); ftpSendMsg(MSG_NORMAL, sessionId, 530, ftpMsg033);
return 0; return 0;
} }
if(ftpCheckAccRights(pSession->userId, cmds[n].neededRights)) if(ftpCheckAccRights(pSession->userId, cmds[n].neededRights))
{ {
if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd User has no ACCESS for sessionId = %d\n",sessionId);
ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg034); ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg034);
return 0; return 0;
} }
@ -906,7 +973,10 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
if((pSession->activeTrans.op != OP_NOP)) // transfer in progress? if((pSession->activeTrans.op != OP_NOP)) // transfer in progress?
{ {
if(cmds[n].duringTransfer == FALSE) // command during transfer allowed? if(cmds[n].duringTransfer == FALSE) // command during transfer allowed?
{
if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd got command during transfer, discarding for sessionId = %d\n",sessionId);
return 0; // no => silently discard command return 0; // no => silently discard command
}
} }
while(cmd[i] != '\0') while(cmd[i] != '\0')
@ -916,12 +986,24 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
i++; i++;
} }
if(VERBOSE_MODE_ENABLED) printf("About to execute cmds[n].cmdToken [%s] command [%s] for sessionId = %d\n",cmds[n].cmdToken,&cmd[i],sessionId);
ret = cmds[n].handler(sessionId, &cmd[i], strlen(&cmd[i])); // execute command ret = cmds[n].handler(sessionId, &cmd[i], strlen(&cmd[i])); // execute command
if(VERBOSE_MODE_ENABLED) printf("Executed cmds[n].cmdToken [%s] command [%s] ret = %d for sessionId = %d\n",cmds[n].cmdToken,&cmd[i],ret,sessionId);
pSession->timeLastCmd = ftpGetUnixTime(); pSession->timeLastCmd = ftpGetUnixTime();
return ret; return ret;
} }
else
{
//if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd SKIPPED COMMAND cmds[n].cmdToken = [%s] n = %d for sessionId = %d\n",cmds[n].cmdToken,n,sessionId);
}
} }
if(VERBOSE_MODE_ENABLED) printf("ERROR UNKNOWN COMMAND cmd [%s] for sessionId = %d\n",cmd,sessionId);
ftpSendMsg(MSG_NORMAL, sessionId, 500, cmd); // reject unknown commands ftpSendMsg(MSG_NORMAL, sessionId, 500, cmd); // reject unknown commands
pSession->timeLastCmd = ftpGetUnixTime(); pSession->timeLastCmd = ftpGetUnixTime();
return ret; return ret;
@ -951,21 +1033,27 @@ void ftpParseCmd(int sessionId)
pSession->rxBuf[c] = toupper(pSession->rxBuf[c]); pSession->rxBuf[c] = toupper(pSession->rxBuf[c]);
} }
if(VERBOSE_MODE_ENABLED) printf("%02d --> %s\n", sessionId, pSession->rxBuf); if(VERBOSE_MODE_ENABLED) printf("%02d --> %s for socket: %d\n", sessionId, pSession->rxBuf,ctrlSocket);
if(execFtpCmd(sessionId, pSession->rxBuf, len - 2) == -1) if(execFtpCmd(sessionId, pSession->rxBuf, len - 2) == -1)
{ {
if(VERBOSE_MODE_ENABLED) printf("In execFtpCmd command triggered close for socket: %d!\n",ctrlSocket);
ftpUntrackSocket(ctrlSocket); ftpUntrackSocket(ctrlSocket);
ftpCloseSession(sessionId); ftpCloseSession(sessionId);
} }
} }
else
{
if(VERBOSE_MODE_ENABLED) printf("ERROR In execFtpCmd problem with parsing string [%s] for socket: %d!\n",pSession->rxBuf,ctrlSocket);
}
if(pSession->rxBufWriteIdx >= LEN_RXBUF) // overflow of receive buffer? if(pSession->rxBufWriteIdx >= LEN_RXBUF) // overflow of receive buffer?
{ {
pSession->rxBufWriteIdx = 0; pSession->rxBufWriteIdx = 0;
ftpSendMsg(MSG_NORMAL, sessionId, 500, ftpMsg035); ftpSendMsg(MSG_NORMAL, sessionId, 500, ftpMsg035);
if(VERBOSE_MODE_ENABLED) printf("Receive buffer overflow. Received data discarded.\n"); if(VERBOSE_MODE_ENABLED) printf("ERROR: Receive buffer overflow. Received data discarded.\n");
} }
} }

View File

@ -146,6 +146,20 @@ int ftpExecute(void)
int sessionId=0; int sessionId=0;
int activeJobs=0; int activeJobs=0;
int len; int len;
int bufLen;
activeJobs = ftpGetActiveTransCnt(); // are there any active transmitions?
//for(n = 0; (activeJobs > 0) && (n < MAX_CONNECTIONS); n++)
for(n = 0; n < MAX_CONNECTIONS; n++)
{
pSession = ftpGetSession(n);
if(pSession->activeTrans.op) // has this session an active transmition?
{
processedWork = 1;
ftpExecTransmission(n); // do the job
activeJobs--;
}
}
if(ftpGetActiveTransCnt()) // don't block if there's still something to do if(ftpGetActiveTransCnt()) // don't block if there's still something to do
{ {
@ -182,32 +196,35 @@ int ftpExecute(void)
} }
else else
{ {
if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reached.\n"); if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reached, about to close socket = %d\n",clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket); ftpCloseSocket(&clientSocket);
} }
} }
} }
//socksRdy = ftpSelect(TRUE);
for(n = 0; (socksRdy > 0) && (n < MAX_CONNECTIONS); n++) for(n = 0; (socksRdy > 0) && (n < MAX_CONNECTIONS); n++)
{ {
pSession = ftpGetSession(n); pSession = ftpGetSession(n);
if(pSession->open) if(pSession->open)
{ {
socket_t ctrlSocket = pSession->ctrlSocket; socket_t ctrlSocket = pSession->ctrlSocket;
if(ftpTestSocket(ctrlSocket)) if(ftpTestSocket(ctrlSocket))
{ {
if(VERBOSE_MODE_ENABLED) printf("ftpExecute socket signalled = %d\n",ctrlSocket); if(VERBOSE_MODE_ENABLED) printf("ftpExecute signaled socket = %d, session = %d\n",ctrlSocket,n);
socksRdy--; socksRdy--;
bufLen = (LEN_RXBUF - pSession->rxBufWriteIdx);
len = ftpReceive(ctrlSocket, len = ftpReceive(ctrlSocket,
&pSession->rxBuf[pSession->rxBufWriteIdx], &pSession->rxBuf[pSession->rxBufWriteIdx],
LEN_RXBUF - pSession->rxBufWriteIdx); bufLen);
if(len <= 0) // has client shutdown the connection? if(len <= 0) // has client shutdown the connection?
{ {
int errorNumber = len; //getLastSocketError(); int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber); const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("ftpExecute ERROR ON RECEIVE for socket = %d, data len = %d, error = %d [%s]\n",ctrlSocket,(LEN_RXBUF - pSession->rxBufWriteIdx),errorNumber,errText); if(VERBOSE_MODE_ENABLED) printf("In ftpExecute ERROR ON RECEIVE session = %d for socket = %d, data len = %d index = %d, len = %d, error = %d [%s]\n",n,ctrlSocket,bufLen,pSession->rxBufWriteIdx,len,errorNumber,errText);
ftpUntrackSocket(ctrlSocket); ftpUntrackSocket(ctrlSocket);
ftpCloseSession(n); ftpCloseSession(n);
@ -219,29 +236,17 @@ if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reache
} }
} }
/// @bug Session-Timeout-Management doesn't work /// @bug Session-Timeout-Management doesn't work
if((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT) if((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT)
{ {
if(VERBOSE_MODE_ENABLED) printf("ftpExecute ERROR: SESSION TIMED OUT for socket = %d\n",ctrlSocket); if(VERBOSE_MODE_ENABLED) printf("\nIn ftpExecute ERROR: SESSION TIMED OUT for socket = %d\n",ctrlSocket);
ftpSendMsg(MSG_NORMAL, n, 421, ftpMsg036); ftpSendMsg(MSG_NORMAL, n, 421, ftpMsg036);
ftpUntrackSocket(ctrlSocket); ftpUntrackSocket(ctrlSocket);
ftpCloseSession(n); ftpCloseSession(n);
} }
} }
} }
} }
activeJobs = ftpGetActiveTransCnt(); // are there any active transmitions?
for(n = 0; (activeJobs > 0) && (n < MAX_CONNECTIONS); n++)
{
pSession = ftpGetSession(n);
if(pSession->activeTrans.op) // has this session an active transmition?
{
processedWork = 1;
ftpExecTransmission(n); // do the job
activeJobs--;
}
}
return processedWork; return processedWork;
} }

View File

@ -87,6 +87,8 @@ int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort)
sessions[n].activeTrans.dataSocket = -1; sessions[n].activeTrans.dataSocket = -1;
sessions[n].activeTrans.fileSize = 0; sessions[n].activeTrans.fileSize = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpOpenSession started for ctrlSocket: %d\n",ctrlSocket);
return n; return n;
} }
} }
@ -119,29 +121,37 @@ int ftpAuthSession(int id)
*/ */
int ftpCloseSession(int id) int ftpCloseSession(int id)
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d\n", if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d, ctrlSocket = %d\n",
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort); id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort,sessions[id].ctrlSocket);
if(ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0) if(ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0)
{ {
if(ftpRemoveUPNPPortForward) //if(ftpRemoveUPNPPortForward)
{ //{
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort); //if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
//ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort); //ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort);
sessions[id].remoteFTPServerPassivePort = 0; sessions[id].remoteFTPServerPassivePort = 0;
} //}
} }
if(sessions[id].open) { //if(sessions[id].open) {
ftpCloseSocket(&sessions[id].ctrlSocket); if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession about to Close socket = %d, dataSocket = %d, activeDataSocket = %d, for sessionId = %d\n",sessions[id].ctrlSocket,sessions[id].passiveDataSocket,sessions[id].activeTrans.dataSocket,id);
ftpCloseTransmission(id);
ftpCloseSocket(&sessions[id].passiveDataSocket); ftpUntrackSocket(sessions[id].ctrlSocket);
} ftpCloseSocket(&sessions[id].ctrlSocket);
sessions[id].remoteIp = 0; ftpCloseTransmission(id);
sessions[id].ctrlSocket = 0; ftpUntrackSocket(sessions[id].passiveDataSocket);
sessions[id].passiveDataSocket = 0; ftpCloseSocket(&sessions[id].passiveDataSocket);
//}
sessions[id].remoteIp = 0;
sessions[id].ctrlSocket = 0;
sessions[id].passiveDataSocket = 0;
sessions[id].passiveIp = 0;
sessions[id].passivePort = 0;
sessions[id].activeTrans.dataSocket = 0; sessions[id].activeTrans.dataSocket = 0;
sessions[id].open = FALSE; sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.fileSize = 0;
sessions[id].open = FALSE;
if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id); if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
@ -325,9 +335,17 @@ void ftpOpenTransmission(int id, operation_E op, void* fsHandle, socket_t dataSo
*/ */
void ftpCloseTransmission(int id) void ftpCloseTransmission(int id)
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseTransmission about to Close socket = %d, for sessionId = %d, fsHandle [%p] op = %d\n",
sessions[id].activeTrans.dataSocket, id,sessions[id].activeTrans.fsHandle,sessions[id].activeTrans.op);
if(sessions[id].activeTrans.dataSocket > 0)
{
ftpUntrackSocket(sessions[id].activeTrans.dataSocket);
ftpCloseSocket(&sessions[id].activeTrans.dataSocket);
}
if(sessions[id].activeTrans.op != OP_NOP) // is thera an active transmission? if(sessions[id].activeTrans.op != OP_NOP) // is thera an active transmission?
{ {
ftpCloseSocket(&sessions[id].activeTrans.dataSocket);
if(sessions[id].activeTrans.op == OP_LIST) if(sessions[id].activeTrans.op == OP_LIST)
{ {
ftpCloseDir(sessions[id].activeTrans.fsHandle); ftpCloseDir(sessions[id].activeTrans.fsHandle);
@ -336,6 +354,7 @@ void ftpCloseTransmission(int id)
{ {
ftpCloseFile(sessions[id].activeTrans.fsHandle); ftpCloseFile(sessions[id].activeTrans.fsHandle);
} }
sessions[id].activeTrans.fsHandle = NULL;
sessions[id].activeTrans.op = OP_NOP; sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.dataSocket = 0; sessions[id].activeTrans.dataSocket = 0;
actTransCnt--; actTransCnt--;

View File

@ -205,13 +205,26 @@ int ftpSend(socket_t s, const void *data, int len)
if(currLen >= 0) if(currLen >= 0)
{ {
if(currLen == 0)
{
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n",s,len,errorNumber,errText);
}
len -= currLen; len -= currLen;
data = (uint8_t*)data + currLen; data = (uint8_t*)data + currLen;
} }
else else
return -1; {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n",s,len,currLen,errorNumber,errText);
}while(len > 0); return -1;
}
} while(len > 0);
return 0; return 0;
} }
@ -236,6 +249,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
{ {
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -244,6 +260,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
myAddr.sin_port = htons(20); myAddr.sin_port = htons(20);
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -252,6 +271,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
clientAddr.sin_port = htons(*port); clientAddr.sin_port = htons(*port);
if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr))) if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -259,28 +281,39 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
else else
{ {
int passivePort = ftpGetPassivePort() + sessionId; int passivePort = ftpGetPassivePort() + sessionId;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d\n",sessionId,passivePort); if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n",sessionId,passivePort,dataSocket);
myAddr.sin_family = AF_INET; myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY; myAddr.sin_addr.s_addr = INADDR_ANY;
myAddr.sin_port = htons(passivePort); myAddr.sin_port = htons(passivePort);
//myAddr.sin_port = htons(ftpGetPassivePort() + sessionId); //myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED: %d\n",sessionId,passivePort,dataSocket); if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok\n",sessionId,passivePort); if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n",sessionId,passivePort,dataSocket);
len = sizeof(myAddr); len = sizeof(myAddr);
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{ {
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -292,8 +325,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
if(listen(dataSocket, 100)) if(listen(dataSocket, 100))
{ {
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED #2: %d\n",sessionId,passivePort,dataSocket); if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -316,17 +350,25 @@ socket_t ftpAcceptDataConnection(socket_t listner)
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len); dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
if(dataSocket < 0) if(dataSocket < 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptDataConnection accept failed, dataSocket = %d\n", dataSocket); if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket,listner);
dataSocket = -1; dataSocket = -1;
} }
else
{
if(VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket,listner);
}
ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen if(VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n",listner);
//ftpUntrackSocket(listner);
//ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen
ip_t remoteIP = ntohl(clientinfo.sin_addr.s_addr); ip_t remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0) if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("Connection with %s is NOT a valid trusted client, dropping connection.\n", inet_ntoa(clientinfo.sin_addr)); if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr),dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
dataSocket = -1; dataSocket = -1;
} }
@ -354,12 +396,18 @@ socket_t ftpCreateServerSocket(int portNumber)
if(bind(theServer, (struct sockaddr *)&serverinfo, len)) if(bind(theServer, (struct sockaddr *)&serverinfo, len))
{ {
if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer); ftpCloseSocket(&theServer);
return -2; return -2;
} }
if(listen(theServer, 100)) if(listen(theServer, 100))
{ {
if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer); ftpCloseSocket(&theServer);
return -3; return -3;
} }
@ -378,7 +426,10 @@ socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remo
clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len); clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len);
if(clientSocket < 0) { if(clientSocket < 0) {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed, dataSocket = %d\n", clientSocket); if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
}
else {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
} }
*remoteIP = ntohl(sockinfo.sin_addr.s_addr); *remoteIP = ntohl(sockinfo.sin_addr.s_addr);
@ -399,8 +450,9 @@ if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", ine
if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0) if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection.\n", inet_ntoa(sockinfo.sin_addr), *remotePort); if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort,clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket); ftpCloseSocket(&clientSocket);
clientSocket = -1; clientSocket = -1;
} }
@ -418,7 +470,10 @@ int ftpTrackSocket(socket_t s)
int ftpUntrackSocket(socket_t s) int ftpUntrackSocket(socket_t s)
{ {
FD_CLR(s, &watchedSockets); if(s > 0)
{
FD_CLR(s, &watchedSockets);
}
// TODO hier sollte eine Möglichkeit geschaffen werden um maxSockNr anzupassen // TODO hier sollte eine Möglichkeit geschaffen werden um maxSockNr anzupassen
return 0; return 0;
} }

View File

@ -236,13 +236,26 @@ int ftpSend(socket_t s, const void *data, int len)
if(currLen >= 0) if(currLen >= 0)
{ {
if(currLen == 0)
{
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n",s,len,errorNumber,errText);
}
len -= currLen; len -= currLen;
data = (uint8_t*)data + currLen; data = (uint8_t*)data + currLen;
} }
else else
return -1; {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n",s,len,currLen,errorNumber,errText);
}while(len > 0); return -1;
}
} while(len > 0);
return 0; return 0;
} }
@ -267,6 +280,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
{ {
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on))) if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -275,6 +291,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
myAddr.sin_port = htons(20); myAddr.sin_port = htons(20);
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -283,6 +302,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
clientAddr.sin_port = htons(*port); clientAddr.sin_port = htons(*port);
if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr))) if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -290,28 +312,39 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
else else
{ {
int passivePort = ftpGetPassivePort() + sessionId; int passivePort = ftpGetPassivePort() + sessionId;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d\n",sessionId,passivePort); if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n",sessionId,passivePort,dataSocket);
myAddr.sin_family = AF_INET; myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY; myAddr.sin_addr.s_addr = INADDR_ANY;
myAddr.sin_port = htons(passivePort); myAddr.sin_port = htons(passivePort);
//myAddr.sin_port = htons(ftpGetPassivePort() + sessionId); //myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{ {
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED: %d\n",sessionId,passivePort,dataSocket); if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok\n",sessionId,passivePort); if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n",sessionId,passivePort,dataSocket);
len = sizeof(myAddr); len = sizeof(myAddr);
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{ {
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -323,8 +356,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
if(listen(dataSocket, 100)) if(listen(dataSocket, 100))
{ {
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED #2: %d\n",sessionId,passivePort,dataSocket); if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
return -1; return -1;
} }
@ -348,17 +382,25 @@ socket_t ftpAcceptDataConnection(socket_t listner)
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len); dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
if(dataSocket < 0) if(dataSocket < 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptDataConnection accept failed, dataSocket = %d\n", dataSocket); if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket,listner);
dataSocket = -1; dataSocket = -1;
} }
else
{
if(VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket,listner);
}
ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen if(VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n",listner);
//ftpUntrackSocket(listner);
//ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen
remoteIP = ntohl(clientinfo.sin_addr.s_addr); remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0) if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("Connection with %s is NOT a valid trusted client, dropping connection.\n", inet_ntoa(clientinfo.sin_addr)); if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr),dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket); ftpCloseSocket(&dataSocket);
dataSocket = -1; dataSocket = -1;
} }
@ -386,12 +428,18 @@ socket_t ftpCreateServerSocket(int portNumber)
if(bind(theServer, (struct sockaddr *)&serverinfo, len)) if(bind(theServer, (struct sockaddr *)&serverinfo, len))
{ {
if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d\n",theServer);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer); ftpCloseSocket(&theServer);
return -2; return -2;
} }
if(listen(theServer, 100)) if(listen(theServer, 100))
{ {
if(VERBOSE_MODE_ENABLED) printf("\ERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer); ftpCloseSocket(&theServer);
return -3; return -3;
} }
@ -410,7 +458,10 @@ socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remo
clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len); clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len);
if(clientSocket < 0) { if(clientSocket < 0) {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed, dataSocket = %d\n", clientSocket); if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
}
else {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
} }
*remoteIP = ntohl(sockinfo.sin_addr.s_addr); *remoteIP = ntohl(sockinfo.sin_addr.s_addr);
@ -431,8 +482,9 @@ if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", ine
if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0) if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0)
{ {
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection.\n", inet_ntoa(sockinfo.sin_addr), *remotePort); if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort,clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket); ftpCloseSocket(&clientSocket);
clientSocket = -1; clientSocket = -1;
} }
@ -450,7 +502,10 @@ int ftpTrackSocket(socket_t s)
int ftpUntrackSocket(socket_t s) int ftpUntrackSocket(socket_t s)
{ {
FD_CLR((SOCKET)s, &watchedSockets); if(s > 0)
{
FD_CLR(s, &watchedSockets);
}
// TODO hier sollte eine Möglichkeit geschaffen werden um maxSockNr anzupassen // TODO hier sollte eine Möglichkeit geschaffen werden um maxSockNr anzupassen
return 0; return 0;
} }
@ -473,10 +528,9 @@ int ftpSelect(int poll)
else else
{ {
struct timeval t = {0}; struct timeval t = {0};
t.tv_usec = 100; t.tv_usec = 1000;
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t); return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
} }
} }