FtpNegotiationCommand.cc 9.9 KB

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