1
1
Этот коммит содержится в:
Aris Adamantiadis 2010-02-12 22:17:50 +01:00
родитель 8baaf64a72
Коммит f8d0055503
2 изменённых файлов: 63 добавлений и 9 удалений

Просмотреть файл

@ -15,7 +15,7 @@ program.
#include <libssh/libsshpp.hpp> #include <libssh/libsshpp.hpp>
int main(int argc, const char **argv){ int main(int argc, const char **argv){
ssh::Session session; ssh::Session session,s2;
try { try {
if(argc>1) if(argc>1)
session.setOption(SSH_OPTIONS_HOST,argv[1]); session.setOption(SSH_OPTIONS_HOST,argv[1]);
@ -27,5 +27,6 @@ int main(int argc, const char **argv){
std::cout << "Error during connection : "; std::cout << "Error during connection : ";
std::cout << e.getError() << std::endl; std::cout << e.getError() << std::endl;
} }
//s2=session;
return 0; return 0;
} }

Просмотреть файл

@ -38,6 +38,7 @@
*/ */
#include <libssh/libssh.h> #include <libssh/libssh.h>
#include <libssh/server.h>
namespace ssh { namespace ssh {
/** @brief This class describes a SSH Exception object. This object can be throwed /** @brief This class describes a SSH Exception object. This object can be throwed
@ -74,6 +75,8 @@ private:
const char *description; const char *description;
}; };
#define ssh_throw(x) if(x!=SSH_OK) throw SshException(getCSession());
/** /**
* The ssh::Session class contains the state of a SSH connection. * The ssh::Session class contains the state of a SSH connection.
*/ */
@ -87,14 +90,32 @@ public:
ssh_free(c_session); ssh_free(c_session);
c_session=NULL; c_session=NULL;
} }
/** @brief sets an SSH session options
* @param type Type of option
* @param option cstring containing the value of option
* @throws SshException on error
* @see ssh_options_set
*/
void setOption(enum ssh_options_e type, const char *option){ void setOption(enum ssh_options_e type, const char *option){
ssh_options_set(c_session,type,option); ssh_throw(ssh_options_set(c_session,type,option));
} }
/** @brief sets an SSH session options
* @param type Type of option
* @param option long integer containing the value of option
* @throws SshException on error
* @see ssh_options_set
*/
void setOption(enum ssh_options_e type, long int option){ void setOption(enum ssh_options_e type, long int option){
ssh_options_set(c_session,type,&option); ssh_throw(ssh_options_set(c_session,type,&option));
} }
/** @brief sets an SSH session options
* @param type Type of option
* @param option void pointer containing the value of option
* @throws SshException on error
* @see ssh_options_set
*/
void setOption(enum ssh_options_e type, void *option){ void setOption(enum ssh_options_e type, void *option){
ssh_options_set(c_session,type,option); ssh_throw(ssh_options_set(c_session,type,option));
} }
/** @brief connects to the remote host /** @brief connects to the remote host
* @throws SshException on error * @throws SshException on error
@ -102,12 +123,16 @@ public:
*/ */
void connect(){ void connect(){
int ret=ssh_connect(c_session); int ret=ssh_connect(c_session);
if(ret != SSH_OK){ ssh_throw(ret);
throw SshException(getCSession());
}
} }
/** @brief Authenticates automatically using public key
* @throws SshException on error
* @see ssh_userauth_autopubkey
*/
int userauthAutopubkey(){ int userauthAutopubkey(){
return ssh_userauth_autopubkey(c_session,NULL); int ret=ssh_userauth_autopubkey(c_session,NULL);
if(ret==SSH_ERROR)
ssh_throw(ret);
} }
int userauthNone(); int userauthNone();
int userauthPassword(const char *password); int userauthPassword(const char *password);
@ -135,6 +160,9 @@ private:
ssh_session getCSession(){ ssh_session getCSession(){
return c_session; return c_session;
} }
/* No copy constructor, no = operator */
Session(const Session &);
Session& operator=(const Session &);
}; };
class Channel { class Channel {
@ -175,10 +203,35 @@ public:
int requestX11(bool single_connection, const char *protocol, const char *cookie, int requestX11(bool single_connection, const char *protocol, const char *cookie,
int screen_number); int screen_number);
int sendEof(); int sendEof();
int write(const void *data, size_t len, bool is_stderr=false); /** @brief Writes on a channel
* @param data data to write.
* @param len number of bytes to write.
* @param is_stderr write should be done on the stderr channel (server only)
* @returns number of bytes written
* @throws SshException in case of error
* @see channel_write
* @see channel_write_stderr
*/
int write(const void *data, size_t len, bool is_stderr=false){
int ret;
if(is_stderr){
ret=channel_write_stderr(channel,data,len);
} else {
ret=channel_write(channel,data,len);
}
if(ret==SSH_ERROR)
ssh_throw(ret);
return ret;
}
private: private:
ssh_session getCSession(){
return session->getCSession();
}
Session *session; Session *session;
ssh_channel channel; ssh_channel channel;
/* No copy and no = operator */
Channel(const Channel &);
Channel &operator=(const Channel &);
}; };
} // namespace ssh } // namespace ssh