Start an FTP session (establish a connection witht the FTP server) Arguments: None

Return TypeFunction nameArguments
hzEcodehzFtpClient::StartSession(void)

Declared in file: hzFtpClient.h
Defined in file : hzFtpClient.cpp

Function Logic:

0:START 1:m_nTries 2:m_nTries<5; 3:_hzGlobal_Debug&HZ_DEBUG_CLIENT 4:hzTcpClient::ConnectStd rc 5:rc!=E_OK 6:_hzGlobal_Debug&HZ_DEBUG_CLIENT 7:Err2Txt 8:m_nTries 9:rc!=E_OK 10:Return rc 11:_hzGlobal_Debug&HZ_DEBUG_CLIENT 12:hzTcpClient::Sock 13:hzTcpClient::SetSendTimeout rc 14:rc!=E_OK 15:hzTcpClient::Close 16:Return rc 17:hzTcpClient::SetRecvTimeout rc 18:rc!=E_OK 19:hzTcpClient::Close 20:Return rc 21:_hzGlobal_Debug&HZ_DEBUG_CLIENT 22:hzFtpClient::_ftprecv rc 23:rc!=E_OK 24:hzTcpClient::Close 25:Return rc 26:m_nRescode!=220 27:hzTcpClient::Close 28:Return E_PROTOCOL 29:_hzGlobal_Debug&HZ_DEBUG_CLIENT 30:sprintf strlen len hzTcpClient::Send rc 31:rc!=E_OK 32:hzTcpClient::Close 33:Return rc 34:hzFtpClient::_ftprecv rc 35:rc!=E_OK 36:hzTcpClient::Close 37:Return rc 38:hzFtpClient::_logrescode 39:m_nRescode!=331 40:hzFtpClient::QuitSession rc 41:Return rc 42:sprintf strlen len 43:(rc=m_ConnControl.Send(m_c_sbuf,len))!=E_OK 44:hzTcpClient::Close 45:Return rc 46:sleep 47:(rc=_ftprecv(nRecv,*_fn))!=E_OK 48:hzTcpClient::Close 49:Return rc 50:hzFtpClient::_logrescode 51:m_nRescode>=400 52:hzFtpClient::QuitSession rc 53:Return rc 54:hzFtpClient::GetServerDir rc 55:rc!=E_OK 56:Return rc 57:m_nTries 58:Return rc

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 ;
}