@@ -666,7 +666,7 @@ namespace llarp::handlers
666
666
NetworkAddress initiator,
667
667
HopID remote_pivot_txid,
668
668
std::shared_ptr<path::Path> path,
669
- shared_kx_data kx_data,
669
+ std::optional< shared_kx_data> kx_data,
670
670
bool use_tun)
671
671
{
672
672
auto tag = session_tag::make (protoflags);
@@ -781,8 +781,7 @@ namespace llarp::handlers
781
781
return ret;
782
782
}
783
783
784
- /* * Session Initiation Message Structure:
785
-
784
+ /* * Client Session Initiation Message Structure:
786
785
- 'k' : next HopID
787
786
- 'n' : symmetric nonce
788
787
- 'x' : encrypted payload
@@ -811,7 +810,7 @@ namespace llarp::handlers
811
810
- 'u' : Authentication field
812
811
- bt-encoded dict, values TBD
813
812
*/
814
- void SessionEndpoint::_make_session (
813
+ void SessionEndpoint::_make_client_session (
815
814
intro_set intros,
816
815
NetworkAddress remote,
817
816
ClientIntro remote_intro,
@@ -833,8 +832,7 @@ namespace llarp::handlers
833
832
834
833
log ::trace (logcat, " inner payload: {}" , buffer_printer{inner_payload});
835
834
836
- auto pivot_payload =
837
- ONION::serialize_hop (remote_intro.pivot_txid .to_view (), SymmNonce::make_random (), inner_payload);
835
+ auto pivot_payload = ONION::serialize_hop (pivot_txid.to_view (), SymmNonce::make_random (), inner_payload);
838
836
log ::trace (logcat, " pivot payload: {}" , buffer_printer{pivot_payload});
839
837
840
838
auto intermediate_payload = PATH::CONTROL::serialize (" path_control" , std::move (pivot_payload));
@@ -852,7 +850,7 @@ namespace llarp::handlers
852
850
session_keys = std::move (kx_data)](oxen::quic::message m) mutable {
853
851
if (m)
854
852
{
855
- log ::debug (logcat, " Call to InitiateSession succeeded!" );
853
+ log ::debug (logcat, " Call to initiate OutboundClientSession succeeded!" );
856
854
session_tag tag;
857
855
858
856
try
@@ -902,7 +900,102 @@ namespace llarp::handlers
902
900
logcat,
903
901
" Lokinet TUN failed to map route for session traffic to remote: {}" ,
904
902
session->remote ());
905
- // TESTNET: TODO: CLOSE THIS BISH HERE
903
+ // TESTNET: TODO: CLOSE THIS HERE
904
+ }
905
+ else
906
+ {
907
+ log ::info (logcat, " Starting TCP listener to route session traffic to backend..." );
908
+ session->tcp_backend_listen (std::move (hook));
909
+ }
910
+ }
911
+ else
912
+ {
913
+ std::optional<std::string> status = std::nullopt;
914
+ try
915
+ {
916
+ oxenc::bt_dict_consumer btdc{m.body ()};
917
+
918
+ if (auto s = btdc.maybe <std::string>(messages::STATUS_KEY))
919
+ status = s;
920
+ }
921
+ catch (const std::exception & e)
922
+ {
923
+ log ::warning (logcat, " Exception: {}" , e.what ());
924
+ }
925
+
926
+ log ::critical (
927
+ logcat,
928
+ " Call to initiate OutboundClientSession FAILED; reason: {}" ,
929
+ status.value_or (" <none given>" ));
930
+ }
931
+ });
932
+
933
+ log ::debug (logcat, " message sent..." );
934
+ }
935
+
936
+ void SessionEndpoint::_make_relay_session (
937
+ RemoteRC rc, NetworkAddress remote, std::shared_ptr<path::Path> path, on_session_init_hook cb)
938
+ {
939
+ std::string payload = InitiateSession::serialize (
940
+ _router.local_rid (),
941
+ path->pivot_txid (),
942
+ path->pivot_txid (),
943
+ fetch_auth_token (remote),
944
+ _router.using_tun_if ());
945
+
946
+ log ::trace (logcat, " payload: {}" , buffer_printer{payload});
947
+
948
+ path->send_path_control_message (
949
+ " session_init" ,
950
+ std::move (payload),
951
+ [this , rc = std::move (rc), remote, path, hook = std::move (cb)](oxen::quic::message m) mutable {
952
+ if (m)
953
+ {
954
+ log ::debug (logcat, " Call to initiate OutboundRelaySession succeeded!" );
955
+ session_tag tag;
956
+
957
+ try
958
+ {
959
+ tag = InitiateSession::deserialize_response (oxenc::bt_dict_consumer{m.body ()});
960
+ }
961
+ catch (const std::exception & e)
962
+ {
963
+ // TESTNET: TODO: close session here?
964
+ log ::warning (logcat, " Exception: {}" , e.what ());
965
+ return ;
966
+ }
967
+
968
+ log ::trace (logcat, " Remote relay has provided session tag: {}" , tag);
969
+
970
+ auto pivot_txid = path->pivot_txid ();
971
+
972
+ auto outbound = std::make_shared<session::OutboundRelaySession>(
973
+ remote, *this , std::move (path), std::move (tag), std::move (pivot_txid));
974
+
975
+ auto [session, _] = _sessions.insert_or_assign (std::move (remote), std::move (outbound));
976
+ session->activate ();
977
+
978
+ log ::trace (logcat, " Outbound session to {} successfully created..." , session->remote ());
979
+
980
+ if (session->using_tun ())
981
+ {
982
+ log ::trace (logcat, " Instructing lokinet TUN device to create mapped route..." );
983
+ if (auto maybe_ip = _router.tun_endpoint ()->map_session_to_local_ip (session->remote ()))
984
+ {
985
+ log ::info (
986
+ logcat,
987
+ " TUN device successfully routing session (remote: {}) via local ip: {}" ,
988
+ session->remote (),
989
+ std::holds_alternative<ipv4>(*maybe_ip) ? std::get<ipv4>(*maybe_ip).to_string ()
990
+ : std::get<ipv6>(*maybe_ip).to_string ());
991
+
992
+ return hook (*maybe_ip);
993
+ }
994
+
995
+ log ::critical (
996
+ logcat,
997
+ " Lokinet TUN failed to map route for session traffic to remote: {}" ,
998
+ session->remote ());
906
999
}
907
1000
else
908
1001
{
@@ -926,30 +1019,32 @@ namespace llarp::handlers
926
1019
}
927
1020
928
1021
log ::critical (
929
- logcat, " Call to InitiateSession FAILED; reason: {}" , status.value_or (" <none given>" ));
1022
+ logcat,
1023
+ " Call to initiate OutboundRelaySession FAILED; reason: {}" ,
1024
+ status.value_or (" <none given>" ));
930
1025
}
931
1026
});
932
1027
933
1028
log ::debug (logcat, " message sent..." );
934
1029
}
935
1030
936
- void SessionEndpoint::_make_session_path (RemoteRC rc, NetworkAddress remote, on_session_init_hook cb)
1031
+ void SessionEndpoint::_make_relay_session_path (RemoteRC rc, NetworkAddress remote, on_session_init_hook cb)
937
1032
{
938
1033
log ::debug (logcat, " {} called" , __PRETTY_FUNCTION__);
939
1034
940
1035
path_build_iterative (
941
1036
SESSION_PATH_BUILD_ATTEMPTS,
942
1037
rc,
943
1038
remote,
944
- [this , rc, remote, cb](std::shared_ptr<path::Path> p ) {
1039
+ [this , rc, remote, cb](std::shared_ptr<path::Path> new_path ) {
945
1040
log ::info (logcat, " Path build to remote:{} succeeded, initiating session!" , remote);
946
1041
(void )this ;
947
- (void )p ;
1042
+ (void )new_path ;
948
1043
},
949
1044
false );
950
1045
}
951
1046
952
- void SessionEndpoint::_make_session_path (intro_set intros, NetworkAddress remote, on_session_init_hook cb)
1047
+ void SessionEndpoint::_make_client_session_path (intro_set intros, NetworkAddress remote, on_session_init_hook cb)
953
1048
{
954
1049
log ::debug (logcat, " {} called" , __PRETTY_FUNCTION__);
955
1050
@@ -958,13 +1053,21 @@ namespace llarp::handlers
958
1053
remote,
959
1054
[this , intros, remote, cb](std::shared_ptr<path::Path> new_path, ClientIntro remote_intro) mutable {
960
1055
log ::info (logcat, " Path build to remote:{} succeeded, initiating session!" , remote);
961
- return _make_session (
1056
+ return _make_client_session (
962
1057
std::move (intros), std::move (remote), std::move (remote_intro), std::move (new_path), std::move (cb));
963
1058
},
964
1059
false );
965
1060
}
966
1061
967
- bool SessionEndpoint::_initiate_client_session (NetworkAddress remote, on_session_init_hook cb)
1062
+ void SessionEndpoint::initiate_remote_session (const NetworkAddress& remote, on_session_init_hook cb)
1063
+ {
1064
+ if (remote.is_client ())
1065
+ _initiate_client_session (remote, std::move (cb));
1066
+ else
1067
+ _initiate_relay_session (remote, std::move (cb));
1068
+ }
1069
+
1070
+ void SessionEndpoint::_initiate_client_session (NetworkAddress remote, on_session_init_hook cb)
968
1071
{
969
1072
auto counter = std::make_shared<size_t >(num_paths_desired);
970
1073
@@ -979,20 +1082,15 @@ namespace llarp::handlers
979
1082
{
980
1083
*counter = 0 ;
981
1084
log ::debug (logcat, " Session initiation returned client contact: {}" , cc->to_string ());
982
- _make_session_path (std::move (*cc).take_intros (), remote, std::move (hook));
1085
+ _make_client_session_path (std::move (*cc).take_intros (), remote, std::move (hook));
983
1086
}
984
1087
else if (--*counter == 0 )
985
- log ::warning (
986
- logcat,
987
- " Failed to initiate session at 'find_cc' (target:{})" ,
988
- remote.router_id ().short_string ());
1088
+ log ::warning (logcat, " Failed to initiate session at 'find_cc' (target:{})" , remote);
989
1089
});
990
1090
});
991
-
992
- return true ;
993
1091
}
994
1092
995
- bool SessionEndpoint::_initiate_relay_session (NetworkAddress remote, on_session_init_hook cb)
1093
+ void SessionEndpoint::_initiate_relay_session (NetworkAddress remote, on_session_init_hook cb)
996
1094
{
997
1095
auto counter = std::make_shared<size_t >(num_paths_desired);
998
1096
@@ -1007,39 +1105,12 @@ namespace llarp::handlers
1007
1105
{
1008
1106
*counter = 0 ;
1009
1107
log ::debug (logcat, " Session initiation returned RC: {}" , rc->to_string ());
1010
- ( void ) this ;
1108
+ _make_relay_session_path ( std::move (*rc), remote, std::move (hook)) ;
1011
1109
}
1012
1110
else if (--*counter == 0 )
1013
1111
log ::warning (logcat, " Failed to initiate session at `fetch_rcs` (target:{})" , remote);
1014
1112
});
1015
1113
});
1016
-
1017
- return true ;
1018
- }
1019
-
1020
- bool SessionEndpoint::_initiate_session (NetworkAddress remote, on_session_init_hook cb)
1021
- {
1022
- auto counter = std::make_shared<size_t >(num_paths_desired);
1023
-
1024
- _router.loop ()->call ([this , remote, handler = std::move (cb), counter]() mutable {
1025
- lookup_client_intro (
1026
- remote.router_id (),
1027
- [this , remote, hook = std::move (handler), counter](std::optional<ClientContact> cc) mutable {
1028
- if (*counter == 0 )
1029
- return ;
1030
-
1031
- if (cc)
1032
- {
1033
- *counter = 0 ;
1034
- log ::debug (logcat, " Session initiation returned client contact: {}" , cc->to_string ());
1035
- _make_session_path (std::move (*cc).take_intros (), remote, std::move (hook));
1036
- }
1037
- else if (--*counter == 0 )
1038
- log ::warning (logcat, " Failed to initiate session at 'find_cc' (target:{})" , remote);
1039
- });
1040
- });
1041
-
1042
- return true ;
1043
1114
}
1044
1115
1045
1116
void SessionEndpoint::map_remote_to_local_addr (NetworkAddress remote, oxen::quic::Address local)
0 commit comments