@@ -44,24 +44,43 @@ namespace llarp::handlers
44
44
s->stop_session (send_close);
45
45
}
46
46
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)
48
49
{
49
50
log ::trace (logcat, " {} called" , __PRETTY_FUNCTION__);
50
51
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))
52
70
{
53
71
// only OutboundSessions send path switch messages
54
72
assert (s && !s->is_outbound ());
55
73
74
+ // PathHandler objects key their paths to the upstream rxid, so we use the conditional get_path
56
75
if (auto path = get_path_conditional (
57
76
[local_pivot_txid](std::shared_ptr<path::Path> p) { return p->pivot_txid () == local_pivot_txid; }))
58
77
{
59
78
log ::debug (
60
79
logcat,
61
80
" 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));
65
84
return true ;
66
85
}
67
86
@@ -117,13 +136,19 @@ namespace llarp::handlers
117
136
118
137
if (_cc_publisher)
119
138
{
120
- _cc_publisher->stop ();
139
+ if (_cc_publisher->is_running ())
140
+ _cc_publisher->stop ();
141
+
142
+ _cc_publisher.reset ();
121
143
log ::trace (logcat, " ClientContact publish ticker stopped!" );
122
144
}
123
145
124
146
if (_path_rotater)
125
147
{
126
- _path_rotater->stop ();
148
+ if (_path_rotater->is_running ())
149
+ _path_rotater->stop ();
150
+
151
+ _path_rotater.reset ();
127
152
log ::trace (logcat, " Path rotation ticker stopped!" );
128
153
}
129
154
@@ -218,8 +243,7 @@ namespace llarp::handlers
218
243
Lock_t l{paths_mutex};
219
244
220
245
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 ());
223
247
drop_path (oldest);
224
248
}
225
249
@@ -238,17 +262,12 @@ namespace llarp::handlers
238
262
}
239
263
240
264
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); });
246
265
}
247
266
248
267
void SessionEndpoint::path_rotation_succeeded (std::shared_ptr<path::Path> new_path)
249
268
{
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());
252
271
path_build_succeeded (std::move (new_path));
253
272
drop_oldest_path ();
254
273
update_and_publish_localcc ();
@@ -298,7 +317,7 @@ namespace llarp::handlers
298
317
299
318
void SessionEndpoint::start_tickers ()
300
319
{
301
- if (should_publish_cc)
320
+ if (!_is_service_node and should_publish_cc)
302
321
{
303
322
log ::trace (logcat, " Starting ClientContact publish ticker..." );
304
323
@@ -662,50 +681,70 @@ namespace llarp::handlers
662
681
663
682
static constexpr auto success_msg = " SessionEndpoint successfully created and mapped InboundSession object!" sv;
664
683
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)
671
685
{
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__);
679
687
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 ())
683
689
{
684
690
log ::trace (logcat, " {} Instructing lokinet TUN device to create mapped route..." , success_msg);
685
691
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 ()))
687
693
{
688
694
log ::info (
689
695
logcat,
690
696
" TUN device successfully routing session (remote: {}) via local ip: {}" ,
691
- session ->remote (),
697
+ s ->remote (),
692
698
std::holds_alternative<ipv4>(*maybe_ip) ? std::get<ipv4>(*maybe_ip).to_string ()
693
699
: std::get<ipv6>(*maybe_ip).to_string ());
700
+ return maybe_ip;
694
701
}
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 ;
701
733
}
702
734
else
703
735
{
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 ;
706
740
}
707
741
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;
709
748
}
710
749
711
750
static void publish_cc_cb (oxen::quic::message m)
@@ -740,16 +779,12 @@ namespace llarp::handlers
740
779
log ::trace (logcat, " Publishing new EncryptedClientContact: {}" , ecc.bt_payload ());
741
780
742
781
_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());
748
782
log ::debug (
749
783
logcat,
750
- " Publishing ClientContact on {}bound session: {} " ,
784
+ " Publishing ClientContact on {}bound session (remote:{}) " ,
751
785
detail::bool_alpha (s->is_outbound (), " Out" , " In" ),
752
- s->current_path ()->debug_string ());
786
+ s->remote ());
787
+
753
788
s->publish_client_contact (ecc, publish_cc_cb);
754
789
});
755
790
@@ -762,8 +797,7 @@ namespace llarp::handlers
762
797
if (not p or not p->is_active ())
763
798
continue ;
764
799
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 ());
767
801
ret &= p->publish_client_contact (ecc, publish_cc_cb);
768
802
}
769
803
}
@@ -809,7 +843,8 @@ namespace llarp::handlers
809
843
- 't' : Use Tun interface (bool)
810
844
- 'u' : Authentication field
811
845
- bt-encoded dict, values TBD
812
- */
846
+ */
847
+ // TESTNET: TODO: DRY out the following two functions
813
848
void SessionEndpoint::_make_client_session (
814
849
intro_set intros,
815
850
NetworkAddress remote,
@@ -819,7 +854,8 @@ namespace llarp::handlers
819
854
{
820
855
std::string inner_payload;
821
856
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));
823
859
824
860
// internal payload for remote client
825
861
std::tie (inner_payload, kx_data) = InitiateSession::serialize_encrypt (
@@ -829,14 +865,9 @@ namespace llarp::handlers
829
865
pivot_txid,
830
866
fetch_auth_token (remote),
831
867
_router.using_tun_if ());
832
-
833
868
log ::trace (logcat, " inner payload: {}" , buffer_printer{inner_payload});
834
869
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);
840
871
841
872
path->send_path_control_message (
842
873
" path_control" ,
@@ -866,7 +897,7 @@ namespace llarp::handlers
866
897
867
898
log ::trace (logcat, " Remote client has provided session tag: {}" , tag);
868
899
869
- auto outbound = std::make_shared<session::OutboundClientSession>(
900
+ auto session = std::make_shared<session::OutboundClientSession>(
870
901
remote,
871
902
*this ,
872
903
std::move (path),
@@ -875,12 +906,12 @@ namespace llarp::handlers
875
906
std::move (remote_intros),
876
907
std::move (session_keys));
877
908
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 () );
880
911
881
912
log ::trace (logcat, " Outbound session to {} successfully created..." , session->remote ());
882
913
883
- // TESTNET:
914
+ // TESTNET: use new ::map_session(...) function after finishing embedded hooks
884
915
if (session->using_tun ())
885
916
{
886
917
log ::trace (logcat, " Instructing lokinet TUN device to create mapped route..." );
@@ -948,7 +979,8 @@ namespace llarp::handlers
948
979
path->send_path_control_message (
949
980
" session_init" ,
950
981
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 {
952
984
if (m)
953
985
{
954
986
log ::debug (logcat, " Call to initiate OutboundRelaySession succeeded!" );
@@ -969,11 +1001,11 @@ namespace llarp::handlers
969
1001
970
1002
auto pivot_txid = path->pivot_txid ();
971
1003
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) );
974
1006
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 () );
977
1009
978
1010
log ::trace (logcat, " Outbound session to {} successfully created..." , session->remote ());
979
1011
@@ -1032,14 +1064,13 @@ namespace llarp::handlers
1032
1064
{
1033
1065
log ::debug (logcat, " {} called" , __PRETTY_FUNCTION__);
1034
1066
1035
- path_build_iterative (
1067
+ path_build_recursive (
1036
1068
SESSION_PATH_BUILD_ATTEMPTS,
1037
1069
rc,
1038
1070
remote,
1039
1071
[this , rc, remote, cb](std::shared_ptr<path::Path> new_path) {
1040
1072
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));
1043
1074
},
1044
1075
false );
1045
1076
}
0 commit comments