#include "stdafx.h"
#include "CFtpd.h"
#include "FtpdPi.h"
#define CRLF "\r\n"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char sadr2[32];
bool canclePasvListenSocket(CFtpPasvSrv* sck)
{
try
{
sck->Close();
delete sck;
return true;
}
catch(...)
{
}
return false;
}
bool deleFtpPI(CFtpdPi* sck)
{
try
{
CFtpd* K = (CFtpd*) sck->pFtpd;
K->RemoveClient(sck);
}
catch(...)
{
}
return true;
}
UINT thFtpCmd( LPVOID pParam )
{
((CFtpdPi*)pParam)->ProcessMsg(((CFtpdPi*)pParam)->bufcmd);
return 1;
}
UINT thSendData( LPVOID pParam)
{
CFtpdPi* pFtpBind = (CFtpdPi*) pParam;
CFile* inFile = NULL;
try
{
char path[256] = "";
strcpy(path,pFtpBind->RemoveLeadingSpace(pFtpBind->curCmdth+5));
char srcPath[300] = "";
if(strcmp(pFtpBind->vCurrentDir,"/")==0)
wsprintf(srcPath,"%s%s%s",pFtpBind->vRealRoot,pFtpBind->vCurrentDir,path);
else
wsprintf(srcPath,"%s%s\\%s",pFtpBind->vRealRoot,pFtpBind->vCurrentDir,path);
pFtpBind->slashFix(srcPath);
if(pFtpBind->isFilePresent(path))
{
bool b_opened = true;
try
{
if(pFtpBind->type == 1)
inFile = new CFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary);//CStdioFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeText);
else if(pFtpBind->type == 3)
inFile = new CFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary);
}
catch(...)
{
b_opened = false;
}
if(b_opened)
{
long flen = inFile->GetLength();
if(pFtpBind->restart_marker > 0)
{
inFile->Seek(pFtpBind->restart_marker,CFile::begin);
pFtpBind->restart_marker = 0;
}
long nCount = 0;
if(pFtpBind->type == 1)
wsprintf(pFtpBind->str,"150 Sending %s (%u bytes). Mode STREAM Type ASCII%s",path,flen,CRLF);
else if(pFtpBind->type == 3)
wsprintf(pFtpBind->str,"150 Sending %s (%u bytes). Mode STREAM Type BINARY%s",path,flen,CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
if(flen)
{
long bytesent = 0;
long offset = 0;
DWORD start,end;
do
{
if(flen > pFtpBind->m_buf_length_out)
nCount = inFile->Read(pFtpBind->lpDataBuf_out,pFtpBind->m_buf_length_out);
else
nCount = inFile->Read(pFtpBind->lpDataBuf_out,flen);
flen = flen - nCount;
if(pFtpBind->b_speed_limit_out)
{
bytesent = 0;
offset = 0;
HANDLE h = CreateEvent(NULL,false,false,"noname");
do
{
start = GetTickCount();
if((nCount-offset) > pFtpBind->m_buf_length_out)
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,pFtpBind->m_buf_length_out,60);
else
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,nCount-offset,60);
offset = offset + bytesent;
end = GetTickCount();
if((end-start) < 1000)
{
int time = end-start;
WaitForSingleObject(h,1000-time);
}
}while(offset < nCount);
}
else
{
bytesent = 0;
offset = 0;
do
{
if((nCount-offset) > pFtpBind->m_buf_length_out)
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,pFtpBind->m_buf_length_out,60);
else
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,nCount-offset,60);
offset = offset + bytesent;
}while(offset < nCount);
if(pFtpBind->type == 1)
{
pFtpBind->dbsocket->Send(CRLF,2,60);
}
}
}while(flen != 0);
}
pFtpBind->dbsocket->Close();
if(pFtpBind->b_passive)
pFtpBind->svSocketSrv->Close();
delete inFile;
wsprintf(pFtpBind->str,"226 Transfer finished successfully. Data connection closed.%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
return true;
}else
{
pFtpBind->dbsocket->Close();
if(pFtpBind->b_passive)
pFtpBind->svSocketSrv->Close();
if(inFile != NULL)
delete inFile;
wsprintf(pFtpBind->str,"426 Connection closed , file opening failed%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
}
}
}catch(...)
{
inFile->Close();
pFtpBind->dbsocket->Close();
canclePasvListenSocket(pFtpBind->svSocketSrv);
delete inFile;
wsprintf(pFtpBind->str,"426 Connection closed; transfer aborted (transfer failed)%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
return false;
}
return true;
}
CFtpdPi::CFtpdPi()
{
clientIpCount = 0;
type = 3; //1 = ascii 2 = ebcdic 3 = binary
ipRoller = 0;
b_use_bsock = true;
b_verbose = true;
b_passive = false;
b_bell = false;
b_prompt = true;
b_globbing = true;
b_debugging = false;
b_hashmark = false;
bLOGINOK = false;
bUSER = false;
bPASS = false;
b_speed_limit_out = false;
b_speed_limit_in = false;
restart_marker = 0;
strcpy(vCurrentDir,"/");
strcpy(old_filename,"");
strcpy(new_filename,"");
strcpy(m_group,"nogroup");
m_buf_length_out = 32768;
m_buf_length_in = 32768;
lpDataBuf_out = (unsigned char*) malloc(m_buf_length_out);
memset(lpDataBuf_out,0,m_buf_length_out);
lpDataBuf_in = (unsigned char*) malloc(m_buf_length_in);
memset(lpDataBuf_in,0,m_buf_length_in);
strcpy(monthStr[1],"Jan");
strcpy(monthStr[2],"Feb");
strcpy(monthStr[3],"Mar");
strcpy(monthStr[4],"Apr");
strcpy(monthStr[5],"May");
strcpy(monthStr[6],"Jun");
strcpy(monthStr[7],"Jul");
strcpy(monthStr[8],"Aug");
strcpy(monthStr[9],"Sep");
strcpy(monthStr[10],"Oct");
strcpy(monthStr[11],"Dec");
strcpy(monthStr[12],"Nov");
sl = sizeof(SOCKADDR_IN);
GetLocalTime(&MyTime);
if(FileExists(ipfile))
LoadIpRange();
h_inprogress = CreateEvent(NULL,false,true,"in_progress");
h_dbsocket_ready_to_send = CreateEvent(NULL,false,false,"in_progress");
dbsocket = NULL;
svSocketSrv = NULL;
}
CFtpdPi::~CFtpdPi()
{
free(lpDataBuf_in);
free(lpDataBuf_out);
if(dbsocket != NULL)
{
dbsocket->Close();
delete dbsocket;
}
if(svSocketSrv != NULL)
{
svSocketSrv->Close();
delete svSocketSrv;
}
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CFtpdPi, CSocket)
//{{AFX_MSG_MAP(CFtpdPi)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
void CFtpdPi::OnReceive(int nErrorCode)
{
unsigned char* bufnow = (unsigned char*)malloc(256);
try
{
memset(bufnow,NULL,256);
result = Receive(bufnow,256,0);
bool cont = true;
int currentPos = 0;
if(*(bufnow+1)==0xf2)
currentPos = 1;
for(int i=0;i<result;i++)
{
if(*(bufnow+i)==0x0d&&*(bufnow+i+1)==0x0a)
{
strncpy(bufcmd,(char*)bufnow+currentPos,i-currentPos);
*(bufcmd+i-currentPos) = NULL;
strcpy(bufcmd_history,bufcmd);
AfxBeginThread(&thFtpCmd,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
currentPos = i+2;
i = i + 1;
}
}
free(bufnow);
}
catch (...)
{
delete bufnow;
char lpMsgBuf[128]="";
DWORD eRRR = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,eRRR,0,lpMsgBuf,128,NULL);
char ecp[256]="";
wsprintf(ecp,"%s\\core.ftpd",AppDir);
CStdioFile ofs((LPCTSTR) ecp ,CFile::modeWrite|CFile::modeCreate);
ofs.WriteString("*---------------------------------------------------------------------------\n");
ofs.WriteString("Exception occurs while processing socket msg\n");
GetLocalTime(&MyTime);
wsprintf(ecp,"Thread crashed at %.4d-%.2d-%.2d-%.2d-%.2d\n",MyTime.wYear,MyTime.wMonth,MyTime.wDay,MyTime.wHour,MyTime.wMinute);
ofs.WriteString(ecp);
ofs.WriteString(lpMsgBuf);
ofs
评论0
最新资源