📄 hftpclient.cpp
字号:
// HFtpClient.cpp: implementation of the HFtpClient class.
//
//////////////////////////////////////////////////////////////////////
#include "HFtpClient.h"
#include "stdio.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
HFtpClient::HFtpClient()
{
m_RemainLen=0;
m_wPort=0;
}
HFtpClient::~HFtpClient()
{
}
int HFtpClient::recvLine(char *pLine, int len,UNI_DWORD ns)
{
char *pEnd;
int tempLen,cpyLen,moveLen;
struct timeval time;
fd_set read_set;
cpyLen=0;
do
{
if(m_RemainLen>0)
{
if(pEnd=strstr(this->m_szReturn,"\n"))
{
pEnd++;
tempLen=moveLen=(int )(pEnd-&m_szReturn[0]);
if(tempLen>len) tempLen=len;
memcpy(pLine+cpyLen,m_szReturn,tempLen);
m_RemainLen-=moveLen;
memcpy(m_szReturn,&m_szReturn[moveLen],m_RemainLen);
return cpyLen+tempLen;
}
else
{
tempLen=moveLen=m_RemainLen;
if(tempLen>len) tempLen=len;
memcpy(pLine+cpyLen,m_szReturn,tempLen);
m_RemainLen-=moveLen;
cpyLen+=tempLen;
len-=tempLen;
}
}
else
{
FD_ZERO(&read_set);
this->m_Control.AddToSet(&read_set);
time.tv_sec=ns/1000;
time.tv_usec=ns%1000;
if(1!=select(0,&read_set,UNI_NULL,UNI_NULL,&time))
{
return -1;
}
}
}while((m_RemainLen=m_Control.Recv(m_szReturn,sizeof(m_szReturn)))>0);
return -1;
}
UNI_BOOL HFtpClient::GetAckString(char *pLine, int len,UNI_DWORD ns)
{
int tmpLen,recvLen;
memset(pLine,0,len);
tmpLen=this->recvLine(pLine,len,ns);
if(tmpLen>0)
{
if(pLine[3]!='-') return UNI_TRUE;
recvLen=0;
do {
recvLen+=tmpLen;
len-=tmpLen;
tmpLen=this->recvLine(pLine+recvLen,len-recvLen,ns);
if(pLine[recvLen]>='0' && pLine[recvLen]<='9')
{
recvLen+=tmpLen;
break;
}
} while(tmpLen>0);
}
return UNI_TRUE;
}
UNI_BOOL HFtpClient::Open(UNI_DWORD dwIp, UNI_WORD wPost, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
if(m_Control.CreateConnect(dwIp,wPost))
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::User(char *userName, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"USER ");
strncat(szTempBuf,userName,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Pass(char *passWord, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"PASS ");
strncat(szTempBuf,passWord,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Initialize(UNI_WORD wPort,int nMaxList)
{
CHTcpSock::Initialize();
if(nMaxList>0 && wPort!=0)
{
if(this->m_Listener.ListenAt(wPort,nMaxList))
{
this->m_wPort =wPort;
return UNI_TRUE;
}
return UNI_FALSE;
}
return UNI_TRUE;
}
void HFtpClient::UnInitialize()
{
m_Listener.Close();
CHTcpSock::UnInitialize();
}
UNI_BOOL HFtpClient::Port(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
UNI_DWORD dwIP;
UNI_WORD wPort;
char szTempBuf[512];
if(this->m_wPort !=0)
{
dwIP=this->m_Control.GetLocal(*(UNI_WORD *)&szTempBuf[0]);
wPort=htons(this->m_wPort);
sprintf(szTempBuf,"PORT %d,%d,%d,%d,%d,%d\r\n",
((UNI_BYTE *)&dwIP)[0],
((UNI_BYTE *)&dwIP)[1],
((UNI_BYTE *)&dwIP)[2],
((UNI_BYTE *)&dwIP)[3],
((UNI_BYTE *)&wPort)[0],
((UNI_BYTE *)&wPort)[1]);
if(m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
}
return UNI_FALSE;
}
UNI_DWORD HFtpClient::Pasv(UNI_WORD &wPort,char *pAckBuf, int bufLen, UNI_DWORD ns)
{
int temp;
UNI_DWORD dwIP;
wPort=0;
dwIP=0;
char szPasv[100]="PASV\r\n";
if(m_Control.Send(szPasv,strlen(szPasv))>0)
{
this->GetAckString(pAckBuf,bufLen,ns);
if(pAckBuf[0]=='2')
{
while (pAckBuf[0]!='(' && pAckBuf[0]!='\r')pAckBuf++;
if(pAckBuf[0]=='\r')
return 0;
pAckBuf++;
strncpy(szPasv,pAckBuf,sizeof(szPasv));
pAckBuf=&szPasv[0];
for(int i=0;i<4;i++)
{
pAckBuf++;
while(pAckBuf[0]!=',')pAckBuf++;
pAckBuf[0]='.';
}
pAckBuf[0]=0;
hostent *p;
if(p=gethostbyname(szPasv))
{
dwIP=*((DWORD *)p->h_addr_list[0]);
}
if(dwIP==0 || dwIP==(UNI_DWORD)-1)
return 0;
for(i=0;i<2;i++)
{
pAckBuf++;
temp=0;
while(pAckBuf[0]>='0' && pAckBuf[0]<='9')
{
temp*=10;
temp+=(pAckBuf[0]-'0');
pAckBuf++;
}
wPort*=256;
wPort+=temp;
}
}
}
return dwIP;
}
UNI_BOOL HFtpClient::CWD(char *szDir, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
if(szDir)
{
strcpy(szTempBuf,"USER ");
strncat(szTempBuf,szDir,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
}
return UNI_FALSE;
}
int HFtpClient::Port_ls(char *szDir, char *recvBuf, int bufLen, UNI_DWORD ns)
{
int len,recvLen=0;
CHTcpSock * s;
char TempBuf[512];
if(this->Port(recvBuf,bufLen,ns))
{
if(s=this->List(szDir,recvBuf,bufLen,ns))
{
while ((len=s->Recv(m_szReturn,sizeof(m_szReturn)))>0)
{
if(recvLen<bufLen)
{
memcpy(recvBuf+recvLen,m_szReturn,((bufLen-recvLen)>len)?len:bufLen-recvLen);
}
recvLen+=len;
}
this->DestroySock(s,TempBuf,sizeof(TempBuf),ns);
return recvLen;
}
}
return 0;
}
int HFtpClient::Pasv_ls(char *szDir, char *recvBuf, int bufLen, UNI_DWORD ns)
{
int len,recvLen=0;
UNI_WORD wPort;
UNI_DWORD dwIP;
CHTcpSock * s;
char TempBuf[512];
dwIP=this->Pasv(wPort,recvBuf,bufLen,ns) ;
if(dwIP!=0 && wPort!=0)
{
bufLen--;
if(s=this->List(dwIP,wPort,szDir,recvBuf,bufLen,ns))
{
recvLen=strlen(recvBuf);
while ((len=s->Recv(m_szReturn,sizeof(m_szReturn)))>0)
{
if(recvLen<bufLen)
{
memcpy(recvBuf+recvLen,m_szReturn,((bufLen-recvLen)>len)?len:bufLen-recvLen);
}
recvLen+=len;
}
if(this->DestroySock(s,TempBuf,sizeof(TempBuf),ns) && recvLen<bufLen)
{
strncat(recvBuf,TempBuf,bufLen);
}
return recvLen;
}
}
return 0;
}
UNI_BOOL HFtpClient::Acct(char *acct, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"ACCT ");
strncat(szTempBuf,acct,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Rein(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="REIN \r\n";
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Quit(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="QUIT \r\n";
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Abor(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="ABOR \r\n";
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Cdup(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="CDUP \r\n";
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Dele(char *fileName, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"DELE ");
strncat(szTempBuf,fileName,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Mkd(char *szDir, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"MKD ");
strncat(szTempBuf,szDir,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Pwd(char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="PWD \r\n";
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Rmd(char *szDir, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
char szTempBuf[512];
strcpy(szTempBuf,"RMD ");
strncat(szTempBuf,szDir,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Type(HFTP_TYPE type, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="TYPE I \r\n";
szTempBuf[5]=type;
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Stru(HFTP_STRU stru, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="STRU F \r\n";
szTempBuf[5]=stru;
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
UNI_BOOL HFtpClient::Mode(HFTP_MODE mode, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
static char szTempBuf[]="MODE S \r\n";
szTempBuf[5]=mode;
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
return this->GetAckString(pAckBuf,bufLen,ns);
}
return UNI_FALSE;
}
CHTcpSock * HFtpClient::GetDataFromListener(char *command, char *arg, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
CHTcpSock *pDataPort;
struct timeval time;
fd_set read_set;
char szTempBuf[512];
if(pDataPort=new CHTcpSock)
{
strcpy(szTempBuf,command);
if(arg)
strncat(szTempBuf,arg,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if(m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
FD_ZERO(&read_set);
this->m_Listener.AddToSet(&read_set);
time.tv_sec=ns/1000;
time.tv_usec=ns%1000;
if(select(0,&read_set,UNI_NULL,UNI_NULL,&time) ==1)
{
if(pDataPort->AcceptConnect(&this->m_Listener))
{
this->GetAckString(pAckBuf,bufLen,ns);
return pDataPort;
}
}
else
{
this->GetAckString(pAckBuf,bufLen,ns);
}
}
delete pDataPort;
}
return UNI_NULL;
}
CHTcpSock* HFtpClient::GetDataFromIP_Port(UNI_DWORD dwIP, UNI_WORD wPort, char *command, char *agr, char *pAckBuf, int bufLen, UNI_DWORD ns)
{
UNI_BOOL bSuccess;
CHTcpSock *pDataPort;
char szTempBuf[512];
if(pDataPort=new CHTcpSock)
{
strcpy(szTempBuf,command);
if(agr)
strncat(szTempBuf,agr,sizeof(szTempBuf));
strncat(szTempBuf,"\r\n",sizeof(szTempBuf));
if(m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
bSuccess=pDataPort->CreateConnect(dwIP,wPort);
this->GetAckString(pAckBuf,bufLen,ns);
if(bSuccess==UNI_TRUE && pAckBuf[0]=='1')
{
return pDataPort;
}
}
delete pDataPort;
}
return UNI_NULL;
}
UNI_BOOL HFtpClient::DestroySock(CHTcpSock *sock, char *recvBuf, int bufLen, UNI_DWORD ns)
{
if(sock)
{
delete sock;
return this->GetAckString(recvBuf,bufLen,ns);
}
return UNI_FALSE;
}
void HFtpClient::ReInitializeControl()
{
struct timeval time;
fd_set read_set;
char szTempBuf[512];
strcpy(szTempBuf,"NOOP\r\n");
if( m_Control.Send(szTempBuf,strlen(szTempBuf))>0)
{
FD_ZERO(&read_set);
m_Control.AddToSet(&read_set);
time.tv_sec=10;
time.tv_usec=0;
while(select(0,&read_set,UNI_NULL,UNI_NULL,&time) ==1)
{
m_Control.Recv(szTempBuf,sizeof(szTempBuf));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -