FtpNegotiationCommand.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <stdint.h>
  57. #include <cassert>
  58. #include <utility>
  59. namespace aria2 {
  60. FtpNegotiationCommand::FtpNegotiationCommand(int32_t cuid,
  61. const RequestHandle& req,
  62. RequestGroup* requestGroup,
  63. DownloadEngine* e,
  64. const SocketHandle& s):
  65. AbstractCommand(cuid, req, requestGroup, e, s), sequence(SEQ_RECV_GREETING)
  66. {
  67. ftp = new FtpConnection(cuid, socket, req, e->option);
  68. disableReadCheckSocket();
  69. setWriteCheckSocket(socket);
  70. }
  71. FtpNegotiationCommand::~FtpNegotiationCommand() {
  72. delete ftp;
  73. }
  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, 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 new 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 new DlAbortEx(EX_BAD_STATUS, status);
  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 new DlAbortEx(EX_BAD_STATUS, status);
  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 new DlAbortEx(EX_BAD_STATUS, status);
  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. throw new DlAbortEx(EX_BAD_STATUS, status);
  182. }
  183. sequence = SEQ_SEND_SIZE;
  184. return true;
  185. }
  186. bool FtpNegotiationCommand::sendSize() {
  187. ftp->sendSize();
  188. sequence = SEQ_RECV_SIZE;
  189. return false;
  190. }
  191. bool FtpNegotiationCommand::recvSize() {
  192. uint64_t size = 0;
  193. unsigned int status = ftp->receiveSizeResponse(size);
  194. if(status == 0) {
  195. return false;
  196. }
  197. if(status != 213) {
  198. throw new DlAbortEx(EX_BAD_STATUS, status);
  199. }
  200. if(size > INT64_MAX) {
  201. throw new DlAbortEx(EX_TOO_LARGE_FILE, Util::uitos(size, true).c_str());
  202. }
  203. if(_requestGroup->getPieceStorage().isNull()) {
  204. SingleFileDownloadContextHandle dctx = _requestGroup->getDownloadContext();
  205. dctx->setTotalLength(size);
  206. dctx->setFilename(Util::urldecode(req->getFile()));
  207. _requestGroup->preDownloadProcessing();
  208. if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
  209. throw new DownloadFailureException(EX_DUPLICATE_FILE_DOWNLOAD,
  210. _requestGroup->getFilePath().c_str());
  211. }
  212. _requestGroup->initPieceStorage();
  213. // TODO Is this really necessary?
  214. if(req->getMethod() == Request::METHOD_HEAD) {
  215. sequence = SEQ_HEAD_OK;
  216. return false;
  217. }
  218. BtProgressInfoFileHandle infoFile = new DefaultBtProgressInfoFile(_requestGroup->getDownloadContext(), _requestGroup->getPieceStorage(), e->option);
  219. if(!infoFile->exists() && _requestGroup->downloadFinishedByFileLength()) {
  220. sequence = SEQ_DOWNLOAD_ALREADY_COMPLETED;
  221. return false;
  222. }
  223. _requestGroup->loadAndOpenFile(infoFile);
  224. prepareForNextAction(this);
  225. sequence = SEQ_FILE_PREPARATION;
  226. disableReadCheckSocket();
  227. //setWriteCheckSocket(dataSocket);
  228. //e->noWait = true;
  229. return false;
  230. } else {
  231. _requestGroup->validateTotalLength(size);
  232. }
  233. if(e->option->get(PREF_FTP_PASV) == V_TRUE) {
  234. sequence = SEQ_SEND_PASV;
  235. } else {
  236. sequence = SEQ_SEND_PORT;
  237. }
  238. return true;
  239. }
  240. void FtpNegotiationCommand::afterFileAllocation()
  241. {
  242. setReadCheckSocket(socket);
  243. }
  244. bool FtpNegotiationCommand::sendPort() {
  245. afterFileAllocation();
  246. serverSocket = ftp->sendPort();
  247. sequence = SEQ_RECV_PORT;
  248. return false;
  249. }
  250. bool FtpNegotiationCommand::recvPort() {
  251. unsigned int status = ftp->receiveResponse();
  252. if(status == 0) {
  253. return false;
  254. }
  255. if(status != 200) {
  256. throw new DlAbortEx(EX_BAD_STATUS, status);
  257. }
  258. sequence = SEQ_SEND_REST;
  259. return true;
  260. }
  261. bool FtpNegotiationCommand::sendPasv() {
  262. afterFileAllocation();
  263. ftp->sendPasv();
  264. sequence = SEQ_RECV_PASV;
  265. return false;
  266. }
  267. bool FtpNegotiationCommand::recvPasv() {
  268. std::pair<std::string, uint16_t> dest;
  269. unsigned int status = ftp->receivePasvResponse(dest);
  270. if(status == 0) {
  271. return false;
  272. }
  273. if(status != 227) {
  274. throw new DlAbortEx(EX_BAD_STATUS, status);
  275. }
  276. // make a data connection to the server.
  277. logger->info(MSG_CONNECTING_TO_SERVER, cuid,
  278. dest.first.c_str(),
  279. dest.second);
  280. dataSocket->establishConnection(dest.first, dest.second);
  281. disableReadCheckSocket();
  282. setWriteCheckSocket(dataSocket);
  283. sequence = SEQ_SEND_REST_PASV;
  284. return false;
  285. }
  286. bool FtpNegotiationCommand::sendRestPasv(const SegmentHandle& segment) {
  287. dataSocket->setBlockingMode();
  288. setReadCheckSocket(socket);
  289. disableWriteCheckSocket();
  290. return sendRest(segment);
  291. }
  292. bool FtpNegotiationCommand::sendRest(const SegmentHandle& segment) {
  293. ftp->sendRest(segment);
  294. sequence = SEQ_RECV_REST;
  295. return false;
  296. }
  297. bool FtpNegotiationCommand::recvRest() {
  298. unsigned int status = ftp->receiveResponse();
  299. if(status == 0) {
  300. return false;
  301. }
  302. // TODO if we recieve negative response, then we set _requestGroup->getSegmentMan()->splittable = false, and continue.
  303. if(status != 350) {
  304. throw new DlAbortEx(EX_BAD_STATUS, status);
  305. }
  306. sequence = SEQ_SEND_RETR;
  307. return true;
  308. }
  309. bool FtpNegotiationCommand::sendRetr() {
  310. ftp->sendRetr();
  311. sequence = SEQ_RECV_RETR;
  312. return false;
  313. }
  314. bool FtpNegotiationCommand::recvRetr() {
  315. unsigned int status = ftp->receiveResponse();
  316. if(status == 0) {
  317. return false;
  318. }
  319. if(status != 150 && status != 125) {
  320. throw new DlAbortEx(EX_BAD_STATUS, status);
  321. }
  322. if(e->option->getAsBool(PREF_FTP_PASV)) {
  323. sequence = SEQ_NEGOTIATION_COMPLETED;
  324. return false;
  325. } else {
  326. disableReadCheckSocket();
  327. setReadCheckSocket(serverSocket);
  328. sequence = SEQ_WAIT_CONNECTION;
  329. return false;
  330. }
  331. }
  332. bool FtpNegotiationCommand::waitConnection()
  333. {
  334. disableReadCheckSocket();
  335. setReadCheckSocket(socket);
  336. dataSocket = serverSocket->acceptConnection();
  337. dataSocket->setBlockingMode();
  338. sequence = SEQ_NEGOTIATION_COMPLETED;
  339. return false;
  340. }
  341. bool FtpNegotiationCommand::processSequence(const SegmentHandle& segment) {
  342. bool doNextSequence = true;
  343. switch(sequence) {
  344. case SEQ_RECV_GREETING:
  345. return recvGreeting();
  346. case SEQ_SEND_USER:
  347. return sendUser();
  348. case SEQ_RECV_USER:
  349. return recvUser();
  350. case SEQ_SEND_PASS:
  351. return sendPass();
  352. case SEQ_RECV_PASS:
  353. return recvPass();
  354. case SEQ_SEND_TYPE:
  355. return sendType();
  356. case SEQ_RECV_TYPE:
  357. return recvType();
  358. case SEQ_SEND_CWD:
  359. return sendCwd();
  360. case SEQ_RECV_CWD:
  361. return recvCwd();
  362. case SEQ_SEND_SIZE:
  363. return sendSize();
  364. case SEQ_RECV_SIZE:
  365. return recvSize();
  366. case SEQ_SEND_PORT:
  367. return sendPort();
  368. case SEQ_RECV_PORT:
  369. return recvPort();
  370. case SEQ_SEND_PASV:
  371. return sendPasv();
  372. case SEQ_RECV_PASV:
  373. return recvPasv();
  374. case SEQ_SEND_REST_PASV:
  375. return sendRestPasv(segment);
  376. case SEQ_SEND_REST:
  377. return sendRest(segment);
  378. case SEQ_RECV_REST:
  379. return recvRest();
  380. case SEQ_SEND_RETR:
  381. return sendRetr();
  382. case SEQ_RECV_RETR:
  383. return recvRetr();
  384. case SEQ_WAIT_CONNECTION:
  385. return waitConnection();
  386. default:
  387. abort();
  388. }
  389. return doNextSequence;
  390. }
  391. } // namespace aria2