- some bugfixes for ftp transfer (i think multiple concurrent users now works properly)
This commit is contained in:
parent
281f489678
commit
e967ac48fd
|
@ -70,6 +70,8 @@ typedef struct
|
|||
uint32_t timeLastCmd; ///< timestamp of last ftp activity (used for timeout generation)
|
||||
socket_t ctrlSocket; ///< socket for control connection
|
||||
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
|
||||
|
||||
}ftpSession_S;
|
||||
|
|
|
@ -25,12 +25,12 @@
|
|||
/**
|
||||
* @brief max. possible simultaneous FTP client connections
|
||||
*/
|
||||
#define MAX_CONNECTIONS 60
|
||||
#define MAX_CONNECTIONS 20
|
||||
|
||||
/**
|
||||
* @brief max. possible user accounts
|
||||
*/
|
||||
#define MAX_USERS 20
|
||||
#define MAX_USERS 10
|
||||
|
||||
/**
|
||||
* @brief max. length of a user account name
|
||||
|
@ -58,7 +58,7 @@
|
|||
* The scratch buffer is used for
|
||||
* send / receive of files and directory listings
|
||||
*/
|
||||
#define LEN_SCRATCHBUF 1024
|
||||
#define LEN_SCRATCHBUF 1024
|
||||
|
||||
/**
|
||||
* @brief Size of the receive buffer for ftp commands
|
||||
|
|
|
@ -108,6 +108,7 @@ int ftpExecTransmission(int sessionId)
|
|||
}
|
||||
else
|
||||
{
|
||||
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpExecTransmission ftpReadFile returned = %d for sessionId = %d\n",len,sessionId);
|
||||
ftpSendMsg(MSG_NORMAL, sessionId, 451, ftpMsg001);
|
||||
finished = TRUE;
|
||||
}
|
||||
|
@ -124,16 +125,24 @@ int ftpExecTransmission(int sessionId)
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
||||
rxLen += len;
|
||||
} while(rxLen < LEN_SCRATCHBUF);
|
||||
|
||||
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);
|
||||
finished = TRUE;
|
||||
}
|
||||
|
@ -227,6 +236,9 @@ LOCAL int ftpCmdPort(int sessionId, const char* args, int len)
|
|||
clientIp[0] = clientIp[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);
|
||||
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||
}
|
||||
|
@ -283,7 +295,7 @@ LOCAL int sendListing(socket_t dataSocket, int sessionId, const char* path, int
|
|||
ftpGetLocalTime(&currTime);
|
||||
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;
|
||||
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);
|
||||
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)
|
||||
{
|
||||
|
@ -393,13 +405,19 @@ if(VERBOSE_MODE_ENABLED) printf("ftpGetRealPath() returned [%s] stat() = %d\n",
|
|||
|
||||
ftpCloseDir(dir);
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
ftpSendMsg(MSG_NORMAL, sessionId, 226, ftpMsg013);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
@ -439,7 +457,10 @@ LOCAL int ftpCmdList(int sessionId, const char* args, int len)
|
|||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -474,7 +495,10 @@ LOCAL int ftpCmdNlst(int sessionId, const char* args, int len)
|
|||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -487,13 +511,15 @@ LOCAL int ftpCmdRetr(int sessionId, const char* args, int len)
|
|||
void *fp;
|
||||
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);
|
||||
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(VERBOSE_MODE_ENABLED) printf("ERROR In ftpCmdRetr [file not available] args [%s] realPath [%s]\n", args, realPath);
|
||||
|
||||
ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg032);
|
||||
return 2;
|
||||
}
|
||||
|
@ -509,9 +535,13 @@ if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,f
|
|||
}
|
||||
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);
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
@ -522,12 +552,19 @@ if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,f
|
|||
fp = ftpOpenFile(realPath, "rb");
|
||||
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);
|
||||
ftpExecTransmission(sessionId);
|
||||
}
|
||||
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);
|
||||
|
||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdRetr about to Close socket = %d for sessionId = %d\n",s,sessionId);
|
||||
ftpUntrackSocket(s);
|
||||
ftpCloseSocket(&s);
|
||||
}
|
||||
|
||||
|
@ -569,8 +606,14 @@ LOCAL int ftpCmdStor(int sessionId, const char* args, int len)
|
|||
}
|
||||
else
|
||||
{
|
||||
if(VERBOSE_MODE_ENABLED) printf("ERROR in ftpCmdStor could not open realPath [%s]\n",realPath);
|
||||
|
||||
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;
|
||||
|
@ -661,29 +704,43 @@ LOCAL int ftpCmdPasv(int sessionId, const char* args, int len)
|
|||
socket_t s;
|
||||
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);
|
||||
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||
}
|
||||
//if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv about to Close socket = %d for sessionId = %d\n",ftpGetSession(sessionId)->passiveDataSocket,sessionId);
|
||||
|
||||
//ftpUntrackSocket(ftpGetSession(sessionId)->passiveDataSocket);
|
||||
//ftpCloseSocket(&ftpGetSession(sessionId)->passiveDataSocket);
|
||||
//}
|
||||
//ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||
s = ftpEstablishDataConnection(TRUE, &ip, &port,sessionId);
|
||||
if(s < 0)
|
||||
{
|
||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
|
||||
return 1;
|
||||
}
|
||||
ftpGetSession(sessionId)->passiveDataSocket = s;
|
||||
s = ftpEstablishDataConnection(TRUE, &ip, &port,sessionId);
|
||||
if(s < 0)
|
||||
{
|
||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
|
||||
return 1;
|
||||
}
|
||||
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",
|
||||
sessionId, ftpGetSession(sessionId)->remoteIp, ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp), port,ftpAddUPNPPortForward,ftpRemoveUPNPPortForward);
|
||||
//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,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)
|
||||
{
|
||||
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);
|
||||
|
||||
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 & 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
|
||||
|
@ -712,7 +769,9 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, str [%s]\n", sess
|
|||
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;
|
||||
return 0;
|
||||
}
|
||||
|
@ -826,7 +885,10 @@ LOCAL int ftpCmdMlsd(int sessionId, const char* args, int len)
|
|||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -882,6 +944,8 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
|
|||
|
||||
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++)
|
||||
{
|
||||
|
@ -893,11 +957,14 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
|
|||
{
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
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);
|
||||
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(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
|
||||
}
|
||||
}
|
||||
|
||||
while(cmd[i] != '\0')
|
||||
|
@ -916,12 +986,24 @@ int execFtpCmd(int sessionId, const char* cmd, int cmdlen)
|
|||
|
||||
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
|
||||
|
||||
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();
|
||||
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
|
||||
pSession->timeLastCmd = ftpGetUnixTime();
|
||||
return ret;
|
||||
|
@ -951,21 +1033,27 @@ void ftpParseCmd(int sessionId)
|
|||
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(VERBOSE_MODE_ENABLED) printf("In execFtpCmd command triggered close for socket: %d!\n",ctrlSocket);
|
||||
|
||||
ftpUntrackSocket(ctrlSocket);
|
||||
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?
|
||||
{
|
||||
pSession->rxBufWriteIdx = 0;
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -146,6 +146,20 @@ int ftpExecute(void)
|
|||
int sessionId=0;
|
||||
int activeJobs=0;
|
||||
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
|
||||
{
|
||||
|
@ -182,32 +196,35 @@ int ftpExecute(void)
|
|||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//socksRdy = ftpSelect(TRUE);
|
||||
for(n = 0; (socksRdy > 0) && (n < MAX_CONNECTIONS); n++)
|
||||
{
|
||||
pSession = ftpGetSession(n);
|
||||
if(pSession->open)
|
||||
{
|
||||
socket_t ctrlSocket = pSession->ctrlSocket;
|
||||
if(pSession->open)
|
||||
{
|
||||
socket_t ctrlSocket = pSession->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--;
|
||||
bufLen = (LEN_RXBUF - pSession->rxBufWriteIdx);
|
||||
len = ftpReceive(ctrlSocket,
|
||||
&pSession->rxBuf[pSession->rxBufWriteIdx],
|
||||
LEN_RXBUF - pSession->rxBufWriteIdx);
|
||||
bufLen);
|
||||
if(len <= 0) // has client shutdown the connection?
|
||||
{
|
||||
int errorNumber = len; //getLastSocketError();
|
||||
int errorNumber = getLastSocketError();
|
||||
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);
|
||||
ftpCloseSession(n);
|
||||
|
@ -219,29 +236,17 @@ if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reache
|
|||
}
|
||||
}
|
||||
/// @bug Session-Timeout-Management doesn't work
|
||||
if((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT)
|
||||
{
|
||||
if(VERBOSE_MODE_ENABLED) printf("ftpExecute ERROR: SESSION TIMED OUT for socket = %d\n",ctrlSocket);
|
||||
if((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT)
|
||||
{
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -87,6 +87,8 @@ int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort)
|
|||
sessions[n].activeTrans.dataSocket = -1;
|
||||
sessions[n].activeTrans.fileSize = 0;
|
||||
|
||||
if(VERBOSE_MODE_ENABLED) printf("ftpOpenSession started for ctrlSocket: %d\n",ctrlSocket);
|
||||
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
@ -119,29 +121,37 @@ int ftpAuthSession(int id)
|
|||
*/
|
||||
int ftpCloseSession(int id)
|
||||
{
|
||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d\n",
|
||||
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort);
|
||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d, ctrlSocket = %d\n",
|
||||
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort,sessions[id].ctrlSocket);
|
||||
|
||||
if(ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0)
|
||||
{
|
||||
if(ftpRemoveUPNPPortForward)
|
||||
{
|
||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
|
||||
//if(ftpRemoveUPNPPortForward)
|
||||
//{
|
||||
//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);
|
||||
sessions[id].remoteFTPServerPassivePort = 0;
|
||||
}
|
||||
//}
|
||||
}
|
||||
if(sessions[id].open) {
|
||||
ftpCloseSocket(&sessions[id].ctrlSocket);
|
||||
ftpCloseTransmission(id);
|
||||
ftpCloseSocket(&sessions[id].passiveDataSocket);
|
||||
}
|
||||
sessions[id].remoteIp = 0;
|
||||
sessions[id].ctrlSocket = 0;
|
||||
sessions[id].passiveDataSocket = 0;
|
||||
//if(sessions[id].open) {
|
||||
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);
|
||||
|
||||
ftpUntrackSocket(sessions[id].ctrlSocket);
|
||||
ftpCloseSocket(&sessions[id].ctrlSocket);
|
||||
ftpCloseTransmission(id);
|
||||
ftpUntrackSocket(sessions[id].passiveDataSocket);
|
||||
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].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);
|
||||
|
||||
|
@ -325,9 +335,17 @@ void ftpOpenTransmission(int id, operation_E op, void* fsHandle, socket_t dataSo
|
|||
*/
|
||||
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?
|
||||
{
|
||||
ftpCloseSocket(&sessions[id].activeTrans.dataSocket);
|
||||
if(sessions[id].activeTrans.op == OP_LIST)
|
||||
{
|
||||
ftpCloseDir(sessions[id].activeTrans.fsHandle);
|
||||
|
@ -336,6 +354,7 @@ void ftpCloseTransmission(int id)
|
|||
{
|
||||
ftpCloseFile(sessions[id].activeTrans.fsHandle);
|
||||
}
|
||||
sessions[id].activeTrans.fsHandle = NULL;
|
||||
sessions[id].activeTrans.op = OP_NOP;
|
||||
sessions[id].activeTrans.dataSocket = 0;
|
||||
actTransCnt--;
|
||||
|
|
|
@ -205,13 +205,26 @@ int ftpSend(socket_t s, const void *data, int len)
|
|||
|
||||
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;
|
||||
data = (uint8_t*)data + currLen;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
|
||||
|
||||
ftpUntrackSocket(dataSocket);
|
||||
ftpCloseSocket(&dataSocket);
|
||||
return -1;
|
||||
}
|
||||
|
@ -244,6 +260,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
myAddr.sin_port = htons(20);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -252,6 +271,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
clientAddr.sin_port = htons(*port);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -259,28 +281,39 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
else
|
||||
{
|
||||
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_addr.s_addr = INADDR_ANY;
|
||||
myAddr.sin_port = htons(passivePort);
|
||||
//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(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);
|
||||
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);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -292,8 +325,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -316,17 +350,25 @@ socket_t ftpAcceptDataConnection(socket_t listner)
|
|||
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
dataSocket = -1;
|
||||
}
|
||||
|
@ -354,12 +396,18 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||
|
||||
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);
|
||||
return -2;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
@ -399,8 +450,9 @@ if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", ine
|
|||
|
||||
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);
|
||||
clientSocket = -1;
|
||||
}
|
||||
|
@ -418,7 +470,10 @@ int ftpTrackSocket(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
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -236,13 +236,26 @@ int ftpSend(socket_t s, const void *data, int len)
|
|||
|
||||
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;
|
||||
data = (uint8_t*)data + currLen;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
|
||||
|
||||
ftpUntrackSocket(dataSocket);
|
||||
ftpCloseSocket(&dataSocket);
|
||||
return -1;
|
||||
}
|
||||
|
@ -275,6 +291,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
myAddr.sin_port = htons(20);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -283,6 +302,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
clientAddr.sin_port = htons(*port);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -290,28 +312,39 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
else
|
||||
{
|
||||
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_addr.s_addr = INADDR_ANY;
|
||||
myAddr.sin_port = htons(passivePort);
|
||||
//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(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);
|
||||
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);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -323,8 +356,9 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
|
|||
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -348,17 +382,25 @@ socket_t ftpAcceptDataConnection(socket_t listner)
|
|||
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
dataSocket = -1;
|
||||
}
|
||||
|
@ -386,12 +428,18 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||
|
||||
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);
|
||||
return -2;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
@ -431,8 +482,9 @@ if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", ine
|
|||
|
||||
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);
|
||||
clientSocket = -1;
|
||||
}
|
||||
|
@ -450,7 +502,10 @@ int ftpTrackSocket(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
|
||||
return 0;
|
||||
}
|
||||
|
@ -473,10 +528,9 @@ int ftpSelect(int poll)
|
|||
else
|
||||
{
|
||||
struct timeval t = {0};
|
||||
t.tv_usec = 100;
|
||||
t.tv_usec = 1000;
|
||||
|
||||
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue