FtpConnection.cc 6.2 KB

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