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 
38 #include "AbstractCommand.h"
39 
40 #include <deque>
41 
42 namespace aria2 {
43 
44 class FtpConnection;
45 class SocketCore;
46 class HttpConnection;
47 
48 class FtpNegotiationCommand : public AbstractCommand {
49 public:
50   enum Seq {
51     SEQ_RECV_GREETING,
52     SEQ_SEND_USER,
53     SEQ_RECV_USER,
54     SEQ_SEND_PASS,
55     SEQ_RECV_PASS,
56     SEQ_SEND_TYPE,
57     SEQ_RECV_TYPE,
58     SEQ_SEND_PWD,
59     SEQ_RECV_PWD,
60     SEQ_SEND_CWD_PREP,
61     SEQ_SEND_CWD,
62     SEQ_RECV_CWD,
63     SEQ_SEND_MDTM,
64     SEQ_RECV_MDTM,
65     SEQ_SEND_SIZE,
66     SEQ_RECV_SIZE,
67     SEQ_PREPARE_PORT,
68     SEQ_PREPARE_SERVER_SOCKET_EPRT,
69     SEQ_SEND_EPRT,
70     SEQ_RECV_EPRT,
71     SEQ_PREPARE_SERVER_SOCKET,
72     SEQ_SEND_PORT,
73     SEQ_RECV_PORT,
74     SEQ_PREPARE_PASV,
75     SEQ_SEND_EPSV,
76     SEQ_RECV_EPSV,
77     SEQ_SEND_PASV,
78     SEQ_RECV_PASV,
79     SEQ_RESOLVE_PROXY,
80     SEQ_SEND_TUNNEL_REQUEST,
81     SEQ_RECV_TUNNEL_RESPONSE,
82     SEQ_SEND_REST_PASV,
83     SEQ_SEND_REST,
84     SEQ_RECV_REST,
85     SEQ_SEND_RETR,
86     SEQ_RECV_RETR,
87     SEQ_WAIT_CONNECTION,
88     SEQ_NEGOTIATION_COMPLETED,
89     SEQ_RETRY,
90     SEQ_HEAD_OK,
91     SEQ_DOWNLOAD_ALREADY_COMPLETED,
92     SEQ_FILE_PREPARATION, // File allocation after SIZE command
93     SEQ_EXIT
94   };
95 
96 private:
97   bool recvGreeting();
98   bool sendUser();
99   bool recvUser();
100   bool sendPass();
101   bool recvPass();
102   bool sendType();
103   bool recvType();
104   bool sendPwd();
105   bool recvPwd();
106   bool sendCwdPrep();
107   bool sendCwd();
108   bool recvCwd();
109   bool sendMdtm();
110   bool recvMdtm();
111   bool sendSize();
112   bool recvSize();
113   bool preparePort();
114   bool prepareServerSocketEprt();
115   bool sendEprt();
116   bool recvEprt();
117   bool prepareServerSocket();
118   bool sendPort();
119   bool recvPort();
120   bool preparePasv();
121   bool sendEpsv();
122   bool recvEpsv();
123   bool sendPasv();
124   bool recvPasv();
125   bool preparePasvConnect();
126   bool resolveProxy();
127   bool sendTunnelRequest();
128   bool recvTunnelResponse();
129   bool sendRest(const std::shared_ptr<Segment>& segment);
130   bool sendRestPasv(const std::shared_ptr<Segment>& segment);
131   bool recvRest(const std::shared_ptr<Segment>& segment);
132   bool sendRetr();
133   bool recvRetr();
134   bool waitConnection();
135   bool processSequence(const std::shared_ptr<Segment>& segment);
136 
137   void afterFileAllocation();
138 
139   void poolConnection() const;
140 
141   bool onFileSizeDetermined(int64_t totalLength);
142 
143   void onDryRunFileFound();
144 
145   std::shared_ptr<SocketCore> dataSocket_;
146   std::shared_ptr<SocketCore> serverSocket_;
147   Seq sequence_;
148   std::shared_ptr<FtpConnection> ftp_;
149   // For tunneling
150   std::shared_ptr<HttpConnection> http_;
151   // Port number in PASV/EPSV response
152   uint16_t pasvPort_;
153   // Resolved address for proxy
154   std::string proxyAddr_;
155 
156   std::deque<std::string> cwdDirs_;
157 
158 protected:
159   virtual bool executeInternal() CXX11_OVERRIDE;
160 
161 public:
162   FtpNegotiationCommand(cuid_t cuid, const std::shared_ptr<Request>& req,
163                         const std::shared_ptr<FileEntry>& fileEntry,
164                         RequestGroup* requestGroup, DownloadEngine* e,
165                         const std::shared_ptr<SocketCore>& s,
166                         Seq seq = SEQ_RECV_GREETING,
167                         const std::string& baseWorkingDir = "/");
168   virtual ~FtpNegotiationCommand();
169 };
170 
171 } // namespace aria2
172 
173 #endif // D_FTP_NEGOTIATION_COMMAND_H
174