FtpNegotiationCommand.cc 10 KB

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