Skip to content

Commit 2520731

Browse files
committed
relay sessions heirarchically implemented
1 parent c2a74a2 commit 2520731

12 files changed

+264
-219
lines changed

llarp/contact/client_contact.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ namespace llarp
132132
public:
133133
intro_set take_intros() && { return std::move(intros); }
134134

135+
intro_set copy_intros() const { return intros; }
136+
135137
bool operator==(const ClientContact& other) const
136138
{
137139
return std::tie(pubkey, intros, SRVs, protos, exit_policy)

llarp/contact/tag.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace llarp
1616

1717
session_tag session_tag::make(uint8_t protos) { return session_tag{static_cast<uint8_t>(protos & proto_mask)}; }
1818

19-
std::tuple<bool, bool> session_tag::proto_bits() const
19+
std::pair<bool, bool> session_tag::proto_bits() const
2020
{
2121
auto& p = buf[0];
2222
return {p & meta::to_underlying(protocol_flag::EXIT), p & meta::to_underlying(protocol_flag::QUICTUN)};

llarp/contact/tag.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace llarp
2020
public:
2121
static session_tag make(uint8_t protocol);
2222

23-
std::tuple<bool, bool> proto_bits() const;
23+
std::pair<bool, bool> proto_bits() const;
2424

2525
void read(std::string_view buf);
2626

llarp/handlers/session.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ namespace llarp::handlers
607607
if (should_publish_cc)
608608
{
609609
log::debug(logcat, "Updating and publishing ClientContact...");
610-
auto intros = get_current_client_intros();
610+
auto intros = get_local_client_intros();
611611
if (intros.empty())
612612
return _localcc_update_fail();
613613
client_contact.regenerate(std::move(intros));
@@ -868,7 +868,7 @@ namespace llarp::handlers
868868

869869
log::trace(logcat, "Remote client has provided session tag: {}", tag);
870870

871-
auto outbound = std::make_shared<session::OutboundSession>(
871+
auto outbound = std::make_shared<session::OutboundClientSession>(
872872
remote,
873873
*this,
874874
std::move(path),

llarp/handlers/session.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace llarp
1919

2020
namespace handlers
2121
{
22-
class SessionEndpoint final : public path::PathHandler, public std::enable_shared_from_this<SessionEndpoint>
22+
class SessionEndpoint final : public path::PathHandler
2323
{
2424
friend class rpc::RPCServer;
2525
friend struct session::BaseSession;
@@ -135,7 +135,7 @@ namespace llarp
135135
template <typename... Opt>
136136
void update_and_publish_localcc(Opt&&... args)
137137
{
138-
auto intros = get_current_client_intros();
138+
auto intros = get_local_client_intros();
139139
if (intros.empty())
140140
return _localcc_update_fail();
141141
client_contact.regenerate(std::move(intros), std::forward<Opt>(args)...);

llarp/link/link_manager.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,8 @@ namespace llarp
11781178
_router.contact_db().put_cc(std::move(enc));
11791179

11801180
if (session->is_outbound())
1181-
session::OutboundSession::upcast(session)->update_remote_intros(std::move(*intro).take_intros());
1181+
session::OutboundClientSession::downcast(session)->update_remote_intros(
1182+
std::move(*intro).take_intros());
11821183

11831184
return m.respond(messages::OK_RESPONSE);
11841185
}

llarp/path/path_handler.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ namespace llarp::path
4949
: _running{true}, num_paths_desired{num_paths}, _router{_r}, num_hops{_n_hops}
5050
{}
5151

52+
void PathHandler::path_rotation_succeeded(std::shared_ptr<Path> new_path)
53+
{
54+
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
55+
path_build_succeeded(std::move(new_path));
56+
drop_oldest_path();
57+
}
58+
5259
static constexpr auto path_map_comp = [comp = PathExpComp{}](auto lhs, auto rhs) -> bool {
5360
// invert parameters passed so ranges::{min,max}_element use it like operator<
5461
return comp(rhs.second, lhs.second);
@@ -62,6 +69,14 @@ namespace llarp::path
6269
return std::ranges::min_element(_paths, path_map_comp)->second;
6370
}
6471

72+
std::shared_ptr<Path> PathHandler::get_newest_path()
73+
{
74+
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
75+
76+
Lock_t l{paths_mutex};
77+
return std::ranges::max_element(_paths, path_map_comp)->second;
78+
}
79+
6580
void PathHandler::print_all_paths() const
6681
{
6782
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
@@ -241,7 +256,7 @@ namespace llarp::path
241256
}
242257
}
243258

244-
intro_set PathHandler::get_current_client_intros() const
259+
intro_set PathHandler::get_local_client_intros() const
245260
{
246261
Lock_t lock{paths_mutex};
247262

@@ -844,21 +859,6 @@ namespace llarp::path
844859
}
845860
}
846861

847-
// void PathHandler::rotate_paths(
848-
// std::vector<RemoteRC> hops, std::function<void(std::shared_ptr<Path>)> success_cb, path_build_fail_hook
849-
// fail_cb)
850-
// {
851-
// log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
852-
853-
// if (auto new_path = build1(hops))
854-
// {
855-
// assert(new_path);
856-
857-
// log::debug(logcat, "Attempting path-rotation to new path...");
858-
// path_build_onepass(std::move(new_path), std::move(success_cb), std::move(fail_cb));
859-
// }
860-
// }
861-
862862
void PathHandler::rotate_paths(std::vector<RemoteRC> hops)
863863
{
864864
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);

llarp/path/path_handler.hpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace llarp
8181
static constexpr bool to_string_formattable = true;
8282
};
8383

84-
struct PathHandler
84+
struct PathHandler : public std::enable_shared_from_this<PathHandler>
8585
{
8686
friend struct Path;
8787

@@ -116,14 +116,12 @@ namespace llarp
116116

117117
virtual void path_build_succeeded(std::shared_ptr<Path> p);
118118

119-
// TESTNET: WIP NEW METHODS
120119
void path_build_recursive(
121120
intro_set intros,
122121
NetworkAddress remote,
123122
std::function<void(std::shared_ptr<Path>, ClientIntro)> cb,
124123
bool keep_path);
125124

126-
// TESTNET: new method, can be DRYed out
127125
void path_build_iterative(
128126
int n_tries,
129127
RemoteRC rc,
@@ -136,16 +134,14 @@ namespace llarp
136134

137135
virtual void rotate_paths() = 0;
138136

139-
// TESTNET: may be superfluous compared to the alternate method
140-
// void rotate_paths(std::vector<RemoteRC> hops, path_build_success_hook success, path_build_fail_hook
141-
// fail);
142-
143137
void rotate_paths(std::vector<RemoteRC> hops);
144138

145-
virtual void path_rotation_succeeded(std::shared_ptr<Path> new_path) = 0;
139+
virtual void path_rotation_succeeded(std::shared_ptr<Path> new_path);
146140

147141
std::shared_ptr<Path> get_oldest_path();
148142

143+
std::shared_ptr<Path> get_newest_path();
144+
149145
virtual void drop_oldest_path() = 0;
150146

151147
// TESTNET: DEBUG LOGGING METHOD
@@ -174,7 +170,7 @@ namespace llarp
174170

175171
std::optional<std::shared_ptr<Path>> get_path(HopID id) const;
176172

177-
intro_set get_current_client_intros() const;
173+
intro_set get_local_client_intros() const;
178174

179175
nlohmann::json ExtractStatus() const;
180176

llarp/rpc/rpc_server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ namespace llarp::rpc
444444
};
445445

446446
if (session->is_outbound())
447-
session::OutboundSession::upcast(session)->stop_session(true, std::move(hook));
447+
session::OutboundClientSession::downcast(session)->stop_session(true, std::move(hook));
448448
else
449449
session->stop_session(true, std::move(hook));
450450

llarp/session/map.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace llarp
5656
for (auto& [_, s] : _sessions)
5757
{
5858
if (s->is_outbound() && s->is_active())
59-
session::OutboundSession::upcast(s)->tick(now);
59+
session::OutboundClientSession::downcast(s)->tick(now);
6060
}
6161
}
6262

0 commit comments

Comments
 (0)