Start an FTP session (establish a connection witht the FTP server) Arguments: None
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzFtpClient::StartSession | (void) |
Declared in file: hzFtpClient.h
Defined in file : hzFtpClient.cpp
Function Logic:
Function body:
hzEcode hzFtpClient::StartSession (void)
{
// Start an FTP session (establish a connection witht the FTP server)
//
// Arguments: None
//
// Returns: E_HOSTFAIL If the initial control channel connection could not be established. The usual remedy is to try again later.
// E_SENDFAIL If either the username or password could not be sent. Broken pipe so try again later.
// E_RECVFAIL If the expected reponses were not recieved. Again a broken pipe so try again later.
// E_PROTOCOL If either the username or password was invalid. Fatal error as this must be sorted out before trying again.
// E_OK If the operation was successful.
_hzfunc("hzFtpClient::StartSession") ;
std::ofstream os ;
std::ifstream is ;
hzChain CR ;
hzChain XR ;
hzEcode rc ;
uint32_t nRecv ;
uint32_t len ;
/*
** ** Connect to server and set socket timeouts
** */
for (m_nTries = 0; m_nTries < 5; m_nTries++)
{
if (_hzGlobal_Debug & HZ_DEBUG_CLIENT)
threadLog("Calling Connect for control channel at server %s\n", *m_Server) ;
rc = m_ConnControl.ConnectStd(m_Server, 21);
if (rc != E_OK)
{
if (_hzGlobal_Debug & HZ_DEBUG_CLIENT)
threadLog("Attempt %d: Could not connect to FTP server control channel. (error=%s)\n", m_nTries, Err2Txt(rc)) ;
continue ;
}
m_nTries = 0;
break ;
}
if (rc != E_OK)
{
threadLog("Given up!\n") ;
return rc ;
}
if (_hzGlobal_Debug & HZ_DEBUG_CLIENT)
threadLog("Connected to control channel at server %s with sock %d on port 21\n", *m_Server, m_ConnControl.Sock()) ;
rc = m_ConnControl.SetSendTimeout(10);
if (rc != E_OK)
{
threadLog("Could not set send timeout: Quitting\n") ;
m_ConnControl.Close() ;
return rc ;
}
rc = m_ConnControl.SetRecvTimeout(10);
if (rc != E_OK)
{
threadLog("Could not set send timeout: Quitting\n") ;
m_ConnControl.Close() ;
return rc ;
}
if (_hzGlobal_Debug & HZ_DEBUG_CLIENT)
threadLog("Send & Recv timeouts set\n") ;
/*
** ** Receive server hello
** */
rc = _ftprecv(nRecv, *_fn) ;
if (rc != E_OK)
{
threadLog("Did not get server hello\n") ;
m_ConnControl.Close() ;
return rc ;
}
if (m_nRescode != 220)
{
threadLog("Expected a 220, got %s\n", m_c_rbuf) ;
m_ConnControl.Close() ;
return E_PROTOCOL ;
}
if (_hzGlobal_Debug & HZ_DEBUG_CLIENT)
threadLog("Got server hello ....\n") ;
/*
** ** Send username
** */
sprintf(m_c_sbuf, "USER %s\r\n", *m_Username) ;
len = strlen(m_c_sbuf) ;
rc = m_ConnControl.Send(m_c_sbuf, len) ;
if (rc != E_OK)
{
threadLog("Failed to send USER command\n") ;
m_ConnControl.Close() ;
return rc ;
}
rc = _ftprecv(nRecv, *_fn) ;
if (rc != E_OK)
{
threadLog("Failed to receive respose to USER command\n") ;
m_ConnControl.Close() ;
return rc ;
}
_logrescode() ;
if (m_nRescode != 331)
{
threadLog("Bad response to USER. Expected a 331, got %d. Presume username %s invalid\n", m_nRescode, *m_Username) ;
QuitSession() ;
rc = E_PROTOCOL ;
return rc ;
}
/*
** ** Send password
** */
sprintf(m_c_sbuf, "PASS %s\r\n", *m_Password) ;
len = strlen(m_c_sbuf) ;
if ((rc = m_ConnControl.Send(m_c_sbuf, len)) != E_OK)
{
threadLog("PASS: Send failed\n") ;
m_ConnControl.Close() ;
return rc ;
}
sleep(1);
if ((rc = _ftprecv(nRecv, *_fn)) != E_OK)
{
threadLog("Could not recv response to PASS command\n") ;
m_ConnControl.Close() ;
return rc ;
}
_logrescode() ;
if (m_nRescode >&eq; 400)
{
threadLog("Aborting Session\n") ;
QuitSession() ;
rc = E_PROTOCOL ;
return rc ;
}
rc = GetServerDir() ;
if (rc != E_OK)
{
threadLog("Could not get server directory\n") ;
return rc ;
}
threadLog("Logged in to %s as %s\n", *m_Server, *m_Username) ;
m_nTries = 0;
return rc ;
}