6 #include "SessionController.hpp"
8 #include "Exceptions.hpp"
10 int Session::m_counter
= 0;
12 Session::Session( int controlSock
, const SessionControllerPtr
& sessionController
, const AuthPtr
& auth
)
13 : m_control( new Telnet( controlSock
) )
14 , m_controlSock( controlSock
)
16 , m_state( S_GREETING
)
17 , m_sessionController( sessionController
)
20 std::cout
<< "[Session] Initializing session " << m_id
<< std::endl
;
22 // Set socket to non-blocking
23 if( fcntl( m_controlSock
, F_SETFL
, O_NONBLOCK
) == -1 )
25 throw strerror( errno
);
31 if( m_controlSock
!= 0 )
33 std::cout
<< "[Session] Closing control socket " << m_id
<< std::endl
;
34 close( m_controlSock
);
38 SessionPtr
Session::Create( int controlSock
, const SessionControllerPtr
& sessionController
, const AuthPtr
& auth
)
40 SessionPtr
ret( new Session( controlSock
, sessionController
, auth
) );
68 PassState state
= AwaitPassword();
70 if( state
== PS_LOGGEDIN
)
74 else if( state
== PS_BADPASS
)
89 catch( SyntaxError
& e
)
93 catch( QuitRequested
& e
)
95 m_control
->Write( "221 Bye" );
99 catch( ConnectionTerminated
& e
)
101 std::cout
<< "[Session] Connection " << m_id
<< " terminated\n";
106 void Session::Remove()
108 SessionControllerPtr sessionController
= m_sessionController
.lock();
110 if( !sessionController
)
112 throw "Session lost SessionController";
115 sessionController
->Remove( m_this
.lock() );
118 void Session::SendGreeting()
120 m_control
->Write( "220 Dumb FTP Server ready" );
123 void Session::SendSyntaxError()
125 m_control
->Write( "500 Syntax error" );
128 void Session::SendNotLoggedIn()
130 m_control
->Write( "530 Not logged in" );
133 bool Session::AwaitLogin()
135 if( m_control
->Read() )
137 Command cmd
= GetCommand();
139 if( cmd
[0] == "USER" )
141 if( cmd
.size() != 2 )
147 if( m_auth
->Login( cmd
[1] ) )
149 m_control
->Write( "331 Need password" );
159 else if( cmd
[0] == "QUIT" )
161 throw QuitRequestedException
;
172 Session::PassState
Session::AwaitPassword()
174 if( m_control
->Read() )
176 Command cmd
= GetCommand();
178 if( cmd
[0] == "PASS" )
180 if( cmd
.size() != 2 )
186 if( m_auth
->Password( cmd
[1] ) )
188 m_control
->Write( "230 Logged in" );
197 else if( cmd
[0] == "QUIT" )
199 throw QuitRequestedException
;
211 void Session::AwaitReady()
213 if( m_control
->Read() )
215 Command cmd
= GetCommand();
217 if( cmd
[0] == "QUIT" )
219 throw QuitRequestedException
;
221 else if( cmd
[0] == "NOOP" )
223 m_control
->Write( "200 OK" );
225 else if( cmd
[0] == "MODE" )
229 else if( cmd
[0] == "TYPE" )
233 else if( cmd
[0] == "STRU" )
239 throw SyntaxErrorException
;
244 void Session::HandleMode( const Command
& cmd
)
246 if( cmd
.size() != 2 )
248 throw SyntaxErrorException
;
251 std::string param
= cmd
[1];
256 m_control
->Write( "200 OK" );
258 else if( param
== "B" || param
== "C" )
260 m_control
->Write( "504 Not implemented" );
264 throw SyntaxErrorException
;
268 void Session::HandleType( const Command
& cmd
)
270 if( cmd
.size() < 2 || cmd
.size() > 3)
272 throw SyntaxErrorException
;
275 std::string param
= cmd
[1];
280 if( cmd
.size() == 3 )
282 std::string param2
= cmd
[2];
287 m_control
->Write( "504 Not implemented" );
292 m_control
->Write( "200 OK" );
294 else if( param
== "E" || param
== "I" || param
== "L" )
296 m_control
->Write( "504 Not implemented" );
300 throw SyntaxErrorException
;
304 void Session::HandleStru( const Command
& cmd
)
306 if( cmd
.size() != 2 )
308 throw SyntaxErrorException
;
311 std::string param
= cmd
[1];
316 m_control
->Write( "200 OK" );
318 else if( param
== "R" )
320 throw "STRU R not implemented";
322 else if( param
== "P" )
324 m_control
->Write( "504 Not implemented" );
328 throw SyntaxErrorException
;