Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lib http client #697

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
remove dns size limits, print error queue on fail
  • Loading branch information
weyrick committed Oct 20, 2023
commit d9388076d9a1a56e98a29df465612490295e2e1e
11 changes: 1 addition & 10 deletions libs/visor_http_client/httpssession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ void HTTPSSession::destroy_session()

void HTTPSSession::process_receive(const uint8_t *data, size_t len)
{
// dnsheader is 12, at least one byte for the minimum name,
// two bytes for the qtype and another two for the qclass
const size_t MIN_DNS_RESPONSE_SIZE = 17;
// 512 over UDP without EDNS, but 65535 over TCP
const size_t MAX_DNS_RESPONSE_SIZE = 65535;
if (len < MIN_DNS_RESPONSE_SIZE || len > MAX_DNS_RESPONSE_SIZE) {
std::cerr << "malformed data" << std::endl;
_malformed_data();
return;
}
auto buf = std::make_unique<char[]>(len);
memcpy(buf.get(), (const char *)data, len);
_got_dns_msg(std::move(buf), len);
Expand Down Expand Up @@ -355,6 +345,7 @@ void HTTPSSession::do_handshake()
int error = SSL_get_error(_ssl_session, err);
if (error == SSL_ERROR_SSL || error == SSL_ERROR_SYSCALL) {
std::cerr << "Handshake failed: SSL or syscall error" << std::endl;
ERR_print_errors_fp(stderr);
_handshake_error();
} else if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) {
// Non-fatal error. OpenSSL wants to either read or write.
Expand Down
12 changes: 0 additions & 12 deletions libs/visor_http_client/tcpsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ void TCPSession::close()
// accumulate data and try to extract DNS messages
void TCPSession::receive_data(const char data[], size_t len)
{
// dnsheader is 12, at least one byte for the minimum name,
// two bytes for the qtype and another two for the qclass
const size_t MIN_DNS_RESPONSE_SIZE = 17;

_buffer.append(data, len);

for (;;) {
Expand All @@ -73,14 +69,6 @@ void TCPSession::receive_data(const char data[], size_t len)
// size is in network byte order.
size = static_cast<unsigned char>(_buffer[1]) | static_cast<unsigned char>(_buffer[0]) << 8;

// no need to check the maximum size here since the maximum size
// that a std::uint16t_t can hold, std::numeric_limits<std::uint16_t>::max()
// (65535 bytes) is allowed over TCP
if (size < MIN_DNS_RESPONSE_SIZE) {
_malformed_data();
break;
}

if (_buffer.size() >= sizeof(size) + size) {
auto data = std::make_unique<char[]>(size);
std::memcpy(data.get(), _buffer.data() + sizeof(size), size);
Expand Down
2 changes: 1 addition & 1 deletion libs/visor_http_client/test_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TEST_CASE("HTTP Client", "[http]")
auto tcp_handle = loop->resource<uvw::tcp_handle>(family);

auto malformed_data = [tcp_handle]() {
std::cout << "malformed_data" << std::endl;
std::cout << "malformed_data or handshake error" << std::endl;
tcp_handle->close();
};
auto got_dns_message = []([[maybe_unused]] std::unique_ptr<const char[]> data,
Expand Down