Blame include/libssh/libsshpp.hpp

Packit Service 31306d
/*
Packit Service 31306d
 * This file is part of the SSH Library
Packit Service 31306d
 *
Packit Service 31306d
 * Copyright (c) 2010 by Aris Adamantiadis
Packit Service 31306d
 *
Packit Service 31306d
 * This library is free software; you can redistribute it and/or
Packit Service 31306d
 * modify it under the terms of the GNU Lesser General Public
Packit Service 31306d
 * License as published by the Free Software Foundation; either
Packit Service 31306d
 * version 2.1 of the License, or (at your option) any later version.
Packit Service 31306d
 *
Packit Service 31306d
 * This library is distributed in the hope that it will be useful,
Packit Service 31306d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 31306d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 31306d
 * Lesser General Public License for more details.
Packit Service 31306d
 *
Packit Service 31306d
 * You should have received a copy of the GNU Lesser General Public
Packit Service 31306d
 * License along with this library; if not, write to the Free Software
Packit Service 31306d
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service 31306d
 */
Packit Service 31306d
Packit Service 31306d
#ifndef LIBSSHPP_HPP_
Packit Service 31306d
#define LIBSSHPP_HPP_
Packit Service 31306d
Packit Service 31306d
/**
Packit Service 31306d
 * @defgroup ssh_cpp The libssh C++ wrapper
Packit Service 31306d
 *
Packit Service 31306d
 * The C++ bindings for libssh are completely embedded in a single .hpp file, and
Packit Service 31306d
 * this for two reasons:
Packit Service 31306d
 * - C++ is hard to keep binary compatible, C is easy. We try to keep libssh C version
Packit Service 31306d
 *   as much as possible binary compatible between releases, while this would be hard for
Packit Service 31306d
 *   C++. If you compile your program with these headers, you will only link to the C version
Packit Service 31306d
 *   of libssh which will be kept ABI compatible. No need to recompile your C++ program
Packit Service 31306d
 *   each time a new binary-compatible version of libssh is out
Packit Service 31306d
 * - Most of the functions in this file are really short and are probably worth the "inline"
Packit Service 31306d
 *   linking mode, which the compiler can decide to do in some case. There would be nearly no
Packit Service 31306d
 *   performance penalty of using the wrapper rather than native calls.
Packit Service 31306d
 *
Packit Service 31306d
 * Please visit the documentation of ssh::Session and ssh::Channel
Packit Service 31306d
 * @see ssh::Session
Packit Service 31306d
 * @see ssh::Channel
Packit Service 31306d
 *
Packit Service 31306d
 * If you wish not to use C++ exceptions, please define SSH_NO_CPP_EXCEPTIONS:
Packit Service 31306d
 * @code
Packit Service 31306d
 * #define SSH_NO_CPP_EXCEPTIONS
Packit Service 31306d
 * #include <libssh/libsshpp.hpp>
Packit Service 31306d
 * @endcode
Packit Service 31306d
 * All functions will then return SSH_ERROR in case of error.
Packit Service 31306d
 * @{
Packit Service 31306d
 */
Packit Service 31306d
Packit Service 31306d
/* do not use deprecated functions */
Packit Service 31306d
#define LIBSSH_LEGACY_0_4
Packit Service 31306d
Packit Service 31306d
#include <libssh/libssh.h>
Packit Service 31306d
#include <libssh/server.h>
Packit Service 31306d
#include <stdlib.h>
Packit Service 31306d
#include <stdarg.h>
Packit Service 31306d
#include <stdio.h>
Packit Service 31306d
#include <string>
Packit Service 31306d
Packit Service 31306d
namespace ssh {
Packit Service 31306d
Packit Service 31306d
class Channel;
Packit Service 31306d
/** Some people do not like C++ exceptions. With this define, we give
Packit Service 31306d
 * the choice to use or not exceptions.
Packit Service 31306d
 * @brief if defined, disable C++ exceptions for libssh c++ wrapper
Packit Service 31306d
 */
Packit Service 31306d
#ifndef SSH_NO_CPP_EXCEPTIONS
Packit Service 31306d
Packit Service 31306d
/** @brief This class describes a SSH Exception object. This object can be thrown
Packit Service 31306d
 * by several SSH functions that interact with the network, and may fail because of
Packit Service 31306d
 * socket, protocol or memory errors.
Packit Service 31306d
 */
Packit Service 31306d
class SshException{
Packit Service 31306d
public:
Packit Service 31306d
  SshException(ssh_session csession){
Packit Service 31306d
    code=ssh_get_error_code(csession);
Packit Service 31306d
    description=std::string(ssh_get_error(csession));
Packit Service 31306d
  }
Packit Service 31306d
  SshException(const SshException &e){
Packit Service 31306d
    code=e.code;
Packit Service 31306d
    description=e.description;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns the Error code
Packit Service 31306d
   * @returns SSH_FATAL Fatal error happened (not recoverable)
Packit Service 31306d
   * @returns SSH_REQUEST_DENIED Request was denied by remote host
Packit Service 31306d
   * @see ssh_get_error_code
Packit Service 31306d
   */
Packit Service 31306d
  int getCode(){
Packit Service 31306d
    return code;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns the error message of the last exception
Packit Service 31306d
   * @returns pointer to a c string containing the description of error
Packit Service 31306d
   * @see ssh_get_error
Packit Service 31306d
   */
Packit Service 31306d
  std::string getError(){
Packit Service 31306d
    return description;
Packit Service 31306d
  }
Packit Service 31306d
private:
Packit Service 31306d
  int code;
Packit Service 31306d
  std::string description;
Packit Service 31306d
};
Packit Service 31306d
Packit Service 31306d
/** @internal
Packit Service 31306d
 * @brief Macro to throw exception if there was an error
Packit Service 31306d
 */
Packit Service 31306d
#define ssh_throw(x) if((x)==SSH_ERROR) throw SshException(getCSession())
Packit Service 31306d
#define ssh_throw_null(CSession,x) if((x)==NULL) throw SshException(CSession)
Packit Service 31306d
#define void_throwable void
Packit Service 31306d
#define return_throwable return
Packit Service 31306d
Packit Service 31306d
#else
Packit Service 31306d
Packit Service 31306d
/* No exception at all. All functions will return an error code instead
Packit Service 31306d
 * of an exception
Packit Service 31306d
 */
Packit Service 31306d
#define ssh_throw(x) if((x)==SSH_ERROR) return SSH_ERROR
Packit Service 31306d
#define ssh_throw_null(CSession,x) if((x)==NULL) return NULL
Packit Service 31306d
#define void_throwable int
Packit Service 31306d
#define return_throwable return SSH_OK
Packit Service 31306d
#endif
Packit Service 31306d
Packit Service 31306d
/**
Packit Service 31306d
 * The ssh::Session class contains the state of a SSH connection.
Packit Service 31306d
 */
Packit Service 31306d
class Session {
Packit Service 31306d
  friend class Channel;
Packit Service 31306d
public:
Packit Service 31306d
  Session(){
Packit Service 31306d
    c_session=ssh_new();
Packit Service 31306d
  }
Packit Service 31306d
  ~Session(){
Packit Service 31306d
    ssh_free(c_session);
Packit Service 31306d
    c_session=NULL;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief sets an SSH session options
Packit Service 31306d
   * @param type Type of option
Packit Service 31306d
   * @param option cstring containing the value of option
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_options_set
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable setOption(enum ssh_options_e type, const char *option){
Packit Service 31306d
    ssh_throw(ssh_options_set(c_session,type,option));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief sets an SSH session options
Packit Service 31306d
   * @param type Type of option
Packit Service 31306d
   * @param option long integer containing the value of option
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_options_set
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable setOption(enum ssh_options_e type, long int option){
Packit Service 31306d
    ssh_throw(ssh_options_set(c_session,type,&option));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief sets an SSH session options
Packit Service 31306d
   * @param type Type of option
Packit Service 31306d
   * @param option void pointer containing the value of option
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_options_set
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable setOption(enum ssh_options_e type, void *option){
Packit Service 31306d
    ssh_throw(ssh_options_set(c_session,type,option));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief connects to the remote host
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_connect
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable connect(){
Packit Service 31306d
    int ret=ssh_connect(c_session);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Authenticates automatically using public key
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS, SSH_AUTH_PARTIAL, SSH_AUTH_DENIED
Packit Service 31306d
   * @see ssh_userauth_autopubkey
Packit Service 31306d
   */
Packit Service 31306d
  int userauthPublickeyAuto(void){
Packit Service 31306d
    int ret=ssh_userauth_publickey_auto(c_session, NULL, NULL);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Authenticates using the "none" method. Prefer using autopubkey if
Packit Service 31306d
   * possible.
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS, SSH_AUTH_PARTIAL, SSH_AUTH_DENIED
Packit Service 31306d
   * @see ssh_userauth_none
Packit Service 31306d
   * @see Session::userauthAutoPubkey
Packit Service 31306d
   */
Packit Service 31306d
  int userauthNone(){
Packit Service 31306d
    int ret=ssh_userauth_none(c_session,NULL);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /**
Packit Service 31306d
   * @brief Authenticate through the "keyboard-interactive" method.
Packit Service 31306d
   *
Packit Service 31306d
   * @param[in] username The username to authenticate. You can specify NULL if
Packit Service 31306d
   *                     ssh_option_set_username() has been used. You cannot
Packit Service 31306d
   *                     try two different logins in a row.
Packit Service 31306d
   *
Packit Service 31306d
   * @param[in] submethods Undocumented. Set it to NULL.
Packit Service 31306d
   *
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   *
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS, SSH_AUTH_PARTIAL, SSH_AUTH_DENIED,
Packit Service 31306d
   *          SSH_AUTH_ERROR, SSH_AUTH_INFO, SSH_AUTH_AGAIN
Packit Service 31306d
   *
Packit Service 31306d
   * @see ssh_userauth_kbdint
Packit Service 31306d
   */
Packit Service 31306d
  int userauthKbdint(const char* username, const char* submethods){
Packit Service 31306d
    int ret = ssh_userauth_kbdint(c_session, username, submethods);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief Get the number of prompts (questions) the server has given.
Packit Service 31306d
   * @returns The number of prompts.
Packit Service 31306d
   * @see ssh_userauth_kbdint_getnprompts
Packit Service 31306d
   */
Packit Service 31306d
  int userauthKbdintGetNPrompts(){
Packit Service 31306d
    return ssh_userauth_kbdint_getnprompts(c_session);
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /**
Packit Service 31306d
   * @brief Set the answer for a question from a message block.
Packit Service 31306d
   *
Packit Service 31306d
   * @param[in] index The index number of the prompt.
Packit Service 31306d
   * @param[in] answer The answer to give to the server. The answer MUST be
Packit Service 31306d
   *                   encoded UTF-8. It is up to the server how to interpret
Packit Service 31306d
   *                   the value and validate it. However, if you read the
Packit Service 31306d
   *                   answer in some other encoding, you MUST convert it to
Packit Service 31306d
   *                   UTF-8.
Packit Service 31306d
   *
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   *
Packit Service 31306d
   * @returns 0 on success, < 0 on error
Packit Service 31306d
   *
Packit Service 31306d
   * @see ssh_userauth_kbdint_setanswer
Packit Service 31306d
   */
Packit Service 31306d
  int userauthKbdintSetAnswer(unsigned int index, const char *answer)
Packit Service 31306d
  {
Packit Service 31306d
    int ret = ssh_userauth_kbdint_setanswer(c_session, index, answer);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
Packit Service 31306d
Packit Service 31306d
  /** @brief Authenticates using the password method.
Packit Service 31306d
   * @param[in] password password to use for authentication
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS, SSH_AUTH_PARTIAL, SSH_AUTH_DENIED
Packit Service 31306d
   * @see ssh_userauth_password
Packit Service 31306d
   */
Packit Service 31306d
  int userauthPassword(const char *password){
Packit Service 31306d
    int ret=ssh_userauth_password(c_session,NULL,password);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Try to authenticate using the publickey method.
Packit Service 31306d
   * @param[in] pubkey public key to use for authentication
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS if the pubkey is accepted,
Packit Service 31306d
   * @returns SSH_AUTH_DENIED if the pubkey is denied
Packit Service 31306d
   * @see ssh_userauth_try_pubkey
Packit Service 31306d
   */
Packit Service 31306d
  int userauthTryPublickey(ssh_key pubkey){
Packit Service 31306d
    int ret=ssh_userauth_try_publickey(c_session, NULL, pubkey);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Authenticates using the publickey method.
Packit Service 31306d
   * @param[in] privkey private key to use for authentication
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns SSH_AUTH_SUCCESS, SSH_AUTH_PARTIAL, SSH_AUTH_DENIED
Packit Service 31306d
   * @see ssh_userauth_pubkey
Packit Service 31306d
   */
Packit Service 31306d
  int userauthPublickey(ssh_key privkey){
Packit Service 31306d
    int ret=ssh_userauth_publickey(c_session, NULL, privkey);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief Returns the available authentication methods from the server
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns Bitfield of available methods.
Packit Service 31306d
   * @see ssh_userauth_list
Packit Service 31306d
   */
Packit Service 31306d
  int getAuthList(){
Packit Service 31306d
    int ret=ssh_userauth_list(c_session, NULL);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Disconnects from the SSH server and closes connection
Packit Service 31306d
   * @see ssh_disconnect
Packit Service 31306d
   */
Packit Service 31306d
  void disconnect(){
Packit Service 31306d
    ssh_disconnect(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Returns the disconnect message from the server, if any
Packit Service 31306d
   * @returns pointer to the message, or NULL. Do not attempt to free
Packit Service 31306d
   * the pointer.
Packit Service 31306d
   */
Packit Service 31306d
  const char *getDisconnectMessage(){
Packit Service 31306d
    const char *msg=ssh_get_disconnect_message(c_session);
Packit Service 31306d
    return msg;
Packit Service 31306d
  }
Packit Service 31306d
  /** @internal
Packit Service 31306d
   * @brief gets error message
Packit Service 31306d
   */
Packit Service 31306d
  const char *getError(){
Packit Service 31306d
    return ssh_get_error(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @internal
Packit Service 31306d
   * @brief returns error code
Packit Service 31306d
   */
Packit Service 31306d
  int getErrorCode(){
Packit Service 31306d
    return ssh_get_error_code(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns the file descriptor used for the communication
Packit Service 31306d
   * @returns the file descriptor
Packit Service 31306d
   * @warning if a proxycommand is used, this function will only return
Packit Service 31306d
   * one of the two file descriptors being used
Packit Service 31306d
   * @see ssh_get_fd
Packit Service 31306d
   */
Packit Service 31306d
  socket_t getSocket(){
Packit Service 31306d
    return ssh_get_fd(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief gets the Issue banner from the ssh server
Packit Service 31306d
   * @returns the issue banner. This is generally a MOTD from server
Packit Service 31306d
   * @see ssh_get_issue_banner
Packit Service 31306d
   */
Packit Service 31306d
  std::string getIssueBanner(){
Packit Service 31306d
    char *banner = ssh_get_issue_banner(c_session);
Packit Service 31306d
    std::string ret = "";
Packit Service 31306d
    if (banner != NULL) {
Packit Service 31306d
      ret = std::string(banner);
Packit Service 31306d
      ::free(banner);
Packit Service 31306d
    }
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns the OpenSSH version (server) if possible
Packit Service 31306d
   * @returns openssh version code
Packit Service 31306d
   * @see ssh_get_openssh_version
Packit Service 31306d
   */
Packit Service 31306d
  int getOpensshVersion(){
Packit Service 31306d
    return ssh_get_openssh_version(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns the version of the SSH protocol being used
Packit Service 31306d
   * @returns the SSH protocol version
Packit Service 31306d
   * @see ssh_get_version
Packit Service 31306d
   */
Packit Service 31306d
  int getVersion(){
Packit Service 31306d
    return ssh_get_version(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief verifies that the server is known
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @returns Integer value depending on the knowledge of the
Packit Service 31306d
   * server key
Packit Service 31306d
   * @see ssh_session_update_known_hosts
Packit Service 31306d
   */
Packit Service 31306d
  int isServerKnown(){
Packit Service 31306d
    int state = ssh_session_is_known_server(c_session);
Packit Service 31306d
    ssh_throw(state);
Packit Service 31306d
    return state;
Packit Service 31306d
  }
Packit Service 31306d
  void log(int priority, const char *format, ...){
Packit Service 31306d
    char buffer[1024];
Packit Service 31306d
    va_list va;
Packit Service 31306d
Packit Service 31306d
    va_start(va, format);
Packit Service 31306d
    vsnprintf(buffer, sizeof(buffer), format, va);
Packit Service 31306d
    va_end(va);
Packit Service 31306d
    _ssh_log(priority, "libsshpp", "%s", buffer);
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief copies options from a session to another
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_options_copy
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable optionsCopy(const Session &source){
Packit Service 31306d
    ssh_throw(ssh_options_copy(source.c_session,&c_session));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief parses a configuration file for options
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @param[in] file configuration file name
Packit Service 31306d
   * @see ssh_options_parse_config
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable optionsParseConfig(const char *file){
Packit Service 31306d
    ssh_throw(ssh_options_parse_config(c_session,file));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief silently disconnect from remote host
Packit Service 31306d
   * @see ssh_silent_disconnect
Packit Service 31306d
   */
Packit Service 31306d
  void silentDisconnect(){
Packit Service 31306d
    ssh_silent_disconnect(c_session);
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Writes the known host file with current
Packit Service 31306d
   * host key
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_write_knownhost
Packit Service 31306d
   */
Packit Service 31306d
  int writeKnownhost(){
Packit Service 31306d
    int ret = ssh_session_update_known_hosts(c_session);
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief accept an incoming forward connection
Packit Service 31306d
   * @param[in] timeout_ms timeout for waiting, in ms
Packit Service 31306d
   * @returns new Channel pointer on the forward connection
Packit Service 31306d
   * @returns NULL in case of error
Packit Service 31306d
   * @warning you have to delete this pointer after use
Packit Service 31306d
   * @see ssh_channel_forward_accept
Packit Service 31306d
   * @see Session::listenForward
Packit Service 31306d
   */
Packit Service 31306d
  inline Channel *acceptForward(int timeout_ms);
Packit Service 31306d
  /* implemented outside the class due Channel references */
Packit Service 31306d
Packit Service 31306d
  void_throwable cancelForward(const char *address, int port){
Packit Service 31306d
    int err=ssh_channel_cancel_forward(c_session, address, port);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  void_throwable listenForward(const char *address, int port,
Packit Service 31306d
      int &boundport){
Packit Service 31306d
    int err=ssh_channel_listen_forward(c_session, address, port, &boundport);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  ssh_session getCSession(){
Packit Service 31306d
    return c_session;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
protected:
Packit Service 31306d
  ssh_session c_session;
Packit Service 31306d
Packit Service 31306d
private:
Packit Service 31306d
  /* No copy constructor, no = operator */
Packit Service 31306d
  Session(const Session &);
Packit Service 31306d
  Session& operator=(const Session &);
Packit Service 31306d
};
Packit Service 31306d
Packit Service 31306d
/** @brief the ssh::Channel class describes the state of an SSH
Packit Service 31306d
 * channel.
Packit Service 31306d
 * @see ssh_channel
Packit Service 31306d
 */
Packit Service 31306d
class Channel {
Packit Service 31306d
  friend class Session;
Packit Service 31306d
public:
Packit Service 31306d
  Channel(Session &ssh_session){
Packit Service 31306d
    channel = ssh_channel_new(ssh_session.getCSession());
Packit Service 31306d
    this->session = &ssh_session;
Packit Service 31306d
  }
Packit Service 31306d
  ~Channel(){
Packit Service 31306d
    ssh_channel_free(channel);
Packit Service 31306d
    channel=NULL;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief accept an incoming X11 connection
Packit Service 31306d
   * @param[in] timeout_ms timeout for waiting, in ms
Packit Service 31306d
   * @returns new Channel pointer on the X11 connection
Packit Service 31306d
   * @returns NULL in case of error
Packit Service 31306d
   * @warning you have to delete this pointer after use
Packit Service 31306d
   * @see ssh_channel_accept_x11
Packit Service 31306d
   * @see Channel::requestX11
Packit Service 31306d
   */
Packit Service 31306d
  Channel *acceptX11(int timeout_ms){
Packit Service 31306d
    ssh_channel x11chan = ssh_channel_accept_x11(channel,timeout_ms);
Packit Service 31306d
    ssh_throw_null(getCSession(),x11chan);
Packit Service 31306d
    Channel *newchan = new Channel(getSession(),x11chan);
Packit Service 31306d
    return newchan;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief change the size of a pseudoterminal
Packit Service 31306d
   * @param[in] cols number of columns
Packit Service 31306d
   * @param[in] rows number of rows
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_channel_change_pty_size
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable changePtySize(int cols, int rows){
Packit Service 31306d
    int err=ssh_channel_change_pty_size(channel,cols,rows);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  /** @brief closes a channel
Packit Service 31306d
   * @throws SshException on error
Packit Service 31306d
   * @see ssh_channel_close
Packit Service 31306d
   */
Packit Service 31306d
  void_throwable close(){
Packit Service 31306d
    ssh_throw(ssh_channel_close(channel));
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  int getExitStatus(){
Packit Service 31306d
    return ssh_channel_get_exit_status(channel);
Packit Service 31306d
  }
Packit Service 31306d
  Session &getSession(){
Packit Service 31306d
    return *session;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns true if channel is in closed state
Packit Service 31306d
   * @see ssh_channel_is_closed
Packit Service 31306d
   */
Packit Service 31306d
  bool isClosed(){
Packit Service 31306d
    return ssh_channel_is_closed(channel) != 0;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns true if channel is in EOF state
Packit Service 31306d
   * @see ssh_channel_is_eof
Packit Service 31306d
   */
Packit Service 31306d
  bool isEof(){
Packit Service 31306d
    return ssh_channel_is_eof(channel) != 0;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief returns true if channel is in open state
Packit Service 31306d
   * @see ssh_channel_is_open
Packit Service 31306d
   */
Packit Service 31306d
  bool isOpen(){
Packit Service 31306d
    return ssh_channel_is_open(channel) != 0;
Packit Service 31306d
  }
Packit Service 31306d
  int openForward(const char *remotehost, int remoteport,
Packit Service 31306d
      const char *sourcehost=NULL, int localport=0){
Packit Service 31306d
    int err=ssh_channel_open_forward(channel,remotehost,remoteport,
Packit Service 31306d
        sourcehost, localport);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  /* TODO: completely remove this ? */
Packit Service 31306d
  void_throwable openSession(){
Packit Service 31306d
    int err=ssh_channel_open_session(channel);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  int poll(bool is_stderr=false){
Packit Service 31306d
    int err=ssh_channel_poll(channel,is_stderr);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  int read(void *dest, size_t count){
Packit Service 31306d
    int err;
Packit Service 31306d
    /* handle int overflow */
Packit Service 31306d
    if(count > 0x7fffffff)
Packit Service 31306d
      count = 0x7fffffff;
Packit Service 31306d
    err=ssh_channel_read_timeout(channel,dest,count,false,-1);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  int read(void *dest, size_t count, int timeout){
Packit Service 31306d
    int err;
Packit Service 31306d
    /* handle int overflow */
Packit Service 31306d
    if(count > 0x7fffffff)
Packit Service 31306d
      count = 0x7fffffff;
Packit Service 31306d
    err=ssh_channel_read_timeout(channel,dest,count,false,timeout);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  int read(void *dest, size_t count, bool is_stderr=false, int timeout=-1){
Packit Service 31306d
    int err;
Packit Service 31306d
    /* handle int overflow */
Packit Service 31306d
    if(count > 0x7fffffff)
Packit Service 31306d
      count = 0x7fffffff;
Packit Service 31306d
    err=ssh_channel_read_timeout(channel,dest,count,is_stderr,timeout);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  int readNonblocking(void *dest, size_t count, bool is_stderr=false){
Packit Service 31306d
    int err;
Packit Service 31306d
    /* handle int overflow */
Packit Service 31306d
    if(count > 0x7fffffff)
Packit Service 31306d
      count = 0x7fffffff;
Packit Service 31306d
    err=ssh_channel_read_nonblocking(channel,dest,count,is_stderr);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  void_throwable requestEnv(const char *name, const char *value){
Packit Service 31306d
    int err=ssh_channel_request_env(channel,name,value);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  void_throwable requestExec(const char *cmd){
Packit Service 31306d
    int err=ssh_channel_request_exec(channel,cmd);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  void_throwable requestPty(const char *term=NULL, int cols=0, int rows=0){
Packit Service 31306d
    int err;
Packit Service 31306d
    if(term != NULL && cols != 0 && rows != 0)
Packit Service 31306d
      err=ssh_channel_request_pty_size(channel,term,cols,rows);
Packit Service 31306d
    else
Packit Service 31306d
      err=ssh_channel_request_pty(channel);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  void_throwable requestShell(){
Packit Service 31306d
    int err=ssh_channel_request_shell(channel);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  void_throwable requestSendSignal(const char *signum){
Packit Service 31306d
    int err=ssh_channel_request_send_signal(channel, signum);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  void_throwable requestSubsystem(const char *subsystem){
Packit Service 31306d
    int err=ssh_channel_request_subsystem(channel,subsystem);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  int requestX11(bool single_connection,
Packit Service 31306d
      const char *protocol, const char *cookie, int screen_number){
Packit Service 31306d
    int err=ssh_channel_request_x11(channel,single_connection,
Packit Service 31306d
        protocol, cookie, screen_number);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return err;
Packit Service 31306d
  }
Packit Service 31306d
  void_throwable sendEof(){
Packit Service 31306d
    int err=ssh_channel_send_eof(channel);
Packit Service 31306d
    ssh_throw(err);
Packit Service 31306d
    return_throwable;
Packit Service 31306d
  }
Packit Service 31306d
  /** @brief Writes on a channel
Packit Service 31306d
   * @param data data to write.
Packit Service 31306d
   * @param len number of bytes to write.
Packit Service 31306d
   * @param is_stderr write should be done on the stderr channel (server only)
Packit Service 31306d
   * @returns number of bytes written
Packit Service 31306d
   * @throws SshException in case of error
Packit Service 31306d
   * @see channel_write
Packit Service 31306d
   * @see channel_write_stderr
Packit Service 31306d
   */
Packit Service 31306d
  int write(const void *data, size_t len, bool is_stderr=false){
Packit Service 31306d
    int ret;
Packit Service 31306d
    if(is_stderr){
Packit Service 31306d
      ret=ssh_channel_write_stderr(channel,data,len);
Packit Service 31306d
    } else {
Packit Service 31306d
      ret=ssh_channel_write(channel,data,len);
Packit Service 31306d
    }
Packit Service 31306d
    ssh_throw(ret);
Packit Service 31306d
    return ret;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  ssh_session getCSession(){
Packit Service 31306d
    return session->getCSession();
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  ssh_channel getCChannel() {
Packit Service 31306d
    return channel;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
protected:
Packit Service 31306d
  Session *session;
Packit Service 31306d
  ssh_channel channel;
Packit Service 31306d
Packit Service 31306d
private:
Packit Service 31306d
  Channel (Session &ssh_session, ssh_channel c_channel){
Packit Service 31306d
    this->channel=c_channel;
Packit Service 31306d
    this->session = &ssh_session;
Packit Service 31306d
  }
Packit Service 31306d
  /* No copy and no = operator */
Packit Service 31306d
  Channel(const Channel &);
Packit Service 31306d
  Channel &operator=(const Channel &);
Packit Service 31306d
};
Packit Service 31306d
Packit Service 31306d
Packit Service 31306d
inline Channel *Session::acceptForward(int timeout_ms){
Packit Service 31306d
    ssh_channel forward =
Packit Service 31306d
        ssh_channel_accept_forward(c_session, timeout_ms, NULL);
Packit Service 31306d
    ssh_throw_null(c_session,forward);
Packit Service 31306d
    Channel *newchan = new Channel(*this,forward);
Packit Service 31306d
    return newchan;
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
} // namespace ssh
Packit Service 31306d
Packit Service 31306d
/** @} */
Packit Service 31306d
#endif /* LIBSSHPP_HPP_ */