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
|
|
|
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <crypto/encrypted.hpp>
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/types.hpp>
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/message.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
#include <routing/message.hpp>
|
2019-04-22 18:35:19 +00:00
|
|
|
#include <service/identity.hpp>
|
|
|
|
#include <service/info.hpp>
|
|
|
|
#include <service/intro.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/handler.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.hpp>
|
|
|
|
#include <util/time.hpp>
|
2019-04-23 14:28:59 +00:00
|
|
|
#include <path/pathset.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
struct llarp_threadpool;
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
class Logic;
|
|
|
|
|
2019-04-10 13:19:32 +00:00
|
|
|
namespace path
|
|
|
|
{
|
|
|
|
/// forward declare
|
|
|
|
struct Path;
|
|
|
|
} // namespace path
|
|
|
|
|
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
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
constexpr ProtocolType eProtocolControl = 0UL;
|
|
|
|
constexpr ProtocolType eProtocolTrafficV4 = 1UL;
|
|
|
|
constexpr ProtocolType eProtocolTrafficV6 = 2UL;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
/// inner message
|
2019-05-24 02:01:36 +00:00
|
|
|
struct ProtocolMessage
|
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();
|
2019-06-11 16:44:05 +00:00
|
|
|
ProtocolType proto = eProtocolTrafficV4;
|
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;
|
|
|
|
ConvoTag tag;
|
2019-05-24 02:01:36 +00:00
|
|
|
uint64_t seqno = 0;
|
|
|
|
uint64_t version = LLARP_PROTO_VERSION;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val);
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
void
|
2019-02-01 01:58:06 +00:00
|
|
|
PutBuffer(const llarp_buffer_t& payload);
|
2018-08-09 19:02:17 +00:00
|
|
|
|
|
|
|
static void
|
2019-07-01 13:44:25 +00:00
|
|
|
ProcessAsync(path::Path_ptr p, PathID_t from,
|
|
|
|
std::shared_ptr< ProtocolMessage > self);
|
2019-05-22 16:20:03 +00:00
|
|
|
|
2019-05-22 16:20:50 +00:00
|
|
|
bool
|
|
|
|
operator<(const ProtocolMessage& other) const
|
2019-05-22 16:20:03 +00:00
|
|
|
{
|
|
|
|
return seqno < other.seqno;
|
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// outer message
|
2019-01-13 16:30:07 +00:00
|
|
|
struct ProtocolFrame final : public routing::IMessage
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2019-01-13 16:30:07 +00:00
|
|
|
using Encrypted_t = Encrypted< 2048 >;
|
|
|
|
PQCipherBlock C;
|
2018-12-20 16:49:05 +00:00
|
|
|
Encrypted_t D;
|
2019-03-08 16:00:45 +00:00
|
|
|
uint64_t R;
|
2019-01-13 16:30:07 +00:00
|
|
|
KeyExchangeNonce N;
|
|
|
|
Signature Z;
|
2019-03-08 15:33:49 +00:00
|
|
|
PathID_t F;
|
2019-01-13 16:30:07 +00:00
|
|
|
service::ConvoTag T;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2018-09-17 13:28:26 +00:00
|
|
|
ProtocolFrame(const ProtocolFrame& other)
|
2019-01-13 16:30:07 +00:00
|
|
|
: routing::IMessage()
|
2018-09-17 15:32:37 +00:00
|
|
|
, C(other.C)
|
|
|
|
, D(other.D)
|
2019-03-08 16:00:45 +00:00
|
|
|
, R(other.R)
|
2018-09-17 15:32:37 +00:00
|
|
|
, N(other.N)
|
|
|
|
, Z(other.Z)
|
2019-03-08 15:33:49 +00:00
|
|
|
, F(other.F)
|
2018-09-17 15:32:37 +00:00
|
|
|
, T(other.T)
|
2018-09-17 13:28:26 +00:00
|
|
|
{
|
|
|
|
S = other.S;
|
|
|
|
version = other.version;
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
ProtocolFrame() : routing::IMessage()
|
2018-09-17 16:12:42 +00:00
|
|
|
{
|
2018-12-29 15:44:25 +00:00
|
|
|
Clear();
|
2018-09-17 16:12:42 +00:00
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
~ProtocolFrame() override;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
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
|
2019-05-28 19:45:08 +00:00
|
|
|
EncryptAndSign(const ProtocolMessage& msg, const SharedSecret& sharedkey,
|
|
|
|
const Identity& localIdent);
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2019-03-08 16:00:45 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Sign(const Identity& localIdent);
|
2019-03-08 16:00:45 +00:00
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
bool
|
2019-07-09 13:47:24 +00:00
|
|
|
AsyncDecryptAndVerify(
|
|
|
|
std::shared_ptr< Logic > logic, path::Path_ptr fromPath,
|
|
|
|
const std::shared_ptr< llarp::thread::ThreadPool >& worker,
|
|
|
|
const Identity& localIdent, IDataHandler* handler) const;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
DecryptPayloadInto(const SharedSecret& 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
|
2019-02-01 01:58:06 +00:00
|
|
|
DecodeKey(const 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
|
|
|
|
2019-05-24 02:01:36 +00:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:32:37 +00:00
|
|
|
void
|
|
|
|
Clear() override
|
|
|
|
{
|
2018-12-29 15:44:25 +00:00
|
|
|
C.Zero();
|
2018-12-27 14:32:37 +00:00
|
|
|
D.Clear();
|
2019-03-08 15:33:49 +00:00
|
|
|
F.Zero();
|
2018-12-29 15:44:25 +00:00
|
|
|
T.Zero();
|
|
|
|
N.Zero();
|
|
|
|
Z.Zero();
|
2019-07-18 16:28:17 +00:00
|
|
|
R = 0;
|
|
|
|
version = LLARP_PROTO_VERSION;
|
2018-12-27 14:32:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Verify(const ServiceInfo& from) const;
|
2018-07-23 07:38:29 +00:00
|
|
|
|
|
|
|
bool
|
2019-02-18 23:58:12 +00:00
|
|
|
HandleMessage(routing::IMessageHandler* h,
|
|
|
|
AbstractRouter* r) const override;
|
2018-07-19 04:58:39 +00:00
|
|
|
};
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-09-10 18:04:30 +00:00
|
|
|
#endif
|