Skip to content

Commit 44b8416

Browse files
committed
path <> transithop ambiguity resolved
- unified under session_path_interface pure virtual class
1 parent b3345be commit 44b8416

20 files changed

+541
-275
lines changed

llarp/handlers/session.cpp

+104-73
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,43 @@ namespace llarp::handlers
4444
s->stop_session(send_close);
4545
}
4646

47-
bool SessionEndpoint::recv_path_switch(session_tag t, HopID remove_pivot_txid, HopID local_pivot_txid)
47+
bool SessionEndpoint::recv_path_switch(
48+
session_tag t, HopID remote_pivot_txid, std::shared_ptr<session_path_interface> new_path)
4849
{
4950
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
5051

51-
if (auto s = get_session<session::InboundSession>(t))
52+
if (auto s = get_session<session::InboundClientSession>(t))
53+
{
54+
log::debug(
55+
logcat,
56+
"Successfully matched path-switch request to InboundSession over path:{}",
57+
new_path->to_string());
58+
59+
s->recv_path_switch(std::move(remote_pivot_txid), std::move(new_path));
60+
}
61+
62+
return false;
63+
}
64+
65+
bool SessionEndpoint::recv_path_switch(session_tag t, HopID remote_pivot_txid, HopID local_pivot_txid)
66+
{
67+
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
68+
69+
if (auto s = get_session<session::InboundClientSession>(t))
5270
{
5371
// only OutboundSessions send path switch messages
5472
assert(s && !s->is_outbound());
5573

74+
// PathHandler objects key their paths to the upstream rxid, so we use the conditional get_path
5675
if (auto path = get_path_conditional(
5776
[local_pivot_txid](std::shared_ptr<path::Path> p) { return p->pivot_txid() == local_pivot_txid; }))
5877
{
5978
log::debug(
6079
logcat,
6180
"Successfully matched path-switch request to InboundSession over path:{}",
62-
path->get()->debug_string());
63-
s->set_remote_pivot_tx(remove_pivot_txid);
64-
s->set_new_current_path(std::move(*path));
81+
(*path)->to_string());
82+
s->set_remote_pivot_tx(remote_pivot_txid);
83+
s->set_new_current_path_interface(std::move(*path));
6584
return true;
6685
}
6786

@@ -117,13 +136,19 @@ namespace llarp::handlers
117136

118137
if (_cc_publisher)
119138
{
120-
_cc_publisher->stop();
139+
if (_cc_publisher->is_running())
140+
_cc_publisher->stop();
141+
142+
_cc_publisher.reset();
121143
log::trace(logcat, "ClientContact publish ticker stopped!");
122144
}
123145

124146
if (_path_rotater)
125147
{
126-
_path_rotater->stop();
148+
if (_path_rotater->is_running())
149+
_path_rotater->stop();
150+
151+
_path_rotater.reset();
127152
log::trace(logcat, "Path rotation ticker stopped!");
128153
}
129154

@@ -218,8 +243,7 @@ namespace llarp::handlers
218243
Lock_t l{paths_mutex};
219244

220245
auto oldest = get_oldest_path();
221-
// log::debug(logcat, "Dropping oldest path: {}", oldest->to_string());
222-
log::debug(logcat, "Dropping oldest path: {}", oldest->debug_string());
246+
log::debug(logcat, "Dropping oldest path: {}", oldest->to_string());
223247
drop_path(oldest);
224248
}
225249

@@ -238,17 +262,12 @@ namespace llarp::handlers
238262
}
239263

240264
path::PathHandler::rotate_paths(std::move(*maybe_hops));
241-
242-
// path::PathHandler::rotate_paths(
243-
// std::move(*maybe_hops),
244-
// [this](auto new_path) mutable { path_rotation_succeeded(std::move(new_path)); },
245-
// [this](auto new_path, int ec) mutable { path_build_failed(std::move(new_path), ec); });
246265
}
247266

248267
void SessionEndpoint::path_rotation_succeeded(std::shared_ptr<path::Path> new_path)
249268
{
250-
// log::info(logcat, "SessionEndpoint successfully rotated in new path: {}", new_path->to_string());
251-
log::info(logcat, "SessionEndpoint successfully rotated in new path: {}", new_path->debug_string());
269+
log::info(logcat, "SessionEndpoint successfully rotated in new path: {}", new_path->to_string());
270+
// log::info(logcat, "SessionEndpoint successfully rotated in new path: {}", new_path->debug_string());
252271
path_build_succeeded(std::move(new_path));
253272
drop_oldest_path();
254273
update_and_publish_localcc();
@@ -298,7 +317,7 @@ namespace llarp::handlers
298317

299318
void SessionEndpoint::start_tickers()
300319
{
301-
if (should_publish_cc)
320+
if (!_is_service_node and should_publish_cc)
302321
{
303322
log::trace(logcat, "Starting ClientContact publish ticker...");
304323

@@ -662,50 +681,70 @@ namespace llarp::handlers
662681

663682
static constexpr auto success_msg = "SessionEndpoint successfully created and mapped InboundSession object!"sv;
664683

665-
std::optional<session_tag> SessionEndpoint::prefigure_session(
666-
NetworkAddress initiator,
667-
HopID remote_pivot_txid,
668-
std::shared_ptr<path::Path> path,
669-
std::optional<shared_kx_data> kx_data,
670-
bool use_tun)
684+
std::optional<ip_v> SessionEndpoint::map_session(std::shared_ptr<session::BaseSession>& s)
671685
{
672-
auto tag = session_tag::make(protoflags);
673-
674-
auto inbound = std::make_shared<session::InboundSession>(
675-
initiator, std::move(path), *this, std::move(remote_pivot_txid), tag, use_tun, std::move(kx_data));
676-
677-
auto [session, _] = _sessions.insert_or_assign(std::move(initiator), std::move(inbound));
678-
session->activate();
686+
log::trace(logcat, "{} called", __PRETTY_FUNCTION__);
679687

680-
// TESTNET:
681-
// instruct the lokinet TUN device to create a mapping from a local IP to this session
682-
if (session->using_tun())
688+
if (s->using_tun())
683689
{
684690
log::trace(logcat, "{} Instructing lokinet TUN device to create mapped route...", success_msg);
685691

686-
if (auto maybe_ip = _router.tun_endpoint()->map_session_to_local_ip(session->remote()))
692+
if (auto maybe_ip = _router.tun_endpoint()->map_session_to_local_ip(s->remote()))
687693
{
688694
log::info(
689695
logcat,
690696
"TUN device successfully routing session (remote: {}) via local ip: {}",
691-
session->remote(),
697+
s->remote(),
692698
std::holds_alternative<ipv4>(*maybe_ip) ? std::get<ipv4>(*maybe_ip).to_string()
693699
: std::get<ipv6>(*maybe_ip).to_string());
700+
return maybe_ip;
694701
}
695-
else
696-
{
697-
// TODO: if this fails, we should close the session
698-
log::warning(logcat, "TUN device failed to route session (remote: {}) to local ip", session->remote());
699-
return std::nullopt;
700-
}
702+
703+
// TODO: if this fails, we should close the session
704+
log::warning(logcat, "TUN device failed to route session (remote: {}) to local ip", s->remote());
705+
return std::nullopt;
706+
}
707+
708+
// TESTNET:
709+
log::warning(logcat, "INCOMPLETE EMBEDDED ROUTE");
710+
// log::info(logcat, "{} Connecting to TCP backend to route session traffic...", success_msg);
711+
// session->tcp_backend_connect();
712+
713+
return std::nullopt;
714+
}
715+
716+
std::optional<session_tag> SessionEndpoint::prefigure_session(
717+
NetworkAddress initiator,
718+
HopID remote_pivot_txid,
719+
std::shared_ptr<session_path_interface> path,
720+
std::optional<shared_kx_data> kx_data,
721+
bool use_tun)
722+
{
723+
auto tag = session_tag::make(protoflags);
724+
725+
std::shared_ptr<session::BaseSession> s = nullptr;
726+
727+
if (_is_service_node)
728+
{
729+
auto session = std::make_shared<session::InboundRelaySession>(
730+
initiator, std::move(path), *this, std::move(remote_pivot_txid), tag, use_tun, std::move(kx_data));
731+
732+
s = _sessions.insert_or_assign(std::move(initiator), std::move(session)).first;
701733
}
702734
else
703735
{
704-
log::info(logcat, "{} Connecting to TCP backend to route session traffic...", success_msg);
705-
// session->tcp_backend_connect();
736+
auto session = std::make_shared<session::InboundClientSession>(
737+
initiator, std::move(path), *this, std::move(remote_pivot_txid), tag, use_tun, std::move(kx_data));
738+
739+
s = _sessions.insert_or_assign(std::move(initiator), std::move(session)).first;
706740
}
707741

708-
return tag;
742+
assert(s and s->is_active());
743+
744+
if (auto maybe_ip = map_session(s))
745+
return tag;
746+
747+
return std::nullopt;
709748
}
710749

711750
static void publish_cc_cb(oxen::quic::message m)
@@ -740,16 +779,12 @@ namespace llarp::handlers
740779
log::trace(logcat, "Publishing new EncryptedClientContact: {}", ecc.bt_payload());
741780

742781
_sessions.for_each([ecc](std::shared_ptr<session::BaseSession>& s) mutable {
743-
// log::debug(
744-
// logcat,
745-
// "Publishing ClientContact on {}bound session (remote:{})",
746-
// detail::bool_alpha(s->is_outbound(), "Out", "In"),
747-
// s->remote());
748782
log::debug(
749783
logcat,
750-
"Publishing ClientContact on {}bound session: {}",
784+
"Publishing ClientContact on {}bound session (remote:{})",
751785
detail::bool_alpha(s->is_outbound(), "Out", "In"),
752-
s->current_path()->debug_string());
786+
s->remote());
787+
753788
s->publish_client_contact(ecc, publish_cc_cb);
754789
});
755790

@@ -762,8 +797,7 @@ namespace llarp::handlers
762797
if (not p or not p->is_active())
763798
continue;
764799

765-
// log::debug(logcat, "Publishing ClientContact on {}", path->hop_string());
766-
log::debug(logcat, "Publishing ClientContact on {}", p->debug_string());
800+
log::debug(logcat, "Publishing ClientContact on {}", p->to_string());
767801
ret &= p->publish_client_contact(ecc, publish_cc_cb);
768802
}
769803
}
@@ -809,7 +843,8 @@ namespace llarp::handlers
809843
- 't' : Use Tun interface (bool)
810844
- 'u' : Authentication field
811845
- bt-encoded dict, values TBD
812-
*/
846+
*/
847+
// TESTNET: TODO: DRY out the following two functions
813848
void SessionEndpoint::_make_client_session(
814849
intro_set intros,
815850
NetworkAddress remote,
@@ -819,7 +854,8 @@ namespace llarp::handlers
819854
{
820855
std::string inner_payload;
821856
shared_kx_data kx_data;
822-
auto pivot_txid = intros.emplace(std::move(remote_intro)).first->pivot_txid;
857+
auto pivot_txid = remote_intro.pivot_txid;
858+
intros.emplace(std::move(remote_intro));
823859

824860
// internal payload for remote client
825861
std::tie(inner_payload, kx_data) = InitiateSession::serialize_encrypt(
@@ -829,14 +865,9 @@ namespace llarp::handlers
829865
pivot_txid,
830866
fetch_auth_token(remote),
831867
_router.using_tun_if());
832-
833868
log::trace(logcat, "inner payload: {}", buffer_printer{inner_payload});
834869

835-
auto pivot_payload = ONION::serialize_hop(pivot_txid.to_view(), SymmNonce::make_random(), inner_payload);
836-
log::trace(logcat, "pivot payload: {}", buffer_printer{pivot_payload});
837-
838-
auto intermediate_payload = PATH::CONTROL::serialize("path_control", std::move(pivot_payload));
839-
log::trace(logcat, "intermediate payload: {}", buffer_printer{intermediate_payload});
870+
auto intermediate_payload = PATH::CONTROL::serialize_aligned(std::move(inner_payload), pivot_txid);
840871

841872
path->send_path_control_message(
842873
"path_control",
@@ -866,7 +897,7 @@ namespace llarp::handlers
866897

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

869-
auto outbound = std::make_shared<session::OutboundClientSession>(
900+
auto session = std::make_shared<session::OutboundClientSession>(
870901
remote,
871902
*this,
872903
std::move(path),
@@ -875,12 +906,12 @@ namespace llarp::handlers
875906
std::move(remote_intros),
876907
std::move(session_keys));
877908

878-
auto [session, _] = _sessions.insert_or_assign(std::move(remote), std::move(outbound));
879-
session->activate();
909+
auto [s, _] = _sessions.insert_or_assign(std::move(remote), session);
910+
assert(s->is_active());
880911

881912
log::trace(logcat, "Outbound session to {} successfully created...", session->remote());
882913

883-
// TESTNET:
914+
// TESTNET: use new ::map_session(...) function after finishing embedded hooks
884915
if (session->using_tun())
885916
{
886917
log::trace(logcat, "Instructing lokinet TUN device to create mapped route...");
@@ -948,7 +979,8 @@ namespace llarp::handlers
948979
path->send_path_control_message(
949980
"session_init",
950981
std::move(payload),
951-
[this, rc = std::move(rc), remote, path, hook = std::move(cb)](oxen::quic::message m) mutable {
982+
[this, rc = std::move(rc), remote, path, hook = std::move(cb), session_keys = path->hops.back().kx](
983+
oxen::quic::message m) mutable {
952984
if (m)
953985
{
954986
log::debug(logcat, "Call to initiate OutboundRelaySession succeeded!");
@@ -969,11 +1001,11 @@ namespace llarp::handlers
9691001

9701002
auto pivot_txid = path->pivot_txid();
9711003

972-
auto outbound = std::make_shared<session::OutboundRelaySession>(
973-
remote, *this, std::move(path), std::move(tag), std::move(pivot_txid));
1004+
auto session = std::make_shared<session::OutboundRelaySession>(
1005+
remote, *this, std::move(path), std::move(tag), std::move(pivot_txid), std::move(session_keys));
9741006

975-
auto [session, _] = _sessions.insert_or_assign(std::move(remote), std::move(outbound));
976-
session->activate();
1007+
auto [s, _] = _sessions.insert_or_assign(std::move(remote), session);
1008+
assert(s->is_active());
9771009

9781010
log::trace(logcat, "Outbound session to {} successfully created...", session->remote());
9791011

@@ -1032,14 +1064,13 @@ namespace llarp::handlers
10321064
{
10331065
log::debug(logcat, "{} called", __PRETTY_FUNCTION__);
10341066

1035-
path_build_iterative(
1067+
path_build_recursive(
10361068
SESSION_PATH_BUILD_ATTEMPTS,
10371069
rc,
10381070
remote,
10391071
[this, rc, remote, cb](std::shared_ptr<path::Path> new_path) {
10401072
log::info(logcat, "Path build to remote:{} succeeded, initiating session!", remote);
1041-
(void)this;
1042-
(void)new_path;
1073+
_make_relay_session(std::move(rc), std::move(remote), std::move(new_path), std::move(cb));
10431074
},
10441075
false);
10451076
}

llarp/handlers/session.hpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ namespace llarp
110110
// get copy of all srv records
111111
std::set<dns::SRVData> srv_records() const { return {_srv_records.begin(), _srv_records.end()}; }
112112

113-
bool recv_path_switch(session_tag t, HopID remove_pivot, HopID local_pivot);
113+
bool recv_path_switch(
114+
session_tag t, HopID remote_pivot_txid, std::shared_ptr<session_path_interface> new_path);
115+
116+
bool recv_path_switch(session_tag t, HopID remote_pivot_txid, HopID local_pivot_txid);
114117

115118
template <concepts::SessionType session_t = session::BaseSession>
116119
std::shared_ptr<session_t> get_session(const session_tag& tag) const
@@ -152,11 +155,12 @@ namespace llarp
152155
// to initiate a session
153156
bool validate(const NetworkAddress& remote, std::optional<std::string> maybe_auth = std::nullopt);
154157

158+
std::optional<ip_v> map_session(std::shared_ptr<session::BaseSession>& s);
159+
155160
std::optional<session_tag> prefigure_session(
156161
NetworkAddress initiator,
157-
// session_tag tag,
158162
HopID remote_pivot_txid,
159-
std::shared_ptr<path::Path> path,
163+
std::shared_ptr<session_path_interface> path,
160164
std::optional<shared_kx_data> kx_data,
161165
bool use_tun);
162166

0 commit comments

Comments
 (0)