FtpNegotiationCommand.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_FTP_NEGOTIATION_COMMAND_H
  36. #define D_FTP_NEGOTIATION_COMMAND_H
  37. #include "AbstractCommand.h"
  38. #include <deque>
  39. namespace aria2 {
  40. class FtpConnection;
  41. class SocketCore;
  42. class HttpConnection;
  43. class FtpNegotiationCommand : public AbstractCommand {
  44. public:
  45. enum Seq {
  46. SEQ_RECV_GREETING,
  47. SEQ_SEND_USER,
  48. SEQ_RECV_USER,
  49. SEQ_SEND_PASS,
  50. SEQ_RECV_PASS,
  51. SEQ_SEND_TYPE,
  52. SEQ_RECV_TYPE,
  53. SEQ_SEND_PWD,
  54. SEQ_RECV_PWD,
  55. SEQ_SEND_CWD_PREP,
  56. SEQ_SEND_CWD,
  57. SEQ_RECV_CWD,
  58. SEQ_SEND_MDTM,
  59. SEQ_RECV_MDTM,
  60. SEQ_SEND_SIZE,
  61. SEQ_RECV_SIZE,
  62. SEQ_PREPARE_PORT,
  63. SEQ_PREPARE_SERVER_SOCKET_EPRT,
  64. SEQ_SEND_EPRT,
  65. SEQ_RECV_EPRT,
  66. SEQ_PREPARE_SERVER_SOCKET,
  67. SEQ_SEND_PORT,
  68. SEQ_RECV_PORT,
  69. SEQ_PREPARE_PASV,
  70. SEQ_SEND_EPSV,
  71. SEQ_RECV_EPSV,
  72. SEQ_SEND_PASV,
  73. SEQ_RECV_PASV,
  74. SEQ_RESOLVE_PROXY,
  75. SEQ_SEND_TUNNEL_REQUEST,
  76. SEQ_RECV_TUNNEL_RESPONSE,
  77. SEQ_SEND_REST_PASV,
  78. SEQ_SEND_REST,
  79. SEQ_RECV_REST,
  80. SEQ_SEND_RETR,
  81. SEQ_RECV_RETR,
  82. SEQ_WAIT_CONNECTION,
  83. SEQ_NEGOTIATION_COMPLETED,
  84. SEQ_RETRY,
  85. SEQ_HEAD_OK,
  86. SEQ_DOWNLOAD_ALREADY_COMPLETED,
  87. SEQ_FILE_PREPARATION, // File allocation after SIZE command
  88. SEQ_EXIT
  89. };
  90. private:
  91. bool recvGreeting();
  92. bool sendUser();
  93. bool recvUser();
  94. bool sendPass();
  95. bool recvPass();
  96. bool sendType();
  97. bool recvType();
  98. bool sendPwd();
  99. bool recvPwd();
  100. bool sendCwdPrep();
  101. bool sendCwd();
  102. bool recvCwd();
  103. bool sendMdtm();
  104. bool recvMdtm();
  105. bool sendSize();
  106. bool recvSize();
  107. bool preparePort();
  108. bool prepareServerSocketEprt();
  109. bool sendEprt();
  110. bool recvEprt();
  111. bool prepareServerSocket();
  112. bool sendPort();
  113. bool recvPort();
  114. bool preparePasv();
  115. bool sendEpsv();
  116. bool recvEpsv();
  117. bool sendPasv();
  118. bool recvPasv();
  119. bool preparePasvConnect();
  120. bool resolveProxy();
  121. bool sendTunnelRequest();
  122. bool recvTunnelResponse();
  123. bool sendRest(const std::shared_ptr<Segment>& segment);
  124. bool sendRestPasv(const std::shared_ptr<Segment>& segment);
  125. bool recvRest(const std::shared_ptr<Segment>& segment);
  126. bool sendRetr();
  127. bool recvRetr();
  128. bool waitConnection();
  129. bool processSequence(const std::shared_ptr<Segment>& segment);
  130. void afterFileAllocation();
  131. void poolConnection() const;
  132. bool onFileSizeDetermined(int64_t totalLength);
  133. void onDryRunFileFound();
  134. std::shared_ptr<SocketCore> dataSocket_;
  135. std::shared_ptr<SocketCore> serverSocket_;
  136. Seq sequence_;
  137. std::shared_ptr<FtpConnection> ftp_;
  138. // For tunneling
  139. std::shared_ptr<HttpConnection> http_;
  140. // Port number in PASV/EPSV response
  141. uint16_t pasvPort_;
  142. // Resolved address for proxy
  143. std::string proxyAddr_;
  144. std::deque<std::string> cwdDirs_;
  145. protected:
  146. virtual bool executeInternal() CXX11_OVERRIDE;
  147. public:
  148. FtpNegotiationCommand(cuid_t cuid, const std::shared_ptr<Request>& req,
  149. const std::shared_ptr<FileEntry>& fileEntry,
  150. RequestGroup* requestGroup, DownloadEngine* e,
  151. const std::shared_ptr<SocketCore>& s,
  152. Seq seq = SEQ_RECV_GREETING,
  153. const std::string& baseWorkingDir = "/");
  154. virtual ~FtpNegotiationCommand();
  155. };
  156. } // namespace aria2
  157. #endif // D_FTP_NEGOTIATION_COMMAND_H