libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
channel.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/config.hpp>
7#include <sshpp/export.hpp>
8#include <sshpp/fwd.hpp>
9#include <sshpp/result.hpp>
10#include <sshpp/types.hpp>
11
12#include <chrono>
13#include <cstdint>
14#include <mutex>
15#include <optional>
16#include <string>
17#include <string_view>
18
19namespace sshpp {
20
21enum class Stream { stdout_, stderr_ };
22
23enum class Signal { abrt, alrm, fpe, hup, ill, int_, kill, pipe, quit, segv, term, usr1, usr2 };
24
25struct ExitState {
26 std::optional<int> status;
27 std::optional<std::string> signal;
28 bool core_dumped = false;
29 std::string error_message;
30};
31
32struct PtySize { int columns = 80; int rows = 24; };
33
36class SSHPP_API Channel {
37public:
38 Channel() = default;
39 ~Channel();
40 Channel(Channel&&) noexcept;
41 Channel& operator=(Channel&&) noexcept;
42 Channel(const Channel&) = delete;
43 Channel& operator=(const Channel&) = delete;
44
45 explicit operator bool() const noexcept { return native_ != nullptr; }
46 native_channel native_handle() const noexcept { return native_; }
48
53 std::recursive_mutex& session_mutex() const noexcept { return core_->mutex(); }
54
55 // ---- opening --------------------------------------------------------
56 Result<void> try_open_session();
57 bool is_open() const noexcept { return open_; }
58
59 // ---- requests --------------------------------------------------------
60 Result<void> try_request_pty(std::string_view term = "xterm-256color", PtySize = {});
61 Result<void> try_change_pty_size(PtySize);
62 Result<void> try_request_shell();
63 Result<void> try_request_exec(std::string_view command);
64 Result<void> try_request_subsystem(std::string_view name);
65 Result<void> try_request_env(std::string_view name, std::string_view value);
66 Result<void> try_send_signal(Signal);
67
68 // ---- I/O ---------------------------------------------------------------
69 Result<std::size_t> try_read_some(MutableByteView buf, Stream = Stream::stdout_);
70 Result<std::size_t> try_read_some(MutableByteView buf, Stream, std::chrono::milliseconds timeout);
71 Result<std::string> try_read_all(Stream = Stream::stdout_, std::size_t limit = 64u << 20);
72
73 Result<std::size_t> try_write_some(ByteView data);
74 Result<std::size_t> try_write_some(ByteView data, Stream stream);
75 Result<void> try_write_all(ByteView data);
76 Result<void> try_write_all(ByteView data, Stream stream);
77 Result<void> try_write_all(std::string_view data);
78 Result<void> try_write_all(std::string_view data, Stream stream);
79
80 Result<std::size_t> try_bytes_available(Stream = Stream::stdout_) const;
81 Result<bool> try_wait_readable(std::chrono::milliseconds, Stream = Stream::stdout_);
82
83 // ---- closing --------------------------------------------------------------
84 Result<void> try_send_eof();
85 bool is_eof() const noexcept;
86 Result<void> try_close();
87 bool is_closed() const noexcept { return !open_; }
88
89 // ---- exit status -----------------------------------------------------------
90 Result<ExitState> try_wait_exit(std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
91 ExitState exit_state() const noexcept;
92
93#if SSHPP_WITH_SERVER
94 // ---- server-side replies (see docs/design/08) -------------------------------
95 Result<void> try_send_exit_status(int code);
96#endif
97
98#if SSHPP_WITH_FORWARDING
99 // ---- forwarding factories (see docs/design/07) ---------------------------------
100 static Result<Channel> open_forward(Session&, std::string_view remote_host, std::uint16_t remote_port,
101 std::string_view origin_host, std::uint16_t origin_port);
102 static Result<Channel> open_forward_unix(Session&, std::string_view remote_socket,
103 std::string_view origin_host, std::uint16_t origin_port);
104#endif
105
106private:
107 Channel(native_channel n, detail::SessionCorePtr core, bool owning)
108 : native_(n), core_(std::move(core)), owning_(owning) {}
109
110 friend class Session;
111#if SSHPP_WITH_FORWARDING
112 friend class X11Forwarder;
113#endif
114
115 native_channel native_ = nullptr;
117 bool owning_ = true;
118 bool open_ = false;
119};
120
121} // namespace sshpp
122
123#if SSHPP_HEADER_ONLY
125#endif
Definition channel.hpp:36
Channel()=default
std::recursive_mutex & session_mutex() const noexcept
Definition channel.hpp:53
bool is_open() const noexcept
Definition channel.hpp:57
native_channel native_handle() const noexcept
Definition channel.hpp:46
Definition result.hpp:16
Definition session.hpp:41
Definition forwarding.hpp:239
std::shared_ptr< SessionCore > SessionCorePtr
Definition session_core.hpp:43
Definition auth.hpp:18
Ownership
Definition types.hpp:16
::ssh_channel_struct * native_channel
Definition native_fwd.hpp:26
Signal
Definition channel.hpp:23
Stream
Definition channel.hpp:21
Definition error.hpp:110
Definition channel.hpp:25
bool core_dumped
Definition channel.hpp:28
std::string error_message
Definition channel.hpp:29
std::optional< int > status
Definition channel.hpp:26
std::optional< std::string > signal
Definition channel.hpp:27
Definition channel.hpp:32
int columns
Definition channel.hpp:32
int rows
Definition channel.hpp:32