FtpConnection.cc 7.0 KB

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