Skip to content

Commit

Permalink
squash v3
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Jan 15, 2025
1 parent 5116981 commit 55a50be
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 104 deletions.
4 changes: 2 additions & 2 deletions llarp/crypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ namespace llarp
SharedSecret& secret,
const SymmNonce& nonce,
const RouterID& remote,
old_uspan payload)
std::span<uint8_t> payload)
{
// derive shared key
if (!crypto::dh_client(secret, remote, shared_key, nonce))
Expand All @@ -262,7 +262,7 @@ namespace llarp
SharedSecret& shared,
const PubKey& remote,
const SymmNonce& nonce,
old_uspan encrypted)
std::span<uint8_t> encrypted)
{
// derive shared secret using shared secret and our secret key (and nonce)
if (!crypto::dh_server(shared, remote, local_sk, nonce))
Expand Down
4 changes: 2 additions & 2 deletions llarp/crypto/crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace llarp
SharedSecret& secret,
const SymmNonce& nonce,
const RouterID& remote,
old_uspan payload);
std::span<uint8_t> payload);

// void derive_encrypt_outer_wrapping(
// const Ed25519SecretKey& shared_key,
Expand All @@ -85,7 +85,7 @@ namespace llarp
SharedSecret& shared,
const PubKey& remote,
const SymmNonce& nonce,
old_uspan encrypted);
std::span<uint8_t> encrypted);

std::array<unsigned char, 32> make_scalar(const PubKey& k, uint64_t domain);

Expand Down
4 changes: 2 additions & 2 deletions llarp/crypto/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ namespace llarp
throw std::runtime_error{"Server DH failed -- should this even ever happen?"};
}

void shared_kx_data::encrypt(old_uspan data)
void shared_kx_data::encrypt(std::span<uint8_t> data)
{
if (!crypto::xchacha20(data.data(), data.size(), shared_secret, nonce))
throw std::runtime_error{"xchacha20 encryption failed -- should this even ever happen?"};
}

// identical methods, separated for clarity of use/logging for now
void shared_kx_data::decrypt(old_uspan data)
void shared_kx_data::decrypt(std::span<uint8_t> data)
{
if (!crypto::xchacha20(data.data(), data.size(), shared_secret, nonce))
throw std::runtime_error{"xchacha20 decryption failed -- should this even ever happen?"};
Expand Down
12 changes: 6 additions & 6 deletions llarp/crypto/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ namespace llarp
explicit Ed25519PrivateData(const AlignedBuffer<64>& key_and_hash) : AlignedBuffer<64>(key_and_hash) {}

// Returns writeable access to the 32-byte Ed25519 Private Scalar
old_uspan scalar() { return {data(), 32}; }
std::span<uint8_t> scalar() { return {data(), 32}; }
// Returns readable access to the 32-byte Ed25519 Private Scalar
const_uspan scalar() const { return {data(), 32}; }
uspan scalar() const { return {data(), 32}; }
// Returns writeable access to the 32-byte Ed25519 Signing Hash
old_uspan signing_hash() { return {data() + 32, 32}; }
std::span<uint8_t> signing_hash() { return {data() + 32, 32}; }
// Returns readable access to the 32-byte Ed25519 Signing Hash
const_uspan signing_hash() const { return {data() + 32, 32}; }
uspan signing_hash() const { return {data() + 32, 32}; }

PubKey to_pubkey() const;

Expand Down Expand Up @@ -125,9 +125,9 @@ namespace llarp

void server_dh(const Ed25519SecretKey& local_sk);

void encrypt(old_uspan data);
void encrypt(std::span<uint8_t> data);

void decrypt(old_uspan enc);
void decrypt(std::span<uint8_t> enc);
};

} // namespace llarp
2 changes: 1 addition & 1 deletion llarp/dns/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace llarp::dns
return true;
}

bool MessageHeader::decode(std::span<unsigned char> b)
bool MessageHeader::decode(std::span<uint8_t> b)
{
std::memcpy(_data.data(), b.data(), sizeof(_data));
for (auto& d : _data)
Expand Down
4 changes: 2 additions & 2 deletions llarp/dns/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace llarp

bool Decode(llarp_buffer_t* buf) override;

bool decode(std::span<unsigned char> b) override;
bool decode(std::span<uint8_t> b) override;

nlohmann::json ToJSON() const override;

Expand Down Expand Up @@ -80,7 +80,7 @@ namespace llarp

bool Decode(llarp_buffer_t* buf) override;

bool decode(std::span<unsigned char> /* b */) override { return {}; }; // TODO:
bool decode(std::span<uint8_t> /* b */) override { return {}; }; // TODO:

// Wrapper around Encode that encodes into a new buffer and returns it
std::vector<uint8_t> to_buffer() const;
Expand Down
2 changes: 1 addition & 1 deletion llarp/dns/question.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace llarp::dns

bool Decode(llarp_buffer_t* buf) override;

bool decode(std::span<unsigned char> /* b */) override { return {}; }
bool decode(std::span<uint8_t> /* b */) override { return {}; }

std::string to_string() const;

Expand Down
2 changes: 1 addition & 1 deletion llarp/dns/rr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace llarp::dns

bool Decode(llarp_buffer_t* buf) override;

bool decode(std::span<unsigned char> /* b */) override { return {}; };
bool decode(std::span<uint8_t> /* b */) override { return {}; };

nlohmann::json ToJSON() const override;

Expand Down
2 changes: 1 addition & 1 deletion llarp/dns/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace llarp::dns
/// decode entity from buffer
virtual bool Decode(llarp_buffer_t* buf) = 0;

virtual bool decode(std::span<unsigned char> b) = 0;
virtual bool decode(std::span<uint8_t> b) = 0;

/// convert this whatever into json
virtual nlohmann::json ToJSON() const = 0;
Expand Down
45 changes: 24 additions & 21 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace llarp::handlers
update_and_publish_localcc(get_current_client_intros(), _srv_records);
}

// static std::atomic<bool> testnet_trigger = false;
static std::atomic<bool> testnet_trigger = false;

void SessionEndpoint::start_tickers()
{
Expand All @@ -152,25 +152,24 @@ namespace llarp::handlers
},
true);

// if (not testnet_trigger)
// {
// testnet_trigger = true;

// _router.loop()->call_later(5s, [this]() {
// try
// {
// RouterID cpk{oxenc::from_base32z("4g96taie3et7dbkumk5x6rycskaxxsgjeiunpe61469z1gsbfkuo")};
// log::info(logcat, "Beginning session init to client: {}", cpk.to_network_address(false));
// _initiate_session(
// NetworkAddress::from_pubkey(cpk, true), [](ip_v) { log::critical(logcat, "FUCK YEAH");
// });
// }
// catch (const std::exception& e)
// {
// log::critical(logcat, "Failed to parse client netaddr: {}", e.what());
// }
// });
// }
if (not testnet_trigger)
{
testnet_trigger = true;

_router.loop()->call_later(5s, [this]() {
try
{
RouterID cpk{oxenc::from_base32z("acit6x8kwxdehpkzrpunw5nb8mf4w5u8tn3ojmxit9rpnhhhp81y")};
log::info(logcat, "Beginning session init to client: {}", cpk.to_network_address(false));
_initiate_session(
NetworkAddress::from_pubkey(cpk, true), [](ip_v) { log::critical(logcat, "FUCK YEAH"); });
}
catch (const std::exception& e)
{
log::critical(logcat, "Failed to parse client netaddr: {}", e.what());
}
});
}
}
else
log::info(logcat, "SessionEndpoint configured to NOT publish ClientContact...");
Expand Down Expand Up @@ -630,7 +629,11 @@ namespace llarp::handlers
if (auto maybe_ip = _router.tun_endpoint()->map_session_to_local_ip(session->remote()))
{
log::info(
logcat, "TUN device successfully routing session to remote: {}", session->remote());
logcat,
"TUN device successfully routing session (remote: {}) via local ip: {}",
session->remote(),
std::holds_alternative<ipv4>(*maybe_ip) ? std::get<ipv4>(*maybe_ip).to_string()
: std::get<ipv6>(*maybe_ip).to_string());

return hook(*maybe_ip);
}
Expand Down
9 changes: 0 additions & 9 deletions llarp/handlers/tun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,6 @@ namespace llarp::handlers

void start_poller();

// protected:
struct WritePacket
{
uint64_t seqno;
IPPacket pkt;

bool operator>(const WritePacket& other) const { return seqno > other.seqno; }
};

// Stores assigned IP's for each session in/out of this lokinet instance
// - Reserved local addresses is directly pre-loaded from config
// - Persisting address map is directly pre-loaded from config
Expand Down
2 changes: 1 addition & 1 deletion llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace llarp
{
static auto logcat = llarp::log::Cat("lquic");

static constexpr auto static_shared_key = "Lokinet static shared secret key"_usv;
static constexpr auto static_shared_key = "Lokinet static shared secret key"_usp;

static static_secret make_static_secret(const Ed25519SecretKey& sk)
{
Expand Down
4 changes: 1 addition & 3 deletions llarp/messages/exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace llarp
{
/*
TODO:
- ADD PUBKEY FIELD OR AT LEAST SEE WHY LINKMANAGER::HANDLE_OBTAIN_EXIT() LOOKS FOR ONE
- change these parameters to ustringviews and ustrings where needed after bumping oxenc
- change these parameters to uspans where needed after bumping oxenc
- change std::string sig(64, '\0') --> std::array<unsigned char, 64> sig
*/

Expand Down
57 changes: 5 additions & 52 deletions llarp/util/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,27 @@

namespace llarp
{
using const_cspan = oxenc::const_span<char>;
using const_uspan = oxenc::const_span<unsigned char>;
using const_span = oxenc::const_span<std::byte>;

using old_cspan = std::span<char>;
using old_uspan = std::span<uint8_t>;
using old_bspan = std::span<std::byte>;
using cspan = oxenc::const_span<const char>;
using uspan = oxenc::const_span<const unsigned char>;
using span = oxenc::const_span<const std::byte>;

using ustring = std::basic_string<uint8_t>;
using ustring_view = std::basic_string_view<uint8_t>;
using bstring = std::basic_string<std::byte>;
using bstring_view = std::basic_string_view<std::byte>;

namespace detail
{
template <size_t N>
struct bsv_literal
{
consteval bsv_literal(const char (&s)[N])
{
for (size_t i = 0; i < N; i++)
str[i] = static_cast<std::byte>(s[i]);
}
std::byte str[N]; // we keep the null on the end, in case you pass .data() to a C func
using size = std::integral_constant<size_t, N - 1>;
};
template <size_t N>
struct usv_literal
{
consteval usv_literal(const char (&s)[N])
{
for (size_t i = 0; i < N; i++)
str[i] = static_cast<unsigned char>(s[i]);
}
unsigned char str[N]; // we keep the null on the end, in case you pass .data() to a C func
using size = std::integral_constant<size_t, N - 1>;
};
} // namespace detail

inline ustring operator""_us(const char* str, size_t len) noexcept
{
return {reinterpret_cast<const unsigned char*>(str), len};
}

template <detail::usv_literal UStr>
constexpr ustring_view operator""_usv() noexcept
{
return {UStr.str, decltype(UStr)::size::value};
}

template <detail::bsv_literal BStr>
constexpr bstring_view operator""_bsv()
{
return {BStr.str, decltype(BStr)::size::value};
}

inline bstring operator""_bs(const char* str, size_t len) noexcept
{
return {reinterpret_cast<const std::byte*>(str), len};
}

// Helper function to switch between string_view and ustring_view
inline ustring_view to_usv(std::string_view v) { return {reinterpret_cast<const uint8_t*>(v.data()), v.size()}; }

template <oxenc::basic_char T>
inline old_uspan to_uspan(std::basic_string<T>& v)
inline std::span<uint8_t> to_uspan(std::basic_string<T>& v)
{
return old_uspan{reinterpret_cast<uint8_t*>(v.data()), v.size()};
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(v.data()), v.size()};
}
} // namespace llarp

Expand Down

0 comments on commit 55a50be

Please sign in to comment.