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:unknown 2:unknown 3:items 4:rc 5:unknown 6:unknown 7:items 8:m_nTries 9:unknown 10:items 11:Return rc 12:unknown 13:items 14:rc 15:unknown 16:items items 17:Return rc 18:rc 19:unknown 20:items items 21:Return rc 22:unknown 23:items 24:rc 25:unknown 26:items items 27:Return rc 28:unknown 29:items items 30:Return E_PROTOCOL 31:unknown 32:items 33:items len rc 34:unknown 35:items items 36:Return rc 37:rc 38:unknown 39:items items 40:Return rc 41:items 42:unknown 43:items items rc 44:Return rc 45:items len 46:unknown 47:items items 48:Return rc 49:items 50:unknown 51:items items 52:Return rc 53:items 54:unknown 55:items items rc 56:Return rc 57:rc 58:unknown 59:items 60:Return rc 61:items m_nTries 62: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 ;        //  Out to dirlist file
   std::ifstream   is ;        //  Read back dirlist file
   hzChain     CR ;        //  Receiver chain (control)
   hzChain     XR ;        //  Receiver chain (data)
   hzEcode     rc ;        //  Return code from publication functions
   uint32_t    nRecv ;         //  Bytes recv
   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 ;
}