FtpControlSocket.h 7.6 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. int CheckOverwriteFile();
  64. BOOL Create();
  65. void TransfersocketListenFinished(unsigned int ip, unsigned short port);
  66. BOOL m_bKeepAliveActive;
  67. BOOL m_bDidRejectCertificate;
  68. // Some servers are broken. Instead of an empty listing, some MVS servers
  69. // for example they return something "550 no members found"
  70. // Other servers return "550 No files found."
  71. bool IsMisleadingListResponse();
  72. bool UsingMlsd();
  73. bool UsingUtf8();
  74. std::string GetTlsVersionStr();
  75. std::string GetCipherName();
  76. bool HandleSize(int code, __int64 & size);
  77. bool HandleMdtm(int code, t_directory::t_direntry::t_date & date);
  78. void TransferHandleListError();
  79. enum transferDirection
  80. {
  81. download = 0,
  82. upload = 1
  83. };
  84. BOOL RemoveActiveTransfer();
  85. BOOL SpeedLimitAddTransferredBytes(enum transferDirection direction, _int64 nBytesTransferred);
  86. _int64 GetSpeedLimit(enum transferDirection direction, CTime & time);
  87. _int64 GetAbleToTransferSize(enum transferDirection direction, bool &beenWaiting, int nBufSize = 0);
  88. t_server GetCurrentServer();
  89. CFtpListResult * CreateListResult(bool mlst);
  90. public:
  91. virtual void OnReceive(int nErrorCode);
  92. virtual void OnConnect(int nErrorCode);
  93. virtual void OnClose(int nErrorCode);
  94. virtual void OnSend(int nErrorCode);
  95. protected:
  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. void DoClose(int nError = 0);
  106. int GetReplyCode();
  107. CString GetReply();
  108. void LogOnToServer(BOOL bSkipReply = FALSE);
  109. BOOL Send(CString str);
  110. BOOL ParsePwdReply(CString & rawpwd);
  111. BOOL ParsePwdReply(CString & rawpwd, CServerPath & realPath);
  112. BOOL SendAuthSsl();
  113. void DiscardLine(CStringA line);
  114. int FileTransferListState(bool get);
  115. bool NeedModeCommand();
  116. bool NeedOptsCommand();
  117. CString GetListingCmd();
  118. bool InitConnect();
  119. int InitConnectState();
  120. bool IsRoutableAddress(const CString & host);
  121. bool CheckForcePasvIp(CString & host);
  122. void TransferFinished(bool preserveFileTimeForUploads);
  123. virtual void LogSocketMessageRaw(int nMessageType, LPCTSTR pMsg);
  124. virtual bool LoggingSocketMessage(int nMessageType);
  125. virtual int GetSocketOptionVal(int OptionID) const;
  126. void ShowStatus(UINT nID, int type) const;
  127. void ShowStatus(CString status,int type) const;
  128. void ShowTimeoutError(UINT nID) const;
  129. void Close();
  130. BOOL Connect(CString hostAddress, UINT nHostPort);
  131. CString ConvertDomainName(CString domain);
  132. bool ConnectTransferSocket(const CString & host, UINT port);
  133. struct t_ActiveList
  134. {
  135. CFtpControlSocket * pOwner;
  136. __int64 nBytesAvailable;
  137. __int64 nBytesTransferred;
  138. };
  139. static std::list<t_ActiveList> m_InstanceList[2];
  140. static CTime m_CurrentTransferTime[2];
  141. static _int64 m_CurrentTransferLimit[2];
  142. static CCriticalSection m_SpeedLimitSync;
  143. _int64 GetAbleToUDSize(bool & beenWaiting, CTime & curTime, _int64 & curLimit, std::list<t_ActiveList>::iterator & iter, enum transferDirection direction, int nBufSize);
  144. _int64 GetSpeedLimit(CTime & time, int valType, int valValue);
  145. void SetDirectoryListing(t_directory * pDirectory, bool bSetWorkingDir = true);
  146. t_directory * m_pDirectoryListing;
  147. CMainThread * m_pOwner;
  148. CFileZillaTools * m_pTools;
  149. CFile * m_pDataFile;
  150. CTransferSocket * m_pTransferSocket;
  151. CStringA m_MultiLine;
  152. CTime m_LastSendTime;
  153. CString m_ServerName;
  154. std::list<CStringA> m_RecvBuffer;
  155. CTime m_LastRecvTime;
  156. class CLogonData;
  157. class CListData;
  158. class CListFileData;
  159. class CFileTransferData;
  160. class CMakeDirData;
  161. #ifndef MPEXT_NO_ZLIB
  162. bool m_useZlib;
  163. bool m_zlibSupported;
  164. int m_zlibLevel;
  165. #endif
  166. bool m_bUTF8;
  167. bool m_bAnnouncesUTF8;
  168. bool m_hasClntCmd;
  169. TFTPServerCapabilities m_serverCapabilities;
  170. CStringA m_ListFile;
  171. __int64 m_ListFileSize;
  172. bool m_isFileZilla;
  173. bool m_awaitsReply;
  174. bool m_skipReply;
  175. char * m_sendBuffer;
  176. int m_sendBufferLen;
  177. bool m_bProtP;
  178. bool m_mayBeMvsFilesystem;
  179. bool m_mayBeBS2000Filesystem;
  180. struct t_operation
  181. {
  182. int nOpMode;
  183. int nOpState;
  184. class COpData //Base class which will store operation specific parameters.
  185. {
  186. public:
  187. COpData() {};
  188. virtual ~COpData() {};
  189. };
  190. COpData * pData;
  191. public:
  192. };
  193. t_operation m_Operation;
  194. CAsyncProxySocketLayer * m_pProxyLayer;
  195. CAsyncSslSocketLayer * m_pSslLayer;
  196. #ifndef MPEXT_NO_GSS
  197. CAsyncGssSocketLayer * m_pGssLayer;
  198. #endif
  199. t_server m_CurrentServer;
  200. private:
  201. BOOL m_bCheckForTimeout;
  202. };
  203. //---------------------------------------------------------------------------
  204. #endif // FtpControlSocketH