2018-07-19 04:58:39 +00:00
|
|
|
#ifndef LLARP_SERVICE_PROTOCOL_HPP
|
|
|
|
#define LLARP_SERVICE_PROTOCOL_HPP
|
2018-12-12 02:04:32 +00:00
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <bencode.hpp>
|
2018-12-12 00:38:58 +00:00
|
|
|
#include <crypto.hpp>
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/message.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <encrypted.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
#include <routing/message.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/Identity.hpp>
|
|
|
|
#include <service/Info.hpp>
|
|
|
|
#include <service/Intro.hpp>
|
|
|
|
#include <service/handler.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <time.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
class Logic;
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
namespace service
|
|
|
|
{
|
2018-09-21 13:52:10 +00:00
|
|
|
constexpr std::size_t MAX_PROTOCOL_MESSAGE_SIZE = 2048 * 2;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2018-11-22 23:59:03 +00:00
|
|
|
using ProtocolType = uint64_t;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
|
|
|
constexpr ProtocolType eProtocolText = 0UL;
|
|
|
|
constexpr ProtocolType eProtocolTraffic = 1UL;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
/// inner message
|
2018-11-05 11:27:12 +00:00
|
|
|
struct ProtocolMessage final : public IBEncodeMessage
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2018-08-09 19:02:17 +00:00
|
|
|
ProtocolMessage(const ConvoTag& tag);
|
2018-07-22 23:14:29 +00:00
|
|
|
ProtocolMessage();
|
2018-07-19 04:58:39 +00:00
|
|
|
~ProtocolMessage();
|
2018-09-10 18:04:30 +00:00
|
|
|
ProtocolType proto = eProtocolTraffic;
|
2018-07-19 04:58:39 +00:00
|
|
|
llarp_time_t queued = 0;
|
|
|
|
std::vector< byte_t > payload;
|
2018-07-22 23:14:29 +00:00
|
|
|
Introduction introReply;
|
|
|
|
ServiceInfo sender;
|
2018-08-09 19:02:17 +00:00
|
|
|
IDataHandler* handler = nullptr;
|
2018-09-14 13:43:42 +00:00
|
|
|
/// local path we got this message from
|
|
|
|
PathID_t srcPath;
|
2018-08-09 19:02:17 +00:00
|
|
|
ConvoTag tag;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const override;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
PutBuffer(llarp_buffer_t payload);
|
2018-08-09 19:02:17 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ProcessAsync(void* user);
|
2018-07-22 23:14:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// outer message
|
2018-11-05 11:27:12 +00:00
|
|
|
struct ProtocolFrame final : public llarp::routing::IMessage
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2018-08-13 23:22:31 +00:00
|
|
|
llarp::PQCipherBlock C;
|
2018-07-22 23:14:29 +00:00
|
|
|
llarp::Encrypted D;
|
|
|
|
llarp::KeyExchangeNonce N;
|
|
|
|
llarp::Signature Z;
|
2018-08-09 19:02:17 +00:00
|
|
|
llarp::service::ConvoTag T;
|
|
|
|
|
2018-09-17 13:28:26 +00:00
|
|
|
ProtocolFrame(const ProtocolFrame& other)
|
2018-09-17 15:32:37 +00:00
|
|
|
: llarp::routing::IMessage()
|
|
|
|
, C(other.C)
|
|
|
|
, D(other.D)
|
|
|
|
, N(other.N)
|
|
|
|
, Z(other.Z)
|
|
|
|
, T(other.T)
|
2018-09-17 13:28:26 +00:00
|
|
|
{
|
|
|
|
S = other.S;
|
|
|
|
version = other.version;
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:12:42 +00:00
|
|
|
ProtocolFrame() : llarp::routing::IMessage()
|
|
|
|
{
|
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
|
|
|
~ProtocolFrame();
|
|
|
|
|
2018-09-17 15:32:37 +00:00
|
|
|
bool
|
|
|
|
operator==(const ProtocolFrame& other) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const ProtocolFrame& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:17:18 +00:00
|
|
|
ProtocolFrame&
|
|
|
|
operator=(const ProtocolFrame& other);
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2018-12-11 00:53:11 +00:00
|
|
|
EncryptAndSign(llarp::Crypto* c, const ProtocolMessage& msg,
|
2018-08-13 23:22:31 +00:00
|
|
|
const byte_t* sharedkey, const Identity& localIdent);
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
bool
|
2018-12-11 00:53:11 +00:00
|
|
|
AsyncDecryptAndVerify(llarp::Logic* logic, llarp::Crypto* c,
|
2018-09-18 21:32:17 +00:00
|
|
|
const PathID_t& srcpath, llarp_threadpool* worker,
|
2018-08-13 23:22:31 +00:00
|
|
|
const Identity& localIdent,
|
2018-08-09 19:02:17 +00:00
|
|
|
IDataHandler* handler) const;
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2018-12-11 00:53:11 +00:00
|
|
|
DecryptPayloadInto(llarp::Crypto* c, const byte_t* sharedkey,
|
2018-08-13 23:22:31 +00:00
|
|
|
ProtocolMessage& into) const;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
|
2018-07-20 04:50:28 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const override;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2018-12-11 00:53:11 +00:00
|
|
|
Verify(llarp::Crypto* c, const ServiceInfo& from) const;
|
2018-07-23 07:38:29 +00:00
|
|
|
|
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
HandleMessage(llarp::routing::IMessageHandler* h,
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router* r) const override;
|
2018-07-19 04:58:39 +00:00
|
|
|
};
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-09-10 18:04:30 +00:00
|
|
|
#endif
|