FtpConnection.cc 7.2 KB

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