Skip to content

Commit 06f50a7

Browse files
committed
FTP 로그인 성공함
1 parent d1b5fda commit 06f50a7

7 files changed

Lines changed: 283 additions & 5 deletions

File tree

FtpStack/FtpClient.cpp

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
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

2527
CFtpClient::~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

5563
bool 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+
}

FtpStack/FtpClient.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define _FTP_CLIENT_H_
2121

2222
#include "SipTcp.h"
23+
#include "FtpResponse.h"
2324

2425
class CFtpClient
2526
{
@@ -33,12 +34,14 @@ class CFtpClient
3334
bool Login( const char * pszUserId, const char * pszPassWord );
3435

3536
private:
36-
bool Send( const char * pszSend );
37-
bool Recv( std::string & strRecv );
37+
bool Send( const char * fmt, ... );
38+
bool Recv( CFtpResponse & clsResponse, int iWantCode = 0 );
39+
bool Recv( int iWantCode );
3840

3941
Socket m_hSocket;
4042
std::string m_strServerIp;
4143
int m_iServerPort;
44+
int m_iTimeout;
4245
};
4346

4447
#endif

FtpStack/FtpResponse.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*/
18+
19+
#include "FtpResponse.h"
20+
#include <stdlib.h>
21+
#include "MemoryDebug.h"
22+
23+
CFtpResponse::CFtpResponse() : m_iCode(0)
24+
{
25+
}
26+
27+
CFtpResponse::~CFtpResponse()
28+
{
29+
}
30+
31+
/**
32+
* @ingroup FtpStack
33+
* @brief FTP 응답 메시지를 파싱하여서 내부 자료구조에 저장한다.
34+
* @param pszText FTP 응답 메시지
35+
* @param iTextLen FTP 응답 메시지 길이
36+
* @returns 성공하면 파싱된 문자열의 길이를 리턴하고 그렇지 않으면 -1 을 리턴한다.
37+
*/
38+
int CFtpResponse::Parse( const char * pszText, int iTextLen )
39+
{
40+
int iPos = 0, iLen;
41+
bool bLastLine = false;
42+
43+
m_iCode = 0;
44+
m_clsReplyList.clear();
45+
46+
while( iPos < iTextLen )
47+
{
48+
iLen = ParseLine( pszText + iPos, iTextLen - iPos, bLastLine );
49+
if( iLen == -1 ) return -1;
50+
iPos += iLen;
51+
52+
if( bLastLine ) break;
53+
}
54+
55+
return iPos;
56+
}
57+
58+
/**
59+
* @ingroup FtpStack
60+
* @brief FTP 응답 메시지의 1라인을 파싱하여서 내부 자료구조에 저장한다.
61+
* @param pszText FTP 응답 메시지
62+
* @param iTextLen FTP 응답 메시지 길이
63+
* @param bLastLine [out] 마지막 라인이면 본 메소드 호출후, true 가 저장되고 그렇지 않으면 false 가 저장된다.
64+
* @returns 성공하면 파싱된 문자열의 길이를 리턴하고 그렇지 않으면 -1 을 리턴한다.
65+
*/
66+
int CFtpResponse::ParseLine( const char * pszText, int iTextLen, bool & bLastLine )
67+
{
68+
// reply code(3byte) space(1byte) CRLF(2byte)
69+
if( iTextLen < 6 )
70+
{
71+
// code(3byte) CRLF(2byte)
72+
if( iTextLen == 5 )
73+
{
74+
for( int i = 0; i < 3; ++i )
75+
{
76+
if( isdigit( pszText[i] ) == 0 ) return -1;
77+
}
78+
79+
if( pszText[3] == '\r' && pszText[4] == '\n' )
80+
{
81+
bLastLine = true;
82+
83+
std::string strCode;
84+
strCode.append( pszText, 3 );
85+
86+
m_iCode = atoi( strCode.c_str() );
87+
88+
return iTextLen;
89+
}
90+
}
91+
92+
return -1;
93+
}
94+
95+
for( int i = 0; i < 3; ++i )
96+
{
97+
if( isdigit( pszText[i] ) == 0 ) return -1;
98+
}
99+
100+
int iLen = -1;
101+
102+
if( pszText[3] == ' ' )
103+
{
104+
bLastLine = true;
105+
106+
std::string strCode;
107+
strCode.append( pszText, 3 );
108+
109+
m_iCode = atoi( strCode.c_str() );
110+
}
111+
else
112+
{
113+
bLastLine = false;
114+
}
115+
116+
for( int i = 5; i < iTextLen; ++i )
117+
{
118+
if( pszText[i-1] == '\r' && pszText[i] == '\n' )
119+
{
120+
std::string strLine;
121+
122+
strLine.append( pszText + 4, i - 5 );
123+
m_clsReplyList.push_back( strLine );
124+
125+
iLen = i + 1;
126+
break;
127+
}
128+
}
129+
130+
return iLen;
131+
}

FtpStack/FtpResponse.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*/
18+
19+
#ifndef _FTP_RESPONSE_H_
20+
#define _FTP_RESPONSE_H_
21+
22+
#include "StringUtility.h"
23+
24+
/**
25+
* @ingroup FtpStack
26+
* @brief FTP 응답 메시지를 파싱하여서 저장하는 클래스
27+
*/
28+
class CFtpResponse
29+
{
30+
public:
31+
CFtpResponse();
32+
~CFtpResponse();
33+
34+
int Parse( const char * pszText, int iTextLen );
35+
int ParseLine( const char * pszText, int iTextLen, bool & bLastLine );
36+
37+
/** FTP 응답 코드 */
38+
int m_iCode;
39+
40+
/** FTP 응답 메시지 리스트 */
41+
STRING_LIST m_clsReplyList;
42+
};
43+
44+
#endif

FtpStack/FtpStack.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@
154154
RelativePath=".\FtpClient.cpp"
155155
>
156156
</File>
157+
<File
158+
RelativePath=".\FtpResponse.cpp"
159+
>
160+
</File>
157161
</Filter>
158162
<Filter
159163
Name="Çì´õ ÆÄÀÏ"
@@ -164,6 +168,10 @@
164168
RelativePath=".\FtpClient.h"
165169
>
166170
</File>
171+
<File
172+
RelativePath=".\FtpResponse.h"
173+
>
174+
</File>
167175
</Filter>
168176
</Files>
169177
<Globals>

TestFtpStack/TestFtpStack.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ int main( int argc, char * argv[] )
1414

1515
CFtpClient clsFtp;
1616

17+
CLog::SetLevel( LOG_DEBUG | LOG_NETWORK );
18+
1719
if( clsFtp.Connect( pszServerIp ) == false )
1820
{
1921
printf( "clsFtp.Connect(%s) error\n", pszServerIp );
2022
}
2123
else
2224
{
25+
if( clsFtp.Login( pszUserId, pszPassWord ) == false )
26+
{
27+
printf( "login error\n" );
28+
}
29+
else
30+
{
2331

32+
}
2433
}
2534

2635
return 0;

TestFtpStack/TestFtpStack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define _TEST_FTP_STACK_H_
33

44
#include "FtpClient.h"
5+
#include "Log.h"
56

67
#endif

0 commit comments

Comments
 (0)