another test case
[ghsmtp.git] / Sock.hpp
blob924e908f9a1d86ac67bbd6d2019db465d34158b7
1 #ifndef SOCK_DOT_HPP
2 #define SOCK_DOT_HPP
4 #include <string>
6 #include "SockBuffer.hpp"
7 #include "sa.hpp"
9 namespace Config {
10 constexpr auto read_timeout_default = std::chrono::seconds(30);
11 constexpr auto write_timeout_default = std::chrono::seconds(30);
12 constexpr auto starttls_timeout_default = std::chrono::seconds(30);
13 } // namespace Config
15 class Sock {
16 public:
17 Sock(const Sock&) = delete;
18 Sock& operator=(const Sock&) = delete;
20 Sock(int fd_in,
21 int fd_out,
22 std::function<void(void)> read_hook = []() {},
23 std::chrono::milliseconds read_timeout = Config::read_timeout_default,
24 std::chrono::milliseconds write_timeout = Config::write_timeout_default,
25 std::chrono::milliseconds starttls_timeout
26 = Config::starttls_timeout_default);
28 char const* us_c_str() const { return us_addr_str_; }
29 char const* them_c_str() const { return them_addr_str_; }
30 std::string const& us_address_literal() const { return us_address_literal_; }
31 std::string const& them_address_literal() const
33 return them_address_literal_;
35 bool has_peername() const { return them_addr_str_[0] != '\0'; }
36 bool input_ready(std::chrono::milliseconds wait)
38 return iostream_->input_ready(wait);
40 bool maxed_out() { return iostream_->maxed_out(); }
41 bool timed_out() { return iostream_->timed_out(); }
43 std::istream& in() { return iostream_; }
44 std::ostream& out() { return iostream_; }
46 bool starttls_server(fs::path config_path)
48 return iostream_->starttls_server(config_path);
50 bool starttls_client(fs::path config_path,
51 char const* client_name,
52 char const* server_name,
53 DNS::RR_collection const& tlsa_rrs,
54 bool enforce_dane)
56 return iostream_->starttls_client(config_path, client_name, server_name,
57 tlsa_rrs, enforce_dane);
59 bool tls() { return iostream_->tls(); }
60 std::string tls_info() { return iostream_->tls_info(); }
61 bool verified() { return iostream_->verified(); };
63 void set_max_read(std::streamsize max) { iostream_->set_max_read(max); }
65 void log_data_on() { iostream_->log_data_on(); }
66 void log_data_off() { iostream_->log_data_off(); }
68 void log_stats() { iostream_->log_stats(); }
69 void log_totals() { iostream_->log_totals(); }
71 void close_fds() { iostream_->close_fds(); }
73 private:
74 boost::iostreams::stream<SockBuffer> iostream_;
76 socklen_t us_addr_len_{sizeof us_addr_};
77 socklen_t them_addr_len_{sizeof them_addr_};
79 sa::sockaddrs us_addr_{};
80 sa::sockaddrs them_addr_{};
82 char us_addr_str_[INET6_ADDRSTRLEN]{'\0'};
83 char them_addr_str_[INET6_ADDRSTRLEN]{'\0'};
85 std::string us_address_literal_;
86 std::string them_address_literal_;
89 #endif // SOCK_DOT_HPP