FtpConnection.cc 7.2 KB

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