C3/Src/Core/Distributor.h

92 lines
3.9 KiB
C
Raw Permalink Normal View History

2019-09-02 10:28:14 +00:00
#pragma once
#include "Procedures.h"
#include "ProceduresG2X.h"
#include "RouteManager.h"
2020-03-05 15:30:50 +00:00
#include "Common/FSecure/Crypto/Crypto.hpp"
2019-09-02 10:28:14 +00:00
// Forward declarations.
2020-03-05 15:30:50 +00:00
namespace FSecure::C3
2019-09-02 10:28:14 +00:00
{
struct LogMessage;
namespace Core
{
struct DeviceBridge;
}
}
2020-03-05 15:30:50 +00:00
namespace FSecure::C3::Core
2019-09-02 10:28:14 +00:00
{
/// Relay's lowest layer - responsible for managing packet transmission.
struct Distributor : std::enable_shared_from_this<Distributor>, RouteManager, ProceduresN2N::RequestHandler, ProceduresS2G::RequestHandler
{
using LoggerCallback = Utils::LoggerCallback;
2019-09-02 10:28:14 +00:00
/// Destructor
virtual ~Distributor() = default;
2019-09-02 10:28:14 +00:00
/// Logs a message. Used by internal Relay mechanisms and attached Interfaces to report errors, warnings, informations and debug messages.
/// @param message information to log.
/// @param sender Interface reporting the message. If sender.IsNull() then the message comes from internal Relay mechanisms.
virtual void Log(LogMessage const& message, DeviceId sender = DeviceId{}) noexcept;
/// Callback fired to by a Channel when a C3 packet arrives.
/// @param packet full C3 packet to interpret.
/// @param sender Interface passing the packet.
/// @throws std::runtime_error.
virtual void OnPacketReceived(ByteView packet, std::shared_ptr<DeviceBridge> sender);
protected:
/// Expose all base classes `On` methods.
using ProceduresN2N::RequestHandler::On;
using ProceduresS2G::RequestHandler::On;
2019-09-02 10:28:14 +00:00
/// A protected ctor.
/// @param callbackOnLog callback fired whenever a new Log entry is being added.
/// @param decryptionKey Relay's private asymmetric key.
/// @param broadcastKey Network's symmetric key.
Distributor(LoggerCallback callbackOnLog, Crypto::PrivateKey const& decryptionKey, Crypto::SymmetricKey const& broadcastKey);
/// Checks whether particular Agent is banned.
/// @param agentId ID of the Agent to check.
virtual bool IsAgentBanned(AgentId agentId);
/// Fired when a N2N protocol packet arrives.
/// @param packet0 a buffer that contains whole packet.
/// @param sender a Channel that provided the packet.
virtual void OnProtocolN2N(ByteView packet0, std::shared_ptr<DeviceBridge> sender);
/// Fired when a S2G protocol packet arrives.
/// @param packet0 a buffer that contains whole packet.
/// @param sender a Channel that provided the packet.
virtual void OnProtocolS2G(ByteView packet0, std::shared_ptr<DeviceBridge> sender) = 0;
/// Fired when a S2G protocol packet arrives.
/// @param packet0 a buffer that contains whole packet.
/// @param sender a Channel that provided the packet.
virtual void OnProtocolG2A(ByteView packet0, std::shared_ptr<DeviceBridge> sender) = 0;
/// Fired when a S2G protocol packet arrives.
/// @param packet0 a buffer that contains whole packet.
/// @param sender a Channel that provided the packet.
virtual void OnProtocolG2R(ByteView packet0, std::shared_ptr<DeviceBridge> sender) = 0;
/// Encrypts a packet with the Network key and sends it through specified Channel. This is the last function to be called for a completely built outgoing packet.
/// @param packet plain-text packet to encrypt.
/// @param channel Interface used to send the packet.
/// @throws std::runtime_error.
virtual void LockAndSendPacket(ByteView packet, std::shared_ptr<DeviceBridge> channel);
/// Decrypts a packet with the Network key. This is the first thing called before parsing a packet from a Channel (even before QOS).
/// @param packet encrypted packet to decrypt.
/// @return decrypted packet.
/// @throws std::runtime_error.
virtual ByteVector UnlockPacket(ByteView packet);
protected:
LoggerCallback m_CallbackOnLog; ///< Callback fired whenever a new Log entry is being added.
Crypto::SymmetricKey m_BroadcastKey; ///< Network key.
Crypto::PrivateKey m_DecryptionKey; ///< Own key used to decrypt messages addressed to me.
};
}