SSHSession.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2015 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. #ifndef SSH_SESSION_H
  36. #define SSH_SESSION_H
  37. #include "common.h"
  38. #include "a2netcompat.h"
  39. #include <string>
  40. #include <libssh2.h>
  41. #include <libssh2_sftp.h>
  42. namespace aria2 {
  43. enum SSHDirection { SSH_WANT_READ = 1, SSH_WANT_WRITE };
  44. enum SSHErrorCode {
  45. SSH_ERR_OK = 0,
  46. SSH_ERR_ERROR = -1,
  47. SSH_ERR_WOULDBLOCK = -2
  48. };
  49. class SSHSession {
  50. public:
  51. SSHSession();
  52. // MUST deallocate all resources
  53. ~SSHSession();
  54. SSHSession(const SSHSession&) = delete;
  55. SSHSession& operator=(const SSHSession&) = delete;
  56. // Initializes SSH session. The |sockfd| is the underlying
  57. // transport socket. This function returns SSH_ERR_OK if it
  58. // succeeds, or SSH_ERR_ERROR.
  59. int init(sock_t sockfd);
  60. // Closes the SSH session. Don't close underlying transport
  61. // socket. This function returns SSH_ERR_OK if it succeeds, or
  62. // SSH_ERR_ERROR.
  63. int closeConnection();
  64. int gracefulShutdown();
  65. // Returns SSH_WANT_READ if SSH session needs more data from remote
  66. // endpoint to proceed, or SSH_WANT_WRITE if SSH session needs to
  67. // write more data to proceed. If SSH session needs neither read nor
  68. // write data at the moment, SSH_WANT_READ must be returned.
  69. int checkDirection();
  70. // Sends |data| with length |len|. This function returns the number
  71. // of bytes sent if it succeeds, or SSH_ERR_WOULDBLOCK if the
  72. // underlying transport blocks, or SSH_ERR_ERROR.
  73. ssize_t writeData(const void* data, size_t len);
  74. // Receives data into |data| with length |len|. This function
  75. // returns the number of bytes received if it succeeds, or
  76. // SSH_ERR_WOULDBLOCK if the underlying transport blocks, or
  77. // SSH_ERR_ERROR.
  78. ssize_t readData(void* data, size_t len);
  79. // Performs handshake. This function returns SSH_ERR_OK
  80. // if it succeeds, or SSH_ERR_WOULDBLOCK if the underlying transport
  81. // blocks, or SSH_ERR_ERROR.
  82. int handshake();
  83. // Returns message digest of host's public key. |hashType| must be
  84. // either "sha-1" or "md5".
  85. std::string hostkeyMessageDigest(const std::string& hashType);
  86. // Performs authentication using username and password. This
  87. // function returns SSH_ERR_OK if it succeeds, or SSH_ERR_WOULDBLOCK
  88. // if the underlying transport blocks, or SSH_ERR_ERROR.
  89. int authPassword(const std::string& user, const std::string& password);
  90. // Starts SFTP session and opens remote file |path|. This function
  91. // returns SSH_ERR_OK if it succeeds, or SSH_ERR_WOULDBLOCK if the
  92. // underlying transport blocks, or SSH_ERR_ERROR.
  93. int sftpOpen(const std::string& path);
  94. // Closes remote file opened by sftpOpen(). This function returns
  95. // SSH_ERR_OK if it succeeds, or SSH_ERR_WOULDBLOCK if the
  96. // underlying transport blocks, or SSH_ERR_ERROR.
  97. int sftpClose();
  98. // Gets total length and modified time of opened file by sftpOpen().
  99. // On success, total length and modified time are assigned to
  100. // |totalLength| and |mtime|. This function returns SSH_ERR_OK if
  101. // it succeeds, or SSH_ERR_WOULDBLOCK if the underlying transport
  102. // blocks, or SSH_ERR_ERROR.
  103. int sftpStat(int64_t& totalLength, time_t& mtime);
  104. // Moves file position to |pos|.
  105. void sftpSeek(int64_t pos);
  106. // Returns last error string
  107. std::string getLastErrorString();
  108. private:
  109. LIBSSH2_SESSION* ssh2_;
  110. LIBSSH2_SFTP* sftp_;
  111. LIBSSH2_SFTP_HANDLE* sftph_;
  112. sock_t fd_;
  113. };
  114. }
  115. #endif // SSH_SESSION_H