FtpConnection.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket,
  43. const RequestHandle req, const Option* op)
  44. :cuid(cuid), socket(socket), req(req), option(op) {
  45. logger = LogFactory::getInstance();
  46. }
  47. FtpConnection::~FtpConnection() {}
  48. void FtpConnection::sendUser() const
  49. throw(DlRetryEx*)
  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. throw(DlRetryEx*)
  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. throw(DlRetryEx*)
  64. {
  65. string type;
  66. if(option->get(PREF_FTP_TYPE) == V_ASCII) {
  67. type = "A";
  68. } else {
  69. type = "I";
  70. }
  71. string request = "TYPE "+type+"\r\n";
  72. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  73. socket->writeData(request);
  74. }
  75. void FtpConnection::sendCwd() const
  76. throw(DlRetryEx*)
  77. {
  78. string request = "CWD "+Util::urldecode(req->getDir())+"\r\n";
  79. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  80. socket->writeData(request);
  81. }
  82. void FtpConnection::sendSize() const
  83. throw(DlRetryEx*)
  84. {
  85. string request = "SIZE "+Util::urldecode(req->getFile())+"\r\n";
  86. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  87. socket->writeData(request);
  88. }
  89. void FtpConnection::sendPasv() const
  90. throw(DlRetryEx*)
  91. {
  92. string request = "PASV\r\n";
  93. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  94. socket->writeData(request);
  95. }
  96. SocketHandle FtpConnection::sendPort() const
  97. throw(DlAbortEx*, DlRetryEx*)
  98. {
  99. SocketHandle serverSocket;
  100. serverSocket->beginListen();
  101. pair<string, int32_t> addrinfo;
  102. socket->getAddrInfo(addrinfo);
  103. int32_t ipaddr[4];
  104. sscanf(addrinfo.first.c_str(), "%d.%d.%d.%d",
  105. &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]);
  106. serverSocket->getAddrInfo(addrinfo);
  107. string request = "PORT "+
  108. Util::itos(ipaddr[0])+","+Util::itos(ipaddr[1])+","+
  109. Util::itos(ipaddr[2])+","+Util::itos(ipaddr[3])+","+
  110. Util::itos(addrinfo.second/256)+","+Util::itos(addrinfo.second%256)+"\r\n";
  111. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  112. socket->writeData(request);
  113. return serverSocket;
  114. }
  115. void FtpConnection::sendRest(const SegmentHandle& segment) const
  116. throw(DlRetryEx*)
  117. {
  118. string request = "REST "+Util::llitos(segment->getPositionToWrite())+"\r\n";
  119. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  120. socket->writeData(request);
  121. }
  122. void FtpConnection::sendRetr() const
  123. throw(DlRetryEx*)
  124. {
  125. 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 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 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. string::size_type p;
  153. p = response.find("\r\n"+Util::itos(status)+" ");
  154. if(p == 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(pair<int32_t, string>& response)
  165. throw(DlRetryEx*)
  166. {
  167. char buf[1024];
  168. while(socket->isReadable(0)) {
  169. int32_t size = sizeof(buf)-1;
  170. socket->readData(buf, size);
  171. if(size == 0) {
  172. throw new DlRetryEx(EX_GOT_EOF);
  173. }
  174. buf[size] = '\0';
  175. strbuf += buf;
  176. }
  177. int32_t status;
  178. if(strbuf.size() >= 4) {
  179. status = getStatus(strbuf);
  180. if(status == 0) {
  181. throw new DlRetryEx(EX_INVALID_RESPONSE);
  182. }
  183. } else {
  184. return false;
  185. }
  186. if(isEndOfResponse(status, strbuf)) {
  187. logger->info(MSG_RECEIVE_RESPONSE, cuid, strbuf.c_str());
  188. response.first = status;
  189. response.second = strbuf;
  190. strbuf.erase();
  191. return true;
  192. } else {
  193. // didn't receive response fully.
  194. return false;
  195. }
  196. }
  197. int32_t FtpConnection::receiveResponse()
  198. throw(DlRetryEx*)
  199. {
  200. pair<int32_t, string> response;
  201. if(bulkReceiveResponse(response)) {
  202. return response.first;
  203. } else {
  204. return 0;
  205. }
  206. }
  207. int32_t FtpConnection::receiveSizeResponse(int64_t& size)
  208. throw(DlRetryEx*)
  209. {
  210. pair<int32_t, string> response;
  211. if(bulkReceiveResponse(response)) {
  212. if(response.first == 213) {
  213. sscanf(response.second.c_str(), "%*d " LONGLONG_SCANF, &size);
  214. }
  215. return response.first;
  216. } else {
  217. return 0;
  218. }
  219. }
  220. int32_t FtpConnection::receivePasvResponse(pair<string, int32_t>& dest)
  221. throw(DlRetryEx*)
  222. {
  223. pair<int32_t, string> response;
  224. if(bulkReceiveResponse(response)) {
  225. if(response.first == 227) {
  226. // we assume the format of response is "227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)."
  227. int32_t h1, h2, h3, h4, p1, p2;
  228. string::size_type p = response.second.find("(");
  229. if(p >= 4) {
  230. sscanf(response.second.substr(response.second.find("(")).c_str(),
  231. "(%d,%d,%d,%d,%d,%d).",
  232. &h1, &h2, &h3, &h4, &p1, &p2);
  233. // ip address
  234. dest.first = Util::itos(h1)+"."+Util::itos(h2)+"."+Util::itos(h3)+"."+Util::itos(h4);
  235. // port number
  236. dest.second = 256*p1+p2;
  237. } else {
  238. throw new DlRetryEx(EX_INVALID_RESPONSE);
  239. }
  240. }
  241. return response.first;
  242. } else {
  243. return 0;
  244. }
  245. }