FtpNegotiationCommand.cc 11 KB

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