1616 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717 */
1818
19+ #include " SipPlatformDefine.h"
1920#include " FtpClient.h"
2021
21- CFtpClient::CFtpClient () : m_hSocket(INVALID_SOCKET ), m_iServerPort(21 )
22+ CFtpClient::CFtpClient () : m_hSocket(INVALID_SOCKET ), m_iServerPort(21 ), m_iTimeout( 10 )
2223{
24+ InitNetwork ();
2325}
2426
2527CFtpClient::~CFtpClient ()
@@ -40,6 +42,12 @@ bool CFtpClient::Connect( const char * pszServerIp, int iServerPort )
4042 m_strServerIp = pszServerIp;
4143 m_iServerPort = iServerPort;
4244
45+ if ( Recv ( 220 ) == false )
46+ {
47+ Close ();
48+ return false ;
49+ }
50+
4351 return true ;
4452}
4553
@@ -54,18 +62,92 @@ void CFtpClient::Close()
5462
5563bool CFtpClient::Login ( const char * pszUserId, const char * pszPassWord )
5664{
65+ if ( Send ( " USER %s" , pszUserId ) == false )
66+ {
67+ return false ;
68+ }
69+
70+ if ( Recv ( 331 ) == false )
71+ {
72+ return false ;
73+ }
74+
75+ if ( Send ( " PASS %s" , pszPassWord ) == false )
76+ {
77+ return false ;
78+ }
79+
80+ if ( Recv ( 230 ) == false )
81+ {
82+ return false ;
83+ }
5784
5885 return true ;
5986}
6087
61- bool CFtpClient::Send ( const char * pszSend )
88+ bool CFtpClient::Send ( const char * fmt, ... )
6289{
90+ va_list ap;
91+ char szSendBuf[8192 ];
92+ int iSendLen;
93+
94+ va_start ( ap, fmt );
95+ iSendLen = vsnprintf ( szSendBuf, sizeof (szSendBuf)-3 , fmt, ap );
96+ va_end ( ap );
97+
98+ snprintf ( szSendBuf + iSendLen, sizeof (szSendBuf) - iSendLen, " \r\n " );
99+ iSendLen += 2 ;
100+
101+ if ( TcpSend ( m_hSocket, szSendBuf, iSendLen ) != iSendLen )
102+ {
103+ CLog::Print ( LOG_ERROR , " %s TcpSend(%s:%d) [%.*s] error(%d)" , __FUNCTION__, m_strServerIp.c_str (), m_iServerPort, iSendLen, szSendBuf, GetError () );
104+ return false ;
105+ }
106+
107+ CLog::Print ( LOG_NETWORK , " TcpSend(%s:%d) [%.*s]" , m_strServerIp.c_str (), m_iServerPort, iSendLen, szSendBuf );
63108
64109 return true ;
65110}
66111
67- bool CFtpClient::Recv ( std::string & strRecv )
112+ bool CFtpClient::Recv ( CFtpResponse & clsResponse, int iWantCode )
68113{
114+ std::string strRecvBuf;
115+ char szRecvBuf[8192 ];
116+ int n;
117+
118+ while ( 1 )
119+ {
120+ n = TcpRecv ( m_hSocket, szRecvBuf, sizeof (szRecvBuf), m_iTimeout );
121+ if ( n <= 0 )
122+ {
123+ CLog::Print ( LOG_ERROR , " %s TcpRecv(%s) error(%d)" , __FUNCTION__, strRecvBuf.c_str (), GetError () );
124+ return false ;
125+ }
126+
127+ CLog::Print ( LOG_NETWORK , " TcpRecv(%s:%d) [%.*s]" , m_strServerIp.c_str (), m_iServerPort, n, szRecvBuf );
128+
129+ strRecvBuf.append ( szRecvBuf, n );
130+ if ( clsResponse.Parse ( strRecvBuf.c_str (), (int )strRecvBuf.length () ) > 0 )
131+ {
132+ break ;
133+ }
134+ }
135+
136+ if ( iWantCode )
137+ {
138+ if ( clsResponse.m_iCode != iWantCode )
139+ {
140+ CLog::Print ( LOG_ERROR , " %s reply code(%d) != want code(%d)" , __FUNCTION__, clsResponse.m_iCode , iWantCode );
141+ return false ;
142+ }
143+ }
69144
70145 return true ;
71146}
147+
148+ bool CFtpClient::Recv ( int iWantCode )
149+ {
150+ CFtpResponse clsRes;
151+
152+ return Recv ( clsRes, iWantCode );
153+ }
0 commit comments