FtpNegotiationCommand.cc 13 KB

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