FtpConnection.cc 7.9 KB

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