FtpNegotiationCommand.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #include "FtpNegotiationCommand.h"
  36. #include "Request.h"
  37. #include "DownloadEngine.h"
  38. #include "FtpConnection.h"
  39. #include "RequestGroup.h"
  40. #include "PieceStorage.h"
  41. #include "FtpDownloadCommand.h"
  42. #include "FileEntry.h"
  43. #include "DlAbortEx.h"
  44. #include "message.h"
  45. #include "prefs.h"
  46. #include "Util.h"
  47. #include "Option.h"
  48. #include "Logger.h"
  49. #include "Segment.h"
  50. #include "SingleFileDownloadContext.h"
  51. #include "DefaultBtProgressInfoFile.h"
  52. #include "RequestGroupMan.h"
  53. #include "DownloadFailureException.h"
  54. #include "ServerHost.h"
  55. #include "Socket.h"
  56. #include "StringFormat.h"
  57. #include <stdint.h>
  58. #include <cassert>
  59. #include <utility>
  60. namespace aria2 {
  61. FtpNegotiationCommand::FtpNegotiationCommand(int32_t cuid,
  62. const RequestHandle& req,
  63. RequestGroup* requestGroup,
  64. DownloadEngine* e,
  65. const SocketHandle& s,
  66. Seq seq):
  67. AbstractCommand(cuid, req, requestGroup, e, s), sequence(seq),
  68. ftp(new FtpConnection(cuid, socket, req, e->option))
  69. {
  70. disableReadCheckSocket();
  71. setWriteCheckSocket(socket);
  72. }
  73. FtpNegotiationCommand::~FtpNegotiationCommand() {}
  74. bool FtpNegotiationCommand::executeInternal() {
  75. while(processSequence(_segments.front()));
  76. if(sequence == SEQ_RETRY) {
  77. return prepareForRetry(0);
  78. } else if(sequence == SEQ_NEGOTIATION_COMPLETED) {
  79. FtpDownloadCommand* command =
  80. new FtpDownloadCommand(cuid, req, _requestGroup, ftp, e, dataSocket, socket);
  81. command->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
  82. command->setStartupIdleTime(e->option->getAsInt(PREF_STARTUP_IDLE_TIME));
  83. command->setLowestDownloadSpeedLimit(e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  84. if(!_requestGroup->isSingleHostMultiConnectionEnabled()) {
  85. _requestGroup->removeURIWhoseHostnameIs(_requestGroup->searchServerHost(cuid)->getHostname());
  86. }
  87. e->commands.push_back(command);
  88. return true;
  89. } else if(sequence == SEQ_HEAD_OK || sequence == SEQ_DOWNLOAD_ALREADY_COMPLETED) {
  90. return true;
  91. } else if(sequence == SEQ_FILE_PREPARATION) {
  92. if(e->option->get(PREF_FTP_PASV) == V_TRUE) {
  93. sequence = SEQ_SEND_PASV;
  94. } else {
  95. sequence = SEQ_SEND_PORT;
  96. }
  97. return false;
  98. } else {
  99. e->commands.push_back(this);
  100. return false;
  101. }
  102. }
  103. bool FtpNegotiationCommand::recvGreeting() {
  104. socket->setBlockingMode();
  105. disableWriteCheckSocket();
  106. setReadCheckSocket(socket);
  107. unsigned int status = ftp->receiveResponse();
  108. if(status == 0) {
  109. return false;
  110. }
  111. if(status != 220) {
  112. throw DlAbortEx(EX_CONNECTION_FAILED);
  113. }
  114. sequence = SEQ_SEND_USER;
  115. return true;
  116. }
  117. bool FtpNegotiationCommand::sendUser() {
  118. ftp->sendUser();
  119. sequence = SEQ_RECV_USER;
  120. return false;
  121. }
  122. bool FtpNegotiationCommand::recvUser() {
  123. unsigned int status = ftp->receiveResponse();
  124. switch(status) {
  125. case 0:
  126. return false;
  127. case 230:
  128. sequence = SEQ_SEND_TYPE;
  129. break;
  130. case 331:
  131. sequence = SEQ_SEND_PASS;
  132. break;
  133. default:
  134. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  135. }
  136. return true;
  137. }
  138. bool FtpNegotiationCommand::sendPass() {
  139. ftp->sendPass();
  140. sequence = SEQ_RECV_PASS;
  141. return false;
  142. }
  143. bool FtpNegotiationCommand::recvPass() {
  144. unsigned int status = ftp->receiveResponse();
  145. if(status == 0) {
  146. return false;
  147. }
  148. if(status != 230) {
  149. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  150. }
  151. sequence = SEQ_SEND_TYPE;
  152. return true;
  153. }
  154. bool FtpNegotiationCommand::sendType() {
  155. ftp->sendType();
  156. sequence = SEQ_RECV_TYPE;
  157. return false;
  158. }
  159. bool FtpNegotiationCommand::recvType() {
  160. unsigned int status = ftp->receiveResponse();
  161. if(status == 0) {
  162. return false;
  163. }
  164. if(status != 200) {
  165. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  166. }
  167. sequence = SEQ_SEND_CWD;
  168. return true;
  169. }
  170. bool FtpNegotiationCommand::sendCwd() {
  171. ftp->sendCwd();
  172. sequence = SEQ_RECV_CWD;
  173. return false;
  174. }
  175. bool FtpNegotiationCommand::recvCwd() {
  176. unsigned int status = ftp->receiveResponse();
  177. if(status == 0) {
  178. return false;
  179. }
  180. if(status != 250) {
  181. poolConnection();
  182. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  183. }
  184. sequence = SEQ_SEND_SIZE;
  185. return true;
  186. }
  187. bool FtpNegotiationCommand::sendSize() {
  188. ftp->sendSize();
  189. sequence = SEQ_RECV_SIZE;
  190. return false;
  191. }
  192. bool FtpNegotiationCommand::recvSize() {
  193. uint64_t size = 0;
  194. unsigned int status = ftp->receiveSizeResponse(size);
  195. if(status == 0) {
  196. return false;
  197. }
  198. if(status != 213) {
  199. poolConnection();
  200. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  201. }
  202. if(size > INT64_MAX) {
  203. throw DlAbortEx
  204. (StringFormat(EX_TOO_LARGE_FILE, Util::uitos(size, true).c_str()).str());
  205. }
  206. if(_requestGroup->getPieceStorage().isNull()) {
  207. SingleFileDownloadContextHandle dctx =
  208. dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext());
  209. dctx->setTotalLength(size);
  210. dctx->setFilename(Util::urldecode(req->getFile()));
  211. _requestGroup->preDownloadProcessing();
  212. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  213. throw DownloadFailureException
  214. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  215. _requestGroup->getFilePath().c_str()).str());
  216. }
  217. _requestGroup->initPieceStorage();
  218. // TODO Is this really necessary?
  219. if(req->getMethod() == Request::METHOD_HEAD) {
  220. sequence = SEQ_HEAD_OK;
  221. return false;
  222. }
  223. BtProgressInfoFileHandle infoFile(new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option));
  224. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  225. sequence = SEQ_DOWNLOAD_ALREADY_COMPLETED;
  226. poolConnection();
  227. return false;
  228. }
  229. _requestGroup->loadAndOpenFile(infoFile);
  230. prepareForNextAction(this);
  231. sequence = SEQ_FILE_PREPARATION;
  232. disableReadCheckSocket();
  233. //setWriteCheckSocket(dataSocket);
  234. //e->noWait = true;
  235. return false;
  236. } else {
  237. _requestGroup->validateTotalLength(size);
  238. }
  239. if(e->option->get(PREF_FTP_PASV) == V_TRUE) {
  240. sequence = SEQ_SEND_PASV;
  241. } else {
  242. sequence = SEQ_SEND_PORT;
  243. }
  244. return true;
  245. }
  246. void FtpNegotiationCommand::afterFileAllocation()
  247. {
  248. setReadCheckSocket(socket);
  249. }
  250. bool FtpNegotiationCommand::sendPort() {
  251. afterFileAllocation();
  252. serverSocket = ftp->sendPort();
  253. sequence = SEQ_RECV_PORT;
  254. return false;
  255. }
  256. bool FtpNegotiationCommand::recvPort() {
  257. unsigned int status = ftp->receiveResponse();
  258. if(status == 0) {
  259. return false;
  260. }
  261. if(status != 200) {
  262. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  263. }
  264. sequence = SEQ_SEND_REST;
  265. return true;
  266. }
  267. bool FtpNegotiationCommand::sendPasv() {
  268. afterFileAllocation();
  269. ftp->sendPasv();
  270. sequence = SEQ_RECV_PASV;
  271. return false;
  272. }
  273. bool FtpNegotiationCommand::recvPasv() {
  274. std::pair<std::string, uint16_t> dest;
  275. unsigned int status = ftp->receivePasvResponse(dest);
  276. if(status == 0) {
  277. return false;
  278. }
  279. if(status != 227) {
  280. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  281. }
  282. // make a data connection to the server.
  283. logger->info(MSG_CONNECTING_TO_SERVER, cuid,
  284. dest.first.c_str(),
  285. dest.second);
  286. dataSocket.reset(new SocketCore());
  287. dataSocket->establishConnection(dest.first, dest.second);
  288. disableReadCheckSocket();
  289. setWriteCheckSocket(dataSocket);
  290. sequence = SEQ_SEND_REST_PASV;
  291. return false;
  292. }
  293. bool FtpNegotiationCommand::sendRestPasv(const SegmentHandle& segment) {
  294. dataSocket->setBlockingMode();
  295. setReadCheckSocket(socket);
  296. disableWriteCheckSocket();
  297. return sendRest(segment);
  298. }
  299. bool FtpNegotiationCommand::sendRest(const SegmentHandle& segment) {
  300. ftp->sendRest(segment);
  301. sequence = SEQ_RECV_REST;
  302. return false;
  303. }
  304. bool FtpNegotiationCommand::recvRest() {
  305. unsigned int status = ftp->receiveResponse();
  306. if(status == 0) {
  307. return false;
  308. }
  309. // TODO if we recieve negative response, then we set _requestGroup->getSegmentMan()->splittable = false, and continue.
  310. if(status != 350) {
  311. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  312. }
  313. sequence = SEQ_SEND_RETR;
  314. return true;
  315. }
  316. bool FtpNegotiationCommand::sendRetr() {
  317. ftp->sendRetr();
  318. sequence = SEQ_RECV_RETR;
  319. return false;
  320. }
  321. bool FtpNegotiationCommand::recvRetr() {
  322. unsigned int status = ftp->receiveResponse();
  323. if(status == 0) {
  324. return false;
  325. }
  326. if(status != 150 && status != 125) {
  327. throw DlAbortEx(StringFormat(EX_BAD_STATUS, status).str());
  328. }
  329. if(e->option->getAsBool(PREF_FTP_PASV)) {
  330. sequence = SEQ_NEGOTIATION_COMPLETED;
  331. return false;
  332. } else {
  333. disableReadCheckSocket();
  334. setReadCheckSocket(serverSocket);
  335. sequence = SEQ_WAIT_CONNECTION;
  336. return false;
  337. }
  338. }
  339. bool FtpNegotiationCommand::waitConnection()
  340. {
  341. disableReadCheckSocket();
  342. setReadCheckSocket(socket);
  343. dataSocket.reset(serverSocket->acceptConnection());
  344. dataSocket->setBlockingMode();
  345. sequence = SEQ_NEGOTIATION_COMPLETED;
  346. return false;
  347. }
  348. bool FtpNegotiationCommand::processSequence(const SegmentHandle& segment) {
  349. bool doNextSequence = true;
  350. switch(sequence) {
  351. case SEQ_RECV_GREETING:
  352. return recvGreeting();
  353. case SEQ_SEND_USER:
  354. return sendUser();
  355. case SEQ_RECV_USER:
  356. return recvUser();
  357. case SEQ_SEND_PASS:
  358. return sendPass();
  359. case SEQ_RECV_PASS:
  360. return recvPass();
  361. case SEQ_SEND_TYPE:
  362. return sendType();
  363. case SEQ_RECV_TYPE:
  364. return recvType();
  365. case SEQ_SEND_CWD:
  366. return sendCwd();
  367. case SEQ_RECV_CWD:
  368. return recvCwd();
  369. case SEQ_SEND_SIZE:
  370. return sendSize();
  371. case SEQ_RECV_SIZE:
  372. return recvSize();
  373. case SEQ_SEND_PORT:
  374. return sendPort();
  375. case SEQ_RECV_PORT:
  376. return recvPort();
  377. case SEQ_SEND_PASV:
  378. return sendPasv();
  379. case SEQ_RECV_PASV:
  380. return recvPasv();
  381. case SEQ_SEND_REST_PASV:
  382. return sendRestPasv(segment);
  383. case SEQ_SEND_REST:
  384. return sendRest(segment);
  385. case SEQ_RECV_REST:
  386. return recvRest();
  387. case SEQ_SEND_RETR:
  388. return sendRetr();
  389. case SEQ_RECV_RETR:
  390. return recvRetr();
  391. case SEQ_WAIT_CONNECTION:
  392. return waitConnection();
  393. default:
  394. abort();
  395. }
  396. return doNextSequence;
  397. }
  398. void FtpNegotiationCommand::poolConnection() const
  399. {
  400. if(e->option->getAsBool(PREF_FTP_REUSE_CONNECTION)) {
  401. std::pair<std::string, uint16_t> peerInfo;
  402. socket->getPeerInfo(peerInfo);
  403. e->poolSocket(peerInfo.first, peerInfo.second, socket);
  404. }
  405. }
  406. } // namespace aria2