FtpConnection.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "FtpConnection.h"
  36. #include "Request.h"
  37. #include "Segment.h"
  38. #include "Option.h"
  39. #include "Util.h"
  40. #include "message.h"
  41. #include "prefs.h"
  42. #include "LogFactory.h"
  43. #include "Logger.h"
  44. #include "AuthConfigFactory.h"
  45. #include "AuthConfig.h"
  46. #include "DlRetryEx.h"
  47. #include "DlAbortEx.h"
  48. #include "Socket.h"
  49. #include "A2STR.h"
  50. #include <cstring>
  51. namespace aria2 {
  52. const std::string FtpConnection::A("A");
  53. const std::string FtpConnection::I("I");
  54. FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket,
  55. const RequestHandle& req, const Option* op):
  56. cuid(cuid), socket(socket), req(req), option(op),
  57. logger(LogFactory::getInstance()) {}
  58. FtpConnection::~FtpConnection() {}
  59. void FtpConnection::sendUser() const
  60. {
  61. std::string request = "USER "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getUser()+"\r\n";
  62. logger->info(MSG_SENDING_REQUEST, cuid, "USER ********");
  63. socket->writeData(request);
  64. }
  65. void FtpConnection::sendPass() const
  66. {
  67. std::string request = "PASS "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getPassword()+"\r\n";
  68. logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********");
  69. socket->writeData(request);
  70. }
  71. void FtpConnection::sendType() const
  72. {
  73. std::string type;
  74. if(option->get(PREF_FTP_TYPE) == V_ASCII) {
  75. type = FtpConnection::A;
  76. } else {
  77. type = FtpConnection::I;
  78. }
  79. std::string request = "TYPE "+type+"\r\n";
  80. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  81. socket->writeData(request);
  82. }
  83. void FtpConnection::sendCwd() const
  84. {
  85. std::string request = "CWD "+Util::urldecode(req->getDir())+"\r\n";
  86. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  87. socket->writeData(request);
  88. }
  89. void FtpConnection::sendMdtm() const
  90. {
  91. std::string request = "MDTM "+Util::urlencode(req->getFile())+"\r\n";
  92. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  93. socket->writeData(request);
  94. }
  95. void FtpConnection::sendSize() const
  96. {
  97. std::string request = "SIZE "+Util::urldecode(req->getFile())+"\r\n";
  98. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  99. socket->writeData(request);
  100. }
  101. void FtpConnection::sendPasv() const
  102. {
  103. static const std::string request("PASV\r\n");
  104. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  105. socket->writeData(request);
  106. }
  107. SocketHandle FtpConnection::sendPort() const
  108. {
  109. SocketHandle serverSocket(new SocketCore());
  110. serverSocket->bind(0);
  111. serverSocket->beginListen();
  112. serverSocket->setNonBlockingMode();
  113. std::pair<std::string, uint16_t> addrinfo;
  114. socket->getAddrInfo(addrinfo);
  115. unsigned int ipaddr[4];
  116. sscanf(addrinfo.first.c_str(), "%u.%u.%u.%u",
  117. &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]);
  118. serverSocket->getAddrInfo(addrinfo);
  119. std::string request = "PORT "+
  120. Util::uitos(ipaddr[0])+","+Util::uitos(ipaddr[1])+","+
  121. Util::uitos(ipaddr[2])+","+Util::uitos(ipaddr[3])+","+
  122. Util::uitos(addrinfo.second/256)+","+Util::uitos(addrinfo.second%256)+"\r\n";
  123. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  124. socket->writeData(request);
  125. return serverSocket;
  126. }
  127. void FtpConnection::sendRest(const SegmentHandle& segment) const
  128. {
  129. std::string request = "REST ";
  130. if(segment.isNull()) {
  131. request += "0";
  132. } else {
  133. request += Util::itos(segment->getPositionToWrite());
  134. }
  135. request += "\r\n";
  136. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  137. socket->writeData(request);
  138. }
  139. void FtpConnection::sendRetr() const
  140. {
  141. std::string request = "RETR "+Util::urldecode(req->getFile())+"\r\n";
  142. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  143. socket->writeData(request);
  144. }
  145. unsigned int FtpConnection::getStatus(const std::string& response) const
  146. {
  147. unsigned int status;
  148. // When the response is not like "%u %*s",
  149. // we return 0.
  150. if(response.find_first_not_of("0123456789") != 3
  151. || !(response.find(" ") == 3 || response.find("-") == 3)) {
  152. return 0;
  153. }
  154. if(sscanf(response.c_str(), "%u %*s", &status) == 1) {
  155. return status;
  156. } else {
  157. return 0;
  158. }
  159. }
  160. bool FtpConnection::isEndOfResponse(unsigned int status, const std::string& response) const
  161. {
  162. if(response.size() <= 4) {
  163. return false;
  164. }
  165. // if 4th character of buf is '-', then multi line response is expected.
  166. if(response.at(3) == '-') {
  167. // multi line response
  168. std::string::size_type p;
  169. p = response.find("\r\n"+Util::uitos(status)+" ");
  170. if(p == std::string::npos) {
  171. return false;
  172. }
  173. }
  174. if(Util::endsWith(response, A2STR::CRLF)) {
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. bool FtpConnection::bulkReceiveResponse(std::pair<unsigned int, std::string>& response)
  181. {
  182. char buf[1024];
  183. while(socket->isReadable(0)) {
  184. size_t size = sizeof(buf)-1;
  185. socket->readData(buf, size);
  186. if(size == 0) {
  187. throw DlRetryEx(EX_GOT_EOF);
  188. }
  189. buf[size] = '\0';
  190. strbuf += buf;
  191. }
  192. unsigned int status;
  193. if(strbuf.size() >= 4) {
  194. status = getStatus(strbuf);
  195. if(status == 0) {
  196. throw DlAbortEx(EX_INVALID_RESPONSE);
  197. }
  198. } else {
  199. return false;
  200. }
  201. if(isEndOfResponse(status, strbuf)) {
  202. logger->info(MSG_RECEIVE_RESPONSE, cuid, strbuf.c_str());
  203. response.first = status;
  204. response.second = strbuf;
  205. strbuf.erase();
  206. return true;
  207. } else {
  208. // didn't receive response fully.
  209. return false;
  210. }
  211. }
  212. unsigned int FtpConnection::receiveResponse()
  213. {
  214. std::pair<unsigned int, std::string> response;
  215. if(bulkReceiveResponse(response)) {
  216. return response.first;
  217. } else {
  218. return 0;
  219. }
  220. }
  221. #ifdef __MINGW32__
  222. # define LONGLONG_PRINTF "%I64d"
  223. # define ULONGLONG_PRINTF "%I64u"
  224. # define LONGLONG_SCANF "%I64d"
  225. # define ULONGLONG_SCANF "%I64u"
  226. #else
  227. # define LONGLONG_PRINTF "%lld"
  228. # define ULONGLONG_PRINTF "%llu"
  229. # define LONGLONG_SCANF "%Ld"
  230. # define ULONGLONG_SCANF "%Lu"
  231. #endif // __MINGW32__
  232. unsigned int FtpConnection::receiveSizeResponse(uint64_t& size)
  233. {
  234. std::pair<unsigned int, std::string> response;
  235. if(bulkReceiveResponse(response)) {
  236. if(response.first == 213) {
  237. sscanf(response.second.c_str(), "%*u " LONGLONG_SCANF, &size);
  238. }
  239. return response.first;
  240. } else {
  241. return 0;
  242. }
  243. }
  244. unsigned int FtpConnection::receiveMdtmResponse(Time& time)
  245. {
  246. // MDTM command, specified in RFC3659.
  247. std::pair<unsigned int, std::string> response;
  248. if(bulkReceiveResponse(response)) {
  249. if(response.first == 213) {
  250. char buf[15]; // YYYYMMDDhhmmss+\0, milli second part is dropped.
  251. sscanf(response.second.c_str(), "%*u %14s", buf);
  252. if(strlen(buf) == 14) {
  253. time = Time::parse(buf, "%Y%m%d%H%M%S");
  254. } else {
  255. time.setTimeInSec(-1);
  256. }
  257. }
  258. return response.first;
  259. } else {
  260. return 0;
  261. }
  262. }
  263. unsigned int FtpConnection::receivePasvResponse(std::pair<std::string, uint16_t>& dest)
  264. {
  265. std::pair<unsigned int, std::string> response;
  266. if(bulkReceiveResponse(response)) {
  267. if(response.first == 227) {
  268. // we assume the format of response is "227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)."
  269. unsigned int h1, h2, h3, h4, p1, p2;
  270. std::string::size_type p = response.second.find("(");
  271. if(p >= 4) {
  272. sscanf(response.second.substr(response.second.find("(")).c_str(),
  273. "(%u,%u,%u,%u,%u,%u).",
  274. &h1, &h2, &h3, &h4, &p1, &p2);
  275. // ip address
  276. dest.first = Util::uitos(h1)+"."+Util::uitos(h2)+"."+Util::uitos(h3)+"."+Util::uitos(h4);
  277. // port number
  278. dest.second = 256*p1+p2;
  279. } else {
  280. throw DlRetryEx(EX_INVALID_RESPONSE);
  281. }
  282. }
  283. return response.first;
  284. } else {
  285. return 0;
  286. }
  287. }
  288. } // namespace aria2