Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Testing
*.junit
.claude
callgrind.*
_codeql_detected_source_root
cmake_test_discovery_*.json
googletest_discovery_*.json
keylog.log
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ if (ENABLE_COVERAGE)
set(COVERAGE_HTML_DIR "${CMAKE_BINARY_DIR}/coverage_report")
set(COVERAGE_MERGE_SCRIPT "${CMAKE_SOURCE_DIR}/cmake/merge_profraw.cmake")
set(COVERAGE_TEST_BIN "${CMAKE_BINARY_DIR}/test/test_all")
set(LLVM_COV_IGNORE_REGEX "/usr/|${CMAKE_BINARY_DIR}/|${CMAKE_SOURCE_DIR}/test/|${CMAKE_SOURCE_DIR}/src/ngtcp2/")
set(LLVM_COV_IGNORE_REGEX "/usr/|${CMAKE_BINARY_DIR}/|${CMAKE_SOURCE_DIR}/test/")

add_custom_target(coverage
COMMAND ${CMAKE_COMMAND} -E rm -rf ${COVERAGE_RAW_DIR} ${COVERAGE_HTML_DIR}
Expand Down
5 changes: 5 additions & 0 deletions HTTP3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HTTP/3 Manual Testing

`osslclient` and `osslserver` are the [ngtcp2](https://github.com/ngtcp2/ngtcp2) example
client and server; the devcontainer image builds them from upstream and installs them as
`/usr/local/bin/osslclient` and `/usr/local/bin/osslserver`. A few small helpers from the
same examples live in `anyhttp/h3_common.hpp`.

```sh
osslserver ::1 8080 pki/out/server-key.pem pki/out/server-chain.pem
```
Expand Down
1 change: 0 additions & 1 deletion _codeql_detected_source_root

This file was deleted.

46 changes: 46 additions & 0 deletions include/anyhttp/h3_common.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once

#include <nghttp3/nghttp3.h>
#include <ngtcp2/ngtcp2.h>

#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <optional>
#include <span>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -46,6 +52,46 @@ void log_headers(std::string_view log_prefix,
/// still pays for the formatting, while a NULL one makes ngtcp2 skip that work entirely.
void ngtcp2_log_printf(void* user, const char* fmt, ...) noexcept;

// =================================================================================================
//
// Below: helpers taken from the ngtcp2 examples (https://github.com/ngtcp2/ngtcp2, examples/,
// v1.25.0, MIT licensed), reduced to what anyhttp actually uses. They used to be vendored
// verbatim under src/ngtcp2/.
//

/// A socket address of any of the families we speak, as ngtcp2 hands them around.
union sockaddr_union
{
sockaddr_storage storage;
sockaddr sa;
sockaddr_in6 in6;
sockaddr_in in;
};

/// A socket address together with its actual length and the interface it was seen on.
struct Address
{
socklen_t len;
union sockaddr_union su;
uint32_t ifindex;
};

/// Returns the local (destination) address of the packet described by \p msg, as delivered by
/// IP(V6)_PKTINFO. \p family is the address family the packet was received from.
std::optional<Address> msghdr_get_local_addr(msghdr* msg, int family);

/// Copies the port of \p src into \p dst.
void set_port(Address& dst, const Address& src);

/// The current steady clock reading in nanoseconds, which is the timestamp ngtcp2 expects.
ngtcp2_tstamp timestamp();

/// Stringifies \p sa of length \p salen in the format "[IP]:PORT".
std::string straddr(const sockaddr* sa, socklen_t salen);

/// Formats \p len bytes at \p data as lowercase hex, for logging connection IDs.
std::string format_hex(const uint8_t* data, size_t len);

// =================================================================================================

} // namespace anyhttp::http3
5 changes: 0 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ target_sources(anyhttp PRIVATE ${anyhttp_sources})
target_include_directories(anyhttp PUBLIC ../include)

target_include_directories(anyhttp PRIVATE "/opt/nghttp3/build/include")
#
# Small helpers taken from the ngtcp2 examples (see ngtcp2/README.md).
#
target_sources(anyhttp PRIVATE ngtcp2/shared.cc ngtcp2/util.cc)

target_link_libraries(anyhttp PRIVATE Threads::Threads)
target_link_libraries(anyhttp PRIVATE OpenSSL::SSL)
target_link_libraries(anyhttp PRIVATE Boost::thread Boost::atomic Boost::url Boost::filesystem)
Expand Down
8 changes: 4 additions & 4 deletions src/h3_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
#include <string_view>
#include <vector>

#include "ngtcp2/util.h"

using namespace std::chrono_literals;
using namespace boost::asio;
namespace errc = boost::system::errc;

using anyhttp::http3::format_hex;
using anyhttp::http3::log_headers;
using anyhttp::http3::make_nv;
using anyhttp::http3::straddr;

namespace anyhttp::client
{
Expand Down Expand Up @@ -442,7 +442,7 @@ int Http3ClientSession::init(asio::ip::udp::endpoint remote)
return -1;
}

log_prefix_ = std::format("h3:{}", ngtcp2::util::straddr(remote.data(), remote.size()));
log_prefix_ = std::format("h3:{}", straddr(remote.data(), remote.size()));

ngtcp2_cid scid{};
scid.datalen = 17;
Expand Down Expand Up @@ -485,7 +485,7 @@ int Http3ClientSession::init(asio::ip::udp::endpoint remote)
if (setup_tls(tls_context().ctx, false /* client */) != 0)
return -1;

logi("[{}] connecting, scid={}", log_prefix_, ngtcp2::util::format_hex(scid.data, scid.datalen));
logi("[{}] connecting, scid={}", log_prefix_, format_hex(scid.data, scid.datalen));
return 0;
}

Expand Down
108 changes: 108 additions & 0 deletions src/h3_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

#include <spdlog/spdlog.h>

#include <netdb.h>

#include <array>
#include <cassert>
#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <format>

namespace anyhttp::http3
{
Expand Down Expand Up @@ -53,6 +59,108 @@ void ngtcp2_log_printf(void* /*user*/, const char* fmt, ...) noexcept
spdlog::trace("{}", buf.data());
}

// =================================================================================================
//
// Below: helpers taken from the ngtcp2 examples, see anyhttp/h3_common.hpp.
//

std::optional<Address> msghdr_get_local_addr(msghdr* msg, int family)
{
switch (family)
{
case AF_INET:
for (auto cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
if (cmsg->cmsg_level != IPPROTO_IP || cmsg->cmsg_type != IP_PKTINFO)
continue;

in_pktinfo pktinfo;
std::memcpy(&pktinfo, CMSG_DATA(cmsg), sizeof(pktinfo));
Address res{.len = sizeof(res.su.in),
.ifindex = static_cast<uint32_t>(pktinfo.ipi_ifindex)};
res.su.in.sin_family = AF_INET;
res.su.in.sin_addr = pktinfo.ipi_addr;
return res;
}
return {};

case AF_INET6:
for (auto cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
if (cmsg->cmsg_level != IPPROTO_IPV6 || cmsg->cmsg_type != IPV6_PKTINFO)
continue;

in6_pktinfo pktinfo;
std::memcpy(&pktinfo, CMSG_DATA(cmsg), sizeof(pktinfo));
Address res{.len = sizeof(res.su.in6),
.ifindex = static_cast<uint32_t>(pktinfo.ipi6_ifindex)};
res.su.in6.sin6_family = AF_INET6;
res.su.in6.sin6_addr = pktinfo.ipi6_addr;
return res;
}
return {};
}

return {};
}

void set_port(Address& dst, const Address& src)
{
switch (dst.su.storage.ss_family)
{
case AF_INET:
assert(AF_INET == src.su.storage.ss_family);
dst.su.in.sin_port = src.su.in.sin_port;
return;

case AF_INET6:
assert(AF_INET6 == src.su.storage.ss_family);
dst.su.in6.sin6_port = src.su.in6.sin6_port;
return;

default:
assert(0);
}
}

ngtcp2_tstamp timestamp()
{
using namespace std::chrono;
return static_cast<ngtcp2_tstamp>(
duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count());
}

std::string straddr(const sockaddr* sa, socklen_t salen)
{
std::array<char, NI_MAXHOST> host;
std::array<char, NI_MAXSERV> port;

auto rv = getnameinfo(sa, salen, host.data(), host.size(), port.data(), port.size(),
NI_NUMERICHOST | NI_NUMERICSERV);
if (rv != 0)
{
loge("getnameinfo: {}", gai_strerror(rv));
return {};
}

return std::format("[{}]:{}", host.data(), port.data());
}

std::string format_hex(const uint8_t* data, size_t len)
{
constexpr char xdigits[] = "0123456789abcdef";

std::string res;
res.reserve(len * 2);
for (size_t i = 0; i < len; ++i)
{
res += xdigits[data[i] >> 4];
res += xdigits[data[i] & 0xf];
}

return res;
}

// =================================================================================================

} // namespace anyhttp::http3
Loading
Loading