1 #include "SockBuffer.hpp"
5 #include <glog/logging.h>
7 #include <gflags/gflags.h>
9 DEFINE_bool(log_data
, false, "log all protocol data");
11 SockBuffer::SockBuffer(int fd_in
,
13 std::function
<void(void)> read_hook
,
14 std::chrono::milliseconds read_timeout
,
15 std::chrono::milliseconds write_timeout
,
16 std::chrono::milliseconds starttls_timeout
)
19 , read_hook_(read_hook
)
20 , read_timeout_(read_timeout
)
21 , write_timeout_(write_timeout
)
22 , starttls_timeout_(starttls_timeout
)
25 POSIX::set_nonblocking(fd_in_
);
26 POSIX::set_nonblocking(fd_out_
);
27 log_data_
= (FLAGS_log_data
|| (getenv("GHSMTP_LOG_DATA") != nullptr));
30 SockBuffer::SockBuffer(SockBuffer
const& that
)
32 , fd_out_(that
.fd_out_
)
33 , read_hook_(that
.read_hook_
)
34 , read_timeout_(that
.read_timeout_
)
35 , write_timeout_(that
.write_timeout_
)
36 , starttls_timeout_(that
.starttls_timeout_
)
37 , limit_read_(that
.limit_read_
)
38 , log_data_(that
.log_data_
)
39 , tls_(that
.read_hook_
)
41 CHECK(!that
.timed_out_
);
42 CHECK(!that
.tls_active_
);
43 CHECK(!that
.limit_read_
);
44 CHECK(!that
.maxed_out());
47 std::streamsize
SockBuffer::read(char* s
, std::streamsize n
)
50 LOG(ERROR
) << "read attempted when"
51 << " total of " << octets_read_
<< " is over limit of "
53 return static_cast<std::streamsize
>(-1);
55 auto read
= tls_active_
? tls_
.read(s
, n
, read_timeout_
, timed_out_
)
56 : POSIX::read(fd_in_
, s
, n
, read_hook_
, read_timeout_
,
58 if (read
!= static_cast<std::streamsize
>(-1)) {
60 total_octets_read_
+= read
;
63 LOG(ERROR
) << "read of " << read
<< " puts total of " << octets_read_
64 << " over limit of " << read_limit_
;
65 return static_cast<std::streamsize
>(-1);
69 auto str
= std::string(s
, static_cast<size_t>(read
));
70 LOG(INFO
) << "< «" << esc(str
, esc_line_option::multi
) << "»";
76 std::streamsize
SockBuffer::write(const char* s
, std::streamsize n
)
78 auto written
= tls_active_
79 ? tls_
.write(s
, n
, write_timeout_
, timed_out_
)
80 : POSIX::write(fd_out_
, s
, n
, write_timeout_
, timed_out_
);
81 if (written
!= static_cast<std::streamsize
>(-1)) {
82 octets_written_
+= written
;
83 total_octets_written_
+= written
;
87 auto str
= std::string(s
, static_cast<size_t>(written
));
88 LOG(INFO
) << "> «" << esc(str
, esc_line_option::multi
) << "»";
94 void SockBuffer::log_stats() const
96 LOG(INFO
) << "read_limit_==" << (read_limit_
? "true" : "false");
97 LOG(INFO
) << "octets_read_==" << octets_read_
;
98 LOG(INFO
) << "octets_written_==" << octets_written_
;
101 LOG(INFO
) << tls_info();
105 void SockBuffer::log_totals() const
107 LOG(INFO
) << "total_octets_read_==" << total_octets_read_
;
108 LOG(INFO
) << "total_octets_written_==" << total_octets_written_
;