Skip to content

Commit

Permalink
More message handling underway
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Oct 16, 2023
1 parent 6b1e3fb commit 99a1036
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 710 deletions.
1 change: 0 additions & 1 deletion llarp/exit/exit_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <llarp/crypto/types.hpp>
#include "policy.hpp"
#include <llarp/routing/message.hpp>

#include <vector>

Expand Down
34 changes: 12 additions & 22 deletions llarp/exit/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,16 @@ namespace llarp::exit
BaseSession::HandlePathBuilt(llarp::path::Path_ptr p)
{
path::Builder::HandlePathBuilt(p);
p->SetDropHandler(util::memFn(&BaseSession::HandleTrafficDrop, this));
p->SetDeadChecker(util::memFn(&BaseSession::CheckPathDead, this));
p->SetExitTrafficHandler(util::memFn(&BaseSession::HandleTraffic, this));
p->AddObtainExitHandler(util::memFn(&BaseSession::HandleGotExit, this));

routing::ObtainExitMessage obtain;
obtain.sequence_number = p->NextSeqNo();
obtain.tx_id = llarp::randint();
PopulateRequest(obtain);
if (!obtain.Sign(exit_key))
{
llarp::LogError("Failed to sign exit request");
return;
}
if (p->SendExitRequest(obtain, router))
llarp::LogInfo("asking ", exit_router, " for exit");
// p->SetDropHandler(util::memFn(&BaseSession::HandleTrafficDrop, this));
// p->SetDeadChecker(util::memFn(&BaseSession::CheckPathDead, this));
// p->SetExitTrafficHandler(util::memFn(&BaseSession::HandleTraffic, this));
// p->AddObtainExitHandler(util::memFn(&BaseSession::HandleGotExit, this));

if (p->obtain_exit(
exit_key, std::is_same_v<decltype(p), ExitSession> ? 1 : 0, p->TXID().bt_encode()))
log::info(link_cat, "Asking {} for exit", exit_router);
else
llarp::LogError("failed to send exit request");
log::warning(link_cat, "Failed to send exit request");
}

void
Expand Down Expand Up @@ -159,16 +151,14 @@ namespace llarp::exit
const static auto roles = llarp::path::ePathRoleExit | llarp::path::ePathRoleSVC;
if (p->SupportsAnyRoles(roles))
{
llarp::LogInfo(p->name(), " closing exit path");
routing::CloseExitMessage msg;
if (msg.Sign(exit_key) && p->SendExitClose(msg, router))
{
log::info(link_cat, "{} closing exit path", p->name());
if (p->close_exit(exit_key, p->TXID().bt_encode()))
p->ClearRoles(roles);
}
else
llarp::LogWarn(p->name(), " failed to send exit close message");
}
};

ForEachPath(sendExitClose);
path::Builder::ResetInternalState();
}
Expand Down
2 changes: 0 additions & 2 deletions llarp/exit/session.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include "exit_messages.hpp"
#include <llarp/service/protocol_type.hpp>
#include <llarp/net/ip_packet.hpp>
#include <llarp/path/pathbuilder.hpp>
#include <llarp/routing/transfer_traffic_message.hpp>
#include <llarp/constants/path.hpp>

#include <deque>
Expand Down
8 changes: 4 additions & 4 deletions llarp/handlers/tun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ namespace llarp::handlers

class TunDNS : public dns::Server
{
TunEndpoint* const m_Endpoint;
std::optional<SockAddr> m_QueryBind;
net::ipaddr_t m_OurIP;
TunEndpoint* const m_Endpoint;

public:
std::shared_ptr<dns::PacketSource_Base> PacketSource;
Expand All @@ -124,9 +124,9 @@ namespace llarp::handlers

explicit TunDNS(TunEndpoint* ep, const llarp::DnsConfig& conf)
: dns::Server{ep->router()->loop(), conf, 0}
, m_Endpoint{ep}
, m_QueryBind{conf.m_QueryBind}
, m_OurIP{ToNet(ep->GetIfAddr())}
, m_Endpoint{ep}
{}

std::shared_ptr<dns::PacketSource_Base>
Expand Down Expand Up @@ -278,12 +278,12 @@ namespace llarp::handlers
if (conf.m_reachable)
{
_publish_introset = true;
LogInfo(Name(), " setting to be reachable by default");
log::info(link_cat, "TunEndpoint setting to be reachable by default");
}
else
{
_publish_introset = false;
LogInfo(Name(), " setting to be not reachable by default");
log::info(link_cat, "TunEndpoint setting to be not reachable by default");
}

if (conf.m_AuthType == service::AuthType::eAuthTypeFile)
Expand Down
10 changes: 2 additions & 8 deletions llarp/handlers/tun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace llarp::handlers
ExtractStatus() const override;

std::unordered_map<std::string, std::string>
NotifyParams() const override;
NotifyParams() const;

bool
SupportsV6() const override;
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace llarp::handlers
}

std::set<IPRange>
GetOwnedRanges() const override
GetOwnedRanges() const
{
return m_OwnedRanges;
}
Expand Down Expand Up @@ -213,12 +213,6 @@ namespace llarp::handlers
}
};

/// queue for sending packets to user from network
util::ascending_priority_queue<WritePacket> m_NetworkToUserPktQueue;

void
Pump(llarp_time_t now) override;

/// return true if we have a remote loki address for this ip address
bool
HasRemoteForIP(huint128_t ipv4) const;
Expand Down
21 changes: 11 additions & 10 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,8 @@ namespace llarp
m.respond(serialize_response({{"STATUS", PathBuildMessage::OK}}), false);
return;
}
// rotate our frame to the end of the list and forward upstream

// rotate our frame to the end of the list and forward upstream
auto payload_list = oxenc::bt_deserialize<oxenc::bt_list>(payload);
payload_list.splice(payload_list.end(), payload_list, payload_list.begin());

Expand Down Expand Up @@ -1432,12 +1432,13 @@ namespace llarp
(CryptoManager::instance()->verify(pubkey, to_usv(dict_data), sig)
and _router.exitContext().ObtainNewExit(PubKey{pubkey.data()}, rx_id, flag != 0));

m.respond(ObtainExit::sign_and_serialize_response(_router.identity(), tx_id), not success);
m.respond(
ObtainExitMessage::sign_and_serialize_response(_router.identity(), tx_id), not success);
}
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{"STATUS", ObtainExit::EXCEPTION}}), true);
m.respond(serialize_response({{"STATUS", ObtainExitMessage::EXCEPTION}}), true);
throw;
}
}
Expand Down Expand Up @@ -1505,16 +1506,16 @@ namespace llarp
if (CryptoManager::instance()->verify(exit_ep->PubKey().data(), to_usv(dict_data), sig))
{
(exit_ep->UpdateLocalPath(transit_hop->info.rxID))
? m.respond(UpdateExit::sign_and_serialize_response(_router.identity(), tx_id))
: m.respond(serialize_response({{"STATUS", UpdateExit::UPDATE_FAILED}}), true);
? m.respond(UpdateExitMessage::sign_and_serialize_response(_router.identity(), tx_id))
: m.respond(serialize_response({{"STATUS", UpdateExitMessage::UPDATE_FAILED}}), true);
}
// If we fail to verify the message, no-op
}
}
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{"STATUS", UpdateExit::EXCEPTION}}), true);
m.respond(serialize_response({{"STATUS", UpdateExitMessage::EXCEPTION}}), true);
return;
}
}
Expand Down Expand Up @@ -1590,16 +1591,16 @@ namespace llarp
if (CryptoManager::instance()->verify(exit_ep->PubKey().data(), to_usv(dict_data), sig))
{
exit_ep->Close();
m.respond(CloseExit::sign_and_serialize_response(_router.identity(), tx_id));
m.respond(CloseExitMessage::sign_and_serialize_response(_router.identity(), tx_id));
}
}

m.respond(serialize_response({{"STATUS", CloseExit::UPDATE_FAILED}}), true);
m.respond(serialize_response({{"STATUS", CloseExitMessage::UPDATE_FAILED}}), true);
}
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{"STATUS", CloseExit::EXCEPTION}}), true);
m.respond(serialize_response({{"STATUS", CloseExitMessage::EXCEPTION}}), true);
return;
}
}
Expand Down Expand Up @@ -1635,7 +1636,7 @@ namespace llarp

if (path_ptr->SupportsAnyRoles(path::ePathRoleExit | path::ePathRoleSVC)
and CryptoManager::instance()->verify(_router.pubkey(), to_usv(dict_data), sig))
path_ptr->close_exit();
path_ptr->mark_exit_closed();
}
catch (const std::exception& e)
{
Expand Down
15 changes: 7 additions & 8 deletions llarp/messages/exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace llarp
- nuke seq_no's
*/

namespace ObtainExit
namespace ObtainExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;

// flag: 0 = Exit, 1 = Snode
inline std::string
sign_and_serialize(SecretKey sk, uint64_t flag, std::string pubkey, std::string tx_id)
sign_and_serialize(SecretKey sk, uint64_t flag, std::string tx_id)
{
oxenc::bt_list_producer btlp;
std::string sig(64, '\0');
Expand All @@ -25,7 +25,6 @@ namespace llarp
auto btdp = btlp.append_dict();

btdp.append("E", flag);
btdp.append("I", pubkey);
btdp.append("T", tx_id);

if (not CryptoManager::instance()->sign(
Expand Down Expand Up @@ -62,9 +61,9 @@ namespace llarp
return std::move(btlp).str();
}

} // namespace ObtainExit
} // namespace ObtainExitMessage

namespace UpdateExit
namespace UpdateExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto UPDATE_FAILED = "EXIT UPDATE FAILED"sv;
Expand Down Expand Up @@ -114,9 +113,9 @@ namespace llarp
btlp.append(sig.data());
return std::move(btlp).str();
}
} // namespace UpdateExit
} // namespace UpdateExitMessage

namespace CloseExit
namespace CloseExitMessage
{
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto UPDATE_FAILED = "CLOSE EXIT FAILED"sv;
Expand Down Expand Up @@ -168,6 +167,6 @@ namespace llarp
btlp.append(sig.data());
return std::move(btlp).str();
}
} // namespace CloseExit
} // namespace CloseExitMessage

} // namespace llarp
5 changes: 1 addition & 4 deletions llarp/path/abstracthophandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace llarp
virtual bool
ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const = 0;

virtual void
virtual bool
send_path_control_message(
std::string method,
std::string body,
Expand All @@ -62,9 +62,6 @@ namespace llarp
virtual llarp_time_t
LastRemoteActivityAt() const = 0;

virtual bool
HandleLRSM(uint64_t status, std::array<EncryptedFrame, 8>& frames, Router* r) = 0;

uint64_t
NextSeqNo()
{
Expand Down

0 comments on commit 99a1036

Please sign in to comment.