FtpControlSocket.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //---------------------------------------------------------------------------
  2. #ifndef FtpControlSocketH
  3. #define FtpControlSocketH
  4. //---------------------------------------------------------------------------
  5. #include "structures.h"
  6. #include "stdafx.h"
  7. #include "FileZillaApi.h"
  8. #include "FileZillaIntf.h"
  9. //---------------------------------------------------------------------------
  10. class CTransferSocket;
  11. class CMainThread;
  12. //---------------------------------------------------------------------------
  13. class CAsyncProxySocketLayer;
  14. class CFtpListResult;
  15. //---------------------------------------------------------------------------
  16. #define CSMODE_NONE 0x0000
  17. #define CSMODE_CONNECT 0x0001
  18. #define CSMODE_COMMAND 0x0002
  19. #define CSMODE_LIST 0x0004
  20. #define CSMODE_TRANSFER 0x0008
  21. #define CSMODE_DOWNLOAD 0x0010
  22. #define CSMODE_UPLOAD 0x0020
  23. #define CSMODE_TRANSFERERROR 0x0040
  24. #define CSMODE_TRANSFERTIMEOUT 0x0080
  25. #define CSMODE_DELETE 0x0100
  26. #define CSMODE_RMDIR 0x0200
  27. #define CSMODE_DISCONNECT 0x0400
  28. #define CSMODE_MKDIR 0x0800
  29. #define CSMODE_RENAME 0x1000
  30. #define CSMODE_CHMOD 0x2000
  31. #define CSMODE_LISTFILE 0x4000
  32. //---------------------------------------------------------------------------
  33. typedef struct
  34. {
  35. BOOL bResume,bResumeAppend,bType;
  36. __int64 transfersize,transferleft;
  37. } t_transferdata;
  38. //---------------------------------------------------------------------------
  39. class CFtpControlSocket : public CAsyncSocketEx, public CApiLog
  40. {
  41. friend CTransferSocket;
  42. public:
  43. CFtpControlSocket(CMainThread * pMainThread, CFileZillaTools * pTools);
  44. virtual ~CFtpControlSocket();
  45. public:
  46. void Connect(t_server & server);
  47. virtual void OnTimer();
  48. BOOL IsReady();
  49. void List(BOOL bFinish, int nError = 0, CServerPath path = CServerPath(), CString subdir = L"");
  50. void ListFile(CString filename, const CServerPath & path);
  51. void FtpCommand(LPCTSTR pCommand);
  52. void Disconnect();
  53. void FileTransfer(t_transferfile * transferfile = 0, BOOL bFinish = FALSE, int nError = 0);
  54. void Delete(CString filename, const CServerPath & path, bool filenameOnly);
  55. void Rename(CString oldName, CString newName, const CServerPath & path, const CServerPath & newPath);
  56. void MakeDir(const CServerPath & path);
  57. void RemoveDir(CString dirname, const CServerPath & path);
  58. void Chmod(CString filename, const CServerPath & path, int nValue);
  59. void ProcessReply();
  60. void TransferEnd(int nMode);
  61. void Cancel(BOOL bQuit = FALSE);
  62. void SetAsyncRequestResult(int nAction, CAsyncRequestData * pData);
  63. BOOL Create();
  64. void TransfersocketListenFinished(unsigned int ip, unsigned short port);
  65. BOOL m_bKeepAliveActive;
  66. BOOL m_bDidRejectCertificate;
  67. // Some servers are broken. Instead of an empty listing, some MVS servers
  68. // for example they return something "550 no members found"
  69. // Other servers return "550 No files found."
  70. bool IsMisleadingListResponse();
  71. bool UsingMlsd();
  72. bool UsingUtf8();
  73. std::string GetTlsVersionStr();
  74. std::string GetCipherName();
  75. bool HandleSize(int code, __int64 & size);
  76. bool HandleMdtm(int code, t_directory::t_direntry::t_date & date);
  77. void TransferHandleListError();
  78. enum transferDirection
  79. {
  80. download = 0,
  81. upload = 1
  82. };
  83. BOOL RemoveActiveTransfer();
  84. BOOL SpeedLimitAddTransferredBytes(enum transferDirection direction, _int64 nBytesTransferred);
  85. _int64 GetSpeedLimit(enum transferDirection direction, CTime & time);
  86. _int64 GetAbleToTransferSize(enum transferDirection direction, bool &beenWaiting);
  87. t_server GetCurrentServer();
  88. CFtpListResult * CreateListResult(bool mlst);
  89. public:
  90. virtual void OnReceive(int nErrorCode);
  91. virtual void OnConnect(int nErrorCode);
  92. virtual void OnClose(int nErrorCode);
  93. virtual void OnSend(int nErrorCode);
  94. protected:
  95. class CFileTransferData;
  96. // Called by OnTimer()
  97. void ResumeTransfer();
  98. void CheckForTimeout();
  99. void SendKeepAliveCommand();
  100. virtual int OnLayerCallback(std::list<t_callbackMsg> & callbacks);
  101. void SetFileExistsAction(int nAction, COverwriteRequestData * pData);
  102. void SetVerifyCertResult(int nResult, t_SslCertData * pData);
  103. void ResetOperation(int nSuccessful = -1);
  104. void ResetTransferSocket(int Error);
  105. int OpenTransferFile(CFileTransferData * pData);
  106. int ActivateTransferSocket(CFileTransferData * pData);
  107. void CancelTransferResume(CFileTransferData * pData);
  108. void DoClose(int nError = 0);
  109. int TryGetReplyCode();
  110. int GetReplyCode();
  111. CString GetReply();
  112. void LogOnToServer(BOOL bSkipReply = FALSE);
  113. BOOL Send(CString str);
  114. BOOL ParsePwdReply(CString & rawpwd);
  115. BOOL ParsePwdReply(CString & rawpwd, CServerPath & realPath);
  116. BOOL SendAuthSsl();
  117. void DiscardLine(RawByteString line);
  118. int FileTransferListState();
  119. bool NeedOptsCommand();
  120. CString GetListingCmd();
  121. bool InitConnect();
  122. int InitConnectState();
  123. bool IsRoutableAddress(const CString & host);
  124. bool CheckForcePasvIp(CString & host);
  125. void TransferFinished(bool preserveFileTimeForUploads);
  126. virtual void LogSocketMessageRaw(int nMessageType, LPCTSTR pMsg);
  127. virtual bool LoggingSocketMessage(int nMessageType);
  128. virtual int GetSocketOptionVal(int OptionID) const;
  129. void ShowStatus(UINT nID, int type) const;
  130. void ShowStatus(CString status,int type) const;
  131. void ShowTimeoutError(UINT nID) const;
  132. void Close();
  133. BOOL Connect(CString hostAddress, UINT nHostPort);
  134. CString ConvertDomainName(CString domain);
  135. bool ConnectTransferSocket(const CString & host, UINT port);
  136. void TransferSocketFailed();
  137. struct t_ActiveList
  138. {
  139. CFtpControlSocket * pOwner;
  140. __int64 nBytesAvailable;
  141. __int64 nBytesTransferred;
  142. };
  143. static std::list<t_ActiveList> m_InstanceList[2];
  144. static CTime m_CurrentTransferTime[2];
  145. static _int64 m_CurrentTransferLimit[2];
  146. static CCriticalSectionWrapper m_SpeedLimitSync;
  147. _int64 GetAbleToUDSize(bool & beenWaiting, CTime & curTime, _int64 & curLimit, std::list<t_ActiveList>::iterator & iter, enum transferDirection direction);
  148. _int64 GetSpeedLimit(CTime & time, int valType, int valValue);
  149. void SetDirectoryListing(t_directory * pDirectory, bool bSetWorkingDir = true);
  150. int CheckOverwriteFile();
  151. int CheckOverwriteFileAndCreateTarget();
  152. int FileTransferHandleDirectoryListing(t_directory * pDirectory);
  153. t_directory * m_pDirectoryListing;
  154. CMainThread * m_pOwner;
  155. CFileZillaTools * m_pTools;
  156. CFile * m_pDataFile;
  157. CTransferSocket * m_pTransferSocket;
  158. RawByteString m_MultiLine;
  159. CTime m_LastSendTime;
  160. CString m_ServerName;
  161. std::list<RawByteString> m_RecvBuffer;
  162. CTime m_LastRecvTime;
  163. class CLogonData;
  164. class CListData;
  165. class CListFileData;
  166. class CMakeDirData;
  167. bool m_bUTF8;
  168. bool m_bAnnouncesUTF8;
  169. bool m_hasClntCmd;
  170. TFTPServerCapabilities m_serverCapabilities;
  171. RawByteString m_ListFile;
  172. __int64 m_ListFileSize;
  173. bool m_isFileZilla;
  174. bool m_awaitsReply;
  175. bool m_skipReply;
  176. char * m_sendBuffer;
  177. int m_sendBufferLen;
  178. bool m_bProtP;
  179. bool m_mayBeMvsFilesystem;
  180. bool m_mayBeBS2000Filesystem;
  181. struct t_operation
  182. {
  183. int nOpMode;
  184. int nOpState;
  185. class COpData //Base class which will store operation specific parameters.
  186. {
  187. public:
  188. COpData() {};
  189. virtual ~COpData() {};
  190. };
  191. COpData * pData;
  192. public:
  193. };
  194. t_operation m_Operation;
  195. CAsyncProxySocketLayer * m_pProxyLayer;
  196. CAsyncSslSocketLayer * m_pSslLayer;
  197. #ifndef MPEXT_NO_GSS
  198. CAsyncGssSocketLayer * m_pGssLayer;
  199. #endif
  200. t_server m_CurrentServer;
  201. private:
  202. BOOL m_bCheckForTimeout;
  203. };
  204. //---------------------------------------------------------------------------
  205. #endif // FtpControlSocketH